Understanding the Linux Kernel Ch.13 I/O Architecture and by 4l1u8V6

VIEWS: 0 PAGES: 24

									 Understanding the Linux Kernel
Ch.13 I/O Architecture and Device
             Drivers
                                       Jung-chul Ahn




        Embedded Software Laboratory           2012-03-08
Contents
   I/O Architecture
   The Device Driver Model
   Device Files
   Device Drivers
   Character Device Drivers




    2/24               Embedded Software Laboratory   2012-03-08
I/O Architecture
   PC’s I/O Architecture
                              CPU




                                    I/O bus



            I/O Port                                I/O port
           Values              …………
           Status
           Command

                             I/O Interface

                  values in the I/O ports to
                  command and data for the device              updates the status register

                                                                                                  I/O
           -commands            Device                                                           Device
           -data               Controller
                                                                            electrical signals

    3/24                               Embedded Software Laboratory                              2012-03-08
I/O Architecture
   I/O Ports
       IBM PC architecture
           I/O address space provides up to 65536 8-bit I/O ports
           Four special assembly language instructions
               in, ins, out, outs
               allow the CPU to read from and write into an I/O port
           I/O ports may also be mapped into addresses of the physical address
            space
               can be combined with DMA
   I/O Interfaces
       I/O interface is a hardware circuit inserted between a group of I/O
        ports and the corresponding device controller
       Translation
           Values in the I/O ports -> command and data for the device
           changes in the device state -> status register.


    4/24                             Embedded Software Laboratory        2012-03-08
The Device Driver Model
   Recent devices
        Drivers should typically take care of:
             Power management
             Plug and play
             Hot-plugging
        Power management examples
                                                           disk controller     hard disk


                                                           graphics card
            Battery           Kernel     standby
           powered
           computer                                         sound card
        status=standby
                                                           network card




    5/24                         Embedded Software Laboratory                2012-03-08
The Device Driver Model
   Device driver model
       Linux 2.6 provides some data structures and helper functions that
        offer a unifying view of all buses, devices, and device drivers in the
        system
   The sysfs filesystem
       A special filesystem that is mounted on the /sys directory
       expose the hierarchical relationships among the components of the
        device driver model
       regular files in the sysfs filesystem is to represent attributes of drivers
        and devices
           ex) dev file in the /sys/block/had directory contains the major and minor
            numbers of the master disk in the first IDE chain.




    6/24                          Embedded Software Laboratory              2012-03-08
The Device Driver Model
   Kobjects
       The core data structure of the device driver model
       each kobject corresponds to a directory in that filesystem.
       Kobjects are embedded inside larger objects that describe the
        components of the device driver model
                  Type         Field                                    Description
        char *               k_name    Pointer to a string holding the name of the container
        char []              name      String holding the name of the container
        struct k_ref         kref      The reference counter for the container
        struct list_head     entry     Pointers for the list in which the kobject is inserted
        struct kobject *     parent    Pointer to the parent kobject, if any
        struct kset *        kset      pointer to the containing kset
        struct kobj_type *   ktype     Pointer to the kobject type descriptor
        struct dentry *      dentry    Pointer to the dentry of sysfs file associated with kobject



    7/24                               Embedded Software Laboratory                                  2012-03-08
The Device Driver Model
   kset
       a collection of kobjects of the same type
   subsystems
       Collections of ksets
       may include ksets of different types
   Registering kobjects, ksets, and subsystems
       kobject_register(), kobject_register()
       kset_register(), kset_unregister()
       subsystem_register(), subsystem_unregister()




    8/24                      Embedded Software Laboratory   2012-03-08
The Device Driver Model
   Relationship between data structures
                                                  subsystem
                                                                    kset

                                                                                 kobj



                               kset                                                   kset

                               subsys         entry    parent      name               subsys     kobj


                                list                        kobj                        list   entry parent   name




    kobject                     kobject                            kobject
               kset    name                kset     name                      kset    name

              parent   entry              parent    entry                    parent   entry


     9/24                              Embedded Software Laboratory                               2012-03-08
The Device Driver Model
   Device driver model hierarchy

                                                  /sys



       susbsystem                                 bus



       susbsystem                                 pci                    usb



       kset                     drivers             devices



       kobject            serial
              device driver for the serial port




    10/24                                 Embedded Software Laboratory         2012-03-08
