高级字符驱动(第六章 )

来源:互联网 发布:回溯算法与n皇后问题 编辑:程序博客网 时间:2024/06/06 04:16
1、用户空间的ioctl系统调用原型:
     int ioctl(int fd, unsigned long cmd, ...);//第三个参数的具体形式依赖于第二个参数,其作为第二个参数的参数值。
     驱动程序的ioctl函数原型:
     int (*ioctl) (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
2、在使用ioctl的附加参数时需要注意,如果是个整数则可以直接使用,如果是个指向用户空间的指针,则需要用access_ok函数进行测试:
     <asm/uaccess.h>
     int access_ok(int type, const void *addr, unsigned long size);
     第一个参数为VERIFY_READ或VERIFY_WRITE,addr是一个用户空间地址。返回值为bool值,1表示成功,0表示失败。
3、全能与受限操作
(1)全部权能操作都可以在<linux/capability>中找到;
(2)权能的检查capable函数实现(定义在<sys/sched.h>):
     int capable(int capcability);
4、等待队列头为一个wait_queue_head_t的结构体,在<linux/wait.h>中被定义。
     其初始方法可以有两种:
     DECLEAR_WAIT_QUEUE_HAED(name); //静态方法
     wait_queue_head_t my_queue;
     init_waitqueue_head(&my_queue);//动态方法
5、简单休眠
     wait_event(queue, condition);
     wait_event_interruptible(queue, condition);
     wait_event_timeout(queue, condition, timeout);
     wait_event_interruptible_timeout(queue, condition, timeout);
     简单唤醒:
     void wake_up(wait_queue_head_t *queue);
     void wake_up_interruptible(wait_queue_head_t *queue);
注:wake_up会唤醒等待在给定queue上的所有进程,而wake_up_interruptible只会唤醒那些执行可中断休眠的进程。
6、独占等待
void prepare_to_wait_exclusive(wait_queue_head_t *queue, wait_queue_t *wait, int state);
注:使用wait_event及其变种无法执行独占等待。
7、wait_up变种:大部分驱动程序从来不需要这些变种但是出于完整性考虑,这里给出:
     wake_up(wait_queue_head_t *queue);
     wake_up_interruptible(wait_queue_head_t *queue);
     wake_up_nr(wait_queue_head_t *queue, int nr);
     wake_up_interruptible_all(wait_queue_head_t *queue, int nr);
     wake_up_all(wait_queue_head_t *queue);
     wake_up_interruptible_all(wait_queue_head_t *queue);
     wake_up_interruptible_sync(wait_queue_head_t *queue);
8、定位设备
(1)llseek实现
     llseek方法实现了lseek和llseek系统调用。如果设备只提供了数据流(例如键盘和串口)而没有提供数据区,定位这些设备需要在open方法中调用nonseekable_open:
     int nonseekable_open(struct inode, struct file *filp);
     为了完整期间,还应该将file_operations结构中的llseek方法设置为特殊的辅助函数no_llseek,该函数定义在<linux/fs.h>.

原创粉丝点击