Merge tag 'usb-5.7-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb

Pull USB fixes from Greg KH:
"Here are a number of USB fixes for 5.7-rc6

The "largest" in here is a bunch of raw-gadget fixes and api changes
as the driver just showed up in -rc1 and work has been done to fix up
some uapi issues found with the original submission, before it shows
up in a -final release.

Other than that, a bunch of other small USB gadget fixes, xhci fixes,
some quirks, andother tiny fixes for reported issues.

All of these have been in linux-next with no reported issues"

* tag 'usb-5.7-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb: (26 commits)
USB: gadget: fix illegal array access in binding with UDC
usb: core: hub: limit HUB_QUIRK_DISABLE_AUTOSUSPEND to USB5534B
USB: usbfs: fix mmap dma mismatch
usb: host: xhci-plat: keep runtime active when removing host
usb: xhci: Fix NULL pointer dereference when enqueuing trbs from urb sg list
usb: cdns3: gadget: make a bunch of functions static
usb: mtu3: constify struct debugfs_reg32
usb: gadget: udc: atmel: Make some symbols static
usb: raw-gadget: fix null-ptr-deref when reenabling endpoints
usb: raw-gadget: documentation updates
usb: raw-gadget: support stalling/halting/wedging endpoints
usb: raw-gadget: fix gadget endpoint selection
usb: raw-gadget: improve uapi headers comments
usb: typec: mux: intel: Fix DP_HPD_LVL bit field
usb: raw-gadget: fix return value of ep read ioctls
usb: dwc3: select USB_ROLE_SWITCH
usb: gadget: legacy: fix error return code in gncm_bind()
usb: gadget: legacy: fix error return code in cdc_bind()
usb: gadget: legacy: fix redundant initialization warnings
usb: gadget: tegra-xudc: Fix idle suspend/resume
...

