aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/core')
-rw-r--r--drivers/usb/core/config.c1
-rw-r--r--drivers/usb/core/devio.c174
-rw-r--r--drivers/usb/core/driver.c27
-rw-r--r--drivers/usb/core/hcd.c37
-rw-r--r--drivers/usb/core/hub.c78
-rw-r--r--drivers/usb/core/message.c7
6 files changed, 274 insertions, 50 deletions
diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index 062967c90b2a..1ab4df1de2da 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -10,7 +10,6 @@
10 10
11 11
12#define USB_MAXALTSETTING 128 /* Hard limit */ 12#define USB_MAXALTSETTING 128 /* Hard limit */
13#define USB_MAXENDPOINTS 30 /* Hard limit */
14 13
15#define USB_MAXCONFIG 8 /* Arbitrary limit */ 14#define USB_MAXCONFIG 8 /* Arbitrary limit */
16 15
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 90e18f6fa2bb..12401ee4ba0e 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -769,6 +769,88 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
769 return ret; 769 return ret;
770} 770}
771 771
772static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
773 unsigned char ep)
774{
775 if (ep & USB_ENDPOINT_DIR_MASK)
776 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
777 else
778 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
779}
780
781static int parse_usbdevfs_streams(struct dev_state *ps,
782 struct usbdevfs_streams __user *streams,
783 unsigned int *num_streams_ret,
784 unsigned int *num_eps_ret,
785 struct usb_host_endpoint ***eps_ret,
786 struct usb_interface **intf_ret)
787{
788 unsigned int i, num_streams, num_eps;
789 struct usb_host_endpoint **eps;
790 struct usb_interface *intf = NULL;
791 unsigned char ep;
792 int ifnum, ret;
793
794 if (get_user(num_streams, &streams->num_streams) ||
795 get_user(num_eps, &streams->num_eps))
796 return -EFAULT;
797
798 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
799 return -EINVAL;
800
801 /* The XHCI controller allows max 2 ^ 16 streams */
802 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
803 return -EINVAL;
804
805 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
806 if (!eps)
807 return -ENOMEM;
808
809 for (i = 0; i < num_eps; i++) {
810 if (get_user(ep, &streams->eps[i])) {
811 ret = -EFAULT;
812 goto error;
813 }
814 eps[i] = ep_to_host_endpoint(ps->dev, ep);
815 if (!eps[i]) {
816 ret = -EINVAL;
817 goto error;
818 }
819
820 /* usb_alloc/free_streams operate on an usb_interface */
821 ifnum = findintfep(ps->dev, ep);
822 if (ifnum < 0) {
823 ret = ifnum;
824 goto error;
825 }
826
827 if (i == 0) {
828 ret = checkintf(ps, ifnum);
829 if (ret < 0)
830 goto error;
831 intf = usb_ifnum_to_if(ps->dev, ifnum);
832 } else {
833 /* Verify all eps belong to the same interface */
834 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
835 ret = -EINVAL;
836 goto error;
837 }
838 }
839 }
840
841 if (num_streams_ret)
842 *num_streams_ret = num_streams;
843 *num_eps_ret = num_eps;
844 *eps_ret = eps;
845 *intf_ret = intf;
846
847 return 0;
848
849error:
850 kfree(eps);
851 return ret;
852}
853
772static int match_devt(struct device *dev, void *data) 854static int match_devt(struct device *dev, void *data)
773{ 855{
774 return dev->devt == (dev_t) (unsigned long) data; 856 return dev->devt == (dev_t) (unsigned long) data;
@@ -1043,6 +1125,20 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
1043 return ret; 1125 return ret;
1044} 1126}
1045 1127
1128static void check_reset_of_active_ep(struct usb_device *udev,
1129 unsigned int epnum, char *ioctl_name)
1130{
1131 struct usb_host_endpoint **eps;
1132 struct usb_host_endpoint *ep;
1133
1134 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1135 ep = eps[epnum & 0x0f];
1136 if (ep && !list_empty(&ep->urb_list))
1137 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1138 task_pid_nr(current), current->comm,
1139 ioctl_name, epnum);
1140}
1141
1046static int proc_resetep(struct dev_state *ps, void __user *arg) 1142static int proc_resetep(struct dev_state *ps, void __user *arg)
1047{ 1143{
1048 unsigned int ep; 1144 unsigned int ep;
@@ -1056,6 +1152,7 @@ static int proc_resetep(struct dev_state *ps, void __user *arg)
1056 ret = checkintf(ps, ret); 1152 ret = checkintf(ps, ret);
1057 if (ret) 1153 if (ret)
1058 return ret; 1154 return ret;
1155 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1059 usb_reset_endpoint(ps->dev, ep); 1156 usb_reset_endpoint(ps->dev, ep);
1060 return 0; 1157 return 0;
1061} 1158}
@@ -1074,6 +1171,7 @@ static int proc_clearhalt(struct dev_state *ps, void __user *arg)
1074 ret = checkintf(ps, ret); 1171 ret = checkintf(ps, ret);
1075 if (ret) 1172 if (ret)
1076 return ret; 1173 return ret;
1174 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1077 if (ep & USB_DIR_IN) 1175 if (ep & USB_DIR_IN)
1078 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); 1176 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1079 else 1177 else
@@ -1127,6 +1225,9 @@ static int proc_setintf(struct dev_state *ps, void __user *arg)
1127 return -EFAULT; 1225 return -EFAULT;
1128 if ((ret = checkintf(ps, setintf.interface))) 1226 if ((ret = checkintf(ps, setintf.interface)))
1129 return ret; 1227 return ret;
1228
1229 destroy_async_on_interface(ps, setintf.interface);
1230
1130 return usb_set_interface(ps->dev, setintf.interface, 1231 return usb_set_interface(ps->dev, setintf.interface,
1131 setintf.altsetting); 1232 setintf.altsetting);
1132} 1233}
@@ -1189,6 +1290,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1189 struct usb_ctrlrequest *dr = NULL; 1290 struct usb_ctrlrequest *dr = NULL;
1190 unsigned int u, totlen, isofrmlen; 1291 unsigned int u, totlen, isofrmlen;
1191 int i, ret, is_in, num_sgs = 0, ifnum = -1; 1292 int i, ret, is_in, num_sgs = 0, ifnum = -1;
1293 int number_of_packets = 0;
1294 unsigned int stream_id = 0;
1192 void *buf; 1295 void *buf;
1193 1296
1194 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP | 1297 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
@@ -1209,15 +1312,10 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1209 if (ret) 1312 if (ret)
1210 return ret; 1313 return ret;
1211 } 1314 }
1212 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) { 1315 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1213 is_in = 1;
1214 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1215 } else {
1216 is_in = 0;
1217 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1218 }
1219 if (!ep) 1316 if (!ep)
1220 return -ENOENT; 1317 return -ENOENT;
1318 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1221 1319
1222 u = 0; 1320 u = 0;
1223 switch(uurb->type) { 1321 switch(uurb->type) {
@@ -1242,7 +1340,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1242 le16_to_cpup(&dr->wIndex)); 1340 le16_to_cpup(&dr->wIndex));
1243 if (ret) 1341 if (ret)
1244 goto error; 1342 goto error;
1245 uurb->number_of_packets = 0;
1246 uurb->buffer_length = le16_to_cpup(&dr->wLength); 1343 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1247 uurb->buffer += 8; 1344 uurb->buffer += 8;
1248 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { 1345 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
@@ -1272,17 +1369,17 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1272 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT; 1369 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1273 goto interrupt_urb; 1370 goto interrupt_urb;
1274 } 1371 }
1275 uurb->number_of_packets = 0;
1276 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); 1372 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1277 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize) 1373 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1278 num_sgs = 0; 1374 num_sgs = 0;
1375 if (ep->streams)
1376 stream_id = uurb->stream_id;
1279 break; 1377 break;
1280 1378
1281 case USBDEVFS_URB_TYPE_INTERRUPT: 1379 case USBDEVFS_URB_TYPE_INTERRUPT:
1282 if (!usb_endpoint_xfer_int(&ep->desc)) 1380 if (!usb_endpoint_xfer_int(&ep->desc))
1283 return -EINVAL; 1381 return -EINVAL;
1284 interrupt_urb: 1382 interrupt_urb:
1285 uurb->number_of_packets = 0;
1286 break; 1383 break;
1287 1384
1288 case USBDEVFS_URB_TYPE_ISO: 1385 case USBDEVFS_URB_TYPE_ISO:
@@ -1292,15 +1389,16 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1292 return -EINVAL; 1389 return -EINVAL;
1293 if (!usb_endpoint_xfer_isoc(&ep->desc)) 1390 if (!usb_endpoint_xfer_isoc(&ep->desc))
1294 return -EINVAL; 1391 return -EINVAL;
1392 number_of_packets = uurb->number_of_packets;
1295 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * 1393 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1296 uurb->number_of_packets; 1394 number_of_packets;
1297 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL))) 1395 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1298 return -ENOMEM; 1396 return -ENOMEM;
1299 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) { 1397 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1300 ret = -EFAULT; 1398 ret = -EFAULT;
1301 goto error; 1399 goto error;
1302 } 1400 }
1303 for (totlen = u = 0; u < uurb->number_of_packets; u++) { 1401 for (totlen = u = 0; u < number_of_packets; u++) {
1304 /* 1402 /*
1305 * arbitrary limit need for USB 3.0 1403 * arbitrary limit need for USB 3.0
1306 * bMaxBurst (0~15 allowed, 1~16 packets) 1404 * bMaxBurst (0~15 allowed, 1~16 packets)
@@ -1331,7 +1429,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1331 ret = -EFAULT; 1429 ret = -EFAULT;
1332 goto error; 1430 goto error;
1333 } 1431 }
1334 as = alloc_async(uurb->number_of_packets); 1432 as = alloc_async(number_of_packets);
1335 if (!as) { 1433 if (!as) {
1336 ret = -ENOMEM; 1434 ret = -ENOMEM;
1337 goto error; 1435 goto error;
@@ -1425,7 +1523,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1425 as->urb->setup_packet = (unsigned char *)dr; 1523 as->urb->setup_packet = (unsigned char *)dr;
1426 dr = NULL; 1524 dr = NULL;
1427 as->urb->start_frame = uurb->start_frame; 1525 as->urb->start_frame = uurb->start_frame;
1428 as->urb->number_of_packets = uurb->number_of_packets; 1526 as->urb->number_of_packets = number_of_packets;
1527 as->urb->stream_id = stream_id;
1429 if (uurb->type == USBDEVFS_URB_TYPE_ISO || 1528 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1430 ps->dev->speed == USB_SPEED_HIGH) 1529 ps->dev->speed == USB_SPEED_HIGH)
1431 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1); 1530 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
@@ -1433,7 +1532,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1433 as->urb->interval = ep->desc.bInterval; 1532 as->urb->interval = ep->desc.bInterval;
1434 as->urb->context = as; 1533 as->urb->context = as;
1435 as->urb->complete = async_completed; 1534 as->urb->complete = async_completed;
1436 for (totlen = u = 0; u < uurb->number_of_packets; u++) { 1535 for (totlen = u = 0; u < number_of_packets; u++) {
1437 as->urb->iso_frame_desc[u].offset = totlen; 1536 as->urb->iso_frame_desc[u].offset = totlen;
1438 as->urb->iso_frame_desc[u].length = isopkt[u].length; 1537 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1439 totlen += isopkt[u].length; 1538 totlen += isopkt[u].length;
@@ -1983,6 +2082,45 @@ static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
1983 return claimintf(ps, dc.interface); 2082 return claimintf(ps, dc.interface);
1984} 2083}
1985 2084
2085static int proc_alloc_streams(struct dev_state *ps, void __user *arg)
2086{
2087 unsigned num_streams, num_eps;
2088 struct usb_host_endpoint **eps;
2089 struct usb_interface *intf;
2090 int r;
2091
2092 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2093 &eps, &intf);
2094 if (r)
2095 return r;
2096
2097 destroy_async_on_interface(ps,
2098 intf->altsetting[0].desc.bInterfaceNumber);
2099
2100 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2101 kfree(eps);
2102 return r;
2103}
2104
2105static int proc_free_streams(struct dev_state *ps, void __user *arg)
2106{
2107 unsigned num_eps;
2108 struct usb_host_endpoint **eps;
2109 struct usb_interface *intf;
2110 int r;
2111
2112 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2113 if (r)
2114 return r;
2115
2116 destroy_async_on_interface(ps,
2117 intf->altsetting[0].desc.bInterfaceNumber);
2118
2119 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2120 kfree(eps);
2121 return r;
2122}
2123
1986/* 2124/*
1987 * NOTE: All requests here that have interface numbers as parameters 2125 * NOTE: All requests here that have interface numbers as parameters
1988 * are assuming that somehow the configuration has been prevented from 2126 * are assuming that somehow the configuration has been prevented from
@@ -2159,6 +2297,12 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2159 case USBDEVFS_DISCONNECT_CLAIM: 2297 case USBDEVFS_DISCONNECT_CLAIM:
2160 ret = proc_disconnect_claim(ps, p); 2298 ret = proc_disconnect_claim(ps, p);
2161 break; 2299 break;
2300 case USBDEVFS_ALLOC_STREAMS:
2301 ret = proc_alloc_streams(ps, p);
2302 break;
2303 case USBDEVFS_FREE_STREAMS:
2304 ret = proc_free_streams(ps, p);
2305 break;
2162 } 2306 }
2163 usb_unlock_device(dev); 2307 usb_unlock_device(dev);
2164 if (ret >= 0) 2308 if (ret >= 0)
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index ab90a0156828..08283d40616c 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -312,9 +312,9 @@ static int usb_probe_interface(struct device *dev)
312 return error; 312 return error;
313 } 313 }
314 314
315 id = usb_match_id(intf, driver->id_table); 315 id = usb_match_dynamic_id(intf, driver);
316 if (!id) 316 if (!id)
317 id = usb_match_dynamic_id(intf, driver); 317 id = usb_match_id(intf, driver->id_table);
318 if (!id) 318 if (!id)
319 return error; 319 return error;
320 320
@@ -400,8 +400,9 @@ static int usb_unbind_interface(struct device *dev)
400{ 400{
401 struct usb_driver *driver = to_usb_driver(dev->driver); 401 struct usb_driver *driver = to_usb_driver(dev->driver);
402 struct usb_interface *intf = to_usb_interface(dev); 402 struct usb_interface *intf = to_usb_interface(dev);
403 struct usb_host_endpoint *ep, **eps = NULL;
403 struct usb_device *udev; 404 struct usb_device *udev;
404 int error, r, lpm_disable_error; 405 int i, j, error, r, lpm_disable_error;
405 406
406 intf->condition = USB_INTERFACE_UNBINDING; 407 intf->condition = USB_INTERFACE_UNBINDING;
407 408
@@ -425,6 +426,26 @@ static int usb_unbind_interface(struct device *dev)
425 driver->disconnect(intf); 426 driver->disconnect(intf);
426 usb_cancel_queued_reset(intf); 427 usb_cancel_queued_reset(intf);
427 428
429 /* Free streams */
430 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
431 ep = &intf->cur_altsetting->endpoint[i];
432 if (ep->streams == 0)
433 continue;
434 if (j == 0) {
435 eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
436 GFP_KERNEL);
437 if (!eps) {
438 dev_warn(dev, "oom, leaking streams\n");
439 break;
440 }
441 }
442 eps[j++] = ep;
443 }
444 if (j) {
445 usb_free_streams(intf, eps, j, GFP_KERNEL);
446 kfree(eps);
447 }
448
428 /* Reset other interface state. 449 /* Reset other interface state.
429 * We cannot do a Set-Interface if the device is suspended or 450 * We cannot do a Set-Interface if the device is suspended or
430 * if it is prepared for a system sleep (since installing a new 451 * if it is prepared for a system sleep (since installing a new
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 2518c3250750..9c4e2922b04d 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2049,7 +2049,7 @@ int usb_alloc_streams(struct usb_interface *interface,
2049{ 2049{
2050 struct usb_hcd *hcd; 2050 struct usb_hcd *hcd;
2051 struct usb_device *dev; 2051 struct usb_device *dev;
2052 int i; 2052 int i, ret;
2053 2053
2054 dev = interface_to_usbdev(interface); 2054 dev = interface_to_usbdev(interface);
2055 hcd = bus_to_hcd(dev->bus); 2055 hcd = bus_to_hcd(dev->bus);
@@ -2058,13 +2058,24 @@ int usb_alloc_streams(struct usb_interface *interface,
2058 if (dev->speed != USB_SPEED_SUPER) 2058 if (dev->speed != USB_SPEED_SUPER)
2059 return -EINVAL; 2059 return -EINVAL;
2060 2060
2061 /* Streams only apply to bulk endpoints. */ 2061 for (i = 0; i < num_eps; i++) {
2062 for (i = 0; i < num_eps; i++) 2062 /* Streams only apply to bulk endpoints. */
2063 if (!usb_endpoint_xfer_bulk(&eps[i]->desc)) 2063 if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
2064 return -EINVAL; 2064 return -EINVAL;
2065 /* Re-alloc is not allowed */
2066 if (eps[i]->streams)
2067 return -EINVAL;
2068 }
2065 2069
2066 return hcd->driver->alloc_streams(hcd, dev, eps, num_eps, 2070 ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
2067 num_streams, mem_flags); 2071 num_streams, mem_flags);
2072 if (ret < 0)
2073 return ret;
2074
2075 for (i = 0; i < num_eps; i++)
2076 eps[i]->streams = ret;
2077
2078 return ret;
2068} 2079}
2069EXPORT_SYMBOL_GPL(usb_alloc_streams); 2080EXPORT_SYMBOL_GPL(usb_alloc_streams);
2070 2081
@@ -2078,8 +2089,7 @@ EXPORT_SYMBOL_GPL(usb_alloc_streams);
2078 * Reverts a group of bulk endpoints back to not using stream IDs. 2089 * Reverts a group of bulk endpoints back to not using stream IDs.
2079 * Can fail if we are given bad arguments, or HCD is broken. 2090 * Can fail if we are given bad arguments, or HCD is broken.
2080 * 2091 *
2081 * Return: On success, the number of allocated streams. On failure, a negative 2092 * Return: 0 on success. On failure, a negative error code.
2082 * error code.
2083 */ 2093 */
2084int usb_free_streams(struct usb_interface *interface, 2094int usb_free_streams(struct usb_interface *interface,
2085 struct usb_host_endpoint **eps, unsigned int num_eps, 2095 struct usb_host_endpoint **eps, unsigned int num_eps,
@@ -2087,19 +2097,26 @@ int usb_free_streams(struct usb_interface *interface,
2087{ 2097{
2088 struct usb_hcd *hcd; 2098 struct usb_hcd *hcd;
2089 struct usb_device *dev; 2099 struct usb_device *dev;
2090 int i; 2100 int i, ret;
2091 2101
2092 dev = interface_to_usbdev(interface); 2102 dev = interface_to_usbdev(interface);
2093 hcd = bus_to_hcd(dev->bus); 2103 hcd = bus_to_hcd(dev->bus);
2094 if (dev->speed != USB_SPEED_SUPER) 2104 if (dev->speed != USB_SPEED_SUPER)
2095 return -EINVAL; 2105 return -EINVAL;
2096 2106
2097 /* Streams only apply to bulk endpoints. */ 2107 /* Double-free is not allowed */
2098 for (i = 0; i < num_eps; i++) 2108 for (i = 0; i < num_eps; i++)
2099 if (!eps[i] || !usb_endpoint_xfer_bulk(&eps[i]->desc)) 2109 if (!eps[i] || !eps[i]->streams)
2100 return -EINVAL; 2110 return -EINVAL;
2101 2111
2102 return hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags); 2112 ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
2113 if (ret < 0)
2114 return ret;
2115
2116 for (i = 0; i < num_eps; i++)
2117 eps[i]->streams = 0;
2118
2119 return ret;
2103} 2120}
2104EXPORT_SYMBOL_GPL(usb_free_streams); 2121EXPORT_SYMBOL_GPL(usb_free_streams);
2105 2122
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 64ea21971be2..5da5394127e9 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -141,19 +141,27 @@ static int usb_device_supports_lpm(struct usb_device *udev)
141 return 0; 141 return 0;
142 } 142 }
143 143
144 /* All USB 3.0 must support LPM, but we need their max exit latency 144 /*
145 * information from the SuperSpeed Extended Capabilities BOS descriptor. 145 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
146 * However, there are some that don't, and they set the U1/U2 exit
147 * latencies to zero.
146 */ 148 */
147 if (!udev->bos->ss_cap) { 149 if (!udev->bos->ss_cap) {
148 dev_warn(&udev->dev, "No LPM exit latency info found. " 150 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
149 "Power management will be impacted.\n");
150 return 0; 151 return 0;
151 } 152 }
152 if (udev->parent->lpm_capable)
153 return 1;
154 153
155 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. " 154 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
156 "Power management will be impacted.\n"); 155 udev->bos->ss_cap->bU2DevExitLat == 0) {
156 if (udev->parent)
157 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
158 else
159 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
160 return 0;
161 }
162
163 if (!udev->parent || udev->parent->lpm_capable)
164 return 1;
157 return 0; 165 return 0;
158} 166}
159 167
@@ -499,7 +507,8 @@ static void led_work (struct work_struct *work)
499 changed++; 507 changed++;
500 } 508 }
501 if (changed) 509 if (changed)
502 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 510 queue_delayed_work(system_power_efficient_wq,
511 &hub->leds, LED_CYCLE_PERIOD);
503} 512}
504 513
505/* use a short timeout for hub/port status fetches */ 514/* use a short timeout for hub/port status fetches */
@@ -1041,7 +1050,8 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1041 if (type == HUB_INIT) { 1050 if (type == HUB_INIT) {
1042 delay = hub_power_on(hub, false); 1051 delay = hub_power_on(hub, false);
1043 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); 1052 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1044 schedule_delayed_work(&hub->init_work, 1053 queue_delayed_work(system_power_efficient_wq,
1054 &hub->init_work,
1045 msecs_to_jiffies(delay)); 1055 msecs_to_jiffies(delay));
1046 1056
1047 /* Suppress autosuspend until init is done */ 1057 /* Suppress autosuspend until init is done */
@@ -1195,7 +1205,8 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1195 /* Don't do a long sleep inside a workqueue routine */ 1205 /* Don't do a long sleep inside a workqueue routine */
1196 if (type == HUB_INIT2) { 1206 if (type == HUB_INIT2) {
1197 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); 1207 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1198 schedule_delayed_work(&hub->init_work, 1208 queue_delayed_work(system_power_efficient_wq,
1209 &hub->init_work,
1199 msecs_to_jiffies(delay)); 1210 msecs_to_jiffies(delay));
1200 return; /* Continues at init3: below */ 1211 return; /* Continues at init3: below */
1201 } else { 1212 } else {
@@ -1209,7 +1220,8 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1209 if (status < 0) 1220 if (status < 0)
1210 dev_err(hub->intfdev, "activate --> %d\n", status); 1221 dev_err(hub->intfdev, "activate --> %d\n", status);
1211 if (hub->has_indicators && blinkenlights) 1222 if (hub->has_indicators && blinkenlights)
1212 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 1223 queue_delayed_work(system_power_efficient_wq,
1224 &hub->leds, LED_CYCLE_PERIOD);
1213 1225
1214 /* Scan all ports that need attention */ 1226 /* Scan all ports that need attention */
1215 kick_khubd(hub); 1227 kick_khubd(hub);
@@ -3958,7 +3970,7 @@ static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
3958 connect_type = usb_get_hub_port_connect_type(udev->parent, 3970 connect_type = usb_get_hub_port_connect_type(udev->parent,
3959 udev->portnum); 3971 udev->portnum);
3960 3972
3961 if ((udev->bos->ext_cap->bmAttributes & USB_BESL_SUPPORT) || 3973 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
3962 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 3974 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
3963 udev->usb2_hw_lpm_allowed = 1; 3975 udev->usb2_hw_lpm_allowed = 1;
3964 usb_set_usb2_hardware_lpm(udev, 1); 3976 usb_set_usb2_hardware_lpm(udev, 1);
@@ -4107,8 +4119,12 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4107 4119
4108 did_new_scheme = true; 4120 did_new_scheme = true;
4109 retval = hub_enable_device(udev); 4121 retval = hub_enable_device(udev);
4110 if (retval < 0) 4122 if (retval < 0) {
4123 dev_err(&udev->dev,
4124 "hub failed to enable device, error %d\n",
4125 retval);
4111 goto fail; 4126 goto fail;
4127 }
4112 4128
4113#define GET_DESCRIPTOR_BUFSIZE 64 4129#define GET_DESCRIPTOR_BUFSIZE 64
4114 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 4130 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
@@ -4311,7 +4327,8 @@ check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4311 /* hub LEDs are probably harder to miss than syslog */ 4327 /* hub LEDs are probably harder to miss than syslog */
4312 if (hub->has_indicators) { 4328 if (hub->has_indicators) {
4313 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 4329 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
4314 schedule_delayed_work (&hub->leds, 0); 4330 queue_delayed_work(system_power_efficient_wq,
4331 &hub->leds, 0);
4315 } 4332 }
4316 } 4333 }
4317 kfree(qual); 4334 kfree(qual);
@@ -4540,7 +4557,9 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4540 if (hub->has_indicators) { 4557 if (hub->has_indicators) {
4541 hub->indicator[port1-1] = 4558 hub->indicator[port1-1] =
4542 INDICATOR_AMBER_BLINK; 4559 INDICATOR_AMBER_BLINK;
4543 schedule_delayed_work (&hub->leds, 0); 4560 queue_delayed_work(
4561 system_power_efficient_wq,
4562 &hub->leds, 0);
4544 } 4563 }
4545 status = -ENOTCONN; /* Don't retry */ 4564 status = -ENOTCONN; /* Don't retry */
4546 goto loop_disable; 4565 goto loop_disable;
@@ -4739,6 +4758,8 @@ static void hub_events(void)
4739 4758
4740 /* deal with port status changes */ 4759 /* deal with port status changes */
4741 for (i = 1; i <= hdev->maxchild; i++) { 4760 for (i = 1; i <= hdev->maxchild; i++) {
4761 struct usb_device *udev = hub->ports[i - 1]->child;
4762
4742 if (test_bit(i, hub->busy_bits)) 4763 if (test_bit(i, hub->busy_bits))
4743 continue; 4764 continue;
4744 connect_change = test_bit(i, hub->change_bits); 4765 connect_change = test_bit(i, hub->change_bits);
@@ -4837,8 +4858,6 @@ static void hub_events(void)
4837 */ 4858 */
4838 if (hub_port_warm_reset_required(hub, portstatus)) { 4859 if (hub_port_warm_reset_required(hub, portstatus)) {
4839 int status; 4860 int status;
4840 struct usb_device *udev =
4841 hub->ports[i - 1]->child;
4842 4861
4843 dev_dbg(hub_dev, "warm reset port %d\n", i); 4862 dev_dbg(hub_dev, "warm reset port %d\n", i);
4844 if (!udev || 4863 if (!udev ||
@@ -4855,6 +4874,24 @@ static void hub_events(void)
4855 usb_unlock_device(udev); 4874 usb_unlock_device(udev);
4856 connect_change = 0; 4875 connect_change = 0;
4857 } 4876 }
4877 /*
4878 * On disconnect USB3 protocol ports transit from U0 to
4879 * SS.Inactive to Rx.Detect. If this happens a warm-
4880 * reset is not needed, but a (re)connect may happen
4881 * before khubd runs and sees the disconnect, and the
4882 * device may be an unknown state.
4883 *
4884 * If the port went through SS.Inactive without khubd
4885 * seeing it the C_LINK_STATE change flag will be set,
4886 * and we reset the dev to put it in a known state.
4887 */
4888 } else if (udev && hub_is_superspeed(hub->hdev) &&
4889 (portchange & USB_PORT_STAT_C_LINK_STATE) &&
4890 (portstatus & USB_PORT_STAT_CONNECTION)) {
4891 usb_lock_device(udev);
4892 usb_reset_device(udev);
4893 usb_unlock_device(udev);
4894 connect_change = 0;
4858 } 4895 }
4859 4896
4860 if (connect_change) 4897 if (connect_change)
@@ -5112,7 +5149,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5112 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 5149 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
5113 struct usb_device_descriptor descriptor = udev->descriptor; 5150 struct usb_device_descriptor descriptor = udev->descriptor;
5114 struct usb_host_bos *bos; 5151 struct usb_host_bos *bos;
5115 int i, ret = 0; 5152 int i, j, ret = 0;
5116 int port1 = udev->portnum; 5153 int port1 = udev->portnum;
5117 5154
5118 if (udev->state == USB_STATE_NOTATTACHED || 5155 if (udev->state == USB_STATE_NOTATTACHED ||
@@ -5238,6 +5275,9 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5238 ret); 5275 ret);
5239 goto re_enumerate; 5276 goto re_enumerate;
5240 } 5277 }
5278 /* Resetting also frees any allocated streams */
5279 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5280 intf->cur_altsetting->endpoint[j].streams = 0;
5241 } 5281 }
5242 5282
5243done: 5283done:
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index f829a1aad1c3..964695741031 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1293,8 +1293,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1293 struct usb_interface *iface; 1293 struct usb_interface *iface;
1294 struct usb_host_interface *alt; 1294 struct usb_host_interface *alt;
1295 struct usb_hcd *hcd = bus_to_hcd(dev->bus); 1295 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1296 int ret; 1296 int i, ret, manual = 0;
1297 int manual = 0;
1298 unsigned int epaddr; 1297 unsigned int epaddr;
1299 unsigned int pipe; 1298 unsigned int pipe;
1300 1299
@@ -1329,6 +1328,10 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1329 mutex_unlock(hcd->bandwidth_mutex); 1328 mutex_unlock(hcd->bandwidth_mutex);
1330 return -ENOMEM; 1329 return -ENOMEM;
1331 } 1330 }
1331 /* Changing alt-setting also frees any allocated streams */
1332 for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
1333 iface->cur_altsetting->endpoint[i].streams = 0;
1334
1332 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt); 1335 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1333 if (ret < 0) { 1336 if (ret < 0) {
1334 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n", 1337 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",