版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年四川大学华西第二医院招聘外聘门诊医师备考题库及1套完整答案详解
- 长沙市望城区人民医院2025年面向社会公开招聘编外合同制专业技术人员备考题库含答案详解
- 2025年天津市双菱中学招聘教师23人备考题库有答案详解
- 2025年鄞州区实验小学教育集团(南校区)招聘备考题库及答案详解1套
- 2025年陕西中放日昇科技产业发展有限公司公开招聘80人备考题库参考答案详解
- 2025年昆明市鲁轩高级中学教师招聘14人备考题库及一套参考答案详解
- 2025年西安交通大学口腔医院医护人员常年招聘备考题库含答案详解
- 2025年佛山市顺德区华南师范大学附属北滘学校招聘临聘教师备考题库及一套参考答案详解
- 南昌高新招商集团2026届校园招聘100名备考题库参考答案详解
- 2025年重庆水泵厂有限责任公司招17人备考题库及1套参考答案详解
- 2024-2025学年广东省深圳实验学校初中部九年级上学期开学考英语试题及答案
- 【MOOC】行为金融学-中央财经大学 中国大学慕课MOOC答案
- 电路分析与应用知到智慧树章节测试课后答案2024年秋吉林电子信息职业技术学院
- 2022年全国职业院校技能大赛-中药传统技能赛项规程
- 管理经济学:理论与案例 毛蕴诗第2版 每章习题答案
- (高清版)WST 415-2024 无室间质量评价时的临床检验质量评价
- 国开(河北)2024年《中外政治思想史》形成性考核1-4答案
- MOOC 微型计算机原理与接口技术-南京邮电大学 中国大学慕课答案
- 有限空间安全检查档案(含检查记录、台账)全套
- 应急预案-光伏
- 科来网络回溯分析系统深圳超算测试报告
评论
0/150
提交评论