Saturday, March 26, 2011

A brief introduction to Linux USB drivers (1)

USB devices are very popular these days, and I have been curious about USB drivers for a long time. Yesterday, finally, I read the book "Essential Linux Device Drivers", and here is a brief summary about the USB chapter on that book. Moreover, since I am more familiar with PCI devices, I will compare USB with PCI from time to time. The purpose of this article is to help me remember what I just read, and allow me to go back to review it if I meet USB again after a long time.

Unlike PCI, which has only one type of connector, USB has many connector types: type A, B, micro and OTG (on the go). Following are from wiki:

"The Standard-A type of USB plug is a flattened rectangle which inserts into a "downstream-port" receptacle on the USB host, or a hub, and carries both power and data. This plug is frequently seen on cables that are permanently attached to a device, such as one connecting a keyboard or mouse to the computer.

A Standard-B plug—which has a square shape with beveled exterior corners—typically plugs into an "upstream receptacle" on a device that uses a removable cable, e.g. a printer. "

USB mini and micor are for smaller devices, such as PDA.

USB OTG can be used as either host side or device side. In other words, it can be use as both type A and type B connectors.

In addition, there are also many proprietary connectors based on USB, such as those on cell phones. It is very interesting that USB has so many different connectors. I guess one reason is that it has to support many different devices with various sizes. This phenomenon also indicates that USB is a very successful standard.

A device driver for USB on Linux is similar to a PCI driver in some sense. You also need to register the device, allocate memory, fill out many data structures. One difference is that each USB device has an endpoint address in a private name space.

Another difference is that USB uses URB (USB request block) to configure and access the hardware. This is different from PCI, which you can directly read and write to the devices via IO port or IO memory.

USB devices support many classes, such as mass storage and HID (human interactive devices, e.g. keyboard, mouse). For mass storage, it uses SCSI commands to transfer data, which is also a surprise to me.

No comments: