1、Linux Device Driver,2009/04/08,Reference Book,Another Reference Book,Embedded Linux Primer: A Practical, Real-World Approach By ChristopherHallinan Publisher: Prentice Hall Pub Date: September 18, 2006 Print ISBN-10: 0-13-167984-8 Print ISBN-13: 978-0-13-167984-9 Pages: 576,Website,http:/wiki.openwr
2、t.orghttp:/www.linuxsir.orghttp:/ of Device Driver,Device initializationHardware operation and managementData transfer between kernel space and user spaceData exchange between hardware and kernel space,Function of device driver,Hardware,Device Driver,Application,Buffer,User space,Kernel Space,User s
3、pace vs Kernel Space programFrom the point of view of CPU,mode reg.,memory address mapping,memory access surveillance,Execution core,Memory,address,data,INT,Concept,MMU,User space vs Kernel Space program From the point of view of CPU,用户应用程序,内核程序,Kernel schedule/Kernel API,用户应用程序,用户应用程序,User space pr
4、ogram,内核程序,内核程序,Kernel space program,Mode reg.,Scheduler, change mode reg. to enter different mode,Concept,Soft interrupt,Address Mapping and accessing of the physical address,Physical Address space,User processs1,User processs2,User process 3,Virtual address mapping,user program access virtual usin
5、g pointersphysical address of IO device cannot be accessed by user program directly,Virtual address mapping,Virtual address mapping,Basic operation of device driver,Device controller is often mapped into memory address,D Q,D Q,D Q,Device circuits,Data bus,Address matching circuit,Address bus,CPU,Bas
6、ic operation of device driver,D Q,D Q,D Q,Data bus,Address matching circuit,Address bus,CPU,Device circuits,User space vs Kernel Space program,User space programLimited priorityVirtual run environmentLogic addressKey resource access is difficultUser invoke function directly,Kernel Space programHighe
7、st priorityPhysical run environmentVirtual addressAccess all resourceKernel invoke function,Direct memory access(/dev/kmem),kmfd=open(/dev/kmem,O_RDONLY);lseek(kmfd,offset,SEEK_SET);read(kmfd,byteArray,byteArrayLen);close(kmfd);,memory is mapped into device file, and can be accessed by file read/wri
8、teCan access kernel address(virtual address of kernel)Most started from 0xC0000000,memory,offset,Access Physical address directly(/dev/mem),mem_fd=open(/dev/mem,O_RDONLY);b=mmap(0, 0x10000, PROT_READ|PROT_WRITE,MAP_SHARED, mem_fd,0xA0000)close(memfd);,0xA0000,0xB0000,Pointer b,mmap mapping data in f
9、ile into arrayPhysical memory(accessed by special file /dev/mem)s mapped into array pointed by bNote that value of B may not be 0xA0000, its value is a virtual address coressponding to the physical address 0xA0000under Linux, /dev/mem is for special memory access, such as video memory,May not bye me
10、mory device file,Directly access IO port(/dev/port),port_fd=open(/dev/port, O_RDWR);lseek(port_fd,port_addr,SEEK_SET);read(port_fd,);write(port_fd,); close(port_fd);,Note, not use fopen/fread/fwrite/fclose, because data operation by these function is not fulfilled immediately (buffered),outb()/outw(
11、)/inb()/inw() Function,#include #include #include #define BASEPORT 0x378 / printerint main() ioperm(BASEPORT, 3, 1);/ get access permission outb(0, BASEPORT); usleep(100000); printf(status: %dn, inb(BASEPORT + 1); ioperm(BASEPORT, 3, 0);/ give up exit(0);,ioperm(from,num,turn_on) port address that c
12、an be obtained from ioperm is 0x0000x3FF,use iopl() can obtain all port addressmust run as rootuse “gcc -02 o xxx.elf xxx.c” to compile,outb(value, port); inb(port); / 8-bitoutw(value, port); inw(port); / 16-bitaccess time is about 1us,Access memory directly by user space programwhy we need device d
13、river,Share devicesINT management,Safe device access method Using Linux device driver,Device driver can access device address by pointerdevice driver also use virtual address but more real than user application(device address mapping can be found in transplanting Linux Kernel),Physical address space
14、,Device Driver,Virtual address mapping,Device address Space,Device address mapping,Device Driver,Virtual address mapping,Device address mapping,Direct access IO port vs Using device driver,Direct IO AccessUser spacesimplepoll mode, slow (response time)difficult to share devices,Access by device driv
15、erKernel spacedifficult to debug and programmingcan use fast INT mode, realtimeeasy to implement device sharing (managed by OS),Device Classification in Linux,Character deviceMouse、Serial port、joystickBlock devicePrinter , hard diskNetwork deviceAccess by BSD Socket,Character device vs Block device,
16、Character deviceRead/write operation is fulfilled immediatelydata buffer is optionalADC/DAC、button、LED、sensor,Block deviceneed data buffer to reduce device write/read operationFor slow device such as hard disk,Dynamically installable device vs static linked device driver,Static linked device driverC
17、hange configuration file, re-compile and install kernelDynamically instable device driverinsmodinstallrmmodremovallsmodquery,Abstraction of devices under Linux,Device fileOpen/Close/Read/WriteExample/dev/mouse/dev/lp0,universal IF,Device Driver and File,Device driver,DeviceFile,Create by command mkn
18、od,installed by command insmod (or statically compiled into kernel),Application,Access by open/read/write/close API,Find real device river by major device number,Similar method,User space,Device Driver and File,Kernel Space,Deevice Driverread()write()open()close(),User applicationread()write()open()
19、close(),Device File,device ID,device ID,Name of device file,name of vice file,Structure of Device Driver,Register and unregister,device file operation function (API)(*open)()(*write)()(*flush)()(*llseek)(),ISR,Example of LED device driver,CPU,struct file_operations LED_fops = read: LED_read, write:
20、LED_write, open: LED_open, release: LED_release,;int LED_init_module(void) SET_MODULE_OWNER(,Program list (1),pointers of functions,invoked when device is installed,invoked when device is uninstalled,Tell kernel the name of function to be invoked when install or uninstall device driver,program(2),in
21、t LED_open(struct inode *inode, struct file *filp) printk(LED_open()n); MOD_INC_USE_COUNT; return 0;int LED_release(struct inode *inode, struct file *filp) printk(“LED_release()n“); MOD_DEC_USE_COUNT; return 0;,new vision Linux doesnt use this Macro,new vision Linux doesnt use this Macro,Program Lis
22、t(3),ssize_t LED_read (struct file *filp, char *buf, size_t count, loff_t *f_pos) int i; for (i=0; icount; i+) *(char*)(buf+i) = LED_Status; return count;ssize_t LED_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos) int i; for (i=0; i /proc/sys/kernel/printkcat /dev/Lamp cat /de
23、v/Lamp programvoid main() int fd=open(“/dev/Lamp, O_RDWR); write(fd, ,User space,Use of device driver,Kernel Space,Device Driver read() write() open() close(),Device File,void main() int fd=open(“/dev/Lamp, O_RDWR); write(fd, ,Uninstall device,/sbin/rmmod LEDremove device driverrm -f /dev/Lampdelete
24、 device file,Function ofMOD_INC_USE_COUNT;MOD_DEC_USE_COUNT;,User space,Uninstall device,Kernel Space,Device Driverread()write()open()close(),Device File,/sbin/rmmod LED,x,x,rm -f /dev/Lamp,Complex Device Driver,register and unregister(device and INT),Divice operation API(function)(*open)()(*write)(
25、)(*flush)()(*llseek)(),ISR,Kernel space buffer,Data in user space,Complex Device Driver(USB Device),Application of IRQif (request_irq(USB_INTR_SOURCE1, usb_ep1_int, SA_INTERRUPT, USB EP1, 0) i_rdev); filp-private_data=sub_dev_datminor; ssize_t dev_write(struct file *filp, const char *buf, size_t cou
26、nt, loff_t *f_pos) switch(*(filp-private_data) ,Allocate data space for each device,use data space allocated for the opened device,Debug of Character device driver,Kernel,Device Driver,Device Read,Device Write,Device Open,Device Close,Debug Read,Debug Write,Debug Open,Debug Close,设备文件 1,设备文件 2,Application(using device),debug and monitor,Virtual Serial port, canbe access by minicom,