Sophie

Sophie

distrib > CentOS > 6 > i386 > by-pkgid > cf93d8a8acdcc6fe2225039da0502495 > files > 4200

kernel-doc-2.6.32-131.17.1.el6.centos.plus.noarch.rpm

<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>Chapter&#160;2.&#160;Linux USB Basics</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="Writing USB Device Drivers" /><link rel="up" href="index.html" title="Writing USB Device Drivers" /><link rel="prev" href="ch01.html" title="Chapter&#160;1.&#160;Introduction" /><link rel="next" href="ch03.html" title="Chapter&#160;3.&#160;Device operation" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;2.&#160;Linux USB Basics</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="ch03.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter&#160;2.&#160;Linux USB Basics"><div class="titlepage"><div><div><h2 class="title"><a id="basics"></a>Chapter&#160;2.&#160;Linux USB Basics</h2></div></div></div><p>
      If you are going to write a Linux USB driver, please become familiar with
      the USB protocol specification. It can be found, along with many other
      useful documents, at the USB home page (see Resources). An excellent
      introduction to the Linux USB subsystem can be found at the USB Working
      Devices List (see Resources). It explains how the Linux USB subsystem is
      structured and introduces the reader to the concept of USB urbs
      (USB Request Blocks), which are essential to USB drivers.
  </p><p>
      The first thing a Linux USB driver needs to do is register itself with
      the Linux USB subsystem, giving it some information about which devices
      the driver supports and which functions to call when a device supported
      by the driver is inserted or removed from the system. All of this
      information is passed to the USB subsystem in the usb_driver structure.
      The skeleton driver declares a usb_driver as:
  </p><pre class="programlisting">
static struct usb_driver skel_driver = {
        .name        = "skeleton",
        .probe       = skel_probe,
        .disconnect  = skel_disconnect,
        .fops        = &amp;skel_fops,
        .minor       = USB_SKEL_MINOR_BASE,
        .id_table    = skel_table,
};
  </pre><p>
      The variable name is a string that describes the driver. It is used in
      informational messages printed to the system log. The probe and
      disconnect function pointers are called when a device that matches the
      information provided in the id_table variable is either seen or removed.
  </p><p>
      The fops and minor variables are optional. Most USB drivers hook into
      another kernel subsystem, such as the SCSI, network or TTY subsystem.
      These types of drivers register themselves with the other kernel
      subsystem, and any user-space interactions are provided through that
      interface. But for drivers that do not have a matching kernel subsystem,
      such as MP3 players or scanners, a method of interacting with user space
      is needed. The USB subsystem provides a way to register a minor device
      number and a set of file_operations function pointers that enable this
      user-space interaction. The skeleton driver needs this kind of interface,
      so it provides a minor starting number and a pointer to its
      file_operations functions.
  </p><p>
      The USB driver is then registered with a call to usb_register, usually in
      the driver's init function, as shown here:
  </p><pre class="programlisting">
static int __init usb_skel_init(void)
{
        int result;

        /* register this driver with the USB subsystem */
        result = usb_register(&amp;skel_driver);
        if (result &lt; 0) {
                err("usb_register failed for the "__FILE__ "driver."
                    "Error number %d", result);
                return -1;
        }

        return 0;
}
module_init(usb_skel_init);
  </pre><p>
      When the driver is unloaded from the system, it needs to deregister
      itself with the USB subsystem. This is done with the usb_deregister
      function:
  </p><pre class="programlisting">
static void __exit usb_skel_exit(void)
{
        /* deregister this driver with the USB subsystem */
        usb_deregister(&amp;skel_driver);
}
module_exit(usb_skel_exit);
  </pre><p>
     To enable the linux-hotplug system to load the driver automatically when
     the device is plugged in, you need to create a MODULE_DEVICE_TABLE. The
     following code tells the hotplug scripts that this module supports a
     single device with a specific vendor and product ID:
  </p><pre class="programlisting">
/* table of devices that work with this driver */
static struct usb_device_id skel_table [] = {
        { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
        { }                      /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, skel_table);
  </pre><p>
     There are other macros that can be used in describing a usb_device_id for
     drivers that support a whole class of USB drivers. See usb.h for more
     information on this.
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="ch03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;1.&#160;Introduction&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Chapter&#160;3.&#160;Device operation</td></tr></table></div></body></html>