Understanding libusb

July 13, 2010

How data can be transferred on the bus.

To transfer data to or from a device, first a handle needs to be obtained to the device. libusb is a user-level library that may be used to implement client-side functionality for USB on Linux. libusb requires that the function namely usb_init() be called before any other function is called. To get to access the various devices on the bus, two functions may be called in sequence: usb_find_busses(), usb_find_devices() . Calling these functions leads to initialization of a global link list names bus (of type usb_bus). The number of elements in this link link is equal to the number of busses on the system. The structure usb_type has a field named devices , which in itself is a linked list of all devices (of type usb_device)on the bus.

After calling the two functions (usb_find_busses() and usb_find_devices()), to get a handle to the required device, all that needs to be done is iteration along the linked list of the devices on the file, to get access to each of the devices. On reaching the desired device, a handle to the device may be obtained by calling the usb_open() method, passing a pointer to the usb_device structure as a parameter.
Once the handle has been obtained, one may use the whole lot of libusb functions on the handle.

The range of functions broadly classify into two classes:

  • those for configuring the device: usb_set_configuration, usb_claim_interface
  • those for doing data transfer: usb_bulk_write,usb_bulk_read,usb_control_msg

Reference: http://www.cs.indiana.edu/~bpisupat/work/usb.html

Android USB

July 6, 2010

1) compile kernel with adb + mass storage gadget support

2) flash and connect USB cable to Host

dmesg looks like..

[11621.761441] usb 2-1: new high speed USB device using ehci_hcd and address 27
[11621.912445] usb 2-1: configuration #1 chosen from 1 choice
[11621.959123] Initializing USB Mass Storage driver…
[11621.959341] scsi6 : SCSI emulation for USB Mass Storage devices
[11621.959551] usbcore: registered new interface driver usb-storage
[11621.959555] USB Mass Storage support registered.
[11621.964209] usb-storage: device found at 27
[11621.964212] usb-storage: waiting for device to settle before scanning
[11626.961587] usb-storage: device scan complete
[11626.962259] scsi 6:0:0:0: Direct-Access     <NULL>   <NULL>           0000 PQ: 0 ANSI: 2
[11626.963870] sd 6:0:0:0: Attached scsi generic sg1 type 0
[11626.966200] sd 6:0:0:0: [sdb] Attached SCSI removable disk

# lsusb
Bus 002 Device 027: ID 18d1:0002

# lsusb -v -d

Bus 002 Device 027: ID 18d1:0002

Device Descriptor:
bLength                18
bDescriptorType         1
bcdUSB               2.00
bDeviceClass            0 (Defined at Interface level)
bDeviceSubClass         0
bDeviceProtocol         0
bMaxPacketSize0        64
idVendor           0x18d1
idProduct          0×0002
bcdDevice            2.16
iManufacturer           1
iProduct                2
iSerial                 3
bNumConfigurations      1

Configuration Descriptor:
bLength                 9
bDescriptorType         2
wTotalLength           55
bNumInterfaces          2
bConfigurationValue     1
iConfiguration          0
bmAttributes         0xe0 ( Self Powered, Remote Wakeup)
MaxPower                2mA

Interface Descriptor:
bLength                 9
bDescriptorType         4
bInterfaceNumber        0
bAlternateSetting       0
bNumEndpoints           2
bInterfaceClass         8 Mass Storage
bInterfaceSubClass      6 SCSI
bInterfaceProtocol     80 Bulk (Zip)
iInterface              0

Endpoint Descriptor:
bLength                 7
bDescriptorType         5
bEndpointAddress     0×81  EP 1 IN
bmAttributes            2
Transfer Type            Bulk
Synch Type               None
Usage Type               Data
wMaxPacketSize     0×0200  1x 512 bytes
bInterval               0

Endpoint Descriptor:
bLength                 7
bDescriptorType         5
bEndpointAddress     0×01  EP 1 OUT
bmAttributes            2
Transfer Type            Bulk
Synch Type               None
Usage Type               Data
wMaxPacketSize     0×0200  1x 512 bytes
bInterval               1

Interface Descriptor:
bLength                 9
bDescriptorType         4
bInterfaceNumber        1
bAlternateSetting       0
bNumEndpoints           2
bInterfaceClass       255 Vendor Specific Class
bInterfaceSubClass     66
bInterfaceProtocol      1
iInterface              0

