打印

scull字符设备之main.c

[复制链接]
154|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
求关注啊|  楼主 | 2018-10-3 20:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
AI, se, ev, ST, AN
/*

* main.c -- the bare scull char module

*

* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet

* Copyright (C) 2001 O'Reilly & Associates

*

* The source code in this file can be freely used, adapted,

* and redistributed in source or binary form, so long as an

* acknowledgment appears in derived source files.  The citation

* should list that the code comes from the book "Linux Device

* Drivers" by Alessandro Rubini and Jonathan Corbet, published

* by O'Reilly & Associates.   No warranty is attached;

* we cannot take responsibility for errors or fitness for use.

*

*/

#include <linux/config.h>

#include <linux/module.h>

#include <linux/moduleparam.h>

#include <linux/init.h>

#include <linux/kernel.h> /* printk() */

#include <linux/slab.h>  /* kmalloc() */

#include <linux/fs.h>  /* everything... */

#include <linux/errno.h> /* error codes */

#include <linux/types.h> /* size_t */

#include <linux/proc_fs.h>

#include <linux/fcntl.h> /* O_ACCMODE */

#include <linux/seq_file.h>

#include <linux/cdev.h>

#include <asm/system.h>  /* cli(), *_flags */

#include <asm/uaccess.h> /* copy_*_user */

#include "scull.h"  /* local definitions */

/*

* Our parameters which can be set at load time.

*/

int scull_major =   SCULL_MAJOR;

int scull_minor =   0;

int scull_nr_devs = SCULL_NR_DEVS; /* number of bare scull devices */

int scull_quantum = SCULL_QUANTUM;

int scull_qset =    SCULL_QSET;

// 模块参数

module_param(scull_major, int, S_IRUGO);

module_param(scull_minor, int, S_IRUGO);

module_param(scull_nr_devs, int, S_IRUGO);

module_param(scull_quantum, int, S_IRUGO);

module_param(scull_qset, int, S_IRUGO);

// 模块描述性定义(MODULE_ 声明)

MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");

MODULE_LICENSE("Dual BSD/GPL");

// scull字符设备集

struct scull_dev *scull_devices; /* allocated in scull_init_module */



/*

* Empty out the scull device; must be called with the device

* semaphore held.

*/

// 释放整个数据区,简单遍历列表并且释放它发现的任何量子和量子集以及量子指针数组

// 在scull_open中,文件只写时调用

// 调用这个函数时必须持有信号量

int scull_trim(struct scull_dev *dev)

{

struct scull_qset *next, *dptr;

// 量子集大小

int qset = dev->qset;   /* "dev" is not-null */

int i;

// 遍历多个量子集,dev->data 指向第一个量子集

for (dptr = dev->data; dptr; dptr = next) { /* all the list items */

  if (dptr->data) {// 量子集中有数据

   for (i = 0; i < qset; i++)// 遍历释放当前量子集中的每一个量子,量子集大小为qset

    kfree(dptr->data);

   kfree(dptr->data);// 释放量子指针数组

   dptr->data = NULL;// 指针置为NULL

  }

  next = dptr->next;// next获取下一个量子集

  kfree(dptr);// 释放当前量子集 <链表结点>

}

dev->size = 0;

dev->quantum = scull_quantum;

dev->qset = scull_qset;

dev->data = NULL;

return 0;

}

// proc 调试用

#ifdef SCULL_DEBUG /* use proc only if debugging */

/*

* The proc filesystem: function to read and entry

*/

// 在 /proc 里实现文件,在文件被读时产生数据

// 当一个进程读 /proc 文件,内核分配了一页内存,驱动可以写入数据返回用户空间

// buf:写数据的缓冲区、start:数据写在页中的位置

// eof:必须被驱动设置,表示写数据结束、data:传递私有数据

int scull_read_procmem(char *buf, char **start, off_t offset,

                   int count, int *eof, void *data)

