2023年全國(guó)碩士研究生考試考研英語(yǔ)一試題真題(含答案詳解+作文范文)_第1頁(yè)
已閱讀1頁(yè),還剩52頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Linux Device Driver,2009/04/08,Reference Book,Another Reference Book,Embedded Linux Primer: A Practical, Real-World Approach By Christopher Hallinan Publisher: Prentice Hall Pub Date: September 18, 2006 Print ISBN-

2、10: 0-13-167984-8 Print ISBN-13: 978-0-13-167984-9 Pages: 576,Website,http://wiki.openwrt.orghttp://www.linuxsir.orghttp://www-128.ibm.com/developerworks/http://lwn.net,Task of Device Driver,Device initializationHard

3、ware 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 S

4、pace,,,,,User space vs Kernel Space program——From 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 p

5、rogram ——From the point of view of CPU,用戶應(yīng)用程序,內(nèi)核程序,Kernel schedule/Kernel API,用戶應(yīng)用程序,用戶應(yīng)用程序,User space program,內(nèi)核程序,內(nèi)核程序,Kernel space program,,,,Mode reg.,Scheduler, change mode reg. to enter different mode,Concept,S

6、oft 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 using pointersphysical addr

7、ess 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

8、Q,,D Q,,Device circuits,Data bus,Address matching circuit,,Address bus,,CPU,,Basic operation of device driver,D Q,,D Q,,D Q,,Data bus,Address matching circuit,,Address bus,,CPU,,Device c

9、ircuits,,,,User space vs Kernel Space program,User space programLimited priorityVirtual run environmentLogic addressKey resource access is difficultUser invoke function directly,Kernel Space programHighest priority

10、Physical run environmentVirtual addressAccess all resourceKernel invoke function,Direct memory access(/dev/kmem),kmfd = open("/dev/kmem", O_RDONLY ); lseek( kmfd, offset,

11、 SEEK_SET ); read( kmfd, byteArray, byteArrayLen ); close(kmfd);,memory is mapped into device file, and can be accessed by file read/writeCan access kernel address(virtual addre

12、ss 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_WRIT

13、E,MAP_SHARED, mem_fd,0xA0000)…close(memfd);,,,0xA0000,0xB0000,,Pointer b,mmap mapping data in file into arrayPhysical memory(accessed by special file /dev/mem)s mapped into array pointed by bNote that value

14、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 memory device file,Directly acce

15、ss 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 us

16、e fopen/fread/fwrite/fclose, because data operation by these function is not fulfilled immediately (buffered),outb()/outw()/inb()/inw() Function,#include #include #include #define BASEPORT 0x378 // printerint main()

17、{ ioperm(BASEPORT, 3, 1));// get access permission outb(0, BASEPORT); usleep(100000); printf("status: %d\n", inb(BASEPORT + 1)); ioperm(BASEPORT, 3, 0));// give up exit(0);},ioperm(fr

18、om,num,turn_on) port address that can be obtained from ioperm is 0x000~0x3FF,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-bitout

19、w(value, port); inw(port); // 16-bitaccess time is about 1us,Access memory directly by user space program——why we need device driver,Share devicesINT management,Safe device access method —— Using Linux device driver,D

20、evice 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,Dev

21、ice 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 spacesimp

22、lepoll mode, slow (response time)difficult to share devices,Access by device driverKernel spacedifficult to debug and programmingcan use fast INT mode, realtimeeasy to implement device sharing (managed by OS),Devic

23、e Classification in Linux,Character deviceMouse、Serial port、joystickBlock devicePrinter , hard diskNetwork deviceAccess by BSD Socket,Character device vs Block device,Character deviceRead/write operation is fulfill

24、ed 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 devi

25、ce driver,Static linked device driverChange configuration file, re-compile and install kernelDynamically instable device driverinsmodinstallrmmodremovallsmodquery,Abstraction of devices under Linux,Device fileO

26、pen/Close/Read/WriteExample/dev/mouse/dev/lp0,universal IF,Device Driver and File,,Device driver,DeviceFile,Create by command mknod,installed by command insmod (or statically compiled into kernel),Application,,Access

27、 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()ope

28、n()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)()

29、…,ISR,Example of LED device driver,,CPU,,struct file_operations LED_fops = { read: LED_read, write: LED_write, open: LED_open, release: LED_release,};int LED_init_module(void)

30、{ SET_MODULE_OWNER(&LED_fops); LED_major = register_chrdev(0, "LED", &LED_fops); LED_off(); LED_status=0; return 0; }void LED_cleanup_module(void) { unregister_chrdev(LED_major,

31、"LED"); }module_init(LED_init_module);module_exit(LED_cleanup_module);,Program list (1),pointers of functions,invoked when device is installed,invoked when device is uninstalled,Tell kernel the name of funct

32、ion to be invoked when install or uninstall device driver,program(2),int LED_open(struct inode *inode, struct file *filp){ printk("LED_open()\n"); MOD_INC_USE_COUNT; return 0;}int LED_releas

33、e(struct inode *inode, struct file *filp){ printk(“LED_release()\n“); MOD_DEC_USE_COUNT; return 0;},new vision Linux doesn’t use this Macro,new vision Linux doesn’t use this Macro,Program List(3),ssize

34、_t LED_read (struct file *filp, char *buf, size_t count, loff_t *f_pos){ int i; for (i=0; i<count; i++) *((char*)(buf+i)) = LED_Status; return count;}ssize_t LED_write(struct file *filp

35、, const char *buf, size_t count, loff_t *f_pos){ int i; for (i=0; i<count; i++) if (*((char*)(buf+i))) LED_on(); else LED_off(); return count;},(*((volatile unsigned int *)(0xXXXXXXXX)

36、)) |= MASK;(*((volatile unsigned int *)(0xXXXXXXXX))) &=~MASK;,,,#ifndef __KERNEL__ #define __KERNEL__#endif#ifndef MODULE #define MODULE#endif#include #include #include #include #include #i

