有关学习过程记录

入门linux设备驱动的时候第一hello world模块

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}
static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);

module_exit(hello_exit);

为该模块编写的Makefile文件

ifeq ($(KERNELRELEASE),)   

    KERNELDIR ?= /lib/modules/$(shell uname -r)/build  

    PWD := $(shell pwd)

modules:

        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:

        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:

        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions \

Module.markers Module.symvers modules.order

.PHONY: modules modules_install clean

else

obj-m :=hello.o

endif

      第一次执行Makefle的时候,发现KERNELRELEASE为NULL, 则进入内核源码树所在的位置,读内核源码树根位置的Makefile,内核源码树的根Makefile对KERNELRELEASE变量进行了赋值。  由于make 后面没有目标,所以make会在Makefile中的第一个不是以TAB开头行的目标作为默认的目标执行。然后运行到了modules目标,执行$(MAKE) -C $(KERNELDIR) M=$(PWD) modules,

在执行$(MAKE) -C $(KERNELDIR) M=$(PWD) modules,过程中第二次读取该Makefile,此时KERNELRELEASE变量已被赋值,执行obj-m :=hello.o完成模块的编译。

           1.这个KERNELRELEASE也很令人困惑,它是什么呢?在模块所在目录下的Makefile中是没有定义这个变量的,所以else…endif这一段不起作用。如果把hello模块移动到内核源代码中。例如放到/usr/src/linux/driver/中, KERNELRELEASE就有定义。在/usr/src/linux/Makefile中有 KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)$(LOCALVERSION) 这时候,hello模块也不再是单独用make编译,而是在内核中用make modules进行 编译。用这种方式,该Makefile在单独编译和作为内核一部分编译时都能正常工作。

             2.这个obj-m := hello.o什么时候会执行到呢?

在执行: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules时,make 去/usr/src/linux/Makefile中寻找目标这时 KERNELRELEASE已经存在。 所以执行的是: obj-m:=hello.o

带参数的hello world模块

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

MODULE_LICENSE("Dual BSD/GPL");

static char *whom = "world";
static int howmany = 1;
module_param(howmany, int, S_IRUGO);
module_param(whom, charp, S_IRUGO);

static int hello_init(void)
{
        int i;
        for (i = 0; i < howmany; i++)
                printk(KERN_ALERT "(%d) Hello, %s\n", i, whom);
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");

}

module_init(hello_init);

module_exit(hello_exit);

编译过程同hello world不带参数时候一样,当多一个参数的问题,

module_param(name, type, perm),通常放在源文件的末尾。

name :变量的名称、

type:变量类型、

perm:用于sysfs入口项的访问许可掩码。

内核支持的模块参数类型:

bool           //布尔值(true/false),

invbool      //invbool的值取bool的反值。

charp        //字符指针,内核会为用户提供的字符串分配内存,并设置指针

int

long

short

uint            //无符号值

ulong

ushort

访问许可掩码定义在文件<linux/stat.h>中,具体内容为

#define S_IRWXU   00700     //用户具有读写执行权限
#define S_IRUSR   00400
#define S_IWUSR  00200
#define S_IXUSR   00100

#define S_IRWXG   00070   //用户所在组具有读写执行权限
#define S_IRGRP   00040
#define S_IWGRP   00020
#define S_IXGRP    00010

#define S_IRWXO   00007   //其它用户具有读写执行权限
#define S_IROTH   00004
#define S_IWOTH   00002
#define S_IXOTH    00001

#define S_IRWXUGO    (S_IRWXU|S_IRWXG|S_IRWXO)       //用户、用户所在组、其它用户和组 具有读写执行权限
#define S_IALLUGO      (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO          (S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO         (S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO           (S_IXUSR|S_IXGRP|S_IXOTH)

使用过程中常使用 “0600”许可掩码,如其为0则不会有相应的sysfs访问入口项。

数组参数:module_param_array(name,type,num,perm);

name:数组名,type:数组元素类型,num:会被设置为用户提供的值的个数,这个值不可超过数组大小。perm:访问许可值。

有关 seq_file 调试接口

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>

MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");

/*
 * The sequence iterator functions.  The position as seen by the
 * filesystem is just the count that we return.
 */
static void *ct_seq_start(struct seq_file *s, loff_t *pos)
{
    loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
    if (!spos)
        return NULL;
    *spos = *pos;
    return spos;
}

static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
    loff_t *spos = (loff_t *) v;
    *pos = ++(*spos);
    return spos;
}

static void ct_seq_stop(struct seq_file *s, void *v)
{
    kfree (v);
}

/*
 * The show function.
 */
static int ct_seq_show(struct seq_file *s, void *v)
{
    loff_t *spos = (loff_t *) v;
    seq_printf(s, "%Ld\n", *spos);
    return 0;
}

/*
 * Tie them all together into a set of seq_operations.
 */
static struct seq_operations ct_seq_ops = {
    .start = ct_seq_start,
    .next  = ct_seq_next,
    .stop  = ct_seq_stop,
    .show  = ct_seq_show
};


/*
 * Time to set up the file operations for our /proc file.  In this case,
 * all we need is an open function which sets up the sequence ops.
 */

static int ct_open(struct inode *inode, struct file *file)
{
    return seq_open(file, &ct_seq_ops);
};

/*
 * The file operations structure contains our open function along with
 * set of the canned seq_ ops.
 */
static struct file_operations ct_file_ops = {
    .owner   = THIS_MODULE,
    .open    = ct_open,
    .read    = seq_read,
    .llseek  = seq_lseek,
    .release = seq_release
};
    
    
/*
 * Module setup and teardown.
 */

static int ct_init(void)
{
    struct proc_dir_entry *entry;

    entry = create_proc_entry("sequence", 0, NULL);
    if (entry)
        entry->proc_fops = &ct_file_ops;
    return 0;
}

static void ct_exit(void)
{
    remove_proc_entry("sequence", NULL);
}

module_init(ct_init);

module_exit(ct_exit);

有关完成量(completion)作用允许一个线程告诉另一线程某个工作已经完成,使用例子

/*
 * complete.c -- the writers awake the readers
 */

#include <linux/module.h>
#include <linux/init.h>

#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/fs.h>    
#include <linux/types.h>
#include <linux/completion.h>

MODULE_LICENSE("Dual BSD/GPL");

static int complete_major = 0;

DECLARE_COMPLETION(comp);

ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
    printk(KERN_DEBUG "process %i (%s) going to sleep\n",
            current->pid, current->comm);
    wait_for_completion(&comp);
    printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
    return 0; /* EOF */
}

ssize_t complete_write (struct file *filp, const char __user *buf, size_t count,
        loff_t *pos)
{
    printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
            current->pid, current->comm);
    complete(&comp);
    return count; /* succeed, to avoid retrial */
}


struct file_operations complete_fops = {
    .owner = THIS_MODULE,
    .read =  complete_read,
    .write = complete_write,
};


int complete_init(void)
{
    int result;

    /*
     * Register your major, and accept a dynamic number
     */
    result = register_chrdev(complete_major, "complete", &complete_fops);
    if (result < 0)
        return result;
    if (complete_major == 0)
        complete_major = result; /* dynamic */
    return 0;
}

void complete_cleanup(void)
{
    unregister_chrdev(complete_major, "complete");
}

module_init(complete_init);
module_exit(complete_cleanup);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值