How USB Works

The Universal Serial Bus, more commonly known as USB, has become a cornerstone of modern computing, facilitating seamless connections between devices such as keyboards, mice, mobile devices, and more. Its popularity stems from its versatility, ease of use, and standardised design, which allows devices from different manufacturers to communicate efficiently. But how exactly does USB technology work? Beneath its seemingly simple plug-and-play exterior lies a sophisticated architecture.

The Architecture of USB

At its core, USB operates on an asymmetric architecture consisting of a host, downstream ports, and peripheral devices. The host, typically a computer, serves as the central controller, managing communication and data flow. It is equipped with one or more host controllers, and each host controller can manage multiple USB ports. Through these ports, peripheral devices such as flash drives, printers, and mobile devices can connect.

USB devices are often connected in a star topology, with hubs acting as central nodes. Every USB network has a root hub integrated into the host controller. Additional hubs can be added, effectively branching into a tree structure. However, the standard allows only up to five levels of hubs, limiting the depth of the tree.

Specialised hubs, known as sharing hubs, allow multiple computers to access the same USB device by manually or automatically switching control between systems. These hubs are commonly used in office environments where devices like printers or external storage need to be shared efficiently.

Device Functions and Endpoints

One of the fascinating aspects of USB devices is their ability to house multiple logical sub-devices or functions. For instance, a single webcam might also include a built-in microphone, effectively functioning as both a video and an audio device. Each function is managed independently, allowing the host to interact with each component seamlessly.

Communication between the host and a USB device happens through endpoints, which are logical channels residing on the device. These endpoints act as conduits for data transfer and are organised into pipes. A USB device can support up to 32 pipes: 16 for sending data to the host and 16 for receiving data from it.

Pipes are unidirectional, meaning each can either send or receive data, but not both. However, endpoint zero is an exception—it is used for device configuration and can handle both directions of data transfer. This endpoint is critical during the initial setup phase when a USB device is first connected.

The Enumeration Process

When a USB device is plugged into a host, a process known as enumeration begins. Enumeration ensures that the device is correctly recognised and configured by the host system. The process starts with the host sending a reset signal to the device. During this reset phase, the USB host determines the device's speed (USB 2.0, USB 3.0, etc.) and assigns it a unique 7-bit address.

Once the device is identified, the host reads its descriptors—small pieces of metadata containing information about the device's capabilities, manufacturer, and functions. If the host has the necessary drivers to support the device, they are loaded, and the device is moved to a configured state. Should the computer be restarted, this enumeration process is repeated for all connected USB devices.

Communication and Data Flow

Data transfer in USB communication is host-driven, which means that no USB device can send data unless explicitly requested by the host. The host controller periodically polls connected devices in a round-robin fashion, checking if they have data to send or receive. This polling ensures that bandwidth is allocated efficiently across multiple devices.

There are four primary modes of USB data transfer:

  • Control Transfers: Used for device configuration and command communication.
  • Bulk Transfers: Used for large data transfers, such as copying files to a USB drive.
  • Interrupt Transfers: Used for small, time-sensitive data packets, like keystrokes from a keyboard.
  • Isochronous Transfers: Used for real-time data streams, such as audio or video playback.

Each mode serves a specific purpose, and the host determines which transfer type to use based on the needs of the connected device.