{

int i, j, len = 0;

int limit = count - 80; /* Don't print more than this */

// 遍历每个设备,输出总字节数小于limit

for (i = 0; i < scull_nr_devs && len <= limit; i++) {

  struct scull_dev *d = &scull_devices;

  struct scull_qset *qs = d->data;

  if (down_interruptible(&d->sem))// 获取信号量

   return -ERESTARTSYS;

  // 输出当前是第几个scull设备,量子集大小,量子大小,设备大小

  len += sprintf(buf+len,"\nDevice %i: qset %i, q %i, sz %li\n",

    i, d->qset, d->quantum, d->size);

  for (; qs && len <= limit; qs = qs->next) { /* scan the list */

   // 输出当前量子集和量子集中的数据在内存中的位置

   len += sprintf(buf + len, "  item at %p, qset at %p\n",

     qs, qs->data);

   // 如果最后一个量子集有数据

   if (qs->data && !qs->next) /* dump only the last item */

    for (j = 0; j < d->qset; j++) {

     // 如果量子不为空

     if (qs->data[j])

      // 输出最后一个量子集中每个量子对应的地址

      len += sprintf(buf + len,

        "    % 4i: %8p\n",

        j, qs->data[j]);

    }

  }

  up(&scull_devices.sem);// 释放信号量

}

*eof = 1;// 写结束

return len;

}



/*

* For now, the seq_file implementation will exist in parallel.  The

* older read_procmem function should maybe go away, though.

*/

/*

* Here are our sequence iteration methods.  Our "position" is

* simply the device number.

*/

// s总被忽略,pos指从哪儿开始读,具体意义依赖于实现

// seq_file典型的实现是遍历一感兴趣的数据序列,pos就用来指示序列中的下一个元素

// 在scull中,pos简单地作为scull_devices数组的索引

static void *scull_seq_start(struct seq_file *s, loff_t *pos)

{

if (*pos >= scull_nr_devs)

  return NULL;   /* No more to read */

return scull_devices + *pos;// 返回索引号是pos的scull设备

}

// 返回下一个scull设备

static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos)

{

(*pos)++;

if (*pos >= scull_nr_devs)

  return NULL;

return scull_devices + *pos;

}

static void scull_seq_stop(struct seq_file *s, void *v)

{

/* Actually, there's nothing to do here */

}

// 输出迭代器v生成的数据到用户空间

static int scull_seq_show(struct seq_file *s, void *v)

{

struct scull_dev *dev = (struct scull_dev *) v;

struct scull_qset *d;

int i;

if (down_interruptible(&dev->sem))

  return -ERESTARTSYS;

// 输出,相当于用于空间的printf,返回非0值表示缓冲区满,超出的数据被丢弃

seq_printf(s, "\nDevice %i: qset %i, q %i, sz %li\n",

   (int) (dev - scull_devices), dev->qset,

   dev->quantum, dev->size);

// 遍历设备的每一个量子集

for (d = dev->data; d; d = d->next) { /* scan the list */

  // 输出量子集地址,量子集中的数据地址

  seq_printf(s, "  item at %p, qset at %p\n", d, d->data);

  // 如果最后一个量子集有数据

  if (d->data && !d->next) /* dump only the last item */

   for (i = 0; i < dev->qset; i++) {

    // 如果量子不为空

    if (d->data)

     // 输出最后一个量子集中每个量子对应的地址

     seq_printf(s, "    % 4i: %8p\n",

       i, d->data);

   }

}

up(&dev->sem);

return 0;

}



/*

* Tie the sequence operators up.

*/

// seq_file 的操作集

// seq_file(实现一种大的 /proc 文件)

static struct seq_operations scull_seq_ops = {

.start = scull_seq_start,

.next  = scull_seq_next,

.stop  = scull_seq_stop,

.show  = scull_seq_show

};

/*

* Now to implement the /proc file we need only make an open

* method which sets up the sequence operators.

*/

// 打开 /proc 文件,在这里也就是初始化seq_file

static int scull_proc_open(struct inode *inode, struct file *file)

{

return seq_open(file, &scull_seq_ops);// 连接file和seq_file

}

/*

* Create a set of file operations for our proc file.

*/

// /proc 文件的操作集

static struct file_operations scull_proc_ops = {

.owner   = THIS_MODULE,

.open    = scull_proc_open,

.read    = seq_read,

.llseek  = seq_lseek,

.release = seq_release

};



/*

* Actually create (and remove) the /proc file(s).

*/

// 建立通过proc方式debug时需要的 /proc 文件

static void scull_create_proc(void)

