| .. | .. |
|---|
| 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 | /** |
|---|
| .. | .. |
|---|
| 1081 | 987 | |
|---|
| 1082 | 988 | usb_dev->devnum = devnum; |
|---|
| 1083 | 989 | usb_dev->bus->devnum_next = devnum + 1; |
|---|
| 1084 | | - memset (&usb_dev->bus->devmap.devicemap, 0, |
|---|
| 1085 | | - sizeof usb_dev->bus->devmap.devicemap); |
|---|
| 1086 | 990 | set_bit (devnum, usb_dev->bus->devmap.devicemap); |
|---|
| 1087 | 991 | usb_set_device_state(usb_dev, USB_STATE_ADDRESS); |
|---|
| 1088 | 992 | |
|---|
| .. | .. |
|---|
| 1351 | 1255 | * using regular system memory - like pci devices doing bus mastering. |
|---|
| 1352 | 1256 | * |
|---|
| 1353 | 1257 | * 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. |
|---|
| 1258 | + * bounce buffers. This feature can be enabled by initializing |
|---|
| 1259 | + * hcd->localmem_pool using usb_hcd_setup_local_mem(). |
|---|
| 1358 | 1260 | * |
|---|
| 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(). |
|---|
| 1261 | + * The initialized hcd->localmem_pool then tells the usb code to allocate all |
|---|
| 1262 | + * data for dma using the genalloc API. |
|---|
| 1362 | 1263 | * |
|---|
| 1363 | 1264 | * So, to summarize... |
|---|
| 1364 | 1265 | * |
|---|
| .. | .. |
|---|
| 1367 | 1268 | * only memory that the controller can read ... |
|---|
| 1368 | 1269 | * (a) "normal" kernel memory is no good, and |
|---|
| 1369 | 1270 | * (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 | 1271 | * |
|---|
| 1374 | 1272 | * - So we use that, even though the primary requirement |
|---|
| 1375 | 1273 | * is that the memory be "local" (hence addressable |
|---|
| .. | .. |
|---|
| 1519 | 1417 | if (usb_endpoint_xfer_control(&urb->ep->desc)) { |
|---|
| 1520 | 1418 | if (hcd->self.uses_pio_for_control) |
|---|
| 1521 | 1419 | 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)) { |
|---|
| 1420 | + if (hcd->localmem_pool) { |
|---|
| 1421 | + ret = hcd_alloc_coherent( |
|---|
| 1422 | + urb->dev->bus, mem_flags, |
|---|
| 1423 | + &urb->setup_dma, |
|---|
| 1424 | + (void **)&urb->setup_packet, |
|---|
| 1425 | + sizeof(struct usb_ctrlrequest), |
|---|
| 1426 | + DMA_TO_DEVICE); |
|---|
| 1427 | + if (ret) |
|---|
| 1428 | + return ret; |
|---|
| 1429 | + urb->transfer_flags |= URB_SETUP_MAP_LOCAL; |
|---|
| 1430 | + } else if (hcd_uses_dma(hcd)) { |
|---|
| 1431 | + if (object_is_on_stack(urb->setup_packet)) { |
|---|
| 1527 | 1432 | WARN_ONCE(1, "setup packet is on stack\n"); |
|---|
| 1528 | 1433 | return -EAGAIN; |
|---|
| 1529 | 1434 | } |
|---|
| .. | .. |
|---|
| 1537 | 1442 | urb->setup_dma)) |
|---|
| 1538 | 1443 | return -EAGAIN; |
|---|
| 1539 | 1444 | 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 | 1445 | } |
|---|
| 1551 | 1446 | } |
|---|
| 1552 | 1447 | |
|---|
| 1553 | 1448 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
|---|
| 1554 | 1449 | if (urb->transfer_buffer_length != 0 |
|---|
| 1555 | 1450 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { |
|---|
| 1556 | | - if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) { |
|---|
| 1451 | + if (hcd->localmem_pool) { |
|---|
| 1452 | + ret = hcd_alloc_coherent( |
|---|
| 1453 | + urb->dev->bus, mem_flags, |
|---|
| 1454 | + &urb->transfer_dma, |
|---|
| 1455 | + &urb->transfer_buffer, |
|---|
| 1456 | + urb->transfer_buffer_length, |
|---|
| 1457 | + dir); |
|---|
| 1458 | + if (ret == 0) |
|---|
| 1459 | + urb->transfer_flags |= URB_MAP_LOCAL; |
|---|
| 1460 | + } else if (hcd_uses_dma(hcd)) { |
|---|
| 1557 | 1461 | if (urb->num_sgs) { |
|---|
| 1558 | 1462 | int n; |
|---|
| 1559 | 1463 | |
|---|
| .. | .. |
|---|
| 1589 | 1493 | ret = -EAGAIN; |
|---|
| 1590 | 1494 | else |
|---|
| 1591 | 1495 | 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 | 1496 | } else if (object_is_on_stack(urb->transfer_buffer)) { |
|---|
| 1596 | 1497 | WARN_ONCE(1, "transfer buffer is on stack\n"); |
|---|
| 1597 | 1498 | ret = -EAGAIN; |
|---|
| .. | .. |
|---|
| 1607 | 1508 | else |
|---|
| 1608 | 1509 | urb->transfer_flags |= URB_DMA_MAP_SINGLE; |
|---|
| 1609 | 1510 | } |
|---|
| 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 | 1511 | } |
|---|
| 1620 | 1512 | if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE | |
|---|
| 1621 | 1513 | URB_SETUP_MAP_LOCAL))) |
|---|
| .. | .. |
|---|
| 1752 | 1644 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); |
|---|
| 1753 | 1645 | struct usb_anchor *anchor = urb->anchor; |
|---|
| 1754 | 1646 | int status = urb->unlinked; |
|---|
| 1755 | | - unsigned long flags; |
|---|
| 1756 | 1647 | |
|---|
| 1757 | 1648 | urb->hcpriv = NULL; |
|---|
| 1758 | 1649 | if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && |
|---|
| .. | .. |
|---|
| 1769 | 1660 | |
|---|
| 1770 | 1661 | /* pass ownership to the completion handler */ |
|---|
| 1771 | 1662 | urb->status = status; |
|---|
| 1772 | | - |
|---|
| 1773 | 1663 | /* |
|---|
| 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. |
|---|
| 1664 | + * This function can be called in task context inside another remote |
|---|
| 1665 | + * coverage collection section, but KCOV doesn't support that kind of |
|---|
| 1666 | + * recursion yet. Only collect coverage in softirq context for now. |
|---|
| 1782 | 1667 | */ |
|---|
| 1783 | | - local_irq_save(flags); |
|---|
| 1668 | + if (in_serving_softirq()) |
|---|
| 1669 | + kcov_remote_start_usb((u64)urb->dev->bus->busnum); |
|---|
| 1784 | 1670 | urb->complete(urb); |
|---|
| 1785 | | - local_irq_restore(flags); |
|---|
| 1671 | + if (in_serving_softirq()) |
|---|
| 1672 | + kcov_remote_stop(); |
|---|
| 1786 | 1673 | |
|---|
| 1787 | 1674 | usb_anchor_resume_wakeups(anchor); |
|---|
| 1788 | 1675 | atomic_dec(&urb->use_count); |
|---|
| .. | .. |
|---|
| 1798 | 1685 | usb_put_urb(urb); |
|---|
| 1799 | 1686 | } |
|---|
| 1800 | 1687 | |
|---|
| 1801 | | -static void usb_giveback_urb_bh(unsigned long param) |
|---|
| 1688 | +static void usb_giveback_urb_bh(struct tasklet_struct *t) |
|---|
| 1802 | 1689 | { |
|---|
| 1803 | | - struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param; |
|---|
| 1690 | + struct giveback_urb_bh *bh = from_tasklet(bh, t, bh); |
|---|
| 1804 | 1691 | struct list_head local_list; |
|---|
| 1805 | 1692 | |
|---|
| 1806 | 1693 | spin_lock_irq(&bh->lock); |
|---|
| .. | .. |
|---|
| 1912 | 1799 | /* kick hcd */ |
|---|
| 1913 | 1800 | unlink1(hcd, urb, -ESHUTDOWN); |
|---|
| 1914 | 1801 | dev_dbg (hcd->self.controller, |
|---|
| 1915 | | - "shutdown urb %pK ep%d%s%s\n", |
|---|
| 1802 | + "shutdown urb %pK ep%d%s-%s\n", |
|---|
| 1916 | 1803 | urb, usb_endpoint_num(&ep->desc), |
|---|
| 1917 | 1804 | 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 | | - })); |
|---|
| 1805 | + usb_ep_type_string(usb_endpoint_type(&ep->desc))); |
|---|
| 1932 | 1806 | usb_put_urb (urb); |
|---|
| 1933 | 1807 | |
|---|
| 1934 | 1808 | /* list contents may have changed */ |
|---|
| .. | .. |
|---|
| 2254 | 2128 | return hcd->driver->get_frame_number (hcd); |
|---|
| 2255 | 2129 | } |
|---|
| 2256 | 2130 | |
|---|
| 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 | 2131 | /*-------------------------------------------------------------------------*/ |
|---|
| 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 | 2132 | |
|---|
| 2323 | 2133 | #ifdef CONFIG_PM |
|---|
| 2324 | 2134 | |
|---|
| .. | .. |
|---|
| 2403 | 2213 | hcd->state = HC_STATE_RESUMING; |
|---|
| 2404 | 2214 | status = hcd->driver->bus_resume(hcd); |
|---|
| 2405 | 2215 | clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); |
|---|
| 2216 | + if (status == 0) |
|---|
| 2217 | + status = usb_phy_roothub_calibrate(hcd->phy_roothub); |
|---|
| 2218 | + |
|---|
| 2406 | 2219 | if (status == 0) { |
|---|
| 2407 | 2220 | struct usb_device *udev; |
|---|
| 2408 | 2221 | int port1; |
|---|
| .. | .. |
|---|
| 2546 | 2359 | |
|---|
| 2547 | 2360 | /*-------------------------------------------------------------------------*/ |
|---|
| 2548 | 2361 | |
|---|
| 2362 | +/* Workqueue routine for when the root-hub has died. */ |
|---|
| 2363 | +static void hcd_died_work(struct work_struct *work) |
|---|
| 2364 | +{ |
|---|
| 2365 | + struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work); |
|---|
| 2366 | + static char *env[] = { |
|---|
| 2367 | + "ERROR=DEAD", |
|---|
| 2368 | + NULL |
|---|
| 2369 | + }; |
|---|
| 2370 | + |
|---|
| 2371 | + /* Notify user space that the host controller has died */ |
|---|
| 2372 | + kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env); |
|---|
| 2373 | +} |
|---|
| 2374 | + |
|---|
| 2549 | 2375 | /** |
|---|
| 2550 | 2376 | * usb_hc_died - report abnormal shutdown of a host controller (bus glue) |
|---|
| 2551 | 2377 | * @hcd: pointer to the HCD representing the controller |
|---|
| .. | .. |
|---|
| 2586 | 2412 | usb_kick_hub_wq(hcd->self.root_hub); |
|---|
| 2587 | 2413 | } |
|---|
| 2588 | 2414 | } |
|---|
| 2415 | + |
|---|
| 2416 | + /* Handle the case where this function gets called with a shared HCD */ |
|---|
| 2417 | + if (usb_hcd_is_primary_hcd(hcd)) |
|---|
| 2418 | + schedule_work(&hcd->died_work); |
|---|
| 2419 | + else |
|---|
| 2420 | + schedule_work(&hcd->primary_hcd->died_work); |
|---|
| 2421 | + |
|---|
| 2589 | 2422 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
|---|
| 2590 | 2423 | /* Make sure that the other roothub is also deallocated. */ |
|---|
| 2591 | | - usb_atomic_notify_dead_bus(&hcd->self); |
|---|
| 2592 | 2424 | } |
|---|
| 2593 | 2425 | EXPORT_SYMBOL_GPL (usb_hc_died); |
|---|
| 2594 | 2426 | |
|---|
| .. | .. |
|---|
| 2599 | 2431 | |
|---|
| 2600 | 2432 | spin_lock_init(&bh->lock); |
|---|
| 2601 | 2433 | INIT_LIST_HEAD(&bh->head); |
|---|
| 2602 | | - tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh); |
|---|
| 2434 | + tasklet_setup(&bh->bh, usb_giveback_urb_bh); |
|---|
| 2603 | 2435 | } |
|---|
| 2604 | 2436 | |
|---|
| 2605 | 2437 | struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver, |
|---|
| .. | .. |
|---|
| 2647 | 2479 | hcd->self.controller = dev; |
|---|
| 2648 | 2480 | hcd->self.sysdev = sysdev; |
|---|
| 2649 | 2481 | hcd->self.bus_name = bus_name; |
|---|
| 2650 | | - hcd->self.uses_dma = (sysdev->dma_mask != NULL); |
|---|
| 2651 | 2482 | |
|---|
| 2652 | 2483 | timer_setup(&hcd->rh_timer, rh_timer_func, 0); |
|---|
| 2653 | 2484 | #ifdef CONFIG_PM |
|---|
| 2654 | 2485 | INIT_WORK(&hcd->wakeup_work, hcd_resume_work); |
|---|
| 2655 | 2486 | #endif |
|---|
| 2487 | + |
|---|
| 2488 | + INIT_WORK(&hcd->died_work, hcd_died_work); |
|---|
| 2656 | 2489 | |
|---|
| 2657 | 2490 | hcd->driver = driver; |
|---|
| 2658 | 2491 | hcd->speed = driver->flags & HCD_MASK; |
|---|
| .. | .. |
|---|
| 2828 | 2661 | { |
|---|
| 2829 | 2662 | int retval; |
|---|
| 2830 | 2663 | struct usb_device *rhdev; |
|---|
| 2664 | + struct usb_hcd *shared_hcd; |
|---|
| 2831 | 2665 | |
|---|
| 2832 | 2666 | if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) { |
|---|
| 2833 | 2667 | hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); |
|---|
| .. | .. |
|---|
| 2838 | 2672 | if (retval) |
|---|
| 2839 | 2673 | return retval; |
|---|
| 2840 | 2674 | |
|---|
| 2675 | + retval = usb_phy_roothub_set_mode(hcd->phy_roothub, |
|---|
| 2676 | + PHY_MODE_USB_HOST_SS); |
|---|
| 2677 | + if (retval) |
|---|
| 2678 | + retval = usb_phy_roothub_set_mode(hcd->phy_roothub, |
|---|
| 2679 | + PHY_MODE_USB_HOST); |
|---|
| 2680 | + if (retval) |
|---|
| 2681 | + goto err_usb_phy_roothub_power_on; |
|---|
| 2682 | + |
|---|
| 2841 | 2683 | retval = usb_phy_roothub_power_on(hcd->phy_roothub); |
|---|
| 2842 | 2684 | if (retval) |
|---|
| 2843 | 2685 | goto err_usb_phy_roothub_power_on; |
|---|
| .. | .. |
|---|
| 2845 | 2687 | |
|---|
| 2846 | 2688 | dev_info(hcd->self.controller, "%s\n", hcd->product_desc); |
|---|
| 2847 | 2689 | |
|---|
| 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); |
|---|
| 2690 | + switch (authorized_default) { |
|---|
| 2691 | + case USB_AUTHORIZE_NONE: |
|---|
| 2692 | + hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE; |
|---|
| 2693 | + break; |
|---|
| 2694 | + |
|---|
| 2695 | + case USB_AUTHORIZE_ALL: |
|---|
| 2696 | + hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL; |
|---|
| 2697 | + break; |
|---|
| 2698 | + |
|---|
| 2699 | + case USB_AUTHORIZE_INTERNAL: |
|---|
| 2700 | + hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL; |
|---|
| 2701 | + break; |
|---|
| 2702 | + |
|---|
| 2703 | + case USB_AUTHORIZE_WIRED: |
|---|
| 2704 | + default: |
|---|
| 2705 | + hcd->dev_policy = hcd->wireless ? |
|---|
| 2706 | + USB_DEVICE_AUTHORIZE_NONE : USB_DEVICE_AUTHORIZE_ALL; |
|---|
| 2707 | + break; |
|---|
| 2859 | 2708 | } |
|---|
| 2709 | + |
|---|
| 2860 | 2710 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
|---|
| 2861 | 2711 | |
|---|
| 2862 | 2712 | /* per default all interfaces are authorized */ |
|---|
| .. | .. |
|---|
| 2905 | 2755 | case HCD_USB32: |
|---|
| 2906 | 2756 | rhdev->rx_lanes = 2; |
|---|
| 2907 | 2757 | rhdev->tx_lanes = 2; |
|---|
| 2908 | | - /* fall through */ |
|---|
| 2758 | + fallthrough; |
|---|
| 2909 | 2759 | case HCD_USB31: |
|---|
| 2910 | 2760 | rhdev->speed = USB_SPEED_SUPER_PLUS; |
|---|
| 2911 | 2761 | break; |
|---|
| .. | .. |
|---|
| 2939 | 2789 | } |
|---|
| 2940 | 2790 | hcd->rh_pollable = 1; |
|---|
| 2941 | 2791 | |
|---|
| 2792 | + retval = usb_phy_roothub_calibrate(hcd->phy_roothub); |
|---|
| 2793 | + if (retval) |
|---|
| 2794 | + goto err_hcd_driver_setup; |
|---|
| 2795 | + |
|---|
| 2942 | 2796 | /* NOTE: root hub and controller capabilities may not be the same */ |
|---|
| 2943 | 2797 | if (device_can_wakeup(hcd->self.controller) |
|---|
| 2944 | 2798 | && device_can_wakeup(&hcd->self.root_hub->dev)) |
|---|
| .. | .. |
|---|
| 2964 | 2818 | goto err_hcd_driver_start; |
|---|
| 2965 | 2819 | } |
|---|
| 2966 | 2820 | |
|---|
| 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; |
|---|
| 2821 | + /* starting here, usbcore will pay attention to the shared HCD roothub */ |
|---|
| 2822 | + shared_hcd = hcd->shared_hcd; |
|---|
| 2823 | + if (!usb_hcd_is_primary_hcd(hcd) && shared_hcd && HCD_DEFER_RH_REGISTER(shared_hcd)) { |
|---|
| 2824 | + retval = register_root_hub(shared_hcd); |
|---|
| 2825 | + if (retval != 0) |
|---|
| 2826 | + goto err_register_root_hub; |
|---|
| 2971 | 2827 | |
|---|
| 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; |
|---|
| 2828 | + if (shared_hcd->uses_new_polling && HCD_POLL_RH(shared_hcd)) |
|---|
| 2829 | + usb_hcd_poll_rh_status(shared_hcd); |
|---|
| 2977 | 2830 | } |
|---|
| 2978 | | - if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) |
|---|
| 2979 | | - usb_hcd_poll_rh_status(hcd); |
|---|
| 2831 | + |
|---|
| 2832 | + /* starting here, usbcore will pay attention to this root hub */ |
|---|
| 2833 | + if (!HCD_DEFER_RH_REGISTER(hcd)) { |
|---|
| 2834 | + retval = register_root_hub(hcd); |
|---|
| 2835 | + if (retval != 0) |
|---|
| 2836 | + goto err_register_root_hub; |
|---|
| 2837 | + |
|---|
| 2838 | + if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) |
|---|
| 2839 | + usb_hcd_poll_rh_status(hcd); |
|---|
| 2840 | + } |
|---|
| 2980 | 2841 | |
|---|
| 2981 | 2842 | return retval; |
|---|
| 2982 | 2843 | |
|---|
| 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 | 2844 | err_register_root_hub: |
|---|
| 2998 | 2845 | hcd->rh_pollable = 0; |
|---|
| 2999 | 2846 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
|---|
| .. | .. |
|---|
| 3033 | 2880 | void usb_remove_hcd(struct usb_hcd *hcd) |
|---|
| 3034 | 2881 | { |
|---|
| 3035 | 2882 | struct usb_device *rhdev = hcd->self.root_hub; |
|---|
| 2883 | + bool rh_registered; |
|---|
| 3036 | 2884 | |
|---|
| 3037 | 2885 | dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); |
|---|
| 3038 | 2886 | |
|---|
| 3039 | 2887 | usb_get_dev(rhdev); |
|---|
| 3040 | | - sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group); |
|---|
| 3041 | | - |
|---|
| 3042 | 2888 | clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
|---|
| 3043 | 2889 | if (HC_IS_RUNNING (hcd->state)) |
|---|
| 3044 | 2890 | hcd->state = HC_STATE_QUIESCING; |
|---|
| 3045 | 2891 | |
|---|
| 3046 | 2892 | dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); |
|---|
| 3047 | 2893 | spin_lock_irq (&hcd_root_hub_lock); |
|---|
| 2894 | + rh_registered = hcd->rh_registered; |
|---|
| 3048 | 2895 | hcd->rh_registered = 0; |
|---|
| 3049 | 2896 | spin_unlock_irq (&hcd_root_hub_lock); |
|---|
| 3050 | 2897 | |
|---|
| 3051 | 2898 | #ifdef CONFIG_PM |
|---|
| 3052 | 2899 | cancel_work_sync(&hcd->wakeup_work); |
|---|
| 3053 | 2900 | #endif |
|---|
| 2901 | + cancel_work_sync(&hcd->died_work); |
|---|
| 3054 | 2902 | |
|---|
| 3055 | 2903 | mutex_lock(&usb_bus_idr_lock); |
|---|
| 3056 | | - usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
|---|
| 2904 | + if (rh_registered) |
|---|
| 2905 | + usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
|---|
| 3057 | 2906 | mutex_unlock(&usb_bus_idr_lock); |
|---|
| 3058 | 2907 | |
|---|
| 3059 | 2908 | /* |
|---|
| .. | .. |
|---|
| 3111 | 2960 | } |
|---|
| 3112 | 2961 | EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); |
|---|
| 3113 | 2962 | |
|---|
| 2963 | +int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr, |
|---|
| 2964 | + dma_addr_t dma, size_t size) |
|---|
| 2965 | +{ |
|---|
| 2966 | + int err; |
|---|
| 2967 | + void *local_mem; |
|---|
| 2968 | + |
|---|
| 2969 | + hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4, |
|---|
| 2970 | + dev_to_node(hcd->self.sysdev), |
|---|
| 2971 | + dev_name(hcd->self.sysdev)); |
|---|
| 2972 | + if (IS_ERR(hcd->localmem_pool)) |
|---|
| 2973 | + return PTR_ERR(hcd->localmem_pool); |
|---|
| 2974 | + |
|---|
| 2975 | + local_mem = devm_memremap(hcd->self.sysdev, phys_addr, |
|---|
| 2976 | + size, MEMREMAP_WC); |
|---|
| 2977 | + if (IS_ERR(local_mem)) |
|---|
| 2978 | + return PTR_ERR(local_mem); |
|---|
| 2979 | + |
|---|
| 2980 | + /* |
|---|
| 2981 | + * Here we pass a dma_addr_t but the arg type is a phys_addr_t. |
|---|
| 2982 | + * It's not backed by system memory and thus there's no kernel mapping |
|---|
| 2983 | + * for it. |
|---|
| 2984 | + */ |
|---|
| 2985 | + err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem, |
|---|
| 2986 | + dma, size, dev_to_node(hcd->self.sysdev)); |
|---|
| 2987 | + if (err < 0) { |
|---|
| 2988 | + dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n", |
|---|
| 2989 | + err); |
|---|
| 2990 | + return err; |
|---|
| 2991 | + } |
|---|
| 2992 | + |
|---|
| 2993 | + return 0; |
|---|
| 2994 | +} |
|---|
| 2995 | +EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem); |
|---|
| 2996 | + |
|---|
| 3114 | 2997 | /*-------------------------------------------------------------------------*/ |
|---|
| 3115 | 2998 | |
|---|
| 3116 | 2999 | #if IS_ENABLED(CONFIG_USB_MON) |
|---|