41
USB ARCHITECTURE P97922006 彭彭彭

USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

Embed Size (px)

Citation preview

Page 1: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

USB ARCHITECTUREP97922006

彭治民

Page 2: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

OUTLINE

Introduction Electronic specification Protocol Linux’s driver

Page 3: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

INTRODUCTION

USB 由 2004 年釋出,採用主從架構方式與周邊裝置溝通,初期速度 12Mbit/s(USB 1.1) ,目前速度最高為 480Mbit/s(USB 2.0) 。

通常有一個主機端 (Host)& 伺服器端 (Slave) 。 USB 採用星狀拓墣方式與各個周邊裝置連接。 特點是,隨插即用,可以快速與周邊建立連接通道,

進而控制 USB 周邊裝置。 傳輸線距離約 1.2 Meter 。 Total device <127

Page 4: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

ELECTRONIC SPECIFICATION-CONNECTOR

A Type B Type Mini-B

Page 5: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

ELECTRONIC SPECIFICATION-USB DETECT

Item Speed Detecting Version

LOW SPEED 1.5Mbit/Sec R1 pull up USB 1.0/1.1

FULL SPEED 12Mbit /Sec R2 pull low USB 1.0/1.1

HIGH SPEED 480Mbit/Sec Auto detect

USB 2.0

PCUSB

Device

D-

D+

R R

R1

R2

VBUS

Page 6: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

ELECTRONIC SPECIFICATION-TRANSMISSION

Page 7: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

ELECTRONIC SPECIFICATION-HUB TOPOLOGY

Max Level => 7

Page 8: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- PACKET DESCRIPTOR

Packet Identifier(PID) Field

SYNC

PID Type SubType Descriptor

Token OUT,IN,SOF,SETUP Control

Data DATA0,DATA1,DATA2.. Send Data

Handshake ACK,NACK,STALL,NYET Respond

Special PRE,ERR,SPLIT,PING

Page 9: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- PACKET DESCRIPTOR

16 bit -CRC

DATA <=1024

Endpoint 0~15

Address 0~127

Page 10: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- TOKEN PACKET

SubType

PID<3:0>

Descriptor

OUT 0001B Address + endpoint number in host-to-function transaction

IN 1001B Address + endpoint number in function-to-host transaction

SOF 0101B Start-of-Frame marker and frame number

SETUP 1101B Address + endpoint number in host-to-function transaction for SETUP to a control pipe

Page 11: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- DATA PACKET

SubType

PID<3:0>

Descriptor

DATA1 0011B Data packet PID even

DATA2 1011B Data packet PID odd

DATA3 0111B Data packet PID high-speed, high bandwidth isochronous transaction in a microframe

MDATA 1111B Data packet PID high-speed for split and high bandwidth isochronous transactions

Page 12: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- HANDSHAKE PACKET

SubType

PID<3:0>

Descriptor

PRE 1100B (Token) Host-issued preamble. Enables downstream bus traffic to low-speed devices.

ERR 1100B (Handshake) Split Transaction Error Handshake (reuses PRE value)

SPLIT 1000B (Token) High-speed Split Transaction Token

PING 0100B (Token) High-speed flow control probe for a bulk/control endpoint

Page 13: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-FOUR TYPES TRANSMISSION

CONTROL BULK INTERRUPT ISOCHRONOUS

Page 14: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- CONTROL/BULK

Page 15: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- INTERRUPT

Page 16: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- ISOCHRONOUS

Page 17: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- FORMAT OF SETUP DATA(1/2)

Page 18: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- FORMAT OF SETUP DATA(2/2)

Page 19: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- GET DESCRIPTOR(1/2)

Page 20: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- GET DESCRIPTOR(2/2)

Page 21: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL- GET QUALIFIER

Page 22: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD CONFIGURATION DESCRIPTOR

Page 23: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD CONFIGURATION DESCRIPTOR

Page 24: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD INTERFACE DESCRIPTOR(1/3)

Page 25: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD INTERFACE DESCRIPTOR(2/3)

Page 26: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD INTERFACE DESCRIPTOR(3/3)

Page 27: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD ENDPOINT DESCRIPTOR(1/4)

Page 28: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD ENDPOINT DESCRIPTOR(2/4)

Page 29: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD ENDPOINT DESCRIPTOR(3/4)

Page 30: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

PROTOCOL-STANDARD ENDPOINT DESCRIPTOR(4/4)

Page 31: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

USB control flowPROTOCOL- USB HOST CONTROL FLOW

