Lnux Device Driver Alessandro Rubini OReilly - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Lnux Device Driver Alessandro Rubini OReilly

Description:

A software component that controls a hardware device. interacts with user ... see grep 'register_' /proc/ksyms. various devices. binary format of an executable ... – PowerPoint PPT presentation

Number of Views:126
Avg rating:3.0/5.0
Slides: 21
Provided by: david2175
Category:

less

Transcript and Presenter's Notes

Title: Lnux Device Driver Alessandro Rubini OReilly


1
Lnux Device DriverAlessandro Rubini OReilly
  • CS 230
  • ???

2
Device Driver
  • A software component that controls a hardware
    device
  • interacts with user programs
  • better provide basic hardware controls only
  • leave high level decision to user programs
  • e.g.) floppy driver provides only a view of bytes
    sequence
  • A layer between hardware and user programs
  • defines how the device appears to user
    applications
  • there can be several drivers for a single
    hardware
  • a single driver can handle several similar
    devices
  • Devices?
  • memory, disk, memory, CPU, .

3
Kernel View
user program applications
system call
kernel parts
process mgmnt
memory mgmnt
file system
device control
network
features implemented
concurrency multitasking
virtual memory
files directories
TTYs device access
connectivity
software support
archi- dependent code
memory manager
FS type
char device
nw subsystem
hardware control
block device
IF driver
hardware
CPU
RAM
disks CD
consol, serial port, boards
nw interface
4
Linux IO System
5
Linux Device Driver
  • A device driver is a collection of subroutines
    and data within the kernel that constitutes the
    software interface to an I/O device.
  • When the kernel recognizes that a particular
    action is required from the device, it calls the
    appropriate driver routine, which passes control
    from the user process to the driver routine.
  • Control is returned to the user process when the
    driver routine has completed.
  • A device driver may be shared simultaneously by
    user applications and must be protected to ensure
    its own integrity.

6
Features of Device Driver
  • A set of routines that communicate with a
    hardware device and provide a uniform interface
    to the operating system kernel.
  • A self-contained component that can be added to,
    or removed from, the operating system
    dynamically.
  • Management of data flow and control between user
    programs and a peripheral device.
  • A user-defined section of the kernel that allows
    a program or a peripheral device to appear as a
    /dev '' device to the rest of the system's
    software.

7
Devices Classes
  • Each class of devices is handled by a module
  • module is replaceable
  • isolate devices from kernel
  • Classes
  • character devices
  • can be regarded as a file byte stream
  • operations supported open, close, read, write,
  • accessed as file system nodes study file system
  • block devices
  • disk
  • transfers a block of data, not byte stream
  • a device can either be char or block device
  • network interfaces
  • packet transmit neither char nor block
  • cannot be treated like a file
  • others SCSI

8
Linux Development Environment
  • security
  • device module runs in kernel space need root
    privilege
  • avoid using untrusted modules
  • tools
  • compiler gcc
  • module utilities
  • GNU tools gmake, debugger,
  • Linux version
  • even number (1.2.x, 2.0.x) versions are stable
  • odd number versions are under development

9
Chap 2. Building and Running Modules
10
How to Load a module
  • at Install time
  • select desired modules
  • /etc/modules
  • /etc/conf.modules optins for the modules
  • there are too many devices
  • the kernel may become huge to support all the
    devices
  • after install time
  • insmod install module
  • lsmod list modules installed
  • Module Size Used by
  • gus 45016 0
  • mad16 6564 0
  • sb 31416 0
  • ad1848 15112 0 mad16
  • uart401 5588 0 mad16 sb
  • sound 54368 0 gus mad16 sb ad1848 uart401

11
insmod
  • define MODULE
  • include ltlinux/module.hgt
  • int init_module(void) printk(lt1gtHello,
    world\n) return 0
  • void cleanup_module printk(lt1gtGoodbye\n)

root root gcc c hello.c root insmod
hello.o Hello, world root rmmod
hello.o Goodbye root
12
A module in the kernel
init_module( )
insmod
register_capability( )
capabilities
printk( )
cleanup_module( )
unregister_capability( )
rmmod
13
User Program in Kernel
  • Functions calls to those only exported by the
    kernel
  • printk( ) instead of printf( )
  • No library in linked
  • never include usual header files
  • include kernel header files
  • /usr/include/linux
  • /usr/include/asm
  • Namespace pollution
  • kernel has a huge set of names
  • symbols in your module should be different from
    them
  • dont pollute the kernel name space
  • use your own prefix
  • declare private names as static
  • symbol table

14
The Kernel Symbol Table
  • Not all the symbols are visible to you
  • only those needed for modularized drivers are
  • /proc/ksyms
  • symbols? variables and function names
  • Global symbols in your module can be seen in that
    directory
  • A new module can use the symbols defined in an
    old module
  • stacking
  • enables layered design
  • when you use static and global in the code
  • it can be messy
  • use register_symtab

15
Registering Symbol Table
static struct symbol_table skull_syms
include ltlinux/symtab_begin.hgt X(skull_functi
on1), . X(skull_variable9), include
ltlinux/symtab_end.hgt register_symtab(skull_sy
ms)
  • global/static has no effect in symbol table
  • you can override the symbol table whenever you
    want
  • if you want to hide all the symbols use
  • register_symbtab(NULL)
  • for the last module on the stack

16
Registration
  • init_module registers facilities offered by
    module
  • facilities are used by applications
  • kernel should know the pointers to the facilities
  • other registrations
  • see grep register_ /proc/ksyms
  • various devices
  • binary format of an executable
  • execution domain
  • mapping from one Unix to another
  • symbol table

17
Error in init_module
  • If there is error while init_module registers
    facilities,
  • the facilities hang inside the kernel
  • you have to reboot the kernel
  • check return code from every registration
  • if (register_aaa(ptra, aname) goto fail_aaa
  • if (register_bbb(ptrb, bname) goto fail_bbb
  • if (register_ccc(ptrc, cname) goto fail_ccc
  • fail_ccc unregister_bbb(ptrb, bname)
  • fail_bbb unregister_aaa(ptra, aname)
  • fail_aaa return (-1)
  • cleanup_module should unregister everything
    registered by init_module

18
The Usage Count
  • Indicates how many processes are actively use the
    device
  • A module (device driver) cannot be removed when
    the device is busy
  • During development,
  • redefine MOD_INC_USE_COUNT, MOD_DEC_USE_COUNT to
    no-ops
  • or use ioctl to set the counter zero before
    unloading a module
  • look at /proc/modules
  • loaded module names
  • number of pages in use
  • usage count

19
Ports
  • most device drivers use ports
  • /proc/ioports shows ports reserved by a driver
  • routines for procuring ports
  • check_region(port, range)
  • probe
  • request_region(port, range)
  • use the ports .
  • release-region(port, range)
  • what if a driver uses a wrong port
  • probing will get funny results
  • probing write read

20
User Space Driver
  • Advantages
  • all user level utilities can be used
  • libraries, debugger
  • an error affects only the driver, not the whole
    kernel
  • memory is swappable
  • Disadvantages
  • interrupts are not available
  • direct memory is not possible
  • slow (user/kernel switching)
  • usually not reentrant
Write a Comment
User Comments (0)
About PowerShow.com