char_dev.c 添加中文注释,


 

char_dev.c里的中文注释,仅代表个人理解,仅供参考。如有错误之处,请指出,谢谢!

 

  1 /*
  2  *  linux/fs/char_dev.c
  3  *
  4  *  Copyright (C) 1991, 1992  Linus Torvalds
  5  */
  6 
  7 #include <linux/init.h>
  8 #include <linux/fs.h>
  9 #include <linux/kdev_t.h>
 10 #include <linux/slab.h>
 11 #include <linux/string.h>
 12 
 13 #include <linux/major.h>
 14 #include <linux/errno.h>
 15 #include <linux/module.h>
 16 #include <linux/smp_lock.h>
 17 #include <linux/seq_file.h>
 18 
 19 #include <linux/kobject.h>
 20 #include <linux/kobj_map.h>
 21 #include <linux/cdev.h>
 22 #include <linux/mutex.h>
 23 #include <linux/backing-dev.h>
 24 
 25 #ifdef CONFIG_KMOD
 26 #include <linux/kmod.h>
 27 #endif
 28 #include "internal.h"
 29 
 30 /*
 31  * capabilities for /dev/mem, /dev/kmem and similar directly mappable character
 32  * devices
 33  * - permits shared-mmap for read, write and/or exec
 34  * - does not permit private mmap in NOMMU mode (can't do COW)
 35  * - no readahead or I/O queue unplugging required
 36  */
 37 
 38 struct backing_dev_info directly_mappable_cdev_bdi = {
 39     .capabilities = (
 40 #ifdef CONFIG_MMU
 41     /* permit private copies of the data to be taken */
 42     BDI_CAP_MAP_COPY |
 43     #endif
 44     /* permit direct mmap, for read, write or exec */
 45     BDI_CAP_MAP_DIRECT |
 46     BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP),
 47 };
 48 
 49 static struct kobj_map *cdev_map;
 50 
 51 static DEFINE_MUTEX(chrdevs_lock);
 52 
 53 static struct char_device_struct {
 54     struct char_device_struct *next;
 55     unsigned int major;
 56     unsigned int baseminor;
 57     int minorct;
 58     char name[64];
 59     struct file_operations *fops;
 60     struct cdev *cdev;
 61     /* will die */
 62 } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
 63 
 64 /* index in the above */
 65 static inline int major_to_index(int major)
 66 {
 67     /* [CGW]: 根据主设备号,转换成对应的索引
 68      * 即主设备号就是索引号
 69      */
 70     return major % CHRDEV_MAJOR_HASH_SIZE;
 71 }
 72 
 73 #ifdef CONFIG_PROC_FS
 74 
 75 void chrdev_show(struct seq_file *f, off_t offset)
 76 {
 77     struct char_device_struct *cd;
 78     /* [CGW]: 根据offset (相当于索引),找到对应设备 */
 79     if (offset < CHRDEV_MAJOR_HASH_SIZE) {
 80         /* [CGW]: 上锁 */
 81         mutex_lock(&chrdevs_lock);
 82         /* [CGW]: 打印该设备项下链表中所有节点的主设备号,和设备名 */
 83         for (cd = chrdevs[offset]; cd; cd = cd->next)
 84             seq_printf(f, "%3d %s\n", cd->major, cd->name);
 85         /* [CGW]: 解锁 */
 86         mutex_unlock(&chrdevs_lock);
 87     }
 88 }
 89 
 90 #endif /* CONFIG_PROC_FS */
 91 
 92 /*
 93  * Register a single major with a specified minor range.
 94  *
 95  * If major == 0 this functions will dynamically allocate a major and return
 96  * its number.
 97  *
 98  * If major > 0 this function will attempt to reserve the passed range of
 99  * minors and will return zero on success.
100  *
101  * Returns a -ve errno on failure.
102  */
103 
104 static struct char_device_struct *
105 __register_chrdev_region(unsigned int major, unsigned int baseminor,
106                          int minorct, const char *name)
107 
108 {
109     struct char_device_struct *cd, **cp;
110     int ret = 0;
111     int i;
112 
113     /* [cgw]: 分配一块char_device_struct大小的内存块 */
114     cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
115     /* [cgw]: 分配失败 */
116     if (cd == NULL)
117         return ERR_PTR(-ENOMEM);
118     /*[cgw]: 上锁,进入临界区*/
119     mutex_lock(&chrdevs_lock);
120     /* temporary */
121     /*[cgw]: 如果主设备号为0,则从最大的设备号开始,往下查找第一个未被
122      *注册的设备
123      */
124     if (major == 0) {
125         /*[cgw]: 从大到小开始查找*/
126         for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
127             /* [cgw]: 找到第一个未被注册的设备 */
128             if (chrdevs[i] == NULL)
129             break;
130         }
131         /* [cgw]:未找到空位 */
132         if (i == 0) {
133             ret = -EBUSY;
134             goto out;
135         }
136         /* [cgw]: 以该空位的序号为主设备号 */
137         major = i;
138         ret = major;
139     }
140 
141     /* [cgw]: 手工分配一个主设备号,和次设备号基址 */
142     cd->major = major;
143     /* [cgw]: 手工分配次设备号基址 */
144     cd->baseminor = baseminor;
145     /* [cgw]: 分配minorct个次设备号 */
146     cd->minorct = minorct;
147     /* [cgw]: 分配设备名 */
148     strncpy(cd->name,name, 64);
149     /* [cgw]: 找到主设备号在设备列表中的索引 */
150     i = major_to_index(major);
151     /* [cgw]:  当前分配的设备号比较设备列表,判断该设备号是否
152      * 合法
153      */
154     for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
155         /* [cgw]: 当前分配的主设备号,小于该主设备号索引对应的,设备列表
156          * 中的主设备号,合法
157          * 这个里有点不解,从i = major_to_index(major);看出,主设备号和该主
158          * 设备号对应的索引是相等的,为什么(*cp)->major > major是合法呢
159          */
160         if ((*cp)->major > major ||
161             /* [cgw]: 当前分配的主设备号,等于该主设备号索引对应的,设备列表
162              * 中的主设备号,并且符合以下条件之一,当前分配的次设备号,等于
163              * 小于该主设备号索引对应的,设备列表中的次设备号。或者,当前分
164              * 配的次设备号,小于该主设备号索引对应的,设备列表中的次设备基
165              * 址以后minorct个次设备号
166              */
167            ((*cp)->major == major &&
168            (((*cp)->baseminor >= baseminor) ||
169            ((*cp)->baseminor + (*cp)->minorct > baseminor))))
170             break;
171     /* Check for overlapping minor ranges.  */
172     /* [cgw]: 当前分配的主设备号,等于该主设备号索引对应的,设备列表
173      * 中的主设备号,判断次设备号是否在范围内
174      */
175     if (*cp && (*cp)->major == major) {
176         int old_min = (*cp)->baseminor;
177         int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
178         int new_min = baseminor;
179         int new_max = baseminor + minorct - 1;
180 
181         /* New driver overlaps from the left.  */
182         if (new_max >= old_min && new_max <= old_max) {
183             ret = -EBUSY;
184             goto out;
185         }
186 
187         /* New driver overlaps from the right.  */
188         if (new_min <= old_max && new_min >= old_min) {
189             ret = -EBUSY;
190             goto out;
191         }
192     }
193 
194     /* [cgw]: 新加入的设备, 添加到该主设备号链表 */
195     cd->next = *cp;
196     /* [cgw]:  设备列表指针指向新加入设备*/
197     *cp = cd;
198     /* [cgw]:  解锁,退出临界区*/
199     mutex_unlock(&chrdevs_lock);
200     return cd;
201 out:
202 
203     /* [cgw]:  解锁,退出临界区*/
204     mutex_unlock(&chrdevs_lock);
205     /* [cgw]:  释放为新设备创建的内存*/
206     kfree(cd);
207     return ERR_PTR(ret);
208 }
209 
210 static struct char_device_struct *
211 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
212 
213 {
214     struct char_device_struct *cd = NULL, **cp;
215     /* [CGW]: 根据主设备号,找出该主设备号所在列表中的索引*/
216     int i = major_to_index(major);
217     /* [CGW]: 上锁,进入临界区 */
218     mutex_lock(&chrdevs_lock);
219     /* [CGW]: 根据索引,找出该主设备号所在列表项 */
220     for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
221         /* [CGW]:主设备号所在列表项中, 在主设备号对应的链表中,查找判断该主设备号,
222          * 次设备号基址,次设备号个数是否已经被注册
223          */
224     if ((*cp)->major == major &&
225        (*cp)->baseminor == baseminor &&
226        (*cp)->minorct == minorct)
227        /* [CGW]: 已经被注册,停止查找 */
228        break;
229     /* [CGW]: 主设备号所在链表中的节点
230      * (注意: 设备列表中,每个设备号都对应一个链表,该链表用于存放此设备号) 
231      */
232     if (*cp) {
233         /* [CGW]: 取出该节点 */
234         cd = *cp;
235         /* [CGW]: 更新cp,指向下一个节点*/
236         *cp = cd->next;
237     }
238 
239     /* [CGW]: 解锁,退出临界区 */
240     mutex_unlock(&chrdevs_lock);
241     /* [CGW]: 返回该设备(节点) */
242     return cd;
243 
244 }
245 
246 
247 
248 
249 /**
250  * register_chrdev_region() - register a range of device numbers
251  * @from: the first in the desired range of device numbers; must include
252  *        the major number.
253  * @count: the number of consecutive device numbers required
254  * @name: the name of the device or driver.
255  *
256  * Return value is zero on success, a negative error code on failure.
257  */
258 
259 int register_chrdev_region(dev_t from, unsigned count, const char *name)
260 
261 {
262     struct char_device_struct *cd;
263     dev_t to = from + count;
264     dev_t n, next;
265     /* [CGW]: 分配count个连续的设备 */
266     for (n = from; n < to; n = next) {
267         /* [CGW]: 主设备号+1递增 */
268         next = MKDEV(MAJOR(n)+1, 0);
269         if (next > to)
270             next = to;
271 
272         /* [CGW]:  根据主、次设备号基址,分配next - n个连续次设备号的设备,
273          * 并根据主设备号分配设备名
274          * 如果MINOR(n)为0,next-n的值应恒为256,未验证!!!
275          */
276         cd = __register_chrdev_region(MAJOR(n), MINOR(n),
277         next - n, name);
278         /* [CGW]: 分配失败 */
279         if (IS_ERR(cd))
280             goto fail;
281     }
282     return 0;
283 
284 fail:
285 
286     /* [CGW]: 当前分配到了第n个设备就失败了*/
287     to = n;
288     /* [CGW]: 注销刚刚分配的所有设备 */
289     for (n = from; n < to; n = next) {
290         next = MKDEV(MAJOR(n)+1, 0);
291         /* [CGW]: 对应的内存空间 */
292         kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
293     }
294     
295     /* [CGW]: 返回这个分配失败的设备指针 */
296     return PTR_ERR(cd);
297 }
298 
299 /**
300  * alloc_chrdev_region() - register a range of char device numbers
301  * @dev: output parameter for first assigned number
302  * @baseminor: first of the requested range of minor numbers
303  * @count: the number of minor numbers required
304  * @name: the name of the associated device or driver
305  *
306  * Allocates a range of char device numbers.  The major number will be
307  * chosen dynamically, and returned (along with the first minor number)
308  * in @dev.  Returns zero or a negative error code.
309  */
310 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
311                         const char *name)
312 
313 {
314     struct char_device_struct *cd;
315     /* [CGW]: 自动分配一个设备,因为主设备号为0 
316      * 以baseminor为基址,分配count个次设备号
317      */
318     cd = __register_chrdev_region(0, baseminor, count, name);
319     /* [CGW]: 分配失败 */
320     if (IS_ERR(cd))
321         return PTR_ERR(cd);
322     /* [CGW]: 返回设备号 */
323     *dev = MKDEV(cd->major, cd->baseminor);
324     return 0;
325 }
326 
327 /**
328  * register_chrdev() - Register a major number for character devices.
329  * @major: major device number or 0 for dynamic allocation
330  * @name: name of this range of devices
331  * @fops: file operations associated with this devices
332  *
333  * If @major == 0 this functions will dynamically allocate a major and return
334  * its number.
335  *
336  * If @major > 0 this function will attempt to reserve a device with the given
337  * major number and will return zero on success.
338  *
339  * Returns a -ve errno on failure.
340  *
341  * The name of this device has nothing to do with the name of the device in
342  * /dev. It only helps to keep track of the different owners of devices. If
343  * your module name has only one type of devices it's ok to use e.g. the name
344  * of the module here.
345  *
346  * This function registers a range of 256 minor numbers. The first minor number
347  * is 0.
348  */
349 
350 int register_chrdev(unsigned int major, const char *name,
351                     const struct file_operations *fops)
352 
353 {
354     struct char_device_struct *cd;
355     struct cdev *cdev;
356     char *s;
357     int err = -ENOMEM;
358 
359     /* [cgw]: 分配一个设备,次设备号为0~255 */
360     cd = __register_chrdev_region(major, 0, 256, name);
361     if (IS_ERR(cd))
362         return PTR_ERR(cd);
363     /* [cgw]:分配一个cdev结构体 */
364     cdev = cdev_alloc();
365     if (!cdev)
366         goto out2;
367     
368     cdev->owner = fops->owner;
369     cdev->ops = fops;
370 
371     /* [cgw]: 设置kobject的名字 */
372     kobject_set_name(&cdev->kobj, "%s", name);
373     /* [cgw]: 把kobject的名字kobj->name中的'/'替换成'!' */
374     for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
375         *s = '!';
376 
377     /* [cgw]: 添加一个字符设备到系统*/
378     err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
379     if (err)
380         goto out;
381 
382     /* [cgw]: 设置char_device_struct中的cdev指针 */
383     cd->cdev = cdev;
384     
385     return major ? 0 : cd->major;
386 
387 out:
388     /* [cgw]: kobect 引用计数-1 */
389     kobject_put(&cdev->kobj);
390 
391 out2:
392     /* [cgw]: 释放刚注册的设备 */
393     kfree(__unregister_chrdev_region(cd->major, 0, 256));
394     return err;
395 }
396 
397 /**
398  * unregister_chrdev_region() - return a range of device numbers
399  * @from: the first in the range of numbers to unregister
400  * @count: the number of device numbers to unregister
401  *
402  * This function will unregister a range of @count device numbers,
403  * starting with @from.  The caller should normally be the one who
404  * allocated those numbers in the first place...
405  */
406 
407 void unregister_chrdev_region(dev_t from, unsigned count)
408 {
409     dev_t to = from + count;
410     dev_t n, next;
411 
412     /* [CGW]: 注销所有从from到to的count个设备 */
413     for (n = from; n < to; n = next) {
414         /* [CGW]:  查找下一设备号*/
415         next = MKDEV(MAJOR(n)+1, 0);
416         if (next > to)
417             next = to;
418         /* [CGW]: 注销所有刚才注册的设备 */
419         kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
420     }
421 }
422 
423 
424 int unregister_chrdev(unsigned int major, const char *name)
425 {
426     struct char_device_struct *cd;
427     /* [CGW]: 根据主设备号,找到对应设备列表项 */
428     cd = __unregister_chrdev_region(major, 0, 256);
429     /* [CGW]: 该设备项有效,并且被注册 */
430     if (cd && cd->cdev)
431        /* [CGW]: 注销该设备 */
432         cdev_del(cd->cdev);
433         /* [CGW]: 释放该设备占用的内存空间 */
434     kfree(cd);
435     
436     return 0;
437 }
438 
439 static DEFINE_SPINLOCK(cdev_lock);
440 
441 static struct kobject *cdev_get(struct cdev *p)
442 {
443     struct module *owner = p->owner;
444     struct kobject *kobj;
445     /* [cgw]:cdev_get uses try_module_get to attempt to increment that module's
446      * usage count. If that operation succeeds, kobject_get is used to increment the
447      * kobject's reference count as well---<Linux Device Drivers>
448      * try_module_get(owner)增加owner (THIS_MODULE)引用计数
449      */
450     /* [cgw]: module使用计数+1 */
451     if (owner && !try_module_get(owner))
452         return NULL;
453     /* [cgw]: kobj引用计数+1 */
454     kobj = kobject_get(&p->kobj);
455     /* [cgw]: kobj指针返回失败 */
456     if (!kobj)
457        /* [cgw]: module使用计数-1 */
458         module_put(owner);
459         
460     return kobj;
461 }
462 
463 void cdev_put(struct cdev *p)
464 {
465     /* [cgw]: cdev指针不为空 */
466     if (p) {
467         /* [cgw]: 获得模块指针 */
468         struct module *owner = p->owner;
469         /* [cgw]: kobj引用计数-1 */
470         kobject_put(&p->kobj);
471         /* [cgw]: module使用计数-1 */
472         module_put(owner);
473     }
474 }
475 
476 /*
477  * Called every time a character special file is opened
478  */
479 int chrdev_open(struct inode * inode, struct file * filp)
480 {
481     struct cdev *p;
482     struct cdev *new = NULL;
483     int ret = 0;
484 
485     /* [cgw]: 进入临界区 */
486     spin_lock(&cdev_lock);
487     /* [cgw]: 从inode中得到一个字符设备cdev指针 */
488     p = inode->i_cdev;
489     /* [cgw]: struct cdev指针为空 */
490     if (!p) {
491         struct kobject *kobj;
492         int idx;
493         /* [cgw]: 进入临界区 */
494         spin_unlock(&cdev_lock);
495         /* [cgw]: 看看cdev_map的probes[inode->i_rdev]链表是否有inode->i_rdev这个设备
496          * 并返回这个设备的kobj
497          */
498         kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
499         /* [cgw]: kobj为空,错误 */
500         if (!kobj)
501             return -ENXIO;
502         /* [cgw]: 根据返回的kobj,找出包含这个kobj的struct cdev指针 */
503         new = container_of(kobj, struct cdev, kobj);
504         /* [cgw]: 进入临界区 */
505         spin_lock(&cdev_lock);
506         /* [cgw]: 从inode中得到一个字符设备cdev指针 */
507         p = inode->i_cdev;
508         /* [cgw]: struct cdev指针为空 */
509         if (!p) {
510             /* [cgw]: 把这个struct cdev指针填装到inode->i_cdev */
511             inode->i_cdev = p = new;
512             /* [cgw]: 记录对应的索引 */
513             inode->i_cindex = idx;
514             /* [cgw]: 把inode->i_devices插入到p->list */
515             list_add(&inode->i_devices, &p->list);
516             /* [cgw]: 清除new指针 */
517             new = NULL;
518             /* [cgw]: 返回cdev中kobj指针为空,错误 */
519         } else if (!cdev_get(p))
520             ret = -ENXIO;
521     /* [cgw]: 返回cdev中kobj指针为空,错误 */
522     } else if (!cdev_get(p))
523         ret = -ENXIO;
524         /* [cgw]: 退出临界区 */
525         spin_unlock(&cdev_lock);
526         /* [cgw]: 实际上是cdev->kobj引用计数-1,module使用计数-1 */
527         cdev_put(new);
528         if (ret)
529             return ret;
530     /* [cgw]: module使用计数+1,并返回cdev->ops指针  */
531     filp->f_op = fops_get(p->ops);
532     /* [cgw]: filp->f_op指针为空,失败 */
533     if (!filp->f_op) {
534         /* [cgw]: 实际上是cdev->kobj引用计数-1,module使用计数-1 */
535         cdev_put(p);
536         return -ENXIO;
537     }
538     /* [cgw]: 调用filp->f_op->open,打开的是用户驱动程序中定义的
539      * file_operations中的open函数
540      */
541     if (filp->f_op->open) {
542         /* [cgw]: 上锁 */
543         lock_kernel();
544         /* [cgw]: 调用filp->f_op->open */
545         ret = filp->f_op->open(inode,filp);
546         /* [cgw]: 解锁 */
547         unlock_kernel();
548     }
549     /* [cgw]: 调用filp->f_op->open失败 */
550     if (ret)
551         /* [cgw]: 实际上是cdev->kobj引用计数-1,module使用计数-1 */
552         cdev_put(p);
553         
554     return ret;
555 }
556 
557 void cd_forget(struct inode *inode)
558 {
559     /* [cgw]: 进入临界区 */
560     spin_lock(&cdev_lock);
561     /* [cgw]: 从链表删除一个inode->i_devices节点,
562      * 并重新初始化这个链表
563      */
564     list_del_init(&inode->i_devices);
565     /* [cgw]: inode->i_cdev指针清0 */
566     inode->i_cdev = NULL;
567     /* [cgw]: 退出临界区 */
568     spin_unlock(&cdev_lock);
569 }
570 
571 static void cdev_purge(struct cdev *cdev)
572 {
573     /* [cgw]: 进入临界区 */
574     spin_lock(&cdev_lock);
575     /* [cgw]: 测试cdev->list这个链表是否为空
576      * 
577      */
578     while (!list_empty(&cdev->list)) {
579         struct inode *inode;
580         /* [cgw]: 找出包含cdev->list的struct inode结构体的指针 */
581         inode = container_of(cdev->list.next, struct inode, i_devices);
582         /* [cgw]: 从链表删除一个inode->i_devices节点,
583          * 并重新初始化这个链表
584          */
585         list_del_init(&inode->i_devices);
586         /* [cgw]: inode->i_cdev指针清0 */
587         inode->i_cdev = NULL;
588     }
589     /* [cgw]: 退出临界区 */
590     spin_unlock(&cdev_lock);
591 }
592 
593 /*
594  * Dummy default file-operations: the only thing this does
595  * is contain the open that then fills in the correct operations
596  * depending on the special file...
597  */
598 
599 const struct file_operations def_chr_fops = {
600     .open = chrdev_open,
601 };
602 static struct kobject *exact_match(dev_t dev, int *part, void *data)
603 {
604     struct cdev *p = data;
605     /* [cgw]: 返回cdev中kobj成员指针 */
606     return &p->kobj;
607 }
608 
609 static int exact_lock(dev_t dev, void *data)
610 {
611     struct cdev *p = data;
612     /* [cgw]: data中kobj引用计数+1,并返回kobj指针 */
613     return cdev_get(p) ? 0 : -1;
614 }
615 
616 /**
617  * cdev_add() - add a char device to the system
618  * @p: the cdev structure for the device
619  * @dev: the first device number for which this device is responsible
620  * @count: the number of consecutive minor numbers corresponding to this
621  *         device
622  *
623  * cdev_add() adds the device represented by @p to the system, making it
624  * live immediately.  A negative error code is returned on failure.
625  */
626 
627 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
628 {
629     /* [cgw]: 分配一个dev设备号给p->dev */
630     p->dev = dev;
631     /* [cgw]: 分配count个连续的次设备号
632      * 这里实际是分配count设备,只是次设备号不一样,主设备号都一样
633      */
634     p->count = count;
635     /* [cgw]: 把新加入的设备填装到一个probe结构,并把这个probe插入到
636      * 对应probes[MAJOR(dev)]链表,即probes[]中每一个元素都是一个链表
637      */
638     return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
639 }
640 
641 static void cdev_unmap(dev_t dev, unsigned count)
642 {
643     /* [cgw]: 从probes[MAJOR(dev)]链表中删除一个节点(probe) */
644     kobj_unmap(cdev_map, dev, count);
645 }
646 
647 /**
648  * cdev_del() - remove a cdev from the system
649  * @p: the cdev structure to be removed
650  *
651  * cdev_del() removes @p from the system, possibly freeing the structure
652  * itself.
653  */
654 
655 void cdev_del(struct cdev *p)
656 {
657     /* [cgw]: 从probes[MAJOR(p->dev)]链表中删除一个节点(probe) 
658      * 
659      */
660     cdev_unmap(p->dev, p->count);
661     /* [cgw]: kobj引用计数-1 */
662     kobject_put(&p->kobj);
663 }
664 
665 static void cdev_default_release(struct kobject *kobj)
666 {
667     /* [cgw]: 找到包含kobj的结构体struct cdev的指针 */
668     struct cdev *p = container_of(kobj, struct cdev, kobj);
669     /* [cgw]: 从cdev->list链表中删除cdev */
670     cdev_purge(p);
671 }
672 
673 static void cdev_dynamic_release(struct kobject *kobj)
674 {
675     /* [cgw]: 找到包含kobj的结构体struct cdev的指针 */
676     struct cdev *p = container_of(kobj, struct cdev, kobj);
677     /* [cgw]: 从cdev->list链表中删除cdev */
678     cdev_purge(p);
679     /* [cgw]: 释放这个cdev的内存空间 */
680     kfree(p);
681 }
682 
683 static struct kobj_type ktype_cdev_default = {
684     .release = cdev_default_release,
685 };
686 
687 static struct kobj_type ktype_cdev_dynamic = {
688     .release = cdev_dynamic_release,
689 };
690 
691 /**
692  * cdev_alloc() - allocate a cdev structure
693  *
694  * Allocates and returns a cdev structure, or NULL on failure.
695  */
696 struct cdev *cdev_alloc(void)
697 {
698     /* [cgw]: 分配一个cdev结构体 */
699     struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
700 
701     /* [cgw]: 分配cdev结构体成功 */
702     if (p) {
703        /* [cgw]: 分配一个kobj.ktype结构体,指向&ktype_cdev_dynamic
704         * 为这个驱动制定一个统一的行为,提供释放kobj的方法
705         */
706         p->kobj.ktype = &ktype_cdev_dynamic;
707         /* [cgw]: 初始化链表,把这个cdev插入链表头 */
708         INIT_LIST_HEAD(&p->list);
709         /* [cgw]: 初始化kobject,每个对象都有一个kobject */
710         kobject_init(&p->kobj);
711     }
712     return p;
713 }
714 
715 /**
716  * cdev_init() - initialize a cdev structure
717  * @cdev: the structure to initialize
718  * @fops: the file_operations for this device
719  *
720  * Initializes @cdev, remembering @fops, making it ready to add to the
721  * system with cdev_add().
722  */
723 void cdev_init(struct cdev *cdev, const struct file_operations *fops)
724 {
725     /* [cgw]: cdev结构体清零 */
726     memset(cdev, 0, sizeof *cdev);
727     /* [cgw]: 初始化链表,把这个cdev插入链表头 */
728     INIT_LIST_HEAD(&cdev->list);
729     /* [cgw]: 分配一个kobj.ktype结构体,指向&ktype_cdev_default
730      * 为这个驱动制定一个默认的统一的行为,提供恢复默认kobj的方法
731      * 没有释放kobj内存空间
732      */
733     cdev->kobj.ktype = &ktype_cdev_default;
734     /* [cgw]: 初始化kobject,每个对象都有一个kobject */
735     kobject_init(&cdev->kobj);
736     /* [cgw]: cdev->ops指向驱动程序中的file_operations结构体 */
737     cdev->ops = fops;
738 }
739 
740 
741 
742 
743 static struct kobject *base_probe(dev_t dev, int *part, void *data)
744 {
745     if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
746         /* Make old-style 2.4 aliases work */
747         request_module("char-major-%d", MAJOR(dev));
748     return NULL;
749 }
750 
751 void __init chrdev_init(void)
752 {
753     /*[cgw]: 初始化cdev_map变量 */
754     cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
755 }
756 
757 /* Let modules do char dev stuff */
758 EXPORT_SYMBOL(register_chrdev_region);
759 EXPORT_SYMBOL(unregister_chrdev_region);
760 EXPORT_SYMBOL(alloc_chrdev_region);
761 EXPORT_SYMBOL(cdev_init);
762 EXPORT_SYMBOL(cdev_alloc);
763 EXPORT_SYMBOL(cdev_del);
764 EXPORT_SYMBOL(cdev_add);
765 EXPORT_SYMBOL(register_chrdev);
766 EXPORT_SYMBOL(unregister_chrdev);
767 EXPORT_SYMBOL(directly_mappable_cdev_bdi);

 

相关内容

    暂无相关文章