Lines Matching +full:usb +full:- +full:c

1 .. _writing-usb-driver:
4 Writing USB Device Drivers
7 :Author: Greg Kroah-Hartman
12 The Linux USB subsystem has grown from supporting only two different
15 almost all USB class devices (standard types of devices like keyboards,
16 mice, modems, printers and speakers) and an ever-growing number of
17 vendor-specific devices (such as USB to serial converters, digital
19 different USB devices currently supported, see Resources.
21 The remaining kinds of USB devices that do not have support on Linux are
22 almost all vendor-specific devices. Each vendor decides to implement a
24 needs to be created. Some vendors are open with their USB protocols and
26 them, and developers are forced to reverse-engineer. See Resources for
27 some links to handy reverse-engineering tools.
30 have written a generic USB driver skeleton, modelled after the
31 pci-skeleton.c file in the kernel source tree upon which many PCI
32 network drivers have been based. This USB skeleton can be found at
33 drivers/usb/usb-skeleton.c in the kernel source tree. In this article I
38 Linux USB Basics
41 If you are going to write a Linux USB driver, please become familiar
42 with the USB protocol specification. It can be found, along with many
43 other useful documents, at the USB home page (see Resources). An
44 excellent introduction to the Linux USB subsystem can be found at the
45 USB Working Devices List (see Resources). It explains how the Linux USB
46 subsystem is structured and introduces the reader to the concept of USB
47 urbs (USB Request Blocks), which are essential to USB drivers.
49 The first thing a Linux USB driver needs to do is register itself with
50 the Linux USB subsystem, giving it some information about which devices
53 information is passed to the USB subsystem in the :c:type:`usb_driver`
54 structure. The skeleton driver declares a :c:type:`usb_driver` as::
75 The fops and minor variables are optional. Most USB drivers hook into
78 subsystem, and any user-space interactions are provided through that
81 is needed. The USB subsystem provides a way to register a minor device
82 number and a set of :c:type:`file_operations` function pointers that enable
83 this user-space interaction. The skeleton driver needs this kind of
85 :c:type:`file_operations` functions.
87 The USB driver is then registered with a call to usb_register(),
94 /* register this driver with the USB subsystem */
99 return -1;
108 itself with the USB subsystem. This is done with usb_deregister()
113 /* deregister this driver with the USB subsystem */
119 To enable the linux-hotplug system to load the driver automatically when
129 MODULE_DEVICE_TABLE (usb, skel_table);
133 :c:type:`usb_device_id` for drivers that support a whole class of USB
134 drivers. See :ref:`usb.h <usb_header>` for more information on this.
139 When a device is plugged into the USB bus that matches the device ID
140 pattern that your driver registered with the USB core, the probe
141 function is called. The :c:type:`usb_device` structure, interface number and
150 initialization, an errorcode (such as ``-ENOMEM`` or ``-ENODEV``) is
154 bulk-in and bulk-out. We create buffers to hold the data that will be
155 sent and received from the device, and a USB urb to write data to the
158 Conversely, when the device is removed from the USB bus, the disconnect
161 any pending urbs that are in the USB system.
164 to the device, any of the functions in the :c:type:`file_operations` structure
165 that were passed to the USB subsystem will be called from a user program
174 kref_get(&dev->kref);
177 file->private_data = dev;
187 space, points the urb to the data and submits the urb to the USB
198 dev->udev,
199 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
208 dev_err(&dev->interface->dev,
209 "%s - failed submitting write urb, error %d\n",
215 :c:func:`usb_fill_bulk_urb` function, we point the urb's completion callback
217 called when the urb is finished by the USB subsystem. The callback
225 driver. Instead we call the :c:func:`usb_bulk_msg` function, which can be used
227 handle urb completion callback functions. We call the :c:func:`usb_bulk_msg`
234 retval = usb_bulk_msg (skel->dev,
235 usb_rcvbulkpipe (skel->dev,
236 skel->bulk_in_endpointAddr),
237 skel->bulk_in_buffer,
238 skel->bulk_in_size,
242 if (copy_to_user (buffer, skel->bulk_in_buffer, count))
243 retval = -EFAULT;
249 The :c:func:`usb_bulk_msg` function can be very useful for doing single reads
252 the USB subsystem.
260 --skel->open_count;
263 One of the more difficult problems that USB drivers must be able to
264 handle smoothly is the fact that the USB device may be removed from the
267 notify the user-space programs that the device is no longer there. The
273 kfree (dev->bulk_in_buffer);
274 if (dev->bulk_out_buffer != NULL)
275 usb_free_coherent (dev->udev, dev->bulk_out_size,
276 dev->bulk_out_buffer,
277 dev->write_urb->transfer_dma);
278 usb_free_urb (dev->write_urb);
287 that the device has disappeared, and a ``-ENODEV`` error is returned to the
288 user-space program. When the release function is eventually called, it
296 This usb-skeleton driver does not have any examples of interrupt or
307 Writing Linux USB device drivers is not a difficult task as the
308 usb-skeleton driver shows. This driver, combined with the other current
309 USB drivers, should provide enough examples to help a beginning author
310 create a working driver in a minimal amount of time. The linux-usb-devel
316 The Linux USB Project:
317 http://www.linux-usb.org/
320 http://linux-hotplug.sourceforge.net/
322 linux-usb Mailing List Archives:
323 https://lore.kernel.org/linux-usb/
325 Programming Guide for Linux USB Device Drivers:
328 USB Home Page: https://www.usb.org