Thursday, May 27, 2010

LDD3 notes: Driver Model

My notes while reading Linux Device Drivers 3rd edition.

2.6 device model addresses aspects common to all types of devices:
  • Power management
  • Userspace communications /sysfs
  • Hotplugging w/ udev
  • Device classes
  • Object lifecycle

Kobject, Kset, Subsystem
  • kobject per object, belongs to a ktype.
  • kset for a collection of kobjects of same type
  • Subsystem as a high-level portion of the kernel as a whole: block, devices, usb, pci
  • Class as high-level view of what a device does
Kobject
  • embed struct kobject in your structure
  • initialize name, ktype, kset and parent
  • implement a destructor (release)
  • implement reference counting by wrapping kobject_{get|put}
  • back-cast kobject references to your structure using using container_of()
Adding a Kobject to a Kset
  • point kobject.kset to set
  • point kobject.parent to the set
  • kobject_register()
Subsystem
  • a kset with a semaphore
  • rooted at the top of the sysfs hierarchy
  • subsystem_{init|register|unregister|get|put}

Bus, Device, Driver, Class, Class device
  • Bus as a channel between processor and devices: physical or virtual
  • Device as an instance to be controlled by a driver, connected to a bus
  • Driver
Bus type
  • bus_[un]register() - confusing name
  • match - compare whether a given device can be handled by the given driver
  • typically implement bus type specific functions to register devices to a specific bus instance
  • on bus enumeration
  • on platform initialization
Bus instance
  • each instance is a device
  • bus_id naming the bus instance
  • parent and/or bus could be null for a singleton top level bus, parent could be the host controller
Device
  • parent pointers reflect a tree of device attachments through buses and host controllers
  • bus_id for unique identification within a bus
  • type of bus, for matching devices and drivers
  • driver and driver private data
Class & Class device
  • Typically handled by high-level code when you register some facilities you implement
  • Device membership in a class is represented by a struct class_device

Attributes

Kobject Attributes, low-level interface
  • struct attribute: owner module and filesystem mode.
  • sysfs_ops: show/store
  • sysfs_create[_bin]_[file|link]
Attributes for high-level objects
  • [bus|driver|device]_create_file
  • BUS_ATTR(name,mode,show,store)
  • DRIVER_ATTR()
  • DEVICE_ATTR()
  • CLASS_DEVICE_ATTR()

Firmware

Driver
  • request_firmware(name)
  • release_firmware()
Userspace reacts to hotplug and special sysfs files
  • loading state
  • binary data
  • device identification
Probably implemented in udev?

No comments:

Post a Comment