{

struct proc_dir_entry *entry;

create_proc_read_entry("scullmem", 0 /* default mode */,// 要创建 /proc 文件名字、按照系统默认的掩码创建文件

   NULL /* parent dir */, scull_read_procmem,// 缺省在 /proc 目录创建、指定 read_proc 方法

   NULL /* client data */);// data 指针



// create_proc_entry 同样用来建立 /proc 文件,但较 create_proc_read_entry 更为底层一些

entry = create_proc_entry("scullseq", 0, NULL);// 名字,掩码,父目录“/proc”

if (entry)

  entry->proc_fops = &scull_proc_ops;

}

// 移除创建的 /proc 文件

static void scull_remove_proc(void)

{

/* no problem if it was not registered */

// 移除一个proc_dir_entry, 如果这个结构还在使用,设置deleted标志,返回

remove_proc_entry("scullmem", NULL /* parent dir */);

remove_proc_entry("scullseq", NULL);

}



#endif /* SCULL_DEBUG */





/*

* Open and close

*/

// 打开设备:文件私有数据,设置成对应的scull_dev

int scull_open(struct inode *inode, struct file *filp)

{

struct scull_dev *dev; /* device information */

// 通过结构中的成员获取结构本身的地址

dev = container_of(inode->i_cdev, struct scull_dev, cdev);

// 填充 private_data 域

filp->private_data = dev; /* for other methods */

/* now trim to 0 the length of the device if open was write-only */

if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {

  if (down_interruptible(&dev->sem))

   return -ERESTARTSYS;

  scull_trim(dev); /* ignore errors */

  up(&dev->sem);

}

return 0;          /* success */

}

// file_operations 中的 release

int scull_release(struct inode *inode, struct file *filp)

{

return 0;

}

/*

* Follow the list

*/

// 返回设备dev的第n个量子集,量子集不够n个就申请新的

struct scull_qset *scull_follow(struct scull_dev *dev, int n)

{

struct scull_qset *qs = dev->data;// 当前设备的量子集

        /* Allocate first qset explicitly if need be */

        // 如果当前设备还没有量子集,就显式分配第一个量子集

if (! qs) {

  // kmalloc() 内核模块中,动态分配连续的物理地址,用于小内存分配

  // size_t size 要分配的块的大小

  // int flags 在当前进程缺少内存时,可以睡眠来等待一页

  // GFP_KERNEL 分配内存的函数,必须可重入,且不能在原子上下文中运行

  qs = dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);

  if (qs == NULL)

   return NULL;  /* Never mind */

  // 对分配的量子集清零,为以后分配空间作准备

  memset(qs, 0, sizeof(struct scull_qset));

}

/* Then follow the list */

// 遍历当前设备的量子集链表n步,量子集不够就申请新的 <意味着没有数据可读>

while (n--) {

  if (!qs->next) {

   qs->next = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);

   if (qs->next == NULL)//分配不成功,会在调用本函数的函数中处理出错,所以不用管

    return NULL;  /* Never mind */

   memset(qs->next, 0, sizeof(struct scull_qset));

  }

  qs = qs->next;

  continue;// 多此一举?

}

return qs;// 第n个量子集

}

/*

* Data management: read and write

*/

ssize_t scull_read(struct file *filp, char __user *buf, size_t count,

                loff_t *f_pos)

