Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Details on USB- no luck so far

Tags:

usb

I've been looking for a detailed description for how USB protocol and cabling works for a long time with no luck. I am looking for a detailed yet not overcomplicated explanation of how things work on the software and hardware side of USB. Links and explanations would be appreciated. I've really run out of ideas, so it would be great if you can help me out.


This is what I do know:

USB hardware carries 4 lines- 5V power, ground, and 2 full duplex lines.

When connecting, the device can ask for a specified amount of current.

The transfer speeds for USB are quite fast compared to traditional serial connections.

When connecting, a device will output descriptors to the host describing itself. These descriptors will also be used for data.


What I don't know:

How does a program in C/C++ write directly to a USB port? Does it write to an address in the port?

How do some devices describe themselves as HID?

How do drivers work?

Everything else...


Thank you!

like image 744
Blue Ice Avatar asked Jul 16 '13 14:07

Blue Ice


1 Answers

Identification

Every device has a (unique) Vendor and Product ID. These are provided (sold) by usb.org to identify a device. You can use a library like libusbx to enumerate all connected devices and select the one with the Vendor and Product ID you are looking for.

HID Descriptors

The point of HID descriptors is actually to do away with drivers. HID descriptors are a universal way of describing your device so you don't need to waste time on a driver for every system/architecture/etc/. (Same concept as the JVM.)

Reports

You will use either the input, output, or feature reports to read or write to your device. You send a stream to your device on the input or feature report. This is typically 8 bytes I believe. Only one of which is a single character you wish to write. The HID descriptor contains all the information you need to put together a report. Although I'm struggling to find a related link to clarify this.

Potential Libraries

In an effort to be open-minded here are all the libraries I am familiar with and some info about them.

libusb-0.1

First off is libusb-0.1. This used to be the go to and was built in to many Linux kernels and Windows I believe. It is very easy to use and there is a lot of documentation. However, the owner never updated and it wasn't edited for many years. It supports only synchronous transfers. (If an error occurs, the program can wait infinitely while it expects a transfer.)

libusbx

Next is libusbx. This is what most people would suggest today and I agree. It was published by those frustrated by the owner of libusb-0.1. The code is much more lightweight, up-to-date, and importantly does not require root privileges like libusb-0.1 and libusb-1.0 (Discussed in a second). It supports synchronous or asynchronous transfers.

libusb-1.0

Then there is libusb-1.0. This was the first update to libusb-0.1 in some number of years. It is not compatible with libusb-0.1. This was published the same day as libusbx as a retaliation (I assume) and an attempt to rectify the lack of updated content and conserve a user-base. It supports synchronous or asynchronous transfers.

hid.h

Finally, there is the hid library. This was built on top of libusb as another layer of abstraction. But honestly, I think it's just really confusing and it just adds more overhead than necessary.

Some Good Resources

Understanding HID Descriptors

Control Message Transfer Documentation (Very Good Link IMO)

Rolling Your Own HID Descriptor

Good Visual of HID Reports for Transfers

Great List of bmRequestType constants (You will need this or similar)

A simple terminal app for speaking with DigiSpark using libusbx and libusb-0.1

I know this isn't exactly what you are looking for, but maybe it will get you started!

like image 121
eatonphil Avatar answered Oct 20 '22 11:10

eatonphil