| // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) |
| /* |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions, and the following disclaimer, |
| * without modification. |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in the |
| * documentation and/or other materials provided with the distribution. |
| * 3. The names of the above-listed copyright holders may not be used |
| * to endorse or promote products derived from this software without |
| * specific prior written permission. |
| * |
| * ALTERNATIVELY, this software may be distributed under the terms of the |
| * GNU General Public License ("GPL") as published by the Free Software |
| * Foundation; either version 2 of the License, or (at your option) any |
| * later version. |
| * |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| */ |
| |
| #include "diag_common.h" |
| #include "diag_misc.h" |
| #include "usb_memmap.h" |
| #include "core.h" |
| #include "com_type.h" |
| #include "string.h" |
| #include "core.h" |
| |
| #define USB_BUFSIZ 512 |
| #define isprint(a) ((a >=' ')&&(a <= '~')) |
| |
| char usb_started; |
| static struct usb_device usb_dev[USB_MAX_DEVICE]; |
| struct usb_device *pdev_usb = &usb_dev[0]; |
| static int dev_index; |
| |
| extern struct dwc2_hsotg dwc2_drv; |
| |
| static int usb_set_address(struct usb_device *dev) |
| { |
| debug("set address %d\n", dev->devnum); |
| |
| return urb_control(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS, |
| 0, (dev->devnum), 0, NULL, 0, USB_CNTL_TIMEOUT); |
| } |
| |
| int usb_device_data_init(struct usb_device **devp) |
| { |
| unsigned int i; |
| debug("New Device %d\n", dev_index); |
| if (dev_index == USB_MAX_DEVICE) { |
| printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE); |
| return -ENOSPC; |
| } |
| /* default Address is 0, real addresses start with 1 */ |
| usb_dev[dev_index].devnum = dev_index + 1; |
| usb_dev[dev_index].maxchild = 0; |
| for (i = 0; i < USB_MAXCHILDREN; i++) |
| usb_dev[dev_index].children[i] = NULL; |
| usb_dev[dev_index].parent = NULL; |
| usb_dev[dev_index].controller = NULL; |
| dev_index++; |
| *devp = &usb_dev[dev_index - 1]; |
| |
| return 0; |
| } |
| |
| int usb_stop(void) |
| { |
| if (usb_started) { |
| usb_started = 0; |
| dwc2_host_reset(dwc2_drv.regs); |
| } |
| return 0; |
| } |
| |
| int urb_control(struct usb_device *dev, unsigned int pipe, |
| unsigned char request, unsigned char requesttype, |
| unsigned short value, unsigned short index, |
| void *data, unsigned short size, int timeout) |
| { |
| struct devrequest *setup_packet = (struct devrequest *)USB_SETUP_PACKET_BUFF; |
| int err; |
| |
| setup_packet->requesttype = requesttype; |
| setup_packet->request = request; |
| setup_packet->value = cpu_to_le16(value); |
| setup_packet->index = cpu_to_le16(index); |
| setup_packet->length = cpu_to_le16(size); |
| debug("urb_control: request: 0x%X, requesttype: 0x%X, " \ |
| "value 0x%X index 0x%X length 0x%X\n", |
| request, requesttype, value, index, size); |
| dev->status = USB_ST_NOT_PROC; |
| |
| err = snd_ctl_msg(dev, pipe, data, size, setup_packet); |
| if (err < 0){ |
| dbg_printf(PRN_ERR, "%s, error:%d\n", __func__, err); |
| return err; |
| } |
| if (timeout == 0) { |
| dbg_printf(PRN_ERR, "%s, timeout-1, size:%d\n", __func__, size); |
| return (int)size; |
| } |
| |
| while (timeout--) { |
| if (!(dev->status & USB_ST_NOT_PROC)) |
| break; |
| mdelay(1); |
| } |
| |
| if (timeout == 0) { |
| dbg_printf(PRN_ERR, "%s, timeout-2\n"); |
| } |
| |
| dbg_printf(PRN_INFO, "%s, dev:%x,size:%d, status_addr:0x%x, len_addr:0x%x, status:0x%x, len:%d\n", |
| __func__, dev, sizeof(*dev), &(dev->status), &(dev->transfer_len), dev->status, dev->transfer_len); |
| |
| return dev->transfer_len; |
| |
| } |
| |
| int urb_bulk(struct usb_device *dev, unsigned int pipe, |
| void *data, int len, int *actual_length, int timeout) |
| { |
| if (len < 0) |
| return -EINVAL; |
| dev->status = USB_ST_NOT_PROC; |
| if (snd_blk_msg(dev, pipe, data, len) < 0) |
| return -EIO; |
| while (timeout--) { |
| if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) |
| break; |
| mdelay(1); |
| } |
| *actual_length = dev->transfer_len; |
| if (dev->status == 0) |
| return 0; |
| else |
| return -EIO; |
| } |
| |
| |
| int usb_maxpacket(struct usb_device *dev, unsigned long pipe) |
| { |
| if ((pipe & USB_DIR_IN) == 0) |
| return dev->epmaxpacketout[((pipe>>15) & 0xf)]; |
| else |
| return dev->epmaxpacketin[((pipe>>15) & 0xf)]; |
| } |
| |
| static int set_max_endpoint(struct usb_device *dev) |
| { |
| int i, j, b; |
| struct usb_endpoint_descriptor *ep; |
| u16 ep_wMaxPacketSize; |
| for (i = 0; i < dev->config.desc.bNumInterfaces; i++) |
| for (j = 0; j < dev->config.if_desc[i].desc.bNumEndpoints; j++){ |
| ep = &dev->config.if_desc[i].ep_desc[j]; |
| |
| b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| ep_wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize); |
| |
| if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| USB_ENDPOINT_XFER_CONTROL) { |
| dev->epmaxpacketout[b] = ep_wMaxPacketSize; |
| dev->epmaxpacketin[b] = ep_wMaxPacketSize; |
| debug("##Control EP epmaxpacketout/in[%d] = %d\n", |
| b, dev->epmaxpacketin[b]); |
| } else { |
| if ((ep->bEndpointAddress & 0x80) == 0) { |
| if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) { |
| dev->epmaxpacketout[b] = ep_wMaxPacketSize; |
| debug("##EP epmaxpacketout[%d] = %d\n", |
| b, dev->epmaxpacketout[b]); |
| } |
| } else { |
| if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) { |
| dev->epmaxpacketin[b] = ep_wMaxPacketSize; |
| debug("##EP epmaxpacketin[%d] = %d\n", |
| b, dev->epmaxpacketin[b]); |
| } |
| } |
| } |
| } |
| |
| return 0; |
| } |
| |
| static int usb_check_descriptor(struct usb_device *dev, |
| unsigned char *buffer, int cfgno) |
| { |
| struct usb_descriptor_header *head; |
| int index, ifno, epno, curr_if_num; |
| u16 ep_wMaxPacketSize; |
| struct usb_interface *if_desc = NULL; |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| |
| ifno = -1; |
| epno = -1; |
| curr_if_num = -1; |
| |
| dev->configno = cfgno; |
| head = (struct usb_descriptor_header *) &buffer[0]; |
| if (head->bDescriptorType != USB_DT_CONFIG) { |
| printf(" ERROR: NOT USB_CONFIG_DESC %x\n", |
| head->bDescriptorType); |
| return -EINVAL; |
| } |
| if (head->bLength != USB_DT_CONFIG_SIZE) { |
| printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength); |
| return -EINVAL; |
| } |
| memcpy(&dev->config, head, USB_DT_CONFIG_SIZE); |
| dev->config.no_of_if = 0; |
| |
| index = dev->config.desc.bLength; |
| head = (struct usb_descriptor_header *) &buffer[index]; |
| while (index + 1 < dev->config.desc.wTotalLength && head->bLength) { |
| switch (head->bDescriptorType) { |
| case USB_DT_INTERFACE: |
| if (head->bLength != USB_DT_INTERFACE_SIZE) { |
| printf("ERROR: Invalid USB IF length (%d)\n", |
| head->bLength); |
| break; |
| } |
| if (index + USB_DT_INTERFACE_SIZE > |
| dev->config.desc.wTotalLength) { |
| puts("USB IF descriptor overflowed buffer!\n"); |
| break; |
| } |
| if (((struct usb_interface_descriptor *) \ |
| head)->bInterfaceNumber != curr_if_num) { |
| ifno = dev->config.no_of_if; |
| if (ifno >= USB_MAXINTERFACES) { |
| puts("Too many USB interfaces!\n"); |
| return -EINVAL; |
| } |
| if_desc = &dev->config.if_desc[ifno]; |
| dev->config.no_of_if++; |
| memcpy(if_desc, head, |
| USB_DT_INTERFACE_SIZE); |
| if_desc->no_of_ep = 0; |
| if_desc->num_altsetting = 1; |
| curr_if_num = |
| if_desc->desc.bInterfaceNumber; |
| } else { |
| if (ifno >= 0) { |
| if_desc = &dev->config.if_desc[ifno]; |
| if_desc->num_altsetting++; |
| } |
| } |
| break; |
| case USB_DT_ENDPOINT: |
| if (head->bLength != USB_DT_ENDPOINT_SIZE && |
| head->bLength != USB_DT_ENDPOINT_AUDIO_SIZE) { |
| printf("ERROR: Invalid USB EP length (%d)\n", |
| head->bLength); |
| break; |
| } |
| if (index + head->bLength > |
| dev->config.desc.wTotalLength) { |
| puts("USB EP descriptor overflowed buffer!\n"); |
| break; |
| } |
| if (ifno < 0) { |
| puts("Endpoint descriptor out of order!\n"); |
| break; |
| } |
| epno = dev->config.if_desc[ifno].no_of_ep; |
| if_desc = &dev->config.if_desc[ifno]; |
| if (epno >= USB_MAXENDPOINTS) { |
| printf("Interface %d has too many endpoints!\n", |
| if_desc->desc.bInterfaceNumber); |
| return -EINVAL; |
| } |
| if_desc->no_of_ep++; |
| memcpy(&if_desc->ep_desc[epno], head, |
| USB_DT_ENDPOINT_SIZE); |
| ep_wMaxPacketSize = le16_to_cpu(dev->config.\ |
| if_desc[ifno].\ |
| ep_desc[epno].\ |
| wMaxPacketSize); |
| dev->config. \ |
| if_desc[ifno].\ |
| ep_desc[epno].\ |
| wMaxPacketSize = ep_wMaxPacketSize; |
| debug("if %d, ep %d, maxpacketSize:0x%x\n", ifno, epno, ep_wMaxPacketSize); |
| break; |
| case USB_DT_SS_ENDPOINT_COMP: |
| if (head->bLength != USB_DT_SS_EP_COMP_SIZE) { |
| printf("ERROR: Invalid USB EPC length (%d)\n", |
| head->bLength); |
| break; |
| } |
| if (index + USB_DT_SS_EP_COMP_SIZE > |
| dev->config.desc.wTotalLength) { |
| puts("USB EPC descriptor overflowed buffer!\n"); |
| break; |
| } |
| if (ifno < 0 || epno < 0) { |
| puts("EPC descriptor out of order!\n"); |
| break; |
| } |
| if_desc = &dev->config.if_desc[ifno]; |
| memcpy(&if_desc->ss_ep_comp_desc[epno], head, |
| USB_DT_SS_EP_COMP_SIZE); |
| break; |
| default: |
| if (head->bLength == 0) |
| return -EINVAL; |
| |
| debug("unknown Description Type : %x\n", |
| head->bDescriptorType); |
| break; |
| } |
| index += head->bLength; |
| head = (struct usb_descriptor_header *)&buffer[index]; |
| } |
| return 0; |
| } |
| |
| |
| static int get_descriptor(struct usb_device *dev, unsigned char type, |
| void *buf, int size) |
| { |
| return urb_control(dev, usb_rcvctrlpipe(dev, 0), |
| USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| (type << 8), 0, buf, size, |
| USB_CNTL_TIMEOUT); |
| } |
| |
| int usb_get_configuration_len(struct usb_device *dev) |
| { |
| int result; |
| unsigned char * buffer = USB_memalloc(9); |
| struct usb_config_descriptor *config; |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| |
| config = (struct usb_config_descriptor *)&buffer[0]; |
| result = get_descriptor(dev, USB_DT_CONFIG, buffer, 9); |
| USB_memfree(buffer); |
| if (result < 9) { |
| if (result < 0) |
| printf("unable to get descriptor, error %lX\n", |
| dev->status); |
| else |
| printf("config descriptor too short " \ |
| "(expected %i, got %i)\n", 9, result); |
| return -EIO; |
| } |
| return le16_to_cpu(config->wTotalLength); |
| } |
| |
| int usb_get_configuration_no(struct usb_device *dev, |
| unsigned char *buffer, int length) |
| { |
| int result; |
| struct usb_config_descriptor *config; |
| |
| config = (struct usb_config_descriptor *)&buffer[0]; |
| result = get_descriptor(dev, USB_DT_CONFIG, buffer, length); |
| debug("Result %d, wLength %d\n", result, |
| le16_to_cpu(config->wTotalLength)); |
| config->wTotalLength = result; |
| |
| return result; |
| } |
| |
| int set_interface(struct usb_device *dev, int interface, int alternate) |
| { |
| struct usb_interface *if_face = NULL; |
| int ret, i; |
| |
| for (i = 0; i < dev->config.desc.bNumInterfaces; i++) { |
| if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) { |
| if_face = &dev->config.if_desc[i]; |
| break; |
| } |
| } |
| if (!if_face) { |
| printf("selecting invalid interface %d", interface); |
| return -EINVAL; |
| } |
| |
| ret = urb_control(dev, usb_sndctrlpipe(dev, 0), |
| USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, |
| alternate, interface, NULL, 0, |
| USB_CNTL_TIMEOUT * 5); |
| if (ret < 0) |
| return ret; |
| |
| return 0; |
| } |
| |
| static int set_configuration(struct usb_device *dev, int configuration) |
| { |
| int res; |
| debug("set configuration %d\n", configuration); |
| res = urb_control(dev, usb_sndctrlpipe(dev, 0), |
| USB_REQ_SET_CONFIGURATION, 0, |
| configuration, 0, |
| NULL, 0, USB_CNTL_TIMEOUT); |
| if (res == 0) { |
| dev->toggle[0] = 0; |
| dev->toggle[1] = 0; |
| return 0; |
| } else |
| return -EIO; |
| } |
| |
| static int get_string_descriptor(struct usb_device *dev, unsigned short langid, |
| unsigned char index, void *buf, int size) |
| { |
| int i; |
| int result; |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| for (i = 0; i < 3; ++i) { |
| /* some devices are flaky */ |
| result = urb_control(dev, usb_rcvctrlpipe(dev, 0), |
| USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| (USB_DT_STRING << 8) + index, langid, buf, size, |
| USB_CNTL_TIMEOUT); |
| |
| if (result > 0) |
| break; |
| } |
| |
| return result; |
| } |
| |
| static int usb_data_u2c_sub(struct usb_device *dev, unsigned int langid, |
| unsigned int index, unsigned char *buf) |
| { |
| int rc, newlength; |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| rc = get_string_descriptor(dev, langid, index, buf, 255); |
| |
| if (rc < 2) { |
| rc = get_string_descriptor(dev, langid, index, buf, 2); |
| if (rc == 2) |
| rc = get_string_descriptor(dev, langid, index, buf, buf[0]); |
| } |
| |
| if (rc >= 2) { |
| if (!buf[0] && !buf[1]){ |
| for (newlength = 2; newlength + 1 < rc; newlength += 2) |
| if (!isprint(buf[newlength]) || buf[newlength + 1]) |
| break; |
| if (newlength > 2) { |
| buf[0] = newlength; |
| rc = newlength; |
| } |
| } |
| |
| if (buf[0] < rc) |
| rc = buf[0]; |
| |
| rc = rc - (rc & 1); |
| } |
| |
| if (rc < 2) |
| rc = -EINVAL; |
| |
| return rc; |
| } |
| |
| int usb_descriptor_u2c(struct usb_device *dev, int index, char *buf, size_t size) |
| { |
| unsigned char * mybuf = USB_memalloc(USB_BUFSIZ); |
| |
| unsigned char *tbuf; |
| int err; |
| unsigned int u, idx; |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| if (size <= 0 || !buf || !index) { |
| USB_memfree(mybuf); |
| return -EINVAL; |
| } |
| buf[0] = 0; |
| tbuf = &mybuf[0]; |
| |
| if (!dev->have_langid) { |
| err = usb_data_u2c_sub(dev, 0, 0, tbuf); |
| if (err < 0) { |
| debug("error getting string descriptor 0 " \ |
| "(error=%lx)\n", dev->status); |
| USB_memfree(mybuf); |
| return -EIO; |
| } else if (tbuf[0] < 4) { |
| debug("string descriptor 0 too short\n"); |
| USB_memfree(mybuf); |
| return -EIO; |
| } else { |
| dev->have_langid = -1; |
| dev->string_langid = tbuf[2] | (tbuf[3] << 8); |
| debug("USB device number %d default " \ |
| "language ID 0x%x\n", |
| dev->devnum, dev->string_langid); |
| } |
| } |
| |
| err = usb_data_u2c_sub(dev, dev->string_langid, index, tbuf); |
| if (err < 0) |
| { |
| USB_memfree(mybuf); |
| return err; |
| } |
| |
| size--; |
| for (idx = 0, u = 2; u < (unsigned int)err; u += 2) { |
| if (idx >= size) |
| break; |
| if (tbuf[u+1]) |
| buf[idx++] = '?'; |
| else |
| buf[idx++] = tbuf[u]; |
| } |
| buf[idx] = 0; |
| err = idx; |
| USB_memfree(mybuf); |
| return err; |
| } |
| |
| static int get_sized_descriptor(struct usb_device *dev, int len, int expect_len) |
| { |
| struct usb_device_descriptor *desc; |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| unsigned char * tmpbuf = USB_memalloc(USB_BUFSIZ); |
| int err; |
| |
| desc = (struct usb_device_descriptor *)tmpbuf; |
| |
| err = get_descriptor(dev, USB_DT_DEVICE, desc, len); |
| if (err < expect_len) { |
| USB_memfree(tmpbuf); |
| if (err < 0) { |
| printf("unable to get device descriptor (error=%d)\n", |
| err); |
| return err; |
| } |
| } |
| memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor)); |
| |
| USB_memfree(tmpbuf); |
| return 0; |
| } |
| |
| static int usb_descriptor_init(struct usb_device *dev) |
| { |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| if (dev->speed == USB_SPEED_LOW) { |
| dev->descriptor.bMaxPacketSize0 = 8; |
| dev->maxpacketsize = PACKET_SIZE_8; |
| } else { |
| dev->descriptor.bMaxPacketSize0 = 64; |
| dev->maxpacketsize = PACKET_SIZE_64; |
| } |
| dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0; |
| dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0; |
| switch (dev->descriptor.bMaxPacketSize0) { |
| case 8: |
| dev->maxpacketsize = PACKET_SIZE_8; |
| break; |
| case 16: |
| dev->maxpacketsize = PACKET_SIZE_16; |
| break; |
| case 32: |
| dev->maxpacketsize = PACKET_SIZE_32; |
| break; |
| case 64: |
| dev->maxpacketsize = PACKET_SIZE_64; |
| break; |
| default: |
| printf("%s: invalid max packet size\n", __func__); |
| return -EIO; |
| } |
| |
| return 0; |
| } |
| |
| int usb_enum_device() |
| { |
| int err; |
| unsigned char *tmpbuf = NULL; |
| struct usb_device *dev = NULL; |
| //int addr; |
| int start_index = 0; |
| |
| start_index = dev_index; |
| err = usb_device_data_init(&dev); |
| if (err) { |
| puts("alloc new device failed\n"); |
| return -1; |
| } |
| |
| err = usb_descriptor_init(dev); |
| if (err) |
| return err; |
| |
| err = usb_set_address(dev); |
| if (err < 0) { |
| printf("\n USB device not accepting new address " \ |
| "(error=%lX)\n", dev->status); |
| return err; |
| } |
| |
| mdelay(10); /* Let the SET_ADDRESS settle */ |
| |
| err = get_sized_descriptor(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE); |
| if (err) |
| return err; |
| |
| mdelay(1); |
| |
| err = usb_get_configuration_len(dev); |
| if (err >= 0) { |
| tmpbuf = (unsigned char *)USB_memalloc(err); |
| if (!tmpbuf) |
| err = -ENOMEM; |
| else |
| err = usb_get_configuration_no(dev, tmpbuf, err); |
| } |
| if (err < 0) { |
| printf("usb_new_device: Cannot read configuration, " \ |
| "skipping device %04x:%04x\n", |
| dev->descriptor.idVendor, dev->descriptor.idProduct); |
| free(tmpbuf); |
| return err; |
| } |
| usb_check_descriptor(dev, tmpbuf, 0); |
| USB_memfree(tmpbuf); |
| set_max_endpoint(dev); |
| |
| err = set_configuration(dev, dev->config.desc.bConfigurationValue); |
| if (err < 0) { |
| printf("failed to set default configuration " \ |
| "len %d, status %lX\n", dev->transfer_len, dev->status); |
| return err; |
| } |
| |
| mdelay(10); |
| |
| debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", |
| dev->descriptor.iManufacturer, dev->descriptor.iProduct, |
| dev->descriptor.iSerialNumber); |
| memset(dev->mf, 0, sizeof(dev->mf)); |
| memset(dev->prod, 0, sizeof(dev->prod)); |
| memset(dev->serial, 0, sizeof(dev->serial)); |
| if (dev->descriptor.iManufacturer) |
| usb_descriptor_u2c(dev, dev->descriptor.iManufacturer, |
| dev->mf, sizeof(dev->mf)); |
| if (dev->descriptor.iProduct) |
| usb_descriptor_u2c(dev, dev->descriptor.iProduct, |
| dev->prod, sizeof(dev->prod)); |
| if (dev->descriptor.iSerialNumber) |
| usb_descriptor_u2c(dev, dev->descriptor.iSerialNumber, |
| dev->serial, sizeof(dev->serial)); |
| printf("Manufacturer %s\n", dev->mf); |
| printf("Product %s\n", dev->prod); |
| printf("SerialNumber %s\n", dev->serial); |
| printf("%d USB Device(s) found\n", |
| dev_index - start_index); |
| |
| if (dev->config.if_desc[0].desc.bInterfaceClass != USB_CLASS_MASS_STORAGE && |
| dev->descriptor.bDeviceClass != USB_CLASS_HUB){ |
| printf("Product:%s is not a mass storage device or a HUB\n", dev->prod); |
| usb_free_device(dev->controller); |
| return -1; |
| } |
| |
| /* Now probe if the device is a hub */ |
| err = usb_hub_probe(dev, 0); |
| if (err < 0) |
| return err; |
| |
| |
| return 0; |
| |
| } |
| |
| int usb_host_init(void) |
| { |
| int retval, i; |
| struct dwc2_hsotg *hsotg = &dwc2_drv; |
| |
| debug("%s,%d\n",__func__,__LINE__); |
| |
| /* first make all devices unknown */ |
| for (i = 0; i < USB_MAX_DEVICE; i++) { |
| memset(&usb_dev[i], 0, sizeof(struct usb_device)); |
| usb_dev[i].devnum = -1; |
| } |
| |
| dwc2_init(hsotg); |
| |
| retval = dwc2_hw_init(hsotg); |
| if (retval) { /* Other error. */ |
| dbg_printf(PRN_RES,"dwc2 init failed\n"); |
| return -EINVAL; |
| } |
| |
| dbg_printf(PRN_RES,"Wait Device detection, plug in device now...\n"); |
| while (!dwc2_wait_device_detection()){ |
| // Wait for device to be connected. |
| } |
| dbg_printf(PRN_RES, "Device ATTACHED!\n"); |
| |
| retval = usb_enum_device(); |
| if (retval) { |
| printf("No USB Device found\n"); |
| return -1; |
| } |
| pdev_usb = &usb_dev[dev_index -1]; |
| usb_started = 1; |
| |
| debug("scan end\n"); |
| |
| return usb_started ? 0 : -ENODEV; |
| } |
| |
| void usb_find_usb2_hub_address_port(struct usb_device *udev, |
| uint8_t *hub_address, uint8_t *hub_port) |
| { |
| /* Find out the nearest parent which is high speed */ |
| while (udev->parent->parent != NULL) |
| if (udev->parent->speed != USB_SPEED_HIGH) { |
| udev = udev->parent; |
| } else { |
| *hub_address = udev->parent->devnum; |
| *hub_port = udev->portnr; |
| return; |
| } |
| |
| printf("Error: Cannot find high speed parent of usb-1 device\n"); |
| *hub_address = 0; |
| *hub_port = 0; |
| } |
| |
| bool usb_device_has_child_on_port(struct usb_device *parent, int port) |
| { |
| return parent->children[port] != NULL; |
| } |
| |
| |
| |
| |
| static int usb_setup_descriptor(struct usb_device *dev, bool do_read) |
| { |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| if (dev->speed == USB_SPEED_LOW) { |
| dev->descriptor.bMaxPacketSize0 = 8; |
| dev->maxpacketsize = PACKET_SIZE_8; |
| } else { |
| dev->descriptor.bMaxPacketSize0 = 64; |
| dev->maxpacketsize = PACKET_SIZE_64; |
| } |
| dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0; |
| dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0; |
| |
| if (do_read){ |
| int err; |
| err = get_sized_descriptor(dev, 64, 8); |
| if (err) |
| return err; |
| } |
| |
| dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0; |
| dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0; |
| switch (dev->descriptor.bMaxPacketSize0) { |
| case 8: |
| dev->maxpacketsize = PACKET_SIZE_8; |
| break; |
| case 16: |
| dev->maxpacketsize = PACKET_SIZE_16; |
| break; |
| case 32: |
| dev->maxpacketsize = PACKET_SIZE_32; |
| break; |
| case 64: |
| dev->maxpacketsize = PACKET_SIZE_64; |
| break; |
| default: |
| printf("%s: invalid max packet size\n", __func__); |
| return -EIO; |
| } |
| |
| return 0; |
| } |
| |
| static int usb_prepare_device(struct usb_device *dev, int addr, bool do_read, |
| struct usb_device *parent) |
| { |
| int err; |
| UNUSED(parent); |
| dbg_printf(PRN_RES,"devaddr:%d\n"); |
| |
| dbg_printf(PRN_INFO | PRN_BUFFERED, "%s\n", __func__); |
| /* |
| * Allocate usb 3.0 device context. |
| * USB 3.0 (xHCI) protocol tries to allocate device slot |
| * and related data structures first. This call does that. |
| * Refer to sec 4.3.2 in xHCI spec rev1.0 |
| */ |
| err = usb_setup_descriptor(dev, do_read); |
| if (err) |
| return err; |
| |
| dev->devnum = addr; |
| |
| err = usb_set_address(dev); /* set address */ |
| |
| if (err < 0) { |
| printf("\n USB device not accepting new address " \ |
| "(error=%lX)\n", dev->status); |
| return err; |
| } |
| |
| mdelay(10); /* Let the SET_ADDRESS settle */ |
| |
| if (!do_read) { |
| err = usb_setup_descriptor(dev, true); |
| if (err) |
| return err; |
| } |
| |
| return 0; |
| } |
| |
| int usb_select_config(struct usb_device *dev) |
| { |
| unsigned char *tmpbuf = NULL; |
| int err; |
| err = get_sized_descriptor(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE); |
| if (err) |
| return err; |
| |
| /* |
| * Kingston DT Ultimate 32GB USB 3.0 seems to be extremely sensitive |
| * about this first Get Descriptor request. If there are any other |
| * requests in the first microframe, the stick crashes. Wait about |
| * one microframe duration here (1mS for USB 1.x , 125uS for USB 2.0). |
| */ |
| mdelay(1); |
| |
| /* only support for one config for now */ |
| err = usb_get_configuration_len(dev); |
| if (err >= 0) { |
| tmpbuf = (unsigned char *)USB_memalloc(err); |
| if (!tmpbuf) |
| err = -ENOMEM; |
| else |
| err = usb_get_configuration_no(dev, tmpbuf, err); |
| } |
| if (err < 0) { |
| printf("usb_new_device: Cannot read configuration, " \ |
| "skipping device %04x:%04x\n", |
| dev->descriptor.idVendor, dev->descriptor.idProduct); |
| free(tmpbuf); |
| return err; |
| } |
| usb_check_descriptor(dev, tmpbuf, 0); |
| USB_memfree(tmpbuf); |
| set_max_endpoint(dev); |
| /* |
| * we set the default configuration here |
| * This seems premature. If the driver wants a different configuration |
| * it will need to select itself. |
| */ |
| err = set_configuration(dev, dev->config.desc.bConfigurationValue); |
| if (err < 0) { |
| printf("failed to set default configuration " \ |
| "len %d, status %lX\n", dev->transfer_len, dev->status); |
| return err; |
| } |
| |
| /* |
| * Wait until the Set Configuration request gets processed by the |
| * device. This is required by at least SanDisk Cruzer Pop USB 2.0 |
| * and Kingston DT Ultimate 32GB USB 3.0 on DWC2 OTG controller. |
| */ |
| mdelay(10); |
| |
| debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", |
| dev->descriptor.iManufacturer, dev->descriptor.iProduct, |
| dev->descriptor.iSerialNumber); |
| memset(dev->mf, 0, sizeof(dev->mf)); |
| memset(dev->prod, 0, sizeof(dev->prod)); |
| memset(dev->serial, 0, sizeof(dev->serial)); |
| if (dev->descriptor.iManufacturer) |
| usb_descriptor_u2c(dev, dev->descriptor.iManufacturer, |
| dev->mf, sizeof(dev->mf)); |
| if (dev->descriptor.iProduct) |
| usb_descriptor_u2c(dev, dev->descriptor.iProduct, |
| dev->prod, sizeof(dev->prod)); |
| if (dev->descriptor.iSerialNumber) |
| usb_descriptor_u2c(dev, dev->descriptor.iSerialNumber, |
| dev->serial, sizeof(dev->serial)); |
| printf("Manufacturer %s\n", dev->mf); |
| printf("Product %s\n", dev->prod); |
| printf("SerialNumber %s\n", dev->serial); |
| |
| if (dev->config.if_desc[0].desc.bInterfaceClass != USB_CLASS_MASS_STORAGE){ |
| printf("Product:%s is not a mass storage device\n", dev->prod); |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| int usb_setup_device(struct usb_device *dev, bool do_read, |
| struct usb_device *parent) |
| { |
| int addr; |
| int ret; |
| |
| dbg_printf(PRN_INFO, "%s\n", __func__); |
| /* We still haven't set the Address yet */ |
| addr = dev->devnum; |
| dev->devnum = 0; |
| |
| ret = usb_prepare_device(dev, addr, do_read, parent); |
| if (ret) |
| return ret; |
| ret = usb_select_config(dev); |
| |
| return ret; |
| } |
| |
| /* |
| * By the time we get here, the device has gotten a new device ID |
| * and is in the default state. We need to identify the thing and |
| * get the ball rolling.. |
| * |
| * Returns 0 for success, != 0 for error. |
| */ |
| |
| int usb_new_device(struct usb_device *dev) |
| { |
| bool do_read = true; |
| int err; |
| |
| err = usb_setup_device(dev, do_read, dev->parent); |
| if (err) |
| return err; |
| |
| /* Now probe if the device is a hub */ |
| err = usb_hub_probe(dev, 0); |
| if (err < 0) |
| return err; |
| |
| return 0; |
| } |
| |
| /* |
| * Free the newly created device node. |
| * Called in error cases where configuring a newly attached |
| * device fails for some reason. |
| */ |
| void usb_free_device(struct usb_device *controller) |
| { |
| UNUSED(controller); |
| dev_index--; |
| debug("Freeing device node: %d\n", dev_index); |
| memset(&usb_dev[dev_index], 0, sizeof(struct usb_device)); |
| usb_dev[dev_index].devnum = -1; |
| } |
| |