Linux Device Driver: Nctu Os Lab Chi-Wei Yang
Linux Device Driver: Nctu Os Lab Chi-Wei Yang
Linux Device Driver: Nctu Os Lab Chi-Wei Yang
NCTU OS Lab
Chi-Wei Yang
Outline
Overall Architecture
Character Driver
Block Driver
Network Driver
Support Functions
Overall Architecture
VFS
Driver Types
• character
• block
• network
Overall Architecture (cont.)
VFS
Socket
File Systems
Hardware
Overall Architecture (cont.)
Operation Modes
• polling
• interrupt
• DMA
Character Driver
Device Properties
• can’t be randomly accessed
• can’t be buffered
• usually are slow devices
Export Interface
• file_operations
Data Flow in read/write
Character Driver (cont.)
int lseek (struct inode *, struct file *, off_t, int)
int read (struct inode *, struct file *, char *, int)
int write (struct inode *, struct file *, const char *, int)
int readdir (struct inode *, struct file *, void *, filldir_t)
int select (struct inode *, struct file *, int, select_table *)
int ioctl (struct inode *, struct file *, unsigned int, unsigned long)
int mmap (struct inode *, struct file *, struct vm_area_struct *)
int open (struct inode *, struct file *)
void release (struct inode *, struct file *)
int fsync (struct inode *, struct file *)
int fasync (struct inode *, struct file *, int)
int check_media_change (kdev_t dev)
int revalidate (kdev_t dev)
Block Driver
Device Properties
• can be randomly accessed
• accessed in units of blocks
• to speed up access, buffer cache is used
Export Interface
• file_operations
• request_fn()
Data Flow in read/write
Block Driver (cont.)
process
buffer
cache
done
Network Driver
Export Interface
Data Flow in read/write
Network Driver (cont.)
User Application
INET Socket
TCP UDP
IP ARP ICMP
Network Device
Support Functions
I/O ports reservations
• request_region()
Memory Allocations
• kmalloc(), vmalloc(), get_free_page()
Interrupt Handler Registration
• request_irq()
Data Transfer between User/Kernel
• memcpy_fromfs()
Support Functions (cont.)
Bottom Halves
• mark_bh()
• its main purpose is to provide fewer blocking
time due to interrupts
• top half/bottom half
• priority-based
• limitions in writing bottom halves
– can't call schedule(), can't block