{

struct scull_dev *dev = filp->private_data;// struct_dev 结构指针存储在此成员中

struct scull_qset *dptr; /* the first listitem */

int quantum = dev->quantum, qset = dev->qset;// 量子所占字节数、量子集中的量子数

int itemsize = quantum * qset; /* how many bytes in the listitem */// 一个量子集的字节数

int item, s_pos, q_pos, rest;

ssize_t retval = 0;// 返回值

// 以可中断睡眠方式获取信号量

if (down_interruptible(&dev->sem))

  return -ERESTARTSYS;

//读位置不能超过设备实际大小

if (*f_pos >= dev->size)

  goto out;

// 要读的count超出了size,裁断count

if (*f_pos + count > dev->size)

  count = dev->size - *f_pos;

/* find listitem, qset index, and offset in the quantum */

// 在量子集、量子中定位读写位置

item = (long)*f_pos / itemsize;// 第几个量子集

rest = (long)*f_pos % itemsize;// 在量子集中的偏移量

s_pos = rest / quantum; q_pos = rest % quantum;// 第几个量子,在量子中的偏移量

/* follow the list up to the right position (defined elsewhere) */

// 获取要读的量子集指针

dptr = scull_follow(dev, item);// 获取设备dev的第item个量子集

// 没有量子集,量子集中没有data,没有第s_pos个量子

if (dptr == NULL || !dptr->data || ! dptr->data[s_pos])

  goto out; /* don't fill holes */

/* read only up to the end of this quantum */

// 只在一个量子中读:如果count超出当前量子,截断count

if (count > quantum - q_pos)

  count = quantum - q_pos;



// 将读位置的内容复制到用户空间buf,共复制count字节

if (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) {

  retval = -EFAULT;

  goto out;

}

*f_pos += count;//调整数据读、写位置

retval = count;

  out:// 出错处理

up(&dev->sem);

return retval;

}

ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,

                loff_t *f_pos)

{

struct scull_dev *dev = filp->private_data;

struct scull_qset *dptr;

int quantum = dev->quantum, qset = dev->qset;// 量子、量子集大小

int itemsize = quantum * qset;// 一个量子集的字节数

int item, s_pos, q_pos, rest;

ssize_t retval = -ENOMEM; /* value used in "goto out" statements */

if (down_interruptible(&dev->sem))

  return -ERESTARTSYS;

/* find listitem, qset index and offset in the quantum */

// 计算有关量子集、量子的索引以及偏移

item = (long)*f_pos / itemsize;

rest = (long)*f_pos % itemsize;

s_pos = rest / quantum; q_pos = rest % quantum;

/* follow the list up to the right position */

// 获取要写入数据的量子集

dptr = scull_follow(dev, item);// 获取设备dev的第item个量子集

if (dptr == NULL)

  goto out;

// 如果该量子集为NULL,申请一块新内存

if (!dptr->data) {

  // 分配存储qset个字符指针的空间

  dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);

  if (!dptr->data)

   goto out;

  memset(dptr->data, 0, qset * sizeof(char *));

}

// 如果第s_pos个量子是NULL,申请一块新内存

if (!dptr->data[s_pos]) {

  // 分配存储quantum个字节的空间

  dptr->data[s_pos] = kmalloc(quantum/* *sizeof(char) */, GFP_KERNEL);

  if (!dptr->data[s_pos])

   goto out;

}

/* write only up to the end of this quantum */

if (count > quantum - q_pos)

  count = quantum - q_pos;



// 从用户空间拷贝数据到内核空间,失败返回没有拷贝的字节数,成功返回0

if (copy_from_user(dptr->data[s_pos]+q_pos, buf, count)) {

  retval = -EFAULT;

  goto out;

}

*f_pos += count;

retval = count;

        /* update the size */

        // 更新设备大小

if (dev->size < *f_pos)

  dev->size = *f_pos;

  out://出错处理

up(&dev->sem);

return retval;

}

/*

* The ioctl() implementation

*/

// ioctl 方法

int scull_ioctl(struct inode *inode, struct file *filp,// 设备文件

                 unsigned int cmd, unsigned long arg)// 功能号,值或者用户空间指针

{

int err = 0, tmp;

int retval = 0;

   

/*

  * extract the type and number bitfields, and don't decode

  * wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()

  */

// 对错误的命令,返回 -ENOTTY

if (_IOC_TYPE(cmd) != SCULL_IOC_MAGIC) return -ENOTTY;

if (_IOC_NR(cmd) > SCULL_IOC_MAXNR) return -ENOTTY;

/*

  * the direction is a bitmask, and VERIFY_WRITE catches R/W

  * transfers. `Type' is user-oriented, while

  * access_ok is kernel-oriented, so the concept of "read" and

  * "write" is reversed

  */

// 如果该ioctl为了读数据,检查当前进程是否可写arg(写到用户空间,用户就读到数据了)

// 如果为了写数据,检查arg是否可读

if (_IOC_DIR(cmd) & _IOC_READ)

  err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));

else if (_IOC_DIR(cmd) & _IOC_WRITE)

  err =  !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));

if (err) return -EFAULT;

