
Inside Your PC I/O Devices and Device Drivers (Topic 11) • Your PC is equipped with – CPU and memory – many I/O devices • VGA card, network card, disk controller, … Network card CPU 홍성 수 서울대학교 공과대학 전기 공학부 Real-Time Operating Systems Laboratory Main board Memory Hard disk 1 Device Connection Inside I/O Device • Devices are attached to I/O bus • I/O device consists of – ISA, PCI, EISA, SCSI, … – Controller, Logic – Register set – Bus interface External interface CPU VGA Ethernet Sound Register Controller Register Logic BUS interface I/O BUS Address Data Control I/O BUS 2 3 Device Control Device Driver • How CPU control device • Software layer between application and device – CPU write register address on the bus • Implements operations exposed to users • Address spaces are assigned to each device – (Ex) open, close, read, write, ioctl, … – Device write / read data on the bus External interface Application External interface OS Register Controller Register Controller Device driver Register Logic Register Logic BUS interface CPU BUS interface Address Data Control Address Data Control Address Data Control Address Data Control I/O BUS I/O BUS 4 5 What is Device Driver? Types of Linux Devices • Software layer between application and device • Character Devices • Implements operations exposed to users – Transfer unit: character (byte) – (Ex) open, close, read, write, ioctl, … – Can be accessed like a file (open, close, read, write, …) – (Ex) console, keyboard, mouse, … (/dev/tty1, /dev/lp1, …) • Block Devices – Transfer unit: block (usually some kilobytes) – Can be accessed like a file (open, close, read, write, …) – External view to users is same as character devices – Internally, block buffer is used for efficiency (contrast to character devices) – (Ex) hard disk drive, CD-ROM drive, … (/dev/hda1, …) • Network Interfaces – Can not be easily mapped to neither character or block devices – (Ex) eth0 6 7 Structure of Device Files Character Device Drivers • Represent an I/O device – /dev/tty0 = first serial port Character Device Table • Two device files can represent same I/O devices 0 (but may be implemented in a different way) Name Index = 1 Character Device Driver Character Device – /dev/psaux, /dev/psmouse = serial mouse File operations major number •Attributes 2 … Device Name – Type: block or character Open – Major number: specifies device driver Read Open code Registered when Write – Minor number: argument to device driver, kernel don’t care device being Read code installed Close Name Type Major Minor Desc write code Ioctl /dev/fd0 Block 2 0 Floppy Disk … /dev/had Block 3 0 First IDE disk … /dev/hda2 Block 3 2 Second partition of first IDE disk /dev/hdb Block 3 64 Second IDE disk /dev/console Char 5 1 Console /dev/null char 1 3 Null device 8 9 Character Device Drivers Block Device Drivers • To applications: • Difference with Character Device Drivers – Device file = Regular file – Actual transfer unit: block (some kilobytes) • To file system: – <Device Driver : Device> transfer unit cannot be character. – Regular file: read from or write to disk drive – Device file: invoke device driver operations • How to implement character oriented read/write? • Open: initialize device – Kernel provides general block conscious read/write functions • Read: device data Æ user buffer • translate character I/O to block I/O • Write: user buffer Æ device – Device driver only need to implement handle block request • Close: called when removed from device table • sometimes called as ‘strategy routine’ • Ioctl: device specific control – (Ex) baud rate change, access permission set, 10 11 Interrupt Handling in Device Structure of Block Device Drivers Drivers Block Device Table Block Device Driver • Register ‘interrupt handling routine’ when opening Open Open code Read • Kernel invokes registered handler when interrupt Write occurs Close … Block Device • Linux interrupt handler Ioctl … – Interrupt Handler Handling request code • High priority function (such as acknowledging to PIC) (strategy routine) • Run with interrupt disabled • Invoked every time interrupt occurred • Marks a bottom half as active Request queue block – Bottom Half Buffer Cache Manager • Low priority function (such as transferring data from device) • Run with interrupt enabled Block Read Buffer Buffer • Execution may be deferred Block Write char BlockBuffer Buffer (Buffer Cache) 12 13 Bottom Half General Device Driver Routines (1) Example of bottom half activated twice, but Proc executed once Proc •Open – Reads minor number – Initialize appropriate device INTR 1 Ack INTR 1, activate bottom half 1 – Initialize device driver internal data structures Int handler 1 – If needed, change the file operation table (according to minor) Intr disabled – Increase usage counter – If needed, register interrupt handler, enable irq •Close – Free dynamically allocated data structures INTR 2 Ack INTR 2, activate bottom half 1 – Decrease usage counter Int handler 2 Bottom half 1 – Un-register the interrupt handler Intr disabled Intr enabled 14 15 General Device Driver Routines (2) General Device Driver Routines (3) • Read / Write Character Device, Polling and Blocking I/O Character Device, Polling and Non-Blocking I/O – Polling vs Interrupt Driven Read Read • Polling – checks device if data can be read or written Check Device Check Device – Usually for character device • Interrupt Driven No No – sleep process until data can be read or written Device ready? Device ready? – Usually for character and block device Yes Yes – Blocking vs Non-Blocking • Blocking Read Data Read Data (device Æ user) (device Æ user) – If data cannot be read or written: wait until ready • Non-Blocking: – If data cannot be read or written: immediately returns Return to User Return to User 16 17 General Device Driver Routines (4) General Device Driver Routines (5) Character Device, Interrupt Driven and Blocking I/O Block Device, Interrupt Driven and Blocking I/O Read Read Read complete interrupt Read ready interrupt Check Device Check read buffer Wakeup process No Wakeup process No Device Ready? Buffer full ? Yes Yes Issue Read Command Read Data Sleep process Sleep process (device Æ user) Wakeup by another Sleep process process Return to User Return to User 18 19.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-