Linux Device Model Part 2

Slides:



Advertisements
Similar presentations
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Advertisements

CSI 400/500 Operating Systems Spring 2009 Lecture #14 – Device Management and Drivers Monday, March 23 rd, 2009.
purpose Search : automation methods for device driver development in IP-based embedded systems in order to achieve high reliability, productivity, reusability.
Primary Author: Girish Verma Secondary Author(s): Navya Prabhakar Presenter: Navya Prabhakar Company/Organization: CircuitSutra USB Modeling Quick Start.
Devices and Drivers (Chapter 7) Khattab Alhabashi UNIX System Administration.
USERSPACE I/O Reporter: R 張凱富.
Hot Plug PCI. 2 PP ppt PCI Hot Plug Objective  To Enable higher availability of file and application servers by standardizing the process of removing.
BIOS (Basic Input Output Service) Contains system data used by the ROM BIOS service routines. Serves as a standardized communication interface between.
Lecture 12 Page 1 CS 111 Online Devices and Device Drivers CS 111 On-Line MS Program Operating Systems Peter Reiher.
Basic Input Output System
Figure 1.1 Interaction between applications and the operating system.
Cs238 Lecture 3 Operating System Structures Dr. Alan R. Davis.
USB Driver and Linux Device Model
1 I/O Management in Representative Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Final presentation- Part A Avi Urman, Kobi Maltinsky Supervisor: Inna Rivkin Linux on SOPC – Embedded System Implementation.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
An Introduction to Device Drivers Sarah Diesburg COP 5641 / CIS 4930.
Data Structures in the Kernel Sarah Diesburg COP 5641.
Hacking the Bluetooth Pairing Authentication Process Graduate Operating System Mini Project Siyuan Jiang and Haipeng Cai.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
Basic Input Output System
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Input Systems (ESP – Fall 2014) Computer Science & Engineering Department Arizona State.
MICROPROCESSOR INPUT/OUTPUT
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS6: Device Management 6.1. Principles of I/O.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
MCTS Guide to Microsoft Windows Vista Chapter 4 Managing Disks.
Firmware Storage : Technical Overview Copyright © Intel Corporation Intel Corporation Software and Services Group.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Lecture 2: Linux devices Roei Ben-Harush Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder.
UNIX Files File organization and a few primitives.
11 INSTALLING AND MANAGING HARDWARE Chapter 6. Chapter 6: Installing and Managing Hardware2 INSTALLING AND MANAGING HARDWARE  Install hardware in a Microsoft.
Linux Device Model Part 1
An Introduction to Device Drivers Ted Baker  Andy Wang COP 5641 / CIS 4930.
Interfacing Device Drivers with the Kernel
User-Space-to-Kernel Interface
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
4P13 Week 12 Talking Points Device Drivers 1.Auto-configuration and initialization routines 2.Routines for servicing I/O requests (the top half)
COMP 3438 – Part I - Lecture 5 Character Device Drivers
© 2009 IBM Corporation Tianhong Wang 2012/06/28 Android’s udev —— Vold.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
Linux kernel TLV meetup, Kfir Gollan. What is hardware probing? Different approaches for detecting hardware Probing in the linux kernel.
What is a Process ? A program in execution.
Commands 3/1/ Boot PROM Fundamentals All Sun systems have resident boot PROM firmware Provides basic hardware testing and initialization prior.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 5.
1 Setup and Compile Linux Kernel Speaker: Yi-Ji Jheng Date:
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 10 Understanding.
The World Leader in High Performance Signal Processing Solutions Linux Industrial I/O Subsystem Open Platform Solutions Michael Hennerich.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
Tgt: Framework Target Drivers FUJITA Tomonori NTT Cyber Solutions Laboratories Mike Christie Red Hat, Inc Ottawa Linux.
Introduction to Operating Systems Concepts
Linux Device Model A device model after 2.5
Kristen Carlson Accardi
Operating Systems (CS 340 D)
Chapter 2: System Structures
Linux Kernel Driver.
The PCI bus (Peripheral Component Interconnect ) is the most commonly used peripheral bus on desktops and bigger computers. higher-level bus architectures.
Introduction to the Kernel and Device Drivers
An Introduction to Device Drivers
Today’s agenda Hardware architecture and runtime system
Kernel Structure and Infrastructure
Chapter 2: The Linux System Part 5
Computer System Laboratory
Introduction to Linux Device Drivers
Presentation transcript:

