Details on USB- no luck so far
Asked Answered
M

2

10

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!

Miceli answered 16/7, 2013 at 14:36 Comment(1)
@phileaton Thanks for the help! However, I would really like to know what goes on behind the scenes.Miceli
R
25

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!

Rubeola answered 16/7, 2013 at 14:39 Comment(5)
Wow phileaton... this is a great answer. It will get me started, and thank you for helping me to clear up my question!Miceli
2018 update: Libusbx is merged back into libusb-1.0 which is now the only maintained version of this library I believe. libusb.info is the only official website at the moment. Seems like libusb-1.0 is the go-to library for USB programming.Fairlie
Libusbx is no longer maintained.You should use libusb instead.Leinster
there is also hiddev github.com/torvalds/linux/blob/master/Documentation/hid/…Musick
all links are brokenDrenthe
T
0

This website has a general overview of how USB devices work: https://www.beyondlogic.org/usbnutshell/usb1.shtml

Particular sections give answers to things from the list of things you don't know yet about USB.

E.g. to find out how USB devices identify themselves, read about USB descriptors: https://www.beyondlogic.org/usbnutshell/usb5.shtml#DeviceDescriptors

To learn how a C/C++ program can talk to a USB device, see examples on using the libusb library: https://github.com/libusb/libusb/tree/master/examples

To learn how USB drivers work, see a tutorial from Bootlin: https://bootlin.com/blog/usb-slides/

Toothlike answered 5/2, 2022 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.