Endpoint Descriptor:
bLength                 7
bDescriptorType         5
bEndpointAddress     0×82  EP 2 IN
bmAttributes            2
Transfer Type            Bulk
Synch Type               None
Usage Type               Data
wMaxPacketSize     0×0200  1x 512 bytes
bInterval               0

Endpoint Descriptor:
bLength                 7
bDescriptorType         5
bEndpointAddress     0×02  EP 2 OUT
bmAttributes            2
Transfer Type            Bulk
Synch Type               None
Usage Type               Data
wMaxPacketSize     0×0200  1x 512 bytes
bInterval               0

3) add a udev rule of your Host with following information
# vim /etc/udev/rules.d/51-android.rules

SUBSYSTEM==”usb”, SYSFS{idVendor}==”18d1″, MODE=”0666″

4) chmod 777 /etc/udev/rules.d/51-android.rules

5) Now disconnect and reconnect USB cable and run “adb devices”

# ./adb kill-server (may be need to kill adb server if was already running)

# ./adb devices
List of devices attached
0123456789ABCDEF    device

# ./adb shell

On device
=========

# ls -l /dev/android*
crw-rw—- adb      adb       10,  60 2010-07-01 07:20 android_adb
crw-rw—- adb      adb       10,  61 2010-07-01 07:20 android_adb_enable

adbd process running

# root      883   1     3364   156   ffffffff 0000f444 S /sbin/adbd

** Modifications for conencting with adb if using another USB Vendor ID.
========================================================================
1) Modify system/core/adb/usb_vendors.c as following.
+++ b/adb/usb_vendors.c
@@ -69,6 +69,7 @@
#define VENDOR_ID_PANTECH       0x10A9
// Qualcomm’s USB Vendor ID
#define VENDOR_ID_QUALCOMM      0x05c6
+#define VENDOR_ID_CORPORATION      0xabcd

/** built-in vendor list */
@@ -90,6 +91,7 @@ int builtInVendorIds[] = {
VENDOR_ID_KYOCERA,
VENDOR_ID_PANTECH,
VENDOR_ID_QUALCOMM,
+    VENDOR_ID_CORPORATION,
};

2) change udev rule(/etc/udev/rules.d/51-android.rules) as following,
SUBSYSTEM==”usb”, SYSFS{idVendor}==”abcd”, MODE=”0666″

then recompile android, and used recompiled “out/host/linux-x86/bin/adb” binary for getting adb shell.

# ./out/host/linux-x86/bin/adb devices

List of devices attached
0123456789ABCDEF    offline

Here, “0123456789ABCDEF” is the sring from usb descriptor “Serial Number” String descriptor as a part of “usb device descriptor”

Linux USB Serial

July 6, 2010

Reference: http://www.tldp.org/HOWTO/html_single/Serial-HOWTO/

Linux USB FAQ

July 6, 2010

Reference: http://www.linux-usb.org/FAQ.html

USB Audio Gadget Driver

July 6, 2010

Gadget  Audio

Gadget Audio implements the USB Audio class, appearing to the host as a soundcard.It receives the PCM stream from host,i.e, a PC, over usb and sends it to the audio codec on board to playback it.Also,it can perform the inverse job for record.

Source file drivers/usb/gadget/audio.c

Kernel config of Audio Gadget
Before enabling this feature,make sure that audio codec works well on your board.

[Linux Kernel Configuration] -→ [Device Drivers] -→ [USB support] -→ [USB Gadget Support]
<M> Support for USB Gadgets
<M>     Audio Gadget (EXPERIMENTAL)

Use BF527-EZKIT as a soundcard for linux Host
Here the CPU is BF527C, with a built-in audio codec named SSM2602. After system booting up, SSM2602 can be seen as follows:

Advanced Linux Sound Architecture Driver Version 1.0.20.
No device for DAI SSM2602
dma rx:3 tx:4, err irq:15, regs:ffc00800
ssm2602 Audio Codec 0.1
asoc: SSM2602 <-> bf5xx-i2s mapping ok
ALSA device list:
#0: bf5xx_ssm2602 (SSM2602)

On target side,You must load the gadget audio driver.
root:/> modprobe g_audio