Linux Device Model Part 2 Sarah Diesburg COP5641

Review Kobject – Device model fundamental structure embedded in devices Reference counting Represents relationships between devices Includes ktype and kset Ktype – Describes default behavior for a family of kobjects Enables kernel objects to share common operations Kset – Collection of related kobjects into same place E.g., “all block devices”

Kernel Events Layer Implements kernel-to-user notification system on top of kobjects Otherwise known as hotplug event E.g., Hard drive full! Hard drive destruction pending! Models events as signals/uevents emitting from kobjects Generated whenever kobject is created or destroyed E.g., digital camera plugged in via USB, user switches console modes, disk is repartitioned

Kernel Events Layer uevents turn into invocation of user program /sbin/hotplug Loads drivers Creates device nodes Mounts partitions Any other appropriate action

Kernel Events Layer Each uevent given Verb or action string representing signal Optional payload as sysfs attribute Uevents pushed to userspace via netlink High-speed multicast socket All userspace has to do is block on the socket to receive events

Kernel Events Layer Generation of uevent takes place when kobject is passed to kobject_add() or kobject_del() Before the event is handed to user space, we can Add extra information to the event Disable event generation entirely

Uevent Generation kobject_uevent() called from kernel to generate a uevent Called from device_add() and device_del() int kobject_uevent(struct kobject *kobj, enum kobject_action action); enum kobject_action { KOBJ_ADD, KOBJ_REMOVE, KOBJ_CHANGE, KOBJ_MOVE, KOBJ_ONLINE, KOBJ_OFFLINE, KOBJ_MAX };

Uevent Generation Each kset has a kset_uevent_ops member Used to be called kset_hotplug_ops If a given object is not contained within a kset, the kernel searches up through hierarchy until it finds a kobject that does have a kset struct kset_uevent_ops { int (*filter)(struct kset *kset, struct kobject *kobj); char *(*name)(struct kset *kset, struct kobject *kobj); int (*uevent)(struct kset *kset, struct kobject *kobj, char **envp, struct kobj_uevent_env *env); }

kset_uevent_ops Filter method determines whether the functions kobject_add() or kobject_del() generate a uevent If filter method returns 0, the event is not created static int block_hotplug_filter(struct kset *kset, struct kobject *kobj) { struct kobj_type *ktype = get_ktype(kobj); return ((ktype = = &ktype_block) || (ktype = = &ktype_part)); } Here, a quick test on the type of kobject is sufficient to decide whether the event should be generated or not.

kset_uevent_ops Name method provides the name of the subsystem generating the event, to be passed as the one-and-only argument of /sbin/hotplug uevent method results in invocation (as user-space process) of /sbin/hotplug env parameter of uevent() provides environment variable values to be passed

Uevent Generation Walk through kobject_uevent() http://lxr.linux.no/linux+*/+code=kobject_uevent Good comments

Device Model Used to track all drivers and models Enables the driver core to match driver with new device The components that play a major role in building and maintaining the model are: Bus Device Device Drivers Class

Bus A channel between processor and one or more devices Can be virtual or physical All devices are connected to a bus A bus can be plugged into another bus E.g., USB controller is usually a PCI device Device model represents actual connections between buses and devices they control

Bus Although material in LDD book is out of date, code examples have been updated and do compile Virtual bus called lddbus Connects to scullp driver In Linux device model, bus is represented by the bus_type structure