37、nclude #include #include #include #include #include #include #include #include #include #include #include #include MODULE_AUTHOR("Rendong Ying");int LED_major, LED_status;,Program

38、 List(4),Header,Device ID, uer can manually write the number or use automatically assigned number by kernel,Compile(Makefile),CC = arm-elf-linux-gccLD = arm-elf-linux-ldINCLUDE = /usr/local/src/bspLinux/includeLIB_INC

39、 = /usr/local/lib/gcc-lib/arm-elf-linux/2.95.3/includeCFLAGS = -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux -nostdinc -I- -I . -I$(INCLUDE) -idirafter $(LIB_INC)LED.o: LED.c$(CC) $(CF

40、LAGS) -c LED.cclean:rm -f LED.o,generate *.o,install device driver and create device file,chmod +x /tmp/LED.o/sbin/insmod -f ./LED.ocat /proc/devices get device ID instralled into the memorymknod /dev/Lamp c Num1

41、Num2Num1 major device IDNum2 Minor device ID,,force install, ignore version checking,assigned in the program: LED_major,can be any value,User space,install device driver,Kernel Space,Device Driverread()write()open

42、()close(),/sbin/insmod -f ./LED.o,User space,Create device file,Kernel Space,Device Driverread()write()open()close(),cat /proc/devices obtain major device IDmknod /dev/Lamp c Num1 Num2Num1 Major device IDNum2 Mi

43、nor device ID,Device File,Device ID,device ID,Test and use device driver,commandecho 8 > /proc/sys/kernel/printkcat /dev/Lamp cat > /dev/Lamp programvoid main(){ int fd=open(“/dev/Lamp, O_RDWR); write(

44、fd, &data, 1); close(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, &

45、;data, 1); close(fd);},,,,Uninstall device,/sbin/rmmod LEDremove device driverrm -f /dev/Lampdelete device file,Function ofMOD_INC_USE_COUNT;MOD_DEC_USE_COUNT;,User space,Uninstall device,Kernel Space,Device Dr

46、iverread()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)()(*flush)()(*llseek)

47、()…,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, &q

48、uot;USB EP1", 0) < 0) printk("Int. req. failed !\n");free_irq(USB_INTR_SOURCE0, 0);,cat /proc/interrupts check register INT,point to ISR,INT number, determined by hardware

49、,kernel find INT number from INT status reg. Then call registed function on that number,mask same INT,INT ID,ISR pointer,Name,ISR,No return value Fats and smallvoid usb_ep1_int(int irq, void *dev_id

50、, struct pt_regs *regs) { //…},Specific format for function declaration,ISR that receive data,void usb_ep1_int(int irq, void *dev_id, struct pt_regs *regs) {

51、 read_data_from_hardware_FIFO(); send_data_to_buffer();},,,,,,,,,,,,,,,,,,ISR that send data,void usb_ep2_int(int irq, void *dev_id, struct pt_regs *regs) { read_data_from_b

52、uffer(); send_data_to_hardware_FIFO ();},,,,,,,,,,,,,,,,,,read function,ssize_t usb_ep1_read (struct file *filp, char *buf, size_t count,

53、 loff_t *f_pos){ if (data_buffer_empty()) return 0; else copy_data_to_user_space(); return data_copyed;},read function (blocking mode),ssize_t usb_ep1_read (struct file *filp,

54、 char *buf, size_t count, loff_t *f_pos){ while(device_driver_buf_empty()) { if (wait_event_interruptible(q_ep2,

55、 device_driver_buf_not_empty)) return -ERESTARTSYS; } copy_data_to_user_space(); return data_copyed;},wait_queue_head_t rq_EP2;init_waitqueue_head(&rq_EP2);,,,queue when multiple use

56、r application access this device,write function,ssize_t usb_ep2_write (struct file *filp, char *buf, size_t count, loff_t *f_pos){

57、 if (data_buffer_full()) return 0; else copy_data_to_device_driver_buf(); if (no_transmission_now) send_1st_data(); return data_copyed;},copy_from_user(device_driver_buf,

58、user_buf, size);,,Memory allocation by device driver,malloc ?kmallockfreevmallocvfree,,,,,,,,,,,,,,,,,,,Prevent open device several times,int LED_flag;int LED_init_module(void){ LED_flag=0;

59、…} int LED_open(struct inode *inode, struct file *filp){ if (LED_flag=0) { LED_flag=1; MOD_INC_USE_COUNT; return 0; } else return -ENODEV;}int LED_release(struct in

60、ode *inode, struct file *filp){ LED_flag=0; MOD_DEC_USE_COUNT; return 0;},need semphora to avoid access conflict,Manage several device by one device driver,Serial Port Device Driver,UART 0,,,UART 0,,A

61、pplication,User space,Create device file,Kernel Space,Device Driverread()write()open()close(),cat /proc/devices Obtain device IDmknod /dev/Lamp c Num1 Num2Num1 Major device IDNum2 Minor device ID,,,,Minor dev. ID

62、 is used to identify devices that is opened,,,Manage several device by one device driver,int dev_open(struct inode *inode, struct file *filp){ int minor = MINOR(inode->i_rdev); filp->private_dat

63、a=sub_dev_dat[minor]; …}ssize_t dev_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos){ switch(*(filp->private_dat

64、a)) { … }},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,Debu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論