170 likes | 263 Views
UQC152H3 Advanced OS. Writing a Device Driver. The SCULL Device Driver. Simple Character Utility for Loading Localities 6 devices types Scull-03 Scullpipe0-3 Scullsingle Scullpriv Sculluid Scullwuid. Types of SCULL. Scull0-3 Global & persistent, can be opened many times Scullpipe0-3
E N D
UQC152H3 Advanced OS Writing a Device Driver
The SCULL Device Driver • Simple Character Utility for Loading Localities • 6 devices types • Scull-03 • Scullpipe0-3 • Scullsingle • Scullpriv • Sculluid • Scullwuid
Types of SCULL • Scull0-3 • Global & persistent, can be opened many times • Scullpipe0-3 • Works like a normal pipe – example of blocking • Scullsingle, scullpriv, sculluid, scullwuid • Access versions of scull
Major and Minor numbers • Major number used for device identification • Minor used by device • Use <linux/fs.h> • Register_chdev(unsigned int major, const char *name, struct file_ops *fops); • Registers major number in static array of devices 128 entries!
Mknod –creating a file access point • Once we have a device table entry we need some way for programs and utilities to access it. • Use mknod to create file access point • mknod /dev/scull c 127 0 Device name Device type Minor number Major number
Dynamic Allocation of Major Numbers • It is better to use dynamic allocation of major numbers • Use register_chrdev with intial arg=0 • There is a limited number of driver numbers • Many are already taken • Requires script to run mknod
Deregistering devices • Unloaded drivers should be de-registered using • Unregister_chrdev(unsigned int major, const char * name); • Can cause procs problems if not de-registered
dev_t and k_dev • System passes the major number to called code through the major number • Passed as i_rdev in struct inode • i_rdev is 16 bits • Only allows 256 minor numbers • Can’t be changed!
kdev_t • Linux kernels greater then 1.2 has introduced kdev_t • It allows larger minor numbers • It has a number of macros • MAJOR, get a major dev_t from kdev_t • MINOR, get a minor dev_t from kdev_t • MKDEV return a kdev_t from a dev_t
File Operations • Pointed to from struct file • lseek • read , write & readdir • Select • mmap • ioctl • open & release • fsync & fasync • check_media_change & revalidate
Struct file • Very important kernel data structure • Not the same of FILE of library calls • Has • Pointer to file operations • Flag data • Read/write position • Inode • Mode
Opening a device • Open does the following • Check for errors – device not ready? • Initialises the device if this is the first open call • Identifies the minor numbers – selects correct file op • Allocates private data • Increments the usage count
Close or release method • This should • Decrement the usage count • Deallocate anything allocated by open • Last one out – turn off the lights! void scull_release (struct inode *inode, struct file *filp){ MOD_DEC_USE_COUNT;}