版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、编写Linux下的设备驱动程序What we have learned?用module实现设备驱动程序init_module, cleanup_module设备也是文件;设备由主设备号、次设备号唯一标识mknod /dev/status c 0登记/注销设备register_chrdev, unregister_chrdevstruct file_operations (include/linux/fs.h)实现file_operations结构中指定的操作What we have learned? (contd)拷贝数据to/from用户空间copy_to_user, copy_from_u
2、ser使用计数(usage count)每个module保留一个usage count宏:MOD_INC_USE_COUNT, MOD_DEC_USE_COUNT, MOD_IN_USE:int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);int unregister_chrdev(unsigned int major, const char *name);:unsigned long copy_to_user(void *to, const void *from, un
3、signed long count);unsigned long copy_from_user(void *to, const void *from, unsigned long count);:struct file_operations int (*open) (struct inode *, struct file *);int (*flush) (struct file *);int (*release) (struct inode *, struct file *);loff_t (*llseek) (struct file *, loff_t, int);ssize_t (*rea
4、d) (struct file *, char *, size_t, loff_t *);ssize_t (*write) (struct file *, const char *, size_t, loff_t *); ;:struct file的几个关键字段mode_t f_mode;loff_t f_pos;unsigned int f_flags;struct file_operations *f_op;void *private_data;struct dentry *f_dentry;What are we still to learn?TimingHardware managem
5、entI/O ports & I/O memoryInterrupt handlingProgramming I/OTwo types of instructions can support I/O:special-purpose I/O instructions;memory-mapped load/store instructions.Intel x86 provides in, out instructions. Most other CPUs use memory-mapped I/O.I/O instructions do not preclude memory-mapped I/O
6、.Using I/O ports:int check_region(unsigned long start, unsigned long len);struct resource *request_region(unsigned long start, unsigned long len, char *name);void release_region(unsigned long start, unsigned long len);Using I/O ports (contd):Read/write 8-bit ports (byte width):unsigned intb(unsigned
7、 port);unsigned outb(unsigned char byte, unsigned port);Read/write 16-bit ports (word width):unsigned intw(unsigned port);unsigned outw(unsigned short word, unsigned port);Read/write 32-bit ports:unsigned intl(unsigned port);unsigned outl(unsigned longword, unsigned port);Interrupt handlingInstallin
8、g an interrupt handlerImplementing a handlerInstalling an Interrupt Handlerrequest_irq, free_irqint request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *), unsigned long flags, const char *dev_name, void *dev_id);void free_irq(unsigned int irq, void *dev_id);调用时机Implementing a
9、handlerstatic void sample_interrupt(int irq, void *dev_id, struct pt_regs *regs);irq: interrupt numberdev_id: client data (private data used by the driver)regs: (rarely used) a snapshot of the processors context before the processor entered interrupt codeImplementing a handler (contd)等待队列(wait queue
10、)wait_queue_head_t类型init_waitqueue_headinterruptible_sleep_on wake_up_interruptibleCode examplesvoid short_interrupt(int irq, void *dev_id, struct pt_regs *regs) struct timeval tv; int written; do_gettimeofday(&tv); /* Write a 16-byte record. Assume PAGE_SIZE is a multiple of 16 */ written = sprintf
11、(char *)short_head,%08u.%06un, (int)(tv.tv_sec % 100000000), (int)(tv.tv_usec); short_incr_bp(&short_head, written); wake_up_interruptible(&short_queue);Code examples (contd)ssize_t short_i_read (struct file *filp, char *buf, size_t count, loff_t *f_pos) int count0; while (short_head = short_tail) i
12、nterruptible_sleep_on(&short_queue); if (signal_pending (current) /* a signal arrived */ return -ERESTARTSYS; /* tell the fs layer to handle it */ count0 = short_head - short_tail; if (count0 0) /* wrapped */ count0 = short_buffer + PAGE_SIZE - short_tail; if (count0 count) count = count0; if (copy_to_user(buf, (char *)short_tail, count) return -EFAULT; short_incr_bp (&short_tail, count); return count;Bottom-Half (BH) Processing中断处理程序尽量短小top half: 实际响应中断的例程bottom half: 被top half 调度,并在稍后更安全的时候运行的例程将
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024轨道车辆座椅动态舒适性试验技术规范
- 培训课件 -OD组织发展全景图之组织激活
- 大理2024年04版小学4年级下册英语第二单元测验卷
- 农场成本核算-记账实操
- 北京市顺义区2023-2024学年八年级下学期期末英语试题
- 第三单元名著导读《儒林外史》教学设计-2023-2024学年统编版语文九年级下册
- 2023年乙二醇辛醇糠醇资金需求报告
- 三维数字内容制作-三维动画制作流程规范
- 1.2.2 反比例函数的性质 同步练习
- 有关读书短篇演讲稿(3篇)
- iml工艺设计要求
- 新概念英语第2册课文(完整版)
- 感受小说中的群众角色-九年级上册《智取生辰纲》《范进中举》《刘姥姥进大观园》群文阅读教学设计-
- 课件紧急分娩的处理
- 部队车辆安全教育课件
- 大学美育教程(高职)全套教学课件
- 高端保洁家政技能培训方案
- 丙型肝炎的防治课件
- 高质量的心肺复苏
- 2023年西南交通大学辅导员招聘考试真题
- 《中考试卷分析》课件
评论
0/150
提交评论