.. | .. |
---|
29 | 29 | #include <linux/workqueue.h> |
---|
30 | 30 | #include <linux/pm_runtime.h> |
---|
31 | 31 | #include <linux/types.h> |
---|
| 32 | +#include <linux/genalloc.h> |
---|
| 33 | +#include <linux/io.h> |
---|
| 34 | +#include <linux/kcov.h> |
---|
32 | 35 | |
---|
33 | 36 | #include <linux/phy/phy.h> |
---|
34 | 37 | #include <linux/usb.h> |
---|
.. | .. |
---|
100 | 103 | |
---|
101 | 104 | /* wait queue for synchronous unlinks */ |
---|
102 | 105 | DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); |
---|
103 | | - |
---|
104 | | -static inline int is_root_hub(struct usb_device *udev) |
---|
105 | | -{ |
---|
106 | | - return (udev->parent == NULL); |
---|
107 | | -} |
---|
108 | 106 | |
---|
109 | 107 | /*-------------------------------------------------------------------------*/ |
---|
110 | 108 | |
---|
.. | .. |
---|
373 | 371 | * -1 is authorized for all devices except wireless (old behaviour) |
---|
374 | 372 | * 0 is unauthorized for all devices |
---|
375 | 373 | * 1 is authorized for all devices |
---|
| 374 | + * 2 is authorized for internal devices |
---|
376 | 375 | */ |
---|
377 | | -static int authorized_default = -1; |
---|
| 376 | +#define USB_AUTHORIZE_WIRED -1 |
---|
| 377 | +#define USB_AUTHORIZE_NONE 0 |
---|
| 378 | +#define USB_AUTHORIZE_ALL 1 |
---|
| 379 | +#define USB_AUTHORIZE_INTERNAL 2 |
---|
| 380 | + |
---|
| 381 | +static int authorized_default = USB_AUTHORIZE_WIRED; |
---|
378 | 382 | module_param(authorized_default, int, S_IRUGO|S_IWUSR); |
---|
379 | 383 | MODULE_PARM_DESC(authorized_default, |
---|
380 | 384 | "Default USB device authorization: 0 is not authorized, 1 is " |
---|
381 | | - "authorized, -1 is authorized except for wireless USB (default, " |
---|
382 | | - "old behaviour"); |
---|
| 385 | + "authorized, 2 is authorized for internal devices, -1 is " |
---|
| 386 | + "authorized except for wireless USB (default, old behaviour)"); |
---|
383 | 387 | /*-------------------------------------------------------------------------*/ |
---|
384 | 388 | |
---|
385 | 389 | /** |
---|
.. | .. |
---|
560 | 564 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: |
---|
561 | 565 | tbuf[0] = 1; |
---|
562 | 566 | len = 1; |
---|
563 | | - /* FALLTHROUGH */ |
---|
| 567 | + fallthrough; |
---|
564 | 568 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
---|
565 | 569 | break; |
---|
566 | 570 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
---|
.. | .. |
---|
629 | 633 | case DeviceRequest | USB_REQ_GET_INTERFACE: |
---|
630 | 634 | tbuf[0] = 0; |
---|
631 | 635 | len = 1; |
---|
632 | | - /* FALLTHROUGH */ |
---|
| 636 | + fallthrough; |
---|
633 | 637 | case DeviceOutRequest | USB_REQ_SET_INTERFACE: |
---|
634 | 638 | break; |
---|
635 | 639 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
---|
.. | .. |
---|
647 | 651 | tbuf[0] = 0; |
---|
648 | 652 | tbuf[1] = 0; |
---|
649 | 653 | len = 2; |
---|
650 | | - /* FALLTHROUGH */ |
---|
| 654 | + fallthrough; |
---|
651 | 655 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
---|
652 | 656 | case EndpointOutRequest | USB_REQ_SET_FEATURE: |
---|
653 | 657 | dev_dbg (hcd->self.controller, "no endpoint features yet\n"); |
---|
.. | .. |
---|
879 | 883 | } |
---|
880 | 884 | |
---|
881 | 885 | |
---|
882 | | - |
---|
883 | | -/* |
---|
884 | | - * Show & store the current value of authorized_default |
---|
885 | | - */ |
---|
886 | | -static ssize_t authorized_default_show(struct device *dev, |
---|
887 | | - struct device_attribute *attr, char *buf) |
---|
888 | | -{ |
---|
889 | | - struct usb_device *rh_usb_dev = to_usb_device(dev); |
---|
890 | | - struct usb_bus *usb_bus = rh_usb_dev->bus; |
---|
891 | | - struct usb_hcd *hcd; |
---|
892 | | - |
---|
893 | | - hcd = bus_to_hcd(usb_bus); |
---|
894 | | - return snprintf(buf, PAGE_SIZE, "%u\n", !!HCD_DEV_AUTHORIZED(hcd)); |
---|
895 | | -} |
---|
896 | | - |
---|
897 | | -static ssize_t authorized_default_store(struct device *dev, |
---|
898 | | - struct device_attribute *attr, |
---|
899 | | - const char *buf, size_t size) |
---|
900 | | -{ |
---|
901 | | - ssize_t result; |
---|
902 | | - unsigned val; |
---|
903 | | - struct usb_device *rh_usb_dev = to_usb_device(dev); |
---|
904 | | - struct usb_bus *usb_bus = rh_usb_dev->bus; |
---|
905 | | - struct usb_hcd *hcd; |
---|
906 | | - |
---|
907 | | - hcd = bus_to_hcd(usb_bus); |
---|
908 | | - result = sscanf(buf, "%u\n", &val); |
---|
909 | | - if (result == 1) { |
---|
910 | | - if (val) |
---|
911 | | - set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); |
---|
912 | | - else |
---|
913 | | - clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); |
---|
914 | | - |
---|
915 | | - result = size; |
---|
916 | | - } else { |
---|
917 | | - result = -EINVAL; |
---|
918 | | - } |
---|
919 | | - return result; |
---|
920 | | -} |
---|
921 | | -static DEVICE_ATTR_RW(authorized_default); |
---|
922 | | - |
---|
923 | | -/* |
---|
924 | | - * interface_authorized_default_show - show default authorization status |
---|
925 | | - * for USB interfaces |
---|
926 | | - * |
---|
927 | | - * note: interface_authorized_default is the default value |
---|
928 | | - * for initializing the authorized attribute of interfaces |
---|
929 | | - */ |
---|
930 | | -static ssize_t interface_authorized_default_show(struct device *dev, |
---|
931 | | - struct device_attribute *attr, char *buf) |
---|
932 | | -{ |
---|
933 | | - struct usb_device *usb_dev = to_usb_device(dev); |
---|
934 | | - struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); |
---|
935 | | - |
---|
936 | | - return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd)); |
---|
937 | | -} |
---|
938 | | - |
---|
939 | | -/* |
---|
940 | | - * interface_authorized_default_store - store default authorization status |
---|
941 | | - * for USB interfaces |
---|
942 | | - * |
---|
943 | | - * note: interface_authorized_default is the default value |
---|
944 | | - * for initializing the authorized attribute of interfaces |
---|
945 | | - */ |
---|
946 | | -static ssize_t interface_authorized_default_store(struct device *dev, |
---|
947 | | - struct device_attribute *attr, const char *buf, size_t count) |
---|
948 | | -{ |
---|
949 | | - struct usb_device *usb_dev = to_usb_device(dev); |
---|
950 | | - struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); |
---|
951 | | - int rc = count; |
---|
952 | | - bool val; |
---|
953 | | - |
---|
954 | | - if (strtobool(buf, &val) != 0) |
---|
955 | | - return -EINVAL; |
---|
956 | | - |
---|
957 | | - if (val) |
---|
958 | | - set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
---|
959 | | - else |
---|
960 | | - clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
---|
961 | | - |
---|
962 | | - return rc; |
---|
963 | | -} |
---|
964 | | -static DEVICE_ATTR_RW(interface_authorized_default); |
---|
965 | | - |
---|
966 | | -/* Group all the USB bus attributes */ |
---|
967 | | -static struct attribute *usb_bus_attrs[] = { |
---|
968 | | - &dev_attr_authorized_default.attr, |
---|
969 | | - &dev_attr_interface_authorized_default.attr, |
---|
970 | | - NULL, |
---|
971 | | -}; |
---|
972 | | - |
---|
973 | | -static const struct attribute_group usb_bus_attr_group = { |
---|
974 | | - .name = NULL, /* we want them in the same directory */ |
---|
975 | | - .attrs = usb_bus_attrs, |
---|
976 | | -}; |
---|
977 | | - |
---|
978 | | - |
---|
979 | | - |
---|
980 | 886 | /*-------------------------------------------------------------------------*/ |
---|
981 | 887 | |
---|
982 | 888 | /** |
---|
.. | .. |
---|
1076 | 982 | { |
---|
1077 | 983 | struct device *parent_dev = hcd->self.controller; |
---|
1078 | 984 | struct usb_device *usb_dev = hcd->self.root_hub; |
---|
| 985 | + struct usb_device_descriptor *descr; |
---|
1079 | 986 | const int devnum = 1; |
---|
1080 | 987 | int retval; |
---|
1081 | 988 | |
---|
1082 | 989 | usb_dev->devnum = devnum; |
---|
1083 | 990 | usb_dev->bus->devnum_next = devnum + 1; |
---|
1084 | | - memset (&usb_dev->bus->devmap.devicemap, 0, |
---|
1085 | | - sizeof usb_dev->bus->devmap.devicemap); |
---|
1086 | 991 | set_bit (devnum, usb_dev->bus->devmap.devicemap); |
---|
1087 | 992 | usb_set_device_state(usb_dev, USB_STATE_ADDRESS); |
---|
1088 | 993 | |
---|
1089 | 994 | mutex_lock(&usb_bus_idr_lock); |
---|
1090 | 995 | |
---|
1091 | 996 | usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
---|
1092 | | - retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); |
---|
1093 | | - if (retval != sizeof usb_dev->descriptor) { |
---|
| 997 | + descr = usb_get_device_descriptor(usb_dev); |
---|
| 998 | + if (IS_ERR(descr)) { |
---|
| 999 | + retval = PTR_ERR(descr); |
---|
1094 | 1000 | mutex_unlock(&usb_bus_idr_lock); |
---|
1095 | 1001 | dev_dbg (parent_dev, "can't read %s device descriptor %d\n", |
---|
1096 | 1002 | dev_name(&usb_dev->dev), retval); |
---|
1097 | | - return (retval < 0) ? retval : -EMSGSIZE; |
---|
| 1003 | + return retval; |
---|
1098 | 1004 | } |
---|
| 1005 | + usb_dev->descriptor = *descr; |
---|
| 1006 | + kfree(descr); |
---|
1099 | 1007 | |
---|
1100 | 1008 | if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) { |
---|
1101 | 1009 | retval = usb_get_bos_descriptor(usb_dev); |
---|
.. | .. |
---|
1351 | 1259 | * using regular system memory - like pci devices doing bus mastering. |
---|
1352 | 1260 | * |
---|
1353 | 1261 | * To support host controllers with limited dma capabilities we provide dma |
---|
1354 | | - * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag. |
---|
1355 | | - * For this to work properly the host controller code must first use the |
---|
1356 | | - * function dma_declare_coherent_memory() to point out which memory area |
---|
1357 | | - * that should be used for dma allocations. |
---|
| 1262 | + * bounce buffers. This feature can be enabled by initializing |
---|
| 1263 | + * hcd->localmem_pool using usb_hcd_setup_local_mem(). |
---|
1358 | 1264 | * |
---|
1359 | | - * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for |
---|
1360 | | - * dma using dma_alloc_coherent() which in turn allocates from the memory |
---|
1361 | | - * area pointed out with dma_declare_coherent_memory(). |
---|
| 1265 | + * The initialized hcd->localmem_pool then tells the usb code to allocate all |
---|
| 1266 | + * data for dma using the genalloc API. |
---|
1362 | 1267 | * |
---|
1363 | 1268 | * So, to summarize... |
---|
1364 | 1269 | * |
---|
.. | .. |
---|
1367 | 1272 | * only memory that the controller can read ... |
---|
1368 | 1273 | * (a) "normal" kernel memory is no good, and |
---|
1369 | 1274 | * (b) there's not enough to share |
---|
1370 | | - * |
---|
1371 | | - * - The only *portable* hook for such stuff in the |
---|
1372 | | - * DMA framework is dma_declare_coherent_memory() |
---|
1373 | 1275 | * |
---|
1374 | 1276 | * - So we use that, even though the primary requirement |
---|
1375 | 1277 | * is that the memory be "local" (hence addressable |
---|
.. | .. |
---|
1519 | 1421 | if (usb_endpoint_xfer_control(&urb->ep->desc)) { |
---|
1520 | 1422 | if (hcd->self.uses_pio_for_control) |
---|
1521 | 1423 | return ret; |
---|
1522 | | - if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) { |
---|
1523 | | - if (is_vmalloc_addr(urb->setup_packet)) { |
---|
1524 | | - WARN_ONCE(1, "setup packet is not dma capable\n"); |
---|
1525 | | - return -EAGAIN; |
---|
1526 | | - } else if (object_is_on_stack(urb->setup_packet)) { |
---|
| 1424 | + if (hcd->localmem_pool) { |
---|
| 1425 | + ret = hcd_alloc_coherent( |
---|
| 1426 | + urb->dev->bus, mem_flags, |
---|
| 1427 | + &urb->setup_dma, |
---|
| 1428 | + (void **)&urb->setup_packet, |
---|
| 1429 | + sizeof(struct usb_ctrlrequest), |
---|
| 1430 | + DMA_TO_DEVICE); |
---|
| 1431 | + if (ret) |
---|
| 1432 | + return ret; |
---|
| 1433 | + urb->transfer_flags |= URB_SETUP_MAP_LOCAL; |
---|
| 1434 | + } else if (hcd_uses_dma(hcd)) { |
---|
| 1435 | + if (object_is_on_stack(urb->setup_packet)) { |
---|
1527 | 1436 | WARN_ONCE(1, "setup packet is on stack\n"); |
---|
1528 | 1437 | return -EAGAIN; |
---|
1529 | 1438 | } |
---|
.. | .. |
---|
1537 | 1446 | urb->setup_dma)) |
---|
1538 | 1447 | return -EAGAIN; |
---|
1539 | 1448 | urb->transfer_flags |= URB_SETUP_MAP_SINGLE; |
---|
1540 | | - } else if (hcd->driver->flags & HCD_LOCAL_MEM) { |
---|
1541 | | - ret = hcd_alloc_coherent( |
---|
1542 | | - urb->dev->bus, mem_flags, |
---|
1543 | | - &urb->setup_dma, |
---|
1544 | | - (void **)&urb->setup_packet, |
---|
1545 | | - sizeof(struct usb_ctrlrequest), |
---|
1546 | | - DMA_TO_DEVICE); |
---|
1547 | | - if (ret) |
---|
1548 | | - return ret; |
---|
1549 | | - urb->transfer_flags |= URB_SETUP_MAP_LOCAL; |
---|
1550 | 1449 | } |
---|
1551 | 1450 | } |
---|
1552 | 1451 | |
---|
1553 | 1452 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
---|
1554 | 1453 | if (urb->transfer_buffer_length != 0 |
---|
1555 | 1454 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { |
---|
1556 | | - if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) { |
---|
| 1455 | + if (hcd->localmem_pool) { |
---|
| 1456 | + ret = hcd_alloc_coherent( |
---|
| 1457 | + urb->dev->bus, mem_flags, |
---|
| 1458 | + &urb->transfer_dma, |
---|
| 1459 | + &urb->transfer_buffer, |
---|
| 1460 | + urb->transfer_buffer_length, |
---|
| 1461 | + dir); |
---|
| 1462 | + if (ret == 0) |
---|
| 1463 | + urb->transfer_flags |= URB_MAP_LOCAL; |
---|
| 1464 | + } else if (hcd_uses_dma(hcd)) { |
---|
1557 | 1465 | if (urb->num_sgs) { |
---|
1558 | 1466 | int n; |
---|
1559 | 1467 | |
---|
.. | .. |
---|
1589 | 1497 | ret = -EAGAIN; |
---|
1590 | 1498 | else |
---|
1591 | 1499 | urb->transfer_flags |= URB_DMA_MAP_PAGE; |
---|
1592 | | - } else if (is_vmalloc_addr(urb->transfer_buffer)) { |
---|
1593 | | - WARN_ONCE(1, "transfer buffer not dma capable\n"); |
---|
1594 | | - ret = -EAGAIN; |
---|
1595 | 1500 | } else if (object_is_on_stack(urb->transfer_buffer)) { |
---|
1596 | 1501 | WARN_ONCE(1, "transfer buffer is on stack\n"); |
---|
1597 | 1502 | ret = -EAGAIN; |
---|
.. | .. |
---|
1607 | 1512 | else |
---|
1608 | 1513 | urb->transfer_flags |= URB_DMA_MAP_SINGLE; |
---|
1609 | 1514 | } |
---|
1610 | | - } else if (hcd->driver->flags & HCD_LOCAL_MEM) { |
---|
1611 | | - ret = hcd_alloc_coherent( |
---|
1612 | | - urb->dev->bus, mem_flags, |
---|
1613 | | - &urb->transfer_dma, |
---|
1614 | | - &urb->transfer_buffer, |
---|
1615 | | - urb->transfer_buffer_length, |
---|
1616 | | - dir); |
---|
1617 | | - if (ret == 0) |
---|
1618 | | - urb->transfer_flags |= URB_MAP_LOCAL; |
---|
1619 | 1515 | } |
---|
1620 | 1516 | if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE | |
---|
1621 | 1517 | URB_SETUP_MAP_LOCAL))) |
---|
.. | .. |
---|
1752 | 1648 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); |
---|
1753 | 1649 | struct usb_anchor *anchor = urb->anchor; |
---|
1754 | 1650 | int status = urb->unlinked; |
---|
1755 | | - unsigned long flags; |
---|
1756 | 1651 | |
---|
1757 | 1652 | urb->hcpriv = NULL; |
---|
1758 | 1653 | if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && |
---|
.. | .. |
---|
1769 | 1664 | |
---|
1770 | 1665 | /* pass ownership to the completion handler */ |
---|
1771 | 1666 | urb->status = status; |
---|
1772 | | - |
---|
1773 | 1667 | /* |
---|
1774 | | - * We disable local IRQs here avoid possible deadlock because |
---|
1775 | | - * drivers may call spin_lock() to hold lock which might be |
---|
1776 | | - * acquired in one hard interrupt handler. |
---|
1777 | | - * |
---|
1778 | | - * The local_irq_save()/local_irq_restore() around complete() |
---|
1779 | | - * will be removed if current USB drivers have been cleaned up |
---|
1780 | | - * and no one may trigger the above deadlock situation when |
---|
1781 | | - * running complete() in tasklet. |
---|
| 1668 | + * This function can be called in task context inside another remote |
---|
| 1669 | + * coverage collection section, but KCOV doesn't support that kind of |
---|
| 1670 | + * recursion yet. Only collect coverage in softirq context for now. |
---|
1782 | 1671 | */ |
---|
1783 | | - local_irq_save(flags); |
---|
| 1672 | + if (in_serving_softirq()) |
---|
| 1673 | + kcov_remote_start_usb((u64)urb->dev->bus->busnum); |
---|
1784 | 1674 | urb->complete(urb); |
---|
1785 | | - local_irq_restore(flags); |
---|
| 1675 | + if (in_serving_softirq()) |
---|
| 1676 | + kcov_remote_stop(); |
---|
1786 | 1677 | |
---|
1787 | 1678 | usb_anchor_resume_wakeups(anchor); |
---|
1788 | 1679 | atomic_dec(&urb->use_count); |
---|
.. | .. |
---|
1798 | 1689 | usb_put_urb(urb); |
---|
1799 | 1690 | } |
---|
1800 | 1691 | |
---|
1801 | | -static void usb_giveback_urb_bh(unsigned long param) |
---|
| 1692 | +static void usb_giveback_urb_bh(struct tasklet_struct *t) |
---|
1802 | 1693 | { |
---|
1803 | | - struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param; |
---|
| 1694 | + struct giveback_urb_bh *bh = from_tasklet(bh, t, bh); |
---|
1804 | 1695 | struct list_head local_list; |
---|
1805 | 1696 | |
---|
1806 | 1697 | spin_lock_irq(&bh->lock); |
---|
.. | .. |
---|
1912 | 1803 | /* kick hcd */ |
---|
1913 | 1804 | unlink1(hcd, urb, -ESHUTDOWN); |
---|
1914 | 1805 | dev_dbg (hcd->self.controller, |
---|
1915 | | - "shutdown urb %pK ep%d%s%s\n", |
---|
| 1806 | + "shutdown urb %pK ep%d%s-%s\n", |
---|
1916 | 1807 | urb, usb_endpoint_num(&ep->desc), |
---|
1917 | 1808 | is_in ? "in" : "out", |
---|
1918 | | - ({ char *s; |
---|
1919 | | - |
---|
1920 | | - switch (usb_endpoint_type(&ep->desc)) { |
---|
1921 | | - case USB_ENDPOINT_XFER_CONTROL: |
---|
1922 | | - s = ""; break; |
---|
1923 | | - case USB_ENDPOINT_XFER_BULK: |
---|
1924 | | - s = "-bulk"; break; |
---|
1925 | | - case USB_ENDPOINT_XFER_INT: |
---|
1926 | | - s = "-intr"; break; |
---|
1927 | | - default: |
---|
1928 | | - s = "-iso"; break; |
---|
1929 | | - }; |
---|
1930 | | - s; |
---|
1931 | | - })); |
---|
| 1809 | + usb_ep_type_string(usb_endpoint_type(&ep->desc))); |
---|
1932 | 1810 | usb_put_urb (urb); |
---|
1933 | 1811 | |
---|
1934 | 1812 | /* list contents may have changed */ |
---|
.. | .. |
---|
2254 | 2132 | return hcd->driver->get_frame_number (hcd); |
---|
2255 | 2133 | } |
---|
2256 | 2134 | |
---|
2257 | | -int usb_hcd_sec_event_ring_setup(struct usb_device *udev, |
---|
2258 | | - unsigned int intr_num) |
---|
2259 | | -{ |
---|
2260 | | - struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
---|
2261 | | - |
---|
2262 | | - if (!HCD_RH_RUNNING(hcd)) |
---|
2263 | | - return 0; |
---|
2264 | | - |
---|
2265 | | - return hcd->driver->sec_event_ring_setup(hcd, intr_num); |
---|
2266 | | -} |
---|
2267 | | - |
---|
2268 | | -int usb_hcd_sec_event_ring_cleanup(struct usb_device *udev, |
---|
2269 | | - unsigned int intr_num) |
---|
2270 | | -{ |
---|
2271 | | - struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
---|
2272 | | - |
---|
2273 | | - if (!HCD_RH_RUNNING(hcd)) |
---|
2274 | | - return 0; |
---|
2275 | | - |
---|
2276 | | - return hcd->driver->sec_event_ring_cleanup(hcd, intr_num); |
---|
2277 | | -} |
---|
2278 | | - |
---|
2279 | 2135 | /*-------------------------------------------------------------------------*/ |
---|
2280 | | - |
---|
2281 | | -phys_addr_t |
---|
2282 | | -usb_hcd_get_sec_event_ring_phys_addr(struct usb_device *udev, |
---|
2283 | | - unsigned int intr_num, dma_addr_t *dma) |
---|
2284 | | -{ |
---|
2285 | | - struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
---|
2286 | | - |
---|
2287 | | - if (!HCD_RH_RUNNING(hcd)) |
---|
2288 | | - return 0; |
---|
2289 | | - |
---|
2290 | | - return hcd->driver->get_sec_event_ring_phys_addr(hcd, intr_num, dma); |
---|
2291 | | -} |
---|
2292 | | - |
---|
2293 | | -phys_addr_t |
---|
2294 | | -usb_hcd_get_xfer_ring_phys_addr(struct usb_device *udev, |
---|
2295 | | - struct usb_host_endpoint *ep, dma_addr_t *dma) |
---|
2296 | | -{ |
---|
2297 | | - struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
---|
2298 | | - |
---|
2299 | | - if (!HCD_RH_RUNNING(hcd)) |
---|
2300 | | - return 0; |
---|
2301 | | - |
---|
2302 | | - return hcd->driver->get_xfer_ring_phys_addr(hcd, udev, ep, dma); |
---|
2303 | | -} |
---|
2304 | | - |
---|
2305 | | -int usb_hcd_get_controller_id(struct usb_device *udev) |
---|
2306 | | -{ |
---|
2307 | | - struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
---|
2308 | | - |
---|
2309 | | - if (!HCD_RH_RUNNING(hcd)) |
---|
2310 | | - return -EINVAL; |
---|
2311 | | - |
---|
2312 | | - return hcd->driver->get_core_id(hcd); |
---|
2313 | | -} |
---|
2314 | | - |
---|
2315 | | -int usb_hcd_stop_endpoint(struct usb_device *udev, |
---|
2316 | | - struct usb_host_endpoint *ep) |
---|
2317 | | -{ |
---|
2318 | | - struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
---|
2319 | | - |
---|
2320 | | - return hcd->driver->stop_endpoint(hcd, udev, ep); |
---|
2321 | | -} |
---|
2322 | 2136 | |
---|
2323 | 2137 | #ifdef CONFIG_PM |
---|
2324 | 2138 | |
---|
.. | .. |
---|
2403 | 2217 | hcd->state = HC_STATE_RESUMING; |
---|
2404 | 2218 | status = hcd->driver->bus_resume(hcd); |
---|
2405 | 2219 | clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); |
---|
| 2220 | + if (status == 0) |
---|
| 2221 | + status = usb_phy_roothub_calibrate(hcd->phy_roothub); |
---|
| 2222 | + |
---|
2406 | 2223 | if (status == 0) { |
---|
2407 | 2224 | struct usb_device *udev; |
---|
2408 | 2225 | int port1; |
---|
.. | .. |
---|
2546 | 2363 | |
---|
2547 | 2364 | /*-------------------------------------------------------------------------*/ |
---|
2548 | 2365 | |
---|
| 2366 | +/* Workqueue routine for when the root-hub has died. */ |
---|
| 2367 | +static void hcd_died_work(struct work_struct *work) |
---|
| 2368 | +{ |
---|
| 2369 | + struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work); |
---|
| 2370 | + static char *env[] = { |
---|
| 2371 | + "ERROR=DEAD", |
---|
| 2372 | + NULL |
---|
| 2373 | + }; |
---|
| 2374 | + |
---|
| 2375 | + /* Notify user space that the host controller has died */ |
---|
| 2376 | + kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env); |
---|
| 2377 | +} |
---|
| 2378 | + |
---|
2549 | 2379 | /** |
---|
2550 | 2380 | * usb_hc_died - report abnormal shutdown of a host controller (bus glue) |
---|
2551 | 2381 | * @hcd: pointer to the HCD representing the controller |
---|
.. | .. |
---|
2586 | 2416 | usb_kick_hub_wq(hcd->self.root_hub); |
---|
2587 | 2417 | } |
---|
2588 | 2418 | } |
---|
| 2419 | + |
---|
| 2420 | + /* Handle the case where this function gets called with a shared HCD */ |
---|
| 2421 | + if (usb_hcd_is_primary_hcd(hcd)) |
---|
| 2422 | + schedule_work(&hcd->died_work); |
---|
| 2423 | + else |
---|
| 2424 | + schedule_work(&hcd->primary_hcd->died_work); |
---|
| 2425 | + |
---|
2589 | 2426 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
---|
2590 | 2427 | /* Make sure that the other roothub is also deallocated. */ |
---|
2591 | | - usb_atomic_notify_dead_bus(&hcd->self); |
---|
2592 | 2428 | } |
---|
2593 | 2429 | EXPORT_SYMBOL_GPL (usb_hc_died); |
---|
2594 | 2430 | |
---|
.. | .. |
---|
2599 | 2435 | |
---|
2600 | 2436 | spin_lock_init(&bh->lock); |
---|
2601 | 2437 | INIT_LIST_HEAD(&bh->head); |
---|
2602 | | - tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh); |
---|
| 2438 | + tasklet_setup(&bh->bh, usb_giveback_urb_bh); |
---|
2603 | 2439 | } |
---|
2604 | 2440 | |
---|
2605 | 2441 | struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver, |
---|
.. | .. |
---|
2647 | 2483 | hcd->self.controller = dev; |
---|
2648 | 2484 | hcd->self.sysdev = sysdev; |
---|
2649 | 2485 | hcd->self.bus_name = bus_name; |
---|
2650 | | - hcd->self.uses_dma = (sysdev->dma_mask != NULL); |
---|
2651 | 2486 | |
---|
2652 | 2487 | timer_setup(&hcd->rh_timer, rh_timer_func, 0); |
---|
2653 | 2488 | #ifdef CONFIG_PM |
---|
2654 | 2489 | INIT_WORK(&hcd->wakeup_work, hcd_resume_work); |
---|
2655 | 2490 | #endif |
---|
| 2491 | + |
---|
| 2492 | + INIT_WORK(&hcd->died_work, hcd_died_work); |
---|
2656 | 2493 | |
---|
2657 | 2494 | hcd->driver = driver; |
---|
2658 | 2495 | hcd->speed = driver->flags & HCD_MASK; |
---|
.. | .. |
---|
2828 | 2665 | { |
---|
2829 | 2666 | int retval; |
---|
2830 | 2667 | struct usb_device *rhdev; |
---|
| 2668 | + struct usb_hcd *shared_hcd; |
---|
2831 | 2669 | |
---|
2832 | 2670 | if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) { |
---|
2833 | 2671 | hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); |
---|
.. | .. |
---|
2838 | 2676 | if (retval) |
---|
2839 | 2677 | return retval; |
---|
2840 | 2678 | |
---|
| 2679 | + retval = usb_phy_roothub_set_mode(hcd->phy_roothub, |
---|
| 2680 | + PHY_MODE_USB_HOST_SS); |
---|
| 2681 | + if (retval) |
---|
| 2682 | + retval = usb_phy_roothub_set_mode(hcd->phy_roothub, |
---|
| 2683 | + PHY_MODE_USB_HOST); |
---|
| 2684 | + if (retval) |
---|
| 2685 | + goto err_usb_phy_roothub_power_on; |
---|
| 2686 | + |
---|
2841 | 2687 | retval = usb_phy_roothub_power_on(hcd->phy_roothub); |
---|
2842 | 2688 | if (retval) |
---|
2843 | 2689 | goto err_usb_phy_roothub_power_on; |
---|
.. | .. |
---|
2845 | 2691 | |
---|
2846 | 2692 | dev_info(hcd->self.controller, "%s\n", hcd->product_desc); |
---|
2847 | 2693 | |
---|
2848 | | - /* Keep old behaviour if authorized_default is not in [0, 1]. */ |
---|
2849 | | - if (authorized_default < 0 || authorized_default > 1) { |
---|
2850 | | - if (hcd->wireless) |
---|
2851 | | - clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); |
---|
2852 | | - else |
---|
2853 | | - set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); |
---|
2854 | | - } else { |
---|
2855 | | - if (authorized_default) |
---|
2856 | | - set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); |
---|
2857 | | - else |
---|
2858 | | - clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); |
---|
| 2694 | + switch (authorized_default) { |
---|
| 2695 | + case USB_AUTHORIZE_NONE: |
---|
| 2696 | + hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE; |
---|
| 2697 | + break; |
---|
| 2698 | + |
---|
| 2699 | + case USB_AUTHORIZE_ALL: |
---|
| 2700 | + hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL; |
---|
| 2701 | + break; |
---|
| 2702 | + |
---|
| 2703 | + case USB_AUTHORIZE_INTERNAL: |
---|
| 2704 | + hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL; |
---|
| 2705 | + break; |
---|
| 2706 | + |
---|
| 2707 | + case USB_AUTHORIZE_WIRED: |
---|
| 2708 | + default: |
---|
| 2709 | + hcd->dev_policy = hcd->wireless ? |
---|
| 2710 | + USB_DEVICE_AUTHORIZE_NONE : USB_DEVICE_AUTHORIZE_ALL; |
---|
| 2711 | + break; |
---|
2859 | 2712 | } |
---|
| 2713 | + |
---|
2860 | 2714 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
---|
2861 | 2715 | |
---|
2862 | 2716 | /* per default all interfaces are authorized */ |
---|
.. | .. |
---|
2905 | 2759 | case HCD_USB32: |
---|
2906 | 2760 | rhdev->rx_lanes = 2; |
---|
2907 | 2761 | rhdev->tx_lanes = 2; |
---|
2908 | | - /* fall through */ |
---|
| 2762 | + fallthrough; |
---|
2909 | 2763 | case HCD_USB31: |
---|
2910 | 2764 | rhdev->speed = USB_SPEED_SUPER_PLUS; |
---|
2911 | 2765 | break; |
---|
.. | .. |
---|
2939 | 2793 | } |
---|
2940 | 2794 | hcd->rh_pollable = 1; |
---|
2941 | 2795 | |
---|
| 2796 | + retval = usb_phy_roothub_calibrate(hcd->phy_roothub); |
---|
| 2797 | + if (retval) |
---|
| 2798 | + goto err_hcd_driver_setup; |
---|
| 2799 | + |
---|
2942 | 2800 | /* NOTE: root hub and controller capabilities may not be the same */ |
---|
2943 | 2801 | if (device_can_wakeup(hcd->self.controller) |
---|
2944 | 2802 | && device_can_wakeup(&hcd->self.root_hub->dev)) |
---|
.. | .. |
---|
2964 | 2822 | goto err_hcd_driver_start; |
---|
2965 | 2823 | } |
---|
2966 | 2824 | |
---|
2967 | | - /* starting here, usbcore will pay attention to this root hub */ |
---|
2968 | | - retval = register_root_hub(hcd); |
---|
2969 | | - if (retval != 0) |
---|
2970 | | - goto err_register_root_hub; |
---|
| 2825 | + /* starting here, usbcore will pay attention to the shared HCD roothub */ |
---|
| 2826 | + shared_hcd = hcd->shared_hcd; |
---|
| 2827 | + if (!usb_hcd_is_primary_hcd(hcd) && shared_hcd && HCD_DEFER_RH_REGISTER(shared_hcd)) { |
---|
| 2828 | + retval = register_root_hub(shared_hcd); |
---|
| 2829 | + if (retval != 0) |
---|
| 2830 | + goto err_register_root_hub; |
---|
2971 | 2831 | |
---|
2972 | | - retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group); |
---|
2973 | | - if (retval < 0) { |
---|
2974 | | - printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n", |
---|
2975 | | - retval); |
---|
2976 | | - goto error_create_attr_group; |
---|
| 2832 | + if (shared_hcd->uses_new_polling && HCD_POLL_RH(shared_hcd)) |
---|
| 2833 | + usb_hcd_poll_rh_status(shared_hcd); |
---|
2977 | 2834 | } |
---|
2978 | | - if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) |
---|
2979 | | - usb_hcd_poll_rh_status(hcd); |
---|
| 2835 | + |
---|
| 2836 | + /* starting here, usbcore will pay attention to this root hub */ |
---|
| 2837 | + if (!HCD_DEFER_RH_REGISTER(hcd)) { |
---|
| 2838 | + retval = register_root_hub(hcd); |
---|
| 2839 | + if (retval != 0) |
---|
| 2840 | + goto err_register_root_hub; |
---|
| 2841 | + |
---|
| 2842 | + if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) |
---|
| 2843 | + usb_hcd_poll_rh_status(hcd); |
---|
| 2844 | + } |
---|
2980 | 2845 | |
---|
2981 | 2846 | return retval; |
---|
2982 | 2847 | |
---|
2983 | | -error_create_attr_group: |
---|
2984 | | - clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
---|
2985 | | - if (HC_IS_RUNNING(hcd->state)) |
---|
2986 | | - hcd->state = HC_STATE_QUIESCING; |
---|
2987 | | - spin_lock_irq(&hcd_root_hub_lock); |
---|
2988 | | - hcd->rh_registered = 0; |
---|
2989 | | - spin_unlock_irq(&hcd_root_hub_lock); |
---|
2990 | | - |
---|
2991 | | -#ifdef CONFIG_PM |
---|
2992 | | - cancel_work_sync(&hcd->wakeup_work); |
---|
2993 | | -#endif |
---|
2994 | | - mutex_lock(&usb_bus_idr_lock); |
---|
2995 | | - usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
---|
2996 | | - mutex_unlock(&usb_bus_idr_lock); |
---|
2997 | 2848 | err_register_root_hub: |
---|
2998 | 2849 | hcd->rh_pollable = 0; |
---|
2999 | 2850 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
---|
.. | .. |
---|
3033 | 2884 | void usb_remove_hcd(struct usb_hcd *hcd) |
---|
3034 | 2885 | { |
---|
3035 | 2886 | struct usb_device *rhdev = hcd->self.root_hub; |
---|
| 2887 | + bool rh_registered; |
---|
3036 | 2888 | |
---|
3037 | 2889 | dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); |
---|
3038 | 2890 | |
---|
3039 | 2891 | usb_get_dev(rhdev); |
---|
3040 | | - sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group); |
---|
3041 | | - |
---|
3042 | 2892 | clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
---|
3043 | 2893 | if (HC_IS_RUNNING (hcd->state)) |
---|
3044 | 2894 | hcd->state = HC_STATE_QUIESCING; |
---|
3045 | 2895 | |
---|
3046 | 2896 | dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); |
---|
3047 | 2897 | spin_lock_irq (&hcd_root_hub_lock); |
---|
| 2898 | + rh_registered = hcd->rh_registered; |
---|
3048 | 2899 | hcd->rh_registered = 0; |
---|
3049 | 2900 | spin_unlock_irq (&hcd_root_hub_lock); |
---|
3050 | 2901 | |
---|
3051 | 2902 | #ifdef CONFIG_PM |
---|
3052 | 2903 | cancel_work_sync(&hcd->wakeup_work); |
---|
3053 | 2904 | #endif |
---|
| 2905 | + cancel_work_sync(&hcd->died_work); |
---|
3054 | 2906 | |
---|
3055 | 2907 | mutex_lock(&usb_bus_idr_lock); |
---|
3056 | | - usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
---|
| 2908 | + if (rh_registered) |
---|
| 2909 | + usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
---|
3057 | 2910 | mutex_unlock(&usb_bus_idr_lock); |
---|
3058 | 2911 | |
---|
3059 | 2912 | /* |
---|
.. | .. |
---|
3111 | 2964 | } |
---|
3112 | 2965 | EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); |
---|
3113 | 2966 | |
---|
| 2967 | +int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr, |
---|
| 2968 | + dma_addr_t dma, size_t size) |
---|
| 2969 | +{ |
---|
| 2970 | + int err; |
---|
| 2971 | + void *local_mem; |
---|
| 2972 | + |
---|
| 2973 | + hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4, |
---|
| 2974 | + dev_to_node(hcd->self.sysdev), |
---|
| 2975 | + dev_name(hcd->self.sysdev)); |
---|
| 2976 | + if (IS_ERR(hcd->localmem_pool)) |
---|
| 2977 | + return PTR_ERR(hcd->localmem_pool); |
---|
| 2978 | + |
---|
| 2979 | + local_mem = devm_memremap(hcd->self.sysdev, phys_addr, |
---|
| 2980 | + size, MEMREMAP_WC); |
---|
| 2981 | + if (IS_ERR(local_mem)) |
---|
| 2982 | + return PTR_ERR(local_mem); |
---|
| 2983 | + |
---|
| 2984 | + /* |
---|
| 2985 | + * Here we pass a dma_addr_t but the arg type is a phys_addr_t. |
---|
| 2986 | + * It's not backed by system memory and thus there's no kernel mapping |
---|
| 2987 | + * for it. |
---|
| 2988 | + */ |
---|
| 2989 | + err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem, |
---|
| 2990 | + dma, size, dev_to_node(hcd->self.sysdev)); |
---|
| 2991 | + if (err < 0) { |
---|
| 2992 | + dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n", |
---|
| 2993 | + err); |
---|
| 2994 | + return err; |
---|
| 2995 | + } |
---|
| 2996 | + |
---|
| 2997 | + return 0; |
---|
| 2998 | +} |
---|
| 2999 | +EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem); |
---|
| 3000 | + |
---|
3114 | 3001 | /*-------------------------------------------------------------------------*/ |
---|
3115 | 3002 | |
---|
3116 | 3003 | #if IS_ENABLED(CONFIG_USB_MON) |
---|