7 #include <linux/module.h>
8 #include <linux/slab.h>
12 #include <linux/ctype.h>
14 #include <linux/device.h>
18 #include <asm/byteorder.h>
22 static void cancel_async_set_config(
struct usb_device *
udev);
29 static void usb_api_blocking_completion(
struct urb *
urb)
44 static int usb_start_wait_urb(
struct urb *
urb,
int timeout,
int *actual_length)
50 init_completion(&
ctx.done);
52 urb->actual_length = 0;
63 "%s timed out on ep%d%s len=%u/%u\n",
65 usb_endpoint_num(&urb->ep->desc),
66 usb_urb_dir_in(urb) ?
"in" :
"out",
68 urb->transfer_buffer_length);
73 *actual_length = urb->actual_length;
81 static int usb_internal_control_msg(
struct usb_device *usb_dev,
84 void *
data,
int len,
int timeout)
94 usb_fill_control_urb(urb, usb_dev, pipe, (
unsigned char *)cmd, data,
95 len, usb_api_blocking_completion,
NULL);
97 retv = usb_start_wait_urb(urb, timeout, &length);
149 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
184 void *data,
int len,
int *actual_length,
int timeout)
186 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
222 void *data,
int len,
int *actual_length,
int timeout)
225 struct usb_host_endpoint *ep;
227 ep = usb_pipe_endpoint(usb_dev, pipe);
237 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
238 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
239 usb_api_blocking_completion,
NULL,
242 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
243 usb_api_blocking_completion,
NULL);
245 return usb_start_wait_urb(urb, timeout, actual_length);
251 static void sg_clean(
struct usb_sg_request *
io)
254 while (io->entries--)
262 static void sg_complete(
struct urb *urb)
264 struct usb_sg_request *io = urb->context;
267 spin_lock(&io->lock);
282 && urb->actual_length) {
283 dev_err(io->dev->bus->controller,
284 "dev %s ep%d%s scatterlist error %d/%d\n",
286 usb_endpoint_num(&urb->ep->desc),
287 usb_urb_dir_in(urb) ?
"in" :
"out",
292 if (io->status == 0 && status && status != -
ECONNRESET) {
301 spin_unlock(&io->lock);
302 for (i = 0, found = 0; i < io->entries; i++) {
303 if (!io->urbs [i] || !io->urbs [i]->dev)
312 "%s, unlink --> %d\n",
314 }
else if (urb == io->urbs [i])
317 spin_lock(&io->lock);
321 io->bytes += urb->actual_length;
326 spin_unlock(&io->lock);
357 int nents,
size_t length,
gfp_t mem_flags)
363 if (!io || !dev || !sg
364 || usb_pipecontrol(pipe)
365 || usb_pipeisoc(pipe)
373 if (dev->bus->sg_tablesize > 0) {
382 io->urbs =
kmalloc(io->entries *
sizeof *io->urbs, mem_flags);
386 urb_flags = URB_NO_INTERRUPT;
387 if (usb_pipein(pipe))
388 urb_flags |= URB_SHORT_NOT_OK;
404 urb->transfer_flags = urb_flags;
405 urb->complete = sg_complete;
411 urb->transfer_buffer =
NULL;
412 urb->num_sgs = nents;
429 if (!PageHighMem(sg_page(sg)))
430 urb->transfer_buffer = sg_virt(sg);
432 urb->transfer_buffer =
NULL;
436 len =
min_t(
size_t, len, length);
442 urb->transfer_buffer_length = len;
444 io->urbs[--
i]->transfer_flags &= ~URB_NO_INTERRUPT;
447 io->count = io->entries;
450 init_completion(&io->complete);
508 spin_lock_irq(&io->lock);
510 while (i < entries && !io->status) {
513 io->urbs[
i]->dev = io->dev;
519 spin_unlock_irq(&io->lock);
543 dev_dbg(&io->dev->dev,
"%s, submit --> %d\n",
547 spin_lock_irq(&io->lock);
548 if (retval && (io->status == 0 || io->status == -
ECONNRESET))
551 io->count -= entries -
i;
554 spin_unlock_irq(&io->lock);
585 spin_unlock(&io->lock);
586 for (i = 0; i < io->entries; i++) {
589 if (!io->urbs [i]->dev)
596 dev_warn(&io->dev->dev,
"%s, unlink --> %d\n",
599 spin_lock(&io->lock);
601 spin_unlock_irqrestore(&io->lock, flags);
637 for (i = 0; i < 3; ++
i) {
641 (type << 8) + index, 0, buf, size,
642 USB_CTRL_GET_TIMEOUT);
645 if (result > 1 && ((
u8 *)buf)[1] != type) {
677 static int usb_get_string(
struct usb_device *
dev,
unsigned short langid,
683 for (i = 0; i < 3; ++
i) {
688 USB_CTRL_GET_TIMEOUT);
689 if (result == 0 || result == -
EPIPE)
700 static void usb_try_string_workarounds(
unsigned char *buf,
int *length)
702 int newlength, oldlength = *
length;
704 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
705 if (!
isprint(buf[newlength]) || buf[newlength + 1])
714 static int usb_string_sub(
struct usb_device *dev,
unsigned int langid,
715 unsigned int index,
unsigned char *buf)
724 rc = usb_get_string(dev, langid, index, buf, 255);
729 rc = usb_get_string(dev, langid, index, buf, 2);
731 rc = usb_get_string(dev, langid, index, buf, buf[0]);
735 if (!buf[0] && !buf[1])
736 usb_try_string_workarounds(buf, &rc);
746 rc = (rc < 0 ? rc : -
EINVAL);
751 static int usb_get_langid(
struct usb_device *dev,
unsigned char *tbuf)
755 if (dev->have_langid)
758 if (dev->string_langid < 0)
761 err = usb_string_sub(dev, 0, 0, tbuf);
765 if (err == -
ENODATA || (err > 0 && err < 4)) {
766 dev->string_langid = 0x0409;
767 dev->have_langid = 1;
769 "string descriptor 0 malformed (err = %d), "
770 "defaulting to 0x%04x\n",
771 err, dev->string_langid);
779 dev_err(&dev->dev,
"string descriptor 0 read error: %d\n",
781 dev->string_langid = -1;
786 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
787 dev->have_langid = 1;
788 dev_dbg(&dev->dev,
"default language 0x%04x\n",
810 int usb_string(
struct usb_device *dev,
int index,
char *buf,
size_t size)
817 if (size <= 0 || !buf || !index)
824 err = usb_get_langid(dev, tbuf);
828 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
839 "wrong descriptor type %02x for string %d (\"%s\")\n",
840 tbuf[1], index, buf);
849 #define MAX_USB_STRING_SIZE (127 * 3 + 1)
862 char *smallbuf =
NULL;
875 memcpy(smallbuf, buf, len);
905 if (size >
sizeof(*desc))
913 memcpy(&dev->descriptor, desc, size);
950 sizeof(*status), USB_CTRL_GET_TIMEOUT);
952 *(
u16 *)data = *status;
984 int endp = usb_pipeendpoint(pipe);
986 if (usb_pipein(pipe))
996 USB_CTRL_SET_TIMEOUT);
1018 struct usb_device *
udev = interface_to_usbdev(intf);
1019 struct usb_host_interface *alt = intf->cur_altsetting;
1022 if (intf->ep_devs_created || intf->unregistering)
1025 for (i = 0; i < alt->desc.bNumEndpoints; ++
i)
1027 intf->ep_devs_created = 1;
1033 struct usb_host_interface *alt = intf->cur_altsetting;
1036 if (!intf->ep_devs_created)
1039 for (i = 0; i < alt->desc.bNumEndpoints; ++
i)
1041 intf->ep_devs_created = 0;
1057 bool reset_hardware)
1060 struct usb_host_endpoint *ep;
1065 if (usb_endpoint_out(epaddr)) {
1066 ep = dev->ep_out[epnum];
1068 dev->ep_out[epnum] =
NULL;
1070 ep = dev->ep_in[epnum];
1072 dev->ep_in[epnum] =
NULL;
1094 struct usb_host_endpoint *ep;
1096 if (usb_endpoint_out(epaddr))
1097 ep = dev->ep_out[epnum];
1099 ep = dev->ep_in[epnum];
1116 bool reset_hardware)
1118 struct usb_host_interface *alt = intf->cur_altsetting;
1121 for (i = 0; i < alt->desc.bNumEndpoints; ++
i) {
1123 alt->endpoint[i].desc.bEndpointAddress,
1141 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1146 if (dev->actconfig) {
1152 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
1153 dev->actconfig->interface[i]->unregistering = 1;
1155 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1159 interface = dev->actconfig->interface[i];
1160 if (!device_is_registered(&interface->dev))
1162 dev_dbg(&dev->dev,
"unregistering interface %s\n",
1163 dev_name(&interface->dev));
1164 remove_intf_ep_devs(interface);
1171 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1172 put_device(&dev->actconfig->interface[i]->dev);
1173 dev->actconfig->interface[
i] =
NULL;
1177 dev->actconfig =
NULL;
1182 dev_dbg(&dev->dev,
"%s nuking %s URBs\n", __func__,
1183 skip_ep0 ?
"non-ep0" :
"all");
1184 if (hcd->driver->check_bandwidth) {
1186 for (i = skip_ep0; i < 16; ++
i) {
1196 for (i = skip_ep0; i < 16; ++
i) {
1214 int epnum = usb_endpoint_num(&ep->desc);
1215 int is_out = usb_endpoint_dir_out(&ep->desc);
1216 int is_control = usb_endpoint_xfer_control(&ep->desc);
1220 if (is_out || is_control)
1221 dev->ep_out[epnum] = ep;
1222 if (!is_out || is_control)
1223 dev->ep_in[epnum] = ep;
1238 struct usb_host_interface *alt = intf->cur_altsetting;
1241 for (i = 0; i < alt->desc.bNumEndpoints; ++
i)
1281 struct usb_host_interface *alt;
1282 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1285 unsigned int epaddr;
1293 dev_dbg(&dev->dev,
"selecting invalid interface %d\n",
1297 if (iface->unregistering)
1302 dev_warn(&dev->dev,
"selecting invalid altsetting %d\n",
1315 dev_err(&iface->dev,
"%s Failed to disable LPM\n.", __func__);
1321 dev_info(&dev->dev,
"Not enough bandwidth for altsetting %d\n",
1333 alternate, interface,
NULL, 0, 5000);
1338 if (ret == -
EPIPE && iface->num_altsetting == 1) {
1340 "manual set_interface for iface %d, alt %d\n",
1341 interface, alternate);
1343 }
else if (ret < 0) {
1359 if (iface->cur_altsetting != alt) {
1360 remove_intf_ep_devs(iface);
1365 iface->cur_altsetting = alt;
1378 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1379 epaddr = alt->endpoint[
i].desc.bEndpointAddress;
1380 pipe = __create_pipe(dev,
1382 (usb_endpoint_out(epaddr) ?
1401 if (device_is_registered(&iface->dev)) {
1403 create_intf_ep_devs(iface);
1434 struct usb_host_config *
config;
1435 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1445 for (i = 1; i < 16; ++
i) {
1450 config = dev->actconfig;
1457 dev_err(&dev->dev,
"%s Failed to disable LPM\n.", __func__);
1462 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1464 struct usb_host_interface *alt;
1468 alt = &intf->altsetting[0];
1469 if (alt != intf->cur_altsetting)
1471 intf->cur_altsetting, alt);
1478 for (i--; i >= 0; i--) {
1480 struct usb_host_interface *alt;
1484 alt = &intf->altsetting[0];
1485 if (alt != intf->cur_altsetting)
1487 alt, intf->cur_altsetting);
1495 config->desc.bConfigurationValue, 0,
1496 NULL, 0, USB_CTRL_SET_TIMEOUT);
1498 goto reset_old_alts;
1502 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1504 struct usb_host_interface *alt;
1514 alt = &intf->altsetting[0];
1516 if (alt != intf->cur_altsetting) {
1517 remove_intf_ep_devs(intf);
1520 intf->cur_altsetting = alt;
1522 if (device_is_registered(&intf->dev)) {
1524 create_intf_ep_devs(intf);
1533 static void usb_release_interface(
struct device *dev)
1536 struct usb_interface_cache *intfc =
1537 altsetting_to_usb_interface_cache(intf->altsetting);
1543 #ifdef CONFIG_HOTPLUG
1546 struct usb_device *usb_dev;
1548 struct usb_host_interface *alt;
1550 intf = to_usb_interface(dev);
1551 usb_dev = interface_to_usbdev(intf);
1552 alt = intf->cur_altsetting;
1555 alt->desc.bInterfaceClass,
1556 alt->desc.bInterfaceSubClass,
1557 alt->desc.bInterfaceProtocol))
1562 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
1566 usb_dev->descriptor.bDeviceClass,
1567 usb_dev->descriptor.bDeviceSubClass,
1568 usb_dev->descriptor.bDeviceProtocol,
1569 alt->desc.bInterfaceClass,
1570 alt->desc.bInterfaceSubClass,
1571 alt->desc.bInterfaceProtocol,
1572 alt->desc.bInterfaceNumber))
1587 .name =
"usb_interface",
1588 .release = usb_release_interface,
1589 .uevent = usb_if_uevent,
1593 struct usb_host_config *
config,
1602 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[
i]); i++) {
1603 intf_assoc = config->intf_assoc[
i];
1609 if (inum >= first_intf && inum <= last_intf) {
1611 retval = intf_assoc;
1613 dev_err(&dev->dev,
"Interface #%d referenced"
1614 " by multiple IADs\n", inum);
1644 static void __usb_queue_reset_device(
struct work_struct *
ws)
1649 struct usb_device *udev = interface_to_usbdev(iface);
1653 iface->reset_running = 1;
1655 iface->reset_running = 0;
1656 usb_unlock_device(udev);
1709 struct usb_host_config *
cp =
NULL;
1711 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1714 if (dev->authorized == 0 || configuration == -1)
1717 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1718 if (dev->config[i].desc.bConfigurationValue ==
1720 cp = &dev->config[
i];
1725 if ((!cp && configuration != 0))
1733 if (cp && configuration == 0)
1734 dev_warn(&dev->dev,
"config 0 descriptor??\n");
1740 nintf = cp->desc.bNumInterfaces;
1741 new_interfaces =
kmalloc(nintf *
sizeof(*new_interfaces),
1743 if (!new_interfaces) {
1744 dev_err(&dev->dev,
"Out of memory\n");
1748 for (; n < nintf; ++
n) {
1749 new_interfaces[
n] = kzalloc(
1752 if (!new_interfaces[n]) {
1753 dev_err(&dev->dev,
"Out of memory\n");
1757 kfree(new_interfaces[n]);
1758 kfree(new_interfaces);
1763 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1765 dev_warn(&dev->dev,
"new config #%d exceeds power "
1771 ret = usb_autoresume_device(dev);
1773 goto free_interfaces;
1782 cancel_async_set_config(dev);
1796 dev_err(&dev->dev,
"%s Failed to disable LPM\n.", __func__);
1806 goto free_interfaces;
1811 NULL, 0, USB_CTRL_SET_TIMEOUT);
1819 dev->actconfig =
cp;
1826 goto free_interfaces;
1834 for (i = 0; i < nintf; ++
i) {
1835 struct usb_interface_cache *intfc;
1837 struct usb_host_interface *alt;
1839 cp->interface[
i] = intf = new_interfaces[
i];
1840 intfc = cp->intf_cache[
i];
1841 intf->altsetting = intfc->altsetting;
1842 intf->num_altsetting = intfc->num_altsetting;
1843 kref_get(&intfc->ref);
1853 alt = &intf->altsetting[0];
1856 find_iad(dev, cp, alt->desc.bInterfaceNumber);
1857 intf->cur_altsetting = alt;
1859 intf->dev.parent = &dev->dev;
1860 intf->dev.driver =
NULL;
1864 intf->dev.dma_mask = dev->dev.dma_mask;
1865 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
1870 dev->bus->busnum, dev->devpath,
1871 configuration, alt->desc.bInterfaceNumber);
1873 kfree(new_interfaces);
1875 if (cp->string ==
NULL &&
1890 for (i = 0; i < nintf; ++
i) {
1894 "adding %s (config #%d, interface %d)\n",
1895 dev_name(&intf->dev), configuration,
1896 intf->cur_altsetting->desc.bInterfaceNumber);
1897 device_enable_async_suspend(&intf->dev);
1900 dev_err(&dev->dev,
"device_add(%s) --> %d\n",
1901 dev_name(&intf->dev), ret);
1904 create_intf_ep_devs(intf);
1926 struct usb_device *udev = req->
udev;
1928 usb_lock_device(udev);
1929 spin_lock(&set_config_lock);
1931 spin_unlock(&set_config_lock);
1935 usb_unlock_device(udev);
1943 static void cancel_async_set_config(
struct usb_device *udev)
1947 spin_lock(&set_config_lock);
1949 if (req->
udev == udev)
1952 spin_unlock(&set_config_lock);
1986 spin_lock(&set_config_lock);
1987 list_add(&req->
node, &set_config_list);
1988 spin_unlock(&set_config_lock);