struct bus_type struct bus_type { const char *name; struct bus_attribute *bus_attrs; struct device_attribute *dev_attrs; struct driver_attribute *drv_attrs; int (*match)(struct device *dev, struct device_driver *drv); int (*uevent)(struct device *dev, struct kobj_uevent_env *env); int (*probe)(struct device *dev); int (*remove)(struct device *dev); void (*shutdown)(struct device *dev); int (*suspend)(struct device *dev, pm_message_t state); int (*resume)(struct device *dev); const struct dev_pm_ops *pm; struct iommu_ops *iommu_ops; struct subsys_private *p; };

struct bus_type Name field is name of the bus Match method: E.g., “pci” Match method: int (*match)(struct device *dev, struct device_driver *drv); Called whenever a new device or driver is added for this bus Returns nonzero value if device dev can be handled by given device driver drv (Will get to details of dev and drv shortly)

struct bus_type Uevent method: Probe method: int (*uevent)(struct device *dev, struct kobj_uevent_env *env); Called when a device is added or removed Similar to uevent method found in kset_uevent_ops Probe method: int (*probe)(struct device *dev); Called when new device or driver added to bus Callback the specific driver’s probe to initialize matched device

struct bus_type Remove method: Shutdown method: Suspend method: int (*remove)(struct device *dev); Called when a device is removed from the bus Shutdown method: void (*shutdown)(struct device *dev); Called to inactivate the device Suspend method: int (*suspend)(struct device *dev, pm_message_t state); Send a device on this bus to sleep mode Resume method: int (*resume)(struct device *dev); Bring a device on this bus out of sleep mode

Bus Functions Registration/unregistration int bus_register(struct bus_type *bus); void bus_unregister(struct bus_type *bus); Iteration over all devices or drivers that have been registered on the bus int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *, void *)) int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int (*fn)(struct device_driver *, void *)) Iterators call *fn for each device or driver.

Device Each device is represented by struct device Examples Intel AC97 audio controller Intel PRO/100 ethernet controller PS/2 mouse Usually embedded in a larger structure much like kobject

struct device Some members omitted… struct device { struct device * parent; struct device_private * p; struct kobject kobj; const struct device_type * type; struct mutex mutex; struct bus_type * bus; struct device_driver * driver; dev_t devt; struct class * class; const struct attribute_group ** groups; void (* release) (struct device *dev); }

struct device Parent Bus Driver Device’s “parent” device to which it is attached In most cases, bus or host controller If parent is NULL, device is top-level device Bus Type of bus the device is on Driver Driver of device

struct device Devt Class Release method Device type void (* release) (struct device *dev); Called by embedded kobject’s release

struct device Registration/unregistration Often performed by the generic bus driver int device_register(struct device *dev) { device_initialize(dev); return device_add(dev); } void device_unregister(struct device *dev) pr_debug("device: '%s': %s\n", dev_name(dev), __func__); device_del(dev); put_device(dev);

Device Driver Used to represent kernel drivers that can handle specific devices When a device is registered that can be handled by a particular driver, the probe function instantiates the driver for that particular device

struct device_driver struct device_driver { const char * name; struct bus_type * bus; struct module * owner; const char * mod_name; bool suppress_bind_attrs; const struct of_device_id * of_match_table; int (* probe) (struct device *dev); int (* remove) (struct device *dev); void (* shutdown) (struct device *dev); int (* suspend) (struct device *dev, pm_message_t state); int (* resume) (struct device *dev); const struct attribute_group ** groups; const struct dev_pm_ops * pm; struct driver_private * p; };

struct device_driver name bus probe method sysfs name Type of bus the device is on probe method int (* probe) (struct device *dev); Callback function which is called for each device detected that is supported by the driver

struct device_driver Registration/unregistration int driver_register(struct device_driver *drv); void driver_unregister(struct device_driver *drv); Usually embedded in a driver-specific structure