If every thing is OK,then messages like follows will show up.
# g_audio gadget: Hardware params: access 3, format 2, channels 2, rate 48000
# g_audio gadget: audio_buf_size 48000, req_buf_size 200, req_count 256
# g_audio gadget: Linux USB Audio Gadget, version: Dec 18, 2008
# g_audio gadget: g_audio ready
# g_audio gadget: high speed config #1: Linux USB Audio Gadget

On the PC side

cliff@debiancliff:~$ cat /proc/asound/cards

0 [Intel          ]: HDA-Intel – HDA Intel
HDA Intel at 0xdfdfc000 irq 16
1 [Gadget         ]: USB-Audio – Linux USB Audio Gadget
Linux 2.6.31.6-ADI-2010R1-pre-svn7883 with musb_h Linux USB Audio Gadget at usb
here,audio gadget device is shown up as sound card 1.

Test Audio Gadget

playback music with audio gadget device(soundcard 1 as above)

cliff@debiancliff:~$ aplay -D plug:hw:1 a.wav
Playing WAVE ‘a.wav’ : Signed 16 bit Little Endian, Rate 22050 Hz, Stereo

Reference: http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:usb-gadget:audio

USB Testing – Omap devices

July 6, 2010

Reference’shttp://www.omappedia.org/wiki/USB_testing

http://processors.wiki.ti.com/index.php/UserGuideUsbDriver_PSP_03.00.00.04

USB Networking

December 18, 2009

USB/IP Project

USB/IP Project aims to develop a general USB device sharing system over IP network. To share USB devices between computers with their full functionality, USB/IP encapsulates “USB I/O messages” into TCP/IP payloads and transmits them between computers.

Reference’s

1. http://wiki.openmoko.org/wiki/USB_Networking

2. Setting us usbnet

3. http://www.handhelds.org/moin/moin.cgi/UsbNet

Document Reference’s

1. Networking handhelds over usb

2. USB networking whitepaper

3. Belcarra white paper for RNDIS & CDC

Understanding Linux USB architecture

October 2, 2009

Here I am trying to understand how linux usb architecture is, in terms of linux kernel source code. i.e. how every subsystem(audio, video, input etc.) for specific usb devices, hits with USB at the end…

USB – Physical

September 7, 2009

USB Physical layer and components description

USB 1.0 - Low speed1.5Mbps

USB 1.1 - Full Speed  -   12 Mbps

USB 2.0 - High Speed  – 480 Mbps

USB 3.0Supper Speed3.2 Gbps

Bus Topology

usb-figure1 Refer. Sec 4.1.1 – USB 2.0 spec

Due to timing constraints allowed for hub and cable propagation times, the maximum number of tiers allowed is seven (including the root tier). Note that in seven tiers[above fig. shows only 4] , five non-root hubs maximum can be supported in a communication path between the host and any device.

USB Host – There is only one host in any USB system. The USB interface to the host computer system is referred to as
the Host Controller. A root hub is integrated within the host system to provide one or more attachment points.

USB Devices - USB devices are one of the following

• Hubs - which provide additional attachment points to the USB
• Functions – which provide capabilities to the system. such as, pen drive, usb keyboard, mouse

Understanding sysfs and usb

September 1, 2009

Reference’s

1) http://www.makelinux.net/ldd3/chp-13-sect-2.shtml

2) Allen Stern’s description

shivdas@shivdas-laptop:/sys/bus/usb/devices# ls -al usb*
usb1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1
usb2 -> ../../../devices/pci0000:00/0000:00:1d.0/usb2
usb3 -> ../../../devices/pci0000:00/0000:00:1d.1/usb3
usb4 -> ../../../devices/pci0000:00/0000:00:1d.2/usb4
usb5 -> ../../../devices/pci0000:00/0000:00:1d.3/usb5

this shows that it has 5 root hubs. Now, lets try to corelate it with lsusb. A simple shell script which will print only RootHubs available on your machine.

#!/bin/sh
TOTAL_BUS_NUM=$1
for (( i=1; i <= $TOTAL_BUS_NUM; i++ ))
do
lsusb -s $i:1
done

Run it as “bash lsusb.sh”

shivdas@shivdas-laptop:~/USB$ bash lsusb.sh 5
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub


Follow

Get every new post delivered to your Inbox.