162306a36Sopenharmony_ci.. _writing-usb-driver: 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci========================== 462306a36Sopenharmony_ciWriting USB Device Drivers 562306a36Sopenharmony_ci========================== 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci:Author: Greg Kroah-Hartman 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciIntroduction 1062306a36Sopenharmony_ci============ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciThe Linux USB subsystem has grown from supporting only two different 1362306a36Sopenharmony_citypes of devices in the 2.2.7 kernel (mice and keyboards), to over 20 1462306a36Sopenharmony_cidifferent types of devices in the 2.4 kernel. Linux currently supports 1562306a36Sopenharmony_cialmost all USB class devices (standard types of devices like keyboards, 1662306a36Sopenharmony_cimice, modems, printers and speakers) and an ever-growing number of 1762306a36Sopenharmony_civendor-specific devices (such as USB to serial converters, digital 1862306a36Sopenharmony_cicameras, Ethernet devices and MP3 players). For a full list of the 1962306a36Sopenharmony_cidifferent USB devices currently supported, see Resources. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciThe remaining kinds of USB devices that do not have support on Linux are 2262306a36Sopenharmony_cialmost all vendor-specific devices. Each vendor decides to implement a 2362306a36Sopenharmony_cicustom protocol to talk to their device, so a custom driver usually 2462306a36Sopenharmony_cineeds to be created. Some vendors are open with their USB protocols and 2562306a36Sopenharmony_cihelp with the creation of Linux drivers, while others do not publish 2662306a36Sopenharmony_cithem, and developers are forced to reverse-engineer. See Resources for 2762306a36Sopenharmony_cisome links to handy reverse-engineering tools. 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ciBecause each different protocol causes a new driver to be created, I 3062306a36Sopenharmony_cihave written a generic USB driver skeleton, modelled after the 3162306a36Sopenharmony_cipci-skeleton.c file in the kernel source tree upon which many PCI 3262306a36Sopenharmony_cinetwork drivers have been based. This USB skeleton can be found at 3362306a36Sopenharmony_cidrivers/usb/usb-skeleton.c in the kernel source tree. In this article I 3462306a36Sopenharmony_ciwill walk through the basics of the skeleton driver, explaining the 3562306a36Sopenharmony_cidifferent pieces and what needs to be done to customize it to your 3662306a36Sopenharmony_cispecific device. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciLinux USB Basics 3962306a36Sopenharmony_ci================ 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ciIf you are going to write a Linux USB driver, please become familiar 4262306a36Sopenharmony_ciwith the USB protocol specification. It can be found, along with many 4362306a36Sopenharmony_ciother useful documents, at the USB home page (see Resources). An 4462306a36Sopenharmony_ciexcellent introduction to the Linux USB subsystem can be found at the 4562306a36Sopenharmony_ciUSB Working Devices List (see Resources). It explains how the Linux USB 4662306a36Sopenharmony_cisubsystem is structured and introduces the reader to the concept of USB 4762306a36Sopenharmony_ciurbs (USB Request Blocks), which are essential to USB drivers. 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciThe first thing a Linux USB driver needs to do is register itself with 5062306a36Sopenharmony_cithe Linux USB subsystem, giving it some information about which devices 5162306a36Sopenharmony_cithe driver supports and which functions to call when a device supported 5262306a36Sopenharmony_ciby the driver is inserted or removed from the system. All of this 5362306a36Sopenharmony_ciinformation is passed to the USB subsystem in the :c:type:`usb_driver` 5462306a36Sopenharmony_cistructure. The skeleton driver declares a :c:type:`usb_driver` as:: 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci static struct usb_driver skel_driver = { 5762306a36Sopenharmony_ci .name = "skeleton", 5862306a36Sopenharmony_ci .probe = skel_probe, 5962306a36Sopenharmony_ci .disconnect = skel_disconnect, 6062306a36Sopenharmony_ci .suspend = skel_suspend, 6162306a36Sopenharmony_ci .resume = skel_resume, 6262306a36Sopenharmony_ci .pre_reset = skel_pre_reset, 6362306a36Sopenharmony_ci .post_reset = skel_post_reset, 6462306a36Sopenharmony_ci .id_table = skel_table, 6562306a36Sopenharmony_ci .supports_autosuspend = 1, 6662306a36Sopenharmony_ci }; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ciThe variable name is a string that describes the driver. It is used in 7062306a36Sopenharmony_ciinformational messages printed to the system log. The probe and 7162306a36Sopenharmony_cidisconnect function pointers are called when a device that matches the 7262306a36Sopenharmony_ciinformation provided in the ``id_table`` variable is either seen or 7362306a36Sopenharmony_ciremoved. 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ciThe fops and minor variables are optional. Most USB drivers hook into 7662306a36Sopenharmony_cianother kernel subsystem, such as the SCSI, network or TTY subsystem. 7762306a36Sopenharmony_ciThese types of drivers register themselves with the other kernel 7862306a36Sopenharmony_cisubsystem, and any user-space interactions are provided through that 7962306a36Sopenharmony_ciinterface. But for drivers that do not have a matching kernel subsystem, 8062306a36Sopenharmony_cisuch as MP3 players or scanners, a method of interacting with user space 8162306a36Sopenharmony_ciis needed. The USB subsystem provides a way to register a minor device 8262306a36Sopenharmony_cinumber and a set of :c:type:`file_operations` function pointers that enable 8362306a36Sopenharmony_cithis user-space interaction. The skeleton driver needs this kind of 8462306a36Sopenharmony_ciinterface, so it provides a minor starting number and a pointer to its 8562306a36Sopenharmony_ci:c:type:`file_operations` functions. 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ciThe USB driver is then registered with a call to usb_register(), 8862306a36Sopenharmony_ciusually in the driver's init function, as shown here:: 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci static int __init usb_skel_init(void) 9162306a36Sopenharmony_ci { 9262306a36Sopenharmony_ci int result; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci /* register this driver with the USB subsystem */ 9562306a36Sopenharmony_ci result = usb_register(&skel_driver); 9662306a36Sopenharmony_ci if (result < 0) { 9762306a36Sopenharmony_ci pr_err("usb_register failed for the %s driver. Error number %d\n", 9862306a36Sopenharmony_ci skel_driver.name, result); 9962306a36Sopenharmony_ci return -1; 10062306a36Sopenharmony_ci } 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci return 0; 10362306a36Sopenharmony_ci } 10462306a36Sopenharmony_ci module_init(usb_skel_init); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ciWhen the driver is unloaded from the system, it needs to deregister 10862306a36Sopenharmony_ciitself with the USB subsystem. This is done with usb_deregister() 10962306a36Sopenharmony_cifunction:: 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci static void __exit usb_skel_exit(void) 11262306a36Sopenharmony_ci { 11362306a36Sopenharmony_ci /* deregister this driver with the USB subsystem */ 11462306a36Sopenharmony_ci usb_deregister(&skel_driver); 11562306a36Sopenharmony_ci } 11662306a36Sopenharmony_ci module_exit(usb_skel_exit); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ciTo enable the linux-hotplug system to load the driver automatically when 12062306a36Sopenharmony_cithe device is plugged in, you need to create a ``MODULE_DEVICE_TABLE``. 12162306a36Sopenharmony_ciThe following code tells the hotplug scripts that this module supports a 12262306a36Sopenharmony_cisingle device with a specific vendor and product ID:: 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci /* table of devices that work with this driver */ 12562306a36Sopenharmony_ci static struct usb_device_id skel_table [] = { 12662306a36Sopenharmony_ci { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, 12762306a36Sopenharmony_ci { } /* Terminating entry */ 12862306a36Sopenharmony_ci }; 12962306a36Sopenharmony_ci MODULE_DEVICE_TABLE (usb, skel_table); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciThere are other macros that can be used in describing a struct 13362306a36Sopenharmony_ci:c:type:`usb_device_id` for drivers that support a whole class of USB 13462306a36Sopenharmony_cidrivers. See :ref:`usb.h <usb_header>` for more information on this. 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ciDevice operation 13762306a36Sopenharmony_ci================ 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ciWhen a device is plugged into the USB bus that matches the device ID 14062306a36Sopenharmony_cipattern that your driver registered with the USB core, the probe 14162306a36Sopenharmony_cifunction is called. The :c:type:`usb_device` structure, interface number and 14262306a36Sopenharmony_cithe interface ID are passed to the function:: 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci static int skel_probe(struct usb_interface *interface, 14562306a36Sopenharmony_ci const struct usb_device_id *id) 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ciThe driver now needs to verify that this device is actually one that it 14962306a36Sopenharmony_cican accept. If so, it returns 0. If not, or if any error occurs during 15062306a36Sopenharmony_ciinitialization, an errorcode (such as ``-ENOMEM`` or ``-ENODEV``) is 15162306a36Sopenharmony_cireturned from the probe function. 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ciIn the skeleton driver, we determine what end points are marked as 15462306a36Sopenharmony_cibulk-in and bulk-out. We create buffers to hold the data that will be 15562306a36Sopenharmony_cisent and received from the device, and a USB urb to write data to the 15662306a36Sopenharmony_cidevice is initialized. 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ciConversely, when the device is removed from the USB bus, the disconnect 15962306a36Sopenharmony_cifunction is called with the device pointer. The driver needs to clean 16062306a36Sopenharmony_ciany private data that has been allocated at this time and to shut down 16162306a36Sopenharmony_ciany pending urbs that are in the USB system. 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ciNow that the device is plugged into the system and the driver is bound 16462306a36Sopenharmony_cito the device, any of the functions in the :c:type:`file_operations` structure 16562306a36Sopenharmony_cithat were passed to the USB subsystem will be called from a user program 16662306a36Sopenharmony_citrying to talk to the device. The first function called will be open, as 16762306a36Sopenharmony_cithe program tries to open the device for I/O. We increment our private 16862306a36Sopenharmony_ciusage count and save a pointer to our internal structure in the file 16962306a36Sopenharmony_cistructure. This is done so that future calls to file operations will 17062306a36Sopenharmony_cienable the driver to determine which device the user is addressing. All 17162306a36Sopenharmony_ciof this is done with the following code:: 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci /* increment our usage count for the device */ 17462306a36Sopenharmony_ci kref_get(&dev->kref); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci /* save our object in the file's private structure */ 17762306a36Sopenharmony_ci file->private_data = dev; 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ciAfter the open function is called, the read and write functions are 18162306a36Sopenharmony_cicalled to receive and send data to the device. In the ``skel_write`` 18262306a36Sopenharmony_cifunction, we receive a pointer to some data that the user wants to send 18362306a36Sopenharmony_cito the device and the size of the data. The function determines how much 18462306a36Sopenharmony_cidata it can send to the device based on the size of the write urb it has 18562306a36Sopenharmony_cicreated (this size depends on the size of the bulk out end point that 18662306a36Sopenharmony_cithe device has). Then it copies the data from user space to kernel 18762306a36Sopenharmony_cispace, points the urb to the data and submits the urb to the USB 18862306a36Sopenharmony_cisubsystem. This can be seen in the following code:: 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci /* we can only write as much as 1 urb will hold */ 19162306a36Sopenharmony_ci size_t writesize = min_t(size_t, count, MAX_TRANSFER); 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci /* copy the data from user space into our urb */ 19462306a36Sopenharmony_ci copy_from_user(buf, user_buffer, writesize); 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci /* set up our urb */ 19762306a36Sopenharmony_ci usb_fill_bulk_urb(urb, 19862306a36Sopenharmony_ci dev->udev, 19962306a36Sopenharmony_ci usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), 20062306a36Sopenharmony_ci buf, 20162306a36Sopenharmony_ci writesize, 20262306a36Sopenharmony_ci skel_write_bulk_callback, 20362306a36Sopenharmony_ci dev); 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci /* send the data out the bulk port */ 20662306a36Sopenharmony_ci retval = usb_submit_urb(urb, GFP_KERNEL); 20762306a36Sopenharmony_ci if (retval) { 20862306a36Sopenharmony_ci dev_err(&dev->interface->dev, 20962306a36Sopenharmony_ci "%s - failed submitting write urb, error %d\n", 21062306a36Sopenharmony_ci __func__, retval); 21162306a36Sopenharmony_ci } 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ciWhen the write urb is filled up with the proper information using the 21562306a36Sopenharmony_ci:c:func:`usb_fill_bulk_urb` function, we point the urb's completion callback 21662306a36Sopenharmony_cito call our own ``skel_write_bulk_callback`` function. This function is 21762306a36Sopenharmony_cicalled when the urb is finished by the USB subsystem. The callback 21862306a36Sopenharmony_cifunction is called in interrupt context, so caution must be taken not to 21962306a36Sopenharmony_cido very much processing at that time. Our implementation of 22062306a36Sopenharmony_ci``skel_write_bulk_callback`` merely reports if the urb was completed 22162306a36Sopenharmony_cisuccessfully or not and then returns. 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ciThe read function works a bit differently from the write function in 22462306a36Sopenharmony_cithat we do not use an urb to transfer data from the device to the 22562306a36Sopenharmony_cidriver. Instead we call the :c:func:`usb_bulk_msg` function, which can be used 22662306a36Sopenharmony_cito send or receive data from a device without having to create urbs and 22762306a36Sopenharmony_cihandle urb completion callback functions. We call the :c:func:`usb_bulk_msg` 22862306a36Sopenharmony_cifunction, giving it a buffer into which to place any data received from 22962306a36Sopenharmony_cithe device and a timeout value. If the timeout period expires without 23062306a36Sopenharmony_cireceiving any data from the device, the function will fail and return an 23162306a36Sopenharmony_cierror message. This can be shown with the following code:: 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci /* do an immediate bulk read to get data from the device */ 23462306a36Sopenharmony_ci retval = usb_bulk_msg (skel->dev, 23562306a36Sopenharmony_ci usb_rcvbulkpipe (skel->dev, 23662306a36Sopenharmony_ci skel->bulk_in_endpointAddr), 23762306a36Sopenharmony_ci skel->bulk_in_buffer, 23862306a36Sopenharmony_ci skel->bulk_in_size, 23962306a36Sopenharmony_ci &count, 5000); 24062306a36Sopenharmony_ci /* if the read was successful, copy the data to user space */ 24162306a36Sopenharmony_ci if (!retval) { 24262306a36Sopenharmony_ci if (copy_to_user (buffer, skel->bulk_in_buffer, count)) 24362306a36Sopenharmony_ci retval = -EFAULT; 24462306a36Sopenharmony_ci else 24562306a36Sopenharmony_ci retval = count; 24662306a36Sopenharmony_ci } 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciThe :c:func:`usb_bulk_msg` function can be very useful for doing single reads 25062306a36Sopenharmony_cior writes to a device; however, if you need to read or write constantly to 25162306a36Sopenharmony_cia device, it is recommended to set up your own urbs and submit them to 25262306a36Sopenharmony_cithe USB subsystem. 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ciWhen the user program releases the file handle that it has been using to 25562306a36Sopenharmony_citalk to the device, the release function in the driver is called. In 25662306a36Sopenharmony_cithis function we decrement our private usage count and wait for possible 25762306a36Sopenharmony_cipending writes:: 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci /* decrement our usage count for the device */ 26062306a36Sopenharmony_ci --skel->open_count; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ciOne of the more difficult problems that USB drivers must be able to 26462306a36Sopenharmony_cihandle smoothly is the fact that the USB device may be removed from the 26562306a36Sopenharmony_cisystem at any point in time, even if a program is currently talking to 26662306a36Sopenharmony_ciit. It needs to be able to shut down any current reads and writes and 26762306a36Sopenharmony_cinotify the user-space programs that the device is no longer there. The 26862306a36Sopenharmony_cifollowing code (function ``skel_delete``) is an example of how to do 26962306a36Sopenharmony_cithis:: 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci static inline void skel_delete (struct usb_skel *dev) 27262306a36Sopenharmony_ci { 27362306a36Sopenharmony_ci kfree (dev->bulk_in_buffer); 27462306a36Sopenharmony_ci if (dev->bulk_out_buffer != NULL) 27562306a36Sopenharmony_ci usb_free_coherent (dev->udev, dev->bulk_out_size, 27662306a36Sopenharmony_ci dev->bulk_out_buffer, 27762306a36Sopenharmony_ci dev->write_urb->transfer_dma); 27862306a36Sopenharmony_ci usb_free_urb (dev->write_urb); 27962306a36Sopenharmony_ci kfree (dev); 28062306a36Sopenharmony_ci } 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ciIf a program currently has an open handle to the device, we reset the 28462306a36Sopenharmony_ciflag ``device_present``. For every read, write, release and other 28562306a36Sopenharmony_cifunctions that expect a device to be present, the driver first checks 28662306a36Sopenharmony_cithis flag to see if the device is still present. If not, it releases 28762306a36Sopenharmony_cithat the device has disappeared, and a ``-ENODEV`` error is returned to the 28862306a36Sopenharmony_ciuser-space program. When the release function is eventually called, it 28962306a36Sopenharmony_cidetermines if there is no device and if not, it does the cleanup that 29062306a36Sopenharmony_cithe ``skel_disconnect`` function normally does if there are no open files 29162306a36Sopenharmony_cion the device (see Listing 5). 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ciIsochronous Data 29462306a36Sopenharmony_ci================ 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ciThis usb-skeleton driver does not have any examples of interrupt or 29762306a36Sopenharmony_ciisochronous data being sent to or from the device. Interrupt data is 29862306a36Sopenharmony_cisent almost exactly as bulk data is, with a few minor exceptions. 29962306a36Sopenharmony_ciIsochronous data works differently with continuous streams of data being 30062306a36Sopenharmony_cisent to or from the device. The audio and video camera drivers are very 30162306a36Sopenharmony_cigood examples of drivers that handle isochronous data and will be useful 30262306a36Sopenharmony_ciif you also need to do this. 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ciConclusion 30562306a36Sopenharmony_ci========== 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ciWriting Linux USB device drivers is not a difficult task as the 30862306a36Sopenharmony_ciusb-skeleton driver shows. This driver, combined with the other current 30962306a36Sopenharmony_ciUSB drivers, should provide enough examples to help a beginning author 31062306a36Sopenharmony_cicreate a working driver in a minimal amount of time. The linux-usb-devel 31162306a36Sopenharmony_cimailing list archives also contain a lot of helpful information. 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ciResources 31462306a36Sopenharmony_ci========= 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ciThe Linux USB Project: 31762306a36Sopenharmony_cihttp://www.linux-usb.org/ 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ciLinux Hotplug Project: 32062306a36Sopenharmony_cihttp://linux-hotplug.sourceforge.net/ 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_cilinux-usb Mailing List Archives: 32362306a36Sopenharmony_cihttps://lore.kernel.org/linux-usb/ 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ciProgramming Guide for Linux USB Device Drivers: 32662306a36Sopenharmony_cihttps://lmu.web.psi.ch/docu/manuals/software_manuals/linux_sl/usb_linux_programming_guide.pdf 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ciUSB Home Page: https://www.usb.org 329