Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the USB storage driver works in Linux?

I am trying to find out a high-level overview of how the USB storage driver works in Linux. I'm looking for a simple article or even a picture/flowchart describing how it works.

Basically, I'm looking to get these questions answered:

When you plug the device into your computer, what happens? Is there a daemon that picks up on it, or does the event trigger an interrupt somewhere? Does the core USB driver read information about the device before passing control over to the USB storage driver? How does it decide what type of device it is? How does the device get mounted, and what allows it to communicate with the computer's filesystem? When I copy a file, what does the data flow look like in the kernel?

I hope the question isn't too vague - I tried Google to no avail, so I'm wondering if anyone knows any articles or diagrams that can explain this, or perhaps if they can explain it themselves without too much effort. Thanks.

like image 295
Sefu Avatar asked Dec 04 '13 20:12

Sefu


People also ask

How does Linux USB driver work?

The Linux kernel supports two main types of USB drivers: drivers on a host system and drivers on a device. The USB drivers for a host system control the USB devices that are plugged into it, from the host's point of view (a common USB host is a desktop computer.)

How does a USB device driver work?

A USB client driver is the software installed on the computer that communicates with the hardware to make the device function. If the device belongs to a device class supported by Microsoft, Windows loads one of the Microsoft-provided USB drivers (in-box class drivers) for the device.

How does a flash drive register in a Linux system?

The first thing a Linux USB driver needs to do is register itself with the Linux USB subsystem, giving it some information about which devices the driver supports and which functions to call when a device supported by the driver is inserted or removed from the system.

What is USB storage driver?

The USB mass storage driver is used to communicate with mass storage devices such as flash drives, external hard drives, and other types of removable media connected through USB.


1 Answers

No, it is a very good question.

The block writing is going in linux with the block device layer. The filesystems are working with this block dev layer.

If this layer wants to write something out, says it to the driver of the usb master device. This driver is talking with the usb controller chip of the motherboard.

This chip is very simple: the usb is practically a serial port, with a lot of extensions, mainly targeting the autoconfiguration and the power management. But basically, you can write out bytes, and read in bytes.

Your questions:

When you plug the device into your computer, what happens? Is there a daemon that picks up on it, or does the event trigger an interrupt somewhere?

The device (usb slave) says the master (in the motherboard): "I am here". The usb controller chip gets the message and says it to the kernel (normally) with an interrupt. The kernel reinitializes and rescans the usb bus, and says the udev: "here is a new 1234:5678 usb device on the usb tree 1.3.5"

"How does it decide what type of device it is?"

Usb devices have a vendor and model id, and they can say this on ask. Google for "usb ids".

"How does the device get mounted, and what allows it to communicate with the computer's filesystem?"

The kernel only loads the driver and says the udev (which is in userspace): "Here is a new block device on device number 22:16". From this, udev tries to mount this with some userspace daemon, it is already distribution-dependant.

like image 81
peterh Avatar answered Oct 08 '22 06:10

peterh