Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 1 | ========================== |
| 2 | Writing USB Device Drivers |
| 3 | ========================== |
| 4 | |
| 5 | :Author: Greg Kroah-Hartman |
| 6 | |
| 7 | Introduction |
| 8 | ============ |
| 9 | |
| 10 | The Linux USB subsystem has grown from supporting only two different |
| 11 | types of devices in the 2.2.7 kernel (mice and keyboards), to over 20 |
| 12 | different types of devices in the 2.4 kernel. Linux currently supports |
| 13 | almost all USB class devices (standard types of devices like keyboards, |
| 14 | mice, modems, printers and speakers) and an ever-growing number of |
| 15 | vendor-specific devices (such as USB to serial converters, digital |
| 16 | cameras, Ethernet devices and MP3 players). For a full list of the |
| 17 | different USB devices currently supported, see Resources. |
| 18 | |
| 19 | The remaining kinds of USB devices that do not have support on Linux are |
| 20 | almost all vendor-specific devices. Each vendor decides to implement a |
| 21 | custom protocol to talk to their device, so a custom driver usually |
| 22 | needs to be created. Some vendors are open with their USB protocols and |
| 23 | help with the creation of Linux drivers, while others do not publish |
| 24 | them, and developers are forced to reverse-engineer. See Resources for |
| 25 | some links to handy reverse-engineering tools. |
| 26 | |
| 27 | Because each different protocol causes a new driver to be created, I |
| 28 | have written a generic USB driver skeleton, modelled after the |
| 29 | pci-skeleton.c file in the kernel source tree upon which many PCI |
| 30 | network drivers have been based. This USB skeleton can be found at |
| 31 | drivers/usb/usb-skeleton.c in the kernel source tree. In this article I |
| 32 | will walk through the basics of the skeleton driver, explaining the |
| 33 | different pieces and what needs to be done to customize it to your |
| 34 | specific device. |
| 35 | |
| 36 | Linux USB Basics |
| 37 | ================ |
| 38 | |
| 39 | If you are going to write a Linux USB driver, please become familiar |
| 40 | with the USB protocol specification. It can be found, along with many |
| 41 | other useful documents, at the USB home page (see Resources). An |
| 42 | excellent introduction to the Linux USB subsystem can be found at the |
| 43 | USB Working Devices List (see Resources). It explains how the Linux USB |
| 44 | subsystem is structured and introduces the reader to the concept of USB |
| 45 | urbs (USB Request Blocks), which are essential to USB drivers. |
| 46 | |
| 47 | The first thing a Linux USB driver needs to do is register itself with |
| 48 | the Linux USB subsystem, giving it some information about which devices |
| 49 | the driver supports and which functions to call when a device supported |
| 50 | by the driver is inserted or removed from the system. All of this |
| 51 | information is passed to the USB subsystem in the usb_driver structure. |
| 52 | The skeleton driver declares a usb_driver as: |
| 53 | |
| 54 | :: |
| 55 | |
| 56 | static struct usb_driver skel_driver = { |
| 57 | .name = "skeleton", |
| 58 | .probe = skel_probe, |
| 59 | .disconnect = skel_disconnect, |
| 60 | .fops = &skel_fops, |
| 61 | .minor = USB_SKEL_MINOR_BASE, |
| 62 | .id_table = skel_table, |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | The variable name is a string that describes the driver. It is used in |
| 67 | informational messages printed to the system log. The probe and |
| 68 | disconnect function pointers are called when a device that matches the |
| 69 | information provided in the id_table variable is either seen or |
| 70 | removed. |
| 71 | |
| 72 | The fops and minor variables are optional. Most USB drivers hook into |
| 73 | another kernel subsystem, such as the SCSI, network or TTY subsystem. |
| 74 | These types of drivers register themselves with the other kernel |
| 75 | subsystem, and any user-space interactions are provided through that |
| 76 | interface. But for drivers that do not have a matching kernel subsystem, |
| 77 | such as MP3 players or scanners, a method of interacting with user space |
| 78 | is needed. The USB subsystem provides a way to register a minor device |
| 79 | number and a set of file_operations function pointers that enable this |
| 80 | user-space interaction. The skeleton driver needs this kind of |
| 81 | interface, so it provides a minor starting number and a pointer to its |
| 82 | file_operations functions. |
| 83 | |
| 84 | The USB driver is then registered with a call to usb_register, usually |
| 85 | in the driver's init function, as shown here: |
| 86 | |
| 87 | :: |
| 88 | |
| 89 | static int __init usb_skel_init(void) |
| 90 | { |
| 91 | int result; |
| 92 | |
| 93 | /* register this driver with the USB subsystem */ |
| 94 | result = usb_register(&skel_driver); |
| 95 | if (result < 0) { |
| 96 | err("usb_register failed for the "__FILE__ "driver." |
| 97 | "Error number %d", result); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | module_init(usb_skel_init); |
| 104 | |
| 105 | |
| 106 | When the driver is unloaded from the system, it needs to deregister |
| 107 | itself with the USB subsystem. This is done with the usb_deregister |
| 108 | function: |
| 109 | |
| 110 | :: |
| 111 | |
| 112 | static void __exit usb_skel_exit(void) |
| 113 | { |
| 114 | /* deregister this driver with the USB subsystem */ |
| 115 | usb_deregister(&skel_driver); |
| 116 | } |
| 117 | module_exit(usb_skel_exit); |
| 118 | |
| 119 | |
| 120 | To enable the linux-hotplug system to load the driver automatically when |
| 121 | the device is plugged in, you need to create a MODULE_DEVICE_TABLE. |
| 122 | The following code tells the hotplug scripts that this module supports a |
| 123 | single device with a specific vendor and product ID: |
| 124 | |
| 125 | :: |
| 126 | |
| 127 | /* table of devices that work with this driver */ |
| 128 | static struct usb_device_id skel_table [] = { |
| 129 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, |
| 130 | { } /* Terminating entry */ |
| 131 | }; |
| 132 | MODULE_DEVICE_TABLE (usb, skel_table); |
| 133 | |
| 134 | |
| 135 | There are other macros that can be used in describing a usb_device_id |
| 136 | for drivers that support a whole class of USB drivers. See usb.h for |
| 137 | more information on this. |
| 138 | |
| 139 | Device operation |
| 140 | ================ |
| 141 | |
| 142 | When a device is plugged into the USB bus that matches the device ID |
| 143 | pattern that your driver registered with the USB core, the probe |
| 144 | function is called. The usb_device structure, interface number and the |
| 145 | interface ID are passed to the function: |
| 146 | |
| 147 | :: |
| 148 | |
| 149 | static int skel_probe(struct usb_interface *interface, |
| 150 | const struct usb_device_id *id) |
| 151 | |
| 152 | |
| 153 | The driver now needs to verify that this device is actually one that it |
| 154 | can accept. If so, it returns 0. If not, or if any error occurs during |
| 155 | initialization, an errorcode (such as ``-ENOMEM`` or ``-ENODEV``) is |
| 156 | returned from the probe function. |
| 157 | |
| 158 | In the skeleton driver, we determine what end points are marked as |
| 159 | bulk-in and bulk-out. We create buffers to hold the data that will be |
| 160 | sent and received from the device, and a USB urb to write data to the |
| 161 | device is initialized. |
| 162 | |
| 163 | Conversely, when the device is removed from the USB bus, the disconnect |
| 164 | function is called with the device pointer. The driver needs to clean |
| 165 | any private data that has been allocated at this time and to shut down |
| 166 | any pending urbs that are in the USB system. |
| 167 | |
| 168 | Now that the device is plugged into the system and the driver is bound |
| 169 | to the device, any of the functions in the file_operations structure |
| 170 | that were passed to the USB subsystem will be called from a user program |
| 171 | trying to talk to the device. The first function called will be open, as |
| 172 | the program tries to open the device for I/O. We increment our private |
| 173 | usage count and save a pointer to our internal structure in the file |
| 174 | structure. This is done so that future calls to file operations will |
| 175 | enable the driver to determine which device the user is addressing. All |
| 176 | of this is done with the following code: |
| 177 | |
| 178 | :: |
| 179 | |
| 180 | /* increment our usage count for the module */ |
| 181 | ++skel->open_count; |
| 182 | |
| 183 | /* save our object in the file's private structure */ |
| 184 | file->private_data = dev; |
| 185 | |
| 186 | |
| 187 | After the open function is called, the read and write functions are |
| 188 | called to receive and send data to the device. In the skel_write |
| 189 | function, we receive a pointer to some data that the user wants to send |
| 190 | to the device and the size of the data. The function determines how much |
| 191 | data it can send to the device based on the size of the write urb it has |
| 192 | created (this size depends on the size of the bulk out end point that |
| 193 | the device has). Then it copies the data from user space to kernel |
| 194 | space, points the urb to the data and submits the urb to the USB |
| 195 | subsystem. This can be seen in the following code: |
| 196 | |
| 197 | :: |
| 198 | |
| 199 | /* we can only write as much as 1 urb will hold */ |
| 200 | bytes_written = (count > skel->bulk_out_size) ? skel->bulk_out_size : count; |
| 201 | |
| 202 | /* copy the data from user space into our urb */ |
| 203 | copy_from_user(skel->write_urb->transfer_buffer, buffer, bytes_written); |
| 204 | |
| 205 | /* set up our urb */ |
| 206 | usb_fill_bulk_urb(skel->write_urb, |
| 207 | skel->dev, |
| 208 | usb_sndbulkpipe(skel->dev, skel->bulk_out_endpointAddr), |
| 209 | skel->write_urb->transfer_buffer, |
| 210 | bytes_written, |
| 211 | skel_write_bulk_callback, |
| 212 | skel); |
| 213 | |
| 214 | /* send the data out the bulk port */ |
| 215 | result = usb_submit_urb(skel->write_urb); |
| 216 | if (result) { |
| 217 | err("Failed submitting write urb, error %d", result); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | When the write urb is filled up with the proper information using the |
| 222 | usb_fill_bulk_urb function, we point the urb's completion callback to |
| 223 | call our own skel_write_bulk_callback function. This function is |
| 224 | called when the urb is finished by the USB subsystem. The callback |
| 225 | function is called in interrupt context, so caution must be taken not to |
| 226 | do very much processing at that time. Our implementation of |
| 227 | skel_write_bulk_callback merely reports if the urb was completed |
| 228 | successfully or not and then returns. |
| 229 | |
| 230 | The read function works a bit differently from the write function in |
| 231 | that we do not use an urb to transfer data from the device to the |
| 232 | driver. Instead we call the usb_bulk_msg function, which can be used |
| 233 | to send or receive data from a device without having to create urbs and |
| 234 | handle urb completion callback functions. We call the usb_bulk_msg |
| 235 | function, giving it a buffer into which to place any data received from |
| 236 | the device and a timeout value. If the timeout period expires without |
| 237 | receiving any data from the device, the function will fail and return an |
| 238 | error message. This can be shown with the following code: |
| 239 | |
| 240 | :: |
| 241 | |
| 242 | /* do an immediate bulk read to get data from the device */ |
| 243 | retval = usb_bulk_msg (skel->dev, |
| 244 | usb_rcvbulkpipe (skel->dev, |
| 245 | skel->bulk_in_endpointAddr), |
| 246 | skel->bulk_in_buffer, |
| 247 | skel->bulk_in_size, |
| 248 | &count, HZ*10); |
| 249 | /* if the read was successful, copy the data to user space */ |
| 250 | if (!retval) { |
| 251 | if (copy_to_user (buffer, skel->bulk_in_buffer, count)) |
| 252 | retval = -EFAULT; |
| 253 | else |
| 254 | retval = count; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | The usb_bulk_msg function can be very useful for doing single reads or |
| 259 | writes to a device; however, if you need to read or write constantly to |
| 260 | a device, it is recommended to set up your own urbs and submit them to |
| 261 | the USB subsystem. |
| 262 | |
| 263 | When the user program releases the file handle that it has been using to |
| 264 | talk to the device, the release function in the driver is called. In |
| 265 | this function we decrement our private usage count and wait for possible |
| 266 | pending writes: |
| 267 | |
| 268 | :: |
| 269 | |
| 270 | /* decrement our usage count for the device */ |
| 271 | --skel->open_count; |
| 272 | |
| 273 | |
| 274 | One of the more difficult problems that USB drivers must be able to |
| 275 | handle smoothly is the fact that the USB device may be removed from the |
| 276 | system at any point in time, even if a program is currently talking to |
| 277 | it. It needs to be able to shut down any current reads and writes and |
| 278 | notify the user-space programs that the device is no longer there. The |
| 279 | following code (function :c:func:`skel_delete()`) is an example of |
| 280 | how to do this: |
| 281 | |
| 282 | :: |
| 283 | |
| 284 | static inline void skel_delete (struct usb_skel *dev) |
| 285 | { |
| 286 | kfree (dev->bulk_in_buffer); |
| 287 | if (dev->bulk_out_buffer != NULL) |
| 288 | usb_free_coherent (dev->udev, dev->bulk_out_size, |
| 289 | dev->bulk_out_buffer, |
| 290 | dev->write_urb->transfer_dma); |
| 291 | usb_free_urb (dev->write_urb); |
| 292 | kfree (dev); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | If a program currently has an open handle to the device, we reset the |
| 297 | flag ``device_present``. For every read, write, release and other |
| 298 | functions that expect a device to be present, the driver first checks |
| 299 | this flag to see if the device is still present. If not, it releases |
| 300 | that the device has disappeared, and a -ENODEV error is returned to the |
| 301 | user-space program. When the release function is eventually called, it |
| 302 | determines if there is no device and if not, it does the cleanup that |
| 303 | the skel_disconnect function normally does if there are no open files |
| 304 | on the device (see Listing 5). |
| 305 | |
| 306 | Isochronous Data |
| 307 | ================ |
| 308 | |
| 309 | This usb-skeleton driver does not have any examples of interrupt or |
| 310 | isochronous data being sent to or from the device. Interrupt data is |
| 311 | sent almost exactly as bulk data is, with a few minor exceptions. |
| 312 | Isochronous data works differently with continuous streams of data being |
| 313 | sent to or from the device. The audio and video camera drivers are very |
| 314 | good examples of drivers that handle isochronous data and will be useful |
| 315 | if you also need to do this. |
| 316 | |
| 317 | Conclusion |
| 318 | ========== |
| 319 | |
| 320 | Writing Linux USB device drivers is not a difficult task as the |
| 321 | usb-skeleton driver shows. This driver, combined with the other current |
| 322 | USB drivers, should provide enough examples to help a beginning author |
| 323 | create a working driver in a minimal amount of time. The linux-usb-devel |
| 324 | mailing list archives also contain a lot of helpful information. |
| 325 | |
| 326 | Resources |
| 327 | ========= |
| 328 | |
| 329 | The Linux USB Project: |
| 330 | `http://www.linux-usb.org/ <http://www.linux-usb.org>`__ |
| 331 | |
| 332 | Linux Hotplug Project: |
| 333 | `http://linux-hotplug.sourceforge.net/ <http://linux-hotplug.sourceforge.net>`__ |
| 334 | |
| 335 | Linux USB Working Devices List: |
| 336 | `http://www.qbik.ch/usb/devices/ <http://www.qbik.ch/usb/devices>`__ |
| 337 | |
| 338 | linux-usb-devel Mailing List Archives: |
| 339 | http://marc.theaimsgroup.com/?l=linux-usb-devel |
| 340 | |
| 341 | Programming Guide for Linux USB Device Drivers: |
| 342 | http://usb.cs.tum.edu/usbdoc |
| 343 | |
| 344 | USB Home Page: http://www.usb.org |