歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

char_dev.c 添加中文注釋,

char_dev.c 添加中文注釋,


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);

 

http://xxxxxx/Linuxjc/1144730.html TechArticle

Copyright © Linux教程網 All Rights Reserved