资源预览内容
第1页 / 共42页
第2页 / 共42页
第3页 / 共42页
第4页 / 共42页
第5页 / 共42页
第6页 / 共42页
第7页 / 共42页
第8页 / 共42页
第9页 / 共42页
第10页 / 共42页
亲,该文档总共42页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Linux Device DriverWriting a real world device driver for embedded Linux.Outline Concept Kernel Module Char Device Driver Interrupt Handling I/O Management Allocating Memory Block Device Driver Network Device DriverConcept The role of device driver To allow interaction with hardware devices. Providing mechanism, not policy. What capabilities are to be provided? Mechanism How those capabilities can be used? Policy Writing a Linux device driver Pre-requisites C programming Microprocessor programming Important concepts User space vs. kernel spaceConcept (Cont.) Execution paths: From user to kernelConcept (Cont.) Classes of devices Characters devices Can be accessed as a stream of bytes. Such a driver usually implements at least the open, close, read, and write system calls. Example: RTC driver. Block devices A device (e.g., a disk) that can host a filesystem. Example: Ramdisk driver. Network interfaces In charge of sending and receiving data packets, driven by the network subsystem of the kernel. Example: Network card driver.Concept (Cont.) A device driver is a kernel module. A kernel module is a device driver? Not necessarily so. Example: ext3 file system moduleOutline Concept Kernel Module Character Device Driver Interrupt Handling I/O Management Allocating Memory Block Device Driver Network Device DriverKernel Module The first kernel module “Hello, world”#include #include static int hello_init(void) printk(KERN_ALERT “Hello, worldn”);return 0; static void hello_exit(void) printk(KERN_ALERT “Goodbye, cruel worldn”); module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE(“GPL”);Kernel Module (Cont.) How to compile # gcc -c hello.c -D_KERNEL_ -DMODULE -o hello.o How to insert into kernel # insmod ./hello.o How to remove from kernel # rmmod helloKernel Module (Cont.) Kernel module vs Application Application Run in user space. Perform a task from beginning to end. Linked to the appropriate library such as libc. Kernel Module Run in kernel space. Register itself in order to serve future requests. Linked only to the kernel, and the only functions it can call are the ones exported by the kernel.Kernel Module (Cont.)Kernel Module (Cont.) Just about all module code has the following: #include #include module.h contains a great many definitions of symbols and functions needed by loadable modules. init.h is needed to specify your initialization and cleanup functions.Kernel Module (Cont.) You should specify which license applies to your code. Doing so is just a matter of including one line: MODULE_LICENSE(“GPL”); Other descriptive definitions that can be contained within a module include MODULE_AUTHOR, MODULE_DESCRIPTION, MODULE_VERSION etc.Kernel Module (Cont.) Module initialization The actual definition of the initialization function always looks like module_init adds a special section to the modules object code stating where the modules initialization function is to be found.static int _init initialization_function(void) /* Initialization code here */ module_init(initialization_function);Kernel Module (Cont.) Module initialization In initialization function, you can register many different type of facilities, including different kind of devices, file systems, and more. Most registration functions are prefixed with register_ , such as register_chrdev() register_blkdev() register_netdev()Kernel Module (Cont.) Module cleanup The cleanup function is defined as In cleanup function, youre supposed to unregister interfaces and return all resources to the system. If your module is built directly into kernel or the kernel is configured to disallow the unloading of modules, functions marked _exit are simply discarded.static void _exit cleanup_function(void) /* Cleanup code here */ module_exit(cleanup_function);Kernel Module (Cont.) printk() Kernel version of printf(). Priority of kernel messages can be specified with the following symbols defined in . KERN_EMERG: Emergency message KERN_ALERT: Alert message KERN_CRIT: Critical situation KERN_ERR: Error report KERN_WARNING: Warning message KERN_NOTICE: Noticeable message KERN_INFO: Information KERN_DEBUG: Debug message Does not support floating point numbers. Example: printk(KERN_DEBUG “line %s:%in”, _FILE_, _LINE_);PriorityLowHighKernel Module (Cont.) Error handling Error recovery is sometimes best handled with the goto statement.int _init my_init_function(void) int err;/* registration takes a pointer and a name */err = register_this(ptr1, “skull“);if (err) goto fail_this; err = register_that(ptr2, “skull“);if (err) goto fail_that;err = register_those(ptr3, “skull“);if (err) goto fail_those;return 0; /* success */ fail_those: unregister_that(ptr2, “skull“); fail_that: unregister_this(ptr1, “skull“); fail_this: return err; /* propagate t
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号