I/O (Device) Communication

How does process communicate with device?

1. Special CPU commands
- special commands (e.g. input, copy_in, test) reserved for device access
- e.g. store the contents of register R3 into CDR #4 of device 18: 
		copy-in R3, 0x012, 4

2.  Memory-mapped I/O
- alternative to dedicated I/O CPU commands (to control device)
- devices are associated with logical memory addresses
- dedicate certain addresses to each device
- e.g. store the contents of register R3 into the CDR: store R3, 0xFFF0124 

3. Direct Memory Access
- frees CPU from data transfer work
- device controller has direct access to memory - no CPU intervention needed
- DMA controller requires address register that points to main memory location
- polling or interrupts used be process to determine when transfer is complete
- far more efficient than CPU transfers
- however, possible contention for data bus

Device Drivers
- want one common interface to all devices but in practice, unachieavable
- need two categories: character and block device drivers
	- block devices: anything that addresses stored data
		e.g. tape, disc, CD
		send/receive data in blocks (e.g. sector on disk)
		require data to be accessed by location
	- character devices: all others - no addresses
		e.g. keyboard
		send/receive data as a stream of bytes
 
Device Drivers:
	Common Interface
	- open (int devNum): before device is used, initialize as needed
	- close (int devNum): when proc finished, release devfor others to use

	Block Device Interface
	- read (int devNum, int devAddr, char *bufAddr): get block to mem
	- write (int devNum, int devAddr, char *bufAddr): save block from mem
	- seek (int devNum, int devAddr): move tape/disk R/W head to position

	Character Device Interface
	- read (int devNum, int numBytes, char *bufAddr): get numBytes 
	- write (int devNum, int numBytes, char *bufAddr): save numBytes
	- ioctl (int devNum, int controlOp, int opData): device specific

The ioctl() interface is common to all character device drivers, but the
implementation is specific to each device.  
	- e.g. display may have controlOp for 'beep' or 'flash screen'
	       terminal may have controlOp for controlling echo on/off
Note: The ioctl() interace is not applicable to block devices such as a tape or disc drive. For a more accurate view of Unix device driver interfaces, see table 5.1 on pg. 113 of the course text.