USB HOST

USB Hub

Addr 0Assign Addr 1

USB Device1 USB Device2

Addr 0

Addr 0

Assign Addr 2

Assign Addr 3

Page 32: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

USB control flow

USB HOST

USB Hub

USB Controller

USB Dev 127

USB Device ..

USB Device 1

USB Device 0

HID ClassCustom Device

Descriptor

Configure

interface

VID,PID 0VID,PID 0

VID,PID 0VID,PID 0subdrivers

VID,PID 0VID,PID 0Class ID

subdrivers

PROTOCOL- USB HOST CONTROL FLOW

Page 33: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLE

static int __init rt2870_init(void){int result;result = usb_register(&rt2870_driver);if(result)

return result;return 0;

}static void __exit rt2870_exit(void){

usb_deregister(&rt2870_driver);}module_init(rt2870_init);module_exit(rt2870_exit);MODULE_LICENSE("GPL");

static struct usb_driver rt2870_driver= {.name = “USBdriver”,.probe = rt2870_probe,.disconnect= rt2870_disconnect,.id_table = rt2870_table, //PID ,VID.supports_autosuspend=0,

};

Page 34: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLE

static struct usb_class_driver rt2870_class = {.name = "rt2870usb%d",.fops = &rt2870_fops,

};

static struct file_operations rt2870_fops = {.owner =THIS_MODULE,.read =rt2870_read,.write =rt2870_write,.open =rt2870_open,

.ioctl = rt2870_ioctl,.release= rt2870_release,

};

Page 35: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

static int rt2870_probe(struct usb_interface *interface, const struct usb_device_id *id){

struct rt2870_dev *dev;struct usb_host_interface *iface_desc;struct usb_endpoint_descriptor *endpoint;

dev = kzalloc(sizeof(*dev), GFP_KERNEL);

usb_set_intfdata(interface, dev); // set private parameter

iface_desc = interface->cur_altsetting; //Current USB status,Endpoint..

ret = usb_register_dev(interface, &rt2870_class);

dev->udev = usb_get_dev(interface_to_usbdev(interface));

return ret;}

LINUX’S DRIVER- MODULE EXAMPLE

Page 36: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLE

static int rt2870_open(struct inode *node, struct file *file){

struct usb_interface *interface;struct rt2870_dev *dev;

interface = usb_find_interface(&rt2870_driver, iminor(node));if(!interface)

return -ENODEV;

dev = usb_get_intfdata(interface);

file->private_data = dev;return 0;

}

Page 37: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLE

static int rt2870_release(struct inode *node, struct file *file){

struct rt2870_dev *dev;dev = (struct rt2870_dev*)(file->private_data);

//free memeoty… return 0;

}

static ssize_t rt2870_read(struct file *file, char __user *buf, size_t len, loff_t *loff){ size_t size; struct rt2870_dev *dev = file->private_data; … //Get Data from queue. return size;}

Page 38: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLE

static ssize_t rt2870_write(struct file *file, const char __user *buf, size_t len, loff_t *loff){ size_t size; struct rt2870_dev *dev = file->private_data; … Setting Data for send... … usb_control_msg(dev->udev); //Send Data return size;}

Page 39: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLECONTROL DATA SEND

ret = int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);Blocking access*dev :usb handleRequestype : Setup packetValue : Setup packetIndex : Setup packet

Bytes : Return data size : Number of buf size

ret = usb_submit_urb(dev->pUrbIn, GFP_ATOMIC);

Page 40: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLEBULK DATA SEND AND RECEIVER

int usb_bulk_msg (struct usb_device * usb_dev , struct urb *purb, unsigned int pipe,void * data ,int len , ,void *function,void *context);

usb_dev: usb handlepurb : urbpipe : endpointdata : Data in or outlen :data size*function :complete call back function.* context :private data

Int usb_submit_urb(dev->pUrb, GFP_ATOMIC);

dev->pUrb =usb_alloc_urb(0,GFP_ATOMIC); //Build Urb for usb packet

Page 41: USB A RCHITECTURE P97922006 彭治民. O UTLINE Introduction Electronic specification Protocol Linux’s driver

LINUX’S DRIVER- MODULE EXAMPLESEND COMPLIETEvoid BulkComplete(struct urb *pUrb, struct pt_regs *regs){ struct rt2870_dev *dev; dev=(struct rt2870_dev *)pUrb->context;

pUrb->actual_length; //Num of data sent

Call Next one

}