Linux Details: Device Drivers David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO 63130
Goal of a Device Driver Hide details of how a device works behind a standard interface User Code User Space Standard Interface Black Box Device Driver Kernel Space Device Hardware CSE 522S – Advanced Operating Systems
Goal of a Device Driver Hide details of how a device works behind a standard interface Be modular and flexible User Code User Code Standard Interface Standard Interface Black Box Device Driver Modified Driver Device Device (version 2) CSE 522S – Advanced Operating Systems
Driver Design Philosophy Support mechanism, not policy Mechanism: technical capabilities Policy: how those capabilities are used Example – sockets: Sockets abstract many types of connections Synchronous or asynchronous access FTP or UDP Variety of protocols By being flexible, sockets place little constraint on how they are used. Expect hardware devices to be used in many contexts! CSE 522S – Advanced Operating Systems
Traditional Driver Types Character Devices Reads and writes a stream of characters May only implement read, write, open, close Console drivers, keyboard, etc. Block Devices Reads and writes whole blocks of bytes E.g. a hard drive only reads and writes 4K bytes at a time Hard drives, CD-ROM drives, floppy drives Network Devices Provides network interfaces (like eth0 or loopback) Packet based read & write CSE 522S – Advanced Operating Systems
CSE 522S – Advanced Operating Systems Driver Interface A driver typically communicates through (how else?) a special file. struct file – kernel representation of an open file struct file_operations – list of function pointers to be called when file is accessed via file system calls (i.e. read, write, flush, etc.), used in file CSE 522S – Advanced Operating Systems
Accessing Hardware Devices Control over peripheral devices is done by writing and the device’s registers How do we read and write those? I/O Ports – R/W special processor instructions I/O Memory – R/W special memory addresses Memory mapping is preferred (and faster)! Maps large regions (e.g. video frame buffer) Some architectures don’t support I/O Ports CSE 522S – Advanced Operating Systems
Memory Mapped I/O Functions #include <linux/ioport.h> request_mem_region() – allocates a memory region suitable for I/O memory map ioremap() – maps peripheral physical I/O space into kernel memory space ioread32(), iowrite32(), io_memcpy() – family of functions for reading and writing memory mapped I/O CSE 522S – Advanced Operating Systems
Devices and Interrupts Syscalls provide a synchronous interface to hardware devices Interrupts allow asynchronous operations Many devices are much slower than processor Processor 3) Interrupt notifies CPU that work is done 4) Deferred processing 1) Writes requested block to device 2) Seeks and writes block into I/O mapped memory Hard-drive CSE 522S – Advanced Operating Systems
CSE 522S – Advanced Operating Systems More Details There are whole books about how to write good device drivers, plus lots of example code: Linux Device Drivers, 3rd ed. by Corbet, Rubini, and Kroah-Hartmann Available online (free) under creative commons license CSE 522S – Advanced Operating Systems