123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778 |
- #ifndef __USBDCORE_H__
- #define __USBDCORE_H__
- #include <common.h>
- #include "usbdescriptors.h"
- #define MAX_URBS_QUEUED 5
- #if 1
- #define usberr(fmt,args...) serial_printf("ERROR: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
- #else
- #define usberr(fmt,args...) do{}while(0)
- #endif
- #if 0
- #define usbdbg(fmt,args...) serial_printf("debug: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
- #else
- #define usbdbg(fmt,args...) do{}while(0)
- #endif
- #if 0
- #define usbinfo(fmt,args...) serial_printf("info: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
- #else
- #define usbinfo(fmt,args...) do{}while(0)
- #endif
- #ifndef le16_to_cpu
- #define le16_to_cpu(x) (x)
- #endif
- #ifndef inb
- #define inb(p) (*(volatile u8*)(p))
- #endif
- #ifndef outb
- #define outb(val,p) (*(volatile u8*)(p) = (val))
- #endif
- #ifndef inw
- #define inw(p) (*(volatile u16*)(p))
- #endif
- #ifndef outw
- #define outw(val,p) (*(volatile u16*)(p) = (val))
- #endif
- #ifndef inl
- #define inl(p) (*(volatile u32*)(p))
- #endif
- #ifndef outl
- #define outl(val,p) (*(volatile u32*)(p) = (val))
- #endif
- #ifndef insw
- #define insw(p,to,len) mmio_insw(p,to,len)
- #endif
- #ifndef outsw
- #define outsw(p,from,len) mmio_outsw(p,from,len)
- #endif
- #ifndef insb
- #define insb(p,to,len) mmio_insb(p,to,len)
- #endif
- #ifndef mmio_insw
- #define mmio_insw(r,b,l) ({ int __i ; \
- u16 *__b2; \
- __b2 = (u16 *) b; \
- for (__i = 0; __i < l; __i++) { \
- *(__b2 + __i) = inw(r); \
- }; \
- })
- #endif
- #ifndef mmio_outsw
- #define mmio_outsw(r,b,l) ({ int __i; \
- u16 *__b2; \
- __b2 = (u16 *) b; \
- for (__i = 0; __i < l; __i++) { \
- outw( *(__b2 + __i), r); \
- } \
- })
- #endif
- #ifndef mmio_insb
- #define mmio_insb(r,b,l) ({ int __i ; \
- u8 *__b2; \
- __b2 = (u8 *) b; \
- for (__i = 0; __i < l; __i++) { \
- *(__b2 + __i) = inb(r); \
- }; \
- })
- #endif
- #define _cv_(ptr) ((char*)(void*)(ptr))
- #define member_offset(type,memb) (_cv_(&(((type*)0)->memb))-(char*)0)
- #define p2surround(type,memb,ptr) ((type*)(void*)(_cv_(ptr)-member_offset(type,memb)))
- struct urb;
- struct usb_endpoint_instance;
- struct usb_interface_instance;
- struct usb_configuration_instance;
- struct usb_device_instance;
- struct usb_bus_instance;
- #define USB_CLASS_PER_INTERFACE 0
- #define USB_CLASS_AUDIO 1
- #define USB_CLASS_COMM 2
- #define USB_CLASS_HID 3
- #define USB_CLASS_PHYSICAL 5
- #define USB_CLASS_PRINTER 7
- #define USB_CLASS_MASS_STORAGE 8
- #define USB_CLASS_HUB 9
- #define USB_CLASS_DATA 10
- #define USB_CLASS_APP_SPEC 0xfe
- #define USB_CLASS_VENDOR_SPEC 0xff
- #define USB_TYPE_STANDARD (0x00 << 5)
- #define USB_TYPE_CLASS (0x01 << 5)
- #define USB_TYPE_VENDOR (0x02 << 5)
- #define USB_TYPE_RESERVED (0x03 << 5)
- #define USB_RECIP_DEVICE 0x00
- #define USB_RECIP_INTERFACE 0x01
- #define USB_RECIP_ENDPOINT 0x02
- #define USB_RECIP_OTHER 0x03
- #define USB_DIR_OUT 0
- #define USB_DIR_IN 0x80
- #define USB_DT_DEVICE 0x01
- #define USB_DT_CONFIG 0x02
- #define USB_DT_STRING 0x03
- #define USB_DT_INTERFACE 0x04
- #define USB_DT_ENDPOINT 0x05
- #if defined(CONFIG_USBD_HS)
- #define USB_DT_QUAL 0x06
- #endif
- #define USB_DT_HID (USB_TYPE_CLASS | 0x01)
- #define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
- #define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
- #define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
- #define USB_DT_DEVICE_SIZE 18
- #define USB_DT_CONFIG_SIZE 9
- #define USB_DT_INTERFACE_SIZE 9
- #define USB_DT_ENDPOINT_SIZE 7
- #define USB_DT_ENDPOINT_AUDIO_SIZE 9
- #define USB_DT_HUB_NONVAR_SIZE 7
- #define USB_DT_HID_SIZE 9
- #define USB_ENDPOINT_NUMBER_MASK 0x0f
- #define USB_ENDPOINT_DIR_MASK 0x80
- #define USB_ENDPOINT_XFERTYPE_MASK 0x03
- #define USB_ENDPOINT_XFER_CONTROL 0
- #define USB_ENDPOINT_XFER_ISOC 1
- #define USB_ENDPOINT_XFER_BULK 2
- #define USB_ENDPOINT_XFER_INT 3
- #define USB_PID_UNDEF_0 0xf0
- #define USB_PID_OUT 0xe1
- #define USB_PID_ACK 0xd2
- #define USB_PID_DATA0 0xc3
- #define USB_PID_PING 0xb4
- #define USB_PID_SOF 0xa5
- #define USB_PID_NYET 0x96
- #define USB_PID_DATA2 0x87
- #define USB_PID_SPLIT 0x78
- #define USB_PID_IN 0x69
- #define USB_PID_NAK 0x5a
- #define USB_PID_DATA1 0x4b
- #define USB_PID_PREAMBLE 0x3c
- #define USB_PID_ERR 0x3c
- #define USB_PID_SETUP 0x2d
- #define USB_PID_STALL 0x1e
- #define USB_PID_MDATA 0x0f
- #define USB_REQ_GET_STATUS 0x00
- #define USB_REQ_CLEAR_FEATURE 0x01
- #define USB_REQ_SET_FEATURE 0x03
- #define USB_REQ_SET_ADDRESS 0x05
- #define USB_REQ_GET_DESCRIPTOR 0x06
- #define USB_REQ_SET_DESCRIPTOR 0x07
- #define USB_REQ_GET_CONFIGURATION 0x08
- #define USB_REQ_SET_CONFIGURATION 0x09
- #define USB_REQ_GET_INTERFACE 0x0A
- #define USB_REQ_SET_INTERFACE 0x0B
- #define USB_REQ_SYNCH_FRAME 0x0C
- #define USBD_DEVICE_REQUESTS(x) (((unsigned int)x <= USB_REQ_SYNCH_FRAME) ? usbd_device_requests[x] : "UNKNOWN")
- #define USB_REQ_GET_REPORT 0x01
- #define USB_REQ_GET_IDLE 0x02
- #define USB_REQ_GET_PROTOCOL 0x03
- #define USB_REQ_SET_REPORT 0x09
- #define USB_REQ_SET_IDLE 0x0A
- #define USB_REQ_SET_PROTOCOL 0x0B
- #if defined(CONFIG_USBD_HS)
- #define USB_BCD_VERSION 0x0200
- #else
- #define USB_BCD_VERSION 0x0110
- #endif
- #define USB_REQ_DIRECTION_MASK 0x80
- #define USB_REQ_TYPE_MASK 0x60
- #define USB_REQ_RECIPIENT_MASK 0x1f
- #define USB_REQ_DEVICE2HOST 0x80
- #define USB_REQ_HOST2DEVICE 0x00
- #define USB_REQ_TYPE_STANDARD 0x00
- #define USB_REQ_TYPE_CLASS 0x20
- #define USB_REQ_TYPE_VENDOR 0x40
- #define USB_REQ_RECIPIENT_DEVICE 0x00
- #define USB_REQ_RECIPIENT_INTERFACE 0x01
- #define USB_REQ_RECIPIENT_ENDPOINT 0x02
- #define USB_REQ_RECIPIENT_OTHER 0x03
- #define USB_STATUS_SELFPOWERED 0x01
- #define USB_STATUS_REMOTEWAKEUP 0x02
- #define USB_STATUS_HALT 0x01
- #define USB_DESCRIPTOR_TYPE_DEVICE 0x01
- #define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02
- #define USB_DESCRIPTOR_TYPE_STRING 0x03
- #define USB_DESCRIPTOR_TYPE_INTERFACE 0x04
- #define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05
- #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06
- #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 0x07
- #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08
- #define USB_DESCRIPTOR_TYPE_HID 0x21
- #define USB_DESCRIPTOR_TYPE_REPORT 0x22
- #define USBD_DEVICE_DESCRIPTORS(x) (((unsigned int)x <= USB_DESCRIPTOR_TYPE_INTERFACE_POWER) ? \
- usbd_device_descriptors[x] : "UNKNOWN")
- #define USB_ENDPOINT_HALT 0x00
- #define USB_DEVICE_REMOTE_WAKEUP 0x01
- #define USB_TEST_MODE 0x02
- struct usb_device_request {
- u8 bmRequestType;
- u8 bRequest;
- u16 wValue;
- u16 wIndex;
- u16 wLength;
- } __attribute__ ((packed));
- typedef enum urb_send_status {
- SEND_IN_PROGRESS,
- SEND_FINISHED_OK,
- SEND_FINISHED_ERROR,
- RECV_READY,
- RECV_OK,
- RECV_ERROR
- } urb_send_status_t;
- typedef enum usb_device_state {
- STATE_INIT,
- STATE_CREATED,
- STATE_ATTACHED,
- STATE_POWERED,
- STATE_DEFAULT,
- STATE_ADDRESSED,
- STATE_CONFIGURED,
- STATE_UNKNOWN,
- } usb_device_state_t;
- #define USBD_DEVICE_STATE(x) (((unsigned int)x <= STATE_UNKNOWN) ? usbd_device_states[x] : "UNKNOWN")
- typedef enum usb_device_status {
- USBD_OPENING,
- USBD_OK,
- USBD_SUSPENDED,
- USBD_CLOSING,
- } usb_device_status_t;
- #define USBD_DEVICE_STATUS(x) (((unsigned int)x <= USBD_CLOSING) ? usbd_device_status[x] : "UNKNOWN")
- typedef enum usb_device_event {
- DEVICE_UNKNOWN,
- DEVICE_INIT,
- DEVICE_CREATE,
- DEVICE_HUB_CONFIGURED,
- DEVICE_RESET,
- DEVICE_ADDRESS_ASSIGNED,
- DEVICE_CONFIGURED,
- DEVICE_SET_INTERFACE,
- DEVICE_SET_FEATURE,
- DEVICE_CLEAR_FEATURE,
- DEVICE_DE_CONFIGURED,
- DEVICE_BUS_INACTIVE,
- DEVICE_BUS_ACTIVITY,
- DEVICE_POWER_INTERRUPTION,
- DEVICE_HUB_RESET,
- DEVICE_DESTROY,
- DEVICE_HOTPLUG,
- DEVICE_FUNCTION_PRIVATE,
- } usb_device_event_t;
- typedef struct urb_link {
- struct urb_link *next;
- struct urb_link *prev;
- } urb_link;
- #define URB_BUF_SIZE 512
- struct urb {
- struct usb_endpoint_instance *endpoint;
- struct usb_device_instance *device;
- struct usb_device_request device_request;
- struct urb_link link;
- u8* buffer;
- unsigned int buffer_length;
- unsigned int actual_length;
- urb_send_status_t status;
- int data;
- u16 buffer_data[URB_BUF_SIZE];
- };
- struct usb_endpoint_instance {
- int endpoint_address;
-
- int status;
- int state;
-
- struct urb_link rcv;
- struct urb_link rdy;
- struct urb *rcv_urb;
- int rcv_attributes;
- int rcv_packetSize;
- int rcv_transferSize;
- int rcv_queue;
-
- struct urb_link tx;
- struct urb_link done;
- struct urb *tx_urb;
- int tx_attributes;
- int tx_packetSize;
- int tx_transferSize;
- int tx_queue;
- int sent;
- int last;
- };
- struct usb_alternate_instance {
- struct usb_interface_descriptor *interface_descriptor;
- int endpoints;
- int *endpoint_transfersize_array;
- struct usb_endpoint_descriptor **endpoints_descriptor_array;
- };
- struct usb_interface_instance {
- int alternates;
- struct usb_alternate_instance *alternates_instance_array;
- };
- struct usb_configuration_instance {
- int interfaces;
- struct usb_configuration_descriptor *configuration_descriptor;
- struct usb_interface_instance *interface_instance_array;
- };
- struct usb_device_instance {
-
- char *name;
- struct usb_device_descriptor *device_descriptor;
- #if defined(CONFIG_USBD_HS)
- struct usb_qualifier_descriptor *qualifier_descriptor;
- #endif
- void (*event) (struct usb_device_instance *device, usb_device_event_t event, int data);
-
- int (*cdc_recv_setup)(struct usb_device_request *request, struct urb *urb);
-
- struct usb_bus_instance *bus;
-
- int configurations;
- struct usb_configuration_instance *configuration_instance_array;
-
- usb_device_state_t device_state;
- usb_device_state_t device_previous_state;
- u8 address;
- u8 configuration;
- u8 interface;
- u8 alternate;
- usb_device_status_t status;
- int urbs_queued;
-
- unsigned long usbd_rxtx_timestamp;
- unsigned long usbd_last_rxtx_timestamp;
- };
- struct usb_bus_instance {
- struct usb_device_instance *device;
- struct usb_endpoint_instance *endpoint_array;
- int max_endpoints;
- unsigned char maxpacketsize;
- unsigned int serial_number;
- char *serial_number_str;
- void *privdata;
- };
- extern char *usbd_device_events[];
- extern char *usbd_device_states[];
- extern char *usbd_device_status[];
- extern char *usbd_device_requests[];
- extern char *usbd_device_descriptors[];
- void urb_link_init (urb_link * ul);
- void urb_detach (struct urb *urb);
- urb_link *first_urb_link (urb_link * hd);
- struct urb *first_urb (urb_link * hd);
- struct urb *first_urb_detached (urb_link * hd);
- void urb_append (urb_link * hd, struct urb *urb);
- struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint);
- void usbd_dealloc_urb (struct urb *urb);
- void usbd_device_event_irq (struct usb_device_instance *conf, usb_device_event_t, int);
- void usbd_device_event (struct usb_device_instance *conf, usb_device_event_t, int);
- struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct usb_device_instance *, int, int);
- struct usb_function_instance *usbd_device_function_instance (struct usb_device_instance *, unsigned int);
- struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *, int, int, int);
- struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *, int, int, int, int);
- struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance *, int, int, int, int);
- struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
- struct usb_class_descriptor *usbd_device_class_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
- struct usb_class_report_descriptor *usbd_device_class_report_descriptor_index( struct usb_device_instance *, int , int , int , int , int );
- struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *, int, int, int, int, int);
- int usbd_device_endpoint_transfersize (struct usb_device_instance *, int, int, int, int, int);
- struct usb_string_descriptor *usbd_get_string (u8);
- struct usb_device_descriptor *usbd_device_device_descriptor(struct
- usb_device_instance *, int);
- #if defined(CONFIG_USBD_HS)
- int is_usbd_high_speed(void);
- #endif
- int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint);
- void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad);
- void usbd_tx_complete (struct usb_endpoint_instance *endpoint);
- #ifdef DEBUG
- static inline void print_urb(struct urb *u)
- {
- serial_printf("urb %p\n", (u));
- serial_printf("\tendpoint %p\n", u->endpoint);
- serial_printf("\tdevice %p\n", u->device);
- serial_printf("\tbuffer %p\n", u->buffer);
- serial_printf("\tbuffer_length %d\n", u->buffer_length);
- serial_printf("\tactual_length %d\n", u->actual_length);
- serial_printf("\tstatus %d\n", u->status);
- serial_printf("\tdata %d\n", u->data);
- }
- static inline void print_usb_device_request(struct usb_device_request *r)
- {
- serial_printf("usb request\n");
- serial_printf("\tbmRequestType 0x%2.2x\n", r->bmRequestType);
- if ((r->bmRequestType & USB_REQ_DIRECTION_MASK) == 0)
- serial_printf("\t\tDirection : To device\n");
- else
- serial_printf("\t\tDirection : To host\n");
- if ((r->bmRequestType & USB_TYPE_STANDARD) == USB_TYPE_STANDARD)
- serial_printf("\t\tType : Standard\n");
- if ((r->bmRequestType & USB_TYPE_CLASS) == USB_TYPE_CLASS)
- serial_printf("\t\tType : Standard\n");
- if ((r->bmRequestType & USB_TYPE_VENDOR) == USB_TYPE_VENDOR)
- serial_printf("\t\tType : Standard\n");
- if ((r->bmRequestType & USB_TYPE_RESERVED) == USB_TYPE_RESERVED)
- serial_printf("\t\tType : Standard\n");
- if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
- USB_REQ_RECIPIENT_DEVICE)
- serial_printf("\t\tRecipient : Device\n");
- if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
- USB_REQ_RECIPIENT_INTERFACE)
- serial_printf("\t\tRecipient : Interface\n");
- if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
- USB_REQ_RECIPIENT_ENDPOINT)
- serial_printf("\t\tRecipient : Endpoint\n");
- if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
- USB_REQ_RECIPIENT_OTHER)
- serial_printf("\t\tRecipient : Other\n");
- serial_printf("\tbRequest 0x%2.2x\n", r->bRequest);
- if (r->bRequest == USB_REQ_GET_STATUS)
- serial_printf("\t\tGET_STATUS\n");
- else if (r->bRequest == USB_REQ_SET_ADDRESS)
- serial_printf("\t\tSET_ADDRESS\n");
- else if (r->bRequest == USB_REQ_SET_FEATURE)
- serial_printf("\t\tSET_FEATURE\n");
- else if (r->bRequest == USB_REQ_GET_DESCRIPTOR)
- serial_printf("\t\tGET_DESCRIPTOR\n");
- else if (r->bRequest == USB_REQ_SET_CONFIGURATION)
- serial_printf("\t\tSET_CONFIGURATION\n");
- else if (r->bRequest == USB_REQ_SET_INTERFACE)
- serial_printf("\t\tUSB_REQ_SET_INTERFACE\n");
- else
- serial_printf("\tUNKNOWN %d\n", r->bRequest);
- serial_printf("\twValue 0x%4.4x\n", r->wValue);
- if (r->bRequest == USB_REQ_GET_DESCRIPTOR) {
- switch (r->wValue >> 8) {
- case USB_DESCRIPTOR_TYPE_DEVICE:
- serial_printf("\tDEVICE\n");
- break;
- case USB_DESCRIPTOR_TYPE_CONFIGURATION:
- serial_printf("\tCONFIGURATION\n");
- break;
- case USB_DESCRIPTOR_TYPE_STRING:
- serial_printf("\tSTRING\n");
- break;
- case USB_DESCRIPTOR_TYPE_INTERFACE:
- serial_printf("\tINTERFACE\n");
- break;
- case USB_DESCRIPTOR_TYPE_ENDPOINT:
- serial_printf("\tENDPOINT\n");
- break;
- case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
- serial_printf("\tDEVICE_QUALIFIER\n");
- break;
- case USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION:
- serial_printf("\tOTHER_SPEED_CONFIGURATION\n");
- break;
- case USB_DESCRIPTOR_TYPE_INTERFACE_POWER:
- serial_printf("\tINTERFACE_POWER\n");
- break;
- case USB_DESCRIPTOR_TYPE_HID:
- serial_printf("\tHID\n");
- break;
- case USB_DESCRIPTOR_TYPE_REPORT:
- serial_printf("\tREPORT\n");
- break;
- default:
- serial_printf("\tUNKNOWN TYPE\n");
- break;
- }
- }
- serial_printf("\twIndex 0x%4.4x\n", r->wIndex);
- serial_printf("\twLength 0x%4.4x\n", r->wLength);
- }
- #else
- #define print_urb(u)
- #define print_usb_device_request(r)
- #endif
- #endif
|