1<html><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.78.1"><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="intro.html" title="Chapter&#160;1.&#160;Introduction"><link rel="next" href="device.html" title="Chapter&#160;3.&#160;Device operation"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><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="intro.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="device.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="basics"></a>Chapter&#160;2.&#160;Linux USB Basics</h1></div></div></div><p>
2      If you are going to write a Linux USB driver, please become familiar with
3      the USB protocol specification. It can be found, along with many other
4      useful documents, at the USB home page (see Resources). An excellent
5      introduction to the Linux USB subsystem can be found at the USB Working
6      Devices List (see Resources). It explains how the Linux USB subsystem is
7      structured and introduces the reader to the concept of USB urbs
8      (USB Request Blocks), which are essential to USB drivers.
9  </p><p>
10      The first thing a Linux USB driver needs to do is register itself with
11      the Linux USB subsystem, giving it some information about which devices
12      the driver supports and which functions to call when a device supported
13      by the driver is inserted or removed from the system. All of this
14      information is passed to the USB subsystem in the usb_driver structure.
15      The skeleton driver declares a usb_driver as:
16  </p><pre class="programlisting">
17static struct usb_driver skel_driver = {
18        .name        = "skeleton",
19        .probe       = skel_probe,
20        .disconnect  = skel_disconnect,
21        .fops        = &amp;skel_fops,
22        .minor       = USB_SKEL_MINOR_BASE,
23        .id_table    = skel_table,
24};
25  </pre><p>
26      The variable name is a string that describes the driver. It is used in
27      informational messages printed to the system log. The probe and
28      disconnect function pointers are called when a device that matches the
29      information provided in the id_table variable is either seen or removed.
30  </p><p>
31      The fops and minor variables are optional. Most USB drivers hook into
32      another kernel subsystem, such as the SCSI, network or TTY subsystem.
33      These types of drivers register themselves with the other kernel
34      subsystem, and any user-space interactions are provided through that
35      interface. But for drivers that do not have a matching kernel subsystem,
36      such as MP3 players or scanners, a method of interacting with user space
37      is needed. The USB subsystem provides a way to register a minor device
38      number and a set of file_operations function pointers that enable this
39      user-space interaction. The skeleton driver needs this kind of interface,
40      so it provides a minor starting number and a pointer to its
41      file_operations functions.
42  </p><p>
43      The USB driver is then registered with a call to usb_register, usually in
44      the driver's init function, as shown here:
45  </p><pre class="programlisting">
46static int __init usb_skel_init(void)
47{
48        int result;
49
50        /* register this driver with the USB subsystem */
51        result = usb_register(&amp;skel_driver);
52        if (result &lt; 0) {
53                err("usb_register failed for the "__FILE__ "driver."
54                    "Error number %d", result);
55                return -1;
56        }
57
58        return 0;
59}
60module_init(usb_skel_init);
61  </pre><p>
62      When the driver is unloaded from the system, it needs to deregister
63      itself with the USB subsystem. This is done with the usb_deregister
64      function:
65  </p><pre class="programlisting">
66static void __exit usb_skel_exit(void)
67{
68        /* deregister this driver with the USB subsystem */
69        usb_deregister(&amp;skel_driver);
70}
71module_exit(usb_skel_exit);
72  </pre><p>
73     To enable the linux-hotplug system to load the driver automatically when
74     the device is plugged in, you need to create a MODULE_DEVICE_TABLE. The
75     following code tells the hotplug scripts that this module supports a
76     single device with a specific vendor and product ID:
77  </p><pre class="programlisting">
78/* table of devices that work with this driver */
79static struct usb_device_id skel_table [] = {
80        { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
81        { }                      /* Terminating entry */
82};
83MODULE_DEVICE_TABLE (usb, skel_table);
84  </pre><p>
85     There are other macros that can be used in describing a usb_device_id for
86     drivers that support a whole class of USB drivers. See usb.h for more
87     information on this.
88  </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="intro.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="device.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>
89