Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 2 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link |
| 4 | * |
Alexander A. Klimov | 10623b8 | 2020-07-11 15:58:04 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 6 | * |
| 7 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 8 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/pm_runtime.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/dma-mapping.h> |
| 21 | |
| 22 | #include <linux/usb/ch9.h> |
| 23 | #include <linux/usb/gadget.h> |
| 24 | |
Felipe Balbi | 80977dc | 2014-08-19 16:37:22 -0500 | [diff] [blame] | 25 | #include "debug.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 26 | #include "core.h" |
| 27 | #include "gadget.h" |
| 28 | #include "io.h" |
| 29 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 30 | #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \ |
Felipe Balbi | f62afb4 | 2018-04-11 10:34:34 +0300 | [diff] [blame] | 31 | & ~((d)->interval - 1)) |
| 32 | |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 33 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 34 | * dwc3_gadget_set_test_mode - enables usb2 test modes |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 35 | * @dwc: pointer to our context structure |
| 36 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) |
| 37 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 38 | * Caller should take care of locking. This function will return 0 on |
| 39 | * success or -EINVAL if wrong Test Selector is passed. |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 40 | */ |
| 41 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) |
| 42 | { |
| 43 | u32 reg; |
| 44 | |
| 45 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 46 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 47 | |
| 48 | switch (mode) { |
Greg Kroah-Hartman | 62fb45d | 2020-06-18 16:42:06 +0200 | [diff] [blame] | 49 | case USB_TEST_J: |
| 50 | case USB_TEST_K: |
| 51 | case USB_TEST_SE0_NAK: |
| 52 | case USB_TEST_PACKET: |
| 53 | case USB_TEST_FORCE_ENABLE: |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 54 | reg |= mode << 1; |
| 55 | break; |
| 56 | default: |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 60 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 65 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 66 | * dwc3_gadget_get_link_state - gets current state of usb link |
Paul Zimmerman | 911f1f8 | 2012-04-27 13:35:15 +0300 | [diff] [blame] | 67 | * @dwc: pointer to our context structure |
| 68 | * |
| 69 | * Caller should take care of locking. This function will |
| 70 | * return the link state on success (>= 0) or -ETIMEDOUT. |
| 71 | */ |
| 72 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) |
| 73 | { |
| 74 | u32 reg; |
| 75 | |
| 76 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 77 | |
| 78 | return DWC3_DSTS_USBLNKST(reg); |
| 79 | } |
| 80 | |
| 81 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 82 | * dwc3_gadget_set_link_state - sets usb link to a particular state |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 83 | * @dwc: pointer to our context structure |
| 84 | * @state: the state to put link into |
| 85 | * |
| 86 | * Caller should take care of locking. This function will |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 87 | * return 0 on success or -ETIMEDOUT. |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 88 | */ |
| 89 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) |
| 90 | { |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 91 | int retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 92 | u32 reg; |
| 93 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 94 | /* |
| 95 | * Wait until device controller is ready. Only applies to 1.94a and |
| 96 | * later RTL. |
| 97 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 98 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 99 | while (--retries) { |
| 100 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 101 | if (reg & DWC3_DSTS_DCNRD) |
| 102 | udelay(5); |
| 103 | else |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | if (retries <= 0) |
| 108 | return -ETIMEDOUT; |
| 109 | } |
| 110 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 111 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 112 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 113 | |
Thinh Nguyen | 2e708fa | 2019-10-23 19:15:55 -0700 | [diff] [blame] | 114 | /* set no action before sending new link state change */ |
| 115 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 116 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 117 | /* set requested state */ |
| 118 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); |
| 119 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 120 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 121 | /* |
| 122 | * The following code is racy when called from dwc3_gadget_wakeup, |
| 123 | * and is not needed, at least on newer versions |
| 124 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 125 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 126 | return 0; |
| 127 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 128 | /* wait for a change in DSTS */ |
Paul Zimmerman | aed430e | 2012-04-27 12:52:01 +0300 | [diff] [blame] | 129 | retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 130 | while (--retries) { |
| 131 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 132 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 133 | if (DWC3_DSTS_USBLNKST(reg) == state) |
| 134 | return 0; |
| 135 | |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 136 | udelay(5); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 139 | return -ETIMEDOUT; |
| 140 | } |
| 141 | |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 142 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 143 | * dwc3_ep_inc_trb - increment a trb index. |
| 144 | * @index: Pointer to the TRB index to increment. |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 145 | * |
| 146 | * The index should never point to the link TRB. After incrementing, |
| 147 | * if it is point to the link TRB, wrap around to the beginning. The |
| 148 | * link TRB is always at the last TRB entry. |
| 149 | */ |
| 150 | static void dwc3_ep_inc_trb(u8 *index) |
| 151 | { |
| 152 | (*index)++; |
| 153 | if (*index == (DWC3_TRB_NUM - 1)) |
| 154 | *index = 0; |
| 155 | } |
| 156 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 157 | /** |
| 158 | * dwc3_ep_inc_enq - increment endpoint's enqueue pointer |
| 159 | * @dep: The endpoint whose enqueue pointer we're incrementing |
| 160 | */ |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 161 | static void dwc3_ep_inc_enq(struct dwc3_ep *dep) |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 162 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 163 | dwc3_ep_inc_trb(&dep->trb_enqueue); |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 164 | } |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 165 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 166 | /** |
| 167 | * dwc3_ep_inc_deq - increment endpoint's dequeue pointer |
| 168 | * @dep: The endpoint whose enqueue pointer we're incrementing |
| 169 | */ |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 170 | static void dwc3_ep_inc_deq(struct dwc3_ep *dep) |
| 171 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 172 | dwc3_ep_inc_trb(&dep->trb_dequeue); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Wei Yongjun | 6910251 | 2018-03-29 02:20:10 +0000 | [diff] [blame] | 175 | static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 176 | struct dwc3_request *req, int status) |
| 177 | { |
| 178 | struct dwc3 *dwc = dep->dwc; |
| 179 | |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 180 | list_del(&req->list); |
| 181 | req->remaining = 0; |
Jack Pham | bd674224 | 2019-01-10 12:39:55 -0800 | [diff] [blame] | 182 | req->needs_extra_trb = false; |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 183 | |
| 184 | if (req->request.status == -EINPROGRESS) |
| 185 | req->request.status = status; |
| 186 | |
| 187 | if (req->trb) |
| 188 | usb_gadget_unmap_request_by_dev(dwc->sysdev, |
| 189 | &req->request, req->direction); |
| 190 | |
| 191 | req->trb = NULL; |
| 192 | trace_dwc3_gadget_giveback(req); |
| 193 | |
| 194 | if (dep->number > 1) |
| 195 | pm_runtime_put(dwc->dev); |
| 196 | } |
| 197 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 198 | /** |
| 199 | * dwc3_gadget_giveback - call struct usb_request's ->complete callback |
| 200 | * @dep: The endpoint to whom the request belongs to |
| 201 | * @req: The request we're giving back |
| 202 | * @status: completion code for the request |
| 203 | * |
| 204 | * Must be called with controller's lock held and interrupts disabled. This |
| 205 | * function will unmap @req and call its ->complete() callback to notify upper |
| 206 | * layers that it has completed. |
| 207 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 208 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 209 | int status) |
| 210 | { |
| 211 | struct dwc3 *dwc = dep->dwc; |
| 212 | |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 213 | dwc3_gadget_del_and_unmap_request(dep, req, status); |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 214 | req->status = DWC3_REQUEST_STATUS_COMPLETED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 215 | |
| 216 | spin_unlock(&dwc->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 217 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 218 | spin_lock(&dwc->lock); |
| 219 | } |
| 220 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 221 | /** |
| 222 | * dwc3_send_gadget_generic_command - issue a generic command for the controller |
| 223 | * @dwc: pointer to the controller context |
| 224 | * @cmd: the command to be issued |
| 225 | * @param: command parameter |
| 226 | * |
| 227 | * Caller should take care of locking. Issue @cmd with a given @param to @dwc |
| 228 | * and wait for its completion. |
| 229 | */ |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 230 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd, |
| 231 | u32 param) |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 232 | { |
| 233 | u32 timeout = 500; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 234 | int status = 0; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 235 | int ret = 0; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 236 | u32 reg; |
| 237 | |
| 238 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 239 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 240 | |
| 241 | do { |
| 242 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 243 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 244 | status = DWC3_DGCMD_STATUS(reg); |
| 245 | if (status) |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 246 | ret = -EINVAL; |
| 247 | break; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 248 | } |
Janusz Dziedzic | e3aee48 | 2016-11-09 11:01:33 +0100 | [diff] [blame] | 249 | } while (--timeout); |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 250 | |
| 251 | if (!timeout) { |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 252 | ret = -ETIMEDOUT; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 253 | status = -ETIMEDOUT; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 254 | } |
| 255 | |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 256 | trace_dwc3_gadget_generic_cmd(cmd, param, status); |
| 257 | |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 258 | return ret; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 259 | } |
| 260 | |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 261 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc); |
| 262 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 263 | /** |
| 264 | * dwc3_send_gadget_ep_cmd - issue an endpoint command |
| 265 | * @dep: the endpoint to which the command is going to be issued |
| 266 | * @cmd: the command to be issued |
| 267 | * @params: parameters to the command |
| 268 | * |
| 269 | * Caller should handle locking. This function will issue @cmd with given |
| 270 | * @params to @dep and wait for its completion. |
| 271 | */ |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 272 | int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd, |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 273 | struct dwc3_gadget_ep_cmd_params *params) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 274 | { |
Felipe Balbi | 8897a76 | 2016-09-22 10:56:08 +0300 | [diff] [blame] | 275 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 276 | struct dwc3 *dwc = dep->dwc; |
Yu Chen | 1c0e69a | 2020-05-21 16:46:43 +0800 | [diff] [blame] | 277 | u32 timeout = 5000; |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 278 | u32 saved_config = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 279 | u32 reg; |
| 280 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 281 | int cmd_status = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 282 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 283 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 284 | /* |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 285 | * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or |
| 286 | * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an |
| 287 | * endpoint command. |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 288 | * |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 289 | * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY |
| 290 | * settings. Restore them after the command is completed. |
| 291 | * |
| 292 | * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 293 | */ |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 294 | if (dwc->gadget->speed <= USB_SPEED_HIGH) { |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 295 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 296 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 297 | saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 298 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 299 | } |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 300 | |
| 301 | if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { |
| 302 | saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 303 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 304 | } |
| 305 | |
| 306 | if (saved_config) |
| 307 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 308 | } |
| 309 | |
Felipe Balbi | 5999914 | 2016-09-22 12:25:28 +0300 | [diff] [blame] | 310 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
Thinh Nguyen | c560e76 | 2021-04-19 19:11:12 -0700 | [diff] [blame] | 311 | int link_state; |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 312 | |
Thinh Nguyen | c560e76 | 2021-04-19 19:11:12 -0700 | [diff] [blame] | 313 | link_state = dwc3_gadget_get_link_state(dwc); |
| 314 | if (link_state == DWC3_LINK_STATE_U1 || |
| 315 | link_state == DWC3_LINK_STATE_U2 || |
| 316 | link_state == DWC3_LINK_STATE_U3) { |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 317 | ret = __dwc3_gadget_wakeup(dwc); |
| 318 | dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", |
| 319 | ret); |
| 320 | } |
| 321 | } |
| 322 | |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 323 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); |
| 324 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); |
| 325 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 326 | |
Felipe Balbi | 8897a76 | 2016-09-22 10:56:08 +0300 | [diff] [blame] | 327 | /* |
| 328 | * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're |
| 329 | * not relying on XferNotReady, we can make use of a special "No |
| 330 | * Response Update Transfer" command where we should clear both CmdAct |
| 331 | * and CmdIOC bits. |
| 332 | * |
| 333 | * With this, we don't need to wait for command completion and can |
| 334 | * straight away issue further commands to the endpoint. |
| 335 | * |
| 336 | * NOTICE: We're making an assumption that control endpoints will never |
| 337 | * make use of Update Transfer command. This is a safe assumption |
| 338 | * because we can never have more than one request at a time with |
| 339 | * Control Endpoints. If anybody changes that assumption, this chunk |
| 340 | * needs to be updated accordingly. |
| 341 | */ |
| 342 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && |
| 343 | !usb_endpoint_xfer_isoc(desc)) |
| 344 | cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); |
| 345 | else |
| 346 | cmd |= DWC3_DEPCMD_CMDACT; |
| 347 | |
| 348 | dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 349 | do { |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 350 | reg = dwc3_readl(dep->regs, DWC3_DEPCMD); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 351 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 352 | cmd_status = DWC3_DEPCMD_STATUS(reg); |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 353 | |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 354 | switch (cmd_status) { |
| 355 | case 0: |
| 356 | ret = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 357 | break; |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 358 | case DEPEVT_TRANSFER_NO_RESOURCE: |
Thinh Nguyen | f7ac582e | 2020-03-29 16:13:16 -0700 | [diff] [blame] | 359 | dev_WARN(dwc->dev, "No resource for %s\n", |
| 360 | dep->name); |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 361 | ret = -EINVAL; |
| 362 | break; |
| 363 | case DEPEVT_TRANSFER_BUS_EXPIRY: |
| 364 | /* |
| 365 | * SW issues START TRANSFER command to |
| 366 | * isochronous ep with future frame interval. If |
| 367 | * future interval time has already passed when |
| 368 | * core receives the command, it will respond |
| 369 | * with an error status of 'Bus Expiry'. |
| 370 | * |
| 371 | * Instead of always returning -EINVAL, let's |
| 372 | * give a hint to the gadget driver that this is |
| 373 | * the case by returning -EAGAIN. |
| 374 | */ |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 375 | ret = -EAGAIN; |
| 376 | break; |
| 377 | default: |
| 378 | dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); |
| 379 | } |
| 380 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 381 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 382 | } |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 383 | } while (--timeout); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 384 | |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 385 | if (timeout == 0) { |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 386 | ret = -ETIMEDOUT; |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 387 | cmd_status = -ETIMEDOUT; |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 388 | } |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 389 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 390 | trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); |
| 391 | |
Thinh Nguyen | 9bc3395 | 2020-03-29 16:13:04 -0700 | [diff] [blame] | 392 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
| 393 | if (ret == 0) |
| 394 | dep->flags |= DWC3_EP_TRANSFER_STARTED; |
| 395 | |
| 396 | if (ret != -ETIMEDOUT) |
| 397 | dwc3_gadget_ep_get_transfer_index(dep); |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 398 | } |
| 399 | |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 400 | if (saved_config) { |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 401 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 402 | reg |= saved_config; |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 403 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 404 | } |
| 405 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 406 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 407 | } |
| 408 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 409 | static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) |
| 410 | { |
| 411 | struct dwc3 *dwc = dep->dwc; |
| 412 | struct dwc3_gadget_ep_cmd_params params; |
| 413 | u32 cmd = DWC3_DEPCMD_CLEARSTALL; |
| 414 | |
| 415 | /* |
| 416 | * As of core revision 2.60a the recommended programming model |
| 417 | * is to set the ClearPendIN bit when issuing a Clear Stall EP |
| 418 | * command for IN endpoints. This is to prevent an issue where |
| 419 | * some (non-compliant) hosts may not send ACK TPs for pending |
| 420 | * IN transfers due to a mishandled error condition. Synopsys |
| 421 | * STAR 9000614252. |
| 422 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 423 | if (dep->direction && |
| 424 | !DWC3_VER_IS_PRIOR(DWC3, 260A) && |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 425 | (dwc->gadget->speed >= USB_SPEED_SUPER)) |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 426 | cmd |= DWC3_DEPCMD_CLEARPENDIN; |
| 427 | |
| 428 | memset(¶ms, 0, sizeof(params)); |
| 429 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 430 | return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 433 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 434 | struct dwc3_trb *trb) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 435 | { |
Paul Zimmerman | c439ef8 | 2011-09-30 10:58:45 +0300 | [diff] [blame] | 436 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 437 | |
| 438 | return dep->trb_pool_dma + offset; |
| 439 | } |
| 440 | |
| 441 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 442 | { |
| 443 | struct dwc3 *dwc = dep->dwc; |
| 444 | |
| 445 | if (dep->trb_pool) |
| 446 | return 0; |
| 447 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 448 | dep->trb_pool = dma_alloc_coherent(dwc->sysdev, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 449 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 450 | &dep->trb_pool_dma, GFP_KERNEL); |
| 451 | if (!dep->trb_pool) { |
| 452 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 453 | dep->name); |
| 454 | return -ENOMEM; |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 461 | { |
| 462 | struct dwc3 *dwc = dep->dwc; |
| 463 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 464 | dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 465 | dep->trb_pool, dep->trb_pool_dma); |
| 466 | |
| 467 | dep->trb_pool = NULL; |
| 468 | dep->trb_pool_dma = 0; |
| 469 | } |
| 470 | |
Felipe Balbi | 20d1d43 | 2018-04-09 12:49:02 +0300 | [diff] [blame] | 471 | static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) |
| 472 | { |
| 473 | struct dwc3_gadget_ep_cmd_params params; |
| 474 | |
| 475 | memset(¶ms, 0x00, sizeof(params)); |
| 476 | |
| 477 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
| 478 | |
| 479 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, |
| 480 | ¶ms); |
| 481 | } |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 482 | |
| 483 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 484 | * dwc3_gadget_start_config - configure ep resources |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 485 | * @dep: endpoint that is being enabled |
| 486 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 487 | * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's |
| 488 | * completion, it will set Transfer Resource for all available endpoints. |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 489 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 490 | * The assignment of transfer resources cannot perfectly follow the data book |
| 491 | * due to the fact that the controller driver does not have all knowledge of the |
| 492 | * configuration in advance. It is given this information piecemeal by the |
| 493 | * composite gadget framework after every SET_CONFIGURATION and |
| 494 | * SET_INTERFACE. Trying to follow the databook programming model in this |
| 495 | * scenario can cause errors. For two reasons: |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 496 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 497 | * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every |
| 498 | * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is |
| 499 | * incorrect in the scenario of multiple interfaces. |
| 500 | * |
| 501 | * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 502 | * endpoint on alt setting (8.1.6). |
| 503 | * |
| 504 | * The following simplified method is used instead: |
| 505 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 506 | * All hardware endpoints can be assigned a transfer resource and this setting |
| 507 | * will stay persistent until either a core reset or hibernation. So whenever we |
| 508 | * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do |
| 509 | * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 510 | * guaranteed that there are as many transfer resources as endpoints. |
| 511 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 512 | * This function is called for each endpoint when it is being enabled but is |
| 513 | * triggered only when called for EP0-out, which always happens first, and which |
| 514 | * should only happen in one of the above conditions. |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 515 | */ |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 516 | static int dwc3_gadget_start_config(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 517 | { |
| 518 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 519 | struct dwc3 *dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 520 | u32 cmd; |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 521 | int i; |
| 522 | int ret; |
| 523 | |
| 524 | if (dep->number) |
| 525 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 526 | |
| 527 | memset(¶ms, 0x00, sizeof(params)); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 528 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 529 | dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 530 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 531 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 532 | if (ret) |
| 533 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 534 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 535 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
| 536 | struct dwc3_ep *dep = dwc->eps[i]; |
| 537 | |
| 538 | if (!dep) |
| 539 | continue; |
| 540 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 541 | ret = dwc3_gadget_set_xfer_resource(dep); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 542 | if (ret) |
| 543 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 549 | static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 550 | { |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 551 | const struct usb_ss_ep_comp_descriptor *comp_desc; |
| 552 | const struct usb_endpoint_descriptor *desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 553 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 554 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 555 | |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 556 | comp_desc = dep->endpoint.comp_desc; |
| 557 | desc = dep->endpoint.desc; |
| 558 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 559 | memset(¶ms, 0x00, sizeof(params)); |
| 560 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 561 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 562 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 563 | |
| 564 | /* Burst size is only needed in SuperSpeed mode */ |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 565 | if (dwc->gadget->speed >= USB_SPEED_SUPER) { |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 566 | u32 burst = dep->endpoint.maxburst; |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 567 | |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 568 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 569 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 570 | |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 571 | params.param0 |= action; |
| 572 | if (action == DWC3_DEPCFG_ACTION_RESTORE) |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 573 | params.param2 |= dep->saved_state; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 574 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 575 | if (usb_endpoint_xfer_control(desc)) |
| 576 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; |
Felipe Balbi | 13fa2e6 | 2016-05-30 13:40:00 +0300 | [diff] [blame] | 577 | |
| 578 | if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) |
| 579 | params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 580 | |
Felipe Balbi | 18b7ede | 2012-01-02 13:35:41 +0200 | [diff] [blame] | 581 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 582 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
Thinh Nguyen | 548f8b3 | 2020-05-05 19:46:45 -0700 | [diff] [blame] | 583 | | DWC3_DEPCFG_XFER_COMPLETE_EN |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 584 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 585 | dep->stream_capable = true; |
| 586 | } |
| 587 | |
Felipe Balbi | 0b93a4c | 2014-09-04 10:28:10 -0500 | [diff] [blame] | 588 | if (!usb_endpoint_xfer_control(desc)) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 589 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * We are doing 1:1 mapping for endpoints, meaning |
| 593 | * Physical Endpoints 2 maps to Logical Endpoint 2 and |
| 594 | * so on. We consider the direction bit as part of the physical |
| 595 | * endpoint number. So USB endpoint 0x81 is 0x03. |
| 596 | */ |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 597 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 598 | |
| 599 | /* |
| 600 | * We must use the lower 16 TX FIFOs even though |
| 601 | * HW might have more |
| 602 | */ |
| 603 | if (dep->direction) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 604 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 605 | |
| 606 | if (desc->bInterval) { |
Thinh Nguyen | a1679af | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 607 | u8 bInterval_m1; |
| 608 | |
| 609 | /* |
Thinh Nguyen | 3232a3c | 2021-04-15 00:41:58 -0700 | [diff] [blame] | 610 | * Valid range for DEPCFG.bInterval_m1 is from 0 to 13. |
| 611 | * |
| 612 | * NOTE: The programming guide incorrectly stated bInterval_m1 |
| 613 | * must be set to 0 when operating in fullspeed. Internally the |
| 614 | * controller does not have this limitation. See DWC_usb3x |
| 615 | * programming guide section 3.2.2.1. |
Thinh Nguyen | a1679af | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 616 | */ |
| 617 | bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); |
Thinh Nguyen | a1679af | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 618 | |
Thinh Nguyen | 4b049f5 | 2021-02-08 13:53:16 -0800 | [diff] [blame] | 619 | if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT && |
| 620 | dwc->gadget->speed == USB_SPEED_FULL) |
| 621 | dep->interval = desc->bInterval; |
| 622 | else |
| 623 | dep->interval = 1 << (desc->bInterval - 1); |
| 624 | |
Thinh Nguyen | a1679af | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 625 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 626 | } |
| 627 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 628 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 629 | } |
| 630 | |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 631 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, |
| 632 | bool interrupt); |
| 633 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 634 | /** |
Wesley Cheng | 9f607a3 | 2021-07-10 02:13:12 -0700 | [diff] [blame^] | 635 | * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value |
| 636 | * @dwc: pointer to the DWC3 context |
| 637 | * @nfifos: number of fifos to calculate for |
| 638 | * |
| 639 | * Calculates the size value based on the equation below: |
| 640 | * |
| 641 | * DWC3 revision 280A and prior: |
| 642 | * fifo_size = mult * (max_packet / mdwidth) + 1; |
| 643 | * |
| 644 | * DWC3 revision 290A and onwards: |
| 645 | * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 |
| 646 | * |
| 647 | * The max packet size is set to 1024, as the txfifo requirements mainly apply |
| 648 | * to super speed USB use cases. However, it is safe to overestimate the fifo |
| 649 | * allocations for other scenarios, i.e. high speed USB. |
| 650 | */ |
| 651 | static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult) |
| 652 | { |
| 653 | int max_packet = 1024; |
| 654 | int fifo_size; |
| 655 | int mdwidth; |
| 656 | |
| 657 | mdwidth = dwc3_mdwidth(dwc); |
| 658 | |
| 659 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 660 | mdwidth >>= 3; |
| 661 | |
| 662 | if (DWC3_VER_IS_PRIOR(DWC3, 290A)) |
| 663 | fifo_size = mult * (max_packet / mdwidth) + 1; |
| 664 | else |
| 665 | fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1; |
| 666 | return fifo_size; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * dwc3_gadget_clear_tx_fifo_size - Clears txfifo allocation |
| 671 | * @dwc: pointer to the DWC3 context |
| 672 | * |
| 673 | * Iterates through all the endpoint registers and clears the previous txfifo |
| 674 | * allocations. |
| 675 | */ |
| 676 | void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc) |
| 677 | { |
| 678 | struct dwc3_ep *dep; |
| 679 | int fifo_depth; |
| 680 | int size; |
| 681 | int num; |
| 682 | |
| 683 | if (!dwc->do_fifo_resize) |
| 684 | return; |
| 685 | |
| 686 | /* Read ep0IN related TXFIFO size */ |
| 687 | dep = dwc->eps[1]; |
| 688 | size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); |
| 689 | if (DWC3_IP_IS(DWC3)) |
| 690 | fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size); |
| 691 | else |
| 692 | fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size); |
| 693 | |
| 694 | dwc->last_fifo_depth = fifo_depth; |
| 695 | /* Clear existing TXFIFO for all IN eps except ep0 */ |
| 696 | for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM); |
| 697 | num += 2) { |
| 698 | dep = dwc->eps[num]; |
| 699 | /* Don't change TXFRAMNUM on usb31 version */ |
| 700 | size = DWC3_IP_IS(DWC3) ? 0 : |
| 701 | dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) & |
| 702 | DWC31_GTXFIFOSIZ_TXFRAMNUM; |
| 703 | |
| 704 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size); |
| 705 | } |
| 706 | dwc->num_ep_resized = 0; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case |
| 711 | * @dwc: pointer to our context structure |
| 712 | * |
| 713 | * This function will a best effort FIFO allocation in order |
| 714 | * to improve FIFO usage and throughput, while still allowing |
| 715 | * us to enable as many endpoints as possible. |
| 716 | * |
| 717 | * Keep in mind that this operation will be highly dependent |
| 718 | * on the configured size for RAM1 - which contains TxFifo -, |
| 719 | * the amount of endpoints enabled on coreConsultant tool, and |
| 720 | * the width of the Master Bus. |
| 721 | * |
| 722 | * In general, FIFO depths are represented with the following equation: |
| 723 | * |
| 724 | * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 |
| 725 | * |
| 726 | * In conjunction with dwc3_gadget_check_config(), this resizing logic will |
| 727 | * ensure that all endpoints will have enough internal memory for one max |
| 728 | * packet per endpoint. |
| 729 | */ |
| 730 | static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep) |
| 731 | { |
| 732 | struct dwc3 *dwc = dep->dwc; |
| 733 | int fifo_0_start; |
| 734 | int ram1_depth; |
| 735 | int fifo_size; |
| 736 | int min_depth; |
| 737 | int num_in_ep; |
| 738 | int remaining; |
| 739 | int num_fifos = 1; |
| 740 | int fifo; |
| 741 | int tmp; |
| 742 | |
| 743 | if (!dwc->do_fifo_resize) |
| 744 | return 0; |
| 745 | |
| 746 | /* resize IN endpoints except ep0 */ |
| 747 | if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1) |
| 748 | return 0; |
| 749 | |
| 750 | ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); |
| 751 | |
| 752 | if ((dep->endpoint.maxburst > 1 && |
| 753 | usb_endpoint_xfer_bulk(dep->endpoint.desc)) || |
| 754 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 755 | num_fifos = 3; |
| 756 | |
| 757 | if (dep->endpoint.maxburst > 6 && |
| 758 | usb_endpoint_xfer_bulk(dep->endpoint.desc) && DWC3_IP_IS(DWC31)) |
| 759 | num_fifos = dwc->tx_fifo_resize_max_num; |
| 760 | |
| 761 | /* FIFO size for a single buffer */ |
| 762 | fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1); |
| 763 | |
| 764 | /* Calculate the number of remaining EPs w/o any FIFO */ |
| 765 | num_in_ep = dwc->max_cfg_eps; |
| 766 | num_in_ep -= dwc->num_ep_resized; |
| 767 | |
| 768 | /* Reserve at least one FIFO for the number of IN EPs */ |
| 769 | min_depth = num_in_ep * (fifo + 1); |
| 770 | remaining = ram1_depth - min_depth - dwc->last_fifo_depth; |
| 771 | remaining = max_t(int, 0, remaining); |
| 772 | /* |
| 773 | * We've already reserved 1 FIFO per EP, so check what we can fit in |
| 774 | * addition to it. If there is not enough remaining space, allocate |
| 775 | * all the remaining space to the EP. |
| 776 | */ |
| 777 | fifo_size = (num_fifos - 1) * fifo; |
| 778 | if (remaining < fifo_size) |
| 779 | fifo_size = remaining; |
| 780 | |
| 781 | fifo_size += fifo; |
| 782 | /* Last increment according to the TX FIFO size equation */ |
| 783 | fifo_size++; |
| 784 | |
| 785 | /* Check if TXFIFOs start at non-zero addr */ |
| 786 | tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); |
| 787 | fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp); |
| 788 | |
| 789 | fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16)); |
| 790 | if (DWC3_IP_IS(DWC3)) |
| 791 | dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); |
| 792 | else |
| 793 | dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); |
| 794 | |
| 795 | /* Check fifo size allocation doesn't exceed available RAM size. */ |
| 796 | if (dwc->last_fifo_depth >= ram1_depth) { |
| 797 | dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n", |
| 798 | dwc->last_fifo_depth, ram1_depth, |
| 799 | dep->endpoint.name, fifo_size); |
| 800 | if (DWC3_IP_IS(DWC3)) |
| 801 | fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); |
| 802 | else |
| 803 | fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); |
| 804 | |
| 805 | dwc->last_fifo_depth -= fifo_size; |
| 806 | return -ENOMEM; |
| 807 | } |
| 808 | |
| 809 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size); |
| 810 | dwc->num_ep_resized++; |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 816 | * __dwc3_gadget_ep_enable - initializes a hw endpoint |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 817 | * @dep: endpoint to be initialized |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 818 | * @action: one of INIT, MODIFY or RESTORE |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 819 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 820 | * Caller should take care of locking. Execute all necessary commands to |
| 821 | * initialize a HW endpoint so it can be used by a gadget driver. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 822 | */ |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 823 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 824 | { |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 825 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 826 | struct dwc3 *dwc = dep->dwc; |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 827 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 828 | u32 reg; |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 829 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 830 | |
| 831 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Wesley Cheng | 9f607a3 | 2021-07-10 02:13:12 -0700 | [diff] [blame^] | 832 | ret = dwc3_gadget_resize_tx_fifos(dep); |
| 833 | if (ret) |
| 834 | return ret; |
| 835 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 836 | ret = dwc3_gadget_start_config(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 837 | if (ret) |
| 838 | return ret; |
| 839 | } |
| 840 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 841 | ret = dwc3_gadget_set_ep_config(dep, action); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 842 | if (ret) |
| 843 | return ret; |
| 844 | |
| 845 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 846 | struct dwc3_trb *trb_st_hw; |
| 847 | struct dwc3_trb *trb_link; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 848 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 849 | dep->type = usb_endpoint_type(desc); |
| 850 | dep->flags |= DWC3_EP_ENABLED; |
| 851 | |
| 852 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 853 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 854 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 855 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 856 | if (usb_endpoint_xfer_control(desc)) |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 857 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 858 | |
John Youn | 0d25744 | 2016-05-19 17:26:08 -0700 | [diff] [blame] | 859 | /* Initialize the TRB ring */ |
| 860 | dep->trb_dequeue = 0; |
| 861 | dep->trb_enqueue = 0; |
| 862 | memset(dep->trb_pool, 0, |
| 863 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM); |
| 864 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 865 | /* Link TRB. The HWO bit is never reset */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 866 | trb_st_hw = &dep->trb_pool[0]; |
| 867 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 868 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 869 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 870 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 871 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 872 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 873 | } |
| 874 | |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 875 | /* |
| 876 | * Issue StartTransfer here with no-op TRB so we can always rely on No |
| 877 | * Response Update Transfer command. |
| 878 | */ |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 879 | if (usb_endpoint_xfer_bulk(desc) || |
Felipe Balbi | 52fcc0b | 2018-03-26 13:19:43 +0300 | [diff] [blame] | 880 | usb_endpoint_xfer_int(desc)) { |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 881 | struct dwc3_gadget_ep_cmd_params params; |
| 882 | struct dwc3_trb *trb; |
| 883 | dma_addr_t trb_dma; |
| 884 | u32 cmd; |
| 885 | |
| 886 | memset(¶ms, 0, sizeof(params)); |
| 887 | trb = &dep->trb_pool[0]; |
| 888 | trb_dma = dwc3_trb_dma_offset(dep, trb); |
| 889 | |
| 890 | params.param0 = upper_32_bits(trb_dma); |
| 891 | params.param1 = lower_32_bits(trb_dma); |
| 892 | |
| 893 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 894 | |
| 895 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 896 | if (ret < 0) |
| 897 | return ret; |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 898 | |
| 899 | if (dep->stream_capable) { |
| 900 | /* |
| 901 | * For streams, at start, there maybe a race where the |
| 902 | * host primes the endpoint before the function driver |
| 903 | * queues a request to initiate a stream. In that case, |
| 904 | * the controller will not see the prime to generate the |
| 905 | * ERDY and start stream. To workaround this, issue a |
| 906 | * no-op TRB as normal, but end it immediately. As a |
| 907 | * result, when the function driver queues the request, |
| 908 | * the next START_TRANSFER command will cause the |
| 909 | * controller to generate an ERDY to initiate the |
| 910 | * stream. |
| 911 | */ |
| 912 | dwc3_stop_active_transfer(dep, true, true); |
| 913 | |
| 914 | /* |
| 915 | * All stream eps will reinitiate stream on NoStream |
| 916 | * rejection until we can determine that the host can |
| 917 | * prime after the first transfer. |
Thinh Nguyen | ddae797 | 2021-04-22 16:51:43 -0700 | [diff] [blame] | 918 | * |
| 919 | * However, if the controller is capable of |
| 920 | * TXF_FLUSH_BYPASS, then IN direction endpoints will |
| 921 | * automatically restart the stream without the driver |
| 922 | * initiation. |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 923 | */ |
Thinh Nguyen | ddae797 | 2021-04-22 16:51:43 -0700 | [diff] [blame] | 924 | if (!dep->direction || |
| 925 | !(dwc->hwparams.hwparams9 & |
| 926 | DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS)) |
| 927 | dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 928 | } |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 929 | } |
| 930 | |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 931 | out: |
| 932 | trace_dwc3_gadget_ep_enable(dep); |
| 933 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 937 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 938 | { |
| 939 | struct dwc3_request *req; |
| 940 | |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 941 | dwc3_stop_active_transfer(dep, true, false); |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 942 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 943 | /* - giveback all requests to gadget driver */ |
| 944 | while (!list_empty(&dep->started_list)) { |
| 945 | req = next_request(&dep->started_list); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 946 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 947 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | ea53b88 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 950 | while (!list_empty(&dep->pending_list)) { |
| 951 | req = next_request(&dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 952 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 953 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 954 | } |
Felipe Balbi | d8eca64 | 2019-10-31 11:07:13 +0200 | [diff] [blame] | 955 | |
| 956 | while (!list_empty(&dep->cancelled_list)) { |
| 957 | req = next_request(&dep->cancelled_list); |
| 958 | |
| 959 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 960 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 964 | * __dwc3_gadget_ep_disable - disables a hw endpoint |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 965 | * @dep: the endpoint to disable |
| 966 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 967 | * This function undoes what __dwc3_gadget_ep_enable did and also removes |
| 968 | * requests which are currently being processed by the hardware and those which |
| 969 | * are not yet scheduled. |
| 970 | * |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 971 | * Caller should take care of locking. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 972 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 973 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 974 | { |
| 975 | struct dwc3 *dwc = dep->dwc; |
| 976 | u32 reg; |
| 977 | |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 978 | trace_dwc3_gadget_ep_disable(dep); |
Felipe Balbi | 7eaeac5 | 2015-07-20 14:46:15 -0500 | [diff] [blame] | 979 | |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 980 | /* make sure HW endpoint isn't stalled */ |
| 981 | if (dep->flags & DWC3_EP_STALL) |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 982 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 983 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 984 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 985 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 986 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 987 | |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 988 | /* Clear out the ep descriptors for non-ep0 */ |
| 989 | if (dep->number > 1) { |
| 990 | dep->endpoint.comp_desc = NULL; |
| 991 | dep->endpoint.desc = NULL; |
| 992 | } |
| 993 | |
Wesley Cheng | f09ddcf | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 994 | dwc3_remove_requests(dwc, dep); |
| 995 | |
Wesley Cheng | 5aef6297 | 2021-03-24 11:31:04 -0700 | [diff] [blame] | 996 | dep->stream_capable = false; |
| 997 | dep->type = 0; |
| 998 | dep->flags = 0; |
| 999 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | /* -------------------------------------------------------------------------- */ |
| 1004 | |
| 1005 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 1006 | const struct usb_endpoint_descriptor *desc) |
| 1007 | { |
| 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | |
| 1011 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 1012 | { |
| 1013 | return -EINVAL; |
| 1014 | } |
| 1015 | |
| 1016 | /* -------------------------------------------------------------------------- */ |
| 1017 | |
| 1018 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 1019 | const struct usb_endpoint_descriptor *desc) |
| 1020 | { |
| 1021 | struct dwc3_ep *dep; |
| 1022 | struct dwc3 *dwc; |
| 1023 | unsigned long flags; |
| 1024 | int ret; |
| 1025 | |
| 1026 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 1027 | pr_debug("dwc3: invalid parameters\n"); |
| 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | |
| 1031 | if (!desc->wMaxPacketSize) { |
| 1032 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 1033 | return -EINVAL; |
| 1034 | } |
| 1035 | |
| 1036 | dep = to_dwc3_ep(ep); |
| 1037 | dwc = dep->dwc; |
| 1038 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 1039 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
| 1040 | "%s is already enabled\n", |
| 1041 | dep->name)) |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 1042 | return 0; |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 1043 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1044 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 1045 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1046 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1047 | |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
| 1051 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 1052 | { |
| 1053 | struct dwc3_ep *dep; |
| 1054 | struct dwc3 *dwc; |
| 1055 | unsigned long flags; |
| 1056 | int ret; |
| 1057 | |
| 1058 | if (!ep) { |
| 1059 | pr_debug("dwc3: invalid parameters\n"); |
| 1060 | return -EINVAL; |
| 1061 | } |
| 1062 | |
| 1063 | dep = to_dwc3_ep(ep); |
| 1064 | dwc = dep->dwc; |
| 1065 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 1066 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
| 1067 | "%s is already disabled\n", |
| 1068 | dep->name)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1069 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1070 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1071 | spin_lock_irqsave(&dwc->lock, flags); |
| 1072 | ret = __dwc3_gadget_ep_disable(dep); |
| 1073 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1074 | |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, |
Felipe Balbi | 0bd0f6d | 2018-03-26 16:09:00 +0300 | [diff] [blame] | 1079 | gfp_t gfp_flags) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1080 | { |
| 1081 | struct dwc3_request *req; |
| 1082 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1083 | |
| 1084 | req = kzalloc(sizeof(*req), gfp_flags); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 1085 | if (!req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1086 | return NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1087 | |
Felipe Balbi | 31a2f5a | 2018-05-07 15:19:31 +0300 | [diff] [blame] | 1088 | req->direction = dep->direction; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1089 | req->epnum = dep->number; |
| 1090 | req->dep = dep; |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 1091 | req->status = DWC3_REQUEST_STATUS_UNKNOWN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1092 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1093 | trace_dwc3_alloc_request(req); |
| 1094 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1095 | return &req->request; |
| 1096 | } |
| 1097 | |
| 1098 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 1099 | struct usb_request *request) |
| 1100 | { |
| 1101 | struct dwc3_request *req = to_dwc3_request(request); |
| 1102 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1103 | trace_dwc3_free_request(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1104 | kfree(req); |
| 1105 | } |
| 1106 | |
Felipe Balbi | 4262691 | 2018-04-09 13:01:43 +0300 | [diff] [blame] | 1107 | /** |
| 1108 | * dwc3_ep_prev_trb - returns the previous TRB in the ring |
| 1109 | * @dep: The endpoint with the TRB ring |
| 1110 | * @index: The index of the current TRB in the ring |
| 1111 | * |
| 1112 | * Returns the TRB prior to the one pointed to by the index. If the |
| 1113 | * index is 0, we will wrap backwards, skip the link TRB, and return |
| 1114 | * the one just before that. |
| 1115 | */ |
| 1116 | static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) |
| 1117 | { |
| 1118 | u8 tmp = index; |
| 1119 | |
| 1120 | if (!tmp) |
| 1121 | tmp = DWC3_TRB_NUM - 1; |
| 1122 | |
| 1123 | return &dep->trb_pool[tmp - 1]; |
| 1124 | } |
| 1125 | |
| 1126 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) |
| 1127 | { |
| 1128 | struct dwc3_trb *tmp; |
| 1129 | u8 trbs_left; |
| 1130 | |
| 1131 | /* |
| 1132 | * If enqueue & dequeue are equal than it is either full or empty. |
| 1133 | * |
| 1134 | * One way to know for sure is if the TRB right before us has HWO bit |
| 1135 | * set or not. If it has, then we're definitely full and can't fit any |
| 1136 | * more transfers in our ring. |
| 1137 | */ |
| 1138 | if (dep->trb_enqueue == dep->trb_dequeue) { |
| 1139 | tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 1140 | if (tmp->ctrl & DWC3_TRB_CTRL_HWO) |
| 1141 | return 0; |
| 1142 | |
| 1143 | return DWC3_TRB_NUM - 1; |
| 1144 | } |
| 1145 | |
| 1146 | trbs_left = dep->trb_dequeue - dep->trb_enqueue; |
| 1147 | trbs_left &= (DWC3_TRB_NUM - 1); |
| 1148 | |
| 1149 | if (dep->trb_dequeue < dep->trb_enqueue) |
| 1150 | trbs_left--; |
| 1151 | |
| 1152 | return trbs_left; |
| 1153 | } |
Felipe Balbi | 2c78c02 | 2016-08-12 13:13:10 +0300 | [diff] [blame] | 1154 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1155 | static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 1156 | dma_addr_t dma, unsigned int length, unsigned int chain, |
| 1157 | unsigned int node, unsigned int stream_id, |
| 1158 | unsigned int short_not_ok, unsigned int no_interrupt, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1159 | unsigned int is_last, bool must_interrupt) |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1160 | { |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1161 | struct dwc3 *dwc = dep->dwc; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 1162 | struct usb_gadget *gadget = dwc->gadget; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1163 | enum usb_device_speed speed = gadget->speed; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1164 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1165 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 1166 | trb->bpl = lower_32_bits(dma); |
| 1167 | trb->bph = upper_32_bits(dma); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1168 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 1169 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1170 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1171 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1172 | break; |
| 1173 | |
| 1174 | case USB_ENDPOINT_XFER_ISOC: |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1175 | if (!node) { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1176 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1177 | |
Manu Gautam | 40d829f | 2017-07-19 17:07:10 +0530 | [diff] [blame] | 1178 | /* |
| 1179 | * USB Specification 2.0 Section 5.9.2 states that: "If |
| 1180 | * there is only a single transaction in the microframe, |
| 1181 | * only a DATA0 data packet PID is used. If there are |
| 1182 | * two transactions per microframe, DATA1 is used for |
| 1183 | * the first transaction data packet and DATA0 is used |
| 1184 | * for the second transaction data packet. If there are |
| 1185 | * three transactions per microframe, DATA2 is used for |
| 1186 | * the first transaction data packet, DATA1 is used for |
| 1187 | * the second, and DATA0 is used for the third." |
| 1188 | * |
| 1189 | * IOW, we should satisfy the following cases: |
| 1190 | * |
| 1191 | * 1) length <= maxpacket |
| 1192 | * - DATA0 |
| 1193 | * |
| 1194 | * 2) maxpacket < length <= (2 * maxpacket) |
| 1195 | * - DATA1, DATA0 |
| 1196 | * |
| 1197 | * 3) (2 * maxpacket) < length <= (3 * maxpacket) |
| 1198 | * - DATA2, DATA1, DATA0 |
| 1199 | */ |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1200 | if (speed == USB_SPEED_HIGH) { |
| 1201 | struct usb_ep *ep = &dep->endpoint; |
Manu Gautam | ec5bb87 | 2017-12-06 12:49:04 +0530 | [diff] [blame] | 1202 | unsigned int mult = 2; |
Manu Gautam | 40d829f | 2017-07-19 17:07:10 +0530 | [diff] [blame] | 1203 | unsigned int maxp = usb_endpoint_maxp(ep->desc); |
| 1204 | |
| 1205 | if (length <= (2 * maxp)) |
| 1206 | mult--; |
| 1207 | |
| 1208 | if (length <= maxp) |
| 1209 | mult--; |
| 1210 | |
| 1211 | trb->size |= DWC3_TRB_SIZE_PCM1(mult); |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1212 | } |
| 1213 | } else { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1214 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1215 | } |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 1216 | |
| 1217 | /* always enable Interrupt on Missed ISOC */ |
| 1218 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1219 | break; |
| 1220 | |
| 1221 | case USB_ENDPOINT_XFER_BULK: |
| 1222 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1223 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1224 | break; |
| 1225 | default: |
| 1226 | /* |
| 1227 | * This is only possible with faulty memory because we |
| 1228 | * checked it already :) |
| 1229 | */ |
Felipe Balbi | 0a695d4 | 2016-10-07 11:20:01 +0300 | [diff] [blame] | 1230 | dev_WARN(dwc->dev, "Unknown endpoint type %d\n", |
| 1231 | usb_endpoint_type(dep->endpoint.desc)); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Tejas Joglekar | 244add8 | 2018-12-10 16:08:13 +0530 | [diff] [blame] | 1234 | /* |
| 1235 | * Enable Continue on Short Packet |
| 1236 | * when endpoint is not a stream capable |
| 1237 | */ |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1238 | if (usb_endpoint_dir_out(dep->endpoint.desc)) { |
Tejas Joglekar | 244add8 | 2018-12-10 16:08:13 +0530 | [diff] [blame] | 1239 | if (!dep->stream_capable) |
| 1240 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
Felipe Balbi | f3af365 | 2013-12-13 14:19:33 -0600 | [diff] [blame] | 1241 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1242 | if (short_not_ok) |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1243 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
| 1244 | } |
| 1245 | |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1246 | if ((!no_interrupt && !chain) || must_interrupt) |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1247 | trb->ctrl |= DWC3_TRB_CTRL_IOC; |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 1248 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1249 | if (chain) |
| 1250 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
Thinh Nguyen | 3eaecd0 | 2020-05-05 19:46:51 -0700 | [diff] [blame] | 1251 | else if (dep->stream_capable && is_last) |
| 1252 | trb->ctrl |= DWC3_TRB_CTRL_LST; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1253 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 1254 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1255 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1256 | |
| 1257 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1258 | |
Anurag Kumar Vulisha | b7a4fbe | 2018-12-01 16:43:29 +0530 | [diff] [blame] | 1259 | dwc3_ep_inc_enq(dep); |
| 1260 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1261 | trace_dwc3_prepare_trb(dep, trb); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 1264 | /** |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1265 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 1266 | * @dep: endpoint for which this request is prepared |
| 1267 | * @req: dwc3_request pointer |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1268 | * @trb_length: buffer size of the TRB |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1269 | * @chain: should this TRB be chained to the next? |
| 1270 | * @node: only for isochronous endpoints. First TRB needs different type. |
Thinh Nguyen | 2b80357 | 2020-09-24 01:21:30 -0700 | [diff] [blame] | 1271 | * @use_bounce_buffer: set to use bounce buffer |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1272 | * @must_interrupt: set to interrupt on TRB completion |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1273 | */ |
| 1274 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1275 | struct dwc3_request *req, unsigned int trb_length, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1276 | unsigned int chain, unsigned int node, bool use_bounce_buffer, |
| 1277 | bool must_interrupt) |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1278 | { |
| 1279 | struct dwc3_trb *trb; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1280 | dma_addr_t dma; |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 1281 | unsigned int stream_id = req->request.stream_id; |
| 1282 | unsigned int short_not_ok = req->request.short_not_ok; |
| 1283 | unsigned int no_interrupt = req->request.no_interrupt; |
| 1284 | unsigned int is_last = req->request.is_last; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1285 | |
Thinh Nguyen | 2b80357 | 2020-09-24 01:21:30 -0700 | [diff] [blame] | 1286 | if (use_bounce_buffer) |
| 1287 | dma = dep->dwc->bounce_addr; |
| 1288 | else if (req->request.num_sgs > 0) |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1289 | dma = sg_dma_address(req->start_sg); |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1290 | else |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1291 | dma = req->request.dma; |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1292 | |
| 1293 | trb = &dep->trb_pool[dep->trb_enqueue]; |
| 1294 | |
| 1295 | if (!req->trb) { |
| 1296 | dwc3_gadget_move_started_request(req); |
| 1297 | req->trb = trb; |
| 1298 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 1301 | req->num_trbs++; |
| 1302 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1303 | __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1304 | stream_id, short_not_ok, no_interrupt, is_last, |
| 1305 | must_interrupt); |
| 1306 | } |
| 1307 | |
| 1308 | static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1309 | { |
| 1310 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); |
| 1311 | unsigned int rem = req->request.length % maxp; |
| 1312 | |
| 1313 | if ((req->request.length && req->request.zero && !rem && |
| 1314 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) || |
| 1315 | (!req->direction && rem)) |
| 1316 | return true; |
| 1317 | |
| 1318 | return false; |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1321 | /** |
| 1322 | * dwc3_prepare_last_sg - prepare TRBs for the last SG entry |
| 1323 | * @dep: The endpoint that the request belongs to |
| 1324 | * @req: The request to prepare |
| 1325 | * @entry_length: The last SG entry size |
| 1326 | * @node: Indicates whether this is not the first entry (for isoc only) |
| 1327 | * |
| 1328 | * Return the number of TRBs prepared. |
| 1329 | */ |
| 1330 | static int dwc3_prepare_last_sg(struct dwc3_ep *dep, |
| 1331 | struct dwc3_request *req, unsigned int entry_length, |
| 1332 | unsigned int node) |
| 1333 | { |
| 1334 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); |
| 1335 | unsigned int rem = req->request.length % maxp; |
| 1336 | unsigned int num_trbs = 1; |
| 1337 | |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1338 | if (dwc3_needs_extra_trb(dep, req)) |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1339 | num_trbs++; |
| 1340 | |
| 1341 | if (dwc3_calc_trbs_left(dep) < num_trbs) |
| 1342 | return 0; |
| 1343 | |
| 1344 | req->needs_extra_trb = num_trbs > 1; |
| 1345 | |
| 1346 | /* Prepare a normal TRB */ |
| 1347 | if (req->direction || req->request.length) |
| 1348 | dwc3_prepare_one_trb(dep, req, entry_length, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1349 | req->needs_extra_trb, node, false, false); |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1350 | |
| 1351 | /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */ |
| 1352 | if ((!req->direction && !req->request.length) || req->needs_extra_trb) |
| 1353 | dwc3_prepare_one_trb(dep, req, |
| 1354 | req->direction ? 0 : maxp - rem, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1355 | false, 1, true, false); |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1356 | |
| 1357 | return num_trbs; |
| 1358 | } |
| 1359 | |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1360 | static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1361 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1362 | { |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1363 | struct scatterlist *sg = req->start_sg; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1364 | struct scatterlist *s; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1365 | int i; |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1366 | unsigned int length = req->request.length; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1367 | unsigned int remaining = req->request.num_mapped_sgs |
| 1368 | - req->num_queued_sgs; |
Thinh Nguyen | 13111fc | 2020-09-24 01:21:49 -0700 | [diff] [blame] | 1369 | unsigned int num_trbs = req->num_trbs; |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1370 | bool needs_extra_trb = dwc3_needs_extra_trb(dep, req); |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1371 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1372 | /* |
| 1373 | * If we resume preparing the request, then get the remaining length of |
| 1374 | * the request and resume where we left off. |
| 1375 | */ |
| 1376 | for_each_sg(req->request.sg, s, req->num_queued_sgs, i) |
| 1377 | length -= sg_dma_len(s); |
| 1378 | |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1379 | for_each_sg(sg, s, remaining, i) { |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1380 | unsigned int num_trbs_left = dwc3_calc_trbs_left(dep); |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1381 | unsigned int trb_length; |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1382 | bool must_interrupt = false; |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1383 | bool last_sg = false; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1384 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1385 | trb_length = min_t(unsigned int, length, sg_dma_len(s)); |
| 1386 | |
| 1387 | length -= trb_length; |
| 1388 | |
Pratham Pratap | dad2aff | 2020-03-02 21:44:43 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * IOMMU driver is coalescing the list of sgs which shares a |
| 1391 | * page boundary into one and giving it to USB driver. With |
| 1392 | * this the number of sgs mapped is not equal to the number of |
| 1393 | * sgs passed. So mark the chain bit to false if it isthe last |
| 1394 | * mapped sg. |
| 1395 | */ |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1396 | if ((i == remaining - 1) || !length) |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1397 | last_sg = true; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1398 | |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1399 | if (!num_trbs_left) |
Thinh Nguyen | 13111fc | 2020-09-24 01:21:49 -0700 | [diff] [blame] | 1400 | break; |
| 1401 | |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1402 | if (last_sg) { |
| 1403 | if (!dwc3_prepare_last_sg(dep, req, trb_length, i)) |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1404 | break; |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1405 | } else { |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * Look ahead to check if we have enough TRBs for the |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1408 | * next SG entry. If not, set interrupt on this TRB to |
| 1409 | * resume preparing the next SG entry when more TRBs are |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1410 | * free. |
| 1411 | */ |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1412 | if (num_trbs_left == 1 || (needs_extra_trb && |
| 1413 | num_trbs_left <= 2 && |
| 1414 | sg_dma_len(sg_next(s)) >= length)) |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1415 | must_interrupt = true; |
| 1416 | |
| 1417 | dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false, |
| 1418 | must_interrupt); |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1419 | } |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1420 | |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1421 | /* |
| 1422 | * There can be a situation where all sgs in sglist are not |
| 1423 | * queued because of insufficient trb number. To handle this |
| 1424 | * case, update start_sg to next sg to be queued, so that |
| 1425 | * we have free trbs we can continue queuing from where we |
| 1426 | * previously stopped |
| 1427 | */ |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1428 | if (!last_sg) |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1429 | req->start_sg = sg_next(s); |
| 1430 | |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1431 | req->num_queued_sgs++; |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 1432 | req->num_pending_sgs--; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1433 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1434 | /* |
| 1435 | * The number of pending SG entries may not correspond to the |
| 1436 | * number of mapped SG entries. If all the data are queued, then |
| 1437 | * don't include unused SG entries. |
| 1438 | */ |
| 1439 | if (length == 0) { |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 1440 | req->num_pending_sgs = 0; |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1444 | if (must_interrupt) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1445 | break; |
| 1446 | } |
Thinh Nguyen | 13111fc | 2020-09-24 01:21:49 -0700 | [diff] [blame] | 1447 | |
Thinh Nguyen | 30892cb | 2020-09-24 01:22:01 -0700 | [diff] [blame] | 1448 | return req->num_trbs - num_trbs; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1449 | } |
| 1450 | |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1451 | static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1452 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1453 | { |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1454 | return dwc3_prepare_last_sg(dep, req, req->request.length, 0); |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1455 | } |
| 1456 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1457 | /* |
| 1458 | * dwc3_prepare_trbs - setup TRBs from requests |
| 1459 | * @dep: endpoint for which requests are being prepared |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1460 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1461 | * The function goes through the requests list and sets up TRBs for the |
| 1462 | * transfers. The function returns once there are no more TRBs available or |
| 1463 | * it runs out of requests. |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1464 | * |
| 1465 | * Returns the number of TRBs prepared or negative errno. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1466 | */ |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1467 | static int dwc3_prepare_trbs(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1468 | { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 1469 | struct dwc3_request *req, *n; |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1470 | int ret = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1471 | |
| 1472 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 1473 | |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1474 | /* |
| 1475 | * We can get in a situation where there's a request in the started list |
| 1476 | * but there weren't enough TRBs to fully kick it in the first time |
| 1477 | * around, so it has been waiting for more TRBs to be freed up. |
| 1478 | * |
| 1479 | * In that case, we should check if we have a request with pending_sgs |
| 1480 | * in the started list and prepare TRBs for that request first, |
| 1481 | * otherwise we will prepare TRBs completely out of order and that will |
| 1482 | * break things. |
| 1483 | */ |
| 1484 | list_for_each_entry(req, &dep->started_list, list) { |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1485 | if (req->num_pending_sgs > 0) { |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1486 | ret = dwc3_prepare_trbs_sg(dep, req); |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1487 | if (!ret || req->num_pending_sgs) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1488 | return ret; |
| 1489 | } |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1490 | |
| 1491 | if (!dwc3_calc_trbs_left(dep)) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1492 | return ret; |
Thinh Nguyen | 63c7bb2 | 2020-05-15 16:40:46 -0700 | [diff] [blame] | 1493 | |
| 1494 | /* |
| 1495 | * Don't prepare beyond a transfer. In DWC_usb32, its transfer |
| 1496 | * burst capability may try to read and use TRBs beyond the |
| 1497 | * active transfer instead of stopping. |
| 1498 | */ |
| 1499 | if (dep->stream_capable && req->request.is_last) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1500 | return ret; |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1501 | } |
| 1502 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1503 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1504 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1505 | |
| 1506 | ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, |
| 1507 | dep->direction); |
| 1508 | if (ret) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1509 | return ret; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1510 | |
| 1511 | req->sg = req->request.sg; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1512 | req->start_sg = req->sg; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1513 | req->num_queued_sgs = 0; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1514 | req->num_pending_sgs = req->request.num_mapped_sgs; |
| 1515 | |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1516 | if (req->num_pending_sgs > 0) { |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1517 | ret = dwc3_prepare_trbs_sg(dep, req); |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1518 | if (req->num_pending_sgs) |
| 1519 | return ret; |
| 1520 | } else { |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1521 | ret = dwc3_prepare_trbs_linear(dep, req); |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1522 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1523 | |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1524 | if (!ret || !dwc3_calc_trbs_left(dep)) |
| 1525 | return ret; |
Thinh Nguyen | aefe3d2 | 2020-05-05 19:47:03 -0700 | [diff] [blame] | 1526 | |
| 1527 | /* |
| 1528 | * Don't prepare beyond a transfer. In DWC_usb32, its transfer |
| 1529 | * burst capability may try to read and use TRBs beyond the |
| 1530 | * active transfer instead of stopping. |
| 1531 | */ |
| 1532 | if (dep->stream_capable && req->request.is_last) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1533 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1534 | } |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1535 | |
| 1536 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1537 | } |
| 1538 | |
Thinh Nguyen | 8d99087 | 2020-03-29 16:12:57 -0700 | [diff] [blame] | 1539 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep); |
| 1540 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1541 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1542 | { |
| 1543 | struct dwc3_gadget_ep_cmd_params params; |
| 1544 | struct dwc3_request *req; |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1545 | int starting; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1546 | int ret; |
| 1547 | u32 cmd; |
| 1548 | |
Thinh Nguyen | d72ecc0 | 2020-09-29 00:18:48 -0700 | [diff] [blame] | 1549 | /* |
| 1550 | * Note that it's normal to have no new TRBs prepared (i.e. ret == 0). |
| 1551 | * This happens when we need to stop and restart a transfer such as in |
| 1552 | * the case of reinitiating a stream or retrying an isoc transfer. |
| 1553 | */ |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1554 | ret = dwc3_prepare_trbs(dep); |
Thinh Nguyen | d72ecc0 | 2020-09-29 00:18:48 -0700 | [diff] [blame] | 1555 | if (ret < 0) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1556 | return ret; |
Felipe Balbi | ccb94eb | 2017-09-05 14:28:46 +0300 | [diff] [blame] | 1557 | |
Felipe Balbi | 1912cbc | 2018-03-29 11:08:46 +0300 | [diff] [blame] | 1558 | starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1559 | |
Thinh Nguyen | 2338484 | 2020-09-30 17:44:38 -0700 | [diff] [blame] | 1560 | /* |
| 1561 | * If there's no new TRB prepared and we don't need to restart a |
| 1562 | * transfer, there's no need to update the transfer. |
| 1563 | */ |
| 1564 | if (!ret && !starting) |
| 1565 | return ret; |
| 1566 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1567 | req = next_request(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1568 | if (!req) { |
| 1569 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 1570 | return 0; |
| 1571 | } |
| 1572 | |
| 1573 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1574 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1575 | if (starting) { |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1576 | params.param0 = upper_32_bits(req->trb_dma); |
| 1577 | params.param1 = lower_32_bits(req->trb_dma); |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1578 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 1579 | |
Anurag Kumar Vulisha | a735180 | 2018-12-01 16:43:25 +0530 | [diff] [blame] | 1580 | if (dep->stream_capable) |
| 1581 | cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); |
| 1582 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1583 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 1584 | cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1585 | } else { |
Felipe Balbi | b6b1c6d | 2016-05-30 13:29:35 +0300 | [diff] [blame] | 1586 | cmd = DWC3_DEPCMD_UPDATETRANSFER | |
| 1587 | DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1588 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1589 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1590 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1591 | if (ret < 0) { |
Thinh Nguyen | 8d99087 | 2020-03-29 16:12:57 -0700 | [diff] [blame] | 1592 | struct dwc3_request *tmp; |
| 1593 | |
| 1594 | if (ret == -EAGAIN) |
| 1595 | return ret; |
| 1596 | |
| 1597 | dwc3_stop_active_transfer(dep, true, true); |
| 1598 | |
| 1599 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) |
Ray Chi | 04dd6e7 | 2021-03-28 02:17:42 +0800 | [diff] [blame] | 1600 | dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED); |
Thinh Nguyen | 8d99087 | 2020-03-29 16:12:57 -0700 | [diff] [blame] | 1601 | |
| 1602 | /* If ep isn't started, then there's no end transfer pending */ |
| 1603 | if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) |
| 1604 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
| 1605 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1606 | return ret; |
| 1607 | } |
| 1608 | |
Thinh Nguyen | e0d1956 | 2020-05-05 19:46:57 -0700 | [diff] [blame] | 1609 | if (dep->stream_capable && req->request.is_last) |
| 1610 | dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; |
| 1611 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1612 | return 0; |
| 1613 | } |
| 1614 | |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 1615 | static int __dwc3_gadget_get_frame(struct dwc3 *dwc) |
| 1616 | { |
| 1617 | u32 reg; |
| 1618 | |
| 1619 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1620 | return DWC3_DSTS_SOFFN(reg); |
| 1621 | } |
| 1622 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1623 | /** |
| 1624 | * dwc3_gadget_start_isoc_quirk - workaround invalid frame number |
| 1625 | * @dep: isoc endpoint |
| 1626 | * |
| 1627 | * This function tests for the correct combination of BIT[15:14] from the 16-bit |
| 1628 | * microframe number reported by the XferNotReady event for the future frame |
| 1629 | * number to start the isoc transfer. |
| 1630 | * |
| 1631 | * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed |
| 1632 | * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the |
| 1633 | * XferNotReady event are invalid. The driver uses this number to schedule the |
| 1634 | * isochronous transfer and passes it to the START TRANSFER command. Because |
| 1635 | * this number is invalid, the command may fail. If BIT[15:14] matches the |
| 1636 | * internal 16-bit microframe, the START TRANSFER command will pass and the |
| 1637 | * transfer will start at the scheduled time, if it is off by 1, the command |
| 1638 | * will still pass, but the transfer will start 2 seconds in the future. For all |
| 1639 | * other conditions, the START TRANSFER command will fail with bus-expiry. |
| 1640 | * |
| 1641 | * In order to workaround this issue, we can test for the correct combination of |
| 1642 | * BIT[15:14] by sending START TRANSFER commands with different values of |
| 1643 | * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart |
| 1644 | * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. |
| 1645 | * As the result, within the 4 possible combinations for BIT[15:14], there will |
| 1646 | * be 2 successful and 2 failure START COMMAND status. One of the 2 successful |
| 1647 | * command status will result in a 2-second delay start. The smaller BIT[15:14] |
| 1648 | * value is the correct combination. |
| 1649 | * |
| 1650 | * Since there are only 4 outcomes and the results are ordered, we can simply |
| 1651 | * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to |
| 1652 | * deduce the smaller successful combination. |
| 1653 | * |
| 1654 | * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 |
| 1655 | * of BIT[15:14]. The correct combination is as follow: |
| 1656 | * |
| 1657 | * if test0 fails and test1 passes, BIT[15:14] is 'b01 |
| 1658 | * if test0 fails and test1 fails, BIT[15:14] is 'b10 |
| 1659 | * if test0 passes and test1 fails, BIT[15:14] is 'b11 |
| 1660 | * if test0 passes and test1 passes, BIT[15:14] is 'b00 |
| 1661 | * |
| 1662 | * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN |
| 1663 | * endpoints. |
| 1664 | */ |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1665 | static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1666 | { |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1667 | int cmd_status = 0; |
| 1668 | bool test0; |
| 1669 | bool test1; |
| 1670 | |
| 1671 | while (dep->combo_num < 2) { |
| 1672 | struct dwc3_gadget_ep_cmd_params params; |
| 1673 | u32 test_frame_number; |
| 1674 | u32 cmd; |
| 1675 | |
| 1676 | /* |
| 1677 | * Check if we can start isoc transfer on the next interval or |
| 1678 | * 4 uframes in the future with BIT[15:14] as dep->combo_num |
| 1679 | */ |
Michael Grzeschik | ca14378 | 2020-07-01 20:24:51 +0200 | [diff] [blame] | 1680 | test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1681 | test_frame_number |= dep->combo_num << 14; |
| 1682 | test_frame_number += max_t(u32, 4, dep->interval); |
| 1683 | |
| 1684 | params.param0 = upper_32_bits(dep->dwc->bounce_addr); |
| 1685 | params.param1 = lower_32_bits(dep->dwc->bounce_addr); |
| 1686 | |
| 1687 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 1688 | cmd |= DWC3_DEPCMD_PARAM(test_frame_number); |
| 1689 | cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 1690 | |
| 1691 | /* Redo if some other failure beside bus-expiry is received */ |
| 1692 | if (cmd_status && cmd_status != -EAGAIN) { |
| 1693 | dep->start_cmd_status = 0; |
| 1694 | dep->combo_num = 0; |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1695 | return 0; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | /* Store the first test status */ |
| 1699 | if (dep->combo_num == 0) |
| 1700 | dep->start_cmd_status = cmd_status; |
| 1701 | |
| 1702 | dep->combo_num++; |
| 1703 | |
| 1704 | /* |
| 1705 | * End the transfer if the START_TRANSFER command is successful |
| 1706 | * to wait for the next XferNotReady to test the command again |
| 1707 | */ |
| 1708 | if (cmd_status == 0) { |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 1709 | dwc3_stop_active_transfer(dep, true, true); |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1710 | return 0; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1711 | } |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1712 | } |
| 1713 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1714 | /* test0 and test1 are both completed at this point */ |
| 1715 | test0 = (dep->start_cmd_status == 0); |
| 1716 | test1 = (cmd_status == 0); |
| 1717 | |
| 1718 | if (!test0 && test1) |
| 1719 | dep->combo_num = 1; |
| 1720 | else if (!test0 && !test1) |
| 1721 | dep->combo_num = 2; |
| 1722 | else if (test0 && !test1) |
| 1723 | dep->combo_num = 3; |
| 1724 | else if (test0 && test1) |
| 1725 | dep->combo_num = 0; |
| 1726 | |
Michael Grzeschik | ca14378 | 2020-07-01 20:24:51 +0200 | [diff] [blame] | 1727 | dep->frame_number &= DWC3_FRNUMBER_MASK; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1728 | dep->frame_number |= dep->combo_num << 14; |
| 1729 | dep->frame_number += max_t(u32, 4, dep->interval); |
| 1730 | |
| 1731 | /* Reinitialize test variables */ |
| 1732 | dep->start_cmd_status = 0; |
| 1733 | dep->combo_num = 0; |
| 1734 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1735 | return __dwc3_gadget_kick_transfer(dep); |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1738 | static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1739 | { |
Michael Olbrich | c5a7092 | 2020-07-01 20:24:52 +0200 | [diff] [blame] | 1740 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1741 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1742 | int ret; |
| 1743 | int i; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1744 | |
Thinh Nguyen | 36f05d3 | 2020-03-29 16:13:10 -0700 | [diff] [blame] | 1745 | if (list_empty(&dep->pending_list) && |
| 1746 | list_empty(&dep->started_list)) { |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1747 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1748 | return -EAGAIN; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1749 | } |
| 1750 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 1751 | if (!dwc->dis_start_transfer_quirk && |
| 1752 | (DWC3_VER_IS_PRIOR(DWC31, 170A) || |
| 1753 | DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) { |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 1754 | if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1755 | return dwc3_gadget_start_isoc_quirk(dep); |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1756 | } |
| 1757 | |
Michael Olbrich | c5a7092 | 2020-07-01 20:24:52 +0200 | [diff] [blame] | 1758 | if (desc->bInterval <= 14 && |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 1759 | dwc->gadget->speed >= USB_SPEED_HIGH) { |
Michael Olbrich | c5a7092 | 2020-07-01 20:24:52 +0200 | [diff] [blame] | 1760 | u32 frame = __dwc3_gadget_get_frame(dwc); |
| 1761 | bool rollover = frame < |
| 1762 | (dep->frame_number & DWC3_FRNUMBER_MASK); |
| 1763 | |
| 1764 | /* |
| 1765 | * frame_number is set from XferNotReady and may be already |
| 1766 | * out of date. DSTS only provides the lower 14 bit of the |
| 1767 | * current frame number. So add the upper two bits of |
| 1768 | * frame_number and handle a possible rollover. |
| 1769 | * This will provide the correct frame_number unless more than |
| 1770 | * rollover has happened since XferNotReady. |
| 1771 | */ |
| 1772 | |
| 1773 | dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | |
| 1774 | frame; |
| 1775 | if (rollover) |
| 1776 | dep->frame_number += BIT(14); |
| 1777 | } |
| 1778 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1779 | for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { |
| 1780 | dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1); |
| 1781 | |
| 1782 | ret = __dwc3_gadget_kick_transfer(dep); |
| 1783 | if (ret != -EAGAIN) |
| 1784 | break; |
| 1785 | } |
| 1786 | |
Thinh Nguyen | 36f05d3 | 2020-03-29 16:13:10 -0700 | [diff] [blame] | 1787 | /* |
| 1788 | * After a number of unsuccessful start attempts due to bus-expiry |
| 1789 | * status, issue END_TRANSFER command and retry on the next XferNotReady |
| 1790 | * event. |
| 1791 | */ |
| 1792 | if (ret == -EAGAIN) { |
| 1793 | struct dwc3_gadget_ep_cmd_params params; |
| 1794 | u32 cmd; |
| 1795 | |
| 1796 | cmd = DWC3_DEPCMD_ENDTRANSFER | |
| 1797 | DWC3_DEPCMD_CMDIOC | |
| 1798 | DWC3_DEPCMD_PARAM(dep->resource_index); |
| 1799 | |
| 1800 | dep->resource_index = 0; |
| 1801 | memset(¶ms, 0, sizeof(params)); |
| 1802 | |
| 1803 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 1804 | if (!ret) |
| 1805 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; |
| 1806 | } |
| 1807 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1808 | return ret; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1809 | } |
| 1810 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1811 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1812 | { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1813 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1814 | |
Wesley Cheng | f09ddcf | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 1815 | if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 1816 | dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n", |
| 1817 | dep->name); |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1818 | return -ESHUTDOWN; |
| 1819 | } |
| 1820 | |
Felipe Balbi | 04fb365 | 2017-05-17 15:57:45 +0300 | [diff] [blame] | 1821 | if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", |
| 1822 | &req->request, req->dep->name)) |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1823 | return -EINVAL; |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1824 | |
Felipe Balbi | b2b6d60 | 2019-01-11 12:58:52 +0200 | [diff] [blame] | 1825 | if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, |
| 1826 | "%s: request %pK already in flight\n", |
| 1827 | dep->name, &req->request)) |
| 1828 | return -EINVAL; |
| 1829 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1830 | pm_runtime_get(dwc->dev); |
| 1831 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1832 | req->request.actual = 0; |
| 1833 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1834 | |
Felipe Balbi | fe84f52 | 2015-09-01 09:01:38 -0500 | [diff] [blame] | 1835 | trace_dwc3_ep_queue(req); |
| 1836 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1837 | list_add_tail(&req->list, &dep->pending_list); |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 1838 | req->status = DWC3_REQUEST_STATUS_QUEUED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1839 | |
Thinh Nguyen | e0d1956 | 2020-05-05 19:46:57 -0700 | [diff] [blame] | 1840 | if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) |
| 1841 | return 0; |
| 1842 | |
Thinh Nguyen | c503672 | 2020-09-02 18:42:58 -0700 | [diff] [blame] | 1843 | /* |
| 1844 | * Start the transfer only after the END_TRANSFER is completed |
| 1845 | * and endpoint STALL is cleared. |
| 1846 | */ |
| 1847 | if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || |
| 1848 | (dep->flags & DWC3_EP_WEDGE) || |
| 1849 | (dep->flags & DWC3_EP_STALL)) { |
Thinh Nguyen | da10bcd | 2019-12-18 18:14:50 -0800 | [diff] [blame] | 1850 | dep->flags |= DWC3_EP_DELAY_START; |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
Felipe Balbi | d889c23 | 2016-09-29 15:44:29 +0300 | [diff] [blame] | 1854 | /* |
| 1855 | * NOTICE: Isochronous endpoints should NEVER be prestarted. We must |
| 1856 | * wait for a XferNotReady event so we will know what's the current |
| 1857 | * (micro-)frame number. |
| 1858 | * |
| 1859 | * Without this trick, we are very, very likely gonna get Bus Expiry |
| 1860 | * errors which will force us issue EndTransfer command. |
| 1861 | */ |
| 1862 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1863 | if (!(dep->flags & DWC3_EP_PENDING_REQUEST) && |
| 1864 | !(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Roger Quadros | f1d6826 | 2017-04-21 15:58:08 +0300 | [diff] [blame] | 1865 | return 0; |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1866 | |
| 1867 | if ((dep->flags & DWC3_EP_PENDING_REQUEST)) { |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 1868 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1869 | return __dwc3_gadget_start_isoc(dep); |
Felipe Balbi | 08a36b5 | 2016-08-11 14:27:52 +0300 | [diff] [blame] | 1870 | } |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1871 | } |
| 1872 | |
Wesley Cheng | 18ffa98 | 2021-05-07 10:55:19 -0700 | [diff] [blame] | 1873 | __dwc3_gadget_kick_transfer(dep); |
| 1874 | |
| 1875 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1879 | gfp_t gfp_flags) |
| 1880 | { |
| 1881 | struct dwc3_request *req = to_dwc3_request(request); |
| 1882 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1883 | struct dwc3 *dwc = dep->dwc; |
| 1884 | |
| 1885 | unsigned long flags; |
| 1886 | |
| 1887 | int ret; |
| 1888 | |
Zhuang Jin Can | fdee4eb | 2014-09-03 14:26:34 +0800 | [diff] [blame] | 1889 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1890 | ret = __dwc3_gadget_ep_queue(dep, req); |
| 1891 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1892 | |
| 1893 | return ret; |
| 1894 | } |
| 1895 | |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1896 | static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1897 | { |
| 1898 | int i; |
| 1899 | |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 1900 | /* If req->trb is not set, then the request has not started */ |
| 1901 | if (!req->trb) |
| 1902 | return; |
| 1903 | |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1904 | /* |
| 1905 | * If request was already started, this means we had to |
| 1906 | * stop the transfer. With that we also need to ignore |
| 1907 | * all TRBs used by the request, however TRBs can only |
| 1908 | * be modified after completion of END_TRANSFER |
| 1909 | * command. So what we do here is that we wait for |
| 1910 | * END_TRANSFER completion and only after that, we jump |
| 1911 | * over TRBs by clearing HWO and incrementing dequeue |
| 1912 | * pointer. |
| 1913 | */ |
| 1914 | for (i = 0; i < req->num_trbs; i++) { |
| 1915 | struct dwc3_trb *trb; |
| 1916 | |
Thinh Nguyen | 2dedea0 | 2020-03-05 13:24:01 -0800 | [diff] [blame] | 1917 | trb = &dep->trb_pool[dep->trb_dequeue]; |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1918 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 1919 | dwc3_ep_inc_deq(dep); |
| 1920 | } |
Thinh Nguyen | c715276 | 2019-02-12 19:39:27 -0800 | [diff] [blame] | 1921 | |
| 1922 | req->num_trbs = 0; |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1923 | } |
| 1924 | |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1925 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) |
| 1926 | { |
| 1927 | struct dwc3_request *req; |
| 1928 | struct dwc3_request *tmp; |
Ray Chi | 04dd6e7 | 2021-03-28 02:17:42 +0800 | [diff] [blame] | 1929 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1930 | |
| 1931 | list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) { |
| 1932 | dwc3_gadget_ep_skip_trbs(dep, req); |
Ray Chi | 04dd6e7 | 2021-03-28 02:17:42 +0800 | [diff] [blame] | 1933 | switch (req->status) { |
| 1934 | case DWC3_REQUEST_STATUS_DISCONNECTED: |
| 1935 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 1936 | break; |
| 1937 | case DWC3_REQUEST_STATUS_DEQUEUED: |
| 1938 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1939 | break; |
| 1940 | case DWC3_REQUEST_STATUS_STALLED: |
| 1941 | dwc3_gadget_giveback(dep, req, -EPIPE); |
| 1942 | break; |
| 1943 | default: |
| 1944 | dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status); |
| 1945 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1946 | break; |
| 1947 | } |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1951 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1952 | struct usb_request *request) |
| 1953 | { |
| 1954 | struct dwc3_request *req = to_dwc3_request(request); |
| 1955 | struct dwc3_request *r = NULL; |
| 1956 | |
| 1957 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1958 | struct dwc3 *dwc = dep->dwc; |
| 1959 | |
| 1960 | unsigned long flags; |
| 1961 | int ret = 0; |
| 1962 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1963 | trace_dwc3_ep_dequeue(req); |
| 1964 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1965 | spin_lock_irqsave(&dwc->lock, flags); |
| 1966 | |
Thinh Nguyen | a7027ca | 2020-03-05 13:24:08 -0800 | [diff] [blame] | 1967 | list_for_each_entry(r, &dep->cancelled_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1968 | if (r == req) |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1969 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1970 | } |
| 1971 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1972 | list_for_each_entry(r, &dep->pending_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1973 | if (r == req) { |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1974 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1975 | goto out; |
| 1976 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1977 | } |
| 1978 | |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1979 | list_for_each_entry(r, &dep->started_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1980 | if (r == req) { |
Thinh Nguyen | a7027ca | 2020-03-05 13:24:08 -0800 | [diff] [blame] | 1981 | struct dwc3_request *t; |
| 1982 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1983 | /* wait until it is processed */ |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 1984 | dwc3_stop_active_transfer(dep, true, true); |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1985 | |
Thinh Nguyen | a7027ca | 2020-03-05 13:24:08 -0800 | [diff] [blame] | 1986 | /* |
| 1987 | * Remove any started request if the transfer is |
| 1988 | * cancelled. |
| 1989 | */ |
| 1990 | list_for_each_entry_safe(r, t, &dep->started_list, list) |
Ray Chi | 04dd6e7 | 2021-03-28 02:17:42 +0800 | [diff] [blame] | 1991 | dwc3_gadget_move_cancelled_request(r, |
| 1992 | DWC3_REQUEST_STATUS_DEQUEUED); |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1993 | |
Thinh Nguyen | a5c7682 | 2021-01-04 22:42:39 -0800 | [diff] [blame] | 1994 | dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; |
| 1995 | |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1996 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1997 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1998 | } |
| 1999 | |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 2000 | dev_err(dwc->dev, "request %pK was not queued to %s\n", |
| 2001 | request, ep->name); |
| 2002 | ret = -EINVAL; |
| 2003 | out: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2004 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2005 | |
| 2006 | return ret; |
| 2007 | } |
| 2008 | |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 2009 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2010 | { |
| 2011 | struct dwc3_gadget_ep_cmd_params params; |
| 2012 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 2013 | struct dwc3_request *req; |
| 2014 | struct dwc3_request *tmp; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2015 | int ret; |
| 2016 | |
Felipe Balbi | 5ad02fb | 2014-09-24 10:48:26 -0500 | [diff] [blame] | 2017 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 2018 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 2019 | return -EINVAL; |
| 2020 | } |
| 2021 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2022 | memset(¶ms, 0x00, sizeof(params)); |
| 2023 | |
| 2024 | if (value) { |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 2025 | struct dwc3_trb *trb; |
| 2026 | |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 2027 | unsigned int transfer_in_flight; |
| 2028 | unsigned int started; |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 2029 | |
| 2030 | if (dep->number > 1) |
| 2031 | trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 2032 | else |
| 2033 | trb = &dwc->ep0_trb[dep->trb_enqueue]; |
| 2034 | |
| 2035 | transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; |
| 2036 | started = !list_empty(&dep->started_list); |
| 2037 | |
| 2038 | if (!protocol && ((dep->direction && transfer_in_flight) || |
| 2039 | (!dep->direction && started))) { |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 2040 | return -EAGAIN; |
| 2041 | } |
| 2042 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 2043 | ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, |
| 2044 | ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2045 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 2046 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2047 | dep->name); |
| 2048 | else |
| 2049 | dep->flags |= DWC3_EP_STALL; |
| 2050 | } else { |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 2051 | /* |
| 2052 | * Don't issue CLEAR_STALL command to control endpoints. The |
| 2053 | * controller automatically clears the STALL when it receives |
| 2054 | * the SETUP token. |
| 2055 | */ |
| 2056 | if (dep->number <= 1) { |
| 2057 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 2058 | return 0; |
| 2059 | } |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 2060 | |
Thinh Nguyen | d97c78a | 2020-09-02 18:43:04 -0700 | [diff] [blame] | 2061 | dwc3_stop_active_transfer(dep, true, true); |
| 2062 | |
| 2063 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) |
Ray Chi | 04dd6e7 | 2021-03-28 02:17:42 +0800 | [diff] [blame] | 2064 | dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED); |
Thinh Nguyen | d97c78a | 2020-09-02 18:43:04 -0700 | [diff] [blame] | 2065 | |
| 2066 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) { |
| 2067 | dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
| 2072 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 2073 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 2074 | if (ret) { |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 2075 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2076 | dep->name); |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 2077 | return ret; |
| 2078 | } |
| 2079 | |
| 2080 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 2081 | |
Thinh Nguyen | c503672 | 2020-09-02 18:42:58 -0700 | [diff] [blame] | 2082 | if ((dep->flags & DWC3_EP_DELAY_START) && |
| 2083 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 2084 | __dwc3_gadget_kick_transfer(dep); |
| 2085 | |
| 2086 | dep->flags &= ~DWC3_EP_DELAY_START; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2087 | } |
Paul Zimmerman | 5275455 | 2011-09-30 10:58:44 +0300 | [diff] [blame] | 2088 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2089 | return ret; |
| 2090 | } |
| 2091 | |
| 2092 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 2093 | { |
| 2094 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 2095 | struct dwc3 *dwc = dep->dwc; |
| 2096 | |
| 2097 | unsigned long flags; |
| 2098 | |
| 2099 | int ret; |
| 2100 | |
| 2101 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 2102 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2103 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2104 | |
| 2105 | return ret; |
| 2106 | } |
| 2107 | |
| 2108 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 2109 | { |
| 2110 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2111 | struct dwc3 *dwc = dep->dwc; |
| 2112 | unsigned long flags; |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 2113 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2114 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2115 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2116 | dep->flags |= DWC3_EP_WEDGE; |
| 2117 | |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 2118 | if (dep->number == 0 || dep->number == 1) |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 2119 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 2120 | else |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 2121 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 2122 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2123 | |
| 2124 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /* -------------------------------------------------------------------------- */ |
| 2128 | |
| 2129 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 2130 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 2131 | .bDescriptorType = USB_DT_ENDPOINT, |
| 2132 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 2133 | }; |
| 2134 | |
| 2135 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 2136 | .enable = dwc3_gadget_ep0_enable, |
| 2137 | .disable = dwc3_gadget_ep0_disable, |
| 2138 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 2139 | .free_request = dwc3_gadget_ep_free_request, |
| 2140 | .queue = dwc3_gadget_ep0_queue, |
| 2141 | .dequeue = dwc3_gadget_ep_dequeue, |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 2142 | .set_halt = dwc3_gadget_ep0_set_halt, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2143 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 2144 | }; |
| 2145 | |
| 2146 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 2147 | .enable = dwc3_gadget_ep_enable, |
| 2148 | .disable = dwc3_gadget_ep_disable, |
| 2149 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 2150 | .free_request = dwc3_gadget_ep_free_request, |
| 2151 | .queue = dwc3_gadget_ep_queue, |
| 2152 | .dequeue = dwc3_gadget_ep_dequeue, |
| 2153 | .set_halt = dwc3_gadget_ep_set_halt, |
| 2154 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 2155 | }; |
| 2156 | |
| 2157 | /* -------------------------------------------------------------------------- */ |
| 2158 | |
| 2159 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 2160 | { |
| 2161 | struct dwc3 *dwc = gadget_to_dwc(g); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2162 | |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 2163 | return __dwc3_gadget_get_frame(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2164 | } |
| 2165 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2166 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2167 | { |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 2168 | int retries; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2169 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2170 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2171 | u32 reg; |
| 2172 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2173 | u8 link_state; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2174 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2175 | /* |
| 2176 | * According to the Databook Remote wakeup request should |
| 2177 | * be issued only when the device is in early suspend state. |
| 2178 | * |
| 2179 | * We can check that via USB Link State bits in DSTS register. |
| 2180 | */ |
| 2181 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2182 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2183 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 2184 | |
| 2185 | switch (link_state) { |
Thinh Nguyen | d0550cd | 2020-01-31 16:25:50 -0800 | [diff] [blame] | 2186 | case DWC3_LINK_STATE_RESET: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2187 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 2188 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
Thinh Nguyen | c560e76 | 2021-04-19 19:11:12 -0700 | [diff] [blame] | 2189 | case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */ |
| 2190 | case DWC3_LINK_STATE_U1: |
Thinh Nguyen | d0550cd | 2020-01-31 16:25:50 -0800 | [diff] [blame] | 2191 | case DWC3_LINK_STATE_RESUME: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2192 | break; |
| 2193 | default: |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2194 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2195 | } |
| 2196 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 2197 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 2198 | if (ret < 0) { |
| 2199 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2200 | return ret; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 2201 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2202 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2203 | /* Recent versions do this automatically */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2204 | if (DWC3_VER_IS_PRIOR(DWC3, 194A)) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2205 | /* write zeroes to Link Change Request */ |
Felipe Balbi | fcc023c | 2012-05-24 10:27:56 +0300 | [diff] [blame] | 2206 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2207 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 2208 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2209 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2210 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 2211 | /* poll until Link State changes to ON */ |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 2212 | retries = 20000; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2213 | |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 2214 | while (retries--) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2215 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2216 | |
| 2217 | /* in HS, means ON */ |
| 2218 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 2219 | break; |
| 2220 | } |
| 2221 | |
| 2222 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 2223 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2224 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2225 | } |
| 2226 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 2231 | { |
| 2232 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2233 | unsigned long flags; |
| 2234 | int ret; |
| 2235 | |
| 2236 | spin_lock_irqsave(&dwc->lock, flags); |
| 2237 | ret = __dwc3_gadget_wakeup(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2238 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2239 | |
| 2240 | return ret; |
| 2241 | } |
| 2242 | |
| 2243 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 2244 | int is_selfpowered) |
| 2245 | { |
| 2246 | struct dwc3 *dwc = gadget_to_dwc(g); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2247 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2248 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2249 | spin_lock_irqsave(&dwc->lock, flags); |
Peter Chen | bcdea50 | 2015-01-28 16:32:40 +0800 | [diff] [blame] | 2250 | g->is_selfpowered = !!is_selfpowered; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2251 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2252 | |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2256 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
| 2257 | { |
| 2258 | u32 epnum; |
| 2259 | |
| 2260 | for (epnum = 2; epnum < dwc->num_eps; epnum++) { |
| 2261 | struct dwc3_ep *dep; |
| 2262 | |
| 2263 | dep = dwc->eps[epnum]; |
| 2264 | if (!dep) |
| 2265 | continue; |
| 2266 | |
| 2267 | dwc3_remove_requests(dwc, dep); |
| 2268 | } |
| 2269 | } |
| 2270 | |
Thinh Nguyen | 072cab8a | 2021-01-19 17:36:28 -0800 | [diff] [blame] | 2271 | static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc) |
| 2272 | { |
| 2273 | enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate; |
| 2274 | u32 reg; |
| 2275 | |
| 2276 | if (ssp_rate == USB_SSP_GEN_UNKNOWN) |
| 2277 | ssp_rate = dwc->max_ssp_rate; |
| 2278 | |
| 2279 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2280 | reg &= ~DWC3_DCFG_SPEED_MASK; |
| 2281 | reg &= ~DWC3_DCFG_NUMLANES(~0); |
| 2282 | |
| 2283 | if (ssp_rate == USB_SSP_GEN_1x2) |
| 2284 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2285 | else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2) |
| 2286 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
| 2287 | |
| 2288 | if (ssp_rate != USB_SSP_GEN_2x1 && |
| 2289 | dwc->max_ssp_rate != USB_SSP_GEN_2x1) |
| 2290 | reg |= DWC3_DCFG_NUMLANES(1); |
| 2291 | |
| 2292 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2293 | } |
| 2294 | |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2295 | static void __dwc3_gadget_set_speed(struct dwc3 *dwc) |
| 2296 | { |
Thinh Nguyen | 450b9e9 | 2021-01-19 17:36:40 -0800 | [diff] [blame] | 2297 | enum usb_device_speed speed; |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2298 | u32 reg; |
| 2299 | |
Thinh Nguyen | 450b9e9 | 2021-01-19 17:36:40 -0800 | [diff] [blame] | 2300 | speed = dwc->gadget_max_speed; |
Thinh Nguyen | 93f1d43 | 2021-03-08 18:16:50 -0800 | [diff] [blame] | 2301 | if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed) |
Thinh Nguyen | 450b9e9 | 2021-01-19 17:36:40 -0800 | [diff] [blame] | 2302 | speed = dwc->maximum_speed; |
| 2303 | |
| 2304 | if (speed == USB_SPEED_SUPER_PLUS && |
Thinh Nguyen | 072cab8a | 2021-01-19 17:36:28 -0800 | [diff] [blame] | 2305 | DWC3_IP_IS(DWC32)) { |
| 2306 | __dwc3_gadget_set_ssp_rate(dwc); |
| 2307 | return; |
| 2308 | } |
| 2309 | |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2310 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2311 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
| 2312 | |
| 2313 | /* |
| 2314 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 2315 | * which would cause metastability state on Run/Stop |
| 2316 | * bit if we try to force the IP to USB2-only mode. |
| 2317 | * |
| 2318 | * Because of that, we cannot configure the IP to any |
| 2319 | * speed other than the SuperSpeed |
| 2320 | * |
| 2321 | * Refers to: |
| 2322 | * |
| 2323 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 2324 | * USB 2.0 Mode |
| 2325 | */ |
| 2326 | if (DWC3_VER_IS_PRIOR(DWC3, 220A) && |
| 2327 | !dwc->dis_metastability_quirk) { |
| 2328 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2329 | } else { |
Thinh Nguyen | 450b9e9 | 2021-01-19 17:36:40 -0800 | [diff] [blame] | 2330 | switch (speed) { |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2331 | case USB_SPEED_FULL: |
| 2332 | reg |= DWC3_DCFG_FULLSPEED; |
| 2333 | break; |
| 2334 | case USB_SPEED_HIGH: |
| 2335 | reg |= DWC3_DCFG_HIGHSPEED; |
| 2336 | break; |
| 2337 | case USB_SPEED_SUPER: |
| 2338 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2339 | break; |
| 2340 | case USB_SPEED_SUPER_PLUS: |
| 2341 | if (DWC3_IP_IS(DWC3)) |
| 2342 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2343 | else |
| 2344 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
| 2345 | break; |
| 2346 | default: |
Thinh Nguyen | 450b9e9 | 2021-01-19 17:36:40 -0800 | [diff] [blame] | 2347 | dev_err(dwc->dev, "invalid speed (%d)\n", speed); |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2348 | |
| 2349 | if (DWC3_IP_IS(DWC3)) |
| 2350 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2351 | else |
| 2352 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
| 2353 | } |
| 2354 | } |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 2355 | |
| 2356 | if (DWC3_IP_IS(DWC32) && |
Thinh Nguyen | 450b9e9 | 2021-01-19 17:36:40 -0800 | [diff] [blame] | 2357 | speed > USB_SPEED_UNKNOWN && |
| 2358 | speed < USB_SPEED_SUPER_PLUS) |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 2359 | reg &= ~DWC3_DCFG_NUMLANES(~0); |
| 2360 | |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2361 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2362 | } |
| 2363 | |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2364 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2365 | { |
| 2366 | u32 reg; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 2367 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2368 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2369 | if (pm_runtime_suspended(dwc->dev)) |
| 2370 | return 0; |
| 2371 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2372 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 2373 | if (is_on) { |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2374 | if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2375 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 2376 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 2377 | } |
| 2378 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2379 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2380 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 2381 | reg |= DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2382 | |
| 2383 | if (dwc->has_hibernation) |
| 2384 | reg |= DWC3_DCTL_KEEP_CONNECT; |
| 2385 | |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2386 | __dwc3_gadget_set_speed(dwc); |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 2387 | dwc->pullups_connected = true; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 2388 | } else { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2389 | reg &= ~DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2390 | |
| 2391 | if (dwc->has_hibernation && !suspend) |
| 2392 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 2393 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 2394 | dwc->pullups_connected = false; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 2395 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2396 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 2397 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2398 | |
| 2399 | do { |
| 2400 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
Felipe Balbi | b6d4e16 | 2016-06-09 16:47:05 +0300 | [diff] [blame] | 2401 | reg &= DWC3_DSTS_DEVCTRLHLT; |
| 2402 | } while (--timeout && !(!is_on ^ !reg)); |
Felipe Balbi | f2df679 | 2016-06-09 16:31:34 +0300 | [diff] [blame] | 2403 | |
| 2404 | if (!timeout) |
| 2405 | return -ETIMEDOUT; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2406 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 2407 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2408 | } |
| 2409 | |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2410 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc); |
| 2411 | static void __dwc3_gadget_stop(struct dwc3 *dwc); |
Wesley Cheng | a1383b3 | 2020-12-29 15:00:37 -0800 | [diff] [blame] | 2412 | static int __dwc3_gadget_start(struct dwc3 *dwc); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2413 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2414 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 2415 | { |
| 2416 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2417 | unsigned long flags; |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 2418 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2419 | |
| 2420 | is_on = !!is_on; |
| 2421 | |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 2422 | /* |
| 2423 | * Per databook, when we want to stop the gadget, if a control transfer |
| 2424 | * is still in process, complete it and get the core into setup phase. |
| 2425 | */ |
| 2426 | if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) { |
| 2427 | reinit_completion(&dwc->ep0_in_setup); |
| 2428 | |
| 2429 | ret = wait_for_completion_timeout(&dwc->ep0_in_setup, |
| 2430 | msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); |
| 2431 | if (ret == 0) { |
| 2432 | dev_err(dwc->dev, "timed out waiting for SETUP phase\n"); |
| 2433 | return -ETIMEDOUT; |
| 2434 | } |
| 2435 | } |
| 2436 | |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2437 | /* |
Wesley Cheng | 77adb8b | 2020-12-29 15:05:35 -0800 | [diff] [blame] | 2438 | * Check the return value for successful resume, or error. For a |
| 2439 | * successful resume, the DWC3 runtime PM resume routine will handle |
| 2440 | * the run stop sequence, so avoid duplicate operations here. |
| 2441 | */ |
| 2442 | ret = pm_runtime_get_sync(dwc->dev); |
| 2443 | if (!ret || ret < 0) { |
| 2444 | pm_runtime_put(dwc->dev); |
| 2445 | return 0; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
Wesley Cheng | 8212937 | 2021-05-20 21:23:57 -0700 | [diff] [blame] | 2449 | * Synchronize and disable any further event handling while controller |
| 2450 | * is being enabled/disabled. |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2451 | */ |
Wesley Cheng | 8212937 | 2021-05-20 21:23:57 -0700 | [diff] [blame] | 2452 | disable_irq(dwc->irq_gadget); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2453 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2454 | spin_lock_irqsave(&dwc->lock, flags); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2455 | |
| 2456 | if (!is_on) { |
| 2457 | u32 count; |
| 2458 | |
Wesley Cheng | f09ddcf | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 2459 | dwc->connected = false; |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2460 | /* |
| 2461 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a |
| 2462 | * Section 4.1.8 Table 4-7, it states that for a device-initiated |
| 2463 | * disconnect, the SW needs to ensure that it sends "a DEPENDXFER |
| 2464 | * command for any active transfers" before clearing the RunStop |
| 2465 | * bit. |
| 2466 | */ |
| 2467 | dwc3_stop_active_transfers(dwc); |
| 2468 | __dwc3_gadget_stop(dwc); |
| 2469 | |
| 2470 | /* |
| 2471 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a |
| 2472 | * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the |
| 2473 | * "software needs to acknowledge the events that are generated |
| 2474 | * (by writing to GEVNTCOUNTn) while it is waiting for this bit |
| 2475 | * to be set to '1'." |
| 2476 | */ |
| 2477 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
| 2478 | count &= DWC3_GEVNTCOUNT_MASK; |
| 2479 | if (count > 0) { |
| 2480 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
| 2481 | dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) % |
| 2482 | dwc->ev_buf->length; |
| 2483 | } |
Wesley Cheng | a1383b3 | 2020-12-29 15:00:37 -0800 | [diff] [blame] | 2484 | } else { |
| 2485 | __dwc3_gadget_start(dwc); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2488 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2489 | spin_unlock_irqrestore(&dwc->lock, flags); |
Wesley Cheng | 8212937 | 2021-05-20 21:23:57 -0700 | [diff] [blame] | 2490 | enable_irq(dwc->irq_gadget); |
| 2491 | |
Wesley Cheng | 77adb8b | 2020-12-29 15:05:35 -0800 | [diff] [blame] | 2492 | pm_runtime_put(dwc->dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2493 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 2494 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2495 | } |
| 2496 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2497 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
| 2498 | { |
| 2499 | u32 reg; |
| 2500 | |
| 2501 | /* Enable all but Start and End of Frame IRQs */ |
Thinh Nguyen | 132ee0d | 2021-01-13 19:55:29 -0800 | [diff] [blame] | 2502 | reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2503 | DWC3_DEVTEN_CMDCMPLTEN | |
| 2504 | DWC3_DEVTEN_ERRTICERREN | |
| 2505 | DWC3_DEVTEN_WKUPEVTEN | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2506 | DWC3_DEVTEN_CONNECTDONEEN | |
| 2507 | DWC3_DEVTEN_USBRSTEN | |
| 2508 | DWC3_DEVTEN_DISCONNEVTEN); |
| 2509 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2510 | if (DWC3_VER_IS_PRIOR(DWC3, 250A)) |
Felipe Balbi | 799e9dc | 2016-09-23 11:20:40 +0300 | [diff] [blame] | 2511 | reg |= DWC3_DEVTEN_ULSTCNGEN; |
| 2512 | |
Jack Pham | d1d90dd | 2021-04-28 02:01:10 -0700 | [diff] [blame] | 2513 | /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ |
| 2514 | if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) |
Jack Pham | 6f26ebb | 2021-04-28 02:01:11 -0700 | [diff] [blame] | 2515 | reg |= DWC3_DEVTEN_U3L2L1SUSPEN; |
Jack Pham | d1d90dd | 2021-04-28 02:01:10 -0700 | [diff] [blame] | 2516 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2517 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 2518 | } |
| 2519 | |
| 2520 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) |
| 2521 | { |
| 2522 | /* mask all interrupts */ |
| 2523 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 2524 | } |
| 2525 | |
| 2526 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2527 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2528 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2529 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 2530 | * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG |
| 2531 | * @dwc: pointer to our context structure |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2532 | * |
| 2533 | * The following looks like complex but it's actually very simple. In order to |
| 2534 | * calculate the number of packets we can burst at once on OUT transfers, we're |
| 2535 | * gonna use RxFIFO size. |
| 2536 | * |
| 2537 | * To calculate RxFIFO size we need two numbers: |
| 2538 | * MDWIDTH = size, in bits, of the internal memory bus |
| 2539 | * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) |
| 2540 | * |
| 2541 | * Given these two numbers, the formula is simple: |
| 2542 | * |
| 2543 | * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; |
| 2544 | * |
| 2545 | * 24 bytes is for 3x SETUP packets |
| 2546 | * 16 bytes is a clock domain crossing tolerance |
| 2547 | * |
| 2548 | * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; |
| 2549 | */ |
| 2550 | static void dwc3_gadget_setup_nump(struct dwc3 *dwc) |
| 2551 | { |
| 2552 | u32 ram2_depth; |
| 2553 | u32 mdwidth; |
| 2554 | u32 nump; |
| 2555 | u32 reg; |
| 2556 | |
| 2557 | ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); |
Thinh Nguyen | d00be77 | 2021-03-27 17:54:01 -0700 | [diff] [blame] | 2558 | mdwidth = dwc3_mdwidth(dwc); |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2559 | |
| 2560 | nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; |
| 2561 | nump = min_t(u32, nump, 16); |
| 2562 | |
| 2563 | /* update NumP */ |
| 2564 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2565 | reg &= ~DWC3_DCFG_NUMP_MASK; |
| 2566 | reg |= nump << DWC3_DCFG_NUMP_SHIFT; |
| 2567 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2568 | } |
| 2569 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2570 | static int __dwc3_gadget_start(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2571 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2572 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2573 | int ret = 0; |
| 2574 | u32 reg; |
| 2575 | |
John Youn | cf40b86 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 2576 | /* |
| 2577 | * Use IMOD if enabled via dwc->imod_interval. Otherwise, if |
| 2578 | * the core supports IMOD, disable it. |
| 2579 | */ |
| 2580 | if (dwc->imod_interval) { |
| 2581 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); |
| 2582 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); |
| 2583 | } else if (dwc3_has_imod(dwc)) { |
| 2584 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); |
| 2585 | } |
| 2586 | |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 2587 | /* |
| 2588 | * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP |
| 2589 | * field instead of letting dwc3 itself calculate that automatically. |
| 2590 | * |
| 2591 | * This way, we maximize the chances that we'll be able to get several |
| 2592 | * bursts of data without going through any sort of endpoint throttling. |
| 2593 | */ |
| 2594 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2595 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | 01b0e2c | 2018-03-16 15:34:13 -0700 | [diff] [blame] | 2596 | reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2597 | else |
| 2598 | reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; |
Thinh Nguyen | 01b0e2c | 2018-03-16 15:34:13 -0700 | [diff] [blame] | 2599 | |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 2600 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); |
| 2601 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2602 | dwc3_gadget_setup_nump(dwc); |
| 2603 | |
Thinh Nguyen | e66bbfb | 2021-04-12 20:00:45 -0700 | [diff] [blame] | 2604 | /* |
| 2605 | * Currently the controller handles single stream only. So, Ignore |
| 2606 | * Packet Pending bit for stream selection and don't search for another |
| 2607 | * stream if the host sends Data Packet with PP=0 (for OUT direction) or |
| 2608 | * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves |
| 2609 | * the stream performance. |
| 2610 | */ |
| 2611 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2612 | reg |= DWC3_DCFG_IGNSTRMPP; |
| 2613 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2614 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2615 | /* Start with SuperSpeed Default */ |
| 2616 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2617 | |
| 2618 | dep = dwc->eps[0]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 2619 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2620 | if (ret) { |
| 2621 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2622 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | dep = dwc->eps[1]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 2626 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2627 | if (ret) { |
| 2628 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2629 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* begin to receive SETUP packets */ |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 2633 | dwc->ep0state = EP0_SETUP_PHASE; |
Zeng Tao | 88b1bb1 | 2018-12-26 19:22:00 +0800 | [diff] [blame] | 2634 | dwc->link_state = DWC3_LINK_STATE_SS_DIS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2635 | dwc3_ep0_out_start(dwc); |
| 2636 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2637 | dwc3_gadget_enable_irq(dwc); |
| 2638 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2639 | return 0; |
| 2640 | |
| 2641 | err1: |
| 2642 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2643 | |
| 2644 | err0: |
| 2645 | return ret; |
| 2646 | } |
| 2647 | |
| 2648 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 2649 | struct usb_gadget_driver *driver) |
| 2650 | { |
| 2651 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2652 | unsigned long flags; |
Thinh Nguyen | 8cf9045 | 2021-02-05 01:53:47 -0800 | [diff] [blame] | 2653 | int ret; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2654 | int irq; |
| 2655 | |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 2656 | irq = dwc->irq_gadget; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2657 | ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, |
| 2658 | IRQF_SHARED, "dwc3", dwc->ev_buf); |
| 2659 | if (ret) { |
| 2660 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", |
| 2661 | irq, ret); |
Thinh Nguyen | 8cf9045 | 2021-02-05 01:53:47 -0800 | [diff] [blame] | 2662 | return ret; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2666 | dwc->gadget_driver = driver; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2667 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2668 | |
| 2669 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2670 | } |
| 2671 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2672 | static void __dwc3_gadget_stop(struct dwc3 *dwc) |
| 2673 | { |
| 2674 | dwc3_gadget_disable_irq(dwc); |
| 2675 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2676 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 2677 | } |
| 2678 | |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 2679 | static int dwc3_gadget_stop(struct usb_gadget *g) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2680 | { |
| 2681 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2682 | unsigned long flags; |
| 2683 | |
| 2684 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2685 | dwc->gadget_driver = NULL; |
Wesley Cheng | 9f607a3 | 2021-07-10 02:13:12 -0700 | [diff] [blame^] | 2686 | dwc->max_cfg_eps = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2687 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2688 | |
Felipe Balbi | 3f308d1 | 2016-05-16 14:17:06 +0300 | [diff] [blame] | 2689 | free_irq(dwc->irq_gadget, dwc->ev_buf); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2690 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2691 | return 0; |
| 2692 | } |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2693 | |
Anurag Kumar Vulisha | 729dcff | 2019-05-10 12:37:28 +0530 | [diff] [blame] | 2694 | static void dwc3_gadget_config_params(struct usb_gadget *g, |
| 2695 | struct usb_dcd_config_params *params) |
| 2696 | { |
| 2697 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2698 | |
Thinh Nguyen | 54fb5ba | 2019-08-19 18:36:06 -0700 | [diff] [blame] | 2699 | params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; |
| 2700 | params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; |
| 2701 | |
| 2702 | /* Recommended BESL */ |
| 2703 | if (!dwc->dis_enblslpm_quirk) { |
Thinh Nguyen | 17b6370 | 2019-08-29 18:00:16 -0700 | [diff] [blame] | 2704 | /* |
| 2705 | * If the recommended BESL baseline is 0 or if the BESL deep is |
| 2706 | * less than 2, Microsoft's Windows 10 host usb stack will issue |
| 2707 | * a usb reset immediately after it receives the extended BOS |
| 2708 | * descriptor and the enumeration will fail. To maintain |
| 2709 | * compatibility with the Windows' usb stack, let's set the |
| 2710 | * recommended BESL baseline to 1 and clamp the BESL deep to be |
| 2711 | * within 2 to 15. |
| 2712 | */ |
| 2713 | params->besl_baseline = 1; |
Thinh Nguyen | 54fb5ba | 2019-08-19 18:36:06 -0700 | [diff] [blame] | 2714 | if (dwc->is_utmi_l1_suspend) |
Thinh Nguyen | 17b6370 | 2019-08-29 18:00:16 -0700 | [diff] [blame] | 2715 | params->besl_deep = |
| 2716 | clamp_t(u8, dwc->hird_threshold, 2, 15); |
Thinh Nguyen | 54fb5ba | 2019-08-19 18:36:06 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Anurag Kumar Vulisha | 729dcff | 2019-05-10 12:37:28 +0530 | [diff] [blame] | 2719 | /* U1 Device exit Latency */ |
| 2720 | if (dwc->dis_u1_entry_quirk) |
| 2721 | params->bU1devExitLat = 0; |
| 2722 | else |
| 2723 | params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; |
| 2724 | |
| 2725 | /* U2 Device exit Latency */ |
| 2726 | if (dwc->dis_u2_entry_quirk) |
| 2727 | params->bU2DevExitLat = 0; |
| 2728 | else |
| 2729 | params->bU2DevExitLat = |
| 2730 | cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT); |
| 2731 | } |
| 2732 | |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2733 | static void dwc3_gadget_set_speed(struct usb_gadget *g, |
| 2734 | enum usb_device_speed speed) |
| 2735 | { |
| 2736 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2737 | unsigned long flags; |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2738 | |
| 2739 | spin_lock_irqsave(&dwc->lock, flags); |
Wesley Cheng | 7c9a259 | 2020-12-29 15:05:36 -0800 | [diff] [blame] | 2740 | dwc->gadget_max_speed = speed; |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2741 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2742 | } |
| 2743 | |
Thinh Nguyen | 072cab8a | 2021-01-19 17:36:28 -0800 | [diff] [blame] | 2744 | static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g, |
| 2745 | enum usb_ssp_rate rate) |
| 2746 | { |
| 2747 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2748 | unsigned long flags; |
| 2749 | |
| 2750 | spin_lock_irqsave(&dwc->lock, flags); |
Thinh Nguyen | cdb651b | 2021-03-08 18:16:44 -0800 | [diff] [blame] | 2751 | dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; |
Thinh Nguyen | 072cab8a | 2021-01-19 17:36:28 -0800 | [diff] [blame] | 2752 | dwc->gadget_ssp_rate = rate; |
| 2753 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2754 | } |
| 2755 | |
Wesley Cheng | 82c46b8 | 2020-12-29 15:03:29 -0800 | [diff] [blame] | 2756 | static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA) |
| 2757 | { |
| 2758 | struct dwc3 *dwc = gadget_to_dwc(g); |
Ray Chi | 99288de | 2021-02-22 19:51:49 +0800 | [diff] [blame] | 2759 | union power_supply_propval val = {0}; |
| 2760 | int ret; |
Wesley Cheng | 82c46b8 | 2020-12-29 15:03:29 -0800 | [diff] [blame] | 2761 | |
| 2762 | if (dwc->usb2_phy) |
| 2763 | return usb_phy_set_power(dwc->usb2_phy, mA); |
| 2764 | |
Ray Chi | 99288de | 2021-02-22 19:51:49 +0800 | [diff] [blame] | 2765 | if (!dwc->usb_psy) |
| 2766 | return -EOPNOTSUPP; |
| 2767 | |
Ray Chi | 8a5b5c3 | 2021-03-28 02:28:08 +0800 | [diff] [blame] | 2768 | val.intval = 1000 * mA; |
Ray Chi | 99288de | 2021-02-22 19:51:49 +0800 | [diff] [blame] | 2769 | ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); |
| 2770 | |
| 2771 | return ret; |
Wesley Cheng | 82c46b8 | 2020-12-29 15:03:29 -0800 | [diff] [blame] | 2772 | } |
| 2773 | |
Wesley Cheng | 9f607a3 | 2021-07-10 02:13:12 -0700 | [diff] [blame^] | 2774 | /** |
| 2775 | * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration |
| 2776 | * @g: pointer to the USB gadget |
| 2777 | * |
| 2778 | * Used to record the maximum number of endpoints being used in a USB composite |
| 2779 | * device. (across all configurations) This is to be used in the calculation |
| 2780 | * of the TXFIFO sizes when resizing internal memory for individual endpoints. |
| 2781 | * It will help ensured that the resizing logic reserves enough space for at |
| 2782 | * least one max packet. |
| 2783 | */ |
| 2784 | static int dwc3_gadget_check_config(struct usb_gadget *g) |
| 2785 | { |
| 2786 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2787 | struct usb_ep *ep; |
| 2788 | int fifo_size = 0; |
| 2789 | int ram1_depth; |
| 2790 | int ep_num = 0; |
| 2791 | |
| 2792 | if (!dwc->do_fifo_resize) |
| 2793 | return 0; |
| 2794 | |
| 2795 | list_for_each_entry(ep, &g->ep_list, ep_list) { |
| 2796 | /* Only interested in the IN endpoints */ |
| 2797 | if (ep->claimed && (ep->address & USB_DIR_IN)) |
| 2798 | ep_num++; |
| 2799 | } |
| 2800 | |
| 2801 | if (ep_num <= dwc->max_cfg_eps) |
| 2802 | return 0; |
| 2803 | |
| 2804 | /* Update the max number of eps in the composition */ |
| 2805 | dwc->max_cfg_eps = ep_num; |
| 2806 | |
| 2807 | fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); |
| 2808 | /* Based on the equation, increment by one for every ep */ |
| 2809 | fifo_size += dwc->max_cfg_eps; |
| 2810 | |
| 2811 | /* Check if we can fit a single fifo per endpoint */ |
| 2812 | ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); |
| 2813 | if (fifo_size > ram1_depth) |
| 2814 | return -ENOMEM; |
| 2815 | |
| 2816 | return 0; |
| 2817 | } |
| 2818 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2819 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 2820 | .get_frame = dwc3_gadget_get_frame, |
| 2821 | .wakeup = dwc3_gadget_wakeup, |
| 2822 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
| 2823 | .pullup = dwc3_gadget_pullup, |
| 2824 | .udc_start = dwc3_gadget_start, |
| 2825 | .udc_stop = dwc3_gadget_stop, |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2826 | .udc_set_speed = dwc3_gadget_set_speed, |
Thinh Nguyen | 072cab8a | 2021-01-19 17:36:28 -0800 | [diff] [blame] | 2827 | .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate, |
Anurag Kumar Vulisha | 729dcff | 2019-05-10 12:37:28 +0530 | [diff] [blame] | 2828 | .get_config_params = dwc3_gadget_config_params, |
Wesley Cheng | 82c46b8 | 2020-12-29 15:03:29 -0800 | [diff] [blame] | 2829 | .vbus_draw = dwc3_gadget_vbus_draw, |
Wesley Cheng | 9f607a3 | 2021-07-10 02:13:12 -0700 | [diff] [blame^] | 2830 | .check_config = dwc3_gadget_check_config, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2831 | }; |
| 2832 | |
| 2833 | /* -------------------------------------------------------------------------- */ |
| 2834 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2835 | static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) |
| 2836 | { |
| 2837 | struct dwc3 *dwc = dep->dwc; |
| 2838 | |
| 2839 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
| 2840 | dep->endpoint.maxburst = 1; |
| 2841 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 2842 | if (!dep->direction) |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2843 | dwc->gadget->ep0 = &dep->endpoint; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2844 | |
| 2845 | dep->endpoint.caps.type_control = true; |
| 2846 | |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) |
| 2851 | { |
| 2852 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | d00be77 | 2021-03-27 17:54:01 -0700 | [diff] [blame] | 2853 | u32 mdwidth; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2854 | int size; |
| 2855 | |
Thinh Nguyen | d00be77 | 2021-03-27 17:54:01 -0700 | [diff] [blame] | 2856 | mdwidth = dwc3_mdwidth(dwc); |
Thinh Nguyen | 4244ba0 | 2020-04-11 19:20:07 -0700 | [diff] [blame] | 2857 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2858 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 2859 | mdwidth /= 8; |
| 2860 | |
| 2861 | size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2862 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | 586f433 | 2020-01-31 16:59:21 -0800 | [diff] [blame] | 2863 | size = DWC3_GTXFIFOSIZ_TXFDEP(size); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2864 | else |
| 2865 | size = DWC31_GTXFIFOSIZ_TXFDEP(size); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2866 | |
| 2867 | /* FIFO Depth is in MDWDITH bytes. Multiply */ |
| 2868 | size *= mdwidth; |
| 2869 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2870 | /* |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2871 | * To meet performance requirement, a minimum TxFIFO size of 3x |
| 2872 | * MaxPacketSize is recommended for endpoints that support burst and a |
| 2873 | * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't |
| 2874 | * support burst. Use those numbers and we can calculate the max packet |
| 2875 | * limit as below. |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2876 | */ |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2877 | if (dwc->maximum_speed >= USB_SPEED_SUPER) |
| 2878 | size /= 3; |
| 2879 | else |
| 2880 | size /= 2; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2881 | |
| 2882 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); |
| 2883 | |
Thinh Nguyen | e0a93d9 | 2020-09-29 15:26:29 -0700 | [diff] [blame] | 2884 | dep->endpoint.max_streams = 16; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2885 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2886 | list_add_tail(&dep->endpoint.ep_list, |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2887 | &dwc->gadget->ep_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2888 | dep->endpoint.caps.type_iso = true; |
| 2889 | dep->endpoint.caps.type_bulk = true; |
| 2890 | dep->endpoint.caps.type_int = true; |
| 2891 | |
| 2892 | return dwc3_alloc_trb_pool(dep); |
| 2893 | } |
| 2894 | |
| 2895 | static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) |
| 2896 | { |
| 2897 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | d00be77 | 2021-03-27 17:54:01 -0700 | [diff] [blame] | 2898 | u32 mdwidth; |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2899 | int size; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2900 | |
Thinh Nguyen | d00be77 | 2021-03-27 17:54:01 -0700 | [diff] [blame] | 2901 | mdwidth = dwc3_mdwidth(dwc); |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2902 | |
| 2903 | /* MDWIDTH is represented in bits, convert to bytes */ |
| 2904 | mdwidth /= 8; |
| 2905 | |
| 2906 | /* All OUT endpoints share a single RxFIFO space */ |
| 2907 | size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2908 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2909 | size = DWC3_GRXFIFOSIZ_RXFDEP(size); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2910 | else |
| 2911 | size = DWC31_GRXFIFOSIZ_RXFDEP(size); |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2912 | |
| 2913 | /* FIFO depth is in MDWDITH bytes */ |
| 2914 | size *= mdwidth; |
| 2915 | |
| 2916 | /* |
| 2917 | * To meet performance requirement, a minimum recommended RxFIFO size |
| 2918 | * is defined as follow: |
| 2919 | * RxFIFO size >= (3 x MaxPacketSize) + |
| 2920 | * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) |
| 2921 | * |
| 2922 | * Then calculate the max packet limit as below. |
| 2923 | */ |
| 2924 | size -= (3 * 8) + 16; |
| 2925 | if (size < 0) |
| 2926 | size = 0; |
| 2927 | else |
| 2928 | size /= 3; |
| 2929 | |
| 2930 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); |
Thinh Nguyen | e0a93d9 | 2020-09-29 15:26:29 -0700 | [diff] [blame] | 2931 | dep->endpoint.max_streams = 16; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2932 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2933 | list_add_tail(&dep->endpoint.ep_list, |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2934 | &dwc->gadget->ep_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2935 | dep->endpoint.caps.type_iso = true; |
| 2936 | dep->endpoint.caps.type_bulk = true; |
| 2937 | dep->endpoint.caps.type_int = true; |
| 2938 | |
| 2939 | return dwc3_alloc_trb_pool(dep); |
| 2940 | } |
| 2941 | |
| 2942 | static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2943 | { |
| 2944 | struct dwc3_ep *dep; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2945 | bool direction = epnum & 1; |
| 2946 | int ret; |
| 2947 | u8 num = epnum >> 1; |
| 2948 | |
| 2949 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 2950 | if (!dep) |
| 2951 | return -ENOMEM; |
| 2952 | |
| 2953 | dep->dwc = dwc; |
| 2954 | dep->number = epnum; |
| 2955 | dep->direction = direction; |
| 2956 | dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); |
| 2957 | dwc->eps[epnum] = dep; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 2958 | dep->combo_num = 0; |
| 2959 | dep->start_cmd_status = 0; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2960 | |
| 2961 | snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, |
| 2962 | direction ? "in" : "out"); |
| 2963 | |
| 2964 | dep->endpoint.name = dep->name; |
| 2965 | |
| 2966 | if (!(dep->number > 1)) { |
| 2967 | dep->endpoint.desc = &dwc3_gadget_ep0_desc; |
| 2968 | dep->endpoint.comp_desc = NULL; |
| 2969 | } |
| 2970 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2971 | if (num == 0) |
| 2972 | ret = dwc3_gadget_init_control_endpoint(dep); |
| 2973 | else if (direction) |
| 2974 | ret = dwc3_gadget_init_in_endpoint(dep); |
| 2975 | else |
| 2976 | ret = dwc3_gadget_init_out_endpoint(dep); |
| 2977 | |
| 2978 | if (ret) |
| 2979 | return ret; |
| 2980 | |
| 2981 | dep->endpoint.caps.dir_in = direction; |
| 2982 | dep->endpoint.caps.dir_out = !direction; |
| 2983 | |
| 2984 | INIT_LIST_HEAD(&dep->pending_list); |
| 2985 | INIT_LIST_HEAD(&dep->started_list); |
Felipe Balbi | d5443bb | 2018-08-01 13:53:29 +0300 | [diff] [blame] | 2986 | INIT_LIST_HEAD(&dep->cancelled_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2987 | |
Jack Pham | 5ff90af | 2021-05-29 12:29:32 -0700 | [diff] [blame] | 2988 | dwc3_debugfs_create_endpoint_dir(dep); |
| 2989 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2990 | return 0; |
| 2991 | } |
| 2992 | |
| 2993 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) |
| 2994 | { |
Bryan O'Donoghue | 47d3946 | 2017-01-31 20:58:10 +0000 | [diff] [blame] | 2995 | u8 epnum; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2996 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2997 | INIT_LIST_HEAD(&dwc->gadget->ep_list); |
Bryan O'Donoghue | f3bcfc7 | 2017-01-31 20:58:11 +0000 | [diff] [blame] | 2998 | |
Andy Shevchenko | 46b780d | 2017-06-12 15:11:25 +0300 | [diff] [blame] | 2999 | for (epnum = 0; epnum < total; epnum++) { |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 3000 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3001 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 3002 | ret = dwc3_gadget_init_endpoint(dwc, epnum); |
| 3003 | if (ret) |
| 3004 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
| 3010 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 3011 | { |
| 3012 | struct dwc3_ep *dep; |
| 3013 | u8 epnum; |
| 3014 | |
| 3015 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 3016 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 3017 | if (!dep) |
| 3018 | continue; |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 3019 | /* |
| 3020 | * Physical endpoints 0 and 1 are special; they form the |
| 3021 | * bi-directional USB endpoint 0. |
| 3022 | * |
| 3023 | * For those two physical endpoints, we don't allocate a TRB |
| 3024 | * pool nor do we add them the endpoints list. Due to that, we |
| 3025 | * shouldn't do these two operations otherwise we would end up |
| 3026 | * with all sorts of bugs when removing dwc3.ko. |
| 3027 | */ |
| 3028 | if (epnum != 0 && epnum != 1) { |
| 3029 | dwc3_free_trb_pool(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3030 | list_del(&dep->endpoint.ep_list); |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 3031 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3032 | |
Greg Kroah-Hartman | 8562d5b | 2021-06-09 11:39:24 +0200 | [diff] [blame] | 3033 | debugfs_remove_recursive(debugfs_lookup(dep->name, |
| 3034 | debugfs_lookup(dev_name(dep->dwc->dev), |
| 3035 | usb_debug_root))); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3036 | kfree(dep); |
| 3037 | } |
| 3038 | } |
| 3039 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3040 | /* -------------------------------------------------------------------------- */ |
Felipe Balbi | e5caff6 | 2013-02-26 15:11:05 +0200 | [diff] [blame] | 3041 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3042 | static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, |
| 3043 | struct dwc3_request *req, struct dwc3_trb *trb, |
| 3044 | const struct dwc3_event_depevt *event, int status, int chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3045 | { |
| 3046 | unsigned int count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3047 | |
Felipe Balbi | dc55c67 | 2016-08-12 13:20:32 +0300 | [diff] [blame] | 3048 | dwc3_ep_inc_deq(dep); |
Felipe Balbi | a9c3ca5 | 2016-10-05 14:24:37 +0300 | [diff] [blame] | 3049 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 3050 | trace_dwc3_complete_trb(dep, trb); |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 3051 | req->num_trbs--; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 3052 | |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 3053 | /* |
| 3054 | * If we're in the middle of series of chained TRBs and we |
| 3055 | * receive a short transfer along the way, DWC3 will skip |
| 3056 | * through all TRBs including the last TRB in the chain (the |
| 3057 | * where CHN bit is zero. DWC3 will also avoid clearing HWO |
| 3058 | * bit and SW has to do it manually. |
| 3059 | * |
| 3060 | * We're going to do that here to avoid problems of HW trying |
| 3061 | * to use bogus TRBs for transfers. |
| 3062 | */ |
| 3063 | if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) |
| 3064 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 3065 | |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 3066 | /* |
Thinh Nguyen | 6abfa0f | 2018-11-15 19:03:27 -0800 | [diff] [blame] | 3067 | * For isochronous transfers, the first TRB in a service interval must |
| 3068 | * have the Isoc-First type. Track and report its interval frame number. |
| 3069 | */ |
| 3070 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 3071 | (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { |
| 3072 | unsigned int frame_number; |
| 3073 | |
| 3074 | frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); |
| 3075 | frame_number &= ~(dep->interval - 1); |
| 3076 | req->request.frame_number = frame_number; |
| 3077 | } |
| 3078 | |
| 3079 | /* |
Thinh Nguyen | a2841f4 | 2020-09-24 01:21:36 -0700 | [diff] [blame] | 3080 | * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If |
| 3081 | * this TRB points to the bounce buffer address, it's a MPS alignment |
| 3082 | * TRB. Don't add it to req->remaining calculation. |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 3083 | */ |
Thinh Nguyen | a2841f4 | 2020-09-24 01:21:36 -0700 | [diff] [blame] | 3084 | if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && |
| 3085 | trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 3086 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 3087 | return 1; |
| 3088 | } |
| 3089 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3090 | count = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | e62c5bc5 | 2016-10-25 13:47:21 +0300 | [diff] [blame] | 3091 | req->remaining += count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3092 | |
Felipe Balbi | 35b2719 | 2017-03-08 13:56:37 +0200 | [diff] [blame] | 3093 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
| 3094 | return 1; |
| 3095 | |
Felipe Balbi | d80fe1b | 2018-04-06 11:04:21 +0300 | [diff] [blame] | 3096 | if (event->status & DEPEVT_STATUS_SHORT && !chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3097 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 3098 | |
Anurag Kumar Vulisha | 5ee8589 | 2020-01-27 19:30:46 +0000 | [diff] [blame] | 3099 | if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || |
| 3100 | (trb->ctrl & DWC3_TRB_CTRL_LST)) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3101 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 3102 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 3103 | return 0; |
| 3104 | } |
| 3105 | |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 3106 | static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, |
| 3107 | struct dwc3_request *req, const struct dwc3_event_depevt *event, |
| 3108 | int status) |
| 3109 | { |
| 3110 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; |
| 3111 | struct scatterlist *sg = req->sg; |
| 3112 | struct scatterlist *s; |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 3113 | unsigned int num_queued = req->num_queued_sgs; |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 3114 | unsigned int i; |
| 3115 | int ret = 0; |
| 3116 | |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 3117 | for_each_sg(sg, s, num_queued, i) { |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 3118 | trb = &dep->trb_pool[dep->trb_dequeue]; |
| 3119 | |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 3120 | req->sg = sg_next(s); |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 3121 | req->num_queued_sgs--; |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 3122 | |
| 3123 | ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, |
| 3124 | trb, event, status, true); |
| 3125 | if (ret) |
| 3126 | break; |
| 3127 | } |
| 3128 | |
| 3129 | return ret; |
| 3130 | } |
| 3131 | |
| 3132 | static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, |
| 3133 | struct dwc3_request *req, const struct dwc3_event_depevt *event, |
| 3134 | int status) |
| 3135 | { |
| 3136 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; |
| 3137 | |
| 3138 | return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, |
| 3139 | event, status, false); |
| 3140 | } |
| 3141 | |
Felipe Balbi | e0c42ce | 2018-04-06 15:37:30 +0300 | [diff] [blame] | 3142 | static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) |
| 3143 | { |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 3144 | return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; |
Felipe Balbi | e0c42ce | 2018-04-06 15:37:30 +0300 | [diff] [blame] | 3145 | } |
| 3146 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 3147 | static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, |
| 3148 | const struct dwc3_event_depevt *event, |
| 3149 | struct dwc3_request *req, int status) |
| 3150 | { |
| 3151 | int ret; |
| 3152 | |
Thinh Nguyen | 25dda9f | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 3153 | if (req->request.num_mapped_sgs) |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 3154 | ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, |
| 3155 | status); |
| 3156 | else |
| 3157 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
| 3158 | status); |
| 3159 | |
Thinh Nguyen | 690e5c2 | 2020-09-24 01:21:24 -0700 | [diff] [blame] | 3160 | req->request.actual = req->request.length - req->remaining; |
| 3161 | |
| 3162 | if (!dwc3_gadget_ep_request_completed(req)) |
| 3163 | goto out; |
| 3164 | |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 3165 | if (req->needs_extra_trb) { |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 3166 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
| 3167 | status); |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 3168 | req->needs_extra_trb = false; |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 3169 | } |
| 3170 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 3171 | dwc3_gadget_giveback(dep, req, status); |
| 3172 | |
| 3173 | out: |
| 3174 | return ret; |
| 3175 | } |
| 3176 | |
Felipe Balbi | 12a3a4a | 2018-03-29 11:53:40 +0300 | [diff] [blame] | 3177 | static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3178 | const struct dwc3_event_depevt *event, int status) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3179 | { |
Felipe Balbi | 6afbdb5 | 2018-04-06 15:49:49 +0300 | [diff] [blame] | 3180 | struct dwc3_request *req; |
| 3181 | struct dwc3_request *tmp; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3182 | |
Felipe Balbi | 6afbdb5 | 2018-04-06 15:49:49 +0300 | [diff] [blame] | 3183 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) { |
Felipe Balbi | fee73e6 | 2018-04-06 15:50:29 +0300 | [diff] [blame] | 3184 | int ret; |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 3185 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 3186 | ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, |
| 3187 | req, status); |
Felipe Balbi | 58f0218 | 2018-03-29 12:10:31 +0300 | [diff] [blame] | 3188 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3189 | break; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 3190 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3191 | } |
| 3192 | |
Thinh Nguyen | d9feef9 | 2020-03-31 01:40:42 -0700 | [diff] [blame] | 3193 | static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep) |
| 3194 | { |
| 3195 | struct dwc3_request *req; |
Wesley Cheng | 02fa4b9 | 2021-03-19 02:31:24 -0700 | [diff] [blame] | 3196 | struct dwc3 *dwc = dep->dwc; |
| 3197 | |
| 3198 | if (!dep->endpoint.desc || !dwc->pullups_connected || |
| 3199 | !dwc->connected) |
| 3200 | return false; |
Thinh Nguyen | d9feef9 | 2020-03-31 01:40:42 -0700 | [diff] [blame] | 3201 | |
| 3202 | if (!list_empty(&dep->pending_list)) |
| 3203 | return true; |
| 3204 | |
| 3205 | /* |
| 3206 | * We only need to check the first entry of the started list. We can |
| 3207 | * assume the completed requests are removed from the started list. |
| 3208 | */ |
| 3209 | req = next_request(&dep->started_list); |
| 3210 | if (!req) |
| 3211 | return false; |
| 3212 | |
| 3213 | return !dwc3_gadget_ep_request_completed(req); |
| 3214 | } |
| 3215 | |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 3216 | static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, |
| 3217 | const struct dwc3_event_depevt *event) |
| 3218 | { |
Felipe Balbi | f62afb4 | 2018-04-11 10:34:34 +0300 | [diff] [blame] | 3219 | dep->frame_number = event->parameters; |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 3220 | } |
| 3221 | |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 3222 | static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, |
| 3223 | const struct dwc3_event_depevt *event, int status) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3224 | { |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3225 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 3226 | bool no_started_trb = true; |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 3227 | |
Felipe Balbi | 5f2e797 | 2018-03-29 11:10:45 +0300 | [diff] [blame] | 3228 | dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3229 | |
Thinh Nguyen | b6842d4 | 2020-05-05 19:46:33 -0700 | [diff] [blame] | 3230 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) |
| 3231 | goto out; |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 3232 | |
Michael Grzeschik | f5e46aa | 2020-07-01 20:24:53 +0200 | [diff] [blame] | 3233 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 3234 | list_empty(&dep->started_list) && |
| 3235 | (list_empty(&dep->pending_list) || status == -EXDEV)) |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3236 | dwc3_stop_active_transfer(dep, true, true); |
Thinh Nguyen | d9feef9 | 2020-03-31 01:40:42 -0700 | [diff] [blame] | 3237 | else if (dwc3_gadget_ep_should_continue(dep)) |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 3238 | if (__dwc3_gadget_kick_transfer(dep) == 0) |
| 3239 | no_started_trb = false; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3240 | |
Thinh Nguyen | b6842d4 | 2020-05-05 19:46:33 -0700 | [diff] [blame] | 3241 | out: |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3242 | /* |
| 3243 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 3244 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 3245 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3246 | if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3247 | u32 reg; |
| 3248 | int i; |
| 3249 | |
| 3250 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
Moiz Sonasath | 348e026 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 3251 | dep = dwc->eps[i]; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3252 | |
| 3253 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 3254 | continue; |
| 3255 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 3256 | if (!list_empty(&dep->started_list)) |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 3257 | return no_started_trb; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3261 | reg |= dwc->u1u2; |
| 3262 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 3263 | |
| 3264 | dwc->u1u2 = 0; |
| 3265 | } |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 3266 | |
| 3267 | return no_started_trb; |
| 3268 | } |
| 3269 | |
| 3270 | static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, |
| 3271 | const struct dwc3_event_depevt *event) |
| 3272 | { |
| 3273 | int status = 0; |
| 3274 | |
| 3275 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 3276 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
| 3277 | |
| 3278 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 3279 | status = -ECONNRESET; |
| 3280 | |
| 3281 | if (event->status & DEPEVT_STATUS_MISSED_ISOC) |
| 3282 | status = -EXDEV; |
| 3283 | |
| 3284 | dwc3_gadget_endpoint_trbs_complete(dep, event, status); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3285 | } |
| 3286 | |
Thinh Nguyen | 3eaecd0 | 2020-05-05 19:46:51 -0700 | [diff] [blame] | 3287 | static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep, |
| 3288 | const struct dwc3_event_depevt *event) |
| 3289 | { |
| 3290 | int status = 0; |
| 3291 | |
| 3292 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
| 3293 | |
| 3294 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 3295 | status = -ECONNRESET; |
| 3296 | |
Thinh Nguyen | e0d1956 | 2020-05-05 19:46:57 -0700 | [diff] [blame] | 3297 | if (dwc3_gadget_endpoint_trbs_complete(dep, event, status)) |
| 3298 | dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3299 | } |
| 3300 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3301 | static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, |
| 3302 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 3203386 | 2018-03-27 10:47:48 +0300 | [diff] [blame] | 3303 | { |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 3304 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
Thinh Nguyen | 36f05d3 | 2020-03-29 16:13:10 -0700 | [diff] [blame] | 3305 | |
| 3306 | /* |
| 3307 | * The XferNotReady event is generated only once before the endpoint |
| 3308 | * starts. It will be generated again when END_TRANSFER command is |
| 3309 | * issued. For some controller versions, the XferNotReady event may be |
| 3310 | * generated while the END_TRANSFER command is still in process. Ignore |
| 3311 | * it and wait for the next XferNotReady event after the command is |
| 3312 | * completed. |
| 3313 | */ |
| 3314 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) |
| 3315 | return; |
| 3316 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 3317 | (void) __dwc3_gadget_start_isoc(dep); |
Felipe Balbi | 3203386 | 2018-03-27 10:47:48 +0300 | [diff] [blame] | 3318 | } |
| 3319 | |
Thinh Nguyen | 8266b08 | 2020-07-30 16:29:03 -0700 | [diff] [blame] | 3320 | static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep, |
| 3321 | const struct dwc3_event_depevt *event) |
| 3322 | { |
| 3323 | u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); |
| 3324 | |
| 3325 | if (cmd != DWC3_DEPCMD_ENDTRANSFER) |
| 3326 | return; |
| 3327 | |
| 3328 | dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; |
| 3329 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
| 3330 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
| 3331 | |
| 3332 | if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { |
| 3333 | struct dwc3 *dwc = dep->dwc; |
| 3334 | |
| 3335 | dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; |
| 3336 | if (dwc3_send_clear_stall_ep_cmd(dep)) { |
| 3337 | struct usb_ep *ep0 = &dwc->eps[0]->endpoint; |
| 3338 | |
| 3339 | dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); |
| 3340 | if (dwc->delayed_status) |
| 3341 | __dwc3_gadget_ep0_set_halt(ep0, 1); |
| 3342 | return; |
| 3343 | } |
| 3344 | |
| 3345 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 3346 | if (dwc->delayed_status) |
| 3347 | dwc3_ep0_send_delayed_status(dwc); |
| 3348 | } |
| 3349 | |
| 3350 | if ((dep->flags & DWC3_EP_DELAY_START) && |
| 3351 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 3352 | __dwc3_gadget_kick_transfer(dep); |
| 3353 | |
| 3354 | dep->flags &= ~DWC3_EP_DELAY_START; |
| 3355 | } |
| 3356 | |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3357 | static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep, |
| 3358 | const struct dwc3_event_depevt *event) |
| 3359 | { |
| 3360 | struct dwc3 *dwc = dep->dwc; |
| 3361 | |
| 3362 | if (event->status == DEPEVT_STREAMEVT_FOUND) { |
| 3363 | dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; |
| 3364 | goto out; |
| 3365 | } |
| 3366 | |
| 3367 | /* Note: NoStream rejection event param value is 0 and not 0xFFFF */ |
| 3368 | switch (event->parameters) { |
| 3369 | case DEPEVT_STREAM_PRIME: |
| 3370 | /* |
| 3371 | * If the host can properly transition the endpoint state from |
| 3372 | * idle to prime after a NoStream rejection, there's no need to |
| 3373 | * force restarting the endpoint to reinitiate the stream. To |
| 3374 | * simplify the check, assume the host follows the USB spec if |
| 3375 | * it primed the endpoint more than once. |
| 3376 | */ |
| 3377 | if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) { |
| 3378 | if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED) |
| 3379 | dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM; |
| 3380 | else |
| 3381 | dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; |
| 3382 | } |
| 3383 | |
| 3384 | break; |
| 3385 | case DEPEVT_STREAM_NOSTREAM: |
| 3386 | if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || |
| 3387 | !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) || |
| 3388 | !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)) |
| 3389 | break; |
| 3390 | |
| 3391 | /* |
| 3392 | * If the host rejects a stream due to no active stream, by the |
| 3393 | * USB and xHCI spec, the endpoint will be put back to idle |
| 3394 | * state. When the host is ready (buffer added/updated), it will |
| 3395 | * prime the endpoint to inform the usb device controller. This |
| 3396 | * triggers the device controller to issue ERDY to restart the |
| 3397 | * stream. However, some hosts don't follow this and keep the |
| 3398 | * endpoint in the idle state. No prime will come despite host |
| 3399 | * streams are updated, and the device controller will not be |
| 3400 | * triggered to generate ERDY to move the next stream data. To |
| 3401 | * workaround this and maintain compatibility with various |
| 3402 | * hosts, force to reinitate the stream until the host is ready |
| 3403 | * instead of waiting for the host to prime the endpoint. |
| 3404 | */ |
Thinh Nguyen | b10e1c2 | 2020-05-05 19:47:15 -0700 | [diff] [blame] | 3405 | if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) { |
| 3406 | unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME; |
| 3407 | |
| 3408 | dwc3_send_gadget_generic_command(dwc, cmd, dep->number); |
| 3409 | } else { |
| 3410 | dep->flags |= DWC3_EP_DELAY_START; |
| 3411 | dwc3_stop_active_transfer(dep, true, true); |
| 3412 | return; |
| 3413 | } |
| 3414 | break; |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | out: |
| 3418 | dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; |
| 3419 | } |
| 3420 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3421 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 3422 | const struct dwc3_event_depevt *event) |
| 3423 | { |
| 3424 | struct dwc3_ep *dep; |
| 3425 | u8 epnum = event->endpoint_number; |
| 3426 | |
| 3427 | dep = dwc->eps[epnum]; |
| 3428 | |
Janusz Dziedzic | d7fd41c | 2016-12-08 10:57:34 +0100 | [diff] [blame] | 3429 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | 3aec991 | 2019-01-21 13:08:44 +0200 | [diff] [blame] | 3430 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Janusz Dziedzic | d7fd41c | 2016-12-08 10:57:34 +0100 | [diff] [blame] | 3431 | return; |
| 3432 | |
| 3433 | /* Handle only EPCMDCMPLT when EP disabled */ |
| 3434 | if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) |
| 3435 | return; |
| 3436 | } |
Felipe Balbi | 3336abb | 2012-06-06 09:19:35 +0300 | [diff] [blame] | 3437 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3438 | if (epnum == 0 || epnum == 1) { |
| 3439 | dwc3_ep0_interrupt(dwc, event); |
| 3440 | return; |
| 3441 | } |
| 3442 | |
| 3443 | switch (event->endpoint_event) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3444 | case DWC3_DEPEVT_XFERINPROGRESS: |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3445 | dwc3_gadget_endpoint_transfer_in_progress(dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3446 | break; |
| 3447 | case DWC3_DEPEVT_XFERNOTREADY: |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3448 | dwc3_gadget_endpoint_transfer_not_ready(dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3449 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3450 | case DWC3_DEPEVT_EPCMDCMPLT: |
Thinh Nguyen | 8266b08 | 2020-07-30 16:29:03 -0700 | [diff] [blame] | 3451 | dwc3_gadget_endpoint_command_complete(dep, event); |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 3452 | break; |
Felipe Balbi | 742a4ff | 2018-03-26 13:26:56 +0300 | [diff] [blame] | 3453 | case DWC3_DEPEVT_XFERCOMPLETE: |
Thinh Nguyen | 3eaecd0 | 2020-05-05 19:46:51 -0700 | [diff] [blame] | 3454 | dwc3_gadget_endpoint_transfer_complete(dep, event); |
| 3455 | break; |
| 3456 | case DWC3_DEPEVT_STREAMEVT: |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3457 | dwc3_gadget_endpoint_stream_event(dep, event); |
| 3458 | break; |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 3459 | case DWC3_DEPEVT_RXTXFIFOEVT: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3460 | break; |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 3465 | { |
| 3466 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 3467 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3468 | dwc->gadget_driver->disconnect(dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3469 | spin_lock(&dwc->lock); |
| 3470 | } |
| 3471 | } |
| 3472 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3473 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
| 3474 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 3475 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3476 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3477 | dwc->gadget_driver->suspend(dwc->gadget); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3478 | spin_lock(&dwc->lock); |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | static void dwc3_resume_gadget(struct dwc3 *dwc) |
| 3483 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 3484 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3485 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3486 | dwc->gadget_driver->resume(dwc->gadget); |
Felipe Balbi | 5c7b3b0 | 2015-01-29 10:29:18 -0600 | [diff] [blame] | 3487 | spin_lock(&dwc->lock); |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | static void dwc3_reset_gadget(struct dwc3 *dwc) |
| 3492 | { |
| 3493 | if (!dwc->gadget_driver) |
| 3494 | return; |
| 3495 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3496 | if (dwc->gadget->speed != USB_SPEED_UNKNOWN) { |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 3497 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3498 | usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3499 | spin_lock(&dwc->lock); |
| 3500 | } |
| 3501 | } |
| 3502 | |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 3503 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, |
| 3504 | bool interrupt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3505 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3506 | struct dwc3_gadget_ep_cmd_params params; |
| 3507 | u32 cmd; |
| 3508 | int ret; |
| 3509 | |
Thinh Nguyen | c58d8bf | 2019-12-18 18:14:44 -0800 | [diff] [blame] | 3510 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || |
| 3511 | (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3512 | return; |
| 3513 | |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 3514 | /* |
| 3515 | * NOTICE: We are violating what the Databook says about the |
| 3516 | * EndTransfer command. Ideally we would _always_ wait for the |
| 3517 | * EndTransfer Command Completion IRQ, but that's causing too |
| 3518 | * much trouble synchronizing between us and gadget driver. |
| 3519 | * |
| 3520 | * We have discussed this with the IP Provider and it was |
Thinh Nguyen | cf2f8b6 | 2019-12-18 18:14:56 -0800 | [diff] [blame] | 3521 | * suggested to giveback all requests here. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 3522 | * |
| 3523 | * Note also that a similar handling was tested by Synopsys |
| 3524 | * (thanks a lot Paul) and nothing bad has come out of it. |
Thinh Nguyen | cf2f8b6 | 2019-12-18 18:14:56 -0800 | [diff] [blame] | 3525 | * In short, what we're doing is issuing EndTransfer with |
| 3526 | * CMDIOC bit set and delay kicking transfer until the |
| 3527 | * EndTransfer command had completed. |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 3528 | * |
| 3529 | * As of IP version 3.10a of the DWC_usb3 IP, the controller |
| 3530 | * supports a mode to work around the above limitation. The |
| 3531 | * software can poll the CMDACT bit in the DEPCMD register |
| 3532 | * after issuing a EndTransfer command. This mode is enabled |
| 3533 | * by writing GUCTL2[14]. This polling is already done in the |
| 3534 | * dwc3_send_gadget_ep_cmd() function so if the mode is |
| 3535 | * enabled, the EndTransfer command will have completed upon |
Thinh Nguyen | cf2f8b6 | 2019-12-18 18:14:56 -0800 | [diff] [blame] | 3536 | * returning from this function. |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 3537 | * |
| 3538 | * This mode is NOT available on the DWC_usb31 IP. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 3539 | */ |
| 3540 | |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3541 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 3542 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 3543 | cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 3544 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3545 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 3546 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3547 | WARN_ON_ONCE(ret); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 3548 | dep->resource_index = 0; |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 3549 | |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3550 | /* |
| 3551 | * The END_TRANSFER command will cause the controller to generate a |
| 3552 | * NoStream Event, and it's not due to the host DP NoStream rejection. |
| 3553 | * Ignore the next NoStream event. |
| 3554 | */ |
| 3555 | if (dep->stream_capable) |
| 3556 | dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; |
| 3557 | |
Thinh Nguyen | d3abda5 | 2019-11-27 13:10:47 -0800 | [diff] [blame] | 3558 | if (!interrupt) |
| 3559 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
Thinh Nguyen | c58d8bf | 2019-12-18 18:14:44 -0800 | [diff] [blame] | 3560 | else |
| 3561 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3562 | } |
| 3563 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3564 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 3565 | { |
| 3566 | u32 epnum; |
| 3567 | |
| 3568 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 3569 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3570 | int ret; |
| 3571 | |
| 3572 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 3573 | if (!dep) |
| 3574 | continue; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3575 | |
| 3576 | if (!(dep->flags & DWC3_EP_STALL)) |
| 3577 | continue; |
| 3578 | |
| 3579 | dep->flags &= ~DWC3_EP_STALL; |
| 3580 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 3581 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3582 | WARN_ON_ONCE(ret); |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 3587 | { |
Felipe Balbi | c4430a2 | 2012-05-24 10:30:01 +0300 | [diff] [blame] | 3588 | int reg; |
| 3589 | |
Thinh Nguyen | 1b6009ea | 2019-10-23 19:15:49 -0700 | [diff] [blame] | 3590 | dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET); |
| 3591 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3592 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3593 | reg &= ~DWC3_DCTL_INITU1ENA; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3594 | reg &= ~DWC3_DCTL_INITU2ENA; |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3595 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3596 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3597 | dwc3_disconnect_gadget(dwc); |
| 3598 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3599 | dwc->gadget->speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3600 | dwc->setup_packet_pending = false; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3601 | usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3602 | |
| 3603 | dwc->connected = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3604 | } |
| 3605 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3606 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 3607 | { |
| 3608 | u32 reg; |
| 3609 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3610 | /* |
Wesley Cheng | 71ca43f | 2021-03-19 02:31:25 -0700 | [diff] [blame] | 3611 | * Ideally, dwc3_reset_gadget() would trigger the function |
| 3612 | * drivers to stop any active transfers through ep disable. |
| 3613 | * However, for functions which defer ep disable, such as mass |
| 3614 | * storage, we will need to rely on the call to stop active |
| 3615 | * transfers here, and avoid allowing of request queuing. |
| 3616 | */ |
| 3617 | dwc->connected = false; |
| 3618 | |
| 3619 | /* |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3620 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 3621 | * would cause a missing Disconnect Event if there's a |
| 3622 | * pending Setup Packet in the FIFO. |
| 3623 | * |
| 3624 | * There's no suggested workaround on the official Bug |
| 3625 | * report, which states that "unless the driver/application |
| 3626 | * is doing any special handling of a disconnect event, |
| 3627 | * there is no functional issue". |
| 3628 | * |
| 3629 | * Unfortunately, it turns out that we _do_ some special |
| 3630 | * handling of a disconnect event, namely complete all |
| 3631 | * pending transfers, notify gadget driver of the |
| 3632 | * disconnection, and so on. |
| 3633 | * |
| 3634 | * Our suggested workaround is to follow the Disconnect |
| 3635 | * Event steps here, instead, based on a setup_packet_pending |
Felipe Balbi | b5d335e | 2015-11-16 16:20:34 -0600 | [diff] [blame] | 3636 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
| 3637 | * status for EP0 TRBs and gets cleared on XferComplete for the |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3638 | * same endpoint. |
| 3639 | * |
| 3640 | * Refers to: |
| 3641 | * |
| 3642 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 3643 | * generated if setup packet pending in FIFO |
| 3644 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3645 | if (DWC3_VER_IS_PRIOR(DWC3, 188A)) { |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3646 | if (dwc->setup_packet_pending) |
| 3647 | dwc3_gadget_disconnect_interrupt(dwc); |
| 3648 | } |
| 3649 | |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 3650 | dwc3_reset_gadget(dwc); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 3651 | /* |
| 3652 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a |
| 3653 | * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW |
| 3654 | * needs to ensure that it sends "a DEPENDXFER command for any active |
| 3655 | * transfers." |
| 3656 | */ |
| 3657 | dwc3_stop_active_transfers(dwc); |
Wesley Cheng | f09ddcf | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 3658 | dwc->connected = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3659 | |
| 3660 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3661 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3662 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 3663 | dwc->test_mode = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3664 | dwc3_clear_stall_all_ep(dwc); |
| 3665 | |
| 3666 | /* Reset device address to zero */ |
| 3667 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 3668 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 3669 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3670 | } |
| 3671 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3672 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 3673 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3674 | struct dwc3_ep *dep; |
| 3675 | int ret; |
| 3676 | u32 reg; |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 3677 | u8 lanes = 1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3678 | u8 speed; |
| 3679 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3680 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 3681 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 3682 | dwc->speed = speed; |
| 3683 | |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 3684 | if (DWC3_IP_IS(DWC32)) |
| 3685 | lanes = DWC3_DSTS_CONNLANES(reg) + 1; |
| 3686 | |
| 3687 | dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; |
| 3688 | |
John Youn | 5fb6fda | 2016-11-10 17:23:25 -0800 | [diff] [blame] | 3689 | /* |
| 3690 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 3691 | * each time on Connect Done. |
| 3692 | * |
| 3693 | * Currently we always use the reset value. If any platform |
| 3694 | * wants to set this to a different value, we need to add a |
| 3695 | * setting and update GCTL.RAMCLKSEL here. |
| 3696 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3697 | |
| 3698 | switch (speed) { |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3699 | case DWC3_DSTS_SUPERSPEED_PLUS: |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 3700 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3701 | dwc->gadget->ep0->maxpacket = 512; |
| 3702 | dwc->gadget->speed = USB_SPEED_SUPER_PLUS; |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 3703 | |
| 3704 | if (lanes > 1) |
| 3705 | dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; |
| 3706 | else |
| 3707 | dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 3708 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3709 | case DWC3_DSTS_SUPERSPEED: |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 3710 | /* |
| 3711 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 3712 | * would cause a missing USB3 Reset event. |
| 3713 | * |
| 3714 | * In such situations, we should force a USB3 Reset |
| 3715 | * event by calling our dwc3_gadget_reset_interrupt() |
| 3716 | * routine. |
| 3717 | * |
| 3718 | * Refers to: |
| 3719 | * |
| 3720 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 3721 | * not be generated always when the link enters poll |
| 3722 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3723 | if (DWC3_VER_IS_PRIOR(DWC3, 190A)) |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 3724 | dwc3_gadget_reset_interrupt(dwc); |
| 3725 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3726 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3727 | dwc->gadget->ep0->maxpacket = 512; |
| 3728 | dwc->gadget->speed = USB_SPEED_SUPER; |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 3729 | |
| 3730 | if (lanes > 1) { |
| 3731 | dwc->gadget->speed = USB_SPEED_SUPER_PLUS; |
| 3732 | dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; |
| 3733 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3734 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3735 | case DWC3_DSTS_HIGHSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3736 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3737 | dwc->gadget->ep0->maxpacket = 64; |
| 3738 | dwc->gadget->speed = USB_SPEED_HIGH; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3739 | break; |
Roger Quadros | 9418ee1 | 2017-01-03 14:32:09 +0200 | [diff] [blame] | 3740 | case DWC3_DSTS_FULLSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3741 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3742 | dwc->gadget->ep0->maxpacket = 64; |
| 3743 | dwc->gadget->speed = USB_SPEED_FULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3744 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3745 | } |
| 3746 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3747 | dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; |
Thinh Nguyen | 6180026 | 2018-01-12 18:18:05 -0800 | [diff] [blame] | 3748 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3749 | /* Enable USB2 LPM Capability */ |
| 3750 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3751 | if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) && |
Thinh Nguyen | 475e8be | 2021-04-13 19:13:18 -0700 | [diff] [blame] | 3752 | !dwc->usb2_gadget_lpm_disable && |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3753 | (speed != DWC3_DSTS_SUPERSPEED) && |
| 3754 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3755 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 3756 | reg |= DWC3_DCFG_LPM_CAP; |
| 3757 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 3758 | |
| 3759 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3760 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 3761 | |
Thinh Nguyen | 16fe4f3 | 2019-08-19 18:35:58 -0700 | [diff] [blame] | 3762 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | |
| 3763 | (dwc->is_utmi_l1_suspend << 4)); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3764 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 3765 | /* |
| 3766 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and |
| 3767 | * DCFG.LPMCap is set, core responses with an ACK and the |
| 3768 | * BESL value in the LPM token is less than or equal to LPM |
| 3769 | * NYET threshold. |
| 3770 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3771 | WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, |
Masanari Iida | 9165dab | 2016-09-17 23:44:17 +0900 | [diff] [blame] | 3772 | "LPM Erratum not available on dwc3 revisions < 2.40a\n"); |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 3773 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3774 | if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) |
Thinh Nguyen | 2e487d2 | 2019-04-25 13:55:30 -0700 | [diff] [blame] | 3775 | reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 3776 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3777 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 3778 | } else { |
Thinh Nguyen | 475e8be | 2021-04-13 19:13:18 -0700 | [diff] [blame] | 3779 | if (dwc->usb2_gadget_lpm_disable) { |
| 3780 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 3781 | reg &= ~DWC3_DCFG_LPM_CAP; |
| 3782 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 3783 | } |
| 3784 | |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 3785 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3786 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3787 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3788 | } |
| 3789 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3790 | dep = dwc->eps[0]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 3791 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3792 | if (ret) { |
| 3793 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 3794 | return; |
| 3795 | } |
| 3796 | |
| 3797 | dep = dwc->eps[1]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 3798 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3799 | if (ret) { |
| 3800 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 3801 | return; |
| 3802 | } |
| 3803 | |
| 3804 | /* |
| 3805 | * Configure PHY via GUSB3PIPECTLn if required. |
| 3806 | * |
| 3807 | * Update GTXFIFOSIZn |
| 3808 | * |
| 3809 | * In both cases reset values should be sufficient. |
| 3810 | */ |
| 3811 | } |
| 3812 | |
| 3813 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) |
| 3814 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3815 | /* |
| 3816 | * TODO take core out of low power mode when that's |
| 3817 | * implemented. |
| 3818 | */ |
| 3819 | |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 3820 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
| 3821 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3822 | dwc->gadget_driver->resume(dwc->gadget); |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 3823 | spin_lock(&dwc->lock); |
| 3824 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 3828 | unsigned int evtinfo) |
| 3829 | { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3830 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3831 | unsigned int pwropt; |
| 3832 | |
| 3833 | /* |
| 3834 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without |
| 3835 | * Hibernation mode enabled which would show up when device detects |
| 3836 | * host-initiated U3 exit. |
| 3837 | * |
| 3838 | * In that case, device will generate a Link State Change Interrupt |
| 3839 | * from U3 to RESUME which is only necessary if Hibernation is |
| 3840 | * configured in. |
| 3841 | * |
| 3842 | * There are no functional changes due to such spurious event and we |
| 3843 | * just need to ignore it. |
| 3844 | * |
| 3845 | * Refers to: |
| 3846 | * |
| 3847 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation |
| 3848 | * operational mode |
| 3849 | */ |
| 3850 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3851 | if (DWC3_VER_IS_PRIOR(DWC3, 250A) && |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3852 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
| 3853 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && |
| 3854 | (next == DWC3_LINK_STATE_RESUME)) { |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3855 | return; |
| 3856 | } |
| 3857 | } |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3858 | |
| 3859 | /* |
| 3860 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 3861 | * on the link partner, the USB session might do multiple entry/exit |
| 3862 | * of low power states before a transfer takes place. |
| 3863 | * |
| 3864 | * Due to this problem, we might experience lower throughput. The |
| 3865 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 3866 | * transitioning from U1/U2 to U0 and enable those bits again |
| 3867 | * after a transfer completes and there are no pending transfers |
| 3868 | * on any of the enabled endpoints. |
| 3869 | * |
| 3870 | * This is the first half of that workaround. |
| 3871 | * |
| 3872 | * Refers to: |
| 3873 | * |
| 3874 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 3875 | * core send LGO_Ux entering U0 |
| 3876 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3877 | if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3878 | if (next == DWC3_LINK_STATE_U0) { |
| 3879 | u32 u1u2; |
| 3880 | u32 reg; |
| 3881 | |
| 3882 | switch (dwc->link_state) { |
| 3883 | case DWC3_LINK_STATE_U1: |
| 3884 | case DWC3_LINK_STATE_U2: |
| 3885 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3886 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 3887 | | DWC3_DCTL_ACCEPTU2ENA |
| 3888 | | DWC3_DCTL_INITU1ENA |
| 3889 | | DWC3_DCTL_ACCEPTU1ENA); |
| 3890 | |
| 3891 | if (!dwc->u1u2) |
| 3892 | dwc->u1u2 = reg & u1u2; |
| 3893 | |
| 3894 | reg &= ~u1u2; |
| 3895 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3896 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3897 | break; |
| 3898 | default: |
| 3899 | /* do nothing */ |
| 3900 | break; |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3905 | switch (next) { |
| 3906 | case DWC3_LINK_STATE_U1: |
| 3907 | if (dwc->speed == USB_SPEED_SUPER) |
| 3908 | dwc3_suspend_gadget(dwc); |
| 3909 | break; |
| 3910 | case DWC3_LINK_STATE_U2: |
| 3911 | case DWC3_LINK_STATE_U3: |
| 3912 | dwc3_suspend_gadget(dwc); |
| 3913 | break; |
| 3914 | case DWC3_LINK_STATE_RESUME: |
| 3915 | dwc3_resume_gadget(dwc); |
| 3916 | break; |
| 3917 | default: |
| 3918 | /* do nothing */ |
| 3919 | break; |
| 3920 | } |
| 3921 | |
Felipe Balbi | e57ebc1 | 2014-04-22 13:20:12 -0500 | [diff] [blame] | 3922 | dwc->link_state = next; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3923 | } |
| 3924 | |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3925 | static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, |
| 3926 | unsigned int evtinfo) |
| 3927 | { |
| 3928 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
| 3929 | |
| 3930 | if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) |
| 3931 | dwc3_suspend_gadget(dwc); |
| 3932 | |
| 3933 | dwc->link_state = next; |
| 3934 | } |
| 3935 | |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3936 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
| 3937 | unsigned int evtinfo) |
| 3938 | { |
| 3939 | unsigned int is_ss = evtinfo & BIT(4); |
| 3940 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 3941 | /* |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3942 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
| 3943 | * have a known issue which can cause USB CV TD.9.23 to fail |
| 3944 | * randomly. |
| 3945 | * |
| 3946 | * Because of this issue, core could generate bogus hibernation |
| 3947 | * events which SW needs to ignore. |
| 3948 | * |
| 3949 | * Refers to: |
| 3950 | * |
| 3951 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 |
| 3952 | * Device Fallback from SuperSpeed |
| 3953 | */ |
| 3954 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) |
| 3955 | return; |
| 3956 | |
| 3957 | /* enter hibernation here */ |
| 3958 | } |
| 3959 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3960 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 3961 | const struct dwc3_event_devt *event) |
| 3962 | { |
| 3963 | switch (event->type) { |
| 3964 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 3965 | dwc3_gadget_disconnect_interrupt(dwc); |
| 3966 | break; |
| 3967 | case DWC3_DEVICE_EVENT_RESET: |
| 3968 | dwc3_gadget_reset_interrupt(dwc); |
| 3969 | break; |
| 3970 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 3971 | dwc3_gadget_conndone_interrupt(dwc); |
| 3972 | break; |
| 3973 | case DWC3_DEVICE_EVENT_WAKEUP: |
| 3974 | dwc3_gadget_wakeup_interrupt(dwc); |
| 3975 | break; |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3976 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
| 3977 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, |
| 3978 | "unexpected hibernation event\n")) |
| 3979 | break; |
| 3980 | |
| 3981 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
| 3982 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3983 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 3984 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 3985 | break; |
Jack Pham | 6f26ebb | 2021-04-28 02:01:11 -0700 | [diff] [blame] | 3986 | case DWC3_DEVICE_EVENT_SUSPEND: |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3987 | /* It changed to be suspend event for version 2.30a and above */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3988 | if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) { |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3989 | /* |
| 3990 | * Ignore suspend event until the gadget enters into |
| 3991 | * USB_STATE_CONFIGURED state. |
| 3992 | */ |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3993 | if (dwc->gadget->state >= USB_STATE_CONFIGURED) |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3994 | dwc3_gadget_suspend_interrupt(dwc, |
| 3995 | event->event_info); |
| 3996 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3997 | break; |
| 3998 | case DWC3_DEVICE_EVENT_SOF: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3999 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4000 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4001 | case DWC3_DEVICE_EVENT_OVERFLOW: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4002 | break; |
| 4003 | default: |
Felipe Balbi | e9f2aa8 | 2015-01-27 13:49:28 -0600 | [diff] [blame] | 4004 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 4009 | const union dwc3_event *event) |
| 4010 | { |
Felipe Balbi | 43c96be | 2016-09-26 13:23:34 +0300 | [diff] [blame] | 4011 | trace_dwc3_event(event->raw, dwc); |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 4012 | |
Felipe Balbi | dfc5e80 | 2017-04-26 13:44:51 +0300 | [diff] [blame] | 4013 | if (!event->type.is_devspec) |
| 4014 | dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 4015 | else if (event->type.type == DWC3_EVENT_TYPE_DEV) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4016 | dwc3_gadget_interrupt(dwc, &event->devt); |
Felipe Balbi | dfc5e80 | 2017-04-26 13:44:51 +0300 | [diff] [blame] | 4017 | else |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4018 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4019 | } |
| 4020 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4021 | static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4022 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4023 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4024 | irqreturn_t ret = IRQ_NONE; |
| 4025 | int left; |
| 4026 | u32 reg; |
| 4027 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4028 | left = evt->count; |
| 4029 | |
| 4030 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
| 4031 | return IRQ_NONE; |
| 4032 | |
| 4033 | while (left > 0) { |
| 4034 | union dwc3_event event; |
| 4035 | |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 4036 | event.raw = *(u32 *) (evt->cache + evt->lpos); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4037 | |
| 4038 | dwc3_process_event_entry(dwc, &event); |
| 4039 | |
| 4040 | /* |
| 4041 | * FIXME we wrap around correctly to the next entry as |
| 4042 | * almost all entries are 4 bytes in size. There is one |
| 4043 | * entry which has 12 bytes which is a regular entry |
| 4044 | * followed by 8 bytes data. ATM I don't know how |
| 4045 | * things are organized if we get next to the a |
| 4046 | * boundary so I worry about that once we try to handle |
| 4047 | * that. |
| 4048 | */ |
Felipe Balbi | caefe6c | 2016-11-15 13:05:23 +0200 | [diff] [blame] | 4049 | evt->lpos = (evt->lpos + 4) % evt->length; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4050 | left -= 4; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | evt->count = 0; |
| 4054 | evt->flags &= ~DWC3_EVENT_PENDING; |
| 4055 | ret = IRQ_HANDLED; |
| 4056 | |
| 4057 | /* Unmask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 4058 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4059 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 4060 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4061 | |
John Youn | cf40b86 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 4062 | if (dwc->imod_interval) { |
| 4063 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); |
| 4064 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); |
| 4065 | } |
| 4066 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 4067 | return ret; |
| 4068 | } |
| 4069 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4070 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 4071 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4072 | struct dwc3_event_buffer *evt = _evt; |
| 4073 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 4074 | unsigned long flags; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 4075 | irqreturn_t ret = IRQ_NONE; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 4076 | |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 4077 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4078 | ret = dwc3_process_event_buf(evt); |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 4079 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 4080 | |
| 4081 | return ret; |
| 4082 | } |
| 4083 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4084 | static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4085 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4086 | struct dwc3 *dwc = evt->dwc; |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 4087 | u32 amount; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4088 | u32 count; |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 4089 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4090 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 4091 | if (pm_runtime_suspended(dwc->dev)) { |
| 4092 | pm_runtime_get(dwc->dev); |
| 4093 | disable_irq_nosync(dwc->irq_gadget); |
| 4094 | dwc->pending_events = true; |
| 4095 | return IRQ_HANDLED; |
| 4096 | } |
| 4097 | |
Thinh Nguyen | d325a1d | 2017-05-11 17:26:47 -0700 | [diff] [blame] | 4098 | /* |
| 4099 | * With PCIe legacy interrupt, test shows that top-half irq handler can |
| 4100 | * be called again after HW interrupt deassertion. Check if bottom-half |
| 4101 | * irq event handler completes before caching new event to prevent |
| 4102 | * losing events. |
| 4103 | */ |
| 4104 | if (evt->flags & DWC3_EVENT_PENDING) |
| 4105 | return IRQ_HANDLED; |
| 4106 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 4107 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4108 | count &= DWC3_GEVNTCOUNT_MASK; |
| 4109 | if (!count) |
| 4110 | return IRQ_NONE; |
| 4111 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 4112 | evt->count = count; |
| 4113 | evt->flags |= DWC3_EVENT_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4114 | |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 4115 | /* Mask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 4116 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 4117 | reg |= DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 4118 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 4119 | |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 4120 | amount = min(count, evt->length - evt->lpos); |
| 4121 | memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); |
| 4122 | |
| 4123 | if (amount < count) |
| 4124 | memcpy(evt->cache, evt->buf, count - amount); |
| 4125 | |
John Youn | 65aca32 | 2016-11-15 13:08:59 +0200 | [diff] [blame] | 4126 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
| 4127 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 4128 | return IRQ_WAKE_THREAD; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4129 | } |
| 4130 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4131 | static irqreturn_t dwc3_interrupt(int irq, void *_evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4132 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4133 | struct dwc3_event_buffer *evt = _evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4134 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 4135 | return dwc3_check_event_buf(evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4136 | } |
| 4137 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 4138 | static int dwc3_gadget_get_irq(struct dwc3 *dwc) |
| 4139 | { |
| 4140 | struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); |
| 4141 | int irq; |
| 4142 | |
Hans de Goede | f146b40b | 2019-10-05 23:04:48 +0200 | [diff] [blame] | 4143 | irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral"); |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 4144 | if (irq > 0) |
| 4145 | goto out; |
| 4146 | |
| 4147 | if (irq == -EPROBE_DEFER) |
| 4148 | goto out; |
| 4149 | |
Hans de Goede | f146b40b | 2019-10-05 23:04:48 +0200 | [diff] [blame] | 4150 | irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 4151 | if (irq > 0) |
| 4152 | goto out; |
| 4153 | |
| 4154 | if (irq == -EPROBE_DEFER) |
| 4155 | goto out; |
| 4156 | |
| 4157 | irq = platform_get_irq(dwc3_pdev, 0); |
| 4158 | if (irq > 0) |
| 4159 | goto out; |
| 4160 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 4161 | if (!irq) |
| 4162 | irq = -EINVAL; |
| 4163 | |
| 4164 | out: |
| 4165 | return irq; |
| 4166 | } |
| 4167 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4168 | static void dwc_gadget_release(struct device *dev) |
| 4169 | { |
| 4170 | struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); |
| 4171 | |
| 4172 | kfree(gadget); |
| 4173 | } |
| 4174 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4175 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 4176 | * dwc3_gadget_init - initializes gadget related registers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 4177 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4178 | * |
| 4179 | * Returns 0 on success otherwise negative errno. |
| 4180 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 4181 | int dwc3_gadget_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4182 | { |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 4183 | int ret; |
| 4184 | int irq; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4185 | struct device *dev; |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 4186 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 4187 | irq = dwc3_gadget_get_irq(dwc); |
| 4188 | if (irq < 0) { |
| 4189 | ret = irq; |
| 4190 | goto err0; |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | dwc->irq_gadget = irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4194 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 4195 | dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, |
| 4196 | sizeof(*dwc->ep0_trb) * 2, |
| 4197 | &dwc->ep0_trb_addr, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4198 | if (!dwc->ep0_trb) { |
| 4199 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 4200 | ret = -ENOMEM; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 4201 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4202 | } |
| 4203 | |
Felipe Balbi | 4199c5f | 2017-04-07 14:09:13 +0300 | [diff] [blame] | 4204 | dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4205 | if (!dwc->setup_buf) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4206 | ret = -ENOMEM; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 4207 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4208 | } |
| 4209 | |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 4210 | dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, |
| 4211 | &dwc->bounce_addr, GFP_KERNEL); |
| 4212 | if (!dwc->bounce) { |
| 4213 | ret = -ENOMEM; |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4214 | goto err2; |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 4215 | } |
| 4216 | |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 4217 | init_completion(&dwc->ep0_in_setup); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4218 | dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); |
| 4219 | if (!dwc->gadget) { |
| 4220 | ret = -ENOMEM; |
| 4221 | goto err3; |
| 4222 | } |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 4223 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4224 | |
| 4225 | usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); |
| 4226 | dev = &dwc->gadget->dev; |
| 4227 | dev->platform_data = dwc; |
| 4228 | dwc->gadget->ops = &dwc3_gadget_ops; |
| 4229 | dwc->gadget->speed = USB_SPEED_UNKNOWN; |
Thinh Nguyen | f551037c | 2021-01-19 17:36:34 -0800 | [diff] [blame] | 4230 | dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4231 | dwc->gadget->sg_supported = true; |
| 4232 | dwc->gadget->name = "dwc3-gadget"; |
Thinh Nguyen | 475e8be | 2021-04-13 19:13:18 -0700 | [diff] [blame] | 4233 | dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4234 | |
| 4235 | /* |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 4236 | * FIXME We might be setting max_speed to <SUPER, however versions |
| 4237 | * <2.20a of dwc3 have an issue with metastability (documented |
| 4238 | * elsewhere in this driver) which tells us we can't set max speed to |
| 4239 | * anything lower than SUPER. |
| 4240 | * |
| 4241 | * Because gadget.max_speed is only used by composite.c and function |
| 4242 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this |
| 4243 | * to happen so we avoid sending SuperSpeed Capability descriptor |
| 4244 | * together with our BOS descriptor as that could confuse host into |
| 4245 | * thinking we can handle super speed. |
| 4246 | * |
| 4247 | * Note that, in fact, we won't even support GetBOS requests when speed |
| 4248 | * is less than super speed because we don't have means, yet, to tell |
| 4249 | * composite.c that we are USB 2.0 + LPM ECN. |
| 4250 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 4251 | if (DWC3_VER_IS_PRIOR(DWC3, 220A) && |
Roger Quadros | 42bf02e | 2017-10-31 15:11:55 +0200 | [diff] [blame] | 4252 | !dwc->dis_metastability_quirk) |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 4253 | dev_info(dwc->dev, "changing max_speed on rev %08x\n", |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 4254 | dwc->revision); |
| 4255 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4256 | dwc->gadget->max_speed = dwc->maximum_speed; |
Thinh Nguyen | 6784814 | 2021-01-19 17:36:21 -0800 | [diff] [blame] | 4257 | dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 4258 | |
| 4259 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4260 | * REVISIT: Here we should clear all pending IRQs to be |
| 4261 | * sure we're starting from a well known location. |
| 4262 | */ |
| 4263 | |
Bryan O'Donoghue | f3bcfc7 | 2017-01-31 20:58:11 +0000 | [diff] [blame] | 4264 | ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4265 | if (ret) |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4266 | goto err4; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4267 | |
| 4268 | ret = usb_add_gadget(dwc->gadget); |
| 4269 | if (ret) { |
| 4270 | dev_err(dwc->dev, "failed to add gadget\n"); |
| 4271 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4272 | } |
| 4273 | |
Thinh Nguyen | 072cab8a | 2021-01-19 17:36:28 -0800 | [diff] [blame] | 4274 | if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) |
| 4275 | dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); |
| 4276 | else |
| 4277 | dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); |
Roger Quadros | 169e3b6 | 2019-01-10 17:04:28 +0200 | [diff] [blame] | 4278 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4279 | return 0; |
Felipe Balbi | 4199c5f | 2017-04-07 14:09:13 +0300 | [diff] [blame] | 4280 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4281 | err5: |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4282 | dwc3_gadget_free_endpoints(dwc); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 4283 | err4: |
| 4284 | usb_put_gadget(dwc->gadget); |
Jack Pham | 03715ea | 2021-05-28 09:04:05 -0700 | [diff] [blame] | 4285 | dwc->gadget = NULL; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 4286 | err3: |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4287 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
| 4288 | dwc->bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 4289 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 4290 | err2: |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 4291 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4292 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 4293 | err1: |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 4294 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4295 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 4296 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4297 | err0: |
| 4298 | return ret; |
| 4299 | } |
| 4300 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4301 | /* -------------------------------------------------------------------------- */ |
| 4302 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4303 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 4304 | { |
Jack Pham | 03715ea | 2021-05-28 09:04:05 -0700 | [diff] [blame] | 4305 | if (!dwc->gadget) |
| 4306 | return; |
| 4307 | |
Jack Pham | bb9c74a | 2021-05-01 02:35:58 -0700 | [diff] [blame] | 4308 | usb_del_gadget(dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4309 | dwc3_gadget_free_endpoints(dwc); |
Jack Pham | bb9c74a | 2021-05-01 02:35:58 -0700 | [diff] [blame] | 4310 | usb_put_gadget(dwc->gadget); |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 4311 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4312 | dwc->bounce_addr); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 4313 | kfree(dwc->setup_buf); |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 4314 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4315 | dwc->ep0_trb, dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4316 | } |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4317 | |
Felipe Balbi | 0b0231a | 2014-10-07 10:19:23 -0500 | [diff] [blame] | 4318 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4319 | { |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 4320 | if (!dwc->gadget_driver) |
| 4321 | return 0; |
| 4322 | |
Roger Quadros | 1551e35 | 2017-02-15 14:16:26 +0200 | [diff] [blame] | 4323 | dwc3_gadget_run_stop(dwc, false, false); |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4324 | dwc3_disconnect_gadget(dwc); |
| 4325 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4326 | |
| 4327 | return 0; |
| 4328 | } |
| 4329 | |
| 4330 | int dwc3_gadget_resume(struct dwc3 *dwc) |
| 4331 | { |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4332 | int ret; |
| 4333 | |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 4334 | if (!dwc->gadget_driver) |
| 4335 | return 0; |
| 4336 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4337 | ret = __dwc3_gadget_start(dwc); |
| 4338 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4339 | goto err0; |
| 4340 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4341 | ret = dwc3_gadget_run_stop(dwc, true, false); |
| 4342 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4343 | goto err1; |
| 4344 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4345 | return 0; |
| 4346 | |
| 4347 | err1: |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4348 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4349 | |
| 4350 | err0: |
| 4351 | return ret; |
| 4352 | } |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 4353 | |
| 4354 | void dwc3_gadget_process_pending_events(struct dwc3 *dwc) |
| 4355 | { |
| 4356 | if (dwc->pending_events) { |
| 4357 | dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); |
| 4358 | dwc->pending_events = false; |
| 4359 | enable_irq(dwc->irq_gadget); |
| 4360 | } |
| 4361 | } |