60static volatile uint32_t g_devmap;
65LIST_HEAD(_UsbDriverList);
66LIST_HEAD(_UsbBusList);
81 list_add_tail(&new_driver->driver_list, &_UsbDriverList);
88 const USB_DEV_ID_T *
id)
100 for(;
id->idVendor ||
id->bDeviceClass ||
id->bInterfaceClass ||
id->driver_info;
id++)
102 if((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
103 (id->idVendor != dev->descriptor.idVendor))
106 if((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
107 (id->idProduct != dev->descriptor.idProduct))
112 if((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
113 (id->bcdDevice_lo > dev->descriptor.bcdDevice))
116 if((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
117 (id->bcdDevice_hi < dev->descriptor.bcdDevice))
120 if((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
121 (id->bDeviceClass != dev->descriptor.bDeviceClass))
124 if((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
125 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
128 if((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
129 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
132 if((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
133 (id->bInterfaceClass != intf->bInterfaceClass))
136 if((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
137 (id->bInterfaceSubClass != intf->bInterfaceSubClass))
140 if((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
141 (id->bInterfaceProtocol != intf->bInterfaceProtocol))
168 const USB_DEV_ID_T *id;
169 USB_DRIVER_T *driver;
172 for(tmp = _UsbDriverList.next; tmp != &_UsbDriverList;)
174 driver = list_entry(tmp, USB_DRIVER_T, driver_list);
177 id = driver->id_table;
178 id = usb_match_id(dev, intf,
id);
181 if(driver->probe(dev, intf,
id) == 0)
186 for(found = 0, i = 0; i < dev->driver_cnt; i++)
187 if(dev->driver[i] == driver) found = 1;
193 USB_error(
"Driver overrun for one device!\n");
196 dev->driver[dev->driver_cnt] = driver;
198 USB_debug(
"Deivce bind driver count %d\n", dev->driver_cnt);
204 if(dev->driver_cnt == 0)
205 USB_warning(
"No matching driver!!\n");
222 return urb->
dev->
bus->op->submit_urb(urb);
240 return urb->
dev->
bus->op->unlink_urb(urb);
281 if(timeout == 0) timeout = 100;
290 for(t0 = 0; t0 < 0x100000; t0++)
302 while(sysGetTicks(
TIMER0) - t0 < timeout)
308 if(sysGetTicks(
TIMER0) - t0 >= timeout)
317 USB_warning(
"usb_control/bulk_msg: timeout\n");
334 DEV_REQ_T *cmd,
void *data,
int len,
int timeout)
340 FILL_CONTROL_URB(&urb, usb_dev, pipe, (uint8_t*)cmd, data, len,
376 uint8_t requesttype, uint16_t
value, uint16_t index,
void *data,
377 uint16_t size,
int timeout)
381 dr.requesttype = requesttype;
382 dr.request = request;
414 void *data,
int len,
int *actual_length,
int timeout)
418 FILL_BULK_URB(&urb, usb_dev, pipe, (uint8_t *)data, len,
426int usb_maxpacket(
USB_DEV_T *dev, uint32_t pipe,
int out)
431 ep_addr = (((pipe) >> 15) & 0xf) | (pipe & 0x80);
433 if((ep_addr == 0) || (ep_addr == 0x80))
434 return dev->ep_list[0].wMaxPacketSize;
436 for(i = 0; i < dev->ep_list_cnt; i++)
438 if((dev->ep_list[i].cfgno == dev->act_config) &&
439 (dev->ep_list[i].bEndpointAddress == ep_addr))
441 return dev->ep_list[i].wMaxPacketSize;
444 USB_error(
"usb_maxpacket - endpoint %x not found!!\n", ep_addr);
449static int usb_parse_endpoint(
USB_DEV_T *dev,
int cfg_value,
USB_IF_DESC_T *ifp, uint8_t *buffer,
int size)
452 USB_DESC_HDR_T *header;
454 int parsed = 0, numskipped;
456 header = (USB_DESC_HDR_T *)buffer;
460 if(header->bLength > size)
462 USB_error(
"Error! - ran out of descriptors parsing");
466 if(header->bDescriptorType != USB_DT_ENDPOINT)
468 USB_warning(
"Warning! - unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X",
469 endpoint.bDescriptorType, USB_DT_ENDPOINT);
475 ep_info = &dev->ep_list[dev->ep_list_cnt];
476 ep_info->cfgno = cfg_value;
477 ep_info->ifnum = ifp->bInterfaceNumber;
478 ep_info->altno = ifp->bAlternateSetting;
479 ep_info->bEndpointAddress = endpoint.bEndpointAddress;
480 ep_info->bmAttributes = endpoint.bmAttributes;
481 ep_info->bInterval = endpoint.bInterval;
482 ep_info->wMaxPacketSize = endpoint.wMaxPacketSize;
483 if ((ep_info->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
485 if (ep_info->wMaxPacketSize > 64)
487 USB_debug(
"Endpoint %x wMaxPacketSize is %d bytes, force to change as 64 bytes!\n",
488 endpoint.bEndpointAddress, endpoint.wMaxPacketSize);
489 ep_info->wMaxPacketSize = 64;
498 USB_error(
"Too many endpoints!\n");
501 buffer += header->bLength;
502 size -= header->bLength;
503 parsed += header->bLength;
507 while(size >=
sizeof(USB_DESC_HDR_T))
509 header = (USB_DESC_HDR_T *)buffer;
511 if(header->bLength < 2)
513 USB_error(
"Error! - invalid descriptor length of %d\n", header->bLength);
519 if((header->bDescriptorType == USB_DT_ENDPOINT) ||
520 (header->bDescriptorType == USB_DT_INTERFACE) ||
521 (header->bDescriptorType == USB_DT_CONFIG) ||
522 (header->bDescriptorType == USB_DT_DEVICE))
524 USB_info(
"skipping descriptor 0x%X\n", header->bDescriptorType);
527 buffer += header->bLength;
528 size -= header->bLength;
529 parsed += header->bLength;
533 USB_warning(
"Skipped %d class/vendor specific endpoint descriptors\n", numskipped);
539static int usb_parse_interface(
USB_DEV_T *dev,
int cfg_value, uint8_t *buffer,
int size)
541 int i, numskipped, retval, parsed = 0;
542 USB_DESC_HDR_T *header;
545#ifdef USB_VERBOSE_DEBUG
551 memcpy((
char *)&ifp, buffer, USB_DT_INTERFACE_SIZE);
554 buffer += ifp.bLength;
555 parsed += ifp.bLength;
565 while(size >=
sizeof(USB_DESC_HDR_T))
567 header = (USB_DESC_HDR_T *)buffer;
569 if(header->bLength < 2)
571 USB_error(
"Invalid descriptor length of %d\n", header->bLength);
577 if((header->bDescriptorType == USB_DT_INTERFACE) ||
578 (header->bDescriptorType == USB_DT_ENDPOINT) ||
579 (header->bDescriptorType == USB_DT_CONFIG) ||
580 (header->bDescriptorType == USB_DT_DEVICE))
583 USB_info(
"skipping descriptor 0x%X\n", header->bDescriptorType);
585 buffer += header->bLength;
586 parsed += header->bLength;
587 size -= header->bLength;
592 USB_warning(
"skipped %d class/vendor specific interface descriptors\n", numskipped);
596 header = (USB_DESC_HDR_T *)buffer;
597 if((size >=
sizeof(USB_DESC_HDR_T)) &&
598 ((header->bDescriptorType == USB_DT_CONFIG) ||
599 (header->bDescriptorType == USB_DT_DEVICE)))
601 USB_warning(
"parsing interface - hit an unexpected descriptor!\n");
605 if(ifp.bNumEndpoints > USB_MAXENDPOINTS)
607 USB_warning(
"Warning - illegal endpoint number %d\n", ifp.bNumEndpoints);
612 if(ifp.bNumEndpoints > 0)
614 for(i = 0; i < ifp.bNumEndpoints; i++)
616 header = (USB_DESC_HDR_T *)buffer;
617 if(header->bLength > size)
619 USB_error(
"Error - ran out of descriptors parsing");
623#ifdef DUMP_DEV_DESCRIPTORS
626 retval = usb_parse_endpoint(dev, cfg_value, &ifp, buffer, size);
637 memcpy((
char *)&ifp, buffer, USB_DT_INTERFACE_SIZE);
638 if((size < USB_DT_INTERFACE_SIZE) ||
639 (ifp.bDescriptorType != USB_DT_INTERFACE) || !ifp.bAlternateSetting)
649 USB_DESC_HDR_T *header;
651 memcpy(config, buffer, USB_DT_CONFIG_SIZE);
652 size = config->wTotalLength;
654 if(config->bNumInterfaces > USB_MAXINTERFACES)
656 USB_warning(
"Warning - too many interfaces\n");
660 buffer += config->bLength;
661 size -= config->bLength;
663 for(i = 0; i < config->bNumInterfaces; i++)
670 while(size >=
sizeof(USB_DESC_HDR_T))
672 header = (USB_DESC_HDR_T *)buffer;
674 if((header->bLength > size) || (header->bLength < 2))
676 USB_error(
"Error - invalid descriptor length of %d\n", header->bLength);
682 if((header->bDescriptorType == USB_DT_ENDPOINT) ||
683 (header->bDescriptorType == USB_DT_INTERFACE) ||
684 (header->bDescriptorType == USB_DT_CONFIG) ||
685 (header->bDescriptorType == USB_DT_DEVICE))
688 USB_info(
"skipping descriptor 0x%X\n", header->bDescriptorType);
691 buffer += header->bLength;
692 size -= header->bLength;
697 USB_warning(
"skipped %d class/vendor specific endpoint descriptors\n", numskipped);
700#ifdef DUMP_DEV_DESCRIPTORS
704 retval = usb_parse_interface(dev, config->bConfigurationValue, buffer, size);
721void usbh_disconnect_device(
USB_DEV_T **pdev)
724 USB_DRIVER_T *driver;
732 USB_info(
"usbh_disconnect_device - USB disconnect on device %d\n", dev->devnum);
736 for(i = 0; i < dev->driver_cnt; i++)
738 driver = dev->driver[i];
739 driver->disconnect(dev);
743 for(i = 0; i < USB_MAXCHILDREN; i++)
747 usbh_disconnect_device(child);
753 g_devmap &= ~(1 << dev->devnum);
773 dev->descriptor.bMaxPacketSize0 = 8;
775 for(devnum = 1; devnum < 31; devnum++)
777 if((g_devmap & (1 << devnum)) == 0)
782 USB_error(
"Serious devnum error!\n");
786 g_devmap |= (1 << devnum);
787 dev->devnum = devnum;
798#define GET_TIMEOUT 1000
799#define SET_TIMEOUT 100
828 memset(buf, 0, size);
833 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
842static int usbh_get_string_descriptor(
USB_DEV_T *dev, uint16_t langid, uint8_t index,
846 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
847 (USB_DT_STRING << 8) + index, langid, buf, size, HZ *
GET_TIMEOUT);
851static int usbh_get_device_descriptor(
USB_DEV_T *dev)
855 ret =
USBH_GetDescriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor,
sizeof(dev->descriptor));
860int usbh_get_protocol(
USB_DEV_T *dev,
int ifnum)
866 USB_REQ_GET_PROTOCOL, USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
874int usbh_set_protocol(
USB_DEV_T *dev,
int ifnum,
int protocol)
877 USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
882int usbh_set_idle(
USB_DEV_T *dev,
int ifnum,
int duration,
int report_id)
885 USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
906 endp = usb_pipeendpoint(pipe) | (usb_pipein(pipe) << 7);
914 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0, endp,
NULL, 0, HZ *
SET_TIMEOUT);
921 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RECIP_ENDPOINT, 0, endp,
926 if(USB_SWAP16(status) & 1)
929 usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
933 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
953 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, alternate,
954 interface,
NULL, 0, HZ * 5)) < 0)
957 dev->act_iface = interface;
958 dev->iface_alternate = alternate;
978 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
NULL, 0, HZ *
SET_TIMEOUT)) < 0)
980 dev->act_config = configuration;
998 uint32_t cfgno, length;
1001 uint32_t stack_buff[256];
1003 if(dev->descriptor.bNumConfigurations > USB_MAXCONFIG)
1005 USB_warning(
"Too many configurations\n");
1009 if(dev->descriptor.bNumConfigurations != 1)
1011 USB_warning(
"This driver does not support multi-configuration devices!\n");
1015 buffer = (uint8_t *)&stack_buff[0];
1018 for(cfgno = 0; cfgno < dev->descriptor.bNumConfigurations; cfgno++)
1026 USB_error(
"Unable to get descriptor\n");
1029 USB_error(
"Config descriptor too short (expected %i, got %i)\n", 8, result);
1036 length = desc->wTotalLength;
1041 USB_error(
"Config descriptor is too large. Read 256 bytes only. This may cause lost of information!\n");
1048 USB_error(
"Couldn't get all of config descriptors\n");
1054 USB_error(
"Config descriptor too short (expected %d, got %d)\n", length, result);
1067 USB_error(
"Failed to set device %d default configuration (error=%d)\n", dev->devnum, result);
1072#ifdef DUMP_DEV_DESCRIPTORS
1073 usbh_dump_config_descriptor(desc);
1076 result = usb_parse_configuration(dev, desc, buffer);
1079 USB_warning(
"Descriptor data left\n");
1083 USB_warning(
"usb_parse_configuration error\n");
1092 dev->descriptor.bNumConfigurations = cfgno;
1102int usbh_translate_string(
USB_DEV_T *dev,
int index,
char *buf,
int size)
1108 if(size == 0 || !buf || !index)
1113 if(!dev->have_langid)
1115 err = usbh_get_string_descriptor(dev, 0, 0, tbuf, 4);
1118 USB_error(
"usbh_translate_string - error getting string descriptor 0 (error=%d)\n", err);
1121 else if(tbuf[0] < 4)
1123 USB_error(
"usbh_translate_string - string descriptor 0 too short\n");
1129 dev->have_langid = -1;
1130 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
1132 USB_info(
"USB device number %d default language ID 0x%x\n",
1133 dev->devnum, dev->string_langid);
1138 if((dev->descriptor.idVendor == 0x416) && (dev->descriptor.idProduct == 0x9683))
1140 USB_warning(
"??? Skip usbh_get_string_descriptor, 255\n");
1149 err = usbh_get_string_descriptor(dev, dev->string_langid, index, tbuf, 255);
1154 for(idx = 0, u = 2; u < err; u += 2)
1161 buf[idx++] = tbuf[u];
1179int usbh_settle_new_device(
USB_DEV_T *dev)
1183 USB_info(
"[USBH] Enter usbh_settle_new_device() ...\n");
1190 dev->ep_list[0].bEndpointAddress = 0;;
1191 dev->ep_list[0].wMaxPacketSize = 8;
1192 dev->ep_list_cnt = 1;
1194 err = usbh_set_address(dev);
1197 USB_error(
"USB device not accepting new address=%d (error=%d)\n", dev->devnum, err);
1198 g_devmap &= ~(1 << dev->devnum);
1205 memset((
char *)&dev->descriptor, 0, 8);
1210 USB_debug(
"USBH_GetDescriptor failed!!\n");
1212 USB_error(
"USB device not responding, giving up (error=%d)\n", err);
1214 USB_error(
"USB device descriptor short read (expected %i, got %i)\n", 8, err);
1215 g_devmap &= ~(1 << dev->devnum);
1220 dev->ep_list[0].wMaxPacketSize = dev->descriptor.bMaxPacketSize0;
1222 err = usbh_get_device_descriptor(dev);
1223 if(err <
sizeof(dev->descriptor))
1226 USB_error(
"unable to get device descriptor (error=%d)\n", err);
1228 USB_error(
"USB device descriptor short read (expected %i, got %i)\n",
1229 sizeof(dev->descriptor), err);
1231 g_devmap &= ~(1 << dev->devnum);
1236#ifdef DUMP_DEV_DESCRIPTORS
1237 usbh_dump_device_descriptor(&dev->descriptor);
1243 USB_error(
"Unable to get device %d configuration (error=%d)\n", dev->devnum, err);
1244 g_devmap &= ~(1 << dev->devnum);
1250#ifdef USB_VERBOSE_DEBUG
1251 if(dev->descriptor.iManufacturer)
1252 usbh_print_usb_string(dev,
"Manufacturer", dev->descriptor.iManufacturer);
1253 if(dev->descriptor.iProduct)
1254 usbh_print_usb_string(dev,
"Product", dev->descriptor.iProduct);
1255 if(dev->descriptor.iSerialNumber)
1256 usbh_print_usb_string(dev,
"SerialNumber", dev->descriptor.iSerialNumber);
1273 USBH->HcMiscControl |= 0x8;
1285 usbh_init_hub_driver();
1287#ifdef SUPPORT_HID_CLASS
1290#ifdef SUPPORT_UMAS_CLASS
1293#ifdef SUPPORT_UVC_CLASS
1296#ifdef SUPPORT_UAC_CLASS
1299#ifdef SUPPORT_CDC_CLASS
1302 if(usbh_init_ohci() < 0)
1304 USB_debug(
"OHCI init failed!\n");
1344 if(dev_alloc_mark[i])
1346 dev = &g_dev_pool[i];
1348 USB_REQ_SET_FEATURE, 0x00, 0x0001, 0x0000,
NULL, 0x0000, HZ *
SET_TIMEOUT);
1353 if(
USBH->HcRhPortStatus[0] & 0x1)
1354 USBH->HcRhPortStatus[0] = 0x4;
1356 if(
USBH->HcRhPortStatus[1] & 0x1)
1357 USBH->HcRhPortStatus[1] = 0x4;
1382 if(
USBH->HcRhPortStatus[0] & 0x4)
1383 USBH->HcRhPortStatus[0] = 0x8;
1384 if(
USBH->HcRhPortStatus[1] & 0x4)
1385 USBH->HcRhPortStatus[1] = 0x8;
#define USBH_HcRhStatus_DRWE_Msk
#define USBH_HcInterruptEnable_RD_Msk
#define USBH_HcRhStatus_LPS_Msk
#define USBH_HcInterruptEnable_RHSC_Msk
#define USBH_HcControl_HCFS_Pos
static void ctrl_msg_complete(URB_T *urb)
static int usb_internal_control_msg(USB_DEV_T *usb_dev, uint32_t pipe, DEV_REQ_T *cmd, void *data, int len, int timeout)
int32_t USBH_RegisterDriver(USB_DRIVER_T *new_driver)
Register a device driver to USB Host Core driver.
int32_t USBH_SubmitUrb(URB_T *urb)
Submit an URB to USB core for transfer data.
int32_t USBH_SendBulkMsg(USB_DEV_T *usb_dev, uint32_t pipe, void *data, int len, int *actual_length, int timeout)
Execute a bulk transfer.
int32_t USBH_Resume(void)
Resume USB Host controller and devices.
int32_t USBH_Close(void)
Disable USB Host controller function.
int32_t USBH_Suspend(void)
Suspend USB Host Controller and devices.
static void clr_ctrl_msg_signal()
int32_t USBH_UnlinkUrb(URB_T *urb)
Cancel an URB which has been submit to USB core.
int32_t USBH_Open(void)
Open USB Host controller function.
static int usbh_settle_configuration(USB_DEV_T *dev)
int32_t USBH_SetInterface(USB_DEV_T *dev, char interface, char alternate)
Set USB device interface.
int32_t USBH_GetDescriptor(USB_DEV_T *dev, uint8_t type, uint8_t index, void *buf, int size)
Get a descriptor from device.
static int usb_start_wait_urb(URB_T *urb, int timeout, int *actual_length)
int32_t USBH_ClearHalt(USB_DEV_T *dev, int pipe)
Clear the halt state of an endpoint.
static int get_ctrl_msg_signal()
int32_t USBH_SetConfiguration(USB_DEV_T *dev, int configuration)
Select USB device configuration.
static volatile int _Event_UrbCompleted
int32_t USBH_SendCtrlMsg(USB_DEV_T *dev, uint32_t pipe, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size, int timeout)
Execute a control transfer.
#define MAX_DRIVER_PER_DEV
USB Host core driver header file.
HIDDEN_SYMBOLS struct usb_device USB_DEV_T
#define USB_ERR_INPROGRESS
void usbh_free_device(USB_DEV_T *dev)
HIDDEN_SYMBOLS void usbh_init_memory()
void usbh_mdelay(uint32_t msec)
void usbh_free_dev_urbs(USB_DEV_T *dev)