Changed files
+448 -123
Documentation
drivers
include
uapi
linux
+30 -7
Documentation/usb/raw-gadget.rst
··· 27 27 3. Raw Gadget provides a way to select a UDC device/driver to bind to, 28 28 while GadgetFS currently binds to the first available UDC. 29 29 30 - 4. Raw Gadget uses predictable endpoint names (handles) across different 31 - UDCs (as long as UDCs have enough endpoints of each required transfer 32 - type). 30 + 4. Raw Gadget explicitly exposes information about endpoints addresses and 31 + capabilities allowing a user to write UDC-agnostic gadgets. 33 32 34 33 5. Raw Gadget has ioctl-based interface instead of a filesystem-based one. 35 34 ··· 49 50 Raw Gadget and react to those depending on what kind of USB device 50 51 needs to be emulated. 51 52 53 + Note, that some UDC drivers have fixed addresses assigned to endpoints, and 54 + therefore arbitrary endpoint addresses can't be used in the descriptors. 55 + Nevertheles, Raw Gadget provides a UDC-agnostic way to write USB gadgets. 56 + Once a USB_RAW_EVENT_CONNECT event is received via USB_RAW_IOCTL_EVENT_FETCH, 57 + the USB_RAW_IOCTL_EPS_INFO ioctl can be used to find out information about 58 + endpoints that the UDC driver has. Based on that information, the user must 59 + chose UDC endpoints that will be used for the gadget being emulated, and 60 + properly assign addresses in endpoint descriptors. 61 + 62 + You can find usage examples (along with a test suite) here: 63 + 64 + https://github.com/xairy/raw-gadget 65 + 66 + Internal details 67 + ~~~~~~~~~~~~~~~~ 68 + 69 + Currently every endpoint read/write ioctl submits a USB request and waits until 70 + its completion. This is the desired mode for coverage-guided fuzzing (as we'd 71 + like all USB request processing happen during the lifetime of a syscall), 72 + and must be kept in the implementation. (This might be slow for real world 73 + applications, thus the O_NONBLOCK improvement suggestion below.) 74 + 52 75 Potential future improvements 53 76 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 54 77 55 - - Implement ioctl's for setting/clearing halt status on endpoints. 56 - 57 - - Reporting more events (suspend, resume, etc.) through 58 - USB_RAW_IOCTL_EVENT_FETCH. 78 + - Report more events (suspend, resume, etc.) through USB_RAW_IOCTL_EVENT_FETCH. 59 79 60 80 - Support O_NONBLOCK I/O. 81 + 82 + - Support USB 3 features (accept SS endpoint companion descriptor when 83 + enabling endpoints; allow providing stream_id for bulk transfers). 84 + 85 + - Support ISO transfer features (expose frame_number for completed requests).
+11 -11
drivers/usb/cdns3/gadget.c
··· 82 82 * @ptr: address of device controller register to be read and changed 83 83 * @mask: bits requested to clar 84 84 */ 85 - void cdns3_clear_register_bit(void __iomem *ptr, u32 mask) 85 + static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask) 86 86 { 87 87 mask = readl(ptr) & ~mask; 88 88 writel(mask, ptr); ··· 137 137 * 138 138 * Returns buffer or NULL if no buffers in list 139 139 */ 140 - struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list) 140 + static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list) 141 141 { 142 142 return list_first_entry_or_null(list, struct cdns3_aligned_buf, list); 143 143 } ··· 148 148 * 149 149 * Returns request or NULL if no requests in list 150 150 */ 151 - struct cdns3_request *cdns3_next_priv_request(struct list_head *list) 151 + static struct cdns3_request *cdns3_next_priv_request(struct list_head *list) 152 152 { 153 153 return list_first_entry_or_null(list, struct cdns3_request, list); 154 154 } ··· 190 190 return priv_ep->trb_pool_dma + offset; 191 191 } 192 192 193 - int cdns3_ring_size(struct cdns3_endpoint *priv_ep) 193 + static int cdns3_ring_size(struct cdns3_endpoint *priv_ep) 194 194 { 195 195 switch (priv_ep->type) { 196 196 case USB_ENDPOINT_XFER_ISOC: ··· 345 345 cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs); 346 346 } 347 347 348 - void cdns3_move_deq_to_next_trb(struct cdns3_request *priv_req) 348 + static void cdns3_move_deq_to_next_trb(struct cdns3_request *priv_req) 349 349 { 350 350 struct cdns3_endpoint *priv_ep = priv_req->priv_ep; 351 351 int current_trb = priv_req->start_trb; ··· 511 511 } 512 512 } 513 513 514 - struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev, 514 + static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev, 515 515 struct cdns3_endpoint *priv_ep, 516 516 struct cdns3_request *priv_req) 517 517 { ··· 551 551 return &priv_req->request; 552 552 } 553 553 554 - int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev, 554 + static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev, 555 555 struct cdns3_endpoint *priv_ep, 556 556 struct cdns3_request *priv_req) 557 557 { ··· 836 836 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request); 837 837 } 838 838 839 - void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep) 839 + static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep) 840 840 { 841 841 /* Work around for stale data address in TRB*/ 842 842 if (priv_ep->wa1_set) { ··· 1904 1904 return 0; 1905 1905 } 1906 1906 1907 - void cdns3_stream_ep_reconfig(struct cdns3_device *priv_dev, 1907 + static void cdns3_stream_ep_reconfig(struct cdns3_device *priv_dev, 1908 1908 struct cdns3_endpoint *priv_ep) 1909 1909 { 1910 1910 if (!priv_ep->use_streams || priv_dev->gadget.speed < USB_SPEED_SUPER) ··· 1925 1925 EP_CFG_TDL_CHK | EP_CFG_SID_CHK); 1926 1926 } 1927 1927 1928 - void cdns3_configure_dmult(struct cdns3_device *priv_dev, 1928 + static void cdns3_configure_dmult(struct cdns3_device *priv_dev, 1929 1929 struct cdns3_endpoint *priv_ep) 1930 1930 { 1931 1931 struct cdns3_usb_regs __iomem *regs = priv_dev->regs; ··· 2548 2548 link_trb = priv_req->trb; 2549 2549 2550 2550 /* Update ring only if removed request is on pending_req_list list */ 2551 - if (req_on_hw_ring) { 2551 + if (req_on_hw_ring && link_trb) { 2552 2552 link_trb->buffer = TRB_BUFFER(priv_ep->trb_pool_dma + 2553 2553 ((priv_req->end_trb + 1) * TRB_SIZE)); 2554 2554 link_trb->control = (link_trb->control & TRB_CYCLE) |
+13 -3
drivers/usb/core/devio.c
··· 251 251 usbm->vma_use_count = 1; 252 252 INIT_LIST_HEAD(&usbm->memlist); 253 253 254 - if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle, size)) { 255 - dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 256 - return -EAGAIN; 254 + if (hcd->localmem_pool || !hcd_uses_dma(hcd)) { 255 + if (remap_pfn_range(vma, vma->vm_start, 256 + virt_to_phys(usbm->mem) >> PAGE_SHIFT, 257 + size, vma->vm_page_prot) < 0) { 258 + dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 259 + return -EAGAIN; 260 + } 261 + } else { 262 + if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle, 263 + size)) { 264 + dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 265 + return -EAGAIN; 266 + } 257 267 } 258 268 259 269 vma->vm_flags |= VM_IO;
+5 -1
drivers/usb/core/hub.c
··· 39 39 40 40 #define USB_VENDOR_GENESYS_LOGIC 0x05e3 41 41 #define USB_VENDOR_SMSC 0x0424 42 + #define USB_PRODUCT_USB5534B 0x5534 42 43 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01 43 44 #define HUB_QUIRK_DISABLE_AUTOSUSPEND 0x02 44 45 ··· 5622 5621 } 5623 5622 5624 5623 static const struct usb_device_id hub_id_table[] = { 5625 - { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_CLASS, 5624 + { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5625 + | USB_DEVICE_ID_MATCH_PRODUCT 5626 + | USB_DEVICE_ID_MATCH_INT_CLASS, 5626 5627 .idVendor = USB_VENDOR_SMSC, 5628 + .idProduct = USB_PRODUCT_USB5534B, 5627 5629 .bInterfaceClass = USB_CLASS_HUB, 5628 5630 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5629 5631 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
+1
drivers/usb/dwc3/Kconfig
··· 4 4 tristate "DesignWare USB3 DRD Core Support" 5 5 depends on (USB || USB_GADGET) && HAS_DMA 6 6 select USB_XHCI_PLATFORM if USB_XHCI_HCD 7 + select USB_ROLE_SWITCH if USB_DWC3_DUAL_ROLE 7 8 help 8 9 Say Y or M here if your system has a Dual Role SuperSpeed 9 10 USB controller based on the DesignWare USB3 IP Core.
+1
drivers/usb/dwc3/dwc3-pci.c
··· 114 114 115 115 static const struct property_entry dwc3_pci_mrfld_properties[] = { 116 116 PROPERTY_ENTRY_STRING("dr_mode", "otg"), 117 + PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"), 117 118 PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 118 119 {} 119 120 };
-3
drivers/usb/dwc3/gadget.c
··· 2483 2483 for_each_sg(sg, s, pending, i) { 2484 2484 trb = &dep->trb_pool[dep->trb_dequeue]; 2485 2485 2486 - if (trb->ctrl & DWC3_TRB_CTRL_HWO) 2487 - break; 2488 - 2489 2486 req->sg = sg_next(s); 2490 2487 req->num_pending_sgs--; 2491 2488
+3
drivers/usb/gadget/configfs.c
··· 260 260 char *name; 261 261 int ret; 262 262 263 + if (strlen(page) < len) 264 + return -EOVERFLOW; 265 + 263 266 name = kstrdup(page, GFP_KERNEL); 264 267 if (!name) 265 268 return -ENOMEM;
+3 -1
drivers/usb/gadget/legacy/audio.c
··· 300 300 struct usb_descriptor_header *usb_desc; 301 301 302 302 usb_desc = usb_otg_descriptor_alloc(cdev->gadget); 303 - if (!usb_desc) 303 + if (!usb_desc) { 304 + status = -ENOMEM; 304 305 goto fail; 306 + } 305 307 usb_otg_descriptor_init(cdev->gadget, usb_desc); 306 308 otg_desc[0] = usb_desc; 307 309 otg_desc[1] = NULL;
+3 -1
drivers/usb/gadget/legacy/cdc2.c
··· 179 179 struct usb_descriptor_header *usb_desc; 180 180 181 181 usb_desc = usb_otg_descriptor_alloc(gadget); 182 - if (!usb_desc) 182 + if (!usb_desc) { 183 + status = -ENOMEM; 183 184 goto fail1; 185 + } 184 186 usb_otg_descriptor_init(gadget, usb_desc); 185 187 otg_desc[0] = usb_desc; 186 188 otg_desc[1] = NULL;
+1 -2
drivers/usb/gadget/legacy/inode.c
··· 1361 1361 1362 1362 req->buf = dev->rbuf; 1363 1363 req->context = NULL; 1364 - value = -EOPNOTSUPP; 1365 1364 switch (ctrl->bRequest) { 1366 1365 1367 1366 case USB_REQ_GET_DESCRIPTOR: ··· 1783 1784 dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr) 1784 1785 { 1785 1786 struct dev_data *dev = fd->private_data; 1786 - ssize_t value = len, length = len; 1787 + ssize_t value, length = len; 1787 1788 unsigned total; 1788 1789 u32 tag; 1789 1790 char *kbuf;
+3 -1
drivers/usb/gadget/legacy/ncm.c
··· 156 156 struct usb_descriptor_header *usb_desc; 157 157 158 158 usb_desc = usb_otg_descriptor_alloc(gadget); 159 - if (!usb_desc) 159 + if (!usb_desc) { 160 + status = -ENOMEM; 160 161 goto fail; 162 + } 161 163 usb_otg_descriptor_init(gadget, usb_desc); 162 164 otg_desc[0] = usb_desc; 163 165 otg_desc[1] = NULL;
+252 -63
drivers/usb/gadget/legacy/raw_gadget.c
··· 7 7 */ 8 8 9 9 #include <linux/compiler.h> 10 + #include <linux/ctype.h> 10 11 #include <linux/debugfs.h> 11 12 #include <linux/delay.h> 12 13 #include <linux/kref.h> ··· 124 123 125 124 struct raw_dev; 126 125 127 - #define USB_RAW_MAX_ENDPOINTS 32 128 - 129 126 enum ep_state { 130 127 STATE_EP_DISABLED, 131 128 STATE_EP_ENABLED, ··· 133 134 struct raw_dev *dev; 134 135 enum ep_state state; 135 136 struct usb_ep *ep; 137 + u8 addr; 136 138 struct usb_request *req; 137 139 bool urb_queued; 138 140 bool disabling; ··· 168 168 bool ep0_out_pending; 169 169 bool ep0_urb_queued; 170 170 ssize_t ep0_status; 171 - struct raw_ep eps[USB_RAW_MAX_ENDPOINTS]; 171 + struct raw_ep eps[USB_RAW_EPS_NUM_MAX]; 172 + int eps_num; 172 173 173 174 struct completion ep0_done; 174 175 struct raw_event_queue queue; ··· 203 202 usb_ep_free_request(dev->gadget->ep0, dev->req); 204 203 } 205 204 raw_event_queue_destroy(&dev->queue); 206 - for (i = 0; i < USB_RAW_MAX_ENDPOINTS; i++) { 207 - if (dev->eps[i].state != STATE_EP_ENABLED) 205 + for (i = 0; i < dev->eps_num; i++) { 206 + if (dev->eps[i].state == STATE_EP_DISABLED) 208 207 continue; 209 208 usb_ep_disable(dev->eps[i].ep); 210 209 usb_ep_free_request(dev->eps[i].ep, dev->eps[i].req); ··· 250 249 complete(&dev->ep0_done); 251 250 } 252 251 252 + static u8 get_ep_addr(const char *name) 253 + { 254 + /* If the endpoint has fixed function (named as e.g. "ep12out-bulk"), 255 + * parse the endpoint address from its name. We deliberately use 256 + * deprecated simple_strtoul() function here, as the number isn't 257 + * followed by '\0' nor '\n'. 258 + */ 259 + if (isdigit(name[2])) 260 + return simple_strtoul(&name[2], NULL, 10); 261 + /* Otherwise the endpoint is configurable (named as e.g. "ep-a"). */ 262 + return USB_RAW_EP_ADDR_ANY; 263 + } 264 + 253 265 static int gadget_bind(struct usb_gadget *gadget, 254 266 struct usb_gadget_driver *driver) 255 267 { 256 - int ret = 0; 268 + int ret = 0, i = 0; 257 269 struct raw_dev *dev = container_of(driver, struct raw_dev, driver); 258 270 struct usb_request *req; 271 + struct usb_ep *ep; 259 272 unsigned long flags; 260 273 261 274 if (strcmp(gadget->name, dev->udc_name) != 0) ··· 288 273 dev->req->context = dev; 289 274 dev->req->complete = gadget_ep0_complete; 290 275 dev->gadget = gadget; 276 + gadget_for_each_ep(ep, dev->gadget) { 277 + dev->eps[i].ep = ep; 278 + dev->eps[i].addr = get_ep_addr(ep->name); 279 + dev->eps[i].state = STATE_EP_DISABLED; 280 + i++; 281 + } 282 + dev->eps_num = i; 291 283 spin_unlock_irqrestore(&dev->lock, flags); 292 284 293 285 /* Matches kref_put() in gadget_unbind(). */ ··· 577 555 578 556 if (copy_from_user(io, ptr, sizeof(*io))) 579 557 return ERR_PTR(-EFAULT); 580 - if (io->ep >= USB_RAW_MAX_ENDPOINTS) 558 + if (io->ep >= USB_RAW_EPS_NUM_MAX) 581 559 return ERR_PTR(-EINVAL); 582 560 if (!usb_raw_io_flags_valid(io->flags)) 583 561 return ERR_PTR(-EINVAL); ··· 691 669 if (IS_ERR(data)) 692 670 return PTR_ERR(data); 693 671 ret = raw_process_ep0_io(dev, &io, data, false); 694 - if (ret) 672 + if (ret < 0) 695 673 goto free; 696 674 697 675 length = min(io.length, (unsigned int)ret); 698 676 if (copy_to_user((void __user *)(value + sizeof(io)), data, length)) 699 677 ret = -EFAULT; 678 + else 679 + ret = length; 700 680 free: 701 681 kfree(data); 702 682 return ret; 703 683 } 704 684 705 - static bool check_ep_caps(struct usb_ep *ep, 706 - struct usb_endpoint_descriptor *desc) 685 + static int raw_ioctl_ep0_stall(struct raw_dev *dev, unsigned long value) 707 686 { 708 - switch (usb_endpoint_type(desc)) { 709 - case USB_ENDPOINT_XFER_ISOC: 710 - if (!ep->caps.type_iso) 711 - return false; 712 - break; 713 - case USB_ENDPOINT_XFER_BULK: 714 - if (!ep->caps.type_bulk) 715 - return false; 716 - break; 717 - case USB_ENDPOINT_XFER_INT: 718 - if (!ep->caps.type_int) 719 - return false; 720 - break; 721 - default: 722 - return false; 687 + int ret = 0; 688 + unsigned long flags; 689 + 690 + if (value) 691 + return -EINVAL; 692 + spin_lock_irqsave(&dev->lock, flags); 693 + if (dev->state != STATE_DEV_RUNNING) { 694 + dev_dbg(dev->dev, "fail, device is not running\n"); 695 + ret = -EINVAL; 696 + goto out_unlock; 697 + } 698 + if (!dev->gadget) { 699 + dev_dbg(dev->dev, "fail, gadget is not bound\n"); 700 + ret = -EBUSY; 701 + goto out_unlock; 702 + } 703 + if (dev->ep0_urb_queued) { 704 + dev_dbg(&dev->gadget->dev, "fail, urb already queued\n"); 705 + ret = -EBUSY; 706 + goto out_unlock; 707 + } 708 + if (!dev->ep0_in_pending && !dev->ep0_out_pending) { 709 + dev_dbg(&dev->gadget->dev, "fail, no request pending\n"); 710 + ret = -EBUSY; 711 + goto out_unlock; 723 712 } 724 713 725 - if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in) 726 - return false; 727 - if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out) 728 - return false; 714 + ret = usb_ep_set_halt(dev->gadget->ep0); 715 + if (ret < 0) 716 + dev_err(&dev->gadget->dev, 717 + "fail, usb_ep_set_halt returned %d\n", ret); 729 718 730 - return true; 719 + if (dev->ep0_in_pending) 720 + dev->ep0_in_pending = false; 721 + else 722 + dev->ep0_out_pending = false; 723 + 724 + out_unlock: 725 + spin_unlock_irqrestore(&dev->lock, flags); 726 + return ret; 731 727 } 732 728 733 729 static int raw_ioctl_ep_enable(struct raw_dev *dev, unsigned long value) ··· 753 713 int ret = 0, i; 754 714 unsigned long flags; 755 715 struct usb_endpoint_descriptor *desc; 756 - struct usb_ep *ep = NULL; 716 + struct raw_ep *ep; 757 717 758 718 desc = memdup_user((void __user *)value, sizeof(*desc)); 759 719 if (IS_ERR(desc)) ··· 781 741 goto out_free; 782 742 } 783 743 784 - for (i = 0; i < USB_RAW_MAX_ENDPOINTS; i++) { 785 - if (dev->eps[i].state == STATE_EP_ENABLED) 744 + for (i = 0; i < dev->eps_num; i++) { 745 + ep = &dev->eps[i]; 746 + if (ep->state != STATE_EP_DISABLED) 786 747 continue; 787 - break; 788 - } 789 - if (i == USB_RAW_MAX_ENDPOINTS) { 790 - dev_dbg(&dev->gadget->dev, 791 - "fail, no device endpoints available\n"); 792 - ret = -EBUSY; 793 - goto out_free; 794 - } 795 - 796 - gadget_for_each_ep(ep, dev->gadget) { 797 - if (ep->enabled) 748 + if (ep->addr != usb_endpoint_num(desc) && 749 + ep->addr != USB_RAW_EP_ADDR_ANY) 798 750 continue; 799 - if (!check_ep_caps(ep, desc)) 751 + if (!usb_gadget_ep_match_desc(dev->gadget, ep->ep, desc, NULL)) 800 752 continue; 801 - ep->desc = desc; 802 - ret = usb_ep_enable(ep); 753 + ep->ep->desc = desc; 754 + ret = usb_ep_enable(ep->ep); 803 755 if (ret < 0) { 804 756 dev_err(&dev->gadget->dev, 805 757 "fail, usb_ep_enable returned %d\n", ret); 806 758 goto out_free; 807 759 } 808 - dev->eps[i].req = usb_ep_alloc_request(ep, GFP_ATOMIC); 809 - if (!dev->eps[i].req) { 760 + ep->req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC); 761 + if (!ep->req) { 810 762 dev_err(&dev->gadget->dev, 811 763 "fail, usb_ep_alloc_request failed\n"); 812 - usb_ep_disable(ep); 764 + usb_ep_disable(ep->ep); 813 765 ret = -ENOMEM; 814 766 goto out_free; 815 767 } 816 - dev->eps[i].ep = ep; 817 - dev->eps[i].state = STATE_EP_ENABLED; 818 - ep->driver_data = &dev->eps[i]; 768 + ep->state = STATE_EP_ENABLED; 769 + ep->ep->driver_data = ep; 819 770 ret = i; 820 771 goto out_unlock; 821 772 } ··· 825 794 { 826 795 int ret = 0, i = value; 827 796 unsigned long flags; 828 - const void *desc; 829 - 830 - if (i < 0 || i >= USB_RAW_MAX_ENDPOINTS) 831 - return -EINVAL; 832 797 833 798 spin_lock_irqsave(&dev->lock, flags); 834 799 if (dev->state != STATE_DEV_RUNNING) { ··· 837 810 ret = -EBUSY; 838 811 goto out_unlock; 839 812 } 840 - if (dev->eps[i].state != STATE_EP_ENABLED) { 813 + if (i < 0 || i >= dev->eps_num) { 814 + dev_dbg(dev->dev, "fail, invalid endpoint\n"); 815 + ret = -EBUSY; 816 + goto out_unlock; 817 + } 818 + if (dev->eps[i].state == STATE_EP_DISABLED) { 841 819 dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n"); 842 820 ret = -EINVAL; 843 821 goto out_unlock; ··· 866 834 867 835 spin_lock_irqsave(&dev->lock, flags); 868 836 usb_ep_free_request(dev->eps[i].ep, dev->eps[i].req); 869 - desc = dev->eps[i].ep->desc; 870 - dev->eps[i].ep = NULL; 837 + kfree(dev->eps[i].ep->desc); 871 838 dev->eps[i].state = STATE_EP_DISABLED; 872 - kfree(desc); 873 839 dev->eps[i].disabling = false; 840 + 841 + out_unlock: 842 + spin_unlock_irqrestore(&dev->lock, flags); 843 + return ret; 844 + } 845 + 846 + static int raw_ioctl_ep_set_clear_halt_wedge(struct raw_dev *dev, 847 + unsigned long value, bool set, bool halt) 848 + { 849 + int ret = 0, i = value; 850 + unsigned long flags; 851 + 852 + spin_lock_irqsave(&dev->lock, flags); 853 + if (dev->state != STATE_DEV_RUNNING) { 854 + dev_dbg(dev->dev, "fail, device is not running\n"); 855 + ret = -EINVAL; 856 + goto out_unlock; 857 + } 858 + if (!dev->gadget) { 859 + dev_dbg(dev->dev, "fail, gadget is not bound\n"); 860 + ret = -EBUSY; 861 + goto out_unlock; 862 + } 863 + if (i < 0 || i >= dev->eps_num) { 864 + dev_dbg(dev->dev, "fail, invalid endpoint\n"); 865 + ret = -EBUSY; 866 + goto out_unlock; 867 + } 868 + if (dev->eps[i].state == STATE_EP_DISABLED) { 869 + dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n"); 870 + ret = -EINVAL; 871 + goto out_unlock; 872 + } 873 + if (dev->eps[i].disabling) { 874 + dev_dbg(&dev->gadget->dev, 875 + "fail, disable is in progress\n"); 876 + ret = -EINVAL; 877 + goto out_unlock; 878 + } 879 + if (dev->eps[i].urb_queued) { 880 + dev_dbg(&dev->gadget->dev, 881 + "fail, waiting for urb completion\n"); 882 + ret = -EINVAL; 883 + goto out_unlock; 884 + } 885 + if (usb_endpoint_xfer_isoc(dev->eps[i].ep->desc)) { 886 + dev_dbg(&dev->gadget->dev, 887 + "fail, can't halt/wedge ISO endpoint\n"); 888 + ret = -EINVAL; 889 + goto out_unlock; 890 + } 891 + 892 + if (set && halt) { 893 + ret = usb_ep_set_halt(dev->eps[i].ep); 894 + if (ret < 0) 895 + dev_err(&dev->gadget->dev, 896 + "fail, usb_ep_set_halt returned %d\n", ret); 897 + } else if (!set && halt) { 898 + ret = usb_ep_clear_halt(dev->eps[i].ep); 899 + if (ret < 0) 900 + dev_err(&dev->gadget->dev, 901 + "fail, usb_ep_clear_halt returned %d\n", ret); 902 + } else if (set && !halt) { 903 + ret = usb_ep_set_wedge(dev->eps[i].ep); 904 + if (ret < 0) 905 + dev_err(&dev->gadget->dev, 906 + "fail, usb_ep_set_wedge returned %d\n", ret); 907 + } 874 908 875 909 out_unlock: 876 910 spin_unlock_irqrestore(&dev->lock, flags); ··· 964 866 { 965 867 int ret = 0; 966 868 unsigned long flags; 967 - struct raw_ep *ep = &dev->eps[io->ep]; 869 + struct raw_ep *ep; 968 870 DECLARE_COMPLETION_ONSTACK(done); 969 871 970 872 spin_lock_irqsave(&dev->lock, flags); ··· 978 880 ret = -EBUSY; 979 881 goto out_unlock; 980 882 } 883 + if (io->ep >= dev->eps_num) { 884 + dev_dbg(&dev->gadget->dev, "fail, invalid endpoint\n"); 885 + ret = -EINVAL; 886 + goto out_unlock; 887 + } 888 + ep = &dev->eps[io->ep]; 981 889 if (ep->state != STATE_EP_ENABLED) { 982 890 dev_dbg(&dev->gadget->dev, "fail, endpoint is not enabled\n"); 983 891 ret = -EBUSY; ··· 1068 964 if (IS_ERR(data)) 1069 965 return PTR_ERR(data); 1070 966 ret = raw_process_ep_io(dev, &io, data, false); 1071 - if (ret) 967 + if (ret < 0) 1072 968 goto free; 1073 969 1074 970 length = min(io.length, (unsigned int)ret); 1075 971 if (copy_to_user((void __user *)(value + sizeof(io)), data, length)) 1076 972 ret = -EFAULT; 973 + else 974 + ret = length; 1077 975 free: 1078 976 kfree(data); 1079 977 return ret; ··· 1129 1023 return ret; 1130 1024 } 1131 1025 1026 + static void fill_ep_caps(struct usb_ep_caps *caps, 1027 + struct usb_raw_ep_caps *raw_caps) 1028 + { 1029 + raw_caps->type_control = caps->type_control; 1030 + raw_caps->type_iso = caps->type_iso; 1031 + raw_caps->type_bulk = caps->type_bulk; 1032 + raw_caps->type_int = caps->type_int; 1033 + raw_caps->dir_in = caps->dir_in; 1034 + raw_caps->dir_out = caps->dir_out; 1035 + } 1036 + 1037 + static void fill_ep_limits(struct usb_ep *ep, struct usb_raw_ep_limits *limits) 1038 + { 1039 + limits->maxpacket_limit = ep->maxpacket_limit; 1040 + limits->max_streams = ep->max_streams; 1041 + } 1042 + 1043 + static int raw_ioctl_eps_info(struct raw_dev *dev, unsigned long value) 1044 + { 1045 + int ret = 0, i; 1046 + unsigned long flags; 1047 + struct usb_raw_eps_info *info; 1048 + struct raw_ep *ep; 1049 + 1050 + info = kmalloc(sizeof(*info), GFP_KERNEL); 1051 + if (!info) { 1052 + ret = -ENOMEM; 1053 + goto out; 1054 + } 1055 + 1056 + spin_lock_irqsave(&dev->lock, flags); 1057 + if (dev->state != STATE_DEV_RUNNING) { 1058 + dev_dbg(dev->dev, "fail, device is not running\n"); 1059 + ret = -EINVAL; 1060 + spin_unlock_irqrestore(&dev->lock, flags); 1061 + goto out_free; 1062 + } 1063 + if (!dev->gadget) { 1064 + dev_dbg(dev->dev, "fail, gadget is not bound\n"); 1065 + ret = -EBUSY; 1066 + spin_unlock_irqrestore(&dev->lock, flags); 1067 + goto out_free; 1068 + } 1069 + 1070 + memset(info, 0, sizeof(*info)); 1071 + for (i = 0; i < dev->eps_num; i++) { 1072 + ep = &dev->eps[i]; 1073 + strscpy(&info->eps[i].name[0], ep->ep->name, 1074 + USB_RAW_EP_NAME_MAX); 1075 + info->eps[i].addr = ep->addr; 1076 + fill_ep_caps(&ep->ep->caps, &info->eps[i].caps); 1077 + fill_ep_limits(ep->ep, &info->eps[i].limits); 1078 + } 1079 + ret = dev->eps_num; 1080 + spin_unlock_irqrestore(&dev->lock, flags); 1081 + 1082 + if (copy_to_user((void __user *)value, info, sizeof(*info))) 1083 + ret = -EFAULT; 1084 + 1085 + out_free: 1086 + kfree(info); 1087 + out: 1088 + return ret; 1089 + } 1090 + 1132 1091 static long raw_ioctl(struct file *fd, unsigned int cmd, unsigned long value) 1133 1092 { 1134 1093 struct raw_dev *dev = fd->private_data; ··· 1235 1064 break; 1236 1065 case USB_RAW_IOCTL_VBUS_DRAW: 1237 1066 ret = raw_ioctl_vbus_draw(dev, value); 1067 + break; 1068 + case USB_RAW_IOCTL_EPS_INFO: 1069 + ret = raw_ioctl_eps_info(dev, value); 1070 + break; 1071 + case USB_RAW_IOCTL_EP0_STALL: 1072 + ret = raw_ioctl_ep0_stall(dev, value); 1073 + break; 1074 + case USB_RAW_IOCTL_EP_SET_HALT: 1075 + ret = raw_ioctl_ep_set_clear_halt_wedge( 1076 + dev, value, true, true); 1077 + break; 1078 + case USB_RAW_IOCTL_EP_CLEAR_HALT: 1079 + ret = raw_ioctl_ep_set_clear_halt_wedge( 1080 + dev, value, false, true); 1081 + break; 1082 + case USB_RAW_IOCTL_EP_SET_WEDGE: 1083 + ret = raw_ioctl_ep_set_clear_halt_wedge( 1084 + dev, value, true, false); 1238 1085 break; 1239 1086 default: 1240 1087 ret = -EINVAL;
+2 -2
drivers/usb/gadget/udc/atmel_usba_udc.c
··· 185 185 return 0; 186 186 } 187 187 188 - const struct file_operations queue_dbg_fops = { 188 + static const struct file_operations queue_dbg_fops = { 189 189 .owner = THIS_MODULE, 190 190 .open = queue_dbg_open, 191 191 .llseek = no_llseek, ··· 193 193 .release = queue_dbg_release, 194 194 }; 195 195 196 - const struct file_operations regs_dbg_fops = { 196 + static const struct file_operations regs_dbg_fops = { 197 197 .owner = THIS_MODULE, 198 198 .open = regs_dbg_open, 199 199 .llseek = generic_file_llseek,
+2
drivers/usb/gadget/udc/net2272.c
··· 2647 2647 err_req: 2648 2648 release_mem_region(base, len); 2649 2649 err: 2650 + kfree(dev); 2651 + 2650 2652 return ret; 2651 2653 } 2652 2654
+4 -4
drivers/usb/gadget/udc/tegra-xudc.c
··· 3840 3840 3841 3841 flush_work(&xudc->usb_role_sw_work); 3842 3842 3843 - /* Forcibly disconnect before powergating. */ 3844 - tegra_xudc_device_mode_off(xudc); 3845 - 3846 - if (!pm_runtime_status_suspended(dev)) 3843 + if (!pm_runtime_status_suspended(dev)) { 3844 + /* Forcibly disconnect before powergating. */ 3845 + tegra_xudc_device_mode_off(xudc); 3847 3846 tegra_xudc_powergate(xudc); 3847 + } 3848 3848 3849 3849 pm_runtime_disable(dev); 3850 3850
+3 -1
drivers/usb/host/xhci-plat.c
··· 362 362 struct clk *reg_clk = xhci->reg_clk; 363 363 struct usb_hcd *shared_hcd = xhci->shared_hcd; 364 364 365 + pm_runtime_get_sync(&dev->dev); 365 366 xhci->xhc_state |= XHCI_STATE_REMOVING; 366 367 367 368 usb_remove_hcd(shared_hcd); ··· 376 375 clk_disable_unprepare(reg_clk); 377 376 usb_put_hcd(hcd); 378 377 379 - pm_runtime_set_suspended(&dev->dev); 380 378 pm_runtime_disable(&dev->dev); 379 + pm_runtime_put_noidle(&dev->dev); 380 + pm_runtime_set_suspended(&dev->dev); 381 381 382 382 return 0; 383 383 }
+2 -2
drivers/usb/host/xhci-ring.c
··· 3433 3433 /* New sg entry */ 3434 3434 --num_sgs; 3435 3435 sent_len -= block_len; 3436 - if (num_sgs != 0) { 3437 - sg = sg_next(sg); 3436 + sg = sg_next(sg); 3437 + if (num_sgs != 0 && sg) { 3438 3438 block_len = sg_dma_len(sg); 3439 3439 addr = (u64) sg_dma_address(sg); 3440 3440 addr += sent_len;
+2 -2
drivers/usb/mtu3/mtu3_debugfs.c
··· 276 276 .release = single_release, 277 277 }; 278 278 279 - static struct debugfs_reg32 mtu3_prb_regs[] = { 279 + static const struct debugfs_reg32 mtu3_prb_regs[] = { 280 280 dump_prb_reg("enable", U3D_SSUSB_PRB_CTRL0), 281 281 dump_prb_reg("byte-sell", U3D_SSUSB_PRB_CTRL1), 282 282 dump_prb_reg("byte-selh", U3D_SSUSB_PRB_CTRL2), ··· 349 349 static void mtu3_debugfs_create_prb_files(struct mtu3 *mtu) 350 350 { 351 351 struct ssusb_mtk *ssusb = mtu->ssusb; 352 - struct debugfs_reg32 *regs; 352 + const struct debugfs_reg32 *regs; 353 353 struct dentry *dir_prb; 354 354 int i; 355 355
+9 -3
drivers/usb/phy/phy-twl6030-usb.c
··· 377 377 if (status < 0) { 378 378 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", 379 379 twl->irq1, status); 380 - return status; 380 + goto err_put_regulator; 381 381 } 382 382 383 383 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq, ··· 386 386 if (status < 0) { 387 387 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", 388 388 twl->irq2, status); 389 - free_irq(twl->irq1, twl); 390 - return status; 389 + goto err_free_irq1; 391 390 } 392 391 393 392 twl->asleep = 0; ··· 395 396 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n"); 396 397 397 398 return 0; 399 + 400 + err_free_irq1: 401 + free_irq(twl->irq1, twl); 402 + err_put_regulator: 403 + regulator_put(twl->usb3v3); 404 + 405 + return status; 398 406 } 399 407 400 408 static int twl6030_usb_remove(struct platform_device *pdev)
+3 -3
drivers/usb/typec/mux/intel_pmc_mux.c
··· 63 63 #define PMC_USB_ALTMODE_DP_MODE_SHIFT 8 64 64 65 65 /* TBT specific Mode Data bits */ 66 + #define PMC_USB_ALTMODE_HPD_HIGH BIT(14) 66 67 #define PMC_USB_ALTMODE_TBT_TYPE BIT(17) 67 68 #define PMC_USB_ALTMODE_CABLE_TYPE BIT(18) 68 69 #define PMC_USB_ALTMODE_ACTIVE_LINK BIT(20) ··· 75 74 #define PMC_USB_ALTMODE_TBT_GEN(_g_) (((_g_) & GENMASK(1, 0)) << 28) 76 75 77 76 /* Display HPD Request bits */ 77 + #define PMC_USB_DP_HPD_LVL BIT(4) 78 78 #define PMC_USB_DP_HPD_IRQ BIT(5) 79 - #define PMC_USB_DP_HPD_LVL BIT(6) 80 79 81 80 struct pmc_usb; 82 81 ··· 159 158 PMC_USB_ALTMODE_DP_MODE_SHIFT; 160 159 161 160 if (data->status & DP_STATUS_HPD_STATE) 162 - req.mode_data |= PMC_USB_DP_HPD_LVL << 163 - PMC_USB_ALTMODE_DP_MODE_SHIFT; 161 + req.mode_data |= PMC_USB_ALTMODE_HPD_HIGH; 164 162 165 163 return pmc_usb_command(port, (void *)&req, sizeof(req)); 166 164 }
+95 -13
include/uapi/linux/usb/raw_gadget.h
··· 93 93 __u8 data[0]; 94 94 }; 95 95 96 + /* Maximum number of non-control endpoints in struct usb_raw_eps_info. */ 97 + #define USB_RAW_EPS_NUM_MAX 30 98 + 99 + /* Maximum length of UDC endpoint name in struct usb_raw_ep_info. */ 100 + #define USB_RAW_EP_NAME_MAX 16 101 + 102 + /* Used as addr in struct usb_raw_ep_info if endpoint accepts any address. */ 103 + #define USB_RAW_EP_ADDR_ANY 0xff 104 + 105 + /* 106 + * struct usb_raw_ep_caps - exposes endpoint capabilities from struct usb_ep 107 + * (technically from its member struct usb_ep_caps). 108 + */ 109 + struct usb_raw_ep_caps { 110 + __u32 type_control : 1; 111 + __u32 type_iso : 1; 112 + __u32 type_bulk : 1; 113 + __u32 type_int : 1; 114 + __u32 dir_in : 1; 115 + __u32 dir_out : 1; 116 + }; 117 + 118 + /* 119 + * struct usb_raw_ep_limits - exposes endpoint limits from struct usb_ep. 120 + * @maxpacket_limit: Maximum packet size value supported by this endpoint. 121 + * @max_streams: maximum number of streams supported by this endpoint 122 + * (actual number is 2^n). 123 + * @reserved: Empty, reserved for potential future extensions. 124 + */ 125 + struct usb_raw_ep_limits { 126 + __u16 maxpacket_limit; 127 + __u16 max_streams; 128 + __u32 reserved; 129 + }; 130 + 131 + /* 132 + * struct usb_raw_ep_info - stores information about a gadget endpoint. 133 + * @name: Name of the endpoint as it is defined in the UDC driver. 134 + * @addr: Address of the endpoint that must be specified in the endpoint 135 + * descriptor passed to USB_RAW_IOCTL_EP_ENABLE ioctl. 136 + * @caps: Endpoint capabilities. 137 + * @limits: Endpoint limits. 138 + */ 139 + struct usb_raw_ep_info { 140 + __u8 name[USB_RAW_EP_NAME_MAX]; 141 + __u32 addr; 142 + struct usb_raw_ep_caps caps; 143 + struct usb_raw_ep_limits limits; 144 + }; 145 + 146 + /* 147 + * struct usb_raw_eps_info - argument for USB_RAW_IOCTL_EPS_INFO ioctl. 148 + * eps: Structures that store information about non-control endpoints. 149 + */ 150 + struct usb_raw_eps_info { 151 + struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX]; 152 + }; 153 + 96 154 /* 97 155 * Initializes a Raw Gadget instance. 98 156 * Accepts a pointer to the usb_raw_init struct as an argument. ··· 173 115 #define USB_RAW_IOCTL_EVENT_FETCH _IOR('U', 2, struct usb_raw_event) 174 116 175 117 /* 176 - * Queues an IN (OUT for READ) urb as a response to the last control request 177 - * received on endpoint 0, provided that was an IN (OUT for READ) request and 178 - * waits until the urb is completed. Copies received data to user for READ. 118 + * Queues an IN (OUT for READ) request as a response to the last setup request 119 + * received on endpoint 0 (provided that was an IN (OUT for READ) request), and 120 + * waits until the request is completed. Copies received data to user for READ. 179 121 * Accepts a pointer to the usb_raw_ep_io struct as an argument. 180 - * Returns length of trasferred data on success or negative error code on 122 + * Returns length of transferred data on success or negative error code on 181 123 * failure. 182 124 */ 183 125 #define USB_RAW_IOCTL_EP0_WRITE _IOW('U', 3, struct usb_raw_ep_io) 184 126 #define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io) 185 127 186 128 /* 187 - * Finds an endpoint that supports the transfer type specified in the 188 - * descriptor and enables it. 189 - * Accepts a pointer to the usb_endpoint_descriptor struct as an argument. 129 + * Finds an endpoint that satisfies the parameters specified in the provided 130 + * descriptors (address, transfer type, etc.) and enables it. 131 + * Accepts a pointer to the usb_raw_ep_descs struct as an argument. 190 132 * Returns enabled endpoint handle on success or negative error code on failure. 191 133 */ 192 134 #define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor) 193 135 194 - /* Disables specified endpoint. 136 + /* 137 + * Disables specified endpoint. 195 138 * Accepts endpoint handle as an argument. 196 139 * Returns 0 on success or negative error code on failure. 197 140 */ 198 141 #define USB_RAW_IOCTL_EP_DISABLE _IOW('U', 6, __u32) 199 142 200 143 /* 201 - * Queues an IN (OUT for READ) urb as a response to the last control request 202 - * received on endpoint usb_raw_ep_io.ep, provided that was an IN (OUT for READ) 203 - * request and waits until the urb is completed. Copies received data to user 204 - * for READ. 144 + * Queues an IN (OUT for READ) request as a response to the last setup request 145 + * received on endpoint usb_raw_ep_io.ep (provided that was an IN (OUT for READ) 146 + * request), and waits until the request is completed. Copies received data to 147 + * user for READ. 205 148 * Accepts a pointer to the usb_raw_ep_io struct as an argument. 206 - * Returns length of trasferred data on success or negative error code on 149 + * Returns length of transferred data on success or negative error code on 207 150 * failure. 208 151 */ 209 152 #define USB_RAW_IOCTL_EP_WRITE _IOW('U', 7, struct usb_raw_ep_io) ··· 222 163 * Returns 0 on success or negative error code on failure. 223 164 */ 224 165 #define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32) 166 + 167 + /* 168 + * Fills in the usb_raw_eps_info structure with information about non-control 169 + * endpoints available for the currently connected UDC. 170 + * Returns the number of available endpoints on success or negative error code 171 + * on failure. 172 + */ 173 + #define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info) 174 + 175 + /* 176 + * Stalls a pending control request on endpoint 0. 177 + * Returns 0 on success or negative error code on failure. 178 + */ 179 + #define USB_RAW_IOCTL_EP0_STALL _IO('U', 12) 180 + 181 + /* 182 + * Sets or clears halt or wedge status of the endpoint. 183 + * Accepts endpoint handle as an argument. 184 + * Returns 0 on success or negative error code on failure. 185 + */ 186 + #define USB_RAW_IOCTL_EP_SET_HALT _IOW('U', 13, __u32) 187 + #define USB_RAW_IOCTL_EP_CLEAR_HALT _IOW('U', 14, __u32) 188 + #define USB_RAW_IOCTL_EP_SET_WEDGE _IOW('U', 15, __u32) 225 189 226 190 #endif /* _UAPI__LINUX_USB_RAW_GADGET_H */