1.llseek
llseek方法实现了lseek和llseek的系统调用,如果设备操作为定义llseek方法,内核默认通过修改filp->f_pos而执行定位,filp->f_pos是文件的当前读取/写入位置,为了使lseek系统调用能正确工作,read和write方法必须通过更新他们收到的偏移量参数来配合。
Scrull的驱动例子
1. loff_t scull_llseek(struct file *filp, loff_t off, int whence)
2. {
3. *dev = filp->private_data;
4. ;
5.
6. (whence) {
7. case 0: /* SEEK_SET */
8. = off;
9. ;
10.
11. case 1: /* SEEK_CUR */
12. = filp->f_pos + off;
13. ;
14.
15. case 2: /* SEEK_END */
16. = dev->size + off;
17. ;
18.
19. : /* can't happen */
20. -EINVAL;
21. }
22. if (newpos < 0) return -EINVAL;
23. ->f_pos = newpos;
24. ;
25. }
大多数设备只提供了数据流(比如串口和键盘),而不是数据区,定义这些设备没有意义,在这中情况下,不能简单的不声明llseek操作,因为默认方法是允许定位的,相反,应该在open方法中调用nonseekable_open,以便通知内核设备不支持llseek,
int nonseekable_open(struct inode *inode, struct file *filp)
该函数会吧给定的filp标记为不可定位;这样内核就不会让这种文件上的lseek调用成功。通过这种方式标记文件,还可以确保通过pread和pwrite系统调用也不能定位文件。
为了完整期间,我们还应该将file_operations结构中的llseek方法设备为特殊的辅助函数no_llseek(定义在 )
2.设备文件的访问控制
提供访问控制对于一个设备节点来的可靠性来说有时是至关重要的。这部分的内容只是在open和release方法上做些修改,增加一些检查机制既可。
(1)独享设备
最生硬的访问控制方法是一次只允许一个进程打开设备(独享),最好避免使用这种技术,因为它制约了用户的灵活性。scullsingle设备维护一个atomic_t变量,称为scull_s_available,该变量初值为1,表明设备真正可用,open调用会减小并测试scull_s_available,并在其他进程已经打开该设备拒绝访问
1. static int scull_s_open(struct inode *inode, struct file *filp)
2. {
3. *dev; /* device information */
4. = container_of(inode->i_cdev, struct scull_dev, cdev);
5.
6. if (! atomic_dec_and_test (&scull_s_available)) {
7. (&scull_s_available);
8. -EBUSY; /* already open */
9. }
10.
11. /* then, everything else is copied from the bare scull device */
12. if ( ((filp->f_flags & O_ACCMODE) && O_WRONLY))
13. (dev);
14. ->private_data = dev;
15. ; /* success */
16. }
release则标记设备为不忙
1. static int scull_s_release(struct inode *inode, struct file *filp)
2. {
3. atomic_inc(&scull_s_available); /* release the device */
4. return 0;
5. }
建议吧打开标志scull_s_available放在私有设备结构里
(2)单用户访问
open调用在第一次打开授权,但它记录下设备的属主,这意味着一个用户可以多次打开设备,晕血多个进程并发的在设备上操作。其他用户不能打开这个设备,这样就避免了外部干扰。
1. spin_lock(&scull_u_lock);
2. if (scull_u_count &&
3. (scull_u_owner != current->uid) && /* allow user */
4. (scull_u_owner != current->euid) && /* allow whoever did su */
5. !capable(CAP_DAC_OVERRIDE)) { /* still allow root */
6. (&scull_u_lock);
7. -EBUSY; /* -EPERM would confuse the user */
8. }
9.
10. if (scull_u_count == 0)
11. = current->uid; /* grab it */
12.
13. ++;
14. (&scull_u_lock);
相应的释放函数
1. static int scull_u_release(struct inode *inode, struct file *filp)
2. {
3. (&scull_u_lock);
4. --; /* nothing else */
5. (&scull_u_lock);
6. ;
7. }
(3)替代EBUSY的阻塞型open
当设备不能访问时返回一个错误,通常这是最合理的方式,但有些情况下可能需要让进程等待设备。
代理EBUSY的一个方法是实现阻塞型open。
1. static int scull_w_open(struct inode *inode, struct file *filp)
2. {
3. *dev = &scull_w_device; /* device information */
4.
5. (&scull_w_lock);
6. while (! scull_w_available()) {
7. (&scull_w_lock);
8. if (filp->f_flags & O_NONBLOCK) return -EAGAIN;
9. if (wait_event_interruptible (scull_w_wait, scull_w_available()))
10. -ERESTARTSYS; /* tell the fs layer to handle it */
11. (&scull_w_lock);
12. }
13. if (scull_w_count == 0)
14. = current->uid; /* grab it */
15. ++;
16. (&scull_w_lock);
17.
18. /* then, everything else is copied from the bare scull device */
19. if ((filp->f_flags & O_ACCMODE) == O_WRONLY)
20. (dev);
21. ->private_data = dev;
22. ; /* success */
23. }
release方法唤醒所有等待的进程
1. static int scull_w_release(struct inode *inode, struct file *filp)
2. {
3. int temp;
4.
5. (&scull_w_lock);
6. --;
7. = scull_w_count;
8. (&scull_w_lock);
9.
10. if (temp == 0)
11. (&scull_w_wait); /* awake other uid's */
12. ;
13. }
这类问题(对同一设备的不同的,不兼容的策略)最好通过为每一种访问策略实现一个设备节点的方法来解决。
(4)在打开时复制设备
另一个实现访问控制的方法是,在进程打开设备时创建设备的不同私有副本。
显然这种方法只有在设备没有绑定到某个硬件对象时才能实现,/dev/tty内部也使用了类似的技术,以提供给它的进程一个不同于/dev入口点所表现出的“情景”,如果复制的设备是由软件驱动程序创建,我们称它们为“虚拟设备”--就像所有的虚拟终端都是用同一个物理终端设备一样。
虽然这种访问控制并不常见,但它的实现展示了内核代码可以轻松的改变应用程序看到的外部环境。
1. /************************************************************************
2. *
3. * Finally the `cloned' private device. This is trickier because it
4. * involves list management, and dynamic allocation.
5. */
6.
7. /* The clone-specific data structure includes a key field */
8.
9. {
10. ;
11. ;
12. ;
13.
14. };
15.
16. /* The list of devices, and a lock to protect it */
17. (scull_c_list);
18. = SPIN_LOCK_UNLOCKED;
19.
20. /* A placeholder scull_dev which really just holds the cdev stuff. */
21. ;
22.
23. /* Look for a device or create one if missing */
24. *scull_c_lookfor_device(dev_t key)
25. {
26. *lptr;
27.
28. (lptr, &scull_c_list, list) {
29. if (lptr->key == key)
30. &(lptr->device);
31. }
32.
33. /* not found */
34. = kmalloc(sizeof(struct scull_listitem), GFP_KERNEL);
35. if (!lptr)
36. NULL;
37.
38. /* initialize the device */
39. (lptr, 0, sizeof(struct scull_listitem));
40. ->key = key;
41. (&(lptr->device)); /* initialize it */
42. (&(lptr->device.sem));
43.
44. /* place it in the list */
45. (&lptr->list, &scull_c_list);
46.
47. &(lptr->device);
48. }
49.
50. int scull_c_open(struct inode *inode, struct file *filp)
51. {
52. *dev;
53. ;
54.
55. if (!current->signal->tty) {
56. ("Process \"%s\" has no ctl tty\n", current->comm);
57. -EINVAL;
58. }
59. = tty_devnum(current->signal->tty);
60.
61. /* look for a scullc device in the list */
62. (&scull_c_lock);
63. = scull_c_lookfor_device(key);
64. (&scull_c_lock);
65.
66. if (!dev)
67. -ENOMEM;
68.
69. /* then, everything else is copied from the bare scull device */
70. if ( (filp->f_flags & O_ACCMODE) == O_WRONLY)
71. (dev);
72. ->private_data = dev;
73. ; /* success */
74. }
75.
76. int scull_c_release(struct inode *inode, struct file *filp)
77. {
78. /*
79. * Nothing to do, because the device is persistent.
80. * A `real' cloned device should be freed on last close
81. */
82. ;
83. }
至此,Linux高级字符驱动程序操作已经学习完毕,(其实这部分已经看过几遍,后续还要继续看,有些没理解透)这部分笔记现在才整理,一方面是因为难度,一方面是实际工作用的不是很多,没有专门花时间来整理。anyway,到目前为止,Linux字符驱动所有基础知识笔记整理完毕,这是个辛苦的过程,但收获很大。再次的,特别感谢Tekkaman Ninja大侠,LDD3笔记整理过程中参考了他的大部分,给了我很大帮助(若涉及版权相关,全都归他)。