编写Linux下的设备驱动程序.ppt
文本预览下载声明
编写Linux下的设备驱动程序 What we have learned? 用module实现设备驱动程序 init_module, cleanup_module 设备也是文件;设备由主设备号、次设备号唯一标识 mknod /dev/status c major_num 0 登记/注销设备 register_chrdev, unregister_chrdev struct file_operations (include/linux/fs.h) 实现file_operations结构中指定的操作 What we have learned? (cont’d) 拷贝数据to/from用户空间 copy_to_user, copy_from_user 使用计数(usage count) 每个module保留一个usage count 宏:MOD_INC_USE_COUNT, MOD_DEC_USE_COUNT, MOD_IN_USE linux/fs.h: int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); int unregister_chrdev(unsigned int major, const char *name); asm/uaccess.h: unsigned long copy_to_user(void *to, const void *from, unsigned long count); unsigned long copy_from_user(void *to, const void *from, unsigned long count); linux/fs.h: 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 (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); … }; linux/fs.h: 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? Timing Hardware management I/O ports I/O memory Interrupt handling Programming I/O Two 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. Using I/O ports linux/ioport.h: 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 (cont’d) asm/io.h: Read/write 8-bit ports (byte width): unsigned intb(unsigned port); unsigned outb(unsigned char byte, unsi
显示全部