The Device Driver Model
   Components of the device driver model
       device object
           globally collected in the devices_subsys subsystem (/sys/devices)
                  Type                  Field                      Description
        struct list_head         node              pointers for the list of sibling devices

        struct list_head         bus_list          pointers for the list of devices on the same
                                                   bus type
        struct list_head         driver_list       pointers for the dirver’s list of devices

        struct list_head         children          head of the list of children devices

        struct device *          parent            pointer to the parent device
        struct kobject           kobj              embedded kobject
        char []                  bus               pointer to the hosting bus
        struct device_driver *   driver            pointer to controlling device driver

       Register functions
           device_register(), device_unregister()


    11/24                                   Embedded Software Laboratory                          2012-03-08
The Device Driver Model
    device_driver object
                  Type                     Field                           Description
 char *                          name               Name of the device driver
 struct bus_type *               bus                Pointer to descriptor of the bus that hosts the supported
                                                    devices
 struct kobject                  kobj               Embedded kobject
 struct list_head                devices            head of the list including all devices supported by the
                                                    driver
 struct module *                 owner              Identifies the module that implements the device driver, if
                                                    any
 int (*)(struct device *)        probe              Method for probing the device
 int (*)(struct device *)        remove             Method invoked on a device when it is removed
 void (*)(struct device *)       shutdown           Method invoked on a device when it is powered off
 int (*)(struct device *,        suspend            Method invoked on a device when it is put in low-power
                                                    state
 unsigned long, unsigned long

    Register functions
         driver_register(), driver_unregister()


  12/24                            Embedded Software Laboratory                             2012-03-08
The Device Driver Model
   bus_type object
                     Type                              Field                          Description
    char *                                   name              Name of the bus type
    struct subsystem                         subsys            Kobject subsystem associated with this bus type
    struct kset                              drivers           The set of kobjects of the drivers
    struct kset                              devices           The set of kobjects of the devices
    struct bus_attribute *                   bus_attrs         Pointer to the object including the bus attributes
    struct device_attribute *                dev_attrs         Pointer to the object including the device attributes
    struct driver_attribute                  drv_attrs         Pointer to the object including the device driver attributes
    int (*)(struct device *, struct          match             Method for checking whether a given driver supports a
    device_driver *)                                           given device
    int (*)(struct device *, char **, int,   hotplug           Method invoked when a device is being registered
    char *, int)
    int (*)(struct device *, unsigned        suspend           Method for saving the hardware context state and
    long)                                                      changing the power level of device
    int (*)(struct device *)                 resume            Method for changing the power level and restoring the
                                                               hardware context of a device


        bus_register(), bus_unregister()
      13/24                                    Embedded Software Laboratory                               2012-03-08
The Device Driver Model
   Bus registration                                                              bus_subsys

819 int bus_register(struct bus_type * bus)
820{
821        int retval;
822
823        BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
825        retval = kobject_set_name(&bus->subsys.kset.kobj, "%s",
           bus->name);
             ..
829        subsys_set_kset(bus, bus_subsys);
830        retval = subsystem_register(&bus->subsys);
831        ..
834        kobject_set_name(&bus->devices.kobj, "devices");
835        bus->devices.subsys = &bus->subsys
836        retval = kset_register(&bus->devices);
                  ..
840        kobject_set_name(&bus->drivers.kobj, "drivers");
841        bus->drivers.kobj.parent = &bus->subsys;
842        bus->drivers.ktype = &ktype_driver;                  pci_bus->subsys
843        retval = kset_register(&bus->drivers);
846        ..
        ..
871        return retval;
872}



    14/24                             Embedded Software Laboratory                         2012-03-08
The Device Driver Model
   Bus registration
819 int bus_register(struct bus_type * bus)                      pci_bus
820{
821      int retval;
822
823      BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
825      retval = kobject_set_name(&bus->subsys.kset.kobj, "%s",
         bus->name);
           ..
829      subsys_set_kset(bus, bus_subsys);
830      retval = subsystem_register(&bus->subsys);
831      ..
834      kobject_set_name(&bus->devices.kobj, "devices");
835      bus->devices.subsys = &bus->subsys
836      retval = kset_register(&bus->devices);
                ..
840      kobject_set_name(&bus->drivers.kobj, "drivers");
841      bus->drivers.kobj.parent = &bus->subsys;
842      bus->drivers.ktype = &ktype_driver;
843      retval = kset_register(&bus->drivers);
846      ..
         ..
871      return retval;                                   pci_bus->devices   pci_bus->drivers
872}



    15/24                            Embedded Software Laboratory                 2012-03-08
The Device Driver Model
   Bus registration(cont’d)




    16/24               Embedded Software Laboratory   2012-03-08
Device Files
   I/O devices are treated as special files called device files
       Device files can be of two types : block or character
       Major number : identifies the device type
       Minor number : identifies a specific device among a group of devices




    17/24                    Embedded Software Laboratory         2012-03-08
Device Files
   VFS Handling of Device Files
       open() system call on a device file
           resolves the pathname to the device file
           sets up the corresponding inode object, dentry object, file object
               invokes init_special_inode()
                inode object                     device driver’s function table
                        i_rdev                        def_chr_fops
                                                           or
                         i_fop                        def_blk_fops

               dentry_open()
                   allocates new file object

                    file object

                             f_op




    18/24                                Embedded Software Laboratory             2012-03-08
Device Drivers
   A device driver is the set of kernel routines that makes a
    hardware device respond to the programming interface
    defined by the canonical set of VFS functions (open, read,
    lseek, ioctl…)
   Device driver registration
       allocate a new device_driver descriptor
       inserting it in the data structures of the device driver model
       linking it to the corresponding device file(s)




    19/24                      Embedded Software Laboratory              2012-03-08
Device Drivers
   Device driver registration example
       To properly handle a generic PCI device
                                                                             pci_driver
           allocate a descriptor of type pci_driver
                                                                                          device_driver
           initilize the descriptor
           invokes pci_register_driver()
               initilizes the fields of the embedded driver descriptor
               invokes driver_register()
           kernel looks for unsupported hardware                pci_bus
               match method of the bus_type descriptor
               probe method of the device_driver object            kset   kset

           allocates a device object
           invokes device_register()                                                               kobj

                                                  devices
                                                                                  kobj

                                                                           Device driver model

    20/24                             Embedded Software Laboratory                        2012-03-08
Character Device Drivers
   cdev structure
       A character device driver is described by a cdev structure
                   Type              Field                            Description
        struct kobject             kobj         Embedded kobject
        struct module *            owner        Pointer to the module implementing the driver
        struct file_operations *   ops          Pointer to the file operations table of the device driver
        struct list_head           list         Head of the list of inodes relative to device files for
                                                this character device
        dev_t                      dev          Initial major and minor numbers assigned to the
                                                device driver
        unsigned int               count        Size of the range of device numbers assigned to the
                                                device driver


   functions
       cdev_alloc(), cdev_add()

    21/24                                 Embedded Software Laboratory                            2012-03-08
Character Device Drivers
   Accessing a Character Device Driver
       open() system call on a device file
           resolves the pathname to the device file
           sets up the corresponding inode object, dentry object, file object
             inode object                 def_chr_fops
                     i_rdev
                                            chrdev_open()
                     i_fop




             file object

                      f_op

           invokes chardev_open() immediately




    22/24                         Embedded Software Laboratory               2012-03-08
Character Device Drivers
   Accessing a Character Device Driver (cont’d)
       chrdev_open() in def_chr_fops table
           parameters : inode, file objects relative to the device file(filp)
                 invokes kboj_lookup(), computes the address of the cdev descriptor
                 sets the inode->i_dev fields to the address of the cdev descriptor
                 initializes the filp->f_ops file operations pointer with the contents of the ops field
                  of the cdev descriptor
                 excutes filp->f_ops->open
                     kobj_map’s hash table                                                     chrdev_open()

                                                   file object
                         *probes[254]
                                                                              inode object
                                                     f_op
                         *probes[253]                                            i_rdev
                                                                                  i_fop
                         *probes[252]                                            i_cdev


                              …..                           cdev descriptor
                                                                 [list]        inode object
inode->rdev               *probes[0]                            [ops]
     % 255
                            [data]
     23/24                                   Embedded Software Laboratory                     2012-03-08
References
   http://kelp.or.kr/korweblog/stories.php?topic=48
   https://www.linuxfoundation.org/en/Linux_Device_Driver_M
    odel




    24/24              Embedded Software Laboratory   2012-03-08

								
To top