LinuxDeviceDriver

上传人:jiups****uk12 文档编号:45417171 上传时间:2018-06-16 格式:PPT 页数:42 大小:472.50KB
返回 下载 相关 举报
LinuxDeviceDriver_第1页
第1页 / 共42页
LinuxDeviceDriver_第2页
第2页 / 共42页
LinuxDeviceDriver_第3页
第3页 / 共42页
LinuxDeviceDriver_第4页
第4页 / 共42页
LinuxDeviceDriver_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《LinuxDeviceDriver》由会员分享,可在线阅读,更多相关《LinuxDeviceDriver(42页珍藏版)》请在金锄头文库上搜索。

1、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. Providi

2、ng 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 kerne

3、lConcept (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. Networ

4、k 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

5、 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) pri

6、ntk(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 (Con

7、t.) 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 ar

8、e 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 fun

9、ctions.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

10、(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 he

11、re */ 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 regist

12、er_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 disa

13、llow 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

14、 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_DEBU

15、G “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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号