switch(cmd) {

  

   // 重置量子集、量子大小

   case SCULL_IOCRESET:

  scull_quantum = SCULL_QUANTUM;

  scull_qset = SCULL_QSET;

  break;

         

          // 设置量子大小,arg是指向量子大小值的指针

   case SCULL_IOCSQUANTUM: /* Set: arg points to the value */

  if (! capable (CAP_SYS_ADMIN))// 检查是否具有系统管理权限

   return -EPERM;

  // 取arg所指内容,赋值给scull_quantum

         // __get_user() 从用户空间获取一个简单变量,基本不做检查

  retval = __get_user(scull_quantum, (int __user *)arg);

  break;



   // 设置量子大小, arg是值

   case SCULL_IOCTQUANTUM: /* Tell: arg is the value */

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  scull_quantum = arg;

  break;

   // 获取量子大小,arg是指针

   case SCULL_IOCGQUANTUM: /* Get: arg is pointer to result */

    retval = __put_user(scull_quantum, (int __user *)arg);

  break;



   // 查询量子大小,返回值

   case SCULL_IOCQQUANTUM: /* Query: return it (it's positive) */

  return scull_quantum;

   // 交换量子大小,指针:按arg指向值设置量子大小,当前量子大小保存到arg指向空间

   case SCULL_IOCXQUANTUM: /* eXchange: use arg as pointer */

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  tmp = scull_quantum;

  retval = __get_user(scull_quantum, (int __user *)arg);

  if (retval == 0)

   retval = __put_user(tmp, (int __user *)arg);

  break;

   // 交换两字大小,值

   case SCULL_IOCHQUANTUM: /* sHift: like Tell + Query */

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  tmp = scull_quantum;

  scull_quantum = arg;

  return tmp;

        

          // 量子集大小,和上面量子功能类似

   case SCULL_IOCSQSET:

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  retval = __get_user(scull_qset, (int __user *)arg);

  break;

   case SCULL_IOCTQSET:

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  scull_qset = arg;

  break;

   case SCULL_IOCGQSET:

  retval = __put_user(scull_qset, (int __user *)arg);

  break;

   case SCULL_IOCQQSET:

  return scull_qset;

   case SCULL_IOCXQSET:

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  tmp = scull_qset;

  retval = __get_user(scull_qset, (int __user *)arg);

  if (retval == 0)

   retval = put_user(tmp, (int __user *)arg);

  break;

   case SCULL_IOCHQSET:

  if (! capable (CAP_SYS_ADMIN))

   return -EPERM;

  tmp = scull_qset;

  scull_qset = arg;

  return tmp;

        /*

         * The following two change the buffer size for scullpipe.

         * The scullpipe device uses this same ioctl method, just to

         * write less code. Actually, it's the same driver, isn't it?

         */

   // 修改 scull_pipe 设备缓冲大小

   case SCULL_P_IOCTSIZE:

  scull_p_buffer = arg;

  break;

   // 返回 scull_pipe 设备缓冲大小

   case SCULL_P_IOCQSIZE:

  return scull_p_buffer;



   default:  /* redundant, as cmd was checked against MAXNR */

  return -ENOTTY;

}

return retval;

}



/*

* The "extended" operations -- only seek

*/

loff_t scull_llseek(struct file *filp, loff_t off, int whence)//设备文件,偏移,seek方式

{

struct scull_dev *dev = filp->private_data;

loff_t newpos;

switch(whence) {

// 文件开头

   case 0: /* SEEK_SET */

  newpos = off;

  break;

// 当前位置

   case 1: /* SEEK_CUR */

  newpos = filp->f_pos + off;

  break;

// 文件末尾

   case 2: /* SEEK_END */

  newpos = dev->size + off;

  break;

// 其它

   default: /* can't happen */

  return -EINVAL;

}

if (newpos < 0) return -EINVAL;

filp->f_pos = newpos;

return newpos;

}



// scull 设备方法

struct file_operations scull_fops = {

.owner =    THIS_MODULE,

.llseek =   scull_llseek,

.read =     scull_read,

.write =    scull_write,

.ioctl =    scull_ioctl,

.open =     scull_open,

.release =  scull_release,

};

/*

* Finally, the module stuff

*/

