Pugs continued, “Let’s build upon the USB device driver coded in our previous session, using the same
handy JetFlash pen drive from Transcend, with the vendor ID 0x058f and product ID 0×6387. For that, let’s dig further into the USB protocol, and then convert our learning into code.”
USB endpoints and their types
Depending on the type and attributes of information to be transferred, a USB device may have one or more endpoints, each belonging to one of the following four categories:
- Control — to transfer control information. Examples include resetting the device, querying information about the device, etc. All USB devices always have the default control endpoint point as zero.
- Interrupt — for small and fast data transfers, typically of up to 8 bytes. Examples include data transfer for serial ports, human interface devices (HIDs) like keyboards, mouse, etc.
- Bulk — for big but comparatively slower data transfers. A typical example is data transfers for mass-storage devices.
- Isochronous — for big data transfers with a bandwidth guarantee, though data integrity may not be guaranteed. Typical practical usage examples include transfers of time-sensitive data like audio, video, etc.
Additionally, all but control endpoints could be “in” or “out”, indicating the direction of data transfer; “in” indicates data flow from the USB device to the host machine, and “out”, the other way.
Technically, an endpoint is identified using an 8-bit number, the most significant bit (MSB) of which indicates the direction — 0 means “out”, and 1 means “in”. Control endpoints are bi-directional, and the MSB is ignored.
Figure 1 shows a typical snippet of USB device specifications for devices connected on a system.
Figure 1: USB’s proc window snippet (click for larger view)
To be specific, the
E: lines in the figure show examples of an interrupt endpoint of a UHCI Host Controller, and two bulk endpoints of the pen drive under consideration. Also, the endpoint numbers (in hex) are, respectively,
0x82 — the MSB of the first and third being
1, indicating ‘in’ endpoints, represented by
(I) in the figure; the second is an
(O) or ‘out’ endpoint.
MxPS specifies the maximum packet size, i.e., the data size that can be transferred in a single go. Again, as expected, for the interrupt endpoint, it is
2 (<=8), and
64 for the bulk endpoints.
Ivl specifies the interval in milliseconds to be given between two consecutive data packet transfers for proper transfer, and is more significant for the interrupt endpoints.
Decoding a USB device section
As we have just discussed regarding the
E: line, it is the right time to decode the relevant fields of others as well. In short, these lines in a USB device section give a complete overview of the device as per the USB specifications, as discussed in our previous article.
Refer back to Figure 1. The first letter of the first line of every device section is a
T, indicating the position of the device in the USB tree, uniquely identified by the triplet
<usb bus number, usb tree level, usb port>.
D represents the device descriptor, containing at least the device version, device class/category, and the number of configurations available for this device.
There would be as many
C lines as the number of configurations, though typically, it is one.
C, the configuration descriptor, contains its index, the device attributes in this configuration, the maximum power (actually, current) the device would draw in this configuration, and the number of interfaces under this configuration.
Depending on this, there would be at least that many
I lines. There could be more in case of an interface having alternates, i.e., the same interface number but with different properties — a typical scenario for Web-cams.
I represents the interface descriptor with its index, alternate number, the functionality class/category of this interface, the driver associated with this interface, and the number of endpoints under this interface.
The interface class may or may not be the same as that of the device class. And
depending on the number of endpoints, there would be as many
E lines, details of which have already been discussed earlier.
* after the
I represents the currently active configuration and interface, respectively. The
P line provides the vendor ID, product ID, and the product revision.
S lines are string descriptors showing up some vendor-specific descriptive information about the device.
“Peeping into cat
/proc/bus/usb/devices is good in order to figure out whether a device has been detected or not, and possibly to get the first-cut overview of the device. But most probably this information would be required to write the driver for the device as well. So, is there a way to access it using
C code?” Shweta asked.
“Yes, definitely; that’s what I am going to tell you about, next. Do you remember that as soon as a USB device is plugged into the system, the USB host controller driver populates its information into the generic USB core layer? To be precise, it puts that into a set of structures embedded into one another, exactly as per the USB specifications,” Pugs replied.
The following are the exact data structures defined in
<linux/usb.h>, ordered here in reverse, for flow clarity:
So, with access to the
struct usb_device handle for a specific device, all the USB-specific information about the device can be decoded, as shown through the
/proc window. But how does one get the device handle?
In fact, the device handle is not available directly in a driver; rather, the per-interface handles (pointers to
struct usb_interface) are available, as USB drivers are written for device interfaces rather than the device as a whole.
Recall that the probe and disconnect callbacks, which are invoked by the USB core for every interface of the registered device, have the corresponding interface handle as their first parameter. Refer to the prototypes below:
So, with the interface pointer, all information about the corresponding interface can be accessed — and to get the container device handle, the following macro comes to the rescue:
Adding this new learning into last month’s registration-only driver gets the following code listing (
Then, the usual steps for any Linux device driver may be repeated, along with the pen drive steps:
- Build the driver (
pen_info.kofile) by running make.
- Load the driver using
- Plug in the pen drive (after making sure that the usb-storage driver is not already loaded).
- Unplug the pen drive.
- Check the output of
dmesgfor the logs.
- Unload the driver using
Figure 2 shows a snippet of the above steps on Pugs’ system. Remember to ensure (in the output of
cat /proc/bus/usb/devices) that the usual
usb-storage driver is not the one associated with the pen drive interface, but rather the
Figure 2: Output of dmesg
Before taking another break, Pugs shared two of the many mechanisms for a driver to specify its device to the USB core, using the
struct usb_device_id table. The first one is by specifying the
<vendor id, product id> pair using the
USB_DEVICE() macro (as done above). The second one is by specifying the device class/category using the
USB_DEVICE_INFO() macro. In fact, many more macros are available in
<linux/usb.h> for various combinations. Moreover, multiple of these macros could be specified in the
usb_device_id table (terminated by a null entry), for matching with any one of the criteria, enabling to write a single driver for possibly many devices.
“Earlier, you mentioned writing multiple drivers for a single device, as well. Basically, how do we selectively register or not register a particular interface of a USB device?”, queried Shweta. “Sure. That’s next in line of our discussion, along with the ultimate task in any device driver — the data-transfer mechanisms,” replied Pugs.