Docsity

Device Drivers - Lecture Slides | CS 0449, Study notes of Computer Science

Toggle sidebar
bg1
DEVICE DRIVERS
Abstraction via the OS
Software Layers
Userโ€level๎˜ƒI/O๎˜ƒsoftware๎˜ƒ&๎˜ƒlibraries
Deviceโ€independent๎˜ƒOS๎˜ƒsoftware
Device๎˜ƒdrivers
Interrupt๎˜ƒhandlers
Hardware
Operating
system
(kernel)
User
Device Drivers
User
space
Kernel
space
User
program
Keyboard
driver
Disk
driver
Rest๎˜ƒof๎˜ƒthe๎˜ƒOS
Keyboard
controller
Disk
controller
Types of Devices
โ€ขBlock๎˜ƒDevices
โ€“A๎˜ƒdevice๎˜ƒthat๎˜ƒstores๎˜ƒdata๎˜ƒin๎˜ƒfixedโ€sized๎˜ƒblocks,๎˜ƒ
each๎˜ƒuniquely๎˜ƒaddressed,๎˜ƒand๎˜ƒcan๎˜ƒbe๎˜ƒrandomly๎˜ƒ
accessed
โ€“E.g.,๎˜ƒDisks,๎˜ƒFlash๎˜ƒDrives
โ€ขCharacter๎˜ƒDevices
โ€“Device๎˜ƒthat๎˜ƒdelivers๎˜ƒor๎˜ƒaccepts๎˜ƒa๎˜ƒstream๎˜ƒof๎˜ƒ
characters
โ€“E.g.,๎˜ƒKeyboard,๎˜ƒmouse,๎˜ƒterminal
Mechanism vs. Policy
โ€ขMechanism๎˜ƒโ€“What๎˜ƒcapabilities๎˜ƒto๎˜ƒhave๎˜ƒ
(Algorithm)
โ€ขPolicy๎˜ƒโ€“How๎˜ƒto๎˜ƒuse๎˜ƒa๎˜ƒmechanism๎˜ƒ
(Parameters)
โ€ขDrivers๎˜ƒshould๎˜ƒbe๎˜ƒflexible๎˜ƒby๎˜ƒonly๎˜ƒproviding๎˜ƒ
mechanisms๎˜ƒnot๎˜ƒpolicies
pf3

Partial preview of the text

Download Device Drivers - Lecture Slides | CS 0449 and more Study notes Computer Science in PDF only on Docsity!

DEVICE DRIVERS

Abstraction via the OS

Software Layers

Userโ€level I/O software & libraries Deviceโ€independent OS software Device drivers Interrupt handlers Hardware Operating system (kernel) User

Device Drivers

User space Kernel space User program Keyboard driver Disk driver Rest of the OS Keyboard controller Disk controller

Types of Devices

  • Block Devices
    • A device that stores data in fixedโ€sized blocks, each uniquely addressed, and can be randomly accessed
    • E.g., Disks, Flash Drives
  • Character Devices
    • Device that delivers or accepts a stream of characters
    • E.g., Keyboard, mouse, terminal

Mechanism vs. Policy

  • Mechanism โ€“ What capabilities to have (Algorithm)
  • Policy โ€“ How to use a mechanism (Parameters)
  • Drivers should be flexible by only providing mechanisms not policies

Device Drivers in Linux

  • Can be compiled into the kernel
  • Can be loaded dynamically as Modules

/dev

  • Character and block devices can be exposed via a filesystem
  • /dev/ typically contains โ€œfilesโ€ that represent the different devices on a system
  • /dev/console โ€“ the console
  • /dev/fd/ โ€ a processโ€™s open file descriptors

/proc

  • Virtual filesystem with information about the system and running programs
  • /proc/cpuinfo โ€“ text โ€œfileโ€ containing CPU information

Sysfs

  • Exports information about devices and drivers to userspace,
  • Can configure aspects of device
  • /sys/

Hello World Module

#include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello, world\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit);

Build and Run

% make make[1]: Entering directory /usr/src/linuxโ€2.6.10' CC [M] /home/ldd3/src/miscโ€modules/hello.o Building modules, stage 2. MODPOST CC /home/ldd3/src/miscโ€modules/hello.mod.o LD [M] /home/ldd3/src/miscโ€modules/hello.ko make[1]: Leaving directory/usr/src/linuxโ€2.6.10' % su root# insmod ./hello.ko Hello, world root# rmmod hello Goodbye cruel world root#