More Related Content
Similar to Linux usb2ether
Similar to Linux usb2ether (20)
Linux usb2ether
- 1. 1
Linux USB to
Ethernet development
spec. overview
kernel backend
driver interfaces
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 2. 2
USB Overview(1)
USB 1.1/2.0 Spec. For S/W engineer:
Chap 5 “USB Data Flow Model”
Chap 9 “USB Device Framework”
Chap 10 “USB Host : H/W and S/W”
client function
S/W Logical
connection
USB USB
system device
Physical
H/W USB Bus USB Bus connection
interface interface
USB cable
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 3. 3
USB Overview(2)
client
USB system
Usbcore (bus driver)
HC driver
USB device F/W
UHCI or OHCI
USB device H/W
USB bus
HC H/W SIE
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 4. 4
USB communication flow
Communication
Client S/W, driver
flow
buffers
pipes
Interface
End points
USB logical device
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 5. 5
USB transfer types
Control Transfer
device enumeration use this type ( issued by USBD
(bus driver))
client driver can use it ( same concept as “IOCTL”)
for “device specific commands”
Bulk Transfer
reliable, but not realtime scanner,printer
Isochronous Transfer
near realtime, but not reliable usbcam
Interrupt Transfer
reliable, realtime, but limit in bandwidth mouse
Important Concept
cncpt
USB is MasterSlave model
USBOnTheGo is peertopeer model
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 6. 6
USB Descriptor Hierarchy
device descriptor
Configuration
Configuration
2
1
Interface 0 Interface 1 Interface 1 Interface 0 Interface 1
AS0 AS0 AS1 AS1 AS0
Endpoint Endpoint Endpoint More endpoint
1 2 3 descriptors
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 7. 7
USB device request
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 8. 8
USB standard commands
(bRequest value)
GET_STATUS 0
CLEAR_FEATURE 1
Reserved for future use 2
SET_FEATURE 3
Reserved for future use 4
SET_ADDRESS 5
GET_DESCRIPTOR 6
SET_DESCRIPTOR 7
GET_CONFIGURATION 8
SET_CONFIGURATION 9
GET_INTERFACE 10
SET_INTERFACE 11
SYNCH_FRAME 12
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 9. 9
9620 vendor commands
(bRequest value)
DM_READ_REGS 0
DM_WRITE_REGS 1
DM_READ_MEMS 2
DM_WRITE_REG 3
DM_WRITE_MEMS 5
DM_WRITE_MEM 7
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 10. 10
Configure Device
When USB Device Plugged in, USBD + driver
should complete the device enumeration steps
1 get dev descriptor
dev descriptor
2 set address
3 get dev descriptor
Host dev descriptor Device
4 set cfg
5 set itf
1 2 done by USBD
CATC can see these transfers...
3 4 5 done by client
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 11. 11
USB Core API Layers
USB device USB device USB device
driver driver driver
upper
API
USBD = USB core ( usb.c )
lower
API
USB UHCI USB OHCI other USB
driver driver HC driver
ready
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 12. 12
USB
spec. overview
kernel backend
driver interfaces
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 13. 13
USB driver structure(1)
struct usb_driver {
const char *name;
void* (*probe) (
struct usb_interface *
const struct usb_device_id *id_table);
void (*disconnect)
(struct usb_device *, void *);
...
const struct usb_device_id *id_table;
struct device_driver driver;
};
@name: module name
@probe: called to see if the driver will handle this USB interface , if
it is, return zero, otherwise return a negative error code.
@disconnect: called when the interface is no longer accessible.
@id_table: USB use this table to support hotplugging, export this
id_table with MODULE_DEVICE_TABLE( ) macro.
@driver: driver modle core driver structure
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 14. 14
USB driver structure(2)
On device attachment,
USB framework walk USBD
through the id_table
usb_register( )
First enter point on
device match
usb_driver
my_probe( )
…
my_disconnect( )
…
id_table : match list
1st match dev
2nd match dev
… On device detachment,
USB framework call this.
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 15. 15
Standard Commands
USBD provides USB standard
commands
usb_set_configuration( struct usb_device* dev, int cfg);
usb_set_interface(struct usb_device*, int itf, int alt);
usb_get_descriptor(…);
usb_get_string(…);
usb_get_status(…);
kenrel_source/drivers/usb/core/message.c
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 16. 16
The USB data path
Can isolate driver,
when proving h/w
App App
Linux O.S. Interface
Network
FS
(TCP/IP)
App open the usb device
interface, direct transfer
data to the USB HC
In PCI USB
Kernel subsystem subsystem App open socket,
data transferred by
Network and USB
PCI USB driver
driver driver
H/W
EtherNet PCI USB EtherNet
Chip bus HC Chip
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 17. 17
The usbnet minidriver
The mini-driver interface
...
Network tx_fixup( )
(TCP/IP) rx_fixup( )
... asix
usbnet dm9601
driver
framework
...other
The usbnet module manages:
client
drivers
1. skb flow control with the Network layer
2. urb flow control with the USB bus driver
The mini-driver only take simple work
for device-specific fixups.
so, the performance issue should be little
between each mini-driver, (Yes !
performance depends major on the H/W)
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 18. 18
USB
spec. overview
kernel backend
driver interfaces
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 19. 19
dm9601_rx_fixup( )
Object: correct the contents of skb ( data for
the network layer ), which is device specific.
e.g. dm9620 h/w give us data with length in
the first 2 bytes, rx_fixup( ) should remove it
before passing to the network layer
Mini-driver interface
static const struct driver_info dm9601_info = {
.description = "Davicom DM9601 USB Ethernet",
.flags = FLAG_ETHER,
.bind = dm9601_bind,
.rx_fixup = dm9601_rx_fixup,
.tx_fixup = dm9601_tx_fixup,
.status = dm9601_status,
.link_reset = dm9601_link_reset,
.reset = dm9601_link_reset,
};
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 20. 20
dm9601_tx_fixup( )
Object: correct the contents of skb ( data for
the network layer ), which is device specific.
e.g. dm9620 h/w need data with length in
the first 2 bytes, tx_fixup( ) should add it
before passing to the chip
static const struct driver_info dm9601_info = {
.description = "Davicom DM9601 USB Ethernet",
.flags = FLAG_ETHER,
.bind = dm9601_bind,
.rx_fixup = dm9601_rx_fixup,
.tx_fixup = dm9601_tx_fixup,
.status = dm9601_status,
.link_reset = dm9601_link_reset,
.reset = dm9601_link_reset,
};
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 21. 21
vendor commands(1)
static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
{
devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
return usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
DM_READ_REGS,
USB_DIR_IN | USB_TYPE_VENDOR |
USB_RECIP_DEVICE,
0, reg, data, length,
USB_CTRL_SET_TIMEOUT);
}
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 22. 22
vendor commands (2)
static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
{
return usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
DM_WRITE_REGS,
USB_DIR_OUT | USB_TYPE_VENDOR |
USB_RECIP_DEVICE,
0, reg, data, length,
USB_CTRL_SET_TIMEOUT);
}
Concept: The vendor commands are used to setup h/w , while
the real data are transferred by b-in , b-out pipes,
not the control pipe.
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 23. 23
Device ID & Vendor ID
u
s
e Use this information to
load driver when plug-in...
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 24. 24
Appendix : libusb
Bypass driver, direct call via the
USB subsystem.
Package 0.1.12 (simple, but not well
support thread )
we need header files:
#> apt-get src libusb-dev
(get libusb-0.1.12 under the same directory)
Makefile:
usbtest: usbtest.c
gcc -g -Wall $< -o $@ -I./libusb-0.1.12 -lusb
( assume usbtest.c is the our test program using the
libusb , use root to test...)
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 25. 25
Example usage of libusb (1)
Include headers:
#include "linux.h"
#include "usbi.h"
Initial setup provided by libusb
usb_init();
usb_find_busses();
usb_find_devices();
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 26. 26
Example usage of libusb (2)
Get our device:
for (usb_bus = usb_busses;
usb_bus;
usb_bus = usb_bus->next) {
for (dev = usb_bus->devices;
dev;
dev = dev->next) {
if ((dev->descriptor.idVendor
== VENDOR_ID) &&
(dev->descriptor.idProduct
== PRODUCT_ID))
return dev;
}
}
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 27. 27
Example usage of libusb (3)
Open:
usb_handle = usb_open(usb_dev);
B-out:
mk_raw(buf, SZBUF); /* prepare data in buf*/
retval = usb_bulk_write(usb_handle, ep_bulk_out,
(char*)buf, SZBUF, 5000);
B-in:
memset(buf, 0, SZBUF);
retval = usb_bulk_read(usb_handle, ep_bulk_in,
(char*)buf, SZBUF, 5000);
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 28. 28
Example usage of libusb (4)
Control pipe:
usb_control_msg(handle, 0x000000c0, DM_READ_REGS,
0,0xff & reg,data,length,5000);
prototype: libusb-0.1.12/linux.c
int usb_control_msg(
usb_dev_handle *dev,
int requesttype,
int request,
int value,
int index,
char *bytes,
int size,
int timeout);
Copyright © 2009 by Tom J.P.Sun All rights reserved.
- 29. 29
The End
Copyright © 2009 by Tom J.P.Sun All rights reserved.