Linuxdd
Linuxdd
CSC345
Project 3 Preview
Write a device driver for a pseudo stack device
Idea from http://www.cs.swarthmore.edu/~newhall/cs45/f01/proj5.html
Linux character device type supports the following operations
Open: only one is allowed. Write: writes an char string to top of the device stack. Error if stack is empty Read: reads an item from top of the device stack. Error if stack is empty Release: release the device
Install with LKM. Test: It will be a dedicated standalone machine in the lab. Root password may be given out. If you mess up, you will re-install the
In Unix,
Kernel module device driver interface = file interface What are normal operations? Block vs. character
Note: This picture is excerpted from Write a Linux Hardware Device Driver, Andrew OShauqhnessy, Unix world
Types of LKM
Device drivers Filesystem driver (one for ext2, MSDOS FAT16, 32, NFS) System calls Network Drivers TTY line disciplines. special terminal devices. Executable interpreters.
Loading a module - normally retricted to root - is managed by issuing the follwing command: # insmod module.o
ksyms
lsmod modinfo
modprobe
Or % more /proc/modules
audio cmpci soundcore nfsd 37840 0 24544 0 4208 4 [audio cmpci] 70464 8 (autoclean)
A set of API subroutines (typically system calls) interface to hardware Hide implementation and hardwarespecific details from a user program Typically use a file interface metaphor Device is a special file
Linux Device Drivers (continued) Manage data flow between a user program and devices A self-contained component (add/remove from kernel) A user can access the device via file name in /dev , e.g. /dev/lp0
Understand the device characteristic and supported commands. Map device specific operations to unix file operation Select the device name (user interface)
Namespace (2-3 characters, /dev/lp0)
4.
(optional) select a major number and minor (a device special file creation) for VFS interface
Mapping the number to right device sub-routines
5. 6. 7. 8.
Implement file interface subroutines Compile the device driver Install the device driver module with loadable kernel module (LKM) or Rebuild (compile) the kernel
Read/write (I/O)
Pooling (or synchronous)
Interrupt based
Note: This picture is excerpted from Write a Linux Hardware Device Driver, Andrew OShauqhnessy, Unix world
struct file_operations Fops = { read: xxx_read, write: xxx_write, open: xxx_open, release: xxx_release, /* a.k.a. close */ };
};
mknod /dev/stk c 38 0
ls l /dev/tty
crw-rw-rw1 root root 5, 0 Apr 21 18:33 /dev/tty
If you let the system pick Major number, you can find the major number (for special creation) by
% more /proc/devices
Implementation
Assuming that your device name is Xxx Xxx_init() initialize the device when OS is booted Xxx_open() open a device Xxx_read() read from kernel memory Xxx_write() write Xxx_release() clean-up (close) init_module() cleanup_module()
kernel functions
add_timer()
Causes a function to be executed when a given amount of time has passed Prevents interrupts from being acknowledged Called when a request has been satisfied or aborted Frees an IRQ previously acquired with request_irq() or irqaction() Allows a driver to access data in user space, a memory area distinct from the kernel Reads a byte from a port. Here, inb() goes as fast as it can, while inb_p() pauses before returning. Registers an interrupt like a signal. Tests if inode is on a file system mounted with the corresponding flag. Frees memory previously allocated with kmalloc()
cli()
MAJOR() MINOR()
kernel functions
memcpy_*fs()
Copies chunks of memory between user space and kernel space Writes a byte to a port. Here, outb() goes as fast as it can, while outb_p() pauses before returning. A version of printf() for the kernel. Allows a driver to write data in user space.
sys_get*() wake_up*()
Pitfalls
1. Using standard libraries: can only use kernel functions, which are the functions you can see in /proc/ksyms. 2. Disabling interrupts You might need to do this for a short time and that is OK, but if you don't enable them afterwards, your system will be stuck 3. Changes from version to version
Resources
Linux Kernel API: http://kernelnewbies.org/documents/kdoc/kernelapi/linuxkernelapi.html Kernel development tool http://www.jungo.com/products.html Linux Device Drivers 2nd Edition by Rubini & Corbet, O'Reilly Pub, ISBN 0596-00008-1