Linux设备驱动程序  分类:其他 | 上传于: 2019-12-17 03:30:26
——From the point of view of CPU\r\nmode reg.\r\nmemory \raddress \rmapping\r\nmemory \raccess \rsurveillance \r\nExecution\r core\r\nMemory\r\naddress\r\ndata\r\nINT\r\nConcept\r\nMMU\r\n" 10."User space vs Kernel Space program ——From the point of view of CPU\r\n用户应用程序\r\n内核程序\r\nKernel schedule/\rKernel API\r\n用户应用程序\r\n用户应用程序\r\nUser space\r program\r\n内核程序\r\n内核程序\r\nKernel space \rprogram\r\nMode reg.\r\nScheduler 11. change mode reg. to enter different mode\r\nConcept\r\nSoft interrupt\r\n" 12."Address Mapping and accessing of the physical address\r\nPhysical Address space\r\nUser \rprocesss1\r\nUser \rprocesss2\r\nUser \rprocess 3\r\nVirtual address \rmapping\r\nuser program access virtual using pointers\rphysical address of IO device cannot be accessed by user program directly\r\nVirtual address \rmapping\r\nVirtual address \rmapping\r\n" 13."Basic operation of device driver\r\nDevice controller is often mapped into memory address\r\nD Q\r\r\r\nD Q\r\r\r\nD Q\r\r\r\nDevice \rcircuits\r\nData\r bus\r\nAddress \rmatching \rcircuit\r\n\r\rAddress\r bus\r\nCPU\r\n" 14."Basic operation of device driver\r\nD Q\r\r\r\nD Q\r\r\r\nD Q\r\r\r\nData \rbus\r\nAddress \rmatching \rcircuit\r\nAddress \rbus\r\nCPU\r\nDevice \rcircuits\r\n" 15."User space vs Kernel Space program\r\nUser space program\rLimited priority\rVirtual run environment\rLogic address\rKey resource access is difficult\rUser invoke function directly\r\nKernel Space program\rHighest priority\rPhysical run environment\rVirtual address\rAccess all resource\rKernel invoke function\r\r \r\n" 16."Direct memory access(/dev/kmem)\r\nkmfd = open(\"/dev/kmem\" 17. O_RDONLY ); lseek( kmfd 18. offset 19. SEEK_SET ); read( kmfd 20. byteArray 21. byteArrayLen ); close(kmfd);\r\nmemory is mapped into device file 22. and can be accessed by file read/write\rCan access kernel address(virtual address of kernel)\rMost started from 0xC0000000\r\nmemory\r\r\r\r\r\r\r\r\r\r\r\noffset\r\n" 23."Access Physical address directly(/dev/mem)\r\nmem_fd = open(\"/dev/mem\" 24. O_RDONLY ); b=mmap(0 25. 0x10000 26. PROT_READ|PROT_WRITE 27.MAP_SHARED 28. mem_fd 29.0xA0000)\r…\rclose(memfd);\r\r\n0xA0000\r\n0xB0000\r\nPointer b\r\nmmap mapping data in file into array\r\rPhysical memory(accessed by special file /dev/mem)s mapped into array pointed by b\r\rNote that value of B may not be 0xA0000 30. its value is a virtual address coressponding to the physical address 0xA0000\r\runder Linux 31. /dev/mem is for special memory access 32. such as video memory\r\nMay not bye memory device file \r\n" 33."Directly access IO port(/dev/port)\r\nport_fd = open(\"/dev/port\" 34. O_RDWR); lseek(port_fd 35. port_addr 36. SEEK_SET); read(port_fd 37. …);\rwrite(port_fd 38. …); close(port_fd);\r\nNote 39. not use fopen/fread/fwrite/fclose 40. because data operation by these function is not fulfilled immediately (buffered)\r\n" 41."outb()/outw()/inb()/inw() Function\r\n#include \r#include \r#include \r#define BASEPORT 0x378 // printer\rint main()\r{\r ioperm(BASEPORT 42. 3 43. 1));\t// get access permission\r outb(0 44. BASEPORT);\r usleep(100000);\r printf(\"status: %d\\n\" 45. inb(BASEPORT 1));\r ioperm(BASEPORT 46. 3 47. 0));\t// give up\r exit(0);\r}\r\nioperm(from 48.num 49.turn_on) \rport address that can be obtained from ioperm is 0x000~0x3FF,use iopl() can obtain all port address\rmust run as root\ruse “gcc -02 –o xxx.elf xxx.c” to compile\r\noutb(value 50. port); inb(port); // 8-bit\routw(value 51. port); inw(port); // 16-bit\raccess time is about 1us\r\n" 52."Access memory directly by user space program——why we need device driver\r\nShare devices\rINT management\r\n" 53."Safe device access method —— Using Linux device driver\r\nDevice driver can access device address by pointer\rdevice driver also use virtual address but more ‘real’ than user application(device address mapping can be found in transplanting Linux Kernel)\r\nPhysical address space\r\nDevice Driver\r\nVirtual address mapping\r\nDevice address Space\r\nDevice address mapping\r\nDevice Driver\r\nVirtual address mapping\r\nDevice address mapping\r\n" 54."Direct access IO port vs Using device driver\r\nDirect IO Access\rUser space\rsimple\rpoll mode 55. slow (response time)\rdifficult to share devices\r\r\nAccess by device driver\rKernel space\rdifficult to debug and programming\rcan use fast INT mode 56. realtime\reasy to implement device sharing (managed by OS)\r\n" 57."Device Classification in Linux\r\nCharacter device\rMouse、Serial port、joystick\rBlock device\rPrinter 58. hard disk\rNetwork device\rAccess by BSD Socket\r\n" 59."Character device vs Block device\r\nCharacter device\rRead/write operation is fulfilled immediately\rdata buffer is optional\r\rADC/DAC、button、LED、sensor\r\nBlock device\rneed data buffer to reduce device write/read operation\rFor slow device such as hard disk\r\n" 60."Dynamically installable device vs static linked device driver\r\nStatic linked device driver\rChange configuration file 61. re-compile and install kernel\rDynamically instable device driver\rinsmod\tinstall\rrmmod\tremoval\rlsmod\t\tquery\r\n" 62."Abstraction of devices under Linux\r\n\t\tDevice file\rOpen/Close/Read/Write\rExample\r/dev/mouse\r/dev/lp0\r\nuniversal IF\r\n" 63."Device Driver and File\r\nDevice driver\r\nDevice\rFile\r\nCreate by command mknod\r\ninstalled by command insmod (or statically compiled into kernel)\r\nApplication\r\nAccess by open/read/write/close API\r\nFind real device river by major device number\r\nSimilar method\r\n" 64."User space \r\r\r\r\r\r\r\nDevice Driver and File\r\nKernel Space \r\r\r\nDeevice Driver\rread()\rwrite()\ropen()\rclose()\r\nUser \rapplication\rread()\rwrite()\ropen()\rclose()\r\nDevice File\r\ndevice ID\r\ndevice ID\r\nName of device file\r\nname of vice file \r\n" 65."Structure of Device Driver\r\nRegister and unregister\r\ndevice file operation \rfunction (API)\r(*open)()\r(*write)()\r(*flush)()\r(*llseek)()\r…\r\nISR\r\n" 66."Example of LED device driver\r\nCPU\r\n" 67."struct file_operations LED_fops = \r{ read: LED_read 68.\r write: LED_write 69.\r open: LED_open 70.\r release: LED_release 71.\r};\rint LED_init_module(void) \r{ SET_MODULE_OWNER( -->
——From the point of view of CPU
mode reg.
memory
address
mapping
memory
access
surveillance
Execution
core
Memory
address
data
INT
Concept
MMU
" 10."User space vs Kernel Space program ——From the point of view of CPU
用户应用程序
内核程序
Kernel schedule/
Kernel API
用户应用程序
用户应用程序
User space
program
内核程序
内核程序
Kernel space
program
Mode reg.
Scheduler 11. change mode reg. to enter different mode
Concept
Soft interrupt
" 12."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 pointers
physical address of IO device cannot be accessed by user program directly
Virtual address
mapping
Virtual address
mapping
" 13."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
" 14."Basic operation of device driver
D Q
D Q
D Q
Data
bus
Address
matching
circuit
Address
bus
CPU
Device
circuits
" 15."User space vs Kernel Space program
User space program
Limited priority
Virtual run environment
Logic address
Key resource access is difficult
User invoke function directly
Kernel Space program
Highest priority
Physical run environment
Virtual address
Access all resource
Kernel invoke function
" 16."Direct memory access(/dev/kmem)
kmfd = open(\"/dev/kmem\" 17. O_RDONLY ); lseek( kmfd 18. offset 19. SEEK_SET ); read( kmfd 20. byteArray 21. byteArrayLen ); close(kmfd);
memory is mapped into device file 22. and can be accessed by file read/write
Can access kernel address(virtual address of kernel)
Most started from 0xC0000000
memory
offset
" 23."Access Physical address directly(/dev/mem)
mem_fd = open(\"/dev/mem\" 24. O_RDONLY ); b=mmap(0 25. 0x10000 26. PROT_READ|PROT_WRITE 27.MAP_SHARED 28. mem_fd 29.0xA0000)
…
close(memfd);
0xA0000
0xB0000
Pointer b
mmap mapping data in file into array
Physical memory(accessed by special file /dev/mem)s mapped into array pointed by b
Note that value of B may not be 0xA0000 30. its value is a virtual address coressponding to the physical address 0xA0000
under Linux 31. /dev/mem is for special memory access 32. such as video memory
May not bye memory device file
" 33."Directly access IO port(/dev/port)
port_fd = open(\"/dev/port\" 34. O_RDWR); lseek(port_fd 35. port_addr 36. SEEK_SET); read(port_fd 37. …);
write(port_fd 38. …); close(port_fd);
Note 39. not use fopen/fread/fwrite/fclose 40. because data operation by these function is not fulfilled immediately (buffered)
" 41."outb()/outw()/inb()/inw() Function
#include
#include
#include
#define BASEPORT 0x378 // printer
int main()
{
ioperm(BASEPORT 42. 3 43. 1));\t// get access permission
outb(0 44. BASEPORT);
usleep(100000);
printf(\"status: %d\
\" 45. inb(BASEPORT 1));
ioperm(BASEPORT 46. 3 47. 0));\t// give up
exit(0);
}
ioperm(from 48.num 49.turn_on)
port address that can be obtained from ioperm is 0x000~0x3FF,use iopl() can obtain all port address
must run as root
use “gcc -02 –o xxx.elf xxx.c” to compile
outb(value 50. port); inb(port); // 8-bit
outw(value 51. port); inw(port); // 16-bit
access time is about 1us
" 52."Access memory directly by user space program——why we need device driver
Share devices
INT management
" 53."Safe device access method —— Using Linux device driver
Device driver can access device address by pointer
device driver also use virtual address but more ‘real’ than user application(device address mapping can be found in transplanting Linux Kernel)
Physical address space
Device Driver
Virtual address mapping
Device address Space
Device address mapping
Device Driver
Virtual address mapping
Device address mapping
" 54."Direct access IO port vs Using device driver
Direct IO Access
User space
simple
poll mode 55. slow (response time)
difficult to share devices
Access by device driver
Kernel space
difficult to debug and programming
can use fast INT mode 56. realtime
easy to implement device sharing (managed by OS)
" 57."Device Classification in Linux
Character device
Mouse、Serial port、joystick
Block device
Printer 58. hard disk
Network device
Access by BSD Socket
" 59."Character device vs Block device
Character device
Read/write operation is fulfilled immediately
data buffer is optional
ADC/DAC、button、LED、sensor
Block device
need data buffer to reduce device write/read operation
For slow device such as hard disk
" 60."Dynamically installable device vs static linked device driver
Static linked device driver
Change configuration file 61. re-compile and install kernel
Dynamically instable device driver
insmod\tinstall
rmmod\tremoval
lsmod\t\tquery
" 62."Abstraction of devices under Linux
\t\tDevice file
Open/Close/Read/Write
Example
/dev/mouse
/dev/lp0
universal IF
" 63."Device Driver and File
Device driver
Device
File
Create by command mknod
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
" 64."User space
Device Driver and File
Kernel Space
Deevice Driver
read()
write()
open()
close()
User
application
read()
write()
open()
close()
Device File
device ID
device ID
Name of device file
name of vice file
" 65."Structure of Device Driver
Register and unregister
device file operation
function (API)
(*open)()
(*write)()
(*flush)()
(*llseek)()
…
ISR
" 66."Example of LED device driver
CPU
" 67."struct file_operations LED_fops =
{ read: LED_read 68.
write: LED_write 69.
open: LED_open 70.
release: LED_release 71.
};
int LED_init_module(void)
{ SET_MODULE_OWNER(
-->
1."Linux Device Driver
2009/04/08
" 2."Reference Book
" 3."Another Reference Book
Embedded Linux Primer: A Practical 4. Real-World Approach By Christopher Hallinan
Publisher: Prentice Hall Pub Date: September 18 5. 2006 Print ISBN-10: 0-13-167984-8 Print ISBN-13: 978-0-13-167984-9 Pages: 576
" 6."Website
http://wiki.openwrt.org
http://www.linuxsir.org
http://www-128.ibm.com/developerworks/
http://lwn.net
" 7."Task of Device Driver
Device initialization
Hardware operation and management
Data transfer between kernel space and user space
Data exchange between hardware and kernel space
" 8."Function of device driver
Hardware
Device Driver
Application
Buffer
User space
Kernel Space
" 9."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
" 10."User space vs Kernel Space program ——From the point of view of CPU
用户应用程序
内核程序
Kernel schedule/
Kernel API
用户应用程序
用户应用程序
User space
program
内核程序
内核程序
Kernel space
program
Mode reg.
Scheduler 11. change mode reg. to enter different mode
Concept
Soft interrupt
" 12."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 pointers
physical address of IO device cannot be accessed by user program directly
Virtual address
mapping
Virtual address
mapping
" 13."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
" 14."Basic operation of device driver
D Q
D Q
D Q
Data
bus
Address
matching
circuit
Address
bus
CPU
Device
circuits
" 15."User space vs Kernel Space program
User space program
Limited priority
Virtual run environment
Logic address
Key resource access is difficult
User invoke function directly
Kernel Space program
Highest priority
Physical run environment
Virtual address
Access all resource
Kernel invoke function
" 16."Direct memory access(/dev/kmem)
kmfd = open(\"/dev/kmem\" 17. O_RDONLY ); lseek( kmfd 18. offset 19. SEEK_SET ); read( kmfd 20. byteArray 21. byteArrayLen ); close(kmfd);
memory is mapped into device file 22. and can be accessed by file read/write
Can access kernel address(virtual address of kernel)
Most started from 0xC0000000
memory
offset
" 23."Access Physical address directly(/dev/mem)
mem_fd = open(\"/dev/mem\" 24. O_RDONLY ); b=mmap(0 25. 0x10000 26. PROT_READ|PROT_WRITE 27.MAP_SHARED 28. mem_fd 29.0xA0000)
…
close(memfd);
0xA0000
0xB0000
Pointer b
mmap mapping data in file into array
Physical memory(accessed by special file /dev/mem)s mapped into array pointed by b
Note that value of B may not be 0xA0000 30. its value is a virtual address coressponding to the physical address 0xA0000
under Linux 31. /dev/mem is for special memory access 32. such as video memory
May not bye memory device file
" 33."Directly access IO port(/dev/port)
port_fd = open(\"/dev/port\" 34. O_RDWR); lseek(port_fd 35. port_addr 36. SEEK_SET); read(port_fd 37. …);
write(port_fd 38. …); close(port_fd);
Note 39. not use fopen/fread/fwrite/fclose 40. because data operation by these function is not fulfilled immediately (buffered)
" 41."outb()/outw()/inb()/inw() Function
#include
#include
#include
#define BASEPORT 0x378 // printer
int main()
{
ioperm(BASEPORT 42. 3 43. 1));\t// get access permission
outb(0 44. BASEPORT);
usleep(100000);
printf(\"status: %d\
\" 45. inb(BASEPORT 1));
ioperm(BASEPORT 46. 3 47. 0));\t// give up
exit(0);
}
ioperm(from 48.num 49.turn_on)
port address that can be obtained from ioperm is 0x000~0x3FF,use iopl() can obtain all port address
must run as root
use “gcc -02 –o xxx.elf xxx.c” to compile
outb(value 50. port); inb(port); // 8-bit
outw(value 51. port); inw(port); // 16-bit
access time is about 1us
" 52."Access memory directly by user space program——why we need device driver
Share devices
INT management
" 53."Safe device access method —— Using Linux device driver
Device driver can access device address by pointer
device driver also use virtual address but more ‘real’ than user application(device address mapping can be found in transplanting Linux Kernel)
Physical address space
Device Driver
Virtual address mapping
Device address Space
Device address mapping
Device Driver
Virtual address mapping
Device address mapping
" 54."Direct access IO port vs Using device driver
Direct IO Access
User space
simple
poll mode 55. slow (response time)
difficult to share devices
Access by device driver
Kernel space
difficult to debug and programming
can use fast INT mode 56. realtime
easy to implement device sharing (managed by OS)
" 57."Device Classification in Linux
Character device
Mouse、Serial port、joystick
Block device
Printer 58. hard disk
Network device
Access by BSD Socket
" 59."Character device vs Block device
Character device
Read/write operation is fulfilled immediately
data buffer is optional
ADC/DAC、button、LED、sensor
Block device
need data buffer to reduce device write/read operation
For slow device such as hard disk
" 60."Dynamically installable device vs static linked device driver
Static linked device driver
Change configuration file 61. re-compile and install kernel
Dynamically instable device driver
insmod\tinstall
rmmod\tremoval
lsmod\t\tquery
" 62."Abstraction of devices under Linux
\t\tDevice file
Open/Close/Read/Write
Example
/dev/mouse
/dev/lp0
universal IF
" 63."Device Driver and File
Device driver
Device
File
Create by command mknod
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
" 64."User space
Device Driver and File
Kernel Space
Deevice Driver
read()
write()
open()
close()
User
application
read()
write()
open()
close()
Device File
device ID
device ID
Name of device file
name of vice file
" 65."Structure of Device Driver
Register and unregister
device file operation
function (API)
(*open)()
(*write)()
(*flush)()
(*llseek)()
…
ISR
" 66."Example of LED device driver
CPU
" 67."struct file_operations LED_fops =
{ read: LED_read 68.
write: LED_write 69.
open: LED_open 70.
release: LED_release 71.
};
int LED_init_module(void)
{ SET_MODULE_OWNER(
查看更多