Class Higher-level view of a device that abstracts out low-level implementation details Allows user space to work with devices based on what they do, rather than how they are interconnected Almost all classes show up under /sys/class /sys/class/net – network interfaces /sys/class/input – input devices /sys/class/tty – serial devices

struct class struct class { const char * name; struct module * owner; struct class_attribute * class_attrs; struct device_attribute * dev_attrs; struct bin_attribute * dev_bin_attrs; struct kobject * dev_kobj; int (* dev_uevent) (struct device *dev, struct kobj_uevent_env *env); char *(* devnode) (struct device *dev, mode_t *mode); void (* class_release) (struct class *class); void (* dev_release) (struct device *dev); int (* suspend) (struct device *dev, pm_message_t state); int (* resume) (struct device *dev); const struct kobj_ns_type_operations * ns_type; const void *(* namespace) (struct device *dev); const struct dev_pm_ops * pm; struct subsys_private * p; };

Full Circle PCI subsystem defines one struct bus_type bus_register() /sys/bus/pci/devices and /sys/bus/pci/drivers PCI drivers define struct pci_driver and register with PCI /* initialize common driver fields */ drv->driver.name = drv->name; drv->driver.bus = &pci_bus_type; drv->driver.probe = pci_device_probe; drv->driver.remove = pci_device_remove; drv->driver.kobj.ktype = &pci_driver_kobj_type; /* register with core */ error = driver_register(&drv->driver);

PCI Device struct pci_dev is created device_register(&dev->dev); struct device parent is set to pci bus device bus variable set to struct pci_bus device_register(&dev->dev); Added to respective bus list All drivers on the bus are walked and match() called PCI match() function casts back into struct pci_dev and struct device_driver casted to struct pci_driver Successful match results in dev->driver / pci driver pairing

PCI Device A successful driver to device pairing results in driver probe function call Driver core then adds the device to list of all devices

When a PCI Device is Removed Eventually, system calls the remove function of the driver In the mean time, the bus returns 0xff (all bits set) values Driver must allow for this E.g., see comment /* card removed */ in function handshake in drivers/usb/host/ehci-hcd.c For USB devices, any pending urbs on the device start failing with -ENODEV

So, what again? Summary When a new gadget is plugged into the system The bus controller driver detects the device and calls device_register() The list of drivers associated with the bus is iterated over to find out if there are any drivers that can handle the device. The match function provided in the bus_type structure is used to check if a given driver can handle a given device. When a driver module is inserted into the kernel The driver calls driver_register() The list of devices associated with the bus is iterated over to find out if there are any devices that the driver can handle. The match function is used for this purpose. http://www.bravegnu.org/device-model/device-model.html

Hotplug Two views Kernel Userspace Hardware, kernel, and driver interact Already saw this with uevents Userspace Kernel and userspace interact, via /sbin/hotplug called by kernel when it wants to notify user about hotplug event in kernel

/sbin/hotplug Utility Usually a small bash script Called whenever a kobject is created or destroyed Allows, for example, loading/unloading of kernel modules Passes event on to a list of programs Single command-line argument is the name of the event Other information passed via environment variables

/sbin/hotplug Utility For more of the story, see default.hotplug usb.agent kobject_uevent

udev Is a daemon process that scans for devices and dynamically creates the corresponding nodes in /dev/ Replaces devfs See Linux symposium paper for more details

Loading Firmware Some devices require firmware to be loaded by OS (e.g., to save cost of EEPROM on device) Should load the firmware from a file Opening files directly from inside the kernel is bad (why?) Special firmware interface solves this problem for drivers

Kernel Firmware Interface #include <linux/firmware.h> int request_firmware(const struct firmware **fw, char *name, struct device *device); struct firmware { size_t size; u8 *data; }; name is name of firmware file Will block until the operation is done Result is returned in firmware struct

Kernel Firmware Interface When done, release the in-kernel firmware structure void release_firmware(struct firmware *fw);