1 / 24

Understanding the Linux Kernel Ch.13 I/O Architecture and Device Drivers

Understanding the Linux Kernel Ch.13 I/O Architecture and Device Drivers. Jung-chul Ahn. Contents. I/O Architecture The Device Driver Model Device Files Device Drivers Character Device Drivers. I/O Architecture. PC’s I/O Architecture. CPU. I/O bus. ………… I/O Interface. I/O Port.

takara
Download Presentation

Understanding the Linux Kernel Ch.13 I/O Architecture and Device Drivers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Understanding the Linux KernelCh.13 I/O Architecture and Device Drivers Jung-chul Ahn Embedded Software Laboratory

  2. Contents • I/O Architecture • The Device Driver Model • Device Files • Device Drivers • Character Device Drivers Embedded Software Laboratory

  3. I/O Architecture • PC’s I/O Architecture CPU I/O bus ………… I/O Interface I/O Port I/O port Values Status Command values in the I/O ports to command and data for the device I/O Device updates the status register Device Controller -commands -data electrical signals Embedded Software Laboratory

  4. 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. Embedded Software Laboratory

  5. The Device Driver Model • Recent devices • Drivers should typically take care of: • Power management • Plug and play • Hot-plugging • Power management examples hard disk disk controller Kernel Battery powered computer status=standby graphics card standby sound card network card Embedded Software Laboratory

  6. 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. Embedded Software Laboratory

  7. 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 Embedded Software Laboratory

  8. 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() Embedded Software Laboratory

  9. The Device Driver Model • Relationship between data structures subsystem kset kobj kset kset subsys entry parent name subsys kobj list entry parent name list kobj kobject kobject kobject kset name kset name kset name parent entry parent entry parent entry Embedded Software Laboratory

  10. The Device Driver Model • Device driver model hierarchy /sys bus susbsystem pci usb susbsystem drivers devices kset kobject serial device driver for the serial port Embedded Software Laboratory

  11. The Device Driver Model • Components of the device driver model • device object • globally collected in the devices_subsys subsystem (/sys/devices) • Register functions • device_register(), device_unregister() Embedded Software Laboratory

  12. The Device Driver Model • device_driver object • Register functions • driver_register(), driver_unregister() Embedded Software Laboratory

  13. The Device Driver Model • bus_type object • bus_register(), bus_unregister() Embedded Software Laboratory

  14. 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; 843 retval = kset_register(&bus->drivers); 846 .. .. 871 return retval; 872} pci_bus->subsys Embedded Software Laboratory

  15. The Device Driver Model • Bus registration 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; 843 retval = kset_register(&bus->drivers); 846 .. .. 871 return retval; 872} pci_bus pci_bus->devices pci_bus->drivers Embedded Software Laboratory

  16. The Device Driver Model • Bus registration(cont’d) Embedded Software Laboratory

  17. 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 Embedded Software Laboratory

  18. 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() • dentry_open() • allocates new file object inode object device driver’s function table i_rdev def_chr_fops or def_blk_fops i_fop file object f_op Embedded Software Laboratory

  19. 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) Embedded Software Laboratory

  20. Device Drivers device_driver • Device driver registration example • To properly handle a generic PCI device • allocate a descriptor of type pci_driver • initilize the descriptor • invokes pci_register_driver() • initilizes the fields of the embedded driver descriptor • invokes driver_register() • kernel looks for unsupported hardware • match method of the bus_type descriptor • probe method of the device_driver object • allocates a device object • invokes device_register() pci_driver pci_bus kset kset kobj devices kobj Device driver model Embedded Software Laboratory

  21. Character Device Drivers • cdev structure • A character device driver is described by a cdev structure • functions • cdev_alloc(), cdev_add() Embedded Software Laboratory

  22. 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 • invokes chardev_open() immediately inode object def_chr_fops i_rdev chrdev_open() i_fop file object f_op Embedded Software Laboratory

  23. 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 chrdev_open() kobj_map’s hash table *probes[254] file object inode object f_op *probes[253] i_rdev i_fop *probes[252] i_cdev ….. cdev descriptor [list] [ops] inode object inode->rdev % 255 *probes[0] [data] Embedded Software Laboratory

  24. References • http://kelp.or.kr/korweblog/stories.php?topic=48 • https://www.linuxfoundation.org/en/Linux_Device_Driver_Model Embedded Software Laboratory

More Related