/*

* The cleanup function is used to handle initialization failures as well.

* Thefore, it must be careful to work correctly even if some of the items

* have not been initialized

*/

// 模块清理函数

void scull_cleanup_module(void)

{

int i;

dev_t devno = MKDEV(scull_major, scull_minor);// MKDEV 把主次设备号合成为一个dev_t结构

/* Get rid of our char dev entries */

// 清除scull_devices数组中的所有字符设备

if (scull_devices) {

  for (i = 0; i < scull_nr_devs; i++) {

   scull_trim(scull_devices + i);// 释放数据区

   cdev_del(&scull_devices.cdev);// 移除cdev

  }

  kfree(scull_devices); // 释放scull_devices本身

}

// 如果使用了 /proc 来debug,移除创建的 /proc 文件

#ifdef SCULL_DEBUG /* use proc only if debugging */

scull_remove_proc();

#endif

/* cleanup_module is never called if registering failed */

// 注销scull_nr_devs个设备号,从devno开始

unregister_chrdev_region(devno, scull_nr_devs);

/* and call the cleanup functions for friend devices */

// 清除其它友设备

scull_p_cleanup();

scull_access_cleanup();

}



/*

* Set up the char_dev structure for this device.

*/

// 建立 char_dev 结构

static void scull_setup_cdev(struct scull_dev *dev, int index)

{

int err, devno = MKDEV(scull_major, scull_minor + index);

   

cdev_init(&dev->cdev, &scull_fops);// 初始化已分配到的结构

dev->cdev.owner = THIS_MODULE;

dev->cdev.ops = &scull_fops;// 多此一举?

// 添加字符设备dev->cdev,立即生效

err = cdev_add (&dev->cdev, devno, 1);//devno:第一个设备号

/* Fail gracefully if need be */

if (err)

  printk(KERN_NOTICE "Error %d adding scull%d", err, index);

}

// 模块初始化函数

int scull_init_module(void)

{

int result, i;

dev_t dev = 0;

/*

* Get a range of minor numbers to work with, asking for a dynamic

* major unless directed otherwise at load time.

*/

  // 申请设备号:获取一系列次设备号, 如果在加载时没有指定主设备号就动态申请一个

if (scull_major) {

  dev = MKDEV(scull_major, scull_minor);

  // register_chrdev_region 用于已知起始设备的设备号的情况

  result = register_chrdev_region(dev, scull_nr_devs, "scull");

} else {

  // alloc_chrdev_region 用于设备号未知,向系统动态申请未被占用的设备号的情况

  result = alloc_chrdev_region(&dev, scull_minor, scull_nr_devs,

    "scull");

  scull_major = MAJOR(dev);// 获取主设备号

}

if (result < 0) {

  printk(KERN_WARNING "scull: can't get major %d\n", scull_major);

  return result;

}

        /*

  * allocate the devices -- we can't have them static, as the number

  * can be specified at load time

  */

// 给scull_devices申请内存

scull_devices = kmalloc(scull_nr_devs * sizeof(struct scull_dev), GFP_KERNEL);

if (!scull_devices) {

  result = -ENOMEM;

  goto fail;  /* Make this more graceful */

}

memset(scull_devices, 0, scull_nr_devs * sizeof(struct scull_dev));

        /* Initialize each device. */

        // 初始化 scull_dev

for (i = 0; i < scull_nr_devs; i++) {

  scull_devices.quantum = scull_quantum;// 量子大小

  scull_devices.qset = scull_qset;// 量子集大小

  init_MUTEX(&scull_devices.sem); // 初始化互斥锁,把信号量sem置为1

  scull_setup_cdev(&scull_devices, i);// 建立char_dev结构

}

        /* At this point call the init function for any friend device */

        // 初始化其它友设备

dev = MKDEV(scull_major, scull_minor + scull_nr_devs);

dev += scull_p_init(dev);

dev += scull_access_init(dev);

#ifdef SCULL_DEBUG /* only when debugging */

scull_create_proc();// 创建用于调试使用的 /proc 文件

#endif

return 0; /* succeed */

  fail://出错处理

scull_cleanup_module();

return result;

}

// 内核宏指定模块入口与出口

module_init(scull_init_module);

module_exit(scull_cleanup_module);复制代码

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

380

主题

380

帖子

0

粉丝