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");
221 return urb->
dev->
bus->op->submit_urb(urb);
239 return urb->
dev->
bus->op->unlink_urb(urb);
251static volatile int _Event_UrbCompleted;
256static void ctrl_msg_complete(
URB_T *urb)
258 _Event_UrbCompleted = 1;
261static int get_ctrl_msg_signal()
263 return _Event_UrbCompleted;
265static void clr_ctrl_msg_signal()
267 _Event_UrbCompleted = 0;
276static int usb_start_wait_urb(
URB_T *urb,
int timeout,
int* actual_length)
282 if (timeout == 0) timeout = 100;
284 clr_ctrl_msg_signal();
291 for (t0 = 0; t0 < 0x100000; t0++)
293 if (get_ctrl_msg_signal())
303 while (sysGetTicks(
TIMER0) - t0 < timeout)
305 if (get_ctrl_msg_signal())
309 if (sysGetTicks(
TIMER0) - t0 >= timeout)
318 USB_warning(
"usb_control/bulk_msg: timeout\n");
334static int usb_internal_control_msg(
USB_DEV_T *usb_dev, uint32_t pipe,
335 DEV_REQ_T *cmd,
void *data,
int len,
int timeout)
341 FILL_CONTROL_URB(&urb, usb_dev, pipe, (uint8_t*)cmd, data, len,
342 ctrl_msg_complete, 0);
344 retv = usb_start_wait_urb(&urb, timeout, &length);
379 uint8_t requesttype, uint16_t
value, uint16_t index,
void *data,
380 uint16_t size,
int timeout)
384 dr.requesttype = requesttype;
385 dr.request = request;
389 return usb_internal_control_msg(dev, pipe, &dr, (uint8_t *)data, size, timeout);
417 void *data,
int len,
int *actual_length,
int timeout)
421 FILL_BULK_URB(&urb,usb_dev,pipe,(uint8_t *)data,len,
422 ctrl_msg_complete, 0);
424 return usb_start_wait_urb(&urb, timeout, actual_length);
429int usb_maxpacket(
USB_DEV_T *dev, uint32_t pipe,
int out)
434 ep_addr = (((pipe) >> 15) & 0xf) | (pipe & 0x80);
436 if ((ep_addr == 0) || (ep_addr == 0x80))
437 return dev->ep_list[0].wMaxPacketSize;
439 for (i = 0; i < dev->ep_list_cnt; i++)
441 if ((dev->ep_list[i].cfgno == dev->act_config) &&
442 (dev->ep_list[i].bEndpointAddress == ep_addr))
444 return dev->ep_list[i].wMaxPacketSize;
447 USB_error(
"usb_maxpacket - endpoint %x not found!!\n", ep_addr);
452static int usb_parse_endpoint(
USB_DEV_T *dev,
int cfg_value,
USB_IF_DESC_T *ifp, uint8_t *buffer,
int size)
455 USB_DESC_HDR_T *header;
457 int parsed = 0, numskipped;
459 header = (USB_DESC_HDR_T *)buffer;
463 if (header->bLength > size)
465 USB_error(
"Error! - ran out of descriptors parsing");
469 if (header->bDescriptorType != USB_DT_ENDPOINT)
471 USB_warning(
"Warning! - unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X",
472 endpoint.bDescriptorType, USB_DT_ENDPOINT);
478 ep_info = &dev->ep_list[dev->ep_list_cnt];
479 ep_info->cfgno = cfg_value;
480 ep_info->ifnum = ifp->bInterfaceNumber;
481 ep_info->altno = ifp->bAlternateSetting;
482 ep_info->bEndpointAddress = endpoint.bEndpointAddress;
483 ep_info->bmAttributes = endpoint.bmAttributes;
484 ep_info->bInterval = endpoint.bInterval;
485 ep_info->wMaxPacketSize = endpoint.wMaxPacketSize;
486 if ((ep_info->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
488 if (ep_info->wMaxPacketSize > 64)
490 USB_debug(
"Endpoint %x wMaxPacketSize is %d bytes, force to change as 64 bytes!\n",
491 endpoint.bEndpointAddress, endpoint.wMaxPacketSize);
492 ep_info->wMaxPacketSize = 64;
501 USB_error(
"Too many endpoints!\n");
504 buffer += header->bLength;
505 size -= header->bLength;
506 parsed += header->bLength;
510 while (size >=
sizeof(USB_DESC_HDR_T))
512 header = (USB_DESC_HDR_T *)buffer;
514 if (header->bLength < 2)
516 USB_error(
"Error! - invalid descriptor length of %d\n", header->bLength);
522 if ((header->bDescriptorType == USB_DT_ENDPOINT) ||
523 (header->bDescriptorType == USB_DT_INTERFACE) ||
524 (header->bDescriptorType == USB_DT_CONFIG) ||
525 (header->bDescriptorType == USB_DT_DEVICE))
527 USB_info(
"skipping descriptor 0x%X\n", header->bDescriptorType);
530 buffer += header->bLength;
531 size -= header->bLength;
532 parsed += header->bLength;
536 USB_warning(
"Skipped %d class/vendor specific endpoint descriptors\n", numskipped);
542static int usb_parse_interface(
USB_DEV_T *dev,
int cfg_value, uint8_t *buffer,
int size)
544 int i, numskipped, retval, parsed = 0;
545 USB_DESC_HDR_T *header;
548#ifdef USB_VERBOSE_DEBUG
554 memcpy((
char *)&ifp, buffer, USB_DT_INTERFACE_SIZE);
557 buffer += ifp.bLength;
558 parsed += ifp.bLength;
568 while (size >=
sizeof(USB_DESC_HDR_T))
570 header = (USB_DESC_HDR_T *)buffer;
572 if (header->bLength < 2)
574 USB_error(
"Invalid descriptor length of %d\n", header->bLength);
580 if ((header->bDescriptorType == USB_DT_INTERFACE) ||
581 (header->bDescriptorType == USB_DT_ENDPOINT) ||
582 (header->bDescriptorType == USB_DT_CONFIG) ||
583 (header->bDescriptorType == USB_DT_DEVICE))
586 USB_info(
"skipping descriptor 0x%X\n", header->bDescriptorType);
588 buffer += header->bLength;
589 parsed += header->bLength;
590 size -= header->bLength;
595 USB_warning(
"skipped %d class/vendor specific interface descriptors\n", numskipped);
599 header = (USB_DESC_HDR_T *)buffer;
600 if ((size >=
sizeof(USB_DESC_HDR_T)) &&
601 ((header->bDescriptorType == USB_DT_CONFIG) ||
602 (header->bDescriptorType == USB_DT_DEVICE)))
604 USB_warning(
"parsing interface - hit an unexpected descriptor!\n");
608 if (ifp.bNumEndpoints > USB_MAXENDPOINTS)
610 USB_warning(
"Warning - illegal endpoint number %d\n", ifp.bNumEndpoints);
615 if (ifp.bNumEndpoints > 0)
617 for (i = 0; i < ifp.bNumEndpoints; i++)
619 header = (USB_DESC_HDR_T *)buffer;
620 if (header->bLength > size)
622 USB_error(
"Error - ran out of descriptors parsing");
626#ifdef DUMP_DEV_DESCRIPTORS
629 retval = usb_parse_endpoint(dev, cfg_value, &ifp, buffer, size);
640 memcpy((
char *)&ifp, buffer, USB_DT_INTERFACE_SIZE);
641 if ((size < USB_DT_INTERFACE_SIZE) ||
642 (ifp.bDescriptorType != USB_DT_INTERFACE) || !ifp.bAlternateSetting)
652 USB_DESC_HDR_T *header;
654 memcpy(config, buffer, USB_DT_CONFIG_SIZE);
655 size = config->wTotalLength;
657 if (config->bNumInterfaces > USB_MAXINTERFACES)
659 USB_warning(
"Warning - too many interfaces\n");
663 buffer += config->bLength;
664 size -= config->bLength;
666 for (i = 0; i < config->bNumInterfaces; i++)
673 while (size >=
sizeof(USB_DESC_HDR_T))
675 header = (USB_DESC_HDR_T *)buffer;
677 if ((header->bLength > size) || (header->bLength < 2))
679 USB_error(
"Error - invalid descriptor length of %d\n", header->bLength);
685 if ((header->bDescriptorType == USB_DT_ENDPOINT) ||
686 (header->bDescriptorType == USB_DT_INTERFACE) ||
687 (header->bDescriptorType == USB_DT_CONFIG) ||
688 (header->bDescriptorType == USB_DT_DEVICE))
691 USB_info(
"skipping descriptor 0x%X\n", header->bDescriptorType);
694 buffer += header->bLength;
695 size -= header->bLength;
700 USB_warning(
"skipped %d class/vendor specific endpoint descriptors\n", numskipped);
703#ifdef DUMP_DEV_DESCRIPTORS
707 retval = usb_parse_interface(dev, config->bConfigurationValue, buffer, size);
724void usbh_disconnect_device(
USB_DEV_T **pdev)
727 USB_DRIVER_T *driver;
735 USB_info(
"usbh_disconnect_device - USB disconnect on device %d\n", dev->devnum);
737 usbh_free_dev_urbs(dev);
739 for (i = 0; i < dev->driver_cnt; i++)
741 driver = dev->driver[i];
742 driver->disconnect(dev);
746 for (i = 0; i < USB_MAXCHILDREN; i++)
750 usbh_disconnect_device(child);
756 g_devmap &= ~(1 << dev->devnum);
760 usbh_free_device(dev);
776 dev->descriptor.bMaxPacketSize0 = 8;
778 for (devnum = 1; devnum < 31; devnum++)
780 if ((g_devmap & (1 << devnum)) == 0)
785 USB_error(
"Serious devnum error!\n");
789 g_devmap |= (1 << devnum);
790 dev->devnum = devnum;
799#define GET_TIMEOUT 1000
800#define SET_TIMEOUT 100
805 0, dev->devnum, 0,
NULL, 0, HZ * GET_TIMEOUT);
828 memset(buf, 0, size);
833 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
834 (type << 8) + index, 0, buf, size, HZ * GET_TIMEOUT)) > 0 || result ==
USB_ERR_PIPE)
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);
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,
867 0, ifnum, &type, 1, HZ * GET_TIMEOUT)) < 0)
874int usbh_set_protocol(
USB_DEV_T *dev,
int ifnum,
int protocol)
877 USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
878 protocol, ifnum,
NULL, 0, HZ * SET_TIMEOUT);
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,
886 (duration << 8) | report_id, ifnum,
NULL, 0, HZ * SET_TIMEOUT);
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,
922 &status,
sizeof(status), HZ * SET_TIMEOUT);
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;
997static int usbh_settle_configuration(
USB_DEV_T *dev)
1000 uint32_t cfgno, length;
1003 uint32_t stack_buff[384];
1005 if (dev->descriptor.bNumConfigurations > USB_MAXCONFIG)
1007 USB_warning(
"Too many configurations\n");
1011 if (dev->descriptor.bNumConfigurations != 1)
1013 USB_warning(
"This driver does not support multi-configuration devices!\n");
1017 buffer = (uint8_t *)&stack_buff[0];
1020 for (cfgno = 0; cfgno < dev->descriptor.bNumConfigurations; cfgno++)
1028 USB_error(
"Unable to get descriptor\n");
1031 USB_error(
"Config descriptor too short (expected %i, got %i)\n", 8, result);
1038 length = desc->wTotalLength;
1043 USB_error(
"Config descriptor is too large. Read 384 bytes only. This may cause lost of information!\n");
1050 USB_error(
"Couldn't get all of config descriptors\n");
1054 if (result < length)
1056 USB_error(
"Config descriptor too short (expected %d, got %d)\n", length, result);
1069 USB_error(
"Failed to set device %d default configuration (error=%d)\n", dev->devnum, result);
1074#ifdef DUMP_DEV_DESCRIPTORS
1075 usbh_dump_config_descriptor(desc);
1078 result = usb_parse_configuration(dev, desc, buffer);
1081 USB_warning(
"Descriptor data left\n");
1083 else if (result < 0)
1085 USB_warning(
"usb_parse_configuration error\n");
1094 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;
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);
1240 err = usbh_settle_configuration(dev);
1243 USB_error(
"Unable to get device %d configuration (error=%d)\n", dev->devnum, err);
1244 g_devmap &= ~(1 << dev->devnum);
1246 usbh_free_device(dev);
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);
1275 USBH->HcMiscControl &= ~0x8;
1286 usbh_init_hub_driver();
1288#ifdef SUPPORT_HID_CLASS
1291#ifdef SUPPORT_UMAS_CLASS
1294#ifdef SUPPORT_UVC_CLASS
1297#ifdef SUPPORT_UAC_CLASS
1300#ifdef SUPPORT_CDC_CLASS
1303 if (usbh_init_ohci() < 0)
1305 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;
1383 if (
USBH->HcRhPortStatus[0] & 0x4)
1384 USBH->HcRhPortStatus[0] = 0x8;
1385 if (
USBH->HcRhPortStatus[1] & 0x4)
1386 USBH->HcRhPortStatus[1] = 0x8;
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
#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
int usbh_get_device_descriptor(uint8_t *desc_buff)
Get device descriptor from the USB device.
#define USB_ERR_INPROGRESS
#define MAX_DRIVER_PER_DEV
int32_t USBH_RegisterDriver(USB_DRIVER_T *new_driver)
Register a device driver to USB Host Core driver.
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_GetDescriptor(USB_DEV_T *dev, uint8_t type, uint8_t index, void *buf, int size)
Get a descriptor from device.
int32_t USBH_Resume(void)
Resume USB Host controller and devices.
int32_t USBH_Close(void)
Disable USB Host controller function.
int32_t USBH_SubmitUrb(URB_T *urb)
Submit an URB to USB core for transfer data.
int32_t USBH_Suspend(void)
Suspend USB Host Controller and devices.
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.
int32_t USBH_SetInterface(USB_DEV_T *dev, char interface, char alternate)
Set USB device interface.
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.
int32_t USBH_ClearHalt(USB_DEV_T *dev, int pipe)
Clear the halt state of an endpoint.
int32_t USBH_SetConfiguration(USB_DEV_T *dev, int configuration)
Select USB device configuration.
HIDDEN_SYMBOLS struct usb_device USB_DEV_T
#define NULL
NULL pointer.
USB Host core driver header file.