Skip to main content

USB file system API for Linux

Project description

USB file system abstraction API

This module provides a USB file system abstraction API which can be used to gain information of the physical USB bus structure on a Linux System.

The USB file system (Taken from http://www.linux-usb.org/FAQ.html)

# ls  /sys/bus/usb/devices/
1-0:1.0      1-1.3        1-1.3.1:1.0  1-1:1.0
1-1          1-1.3.1      1-1.3:1.0    usb1

The names that begin with “usb” refer to USB controllers. More accurately, they refer to the “root hub” associated with each controller. The number is the USB bus number. In the example there is only one controller, so its bus is number 1. Hence the name “usb1”.

“1-0:1.0” is a special case. It refers to the root hub’s interface. This acts just like the interface in an actual hub an almost every respect; see below.

All the other entries refer to genuine USB devices and their interfaces. The devices are named by a scheme like this:

bus-port.port.port ...

In other words, the name starts with the bus number followed by a ‘-’. Then comes the sequence of port numbers for each of the intermediate hubs along the path to the device.

For example, “1-1” is a device plugged into bus 1, port 1. It happens to be a hub, and “1-1.3” is the device plugged into port 3 of that hub. That device is another hub, and “1-1.3.1” is the device plugged into its port 1.

The interfaces are indicated by suffixes having this form:

:config.interface

That is, a ‘:’ followed by the configuration number followed by ‘.’ followed by the interface number. In the above example, each of the devices is using configuration 1 and this configuration has only a single interface, number 0. So the interfaces show up as:

1-1:1.0        1-1.3:1.0        1-1.3.1:1.0

A hub will never have more than a single interface; that’s part of the USB spec. But other devices can and do have multiple interfaces (and sometimes multiple configurations). Each interface gets its own entry in sysfs and can have its own driver.

Usage

The API consists of a USB root object, from which all children can be accessed like python container types.

>>> from usbid import USB
>>> usb = USB()
>>> usb
<usbid.fs.USB [/sys/bus/usb/devices] at ...>

>>> usb.keys()
['1', '2']

Get a specific bus.

>>> bus = usb['1']
>>> bus
<usbid.fs.Bus [usb1] at ...>

Get port from bus.

>>> port = bus['1']
>>> port
<usbid.fs.Port [1-1] at ...>

Get interface from port.

>>> port.interfaces
[<usbid.fs.Interface [1-1:1.0] at ...>]

Interfaces might have tty associated.

>>> port.interfaces[0].tty
'ttyUSB0'

It’s not a good idea to refer to a USB interface by its tty mount name. But it’s a good idea to remember the file system name for unique identification, lookup interface by this name and then connect to corresponding tty.

>>> interface = usb.get_interface('1-1:1.0')
>>> interface.tty
'ttyUSB0'

For debugging you can print the USB structure.

>>> usb.printtree()
<usbid.fs.USB [/sys/bus/usb/devices] at ...>
  <usbid.fs.Bus [usb1] at ...>
      - Linux 3.13.0-48-generic xhci_hcd
      - xHCI Host Controller
    <usbid.fs.Interface [1-0:1.0] at ...>
    <usbid.fs.Port [1-1] at ...>
        - FTDI
        - FT232R USB UART
      <usbid.fs.Interface [1-1:1.0] at ...>
        - ttyUSB0

Source Code

The sources are in a GIT DVCS with its main branches at github.

Contributors

Changelog

2.2 (2023-05-15)

  • Officially support Python 3.7 - 3.11. [rnix]

  • CI Integration. [rnix]

  • Convert doctests to unittests. [rnix]

2.1 (2022-07-18)

  • Python 3 Support [rnix, 2022-07-17]

2.0 (2015-04-09)

  • Complete rewrite. [rnix, 2015-04-08]

1.0.3 (2014-03-03)

  • concrete ttyXXX device might live in tty subdirectory. [rnix, 2014-02-26]

1.0.2 (2014-02-26)

  • Rename usbid.device.devicelist to usbid.device.device_list. B/C import available. [rnix, 2014-02-26]

  • Introduce usbid.device.device_by_path utility. [rnix, 2014-02-26]

  • use latest usb.id file version 2014.02.03 from http://www.linux-usb.org/usb.ids [jensens, 2014-02-20]

1.0.1 (2014-02-20)

  • fix device node hierarchy [rnix, 2014-02-17]

  • housekeeping, fix tests [rnix, 2014-02-17]

1.0 (2013-12-19)

  • initial work [benniboy, jensens, 2013-12-19]

License

Copyright (c) 2013-2023, BlueDynamics Alliance, Austria, Germany, Switzerland All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the BlueDynamics Alliance nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY BlueDynamics Alliance AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BlueDynamics Alliance BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

usbid-2.2.tar.gz (429.0 kB view hashes)

Uploaded Source

Built Distribution

usbid-2.2-py3-none-any.whl (425.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page