170 likes | 323 Views
AT91SAM Linux Introduction. Linux Device Driver Introduction. Afternoon Agenda. Linux Kernel Introduction. Linux Device Driver Introduction. Linux Device Driver Getting Started. Hands-on 06. Character Device Driver Developing. Hands-on 07. Platform Device Driver Developing. Hands-on 08.
E N D
AT91SAM Linux Introduction Linux Device Driver Introduction
Afternoon Agenda Linux Kernel Introduction Linux Device Driver Introduction Linux Device Driver Getting Started Hands-on 06 Character Device Driver Developing Hands-on 07 Platform Device Driver Developing Hands-on 08 Add Interrupt Handling in Driver Hands-on 09
Outlines Understanding Linux Device Driver Driver Developing Rules How Is Driver Built Simple Driver Sample Passing parameter to driver module Character device driver Device Driver Model Summary
Understanding Linux Device Driver What’s device driver Device drivers provide hardware device operation for kernel and applications. Device drivers provide connection for Linux and hardware. Device cannot work without its driver Application User space System call interface Driver Kernel Hardware
Understanding Linux Device Driver Device drivers are associated with hardware devices • Such as display, LED, serial port, USB, SD… Device driver classification: • According to memory space • User space driver • such as printer, X drivers • Kernel space driver • Such as display, audio According to access types • Character driver • Access through a sequential flow of individual characters. • Such as keyboard, mouse, Bluetooth port. • Block driver • Access through data blocks. • Such as hard disk, floppy, ram disk. • Other driver types • Network drivers. Such as eth0, usbnet. • Intermediate drivers just interfacing with other drivers
Supported Hardware Architectures major number minor number Most devices are files in Linux (except network device) List devices in (ls –l /dev) crw-rw-rw- 1 root tty 4, 64 2009-12-03 01:48 ttyS0 crw--w---- 1 root root 4, 65 2009-12-03 01:48 ttyS1 …… …… brw-rw---- 1 root disk 8, 0 2009-12-03 01:48 sda brw-rw---- 1 root disk 8, 1 2009-12-03 01:48 sda1 c means character device b means block device Any device will be associated to 2 numbers: major number and minor number. • One driver can be designed for many devices • Major number is associated to the driver for this device • Minor number is uniquely associated to the device • Please refer to kernel source file Documentation/devices.txt
Understanding Linux Device Driver – List devices cat /proc/devices major number registered name Character devices: 1 mem 2 pty 3 ttyp 4 /dev/vc/0 4 tty 4 ttyS 5 /dev/tty 5 /dev/console 5 /dev/ptmx 7 vcs 10 misc 13 input 14 sound 29 fb 90 mtd 116 alsa 128 ptm 136 pts 180 usb 189 usb_device 253 usb_endpoint 254 rtc Block devices: 1 ramdisk 259 blkext 7 loop 8 sd 31 mtdblock 65 sd 66 sd 67 sd 68 sd 69 sd 70 sd 71 sd 128 sd 129 sd 130 sd 131 sd 132 sd 133 sd 134 sd 135 sd 179 mmc
Rules Use C language and assembly language No standard C library functions • C library is implemented on the top of the kernel • No printf(), strcat(), etc. Linux kernel source provides some C functions for driver • printk() function is similar to printf() • Need include kernel header file Never use floating point numbers in kernel code Follow coding style of kernel • Refer to Documentation/CodingStyle Define all symbols as static, except exported ones • Avoid namespace pollution
Configure Linux kernel Directly compiling the driver to kernel image • If existing many drivers, the kernel Image will be very big • If add or remove functionality in running kernel, we have to rebuild the kernel and reboot it. Driver source code *.c Makefile Includes build options and dependency Kernel source code KConfig Includes build options and dependency Cross tool chain Compile driver module Linux Kernel Image
How Is Driver Build Linux Module • Each piece of code that can be added to the kernel at runtime is called module. • You can add or remove functionality to the kernel while the system is up and running • Each module is made up of object code (not linked into a complete executable) that can be dynamically linked to the running kernel by the insmod (or modprobe) program and can be unlinked by the rmmod program Driver source code *.c Makefile Includes build options and dependency Cross tool chain Compile driver module Running Linux Kernel Load Module Device driver *.ko
Character Device Driver What is character driver? • Character device can be accessed as a stream of bytes(like a file) • Implement at least the open, releaseand read or write system calls • Accessed by file system nodes, such as /dev/ttyS1 Most drivers are implemented as character drivers
Character Device Driver - Overview How application operates device • Need know the device name • Open device file • Read/write the device file • Send ioctl to the device file • Close device file What kernel will do? • Translate device file name to major & minor number • major number indicates which driver is in charge of the operations of this device • Kernel pass the device operation request to the corresponding driver • Provide APIs for user space and kernel space memory transfer How driver act for the operations • Driver provide the operation handler for such kind of operation • Send back the data to application through kernel API (since driver is in kernel space and application is in user space)
Application read/write/ioctl… /dev/xyz mknod to create a device name User space major & minor number Character Driver read handler ioctl handler write handler Other operation handler Kernel Hardware
Device Driver Model The 2.6 kernel included a unified device model • Instead of having different mechanisms in various subsystems, the device model unifies the description of the device and their topology • Bus driver • Non-bus driver • platform driver / platform device Benefit of using unified device driver model • Reduce code duplication • Unified device mode provides common functions • Categorize drivers by classes • Detect device and enumerate them • Manage all the drivers and devices’ connection
Bus device driver model • Using PCI as example PCI device driver1 PCI device driver2 … 1 3 Detect PCI device1 and match with PCI device driver1 Call probe() of driver1 Register to PCI bus driver: PCI device1 ID Driver1 itself PCI bus driver 2 Plug in PCI device1 PCI device2 …
Device Driver Model Platform device / platform driver platform driver1 platform driver2 … 1 3 Detect device1 is added and match the name with driver1 Call probe() of driver1 Register to kernel: device1 name driver1 itself platform driver / device framework 2 Somewhere a code calls to add device1 … platform device1 hardware resources (chipset, board) platform device2 hardware resources (chipset, board)
More information AT91SAM OS Ecosystem: • AT91SAM Linux Ecosystem http://linux4sam.org • AT91SAM WinCE Ecosystem http://www.at91.com/windows4sam • AT91SAM Android Ecosystem http://www.at91.com/android4sam • AT91SAM community forum: http://www.at91.com