Title: Linux Device Drivers
1Linux Device Drivers
- CSlab _at_ NTUA
- ???ast???? ?p?????st???? S?st?µ?t??
- ?????? ?ets?ß?? ????te??e??
2Device Drivers
- Black boxes
- Hide details
- Standardized calls
- Map standard calls to specific hardware
- Build separately from kernel plugged in at
runtime
3Why Do We Need Drivers?
- New devices constantly available
- Hardware vendors
- Learn things most people dont know ?
- Learn how machine works
- Learn about OS kernel
4Mechanism vs. Policy
- device driver provides mechanism, not policy
- X server vs. window manager
- TCP/IP (sockets interface is mechanism, servers
provide policy) - Floppy driver (mechanism to see data blocks,
filesystem provides policy)
5User Programs Needed
- Kernel programming not enough
- User apps needed to communicate with kernel
- e.g. sound volume control (sndctl)
6Kernel
- Big chunk of executable code
- Process management
- Memory management
- Filesystems
- Device control
- Networking
7Modules
- Ability to extend features offered by kernel at
runtime - Module is piece of code
- insmod/rmmod
- Device driver can be a module
8Classes of Devices
- Char devices
- Block devices
- Network devices
9Classes of Modules
- Device drivers
- filesystems
10Building and Running Modules
include ltlinux/module.hgt int init_module()
printk("Hello, world\n") Â return 0 void
cleanup_module() printk(Bye Bye\n")
11Building and Running Modules (2)
- Kernel functions only, no C library
- Dont use printf, only printk
- insmod ./hello.o
- dmesg to see kernel messages
- rmmod hello.o
- Dmesg to see kernel messages
- Insmod/rmmod called by super-user (root)
12User Space and Kernel Space
- Modules run in kernel space
- Applications in user space
- Different memory mapping-address space
- User?kernel space by system call or hardware
interrupt
Kernel code is executed in process context,
acceses data in process address space
Runs in no process context
13Character Devices
mknod /dev/lunix c 69 0
Example of ls l /dev on my system
- crw------- 1 gtsouk users 14, 3 Apr 17
1999 /dev/dsp - crw-rw-rw- 1 root root 69, 0 May 5
1998 /dev/lunix - crw-rw-rw- 1 root root 1, 3 May 5
1998 /dev/null - crw-r--r-- 1 root root 1, 8 May 5
1998 /dev/random - crw------- 1 sotirop users 4, 0 May 5
1998 /dev/tty0 - crw-r----- 1 root uucp 4, 64 Mar 19
1456 /dev/ttyS0 - crw-rw-rw- 1 root root 1, 5 May 5
1998 /dev/zero
14Character Devices
- int (open) (struct inode , struct file )
- void (release) (struct inode , struct file )
- int (read) (struct inode , struct file , char
, unsigned long) - int (write) (struct inode , struct file ,
const char , unsigned long) - int (lseek) (struct inode , struct file , long
long, int) - int (ioctl) (struct inode , struct file ,
unsigned int, unseigned long)
15Character Devices
- static struct file_operations lunix_fops
- open (void )lunix_open,
- release (void )lunix_release,
- read (void )lunix_read,
- write (void )lunix_write,
- llseek (void )lunix_llseek,
- ioctl (void )lunix_ioctl,
16Character Devices
- register_chrdev()
- unregister_chrdev()
- Character devices
- 1 mem
- 2 pty
- 69 lunix_driver
- 180 usb
- Block devices
- 2 fd
- 33 ide2
17Exercise and Notes
- http//www.cslab.ntua.gr/courses
- http//ray.cslab.ntua.gr/dd
- Labs exercise and notes in .pdf.
- Give it a try!
18Linux Device Drivers, 2nd Edition
19Bibliography
- Writing Character Device Driver for Linux
- ftp//ftp.llp.fu-berlin.de/pub/linux/LINUX-LAB/whi
tepapers/drivers.ps.gz - Â
- The Linux Kernel Module Programming Guide
http//www.ntua.gr/LDP/guides.html - Â
- Linux Device Drivers, 2nd Edition, Alessandro
Rubini and Jonathan Corbet. http//www.xml.com/ldd
/chapter/book/