Date post: | 18-Jan-2015 |
Category: |
Technology |
Upload: | tom-sun |
View: | 1,270 times |
Download: | 0 times |
1
Copyright © 2009 by Tom J.P.Sun All rights reserved.
1
Linux USB to Ethernet development
spec. overviewkernel backenddriver interfaces
2
Copyright © 2009 by Tom J.P.Sun All rights reserved.
2
USB Overview(1)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
USB system
USB Bus interface
function
USB device
USB Bus interface
Logical connection
Physicalconnection
S/W
H/W
USB cable
3
Copyright © 2009 by Tom J.P.Sun All rights reserved.
3
USB Overview(2)USB Overview(2)
client
USB system
Usbcore (bus driver)
HC driver
HC H/W SIEUSB device H/W
USB device F/W
USB bus
UHCI or OHCI
4
Copyright © 2009 by Tom J.P.Sun All rights reserved.
4
USB communication flowUSB communication flow
Client S/W, driver
buffers
pipes
End points
Communicationflow
Interface
USB logical device
5
Copyright © 2009 by Tom J.P.Sun All rights reserved.
5
USB transfer typesUSB transfer types
Control Transferdevice enumeration use this type ( issued by USBD (bus driver))client driver can use it ( same concept as “IOCTL”) for “device specific commands”
Bulk Transferreliable, but not realtime scanner,printer
Isochronous Transfernear realtime, but not reliable usbcam
Interrupt Transferreliable, realtime, but limit in bandwidth mouse
Important ConceptUSB is MasterSlave modelUSBOnTheGo is peertopeer model
cncpt
6
Copyright © 2009 by Tom J.P.Sun All rights reserved.
6
USB Descriptor HierarchyUSB Descriptor Hierarchy
device descriptor
Configuration1
Configuration2
Interface 0AS0
Interface 1AS0
Interface 1AS1
Interface 0AS1
Interface 1AS0
Endpoint1
Endpoint2
Endpoint3
More endpoint descriptors
7
Copyright © 2009 by Tom J.P.Sun All rights reserved.
7
USB device requestUSB device request
8
Copyright © 2009 by Tom J.P.Sun All rights reserved.
8
USB standard commandsUSB standard commands(bRequest value)(bRequest value)
GET_STATUS 0CLEAR_FEATURE 1Reserved for future use 2SET_FEATURE 3Reserved for future use 4SET_ADDRESS 5GET_DESCRIPTOR 6SET_DESCRIPTOR 7GET_CONFIGURATION 8SET_CONFIGURATION 9GET_INTERFACE 10SET_INTERFACE 11SYNCH_FRAME 12
9
Copyright © 2009 by Tom J.P.Sun All rights reserved.
9
9620 vendor commands9620 vendor commands(bRequest value)(bRequest value)
DM_READ_REGS 0DM_WRITE_REGS 1DM_READ_MEMS 2DM_WRITE_REG 3DM_WRITE_MEMS 5DM_WRITE_MEM 7
10
Copyright © 2009 by Tom J.P.Sun All rights reserved.
10
Configure DeviceConfigure Device
When USB Device Plugged in, USBD + driver should complete the device enumeration steps
Host Device
1 get dev descriptor
dev descriptor
2 set address
3 get dev descriptor
4 set cfg
1 2
3 4
done by USBD
done by client
5 set itf
dev descriptor
5CATC can see these transfers...
11
Copyright © 2009 by Tom J.P.Sun All rights reserved.
11
USB Core API LayersUSB Core API Layers
USB device driver
USB device driver
USB device driver
USBD = USB core ( usb.c )
USB UHCI driver
USB OHCI driver
other USB HC driver
upperAPI
lowerAPI
ready
12
Copyright © 2009 by Tom J.P.Sun All rights reserved.
12
USB
spec. overviewkernel backenddriver interfaces
13
Copyright © 2009 by Tom J.P.Sun All rights reserved.
13
USB driver structure(1)USB driver structure(1)
@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
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;
};
14
Copyright © 2009 by Tom J.P.Sun All rights reserved.
14
USB driver structure(2)USB driver structure(2)
usb_driver
USBD
usb_register( )
my_probe( )…my_disconnect( )…
First enter point on device match
On device detachment, USB framework call this.
id_table : match list1st match dev2nd match dev…
On device attachment, USB framework walk through the id_table
15
Copyright © 2009 by Tom J.P.Sun All rights reserved.
15
Standard CommandsStandard 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
16
Copyright © 2009 by Tom J.P.Sun All rights reserved.
16
The USB data pathThe USB data path
PCIdriver
App
Linux O.S. Interface
App
Network(TCP/IP)
USB subsystem
FS
InKernel
PCI subsystem
EtherNetChip
H/W
USBdriver
EtherNetChip
PCI bus
USBHC
App open socket,data transferred byNetwork and USB driver
App open the usb device interface, direct transferdata to the USB HC
Can isolate driver, when proving h/w
17
Copyright © 2009 by Tom J.P.Sun All rights reserved.
17
The usbnet minidriverThe usbnet minidriver
Network(TCP/IP)
usbnetdriver
framework
asix
dm9601
...other client
drivers
The mini-driver interface...tx_fixup( )rx_fixup( )...
The mini-driver only take simple work for device-specific fixups.
The usbnet module manages:
1. skb flow control with the Network layer2. urb flow control with the USB bus driver
so, the performance issue should be little between each mini-driver, (Yes ! performance depends major on the H/W)
18
Copyright © 2009 by Tom J.P.Sun All rights reserved.
18
USB
spec. overviewkernel backenddriver interfaces
19
Copyright © 2009 by Tom J.P.Sun All rights reserved.
19
dm9601_rx_fixup( )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
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,
};
Mini-driver interface
20
Copyright © 2009 by Tom J.P.Sun All rights reserved.
20
dm9601_tx_fixup( )dm9601_tx_fixup( )
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,
};
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
21
Copyright © 2009 by Tom J.P.Sun All rights reserved.
21
vendor commands(1)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);}
22
Copyright © 2009 by Tom J.P.Sun All rights reserved.
22
vendor commands (2)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 , whilethe real data are transferred by b-in , b-out pipes, not the control pipe.
23
Copyright © 2009 by Tom J.P.Sun All rights reserved.
23
Device ID & Vendor ID Device ID & Vendor ID
use Use this information to
load driver when plug-in...
24
Copyright © 2009 by Tom J.P.Sun All rights reserved.
24
Appendix : libusbAppendix : 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...)
25
Copyright © 2009 by Tom J.P.Sun All rights reserved.
25
Example usage of libusb (1)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();
26
Copyright © 2009 by Tom J.P.Sun All rights reserved.
26
Example usage of libusb (2)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; } }
27
Copyright © 2009 by Tom J.P.Sun All rights reserved.
27
Example usage of libusb (3)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);
28
Copyright © 2009 by Tom J.P.Sun All rights reserved.
28
Example usage of libusb (4)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.cint usb_control_msg(
usb_dev_handle *dev, int requesttype, int request,int value, int index, char *bytes, int size, int timeout);
29
Copyright © 2009 by Tom J.P.Sun All rights reserved.
29
The EndThe End