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 | * |
| 5 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://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) { |
| 49 | case TEST_J: |
| 50 | case TEST_K: |
| 51 | case TEST_SE0_NAK: |
| 52 | case TEST_PACKET: |
| 53 | case TEST_FORCE_EN: |
| 54 | reg |= mode << 1; |
| 55 | break; |
| 56 | default: |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 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 | */ |
| 98 | if (dwc->revision >= DWC3_REVISION_194A) { |
| 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 | |
| 114 | /* set requested state */ |
| 115 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); |
| 116 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 117 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 118 | /* |
| 119 | * The following code is racy when called from dwc3_gadget_wakeup, |
| 120 | * and is not needed, at least on newer versions |
| 121 | */ |
| 122 | if (dwc->revision >= DWC3_REVISION_194A) |
| 123 | return 0; |
| 124 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 125 | /* wait for a change in DSTS */ |
Paul Zimmerman | aed430e | 2012-04-27 12:52:01 +0300 | [diff] [blame] | 126 | retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 127 | while (--retries) { |
| 128 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 129 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 130 | if (DWC3_DSTS_USBLNKST(reg) == state) |
| 131 | return 0; |
| 132 | |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 133 | udelay(5); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 136 | return -ETIMEDOUT; |
| 137 | } |
| 138 | |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 139 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 140 | * dwc3_ep_inc_trb - increment a trb index. |
| 141 | * @index: Pointer to the TRB index to increment. |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 142 | * |
| 143 | * The index should never point to the link TRB. After incrementing, |
| 144 | * if it is point to the link TRB, wrap around to the beginning. The |
| 145 | * link TRB is always at the last TRB entry. |
| 146 | */ |
| 147 | static void dwc3_ep_inc_trb(u8 *index) |
| 148 | { |
| 149 | (*index)++; |
| 150 | if (*index == (DWC3_TRB_NUM - 1)) |
| 151 | *index = 0; |
| 152 | } |
| 153 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 154 | /** |
| 155 | * dwc3_ep_inc_enq - increment endpoint's enqueue pointer |
| 156 | * @dep: The endpoint whose enqueue pointer we're incrementing |
| 157 | */ |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 158 | static void dwc3_ep_inc_enq(struct dwc3_ep *dep) |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 159 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 160 | dwc3_ep_inc_trb(&dep->trb_enqueue); |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 161 | } |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 162 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 163 | /** |
| 164 | * dwc3_ep_inc_deq - increment endpoint's dequeue pointer |
| 165 | * @dep: The endpoint whose enqueue pointer we're incrementing |
| 166 | */ |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 167 | static void dwc3_ep_inc_deq(struct dwc3_ep *dep) |
| 168 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 169 | dwc3_ep_inc_trb(&dep->trb_dequeue); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Wei Yongjun | 6910251 | 2018-03-29 02:20:10 +0000 | [diff] [blame] | 172 | static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 173 | struct dwc3_request *req, int status) |
| 174 | { |
| 175 | struct dwc3 *dwc = dep->dwc; |
| 176 | |
| 177 | req->started = false; |
| 178 | list_del(&req->list); |
| 179 | req->remaining = 0; |
Jack Pham | bd674224 | 2019-01-10 12:39:55 -0800 | [diff] [blame] | 180 | req->needs_extra_trb = false; |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 181 | |
| 182 | if (req->request.status == -EINPROGRESS) |
| 183 | req->request.status = status; |
| 184 | |
| 185 | if (req->trb) |
| 186 | usb_gadget_unmap_request_by_dev(dwc->sysdev, |
| 187 | &req->request, req->direction); |
| 188 | |
| 189 | req->trb = NULL; |
| 190 | trace_dwc3_gadget_giveback(req); |
| 191 | |
| 192 | if (dep->number > 1) |
| 193 | pm_runtime_put(dwc->dev); |
| 194 | } |
| 195 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 196 | /** |
| 197 | * dwc3_gadget_giveback - call struct usb_request's ->complete callback |
| 198 | * @dep: The endpoint to whom the request belongs to |
| 199 | * @req: The request we're giving back |
| 200 | * @status: completion code for the request |
| 201 | * |
| 202 | * Must be called with controller's lock held and interrupts disabled. This |
| 203 | * function will unmap @req and call its ->complete() callback to notify upper |
| 204 | * layers that it has completed. |
| 205 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 206 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 207 | int status) |
| 208 | { |
| 209 | struct dwc3 *dwc = dep->dwc; |
| 210 | |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 211 | dwc3_gadget_del_and_unmap_request(dep, req, status); |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 212 | req->status = DWC3_REQUEST_STATUS_COMPLETED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 213 | |
| 214 | spin_unlock(&dwc->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 215 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 216 | spin_lock(&dwc->lock); |
| 217 | } |
| 218 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 219 | /** |
| 220 | * dwc3_send_gadget_generic_command - issue a generic command for the controller |
| 221 | * @dwc: pointer to the controller context |
| 222 | * @cmd: the command to be issued |
| 223 | * @param: command parameter |
| 224 | * |
| 225 | * Caller should take care of locking. Issue @cmd with a given @param to @dwc |
| 226 | * and wait for its completion. |
| 227 | */ |
Felipe Balbi | 3ece0ec | 2014-09-05 09:47:44 -0500 | [diff] [blame] | 228 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 229 | { |
| 230 | u32 timeout = 500; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 231 | int status = 0; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 232 | int ret = 0; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 233 | u32 reg; |
| 234 | |
| 235 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 236 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 237 | |
| 238 | do { |
| 239 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 240 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 241 | status = DWC3_DGCMD_STATUS(reg); |
| 242 | if (status) |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 243 | ret = -EINVAL; |
| 244 | break; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 245 | } |
Janusz Dziedzic | e3aee48 | 2016-11-09 11:01:33 +0100 | [diff] [blame] | 246 | } while (--timeout); |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 247 | |
| 248 | if (!timeout) { |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 249 | ret = -ETIMEDOUT; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 250 | status = -ETIMEDOUT; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 251 | } |
| 252 | |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 253 | trace_dwc3_gadget_generic_cmd(cmd, param, status); |
| 254 | |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 255 | return ret; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 256 | } |
| 257 | |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 258 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc); |
| 259 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 260 | /** |
| 261 | * dwc3_send_gadget_ep_cmd - issue an endpoint command |
| 262 | * @dep: the endpoint to which the command is going to be issued |
| 263 | * @cmd: the command to be issued |
| 264 | * @params: parameters to the command |
| 265 | * |
| 266 | * Caller should handle locking. This function will issue @cmd with given |
| 267 | * @params to @dep and wait for its completion. |
| 268 | */ |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 269 | int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, |
| 270 | struct dwc3_gadget_ep_cmd_params *params) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 271 | { |
Felipe Balbi | 8897a76 | 2016-09-22 10:56:08 +0300 | [diff] [blame] | 272 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 273 | struct dwc3 *dwc = dep->dwc; |
Vincent Pelletier | 8722e09 | 2017-11-30 15:31:06 +0000 | [diff] [blame] | 274 | u32 timeout = 1000; |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 275 | u32 saved_config = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 276 | u32 reg; |
| 277 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 278 | int cmd_status = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 279 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 280 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 281 | /* |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 282 | * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or |
| 283 | * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an |
| 284 | * endpoint command. |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 285 | * |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 286 | * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY |
| 287 | * settings. Restore them after the command is completed. |
| 288 | * |
| 289 | * 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] | 290 | */ |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 291 | if (dwc->gadget.speed <= USB_SPEED_HIGH) { |
| 292 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 293 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 294 | saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 295 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 296 | } |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 297 | |
| 298 | if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { |
| 299 | saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 300 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 301 | } |
| 302 | |
| 303 | if (saved_config) |
| 304 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 305 | } |
| 306 | |
Felipe Balbi | 5999914 | 2016-09-22 12:25:28 +0300 | [diff] [blame] | 307 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 308 | int needs_wakeup; |
| 309 | |
| 310 | needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 || |
| 311 | dwc->link_state == DWC3_LINK_STATE_U2 || |
| 312 | dwc->link_state == DWC3_LINK_STATE_U3); |
| 313 | |
| 314 | if (unlikely(needs_wakeup)) { |
| 315 | ret = __dwc3_gadget_wakeup(dwc); |
| 316 | dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", |
| 317 | ret); |
| 318 | } |
| 319 | } |
| 320 | |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 321 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); |
| 322 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); |
| 323 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 324 | |
Felipe Balbi | 8897a76 | 2016-09-22 10:56:08 +0300 | [diff] [blame] | 325 | /* |
| 326 | * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're |
| 327 | * not relying on XferNotReady, we can make use of a special "No |
| 328 | * Response Update Transfer" command where we should clear both CmdAct |
| 329 | * and CmdIOC bits. |
| 330 | * |
| 331 | * With this, we don't need to wait for command completion and can |
| 332 | * straight away issue further commands to the endpoint. |
| 333 | * |
| 334 | * NOTICE: We're making an assumption that control endpoints will never |
| 335 | * make use of Update Transfer command. This is a safe assumption |
| 336 | * because we can never have more than one request at a time with |
| 337 | * Control Endpoints. If anybody changes that assumption, this chunk |
| 338 | * needs to be updated accordingly. |
| 339 | */ |
| 340 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && |
| 341 | !usb_endpoint_xfer_isoc(desc)) |
| 342 | cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); |
| 343 | else |
| 344 | cmd |= DWC3_DEPCMD_CMDACT; |
| 345 | |
| 346 | dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 347 | do { |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 348 | reg = dwc3_readl(dep->regs, DWC3_DEPCMD); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 349 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 350 | cmd_status = DWC3_DEPCMD_STATUS(reg); |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 351 | |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 352 | switch (cmd_status) { |
| 353 | case 0: |
| 354 | ret = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 355 | break; |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 356 | case DEPEVT_TRANSFER_NO_RESOURCE: |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 357 | ret = -EINVAL; |
| 358 | break; |
| 359 | case DEPEVT_TRANSFER_BUS_EXPIRY: |
| 360 | /* |
| 361 | * SW issues START TRANSFER command to |
| 362 | * isochronous ep with future frame interval. If |
| 363 | * future interval time has already passed when |
| 364 | * core receives the command, it will respond |
| 365 | * with an error status of 'Bus Expiry'. |
| 366 | * |
| 367 | * Instead of always returning -EINVAL, let's |
| 368 | * give a hint to the gadget driver that this is |
| 369 | * the case by returning -EAGAIN. |
| 370 | */ |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 371 | ret = -EAGAIN; |
| 372 | break; |
| 373 | default: |
| 374 | dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); |
| 375 | } |
| 376 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 377 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 378 | } |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 379 | } while (--timeout); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 380 | |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 381 | if (timeout == 0) { |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 382 | ret = -ETIMEDOUT; |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 383 | cmd_status = -ETIMEDOUT; |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 384 | } |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 385 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 386 | trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); |
| 387 | |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 388 | if (ret == 0) { |
| 389 | switch (DWC3_DEPCMD_CMD(cmd)) { |
| 390 | case DWC3_DEPCMD_STARTTRANSFER: |
| 391 | dep->flags |= DWC3_EP_TRANSFER_STARTED; |
Felipe Balbi | d7ca7e1 | 2018-04-11 12:58:46 +0300 | [diff] [blame] | 392 | dwc3_gadget_ep_get_transfer_index(dep); |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 393 | break; |
| 394 | case DWC3_DEPCMD_ENDTRANSFER: |
| 395 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
| 396 | break; |
| 397 | default: |
| 398 | /* nothing */ |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 403 | if (saved_config) { |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 404 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 405 | reg |= saved_config; |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 406 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 407 | } |
| 408 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 409 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 410 | } |
| 411 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 412 | static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) |
| 413 | { |
| 414 | struct dwc3 *dwc = dep->dwc; |
| 415 | struct dwc3_gadget_ep_cmd_params params; |
| 416 | u32 cmd = DWC3_DEPCMD_CLEARSTALL; |
| 417 | |
| 418 | /* |
| 419 | * As of core revision 2.60a the recommended programming model |
| 420 | * is to set the ClearPendIN bit when issuing a Clear Stall EP |
| 421 | * command for IN endpoints. This is to prevent an issue where |
| 422 | * some (non-compliant) hosts may not send ACK TPs for pending |
| 423 | * IN transfers due to a mishandled error condition. Synopsys |
| 424 | * STAR 9000614252. |
| 425 | */ |
Lu Baolu | 5e6c88d | 2016-09-09 12:51:27 +0800 | [diff] [blame] | 426 | if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) && |
| 427 | (dwc->gadget.speed >= USB_SPEED_SUPER)) |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 428 | cmd |= DWC3_DEPCMD_CLEARPENDIN; |
| 429 | |
| 430 | memset(¶ms, 0, sizeof(params)); |
| 431 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 432 | return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 435 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 436 | struct dwc3_trb *trb) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 437 | { |
Paul Zimmerman | c439ef8 | 2011-09-30 10:58:45 +0300 | [diff] [blame] | 438 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 439 | |
| 440 | return dep->trb_pool_dma + offset; |
| 441 | } |
| 442 | |
| 443 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 444 | { |
| 445 | struct dwc3 *dwc = dep->dwc; |
| 446 | |
| 447 | if (dep->trb_pool) |
| 448 | return 0; |
| 449 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 450 | dep->trb_pool = dma_alloc_coherent(dwc->sysdev, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 451 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 452 | &dep->trb_pool_dma, GFP_KERNEL); |
| 453 | if (!dep->trb_pool) { |
| 454 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 455 | dep->name); |
| 456 | return -ENOMEM; |
| 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 463 | { |
| 464 | struct dwc3 *dwc = dep->dwc; |
| 465 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 466 | dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 467 | dep->trb_pool, dep->trb_pool_dma); |
| 468 | |
| 469 | dep->trb_pool = NULL; |
| 470 | dep->trb_pool_dma = 0; |
| 471 | } |
| 472 | |
Felipe Balbi | 20d1d43 | 2018-04-09 12:49:02 +0300 | [diff] [blame] | 473 | static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) |
| 474 | { |
| 475 | struct dwc3_gadget_ep_cmd_params params; |
| 476 | |
| 477 | memset(¶ms, 0x00, sizeof(params)); |
| 478 | |
| 479 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
| 480 | |
| 481 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, |
| 482 | ¶ms); |
| 483 | } |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 484 | |
| 485 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 486 | * dwc3_gadget_start_config - configure ep resources |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 487 | * @dep: endpoint that is being enabled |
| 488 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 489 | * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's |
| 490 | * completion, it will set Transfer Resource for all available endpoints. |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 491 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 492 | * The assignment of transfer resources cannot perfectly follow the data book |
| 493 | * due to the fact that the controller driver does not have all knowledge of the |
| 494 | * configuration in advance. It is given this information piecemeal by the |
| 495 | * composite gadget framework after every SET_CONFIGURATION and |
| 496 | * SET_INTERFACE. Trying to follow the databook programming model in this |
| 497 | * scenario can cause errors. For two reasons: |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 498 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 499 | * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every |
| 500 | * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is |
| 501 | * incorrect in the scenario of multiple interfaces. |
| 502 | * |
| 503 | * 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] | 504 | * endpoint on alt setting (8.1.6). |
| 505 | * |
| 506 | * The following simplified method is used instead: |
| 507 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 508 | * All hardware endpoints can be assigned a transfer resource and this setting |
| 509 | * will stay persistent until either a core reset or hibernation. So whenever we |
| 510 | * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do |
| 511 | * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 512 | * guaranteed that there are as many transfer resources as endpoints. |
| 513 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 514 | * This function is called for each endpoint when it is being enabled but is |
| 515 | * triggered only when called for EP0-out, which always happens first, and which |
| 516 | * should only happen in one of the above conditions. |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 517 | */ |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 518 | static int dwc3_gadget_start_config(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 519 | { |
| 520 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 521 | struct dwc3 *dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 522 | u32 cmd; |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 523 | int i; |
| 524 | int ret; |
| 525 | |
| 526 | if (dep->number) |
| 527 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 528 | |
| 529 | memset(¶ms, 0x00, sizeof(params)); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 530 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 531 | dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 532 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 533 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 534 | if (ret) |
| 535 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 536 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 537 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
| 538 | struct dwc3_ep *dep = dwc->eps[i]; |
| 539 | |
| 540 | if (!dep) |
| 541 | continue; |
| 542 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 543 | ret = dwc3_gadget_set_xfer_resource(dep); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 544 | if (ret) |
| 545 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 551 | 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] | 552 | { |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 553 | const struct usb_ss_ep_comp_descriptor *comp_desc; |
| 554 | const struct usb_endpoint_descriptor *desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 555 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 556 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 557 | |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 558 | comp_desc = dep->endpoint.comp_desc; |
| 559 | desc = dep->endpoint.desc; |
| 560 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 561 | memset(¶ms, 0x00, sizeof(params)); |
| 562 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 563 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 564 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 565 | |
| 566 | /* Burst size is only needed in SuperSpeed mode */ |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 567 | if (dwc->gadget.speed >= USB_SPEED_SUPER) { |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 568 | u32 burst = dep->endpoint.maxburst; |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 569 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 570 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 571 | |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 572 | params.param0 |= action; |
| 573 | if (action == DWC3_DEPCFG_ACTION_RESTORE) |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 574 | params.param2 |= dep->saved_state; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 575 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 576 | if (usb_endpoint_xfer_control(desc)) |
| 577 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; |
Felipe Balbi | 13fa2e6 | 2016-05-30 13:40:00 +0300 | [diff] [blame] | 578 | |
| 579 | if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) |
| 580 | params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 581 | |
Felipe Balbi | 18b7ede | 2012-01-02 13:35:41 +0200 | [diff] [blame] | 582 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 583 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
| 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) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 607 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 608 | dep->interval = 1 << (desc->bInterval - 1); |
| 609 | } |
| 610 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 611 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 612 | } |
| 613 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 614 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 615 | * __dwc3_gadget_ep_enable - initializes a hw endpoint |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 616 | * @dep: endpoint to be initialized |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 617 | * @action: one of INIT, MODIFY or RESTORE |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 618 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 619 | * Caller should take care of locking. Execute all necessary commands to |
| 620 | * 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] | 621 | */ |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 622 | 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] | 623 | { |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 624 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 625 | struct dwc3 *dwc = dep->dwc; |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 626 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 627 | u32 reg; |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 628 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 629 | |
| 630 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 631 | ret = dwc3_gadget_start_config(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 632 | if (ret) |
| 633 | return ret; |
| 634 | } |
| 635 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 636 | ret = dwc3_gadget_set_ep_config(dep, action); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 637 | if (ret) |
| 638 | return ret; |
| 639 | |
| 640 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 641 | struct dwc3_trb *trb_st_hw; |
| 642 | struct dwc3_trb *trb_link; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 643 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 644 | dep->type = usb_endpoint_type(desc); |
| 645 | dep->flags |= DWC3_EP_ENABLED; |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 646 | dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 647 | |
| 648 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 649 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 650 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 651 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 652 | if (usb_endpoint_xfer_control(desc)) |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 653 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 654 | |
John Youn | 0d25744 | 2016-05-19 17:26:08 -0700 | [diff] [blame] | 655 | /* Initialize the TRB ring */ |
| 656 | dep->trb_dequeue = 0; |
| 657 | dep->trb_enqueue = 0; |
| 658 | memset(dep->trb_pool, 0, |
| 659 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM); |
| 660 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 661 | /* Link TRB. The HWO bit is never reset */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 662 | trb_st_hw = &dep->trb_pool[0]; |
| 663 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 664 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 665 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 666 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 667 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 668 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 669 | } |
| 670 | |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 671 | /* |
| 672 | * Issue StartTransfer here with no-op TRB so we can always rely on No |
| 673 | * Response Update Transfer command. |
| 674 | */ |
Anurag Kumar Vulisha | 26d62b4 | 2018-12-01 16:43:27 +0530 | [diff] [blame] | 675 | if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) || |
Felipe Balbi | 52fcc0b | 2018-03-26 13:19:43 +0300 | [diff] [blame] | 676 | usb_endpoint_xfer_int(desc)) { |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 677 | struct dwc3_gadget_ep_cmd_params params; |
| 678 | struct dwc3_trb *trb; |
| 679 | dma_addr_t trb_dma; |
| 680 | u32 cmd; |
| 681 | |
| 682 | memset(¶ms, 0, sizeof(params)); |
| 683 | trb = &dep->trb_pool[0]; |
| 684 | trb_dma = dwc3_trb_dma_offset(dep, trb); |
| 685 | |
| 686 | params.param0 = upper_32_bits(trb_dma); |
| 687 | params.param1 = lower_32_bits(trb_dma); |
| 688 | |
| 689 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 690 | |
| 691 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 692 | if (ret < 0) |
| 693 | return ret; |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 694 | } |
| 695 | |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 696 | out: |
| 697 | trace_dwc3_gadget_ep_enable(dep); |
| 698 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 702 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 703 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 704 | { |
| 705 | struct dwc3_request *req; |
| 706 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 707 | dwc3_stop_active_transfer(dep, true); |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 708 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 709 | /* - giveback all requests to gadget driver */ |
| 710 | while (!list_empty(&dep->started_list)) { |
| 711 | req = next_request(&dep->started_list); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 712 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 713 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | ea53b88 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 716 | while (!list_empty(&dep->pending_list)) { |
| 717 | req = next_request(&dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 718 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 719 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 720 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 724 | * __dwc3_gadget_ep_disable - disables a hw endpoint |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 725 | * @dep: the endpoint to disable |
| 726 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 727 | * This function undoes what __dwc3_gadget_ep_enable did and also removes |
| 728 | * requests which are currently being processed by the hardware and those which |
| 729 | * are not yet scheduled. |
| 730 | * |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 731 | * Caller should take care of locking. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 732 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 733 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 734 | { |
| 735 | struct dwc3 *dwc = dep->dwc; |
| 736 | u32 reg; |
| 737 | |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 738 | trace_dwc3_gadget_ep_disable(dep); |
Felipe Balbi | 7eaeac5 | 2015-07-20 14:46:15 -0500 | [diff] [blame] | 739 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 740 | dwc3_remove_requests(dwc, dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 741 | |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 742 | /* make sure HW endpoint isn't stalled */ |
| 743 | if (dep->flags & DWC3_EP_STALL) |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 744 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 745 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 746 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 747 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 748 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 749 | |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 750 | dep->stream_capable = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 751 | dep->type = 0; |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 752 | dep->flags &= DWC3_EP_END_TRANSFER_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 753 | |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 754 | /* Clear out the ep descriptors for non-ep0 */ |
| 755 | if (dep->number > 1) { |
| 756 | dep->endpoint.comp_desc = NULL; |
| 757 | dep->endpoint.desc = NULL; |
| 758 | } |
| 759 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* -------------------------------------------------------------------------- */ |
| 764 | |
| 765 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 766 | const struct usb_endpoint_descriptor *desc) |
| 767 | { |
| 768 | return -EINVAL; |
| 769 | } |
| 770 | |
| 771 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 772 | { |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | /* -------------------------------------------------------------------------- */ |
| 777 | |
| 778 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 779 | const struct usb_endpoint_descriptor *desc) |
| 780 | { |
| 781 | struct dwc3_ep *dep; |
| 782 | struct dwc3 *dwc; |
| 783 | unsigned long flags; |
| 784 | int ret; |
| 785 | |
| 786 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 787 | pr_debug("dwc3: invalid parameters\n"); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
| 791 | if (!desc->wMaxPacketSize) { |
| 792 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 793 | return -EINVAL; |
| 794 | } |
| 795 | |
| 796 | dep = to_dwc3_ep(ep); |
| 797 | dwc = dep->dwc; |
| 798 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 799 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
| 800 | "%s is already enabled\n", |
| 801 | dep->name)) |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 802 | return 0; |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 803 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 804 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 805 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 806 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 812 | { |
| 813 | struct dwc3_ep *dep; |
| 814 | struct dwc3 *dwc; |
| 815 | unsigned long flags; |
| 816 | int ret; |
| 817 | |
| 818 | if (!ep) { |
| 819 | pr_debug("dwc3: invalid parameters\n"); |
| 820 | return -EINVAL; |
| 821 | } |
| 822 | |
| 823 | dep = to_dwc3_ep(ep); |
| 824 | dwc = dep->dwc; |
| 825 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 826 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
| 827 | "%s is already disabled\n", |
| 828 | dep->name)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 829 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 830 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 831 | spin_lock_irqsave(&dwc->lock, flags); |
| 832 | ret = __dwc3_gadget_ep_disable(dep); |
| 833 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 834 | |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | 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] | 839 | gfp_t gfp_flags) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 840 | { |
| 841 | struct dwc3_request *req; |
| 842 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 843 | |
| 844 | req = kzalloc(sizeof(*req), gfp_flags); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 845 | if (!req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 846 | return NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 847 | |
Felipe Balbi | 31a2f5a | 2018-05-07 15:19:31 +0300 | [diff] [blame] | 848 | req->direction = dep->direction; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 849 | req->epnum = dep->number; |
| 850 | req->dep = dep; |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 851 | req->status = DWC3_REQUEST_STATUS_UNKNOWN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 852 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 853 | trace_dwc3_alloc_request(req); |
| 854 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 855 | return &req->request; |
| 856 | } |
| 857 | |
| 858 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 859 | struct usb_request *request) |
| 860 | { |
| 861 | struct dwc3_request *req = to_dwc3_request(request); |
| 862 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 863 | trace_dwc3_free_request(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 864 | kfree(req); |
| 865 | } |
| 866 | |
Felipe Balbi | 4262691 | 2018-04-09 13:01:43 +0300 | [diff] [blame] | 867 | /** |
| 868 | * dwc3_ep_prev_trb - returns the previous TRB in the ring |
| 869 | * @dep: The endpoint with the TRB ring |
| 870 | * @index: The index of the current TRB in the ring |
| 871 | * |
| 872 | * Returns the TRB prior to the one pointed to by the index. If the |
| 873 | * index is 0, we will wrap backwards, skip the link TRB, and return |
| 874 | * the one just before that. |
| 875 | */ |
| 876 | static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) |
| 877 | { |
| 878 | u8 tmp = index; |
| 879 | |
| 880 | if (!tmp) |
| 881 | tmp = DWC3_TRB_NUM - 1; |
| 882 | |
| 883 | return &dep->trb_pool[tmp - 1]; |
| 884 | } |
| 885 | |
| 886 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) |
| 887 | { |
| 888 | struct dwc3_trb *tmp; |
| 889 | u8 trbs_left; |
| 890 | |
| 891 | /* |
| 892 | * If enqueue & dequeue are equal than it is either full or empty. |
| 893 | * |
| 894 | * One way to know for sure is if the TRB right before us has HWO bit |
| 895 | * set or not. If it has, then we're definitely full and can't fit any |
| 896 | * more transfers in our ring. |
| 897 | */ |
| 898 | if (dep->trb_enqueue == dep->trb_dequeue) { |
| 899 | tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 900 | if (tmp->ctrl & DWC3_TRB_CTRL_HWO) |
| 901 | return 0; |
| 902 | |
| 903 | return DWC3_TRB_NUM - 1; |
| 904 | } |
| 905 | |
| 906 | trbs_left = dep->trb_dequeue - dep->trb_enqueue; |
| 907 | trbs_left &= (DWC3_TRB_NUM - 1); |
| 908 | |
| 909 | if (dep->trb_dequeue < dep->trb_enqueue) |
| 910 | trbs_left--; |
| 911 | |
| 912 | return trbs_left; |
| 913 | } |
Felipe Balbi | 2c78c02 | 2016-08-12 13:13:10 +0300 | [diff] [blame] | 914 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 915 | static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, |
| 916 | dma_addr_t dma, unsigned length, unsigned chain, unsigned node, |
| 917 | unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt) |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 918 | { |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 919 | struct dwc3 *dwc = dep->dwc; |
| 920 | struct usb_gadget *gadget = &dwc->gadget; |
| 921 | enum usb_device_speed speed = gadget->speed; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 922 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 923 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 924 | trb->bpl = lower_32_bits(dma); |
| 925 | trb->bph = upper_32_bits(dma); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 926 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 927 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 928 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 929 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 930 | break; |
| 931 | |
| 932 | case USB_ENDPOINT_XFER_ISOC: |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 933 | if (!node) { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 934 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 935 | |
Manu Gautam | 40d829f | 2017-07-19 17:07:10 +0530 | [diff] [blame] | 936 | /* |
| 937 | * USB Specification 2.0 Section 5.9.2 states that: "If |
| 938 | * there is only a single transaction in the microframe, |
| 939 | * only a DATA0 data packet PID is used. If there are |
| 940 | * two transactions per microframe, DATA1 is used for |
| 941 | * the first transaction data packet and DATA0 is used |
| 942 | * for the second transaction data packet. If there are |
| 943 | * three transactions per microframe, DATA2 is used for |
| 944 | * the first transaction data packet, DATA1 is used for |
| 945 | * the second, and DATA0 is used for the third." |
| 946 | * |
| 947 | * IOW, we should satisfy the following cases: |
| 948 | * |
| 949 | * 1) length <= maxpacket |
| 950 | * - DATA0 |
| 951 | * |
| 952 | * 2) maxpacket < length <= (2 * maxpacket) |
| 953 | * - DATA1, DATA0 |
| 954 | * |
| 955 | * 3) (2 * maxpacket) < length <= (3 * maxpacket) |
| 956 | * - DATA2, DATA1, DATA0 |
| 957 | */ |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 958 | if (speed == USB_SPEED_HIGH) { |
| 959 | struct usb_ep *ep = &dep->endpoint; |
Manu Gautam | ec5bb87 | 2017-12-06 12:49:04 +0530 | [diff] [blame] | 960 | unsigned int mult = 2; |
Manu Gautam | 40d829f | 2017-07-19 17:07:10 +0530 | [diff] [blame] | 961 | unsigned int maxp = usb_endpoint_maxp(ep->desc); |
| 962 | |
| 963 | if (length <= (2 * maxp)) |
| 964 | mult--; |
| 965 | |
| 966 | if (length <= maxp) |
| 967 | mult--; |
| 968 | |
| 969 | trb->size |= DWC3_TRB_SIZE_PCM1(mult); |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 970 | } |
| 971 | } else { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 972 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 973 | } |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 974 | |
| 975 | /* always enable Interrupt on Missed ISOC */ |
| 976 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 977 | break; |
| 978 | |
| 979 | case USB_ENDPOINT_XFER_BULK: |
| 980 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 981 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 982 | break; |
| 983 | default: |
| 984 | /* |
| 985 | * This is only possible with faulty memory because we |
| 986 | * checked it already :) |
| 987 | */ |
Felipe Balbi | 0a695d4 | 2016-10-07 11:20:01 +0300 | [diff] [blame] | 988 | dev_WARN(dwc->dev, "Unknown endpoint type %d\n", |
| 989 | usb_endpoint_type(dep->endpoint.desc)); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Tejas Joglekar | 244add8 | 2018-12-10 16:08:13 +0530 | [diff] [blame] | 992 | /* |
| 993 | * Enable Continue on Short Packet |
| 994 | * when endpoint is not a stream capable |
| 995 | */ |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 996 | if (usb_endpoint_dir_out(dep->endpoint.desc)) { |
Tejas Joglekar | 244add8 | 2018-12-10 16:08:13 +0530 | [diff] [blame] | 997 | if (!dep->stream_capable) |
| 998 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
Felipe Balbi | f3af365 | 2013-12-13 14:19:33 -0600 | [diff] [blame] | 999 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1000 | if (short_not_ok) |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1001 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
| 1002 | } |
| 1003 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1004 | if ((!no_interrupt && !chain) || |
Anurag Kumar Vulisha | b7a4fbe | 2018-12-01 16:43:29 +0530 | [diff] [blame] | 1005 | (dwc3_calc_trbs_left(dep) == 1)) |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1006 | trb->ctrl |= DWC3_TRB_CTRL_IOC; |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 1007 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1008 | if (chain) |
| 1009 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
| 1010 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 1011 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1012 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1013 | |
| 1014 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1015 | |
Anurag Kumar Vulisha | b7a4fbe | 2018-12-01 16:43:29 +0530 | [diff] [blame] | 1016 | dwc3_ep_inc_enq(dep); |
| 1017 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1018 | trace_dwc3_prepare_trb(dep, trb); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 1021 | /** |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1022 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 1023 | * @dep: endpoint for which this request is prepared |
| 1024 | * @req: dwc3_request pointer |
| 1025 | * @chain: should this TRB be chained to the next? |
| 1026 | * @node: only for isochronous endpoints. First TRB needs different type. |
| 1027 | */ |
| 1028 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
| 1029 | struct dwc3_request *req, unsigned chain, unsigned node) |
| 1030 | { |
| 1031 | struct dwc3_trb *trb; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1032 | unsigned int length; |
| 1033 | dma_addr_t dma; |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1034 | unsigned stream_id = req->request.stream_id; |
| 1035 | unsigned short_not_ok = req->request.short_not_ok; |
| 1036 | unsigned no_interrupt = req->request.no_interrupt; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1037 | |
| 1038 | if (req->request.num_sgs > 0) { |
| 1039 | length = sg_dma_len(req->start_sg); |
| 1040 | dma = sg_dma_address(req->start_sg); |
| 1041 | } else { |
| 1042 | length = req->request.length; |
| 1043 | dma = req->request.dma; |
| 1044 | } |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1045 | |
| 1046 | trb = &dep->trb_pool[dep->trb_enqueue]; |
| 1047 | |
| 1048 | if (!req->trb) { |
| 1049 | dwc3_gadget_move_started_request(req); |
| 1050 | req->trb = trb; |
| 1051 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 1054 | req->num_trbs++; |
| 1055 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1056 | __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node, |
| 1057 | stream_id, short_not_ok, no_interrupt); |
| 1058 | } |
| 1059 | |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1060 | static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1061 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1062 | { |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1063 | struct scatterlist *sg = req->start_sg; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1064 | struct scatterlist *s; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1065 | int i; |
| 1066 | |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1067 | unsigned int remaining = req->request.num_mapped_sgs |
| 1068 | - req->num_queued_sgs; |
| 1069 | |
| 1070 | for_each_sg(sg, s, remaining, i) { |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1071 | unsigned int length = req->request.length; |
| 1072 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); |
| 1073 | unsigned int rem = length % maxp; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1074 | unsigned chain = true; |
| 1075 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 1076 | if (sg_is_last(s)) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1077 | chain = false; |
| 1078 | |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1079 | if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) { |
| 1080 | struct dwc3 *dwc = dep->dwc; |
| 1081 | struct dwc3_trb *trb; |
| 1082 | |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 1083 | req->needs_extra_trb = true; |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* prepare normal TRB */ |
| 1086 | dwc3_prepare_one_trb(dep, req, true, i); |
| 1087 | |
| 1088 | /* Now prepare one extra TRB to align transfer size */ |
| 1089 | trb = &dep->trb_pool[dep->trb_enqueue]; |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 1090 | req->num_trbs++; |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1091 | __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, |
Felipe Balbi | 2fc6d4b | 2018-08-01 09:37:34 +0300 | [diff] [blame] | 1092 | maxp - rem, false, 1, |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1093 | req->request.stream_id, |
| 1094 | req->request.short_not_ok, |
| 1095 | req->request.no_interrupt); |
| 1096 | } else { |
| 1097 | dwc3_prepare_one_trb(dep, req, chain, i); |
| 1098 | } |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1099 | |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1100 | /* |
| 1101 | * There can be a situation where all sgs in sglist are not |
| 1102 | * queued because of insufficient trb number. To handle this |
| 1103 | * case, update start_sg to next sg to be queued, so that |
| 1104 | * we have free trbs we can continue queuing from where we |
| 1105 | * previously stopped |
| 1106 | */ |
| 1107 | if (chain) |
| 1108 | req->start_sg = sg_next(s); |
| 1109 | |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1110 | req->num_queued_sgs++; |
| 1111 | |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1112 | if (!dwc3_calc_trbs_left(dep)) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1118 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1119 | { |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1120 | unsigned int length = req->request.length; |
| 1121 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); |
| 1122 | unsigned int rem = length % maxp; |
| 1123 | |
| 1124 | if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) { |
| 1125 | struct dwc3 *dwc = dep->dwc; |
| 1126 | struct dwc3_trb *trb; |
| 1127 | |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 1128 | req->needs_extra_trb = true; |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1129 | |
| 1130 | /* prepare normal TRB */ |
| 1131 | dwc3_prepare_one_trb(dep, req, true, 0); |
| 1132 | |
| 1133 | /* Now prepare one extra TRB to align transfer size */ |
| 1134 | trb = &dep->trb_pool[dep->trb_enqueue]; |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 1135 | req->num_trbs++; |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1136 | __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem, |
Felipe Balbi | 2fc6d4b | 2018-08-01 09:37:34 +0300 | [diff] [blame] | 1137 | false, 1, req->request.stream_id, |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1138 | req->request.short_not_ok, |
| 1139 | req->request.no_interrupt); |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 1140 | } else if (req->request.zero && req->request.length && |
Thinh Nguyen | 4ea438d | 2018-07-27 18:52:41 -0700 | [diff] [blame] | 1141 | (IS_ALIGNED(req->request.length, maxp))) { |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 1142 | struct dwc3 *dwc = dep->dwc; |
| 1143 | struct dwc3_trb *trb; |
| 1144 | |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 1145 | req->needs_extra_trb = true; |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 1146 | |
| 1147 | /* prepare normal TRB */ |
| 1148 | dwc3_prepare_one_trb(dep, req, true, 0); |
| 1149 | |
| 1150 | /* Now prepare one extra TRB to handle ZLP */ |
| 1151 | trb = &dep->trb_pool[dep->trb_enqueue]; |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 1152 | req->num_trbs++; |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 1153 | __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0, |
Felipe Balbi | 2fc6d4b | 2018-08-01 09:37:34 +0300 | [diff] [blame] | 1154 | false, 1, req->request.stream_id, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 1155 | req->request.short_not_ok, |
| 1156 | req->request.no_interrupt); |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1157 | } else { |
| 1158 | dwc3_prepare_one_trb(dep, req, false, 0); |
| 1159 | } |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1160 | } |
| 1161 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1162 | /* |
| 1163 | * dwc3_prepare_trbs - setup TRBs from requests |
| 1164 | * @dep: endpoint for which requests are being prepared |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1165 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1166 | * The function goes through the requests list and sets up TRBs for the |
| 1167 | * transfers. The function returns once there are no more TRBs available or |
| 1168 | * it runs out of requests. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1169 | */ |
Felipe Balbi | c423357 | 2016-05-12 14:08:34 +0300 | [diff] [blame] | 1170 | static void dwc3_prepare_trbs(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1171 | { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 1172 | struct dwc3_request *req, *n; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1173 | |
| 1174 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 1175 | |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1176 | /* |
| 1177 | * We can get in a situation where there's a request in the started list |
| 1178 | * but there weren't enough TRBs to fully kick it in the first time |
| 1179 | * around, so it has been waiting for more TRBs to be freed up. |
| 1180 | * |
| 1181 | * In that case, we should check if we have a request with pending_sgs |
| 1182 | * in the started list and prepare TRBs for that request first, |
| 1183 | * otherwise we will prepare TRBs completely out of order and that will |
| 1184 | * break things. |
| 1185 | */ |
| 1186 | list_for_each_entry(req, &dep->started_list, list) { |
| 1187 | if (req->num_pending_sgs > 0) |
| 1188 | dwc3_prepare_one_trb_sg(dep, req); |
| 1189 | |
| 1190 | if (!dwc3_calc_trbs_left(dep)) |
| 1191 | return; |
| 1192 | } |
| 1193 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1194 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1195 | struct dwc3 *dwc = dep->dwc; |
| 1196 | int ret; |
| 1197 | |
| 1198 | ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, |
| 1199 | dep->direction); |
| 1200 | if (ret) |
| 1201 | return; |
| 1202 | |
| 1203 | req->sg = req->request.sg; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1204 | req->start_sg = req->sg; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1205 | req->num_queued_sgs = 0; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1206 | req->num_pending_sgs = req->request.num_mapped_sgs; |
| 1207 | |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 1208 | if (req->num_pending_sgs > 0) |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1209 | dwc3_prepare_one_trb_sg(dep, req); |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1210 | else |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1211 | dwc3_prepare_one_trb_linear(dep, req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1212 | |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1213 | if (!dwc3_calc_trbs_left(dep)) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1214 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1215 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1216 | } |
| 1217 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1218 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1219 | { |
| 1220 | struct dwc3_gadget_ep_cmd_params params; |
| 1221 | struct dwc3_request *req; |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1222 | int starting; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1223 | int ret; |
| 1224 | u32 cmd; |
| 1225 | |
Felipe Balbi | ccb94eb | 2017-09-05 14:28:46 +0300 | [diff] [blame] | 1226 | if (!dwc3_calc_trbs_left(dep)) |
| 1227 | return 0; |
| 1228 | |
Felipe Balbi | 1912cbc | 2018-03-29 11:08:46 +0300 | [diff] [blame] | 1229 | starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1230 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1231 | dwc3_prepare_trbs(dep); |
| 1232 | req = next_request(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1233 | if (!req) { |
| 1234 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1239 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1240 | if (starting) { |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1241 | params.param0 = upper_32_bits(req->trb_dma); |
| 1242 | params.param1 = lower_32_bits(req->trb_dma); |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1243 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 1244 | |
Anurag Kumar Vulisha | a735180 | 2018-12-01 16:43:25 +0530 | [diff] [blame] | 1245 | if (dep->stream_capable) |
| 1246 | cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); |
| 1247 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1248 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 1249 | cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1250 | } else { |
Felipe Balbi | b6b1c6d | 2016-05-30 13:29:35 +0300 | [diff] [blame] | 1251 | cmd = DWC3_DEPCMD_UPDATETRANSFER | |
| 1252 | DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1253 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1254 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1255 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1256 | if (ret < 0) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1257 | /* |
| 1258 | * FIXME we need to iterate over the list of requests |
| 1259 | * here and stop, unmap, free and del each of the linked |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1260 | * requests instead of what we do now. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1261 | */ |
Janusz Dziedzic | ce3fc8b | 2016-11-09 11:01:32 +0100 | [diff] [blame] | 1262 | if (req->trb) |
| 1263 | memset(req->trb, 0, sizeof(struct dwc3_trb)); |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 1264 | dwc3_gadget_del_and_unmap_request(dep, req, ret); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1265 | return ret; |
| 1266 | } |
| 1267 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
| 1270 | |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 1271 | static int __dwc3_gadget_get_frame(struct dwc3 *dwc) |
| 1272 | { |
| 1273 | u32 reg; |
| 1274 | |
| 1275 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1276 | return DWC3_DSTS_SOFFN(reg); |
| 1277 | } |
| 1278 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1279 | /** |
| 1280 | * dwc3_gadget_start_isoc_quirk - workaround invalid frame number |
| 1281 | * @dep: isoc endpoint |
| 1282 | * |
| 1283 | * This function tests for the correct combination of BIT[15:14] from the 16-bit |
| 1284 | * microframe number reported by the XferNotReady event for the future frame |
| 1285 | * number to start the isoc transfer. |
| 1286 | * |
| 1287 | * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed |
| 1288 | * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the |
| 1289 | * XferNotReady event are invalid. The driver uses this number to schedule the |
| 1290 | * isochronous transfer and passes it to the START TRANSFER command. Because |
| 1291 | * this number is invalid, the command may fail. If BIT[15:14] matches the |
| 1292 | * internal 16-bit microframe, the START TRANSFER command will pass and the |
| 1293 | * transfer will start at the scheduled time, if it is off by 1, the command |
| 1294 | * will still pass, but the transfer will start 2 seconds in the future. For all |
| 1295 | * other conditions, the START TRANSFER command will fail with bus-expiry. |
| 1296 | * |
| 1297 | * In order to workaround this issue, we can test for the correct combination of |
| 1298 | * BIT[15:14] by sending START TRANSFER commands with different values of |
| 1299 | * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart |
| 1300 | * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. |
| 1301 | * As the result, within the 4 possible combinations for BIT[15:14], there will |
| 1302 | * be 2 successful and 2 failure START COMMAND status. One of the 2 successful |
| 1303 | * command status will result in a 2-second delay start. The smaller BIT[15:14] |
| 1304 | * value is the correct combination. |
| 1305 | * |
| 1306 | * Since there are only 4 outcomes and the results are ordered, we can simply |
| 1307 | * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to |
| 1308 | * deduce the smaller successful combination. |
| 1309 | * |
| 1310 | * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 |
| 1311 | * of BIT[15:14]. The correct combination is as follow: |
| 1312 | * |
| 1313 | * if test0 fails and test1 passes, BIT[15:14] is 'b01 |
| 1314 | * if test0 fails and test1 fails, BIT[15:14] is 'b10 |
| 1315 | * if test0 passes and test1 fails, BIT[15:14] is 'b11 |
| 1316 | * if test0 passes and test1 passes, BIT[15:14] is 'b00 |
| 1317 | * |
| 1318 | * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN |
| 1319 | * endpoints. |
| 1320 | */ |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1321 | static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1322 | { |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1323 | int cmd_status = 0; |
| 1324 | bool test0; |
| 1325 | bool test1; |
| 1326 | |
| 1327 | while (dep->combo_num < 2) { |
| 1328 | struct dwc3_gadget_ep_cmd_params params; |
| 1329 | u32 test_frame_number; |
| 1330 | u32 cmd; |
| 1331 | |
| 1332 | /* |
| 1333 | * Check if we can start isoc transfer on the next interval or |
| 1334 | * 4 uframes in the future with BIT[15:14] as dep->combo_num |
| 1335 | */ |
| 1336 | test_frame_number = dep->frame_number & 0x3fff; |
| 1337 | test_frame_number |= dep->combo_num << 14; |
| 1338 | test_frame_number += max_t(u32, 4, dep->interval); |
| 1339 | |
| 1340 | params.param0 = upper_32_bits(dep->dwc->bounce_addr); |
| 1341 | params.param1 = lower_32_bits(dep->dwc->bounce_addr); |
| 1342 | |
| 1343 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 1344 | cmd |= DWC3_DEPCMD_PARAM(test_frame_number); |
| 1345 | cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 1346 | |
| 1347 | /* Redo if some other failure beside bus-expiry is received */ |
| 1348 | if (cmd_status && cmd_status != -EAGAIN) { |
| 1349 | dep->start_cmd_status = 0; |
| 1350 | dep->combo_num = 0; |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1351 | return 0; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /* Store the first test status */ |
| 1355 | if (dep->combo_num == 0) |
| 1356 | dep->start_cmd_status = cmd_status; |
| 1357 | |
| 1358 | dep->combo_num++; |
| 1359 | |
| 1360 | /* |
| 1361 | * End the transfer if the START_TRANSFER command is successful |
| 1362 | * to wait for the next XferNotReady to test the command again |
| 1363 | */ |
| 1364 | if (cmd_status == 0) { |
| 1365 | dwc3_stop_active_transfer(dep, true); |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1366 | return 0; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1367 | } |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1368 | } |
| 1369 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1370 | /* test0 and test1 are both completed at this point */ |
| 1371 | test0 = (dep->start_cmd_status == 0); |
| 1372 | test1 = (cmd_status == 0); |
| 1373 | |
| 1374 | if (!test0 && test1) |
| 1375 | dep->combo_num = 1; |
| 1376 | else if (!test0 && !test1) |
| 1377 | dep->combo_num = 2; |
| 1378 | else if (test0 && !test1) |
| 1379 | dep->combo_num = 3; |
| 1380 | else if (test0 && test1) |
| 1381 | dep->combo_num = 0; |
| 1382 | |
| 1383 | dep->frame_number &= 0x3fff; |
| 1384 | dep->frame_number |= dep->combo_num << 14; |
| 1385 | dep->frame_number += max_t(u32, 4, dep->interval); |
| 1386 | |
| 1387 | /* Reinitialize test variables */ |
| 1388 | dep->start_cmd_status = 0; |
| 1389 | dep->combo_num = 0; |
| 1390 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1391 | return __dwc3_gadget_kick_transfer(dep); |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1394 | static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1395 | { |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1396 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1397 | int ret; |
| 1398 | int i; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1399 | |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1400 | if (list_empty(&dep->pending_list)) { |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1401 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1402 | return -EAGAIN; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1403 | } |
| 1404 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1405 | if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) && |
| 1406 | (dwc->revision <= DWC3_USB31_REVISION_160A || |
| 1407 | (dwc->revision == DWC3_USB31_REVISION_170A && |
| 1408 | dwc->version_type >= DWC31_VERSIONTYPE_EA01 && |
| 1409 | dwc->version_type <= DWC31_VERSIONTYPE_EA06))) { |
| 1410 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1411 | if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction) |
| 1412 | return dwc3_gadget_start_isoc_quirk(dep); |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1415 | for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { |
| 1416 | dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1); |
| 1417 | |
| 1418 | ret = __dwc3_gadget_kick_transfer(dep); |
| 1419 | if (ret != -EAGAIN) |
| 1420 | break; |
| 1421 | } |
| 1422 | |
| 1423 | return ret; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1424 | } |
| 1425 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1426 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1427 | { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1428 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1429 | |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1430 | if (!dep->endpoint.desc) { |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 1431 | dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n", |
| 1432 | dep->name); |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1433 | return -ESHUTDOWN; |
| 1434 | } |
| 1435 | |
Felipe Balbi | 04fb365 | 2017-05-17 15:57:45 +0300 | [diff] [blame] | 1436 | if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", |
| 1437 | &req->request, req->dep->name)) |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1438 | return -EINVAL; |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1439 | |
Felipe Balbi | b2b6d60 | 2019-01-11 12:58:52 +0200 | [diff] [blame^] | 1440 | if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, |
| 1441 | "%s: request %pK already in flight\n", |
| 1442 | dep->name, &req->request)) |
| 1443 | return -EINVAL; |
| 1444 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1445 | pm_runtime_get(dwc->dev); |
| 1446 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1447 | req->request.actual = 0; |
| 1448 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1449 | |
Felipe Balbi | fe84f52 | 2015-09-01 09:01:38 -0500 | [diff] [blame] | 1450 | trace_dwc3_ep_queue(req); |
| 1451 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1452 | list_add_tail(&req->list, &dep->pending_list); |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 1453 | req->status = DWC3_REQUEST_STATUS_QUEUED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1454 | |
Felipe Balbi | d889c23 | 2016-09-29 15:44:29 +0300 | [diff] [blame] | 1455 | /* |
| 1456 | * NOTICE: Isochronous endpoints should NEVER be prestarted. We must |
| 1457 | * wait for a XferNotReady event so we will know what's the current |
| 1458 | * (micro-)frame number. |
| 1459 | * |
| 1460 | * Without this trick, we are very, very likely gonna get Bus Expiry |
| 1461 | * errors which will force us issue EndTransfer command. |
| 1462 | */ |
| 1463 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1464 | if (!(dep->flags & DWC3_EP_PENDING_REQUEST) && |
| 1465 | !(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Roger Quadros | f1d6826 | 2017-04-21 15:58:08 +0300 | [diff] [blame] | 1466 | return 0; |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1467 | |
| 1468 | if ((dep->flags & DWC3_EP_PENDING_REQUEST)) { |
| 1469 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1470 | return __dwc3_gadget_start_isoc(dep); |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1471 | } |
Felipe Balbi | 08a36b5 | 2016-08-11 14:27:52 +0300 | [diff] [blame] | 1472 | } |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1473 | } |
| 1474 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1475 | return __dwc3_gadget_kick_transfer(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1479 | gfp_t gfp_flags) |
| 1480 | { |
| 1481 | struct dwc3_request *req = to_dwc3_request(request); |
| 1482 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1483 | struct dwc3 *dwc = dep->dwc; |
| 1484 | |
| 1485 | unsigned long flags; |
| 1486 | |
| 1487 | int ret; |
| 1488 | |
Zhuang Jin Can | fdee4eb | 2014-09-03 14:26:34 +0800 | [diff] [blame] | 1489 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1490 | ret = __dwc3_gadget_ep_queue(dep, req); |
| 1491 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1492 | |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1496 | static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1497 | { |
| 1498 | int i; |
| 1499 | |
| 1500 | /* |
| 1501 | * If request was already started, this means we had to |
| 1502 | * stop the transfer. With that we also need to ignore |
| 1503 | * all TRBs used by the request, however TRBs can only |
| 1504 | * be modified after completion of END_TRANSFER |
| 1505 | * command. So what we do here is that we wait for |
| 1506 | * END_TRANSFER completion and only after that, we jump |
| 1507 | * over TRBs by clearing HWO and incrementing dequeue |
| 1508 | * pointer. |
| 1509 | */ |
| 1510 | for (i = 0; i < req->num_trbs; i++) { |
| 1511 | struct dwc3_trb *trb; |
| 1512 | |
| 1513 | trb = req->trb + i; |
| 1514 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 1515 | dwc3_ep_inc_deq(dep); |
| 1516 | } |
| 1517 | } |
| 1518 | |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1519 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) |
| 1520 | { |
| 1521 | struct dwc3_request *req; |
| 1522 | struct dwc3_request *tmp; |
| 1523 | |
| 1524 | list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) { |
| 1525 | dwc3_gadget_ep_skip_trbs(dep, req); |
| 1526 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1530 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1531 | struct usb_request *request) |
| 1532 | { |
| 1533 | struct dwc3_request *req = to_dwc3_request(request); |
| 1534 | struct dwc3_request *r = NULL; |
| 1535 | |
| 1536 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1537 | struct dwc3 *dwc = dep->dwc; |
| 1538 | |
| 1539 | unsigned long flags; |
| 1540 | int ret = 0; |
| 1541 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1542 | trace_dwc3_ep_dequeue(req); |
| 1543 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1544 | spin_lock_irqsave(&dwc->lock, flags); |
| 1545 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1546 | list_for_each_entry(r, &dep->pending_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1547 | if (r == req) |
| 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | if (r != req) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1552 | list_for_each_entry(r, &dep->started_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1553 | if (r == req) |
| 1554 | break; |
| 1555 | } |
| 1556 | if (r == req) { |
| 1557 | /* wait until it is processed */ |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 1558 | dwc3_stop_active_transfer(dep, true); |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1559 | |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1560 | if (!r->trb) |
Mayank Rana | 0564536 | 2018-03-23 10:05:33 -0700 | [diff] [blame] | 1561 | goto out0; |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1562 | |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1563 | dwc3_gadget_move_cancelled_request(req); |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1564 | goto out0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1565 | } |
Felipe Balbi | 04fb365 | 2017-05-17 15:57:45 +0300 | [diff] [blame] | 1566 | dev_err(dwc->dev, "request %pK was not queued to %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1567 | request, ep->name); |
| 1568 | ret = -EINVAL; |
| 1569 | goto out0; |
| 1570 | } |
| 1571 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1572 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1573 | |
| 1574 | out0: |
| 1575 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1576 | |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1580 | 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] | 1581 | { |
| 1582 | struct dwc3_gadget_ep_cmd_params params; |
| 1583 | struct dwc3 *dwc = dep->dwc; |
| 1584 | int ret; |
| 1585 | |
Felipe Balbi | 5ad02fb | 2014-09-24 10:48:26 -0500 | [diff] [blame] | 1586 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1587 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 1588 | return -EINVAL; |
| 1589 | } |
| 1590 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1591 | memset(¶ms, 0x00, sizeof(params)); |
| 1592 | |
| 1593 | if (value) { |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 1594 | struct dwc3_trb *trb; |
| 1595 | |
| 1596 | unsigned transfer_in_flight; |
| 1597 | unsigned started; |
| 1598 | |
| 1599 | if (dep->number > 1) |
| 1600 | trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 1601 | else |
| 1602 | trb = &dwc->ep0_trb[dep->trb_enqueue]; |
| 1603 | |
| 1604 | transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; |
| 1605 | started = !list_empty(&dep->started_list); |
| 1606 | |
| 1607 | if (!protocol && ((dep->direction && transfer_in_flight) || |
| 1608 | (!dep->direction && started))) { |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1609 | return -EAGAIN; |
| 1610 | } |
| 1611 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1612 | ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, |
| 1613 | ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1614 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1615 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1616 | dep->name); |
| 1617 | else |
| 1618 | dep->flags |= DWC3_EP_STALL; |
| 1619 | } else { |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1620 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 1621 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1622 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1623 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1624 | dep->name); |
| 1625 | else |
Alan Stern | a535d81 | 2013-11-01 12:05:12 -0400 | [diff] [blame] | 1626 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1627 | } |
Paul Zimmerman | 5275455 | 2011-09-30 10:58:44 +0300 | [diff] [blame] | 1628 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1629 | return ret; |
| 1630 | } |
| 1631 | |
| 1632 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 1633 | { |
| 1634 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1635 | struct dwc3 *dwc = dep->dwc; |
| 1636 | |
| 1637 | unsigned long flags; |
| 1638 | |
| 1639 | int ret; |
| 1640 | |
| 1641 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1642 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1643 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1644 | |
| 1645 | return ret; |
| 1646 | } |
| 1647 | |
| 1648 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 1649 | { |
| 1650 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1651 | struct dwc3 *dwc = dep->dwc; |
| 1652 | unsigned long flags; |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1653 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1654 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1655 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1656 | dep->flags |= DWC3_EP_WEDGE; |
| 1657 | |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1658 | if (dep->number == 0 || dep->number == 1) |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1659 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1660 | else |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1661 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1662 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1663 | |
| 1664 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | /* -------------------------------------------------------------------------- */ |
| 1668 | |
| 1669 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 1670 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1671 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1672 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1673 | }; |
| 1674 | |
| 1675 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 1676 | .enable = dwc3_gadget_ep0_enable, |
| 1677 | .disable = dwc3_gadget_ep0_disable, |
| 1678 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1679 | .free_request = dwc3_gadget_ep_free_request, |
| 1680 | .queue = dwc3_gadget_ep0_queue, |
| 1681 | .dequeue = dwc3_gadget_ep_dequeue, |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1682 | .set_halt = dwc3_gadget_ep0_set_halt, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1683 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1684 | }; |
| 1685 | |
| 1686 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 1687 | .enable = dwc3_gadget_ep_enable, |
| 1688 | .disable = dwc3_gadget_ep_disable, |
| 1689 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1690 | .free_request = dwc3_gadget_ep_free_request, |
| 1691 | .queue = dwc3_gadget_ep_queue, |
| 1692 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1693 | .set_halt = dwc3_gadget_ep_set_halt, |
| 1694 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1695 | }; |
| 1696 | |
| 1697 | /* -------------------------------------------------------------------------- */ |
| 1698 | |
| 1699 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 1700 | { |
| 1701 | struct dwc3 *dwc = gadget_to_dwc(g); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1702 | |
Felipe Balbi | 6cb2e4e3 | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 1703 | return __dwc3_gadget_get_frame(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1704 | } |
| 1705 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1706 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1707 | { |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1708 | int retries; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1709 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1710 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1711 | u32 reg; |
| 1712 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1713 | u8 link_state; |
| 1714 | u8 speed; |
| 1715 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1716 | /* |
| 1717 | * According to the Databook Remote wakeup request should |
| 1718 | * be issued only when the device is in early suspend state. |
| 1719 | * |
| 1720 | * We can check that via USB Link State bits in DSTS register. |
| 1721 | */ |
| 1722 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1723 | |
| 1724 | speed = reg & DWC3_DSTS_CONNECTSPD; |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 1725 | if ((speed == DWC3_DSTS_SUPERSPEED) || |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 1726 | (speed == DWC3_DSTS_SUPERSPEED_PLUS)) |
Felipe Balbi | 6b74289 | 2016-05-13 10:19:42 +0300 | [diff] [blame] | 1727 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1728 | |
| 1729 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 1730 | |
| 1731 | switch (link_state) { |
| 1732 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 1733 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
| 1734 | break; |
| 1735 | default: |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1736 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1737 | } |
| 1738 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 1739 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 1740 | if (ret < 0) { |
| 1741 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1742 | return ret; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 1743 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1744 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1745 | /* Recent versions do this automatically */ |
| 1746 | if (dwc->revision < DWC3_REVISION_194A) { |
| 1747 | /* write zeroes to Link Change Request */ |
Felipe Balbi | fcc023c | 2012-05-24 10:27:56 +0300 | [diff] [blame] | 1748 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1749 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 1750 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1751 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1752 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1753 | /* poll until Link State changes to ON */ |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1754 | retries = 20000; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1755 | |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1756 | while (retries--) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1757 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1758 | |
| 1759 | /* in HS, means ON */ |
| 1760 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 1761 | break; |
| 1762 | } |
| 1763 | |
| 1764 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 1765 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1766 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1767 | } |
| 1768 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 1773 | { |
| 1774 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1775 | unsigned long flags; |
| 1776 | int ret; |
| 1777 | |
| 1778 | spin_lock_irqsave(&dwc->lock, flags); |
| 1779 | ret = __dwc3_gadget_wakeup(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1780 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1781 | |
| 1782 | return ret; |
| 1783 | } |
| 1784 | |
| 1785 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 1786 | int is_selfpowered) |
| 1787 | { |
| 1788 | struct dwc3 *dwc = gadget_to_dwc(g); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1789 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1790 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1791 | spin_lock_irqsave(&dwc->lock, flags); |
Peter Chen | bcdea50 | 2015-01-28 16:32:40 +0800 | [diff] [blame] | 1792 | g->is_selfpowered = !!is_selfpowered; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1793 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1794 | |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1798 | 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] | 1799 | { |
| 1800 | u32 reg; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 1801 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1802 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1803 | if (pm_runtime_suspended(dwc->dev)) |
| 1804 | return 0; |
| 1805 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1806 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1807 | if (is_on) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1808 | if (dwc->revision <= DWC3_REVISION_187A) { |
| 1809 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 1810 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 1811 | } |
| 1812 | |
| 1813 | if (dwc->revision >= DWC3_REVISION_194A) |
| 1814 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1815 | reg |= DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1816 | |
| 1817 | if (dwc->has_hibernation) |
| 1818 | reg |= DWC3_DCTL_KEEP_CONNECT; |
| 1819 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 1820 | dwc->pullups_connected = true; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1821 | } else { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1822 | reg &= ~DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1823 | |
| 1824 | if (dwc->has_hibernation && !suspend) |
| 1825 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1826 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 1827 | dwc->pullups_connected = false; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1828 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1829 | |
| 1830 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1831 | |
| 1832 | do { |
| 1833 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
Felipe Balbi | b6d4e16 | 2016-06-09 16:47:05 +0300 | [diff] [blame] | 1834 | reg &= DWC3_DSTS_DEVCTRLHLT; |
| 1835 | } while (--timeout && !(!is_on ^ !reg)); |
Felipe Balbi | f2df679 | 2016-06-09 16:31:34 +0300 | [diff] [blame] | 1836 | |
| 1837 | if (!timeout) |
| 1838 | return -ETIMEDOUT; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1839 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1840 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 1844 | { |
| 1845 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1846 | unsigned long flags; |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1847 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1848 | |
| 1849 | is_on = !!is_on; |
| 1850 | |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 1851 | /* |
| 1852 | * Per databook, when we want to stop the gadget, if a control transfer |
| 1853 | * is still in process, complete it and get the core into setup phase. |
| 1854 | */ |
| 1855 | if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) { |
| 1856 | reinit_completion(&dwc->ep0_in_setup); |
| 1857 | |
| 1858 | ret = wait_for_completion_timeout(&dwc->ep0_in_setup, |
| 1859 | msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); |
| 1860 | if (ret == 0) { |
| 1861 | dev_err(dwc->dev, "timed out waiting for SETUP phase\n"); |
| 1862 | return -ETIMEDOUT; |
| 1863 | } |
| 1864 | } |
| 1865 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1866 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1867 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1868 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1869 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1870 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1871 | } |
| 1872 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1873 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
| 1874 | { |
| 1875 | u32 reg; |
| 1876 | |
| 1877 | /* Enable all but Start and End of Frame IRQs */ |
| 1878 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | |
| 1879 | DWC3_DEVTEN_EVNTOVERFLOWEN | |
| 1880 | DWC3_DEVTEN_CMDCMPLTEN | |
| 1881 | DWC3_DEVTEN_ERRTICERREN | |
| 1882 | DWC3_DEVTEN_WKUPEVTEN | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1883 | DWC3_DEVTEN_CONNECTDONEEN | |
| 1884 | DWC3_DEVTEN_USBRSTEN | |
| 1885 | DWC3_DEVTEN_DISCONNEVTEN); |
| 1886 | |
Felipe Balbi | 799e9dc | 2016-09-23 11:20:40 +0300 | [diff] [blame] | 1887 | if (dwc->revision < DWC3_REVISION_250A) |
| 1888 | reg |= DWC3_DEVTEN_ULSTCNGEN; |
| 1889 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1890 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1891 | } |
| 1892 | |
| 1893 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) |
| 1894 | { |
| 1895 | /* mask all interrupts */ |
| 1896 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 1897 | } |
| 1898 | |
| 1899 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 1900 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1901 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 1902 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 1903 | * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG |
| 1904 | * @dwc: pointer to our context structure |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 1905 | * |
| 1906 | * The following looks like complex but it's actually very simple. In order to |
| 1907 | * calculate the number of packets we can burst at once on OUT transfers, we're |
| 1908 | * gonna use RxFIFO size. |
| 1909 | * |
| 1910 | * To calculate RxFIFO size we need two numbers: |
| 1911 | * MDWIDTH = size, in bits, of the internal memory bus |
| 1912 | * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) |
| 1913 | * |
| 1914 | * Given these two numbers, the formula is simple: |
| 1915 | * |
| 1916 | * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; |
| 1917 | * |
| 1918 | * 24 bytes is for 3x SETUP packets |
| 1919 | * 16 bytes is a clock domain crossing tolerance |
| 1920 | * |
| 1921 | * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; |
| 1922 | */ |
| 1923 | static void dwc3_gadget_setup_nump(struct dwc3 *dwc) |
| 1924 | { |
| 1925 | u32 ram2_depth; |
| 1926 | u32 mdwidth; |
| 1927 | u32 nump; |
| 1928 | u32 reg; |
| 1929 | |
| 1930 | ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); |
| 1931 | mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0); |
| 1932 | |
| 1933 | nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; |
| 1934 | nump = min_t(u32, nump, 16); |
| 1935 | |
| 1936 | /* update NumP */ |
| 1937 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1938 | reg &= ~DWC3_DCFG_NUMP_MASK; |
| 1939 | reg |= nump << DWC3_DCFG_NUMP_SHIFT; |
| 1940 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1941 | } |
| 1942 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 1943 | static int __dwc3_gadget_start(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1944 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1945 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1946 | int ret = 0; |
| 1947 | u32 reg; |
| 1948 | |
John Youn | cf40b86 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 1949 | /* |
| 1950 | * Use IMOD if enabled via dwc->imod_interval. Otherwise, if |
| 1951 | * the core supports IMOD, disable it. |
| 1952 | */ |
| 1953 | if (dwc->imod_interval) { |
| 1954 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); |
| 1955 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); |
| 1956 | } else if (dwc3_has_imod(dwc)) { |
| 1957 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); |
| 1958 | } |
| 1959 | |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 1960 | /* |
| 1961 | * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP |
| 1962 | * field instead of letting dwc3 itself calculate that automatically. |
| 1963 | * |
| 1964 | * This way, we maximize the chances that we'll be able to get several |
| 1965 | * bursts of data without going through any sort of endpoint throttling. |
| 1966 | */ |
| 1967 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); |
Thinh Nguyen | 01b0e2c | 2018-03-16 15:34:13 -0700 | [diff] [blame] | 1968 | if (dwc3_is_usb31(dwc)) |
| 1969 | reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; |
| 1970 | else |
| 1971 | reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; |
| 1972 | |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 1973 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); |
| 1974 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 1975 | dwc3_gadget_setup_nump(dwc); |
| 1976 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1977 | /* Start with SuperSpeed Default */ |
| 1978 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 1979 | |
| 1980 | dep = dwc->eps[0]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 1981 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1982 | if (ret) { |
| 1983 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 1984 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | dep = dwc->eps[1]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 1988 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1989 | if (ret) { |
| 1990 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 1991 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | /* begin to receive SETUP packets */ |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1995 | dwc->ep0state = EP0_SETUP_PHASE; |
Zeng Tao | 88b1bb1 | 2018-12-26 19:22:00 +0800 | [diff] [blame] | 1996 | dwc->link_state = DWC3_LINK_STATE_SS_DIS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1997 | dwc3_ep0_out_start(dwc); |
| 1998 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1999 | dwc3_gadget_enable_irq(dwc); |
| 2000 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2001 | return 0; |
| 2002 | |
| 2003 | err1: |
| 2004 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2005 | |
| 2006 | err0: |
| 2007 | return ret; |
| 2008 | } |
| 2009 | |
| 2010 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 2011 | struct usb_gadget_driver *driver) |
| 2012 | { |
| 2013 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2014 | unsigned long flags; |
| 2015 | int ret = 0; |
| 2016 | int irq; |
| 2017 | |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 2018 | irq = dwc->irq_gadget; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2019 | ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, |
| 2020 | IRQF_SHARED, "dwc3", dwc->ev_buf); |
| 2021 | if (ret) { |
| 2022 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", |
| 2023 | irq, ret); |
| 2024 | goto err0; |
| 2025 | } |
| 2026 | |
| 2027 | spin_lock_irqsave(&dwc->lock, flags); |
| 2028 | if (dwc->gadget_driver) { |
| 2029 | dev_err(dwc->dev, "%s is already bound to %s\n", |
| 2030 | dwc->gadget.name, |
| 2031 | dwc->gadget_driver->driver.name); |
| 2032 | ret = -EBUSY; |
| 2033 | goto err1; |
| 2034 | } |
| 2035 | |
| 2036 | dwc->gadget_driver = driver; |
| 2037 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2038 | if (pm_runtime_active(dwc->dev)) |
| 2039 | __dwc3_gadget_start(dwc); |
| 2040 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2041 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2042 | |
| 2043 | return 0; |
| 2044 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2045 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2046 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2047 | free_irq(irq, dwc); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2048 | |
| 2049 | err0: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2050 | return ret; |
| 2051 | } |
| 2052 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2053 | static void __dwc3_gadget_stop(struct dwc3 *dwc) |
| 2054 | { |
| 2055 | dwc3_gadget_disable_irq(dwc); |
| 2056 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2057 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 2058 | } |
| 2059 | |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 2060 | static int dwc3_gadget_stop(struct usb_gadget *g) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2061 | { |
| 2062 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2063 | unsigned long flags; |
| 2064 | |
| 2065 | spin_lock_irqsave(&dwc->lock, flags); |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2066 | |
| 2067 | if (pm_runtime_suspended(dwc->dev)) |
| 2068 | goto out; |
| 2069 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2070 | __dwc3_gadget_stop(dwc); |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2071 | |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2072 | out: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2073 | dwc->gadget_driver = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2074 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2075 | |
Felipe Balbi | 3f308d1 | 2016-05-16 14:17:06 +0300 | [diff] [blame] | 2076 | free_irq(dwc->irq_gadget, dwc->ev_buf); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2077 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2078 | return 0; |
| 2079 | } |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2080 | |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2081 | static void dwc3_gadget_set_speed(struct usb_gadget *g, |
| 2082 | enum usb_device_speed speed) |
| 2083 | { |
| 2084 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2085 | unsigned long flags; |
| 2086 | u32 reg; |
| 2087 | |
| 2088 | spin_lock_irqsave(&dwc->lock, flags); |
| 2089 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2090 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
| 2091 | |
| 2092 | /* |
| 2093 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 2094 | * which would cause metastability state on Run/Stop |
| 2095 | * bit if we try to force the IP to USB2-only mode. |
| 2096 | * |
| 2097 | * Because of that, we cannot configure the IP to any |
| 2098 | * speed other than the SuperSpeed |
| 2099 | * |
| 2100 | * Refers to: |
| 2101 | * |
| 2102 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 2103 | * USB 2.0 Mode |
| 2104 | */ |
Roger Quadros | 42bf02e | 2017-10-31 15:11:55 +0200 | [diff] [blame] | 2105 | if (dwc->revision < DWC3_REVISION_220A && |
| 2106 | !dwc->dis_metastability_quirk) { |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2107 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2108 | } else { |
| 2109 | switch (speed) { |
| 2110 | case USB_SPEED_LOW: |
| 2111 | reg |= DWC3_DCFG_LOWSPEED; |
| 2112 | break; |
| 2113 | case USB_SPEED_FULL: |
| 2114 | reg |= DWC3_DCFG_FULLSPEED; |
| 2115 | break; |
| 2116 | case USB_SPEED_HIGH: |
| 2117 | reg |= DWC3_DCFG_HIGHSPEED; |
| 2118 | break; |
| 2119 | case USB_SPEED_SUPER: |
| 2120 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2121 | break; |
| 2122 | case USB_SPEED_SUPER_PLUS: |
Thinh Nguyen | 2f3090c | 2018-03-16 15:35:57 -0700 | [diff] [blame] | 2123 | if (dwc3_is_usb31(dwc)) |
| 2124 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
| 2125 | else |
| 2126 | reg |= DWC3_DCFG_SUPERSPEED; |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2127 | break; |
| 2128 | default: |
| 2129 | dev_err(dwc->dev, "invalid speed (%d)\n", speed); |
| 2130 | |
| 2131 | if (dwc->revision & DWC3_REVISION_IS_DWC31) |
| 2132 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
| 2133 | else |
| 2134 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2135 | } |
| 2136 | } |
| 2137 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2138 | |
| 2139 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2140 | } |
| 2141 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2142 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 2143 | .get_frame = dwc3_gadget_get_frame, |
| 2144 | .wakeup = dwc3_gadget_wakeup, |
| 2145 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
| 2146 | .pullup = dwc3_gadget_pullup, |
| 2147 | .udc_start = dwc3_gadget_start, |
| 2148 | .udc_stop = dwc3_gadget_stop, |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2149 | .udc_set_speed = dwc3_gadget_set_speed, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2150 | }; |
| 2151 | |
| 2152 | /* -------------------------------------------------------------------------- */ |
| 2153 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2154 | static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) |
| 2155 | { |
| 2156 | struct dwc3 *dwc = dep->dwc; |
| 2157 | |
| 2158 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
| 2159 | dep->endpoint.maxburst = 1; |
| 2160 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 2161 | if (!dep->direction) |
| 2162 | dwc->gadget.ep0 = &dep->endpoint; |
| 2163 | |
| 2164 | dep->endpoint.caps.type_control = true; |
| 2165 | |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) |
| 2170 | { |
| 2171 | struct dwc3 *dwc = dep->dwc; |
| 2172 | int mdwidth; |
| 2173 | int kbytes; |
| 2174 | int size; |
| 2175 | |
| 2176 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
| 2177 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 2178 | mdwidth /= 8; |
| 2179 | |
| 2180 | size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); |
| 2181 | if (dwc3_is_usb31(dwc)) |
| 2182 | size = DWC31_GTXFIFOSIZ_TXFDEF(size); |
| 2183 | else |
| 2184 | size = DWC3_GTXFIFOSIZ_TXFDEF(size); |
| 2185 | |
| 2186 | /* FIFO Depth is in MDWDITH bytes. Multiply */ |
| 2187 | size *= mdwidth; |
| 2188 | |
| 2189 | kbytes = size / 1024; |
| 2190 | if (kbytes == 0) |
| 2191 | kbytes = 1; |
| 2192 | |
| 2193 | /* |
| 2194 | * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for |
| 2195 | * internal overhead. We don't really know how these are used, |
| 2196 | * but documentation say it exists. |
| 2197 | */ |
| 2198 | size -= mdwidth * (kbytes + 1); |
| 2199 | size /= kbytes; |
| 2200 | |
| 2201 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); |
| 2202 | |
| 2203 | dep->endpoint.max_streams = 15; |
| 2204 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2205 | list_add_tail(&dep->endpoint.ep_list, |
| 2206 | &dwc->gadget.ep_list); |
| 2207 | dep->endpoint.caps.type_iso = true; |
| 2208 | dep->endpoint.caps.type_bulk = true; |
| 2209 | dep->endpoint.caps.type_int = true; |
| 2210 | |
| 2211 | return dwc3_alloc_trb_pool(dep); |
| 2212 | } |
| 2213 | |
| 2214 | static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) |
| 2215 | { |
| 2216 | struct dwc3 *dwc = dep->dwc; |
| 2217 | |
| 2218 | usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); |
| 2219 | dep->endpoint.max_streams = 15; |
| 2220 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2221 | list_add_tail(&dep->endpoint.ep_list, |
| 2222 | &dwc->gadget.ep_list); |
| 2223 | dep->endpoint.caps.type_iso = true; |
| 2224 | dep->endpoint.caps.type_bulk = true; |
| 2225 | dep->endpoint.caps.type_int = true; |
| 2226 | |
| 2227 | return dwc3_alloc_trb_pool(dep); |
| 2228 | } |
| 2229 | |
| 2230 | static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2231 | { |
| 2232 | struct dwc3_ep *dep; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2233 | bool direction = epnum & 1; |
| 2234 | int ret; |
| 2235 | u8 num = epnum >> 1; |
| 2236 | |
| 2237 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 2238 | if (!dep) |
| 2239 | return -ENOMEM; |
| 2240 | |
| 2241 | dep->dwc = dwc; |
| 2242 | dep->number = epnum; |
| 2243 | dep->direction = direction; |
| 2244 | dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); |
| 2245 | dwc->eps[epnum] = dep; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 2246 | dep->combo_num = 0; |
| 2247 | dep->start_cmd_status = 0; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2248 | |
| 2249 | snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, |
| 2250 | direction ? "in" : "out"); |
| 2251 | |
| 2252 | dep->endpoint.name = dep->name; |
| 2253 | |
| 2254 | if (!(dep->number > 1)) { |
| 2255 | dep->endpoint.desc = &dwc3_gadget_ep0_desc; |
| 2256 | dep->endpoint.comp_desc = NULL; |
| 2257 | } |
| 2258 | |
| 2259 | spin_lock_init(&dep->lock); |
| 2260 | |
| 2261 | if (num == 0) |
| 2262 | ret = dwc3_gadget_init_control_endpoint(dep); |
| 2263 | else if (direction) |
| 2264 | ret = dwc3_gadget_init_in_endpoint(dep); |
| 2265 | else |
| 2266 | ret = dwc3_gadget_init_out_endpoint(dep); |
| 2267 | |
| 2268 | if (ret) |
| 2269 | return ret; |
| 2270 | |
| 2271 | dep->endpoint.caps.dir_in = direction; |
| 2272 | dep->endpoint.caps.dir_out = !direction; |
| 2273 | |
| 2274 | INIT_LIST_HEAD(&dep->pending_list); |
| 2275 | INIT_LIST_HEAD(&dep->started_list); |
Felipe Balbi | d5443bb | 2018-08-01 13:53:29 +0300 | [diff] [blame] | 2276 | INIT_LIST_HEAD(&dep->cancelled_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2277 | |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) |
| 2282 | { |
Bryan O'Donoghue | 47d3946 | 2017-01-31 20:58:10 +0000 | [diff] [blame] | 2283 | u8 epnum; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2284 | |
Bryan O'Donoghue | f3bcfc7 | 2017-01-31 20:58:11 +0000 | [diff] [blame] | 2285 | INIT_LIST_HEAD(&dwc->gadget.ep_list); |
| 2286 | |
Andy Shevchenko | 46b780d | 2017-06-12 15:11:25 +0300 | [diff] [blame] | 2287 | for (epnum = 0; epnum < total; epnum++) { |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2288 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2289 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2290 | ret = dwc3_gadget_init_endpoint(dwc, epnum); |
| 2291 | if (ret) |
| 2292 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
| 2298 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 2299 | { |
| 2300 | struct dwc3_ep *dep; |
| 2301 | u8 epnum; |
| 2302 | |
| 2303 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2304 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2305 | if (!dep) |
| 2306 | continue; |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 2307 | /* |
| 2308 | * Physical endpoints 0 and 1 are special; they form the |
| 2309 | * bi-directional USB endpoint 0. |
| 2310 | * |
| 2311 | * For those two physical endpoints, we don't allocate a TRB |
| 2312 | * pool nor do we add them the endpoints list. Due to that, we |
| 2313 | * shouldn't do these two operations otherwise we would end up |
| 2314 | * with all sorts of bugs when removing dwc3.ko. |
| 2315 | */ |
| 2316 | if (epnum != 0 && epnum != 1) { |
| 2317 | dwc3_free_trb_pool(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2318 | list_del(&dep->endpoint.ep_list); |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 2319 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2320 | |
| 2321 | kfree(dep); |
| 2322 | } |
| 2323 | } |
| 2324 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2325 | /* -------------------------------------------------------------------------- */ |
Felipe Balbi | e5caff6 | 2013-02-26 15:11:05 +0200 | [diff] [blame] | 2326 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2327 | static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, |
| 2328 | struct dwc3_request *req, struct dwc3_trb *trb, |
| 2329 | const struct dwc3_event_depevt *event, int status, int chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2330 | { |
| 2331 | unsigned int count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2332 | |
Felipe Balbi | dc55c67 | 2016-08-12 13:20:32 +0300 | [diff] [blame] | 2333 | dwc3_ep_inc_deq(dep); |
Felipe Balbi | a9c3ca5 | 2016-10-05 14:24:37 +0300 | [diff] [blame] | 2334 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 2335 | trace_dwc3_complete_trb(dep, trb); |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 2336 | req->num_trbs--; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 2337 | |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2338 | /* |
| 2339 | * If we're in the middle of series of chained TRBs and we |
| 2340 | * receive a short transfer along the way, DWC3 will skip |
| 2341 | * through all TRBs including the last TRB in the chain (the |
| 2342 | * where CHN bit is zero. DWC3 will also avoid clearing HWO |
| 2343 | * bit and SW has to do it manually. |
| 2344 | * |
| 2345 | * We're going to do that here to avoid problems of HW trying |
| 2346 | * to use bogus TRBs for transfers. |
| 2347 | */ |
| 2348 | if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) |
| 2349 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 2350 | |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 2351 | /* |
Thinh Nguyen | 6abfa0f | 2018-11-15 19:03:27 -0800 | [diff] [blame] | 2352 | * For isochronous transfers, the first TRB in a service interval must |
| 2353 | * have the Isoc-First type. Track and report its interval frame number. |
| 2354 | */ |
| 2355 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 2356 | (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { |
| 2357 | unsigned int frame_number; |
| 2358 | |
| 2359 | frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); |
| 2360 | frame_number &= ~(dep->interval - 1); |
| 2361 | req->request.frame_number = frame_number; |
| 2362 | } |
| 2363 | |
| 2364 | /* |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 2365 | * If we're dealing with unaligned size OUT transfer, we will be left |
| 2366 | * with one TRB pending in the ring. We need to manually clear HWO bit |
| 2367 | * from that TRB. |
| 2368 | */ |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 2369 | |
| 2370 | if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) { |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 2371 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 2372 | return 1; |
| 2373 | } |
| 2374 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2375 | count = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | e62c5bc5 | 2016-10-25 13:47:21 +0300 | [diff] [blame] | 2376 | req->remaining += count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2377 | |
Felipe Balbi | 35b2719 | 2017-03-08 13:56:37 +0200 | [diff] [blame] | 2378 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
| 2379 | return 1; |
| 2380 | |
Felipe Balbi | d80fe1b | 2018-04-06 11:04:21 +0300 | [diff] [blame] | 2381 | if (event->status & DEPEVT_STATUS_SHORT && !chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2382 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 2383 | |
Felipe Balbi | e0c42ce | 2018-04-06 15:37:30 +0300 | [diff] [blame] | 2384 | if (event->status & DEPEVT_STATUS_IOC) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2385 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 2386 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2387 | return 0; |
| 2388 | } |
| 2389 | |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 2390 | static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, |
| 2391 | struct dwc3_request *req, const struct dwc3_event_depevt *event, |
| 2392 | int status) |
| 2393 | { |
| 2394 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; |
| 2395 | struct scatterlist *sg = req->sg; |
| 2396 | struct scatterlist *s; |
| 2397 | unsigned int pending = req->num_pending_sgs; |
| 2398 | unsigned int i; |
| 2399 | int ret = 0; |
| 2400 | |
| 2401 | for_each_sg(sg, s, pending, i) { |
| 2402 | trb = &dep->trb_pool[dep->trb_dequeue]; |
| 2403 | |
| 2404 | if (trb->ctrl & DWC3_TRB_CTRL_HWO) |
| 2405 | break; |
| 2406 | |
| 2407 | req->sg = sg_next(s); |
| 2408 | req->num_pending_sgs--; |
| 2409 | |
| 2410 | ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, |
| 2411 | trb, event, status, true); |
| 2412 | if (ret) |
| 2413 | break; |
| 2414 | } |
| 2415 | |
| 2416 | return ret; |
| 2417 | } |
| 2418 | |
| 2419 | static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, |
| 2420 | struct dwc3_request *req, const struct dwc3_event_depevt *event, |
| 2421 | int status) |
| 2422 | { |
| 2423 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; |
| 2424 | |
| 2425 | return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, |
| 2426 | event, status, false); |
| 2427 | } |
| 2428 | |
Felipe Balbi | e0c42ce | 2018-04-06 15:37:30 +0300 | [diff] [blame] | 2429 | static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) |
| 2430 | { |
| 2431 | return req->request.actual == req->request.length; |
| 2432 | } |
| 2433 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2434 | static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, |
| 2435 | const struct dwc3_event_depevt *event, |
| 2436 | struct dwc3_request *req, int status) |
| 2437 | { |
| 2438 | int ret; |
| 2439 | |
| 2440 | if (req->num_pending_sgs) |
| 2441 | ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, |
| 2442 | status); |
| 2443 | else |
| 2444 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
| 2445 | status); |
| 2446 | |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 2447 | if (req->needs_extra_trb) { |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2448 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
| 2449 | status); |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 2450 | req->needs_extra_trb = false; |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | req->request.actual = req->request.length - req->remaining; |
| 2454 | |
| 2455 | if (!dwc3_gadget_ep_request_completed(req) && |
| 2456 | req->num_pending_sgs) { |
| 2457 | __dwc3_gadget_kick_transfer(dep); |
| 2458 | goto out; |
| 2459 | } |
| 2460 | |
| 2461 | dwc3_gadget_giveback(dep, req, status); |
| 2462 | |
| 2463 | out: |
| 2464 | return ret; |
| 2465 | } |
| 2466 | |
Felipe Balbi | 12a3a4a | 2018-03-29 11:53:40 +0300 | [diff] [blame] | 2467 | static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2468 | const struct dwc3_event_depevt *event, int status) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2469 | { |
Felipe Balbi | 6afbdb5 | 2018-04-06 15:49:49 +0300 | [diff] [blame] | 2470 | struct dwc3_request *req; |
| 2471 | struct dwc3_request *tmp; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2472 | |
Felipe Balbi | 6afbdb5 | 2018-04-06 15:49:49 +0300 | [diff] [blame] | 2473 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) { |
Felipe Balbi | fee73e6 | 2018-04-06 15:50:29 +0300 | [diff] [blame] | 2474 | int ret; |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2475 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2476 | ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, |
| 2477 | req, status); |
Felipe Balbi | 58f0218 | 2018-03-29 12:10:31 +0300 | [diff] [blame] | 2478 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2479 | break; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2480 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2481 | } |
| 2482 | |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 2483 | static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, |
| 2484 | const struct dwc3_event_depevt *event) |
| 2485 | { |
Felipe Balbi | f62afb4 | 2018-04-11 10:34:34 +0300 | [diff] [blame] | 2486 | dep->frame_number = event->parameters; |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 2487 | } |
| 2488 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2489 | static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, |
| 2490 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2491 | { |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2492 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2493 | unsigned status = 0; |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 2494 | bool stop = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2495 | |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 2496 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
| 2497 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2498 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 2499 | status = -ECONNRESET; |
| 2500 | |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 2501 | if (event->status & DEPEVT_STATUS_MISSED_ISOC) { |
| 2502 | status = -EXDEV; |
Felipe Balbi | d513320 | 2018-04-11 10:32:52 +0300 | [diff] [blame] | 2503 | |
| 2504 | if (list_empty(&dep->started_list)) |
| 2505 | stop = true; |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 2506 | } |
| 2507 | |
Felipe Balbi | 5f2e797 | 2018-03-29 11:10:45 +0300 | [diff] [blame] | 2508 | dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2509 | |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 2510 | if (stop) { |
| 2511 | dwc3_stop_active_transfer(dep, true); |
| 2512 | dep->flags = DWC3_EP_ENABLED; |
| 2513 | } |
| 2514 | |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2515 | /* |
| 2516 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 2517 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 2518 | */ |
| 2519 | if (dwc->revision < DWC3_REVISION_183A) { |
| 2520 | u32 reg; |
| 2521 | int i; |
| 2522 | |
| 2523 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
Moiz Sonasath | 348e026 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 2524 | dep = dwc->eps[i]; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2525 | |
| 2526 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2527 | continue; |
| 2528 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2529 | if (!list_empty(&dep->started_list)) |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2530 | return; |
| 2531 | } |
| 2532 | |
| 2533 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2534 | reg |= dwc->u1u2; |
| 2535 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2536 | |
| 2537 | dwc->u1u2 = 0; |
| 2538 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2539 | } |
| 2540 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2541 | static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, |
| 2542 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 3203386 | 2018-03-27 10:47:48 +0300 | [diff] [blame] | 2543 | { |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 2544 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 2545 | (void) __dwc3_gadget_start_isoc(dep); |
Felipe Balbi | 3203386 | 2018-03-27 10:47:48 +0300 | [diff] [blame] | 2546 | } |
| 2547 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2548 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 2549 | const struct dwc3_event_depevt *event) |
| 2550 | { |
| 2551 | struct dwc3_ep *dep; |
| 2552 | u8 epnum = event->endpoint_number; |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2553 | u8 cmd; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2554 | |
| 2555 | dep = dwc->eps[epnum]; |
| 2556 | |
Janusz Dziedzic | d7fd41c | 2016-12-08 10:57:34 +0100 | [diff] [blame] | 2557 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 2558 | if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) |
| 2559 | return; |
| 2560 | |
| 2561 | /* Handle only EPCMDCMPLT when EP disabled */ |
| 2562 | if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) |
| 2563 | return; |
| 2564 | } |
Felipe Balbi | 3336abb | 2012-06-06 09:19:35 +0300 | [diff] [blame] | 2565 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2566 | if (epnum == 0 || epnum == 1) { |
| 2567 | dwc3_ep0_interrupt(dwc, event); |
| 2568 | return; |
| 2569 | } |
| 2570 | |
| 2571 | switch (event->endpoint_event) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2572 | case DWC3_DEPEVT_XFERINPROGRESS: |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2573 | dwc3_gadget_endpoint_transfer_in_progress(dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2574 | break; |
| 2575 | case DWC3_DEPEVT_XFERNOTREADY: |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2576 | dwc3_gadget_endpoint_transfer_not_ready(dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2577 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2578 | case DWC3_DEPEVT_EPCMDCMPLT: |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2579 | cmd = DEPEVT_PARAMETER_CMD(event->parameters); |
| 2580 | |
| 2581 | if (cmd == DWC3_DEPCMD_ENDTRANSFER) { |
| 2582 | dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; |
Felipe Balbi | fec9095 | 2018-08-01 13:56:50 +0300 | [diff] [blame] | 2583 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2584 | } |
| 2585 | break; |
Felipe Balbi | a24a6ab | 2018-03-27 10:41:39 +0300 | [diff] [blame] | 2586 | case DWC3_DEPEVT_STREAMEVT: |
Felipe Balbi | 742a4ff | 2018-03-26 13:26:56 +0300 | [diff] [blame] | 2587 | case DWC3_DEPEVT_XFERCOMPLETE: |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2588 | case DWC3_DEPEVT_RXTXFIFOEVT: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2589 | break; |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 2594 | { |
| 2595 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 2596 | spin_unlock(&dwc->lock); |
| 2597 | dwc->gadget_driver->disconnect(&dwc->gadget); |
| 2598 | spin_lock(&dwc->lock); |
| 2599 | } |
| 2600 | } |
| 2601 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2602 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
| 2603 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 2604 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2605 | spin_unlock(&dwc->lock); |
| 2606 | dwc->gadget_driver->suspend(&dwc->gadget); |
| 2607 | spin_lock(&dwc->lock); |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | static void dwc3_resume_gadget(struct dwc3 *dwc) |
| 2612 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 2613 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2614 | spin_unlock(&dwc->lock); |
| 2615 | dwc->gadget_driver->resume(&dwc->gadget); |
Felipe Balbi | 5c7b3b0 | 2015-01-29 10:29:18 -0600 | [diff] [blame] | 2616 | spin_lock(&dwc->lock); |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | static void dwc3_reset_gadget(struct dwc3 *dwc) |
| 2621 | { |
| 2622 | if (!dwc->gadget_driver) |
| 2623 | return; |
| 2624 | |
| 2625 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { |
| 2626 | spin_unlock(&dwc->lock); |
| 2627 | usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2628 | spin_lock(&dwc->lock); |
| 2629 | } |
| 2630 | } |
| 2631 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2632 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2633 | { |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2634 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2635 | struct dwc3_gadget_ep_cmd_params params; |
| 2636 | u32 cmd; |
| 2637 | int ret; |
| 2638 | |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2639 | if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || |
| 2640 | !dep->resource_index) |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2641 | return; |
| 2642 | |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2643 | /* |
| 2644 | * NOTICE: We are violating what the Databook says about the |
| 2645 | * EndTransfer command. Ideally we would _always_ wait for the |
| 2646 | * EndTransfer Command Completion IRQ, but that's causing too |
| 2647 | * much trouble synchronizing between us and gadget driver. |
| 2648 | * |
| 2649 | * We have discussed this with the IP Provider and it was |
| 2650 | * suggested to giveback all requests here, but give HW some |
| 2651 | * extra time to synchronize with the interconnect. We're using |
Mickael Maison | dc93b41 | 2014-12-23 17:34:43 +0100 | [diff] [blame] | 2652 | * an arbitrary 100us delay for that. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2653 | * |
| 2654 | * Note also that a similar handling was tested by Synopsys |
| 2655 | * (thanks a lot Paul) and nothing bad has come out of it. |
| 2656 | * In short, what we're doing is: |
| 2657 | * |
| 2658 | * - Issue EndTransfer WITH CMDIOC bit set |
| 2659 | * - Wait 100us |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 2660 | * |
| 2661 | * As of IP version 3.10a of the DWC_usb3 IP, the controller |
| 2662 | * supports a mode to work around the above limitation. The |
| 2663 | * software can poll the CMDACT bit in the DEPCMD register |
| 2664 | * after issuing a EndTransfer command. This mode is enabled |
| 2665 | * by writing GUCTL2[14]. This polling is already done in the |
| 2666 | * dwc3_send_gadget_ep_cmd() function so if the mode is |
| 2667 | * enabled, the EndTransfer command will have completed upon |
| 2668 | * returning from this function and we don't need to delay for |
| 2669 | * 100us. |
| 2670 | * |
| 2671 | * This mode is NOT available on the DWC_usb31 IP. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2672 | */ |
| 2673 | |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2674 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 2675 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
| 2676 | cmd |= DWC3_DEPCMD_CMDIOC; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2677 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2678 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 2679 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2680 | WARN_ON_ONCE(ret); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2681 | dep->resource_index = 0; |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 2682 | |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2683 | if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) { |
| 2684 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 2685 | udelay(100); |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 2686 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2687 | } |
| 2688 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2689 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 2690 | { |
| 2691 | u32 epnum; |
| 2692 | |
| 2693 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2694 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2695 | int ret; |
| 2696 | |
| 2697 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2698 | if (!dep) |
| 2699 | continue; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2700 | |
| 2701 | if (!(dep->flags & DWC3_EP_STALL)) |
| 2702 | continue; |
| 2703 | |
| 2704 | dep->flags &= ~DWC3_EP_STALL; |
| 2705 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 2706 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2707 | WARN_ON_ONCE(ret); |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 2712 | { |
Felipe Balbi | c4430a2 | 2012-05-24 10:30:01 +0300 | [diff] [blame] | 2713 | int reg; |
| 2714 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2715 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2716 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 2717 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2718 | |
| 2719 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 2720 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2721 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2722 | dwc3_disconnect_gadget(dwc); |
| 2723 | |
| 2724 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2725 | dwc->setup_packet_pending = false; |
Felipe Balbi | 06a374e | 2014-10-10 15:24:00 -0500 | [diff] [blame] | 2726 | usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2727 | |
| 2728 | dwc->connected = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2729 | } |
| 2730 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2731 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 2732 | { |
| 2733 | u32 reg; |
| 2734 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2735 | dwc->connected = true; |
| 2736 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2737 | /* |
| 2738 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 2739 | * would cause a missing Disconnect Event if there's a |
| 2740 | * pending Setup Packet in the FIFO. |
| 2741 | * |
| 2742 | * There's no suggested workaround on the official Bug |
| 2743 | * report, which states that "unless the driver/application |
| 2744 | * is doing any special handling of a disconnect event, |
| 2745 | * there is no functional issue". |
| 2746 | * |
| 2747 | * Unfortunately, it turns out that we _do_ some special |
| 2748 | * handling of a disconnect event, namely complete all |
| 2749 | * pending transfers, notify gadget driver of the |
| 2750 | * disconnection, and so on. |
| 2751 | * |
| 2752 | * Our suggested workaround is to follow the Disconnect |
| 2753 | * Event steps here, instead, based on a setup_packet_pending |
Felipe Balbi | b5d335e | 2015-11-16 16:20:34 -0600 | [diff] [blame] | 2754 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
| 2755 | * status for EP0 TRBs and gets cleared on XferComplete for the |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2756 | * same endpoint. |
| 2757 | * |
| 2758 | * Refers to: |
| 2759 | * |
| 2760 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 2761 | * generated if setup packet pending in FIFO |
| 2762 | */ |
| 2763 | if (dwc->revision < DWC3_REVISION_188A) { |
| 2764 | if (dwc->setup_packet_pending) |
| 2765 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2766 | } |
| 2767 | |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 2768 | dwc3_reset_gadget(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2769 | |
| 2770 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2771 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 2772 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 2773 | dwc->test_mode = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2774 | dwc3_clear_stall_all_ep(dwc); |
| 2775 | |
| 2776 | /* Reset device address to zero */ |
| 2777 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2778 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 2779 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2780 | } |
| 2781 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2782 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 2783 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2784 | struct dwc3_ep *dep; |
| 2785 | int ret; |
| 2786 | u32 reg; |
| 2787 | u8 speed; |
| 2788 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2789 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2790 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 2791 | dwc->speed = speed; |
| 2792 | |
John Youn | 5fb6fda | 2016-11-10 17:23:25 -0800 | [diff] [blame] | 2793 | /* |
| 2794 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 2795 | * each time on Connect Done. |
| 2796 | * |
| 2797 | * Currently we always use the reset value. If any platform |
| 2798 | * wants to set this to a different value, we need to add a |
| 2799 | * setting and update GCTL.RAMCLKSEL here. |
| 2800 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2801 | |
| 2802 | switch (speed) { |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2803 | case DWC3_DSTS_SUPERSPEED_PLUS: |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 2804 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2805 | dwc->gadget.ep0->maxpacket = 512; |
| 2806 | dwc->gadget.speed = USB_SPEED_SUPER_PLUS; |
| 2807 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2808 | case DWC3_DSTS_SUPERSPEED: |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 2809 | /* |
| 2810 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 2811 | * would cause a missing USB3 Reset event. |
| 2812 | * |
| 2813 | * In such situations, we should force a USB3 Reset |
| 2814 | * event by calling our dwc3_gadget_reset_interrupt() |
| 2815 | * routine. |
| 2816 | * |
| 2817 | * Refers to: |
| 2818 | * |
| 2819 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 2820 | * not be generated always when the link enters poll |
| 2821 | */ |
| 2822 | if (dwc->revision < DWC3_REVISION_190A) |
| 2823 | dwc3_gadget_reset_interrupt(dwc); |
| 2824 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2825 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2826 | dwc->gadget.ep0->maxpacket = 512; |
| 2827 | dwc->gadget.speed = USB_SPEED_SUPER; |
| 2828 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2829 | case DWC3_DSTS_HIGHSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2830 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2831 | dwc->gadget.ep0->maxpacket = 64; |
| 2832 | dwc->gadget.speed = USB_SPEED_HIGH; |
| 2833 | break; |
Roger Quadros | 9418ee1 | 2017-01-03 14:32:09 +0200 | [diff] [blame] | 2834 | case DWC3_DSTS_FULLSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2835 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2836 | dwc->gadget.ep0->maxpacket = 64; |
| 2837 | dwc->gadget.speed = USB_SPEED_FULL; |
| 2838 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2839 | case DWC3_DSTS_LOWSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2840 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); |
| 2841 | dwc->gadget.ep0->maxpacket = 8; |
| 2842 | dwc->gadget.speed = USB_SPEED_LOW; |
| 2843 | break; |
| 2844 | } |
| 2845 | |
Thinh Nguyen | 6180026 | 2018-01-12 18:18:05 -0800 | [diff] [blame] | 2846 | dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket; |
| 2847 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2848 | /* Enable USB2 LPM Capability */ |
| 2849 | |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 2850 | if ((dwc->revision > DWC3_REVISION_194A) && |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2851 | (speed != DWC3_DSTS_SUPERSPEED) && |
| 2852 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2853 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2854 | reg |= DWC3_DCFG_LPM_CAP; |
| 2855 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2856 | |
| 2857 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2858 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 2859 | |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 2860 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2861 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 2862 | /* |
| 2863 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and |
| 2864 | * DCFG.LPMCap is set, core responses with an ACK and the |
| 2865 | * BESL value in the LPM token is less than or equal to LPM |
| 2866 | * NYET threshold. |
| 2867 | */ |
| 2868 | WARN_ONCE(dwc->revision < DWC3_REVISION_240A |
| 2869 | && dwc->has_lpm_erratum, |
Masanari Iida | 9165dab | 2016-09-17 23:44:17 +0900 | [diff] [blame] | 2870 | "LPM Erratum not available on dwc3 revisions < 2.40a\n"); |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 2871 | |
| 2872 | if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) |
| 2873 | reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); |
| 2874 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2875 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 2876 | } else { |
| 2877 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2878 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; |
| 2879 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2880 | } |
| 2881 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2882 | dep = dwc->eps[0]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 2883 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2884 | if (ret) { |
| 2885 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2886 | return; |
| 2887 | } |
| 2888 | |
| 2889 | dep = dwc->eps[1]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 2890 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2891 | if (ret) { |
| 2892 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2893 | return; |
| 2894 | } |
| 2895 | |
| 2896 | /* |
| 2897 | * Configure PHY via GUSB3PIPECTLn if required. |
| 2898 | * |
| 2899 | * Update GTXFIFOSIZn |
| 2900 | * |
| 2901 | * In both cases reset values should be sufficient. |
| 2902 | */ |
| 2903 | } |
| 2904 | |
| 2905 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) |
| 2906 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2907 | /* |
| 2908 | * TODO take core out of low power mode when that's |
| 2909 | * implemented. |
| 2910 | */ |
| 2911 | |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 2912 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
| 2913 | spin_unlock(&dwc->lock); |
| 2914 | dwc->gadget_driver->resume(&dwc->gadget); |
| 2915 | spin_lock(&dwc->lock); |
| 2916 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 2920 | unsigned int evtinfo) |
| 2921 | { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2922 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 2923 | unsigned int pwropt; |
| 2924 | |
| 2925 | /* |
| 2926 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without |
| 2927 | * Hibernation mode enabled which would show up when device detects |
| 2928 | * host-initiated U3 exit. |
| 2929 | * |
| 2930 | * In that case, device will generate a Link State Change Interrupt |
| 2931 | * from U3 to RESUME which is only necessary if Hibernation is |
| 2932 | * configured in. |
| 2933 | * |
| 2934 | * There are no functional changes due to such spurious event and we |
| 2935 | * just need to ignore it. |
| 2936 | * |
| 2937 | * Refers to: |
| 2938 | * |
| 2939 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation |
| 2940 | * operational mode |
| 2941 | */ |
| 2942 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); |
| 2943 | if ((dwc->revision < DWC3_REVISION_250A) && |
| 2944 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
| 2945 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && |
| 2946 | (next == DWC3_LINK_STATE_RESUME)) { |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 2947 | return; |
| 2948 | } |
| 2949 | } |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2950 | |
| 2951 | /* |
| 2952 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 2953 | * on the link partner, the USB session might do multiple entry/exit |
| 2954 | * of low power states before a transfer takes place. |
| 2955 | * |
| 2956 | * Due to this problem, we might experience lower throughput. The |
| 2957 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 2958 | * transitioning from U1/U2 to U0 and enable those bits again |
| 2959 | * after a transfer completes and there are no pending transfers |
| 2960 | * on any of the enabled endpoints. |
| 2961 | * |
| 2962 | * This is the first half of that workaround. |
| 2963 | * |
| 2964 | * Refers to: |
| 2965 | * |
| 2966 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 2967 | * core send LGO_Ux entering U0 |
| 2968 | */ |
| 2969 | if (dwc->revision < DWC3_REVISION_183A) { |
| 2970 | if (next == DWC3_LINK_STATE_U0) { |
| 2971 | u32 u1u2; |
| 2972 | u32 reg; |
| 2973 | |
| 2974 | switch (dwc->link_state) { |
| 2975 | case DWC3_LINK_STATE_U1: |
| 2976 | case DWC3_LINK_STATE_U2: |
| 2977 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2978 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 2979 | | DWC3_DCTL_ACCEPTU2ENA |
| 2980 | | DWC3_DCTL_INITU1ENA |
| 2981 | | DWC3_DCTL_ACCEPTU1ENA); |
| 2982 | |
| 2983 | if (!dwc->u1u2) |
| 2984 | dwc->u1u2 = reg & u1u2; |
| 2985 | |
| 2986 | reg &= ~u1u2; |
| 2987 | |
| 2988 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2989 | break; |
| 2990 | default: |
| 2991 | /* do nothing */ |
| 2992 | break; |
| 2993 | } |
| 2994 | } |
| 2995 | } |
| 2996 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2997 | switch (next) { |
| 2998 | case DWC3_LINK_STATE_U1: |
| 2999 | if (dwc->speed == USB_SPEED_SUPER) |
| 3000 | dwc3_suspend_gadget(dwc); |
| 3001 | break; |
| 3002 | case DWC3_LINK_STATE_U2: |
| 3003 | case DWC3_LINK_STATE_U3: |
| 3004 | dwc3_suspend_gadget(dwc); |
| 3005 | break; |
| 3006 | case DWC3_LINK_STATE_RESUME: |
| 3007 | dwc3_resume_gadget(dwc); |
| 3008 | break; |
| 3009 | default: |
| 3010 | /* do nothing */ |
| 3011 | break; |
| 3012 | } |
| 3013 | |
Felipe Balbi | e57ebc1 | 2014-04-22 13:20:12 -0500 | [diff] [blame] | 3014 | dwc->link_state = next; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3015 | } |
| 3016 | |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3017 | static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, |
| 3018 | unsigned int evtinfo) |
| 3019 | { |
| 3020 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
| 3021 | |
| 3022 | if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) |
| 3023 | dwc3_suspend_gadget(dwc); |
| 3024 | |
| 3025 | dwc->link_state = next; |
| 3026 | } |
| 3027 | |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3028 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
| 3029 | unsigned int evtinfo) |
| 3030 | { |
| 3031 | unsigned int is_ss = evtinfo & BIT(4); |
| 3032 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 3033 | /* |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3034 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
| 3035 | * have a known issue which can cause USB CV TD.9.23 to fail |
| 3036 | * randomly. |
| 3037 | * |
| 3038 | * Because of this issue, core could generate bogus hibernation |
| 3039 | * events which SW needs to ignore. |
| 3040 | * |
| 3041 | * Refers to: |
| 3042 | * |
| 3043 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 |
| 3044 | * Device Fallback from SuperSpeed |
| 3045 | */ |
| 3046 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) |
| 3047 | return; |
| 3048 | |
| 3049 | /* enter hibernation here */ |
| 3050 | } |
| 3051 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3052 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 3053 | const struct dwc3_event_devt *event) |
| 3054 | { |
| 3055 | switch (event->type) { |
| 3056 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 3057 | dwc3_gadget_disconnect_interrupt(dwc); |
| 3058 | break; |
| 3059 | case DWC3_DEVICE_EVENT_RESET: |
| 3060 | dwc3_gadget_reset_interrupt(dwc); |
| 3061 | break; |
| 3062 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 3063 | dwc3_gadget_conndone_interrupt(dwc); |
| 3064 | break; |
| 3065 | case DWC3_DEVICE_EVENT_WAKEUP: |
| 3066 | dwc3_gadget_wakeup_interrupt(dwc); |
| 3067 | break; |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3068 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
| 3069 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, |
| 3070 | "unexpected hibernation event\n")) |
| 3071 | break; |
| 3072 | |
| 3073 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
| 3074 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3075 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 3076 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 3077 | break; |
| 3078 | case DWC3_DEVICE_EVENT_EOPF: |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3079 | /* It changed to be suspend event for version 2.30a and above */ |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 3080 | if (dwc->revision >= DWC3_REVISION_230A) { |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3081 | /* |
| 3082 | * Ignore suspend event until the gadget enters into |
| 3083 | * USB_STATE_CONFIGURED state. |
| 3084 | */ |
| 3085 | if (dwc->gadget.state >= USB_STATE_CONFIGURED) |
| 3086 | dwc3_gadget_suspend_interrupt(dwc, |
| 3087 | event->event_info); |
| 3088 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3089 | break; |
| 3090 | case DWC3_DEVICE_EVENT_SOF: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3091 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3092 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3093 | case DWC3_DEVICE_EVENT_OVERFLOW: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3094 | break; |
| 3095 | default: |
Felipe Balbi | e9f2aa8 | 2015-01-27 13:49:28 -0600 | [diff] [blame] | 3096 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 3101 | const union dwc3_event *event) |
| 3102 | { |
Felipe Balbi | 43c96be | 2016-09-26 13:23:34 +0300 | [diff] [blame] | 3103 | trace_dwc3_event(event->raw, dwc); |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 3104 | |
Felipe Balbi | dfc5e80 | 2017-04-26 13:44:51 +0300 | [diff] [blame] | 3105 | if (!event->type.is_devspec) |
| 3106 | dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 3107 | else if (event->type.type == DWC3_EVENT_TYPE_DEV) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3108 | dwc3_gadget_interrupt(dwc, &event->devt); |
Felipe Balbi | dfc5e80 | 2017-04-26 13:44:51 +0300 | [diff] [blame] | 3109 | else |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3110 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3111 | } |
| 3112 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3113 | static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3114 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3115 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3116 | irqreturn_t ret = IRQ_NONE; |
| 3117 | int left; |
| 3118 | u32 reg; |
| 3119 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3120 | left = evt->count; |
| 3121 | |
| 3122 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
| 3123 | return IRQ_NONE; |
| 3124 | |
| 3125 | while (left > 0) { |
| 3126 | union dwc3_event event; |
| 3127 | |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 3128 | event.raw = *(u32 *) (evt->cache + evt->lpos); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3129 | |
| 3130 | dwc3_process_event_entry(dwc, &event); |
| 3131 | |
| 3132 | /* |
| 3133 | * FIXME we wrap around correctly to the next entry as |
| 3134 | * almost all entries are 4 bytes in size. There is one |
| 3135 | * entry which has 12 bytes which is a regular entry |
| 3136 | * followed by 8 bytes data. ATM I don't know how |
| 3137 | * things are organized if we get next to the a |
| 3138 | * boundary so I worry about that once we try to handle |
| 3139 | * that. |
| 3140 | */ |
Felipe Balbi | caefe6c | 2016-11-15 13:05:23 +0200 | [diff] [blame] | 3141 | evt->lpos = (evt->lpos + 4) % evt->length; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3142 | left -= 4; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | evt->count = 0; |
| 3146 | evt->flags &= ~DWC3_EVENT_PENDING; |
| 3147 | ret = IRQ_HANDLED; |
| 3148 | |
| 3149 | /* Unmask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3150 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3151 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3152 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3153 | |
John Youn | cf40b86 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 3154 | if (dwc->imod_interval) { |
| 3155 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); |
| 3156 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); |
| 3157 | } |
| 3158 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3159 | return ret; |
| 3160 | } |
| 3161 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3162 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3163 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3164 | struct dwc3_event_buffer *evt = _evt; |
| 3165 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3166 | unsigned long flags; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3167 | irqreturn_t ret = IRQ_NONE; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3168 | |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3169 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3170 | ret = dwc3_process_event_buf(evt); |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3171 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3172 | |
| 3173 | return ret; |
| 3174 | } |
| 3175 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3176 | static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3177 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3178 | struct dwc3 *dwc = evt->dwc; |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 3179 | u32 amount; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3180 | u32 count; |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3181 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3182 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3183 | if (pm_runtime_suspended(dwc->dev)) { |
| 3184 | pm_runtime_get(dwc->dev); |
| 3185 | disable_irq_nosync(dwc->irq_gadget); |
| 3186 | dwc->pending_events = true; |
| 3187 | return IRQ_HANDLED; |
| 3188 | } |
| 3189 | |
Thinh Nguyen | d325a1d | 2017-05-11 17:26:47 -0700 | [diff] [blame] | 3190 | /* |
| 3191 | * With PCIe legacy interrupt, test shows that top-half irq handler can |
| 3192 | * be called again after HW interrupt deassertion. Check if bottom-half |
| 3193 | * irq event handler completes before caching new event to prevent |
| 3194 | * losing events. |
| 3195 | */ |
| 3196 | if (evt->flags & DWC3_EVENT_PENDING) |
| 3197 | return IRQ_HANDLED; |
| 3198 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3199 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3200 | count &= DWC3_GEVNTCOUNT_MASK; |
| 3201 | if (!count) |
| 3202 | return IRQ_NONE; |
| 3203 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3204 | evt->count = count; |
| 3205 | evt->flags |= DWC3_EVENT_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3206 | |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3207 | /* Mask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3208 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3209 | reg |= DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3210 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3211 | |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 3212 | amount = min(count, evt->length - evt->lpos); |
| 3213 | memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); |
| 3214 | |
| 3215 | if (amount < count) |
| 3216 | memcpy(evt->cache, evt->buf, count - amount); |
| 3217 | |
John Youn | 65aca32 | 2016-11-15 13:08:59 +0200 | [diff] [blame] | 3218 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
| 3219 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3220 | return IRQ_WAKE_THREAD; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3221 | } |
| 3222 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3223 | static irqreturn_t dwc3_interrupt(int irq, void *_evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3224 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3225 | struct dwc3_event_buffer *evt = _evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3226 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3227 | return dwc3_check_event_buf(evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3228 | } |
| 3229 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3230 | static int dwc3_gadget_get_irq(struct dwc3 *dwc) |
| 3231 | { |
| 3232 | struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); |
| 3233 | int irq; |
| 3234 | |
| 3235 | irq = platform_get_irq_byname(dwc3_pdev, "peripheral"); |
| 3236 | if (irq > 0) |
| 3237 | goto out; |
| 3238 | |
| 3239 | if (irq == -EPROBE_DEFER) |
| 3240 | goto out; |
| 3241 | |
| 3242 | irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3"); |
| 3243 | if (irq > 0) |
| 3244 | goto out; |
| 3245 | |
| 3246 | if (irq == -EPROBE_DEFER) |
| 3247 | goto out; |
| 3248 | |
| 3249 | irq = platform_get_irq(dwc3_pdev, 0); |
| 3250 | if (irq > 0) |
| 3251 | goto out; |
| 3252 | |
| 3253 | if (irq != -EPROBE_DEFER) |
| 3254 | dev_err(dwc->dev, "missing peripheral IRQ\n"); |
| 3255 | |
| 3256 | if (!irq) |
| 3257 | irq = -EINVAL; |
| 3258 | |
| 3259 | out: |
| 3260 | return irq; |
| 3261 | } |
| 3262 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3263 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 3264 | * dwc3_gadget_init - initializes gadget related registers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 3265 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3266 | * |
| 3267 | * Returns 0 on success otherwise negative errno. |
| 3268 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 3269 | int dwc3_gadget_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3270 | { |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3271 | int ret; |
| 3272 | int irq; |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 3273 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3274 | irq = dwc3_gadget_get_irq(dwc); |
| 3275 | if (irq < 0) { |
| 3276 | ret = irq; |
| 3277 | goto err0; |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | dwc->irq_gadget = irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3281 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 3282 | dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, |
| 3283 | sizeof(*dwc->ep0_trb) * 2, |
| 3284 | &dwc->ep0_trb_addr, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3285 | if (!dwc->ep0_trb) { |
| 3286 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 3287 | ret = -ENOMEM; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3288 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3289 | } |
| 3290 | |
Felipe Balbi | 4199c5f | 2017-04-07 14:09:13 +0300 | [diff] [blame] | 3291 | dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3292 | if (!dwc->setup_buf) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3293 | ret = -ENOMEM; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3294 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3295 | } |
| 3296 | |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 3297 | dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, |
| 3298 | &dwc->bounce_addr, GFP_KERNEL); |
| 3299 | if (!dwc->bounce) { |
| 3300 | ret = -ENOMEM; |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3301 | goto err2; |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 3304 | init_completion(&dwc->ep0_in_setup); |
| 3305 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3306 | dwc->gadget.ops = &dwc3_gadget_ops; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3307 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 3308 | dwc->gadget.sg_supported = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3309 | dwc->gadget.name = "dwc3-gadget"; |
Jianqiang Tang | 6a4290c | 2016-01-20 14:09:39 +0800 | [diff] [blame] | 3310 | dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3311 | |
| 3312 | /* |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3313 | * FIXME We might be setting max_speed to <SUPER, however versions |
| 3314 | * <2.20a of dwc3 have an issue with metastability (documented |
| 3315 | * elsewhere in this driver) which tells us we can't set max speed to |
| 3316 | * anything lower than SUPER. |
| 3317 | * |
| 3318 | * Because gadget.max_speed is only used by composite.c and function |
| 3319 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this |
| 3320 | * to happen so we avoid sending SuperSpeed Capability descriptor |
| 3321 | * together with our BOS descriptor as that could confuse host into |
| 3322 | * thinking we can handle super speed. |
| 3323 | * |
| 3324 | * Note that, in fact, we won't even support GetBOS requests when speed |
| 3325 | * is less than super speed because we don't have means, yet, to tell |
| 3326 | * composite.c that we are USB 2.0 + LPM ECN. |
| 3327 | */ |
Roger Quadros | 42bf02e | 2017-10-31 15:11:55 +0200 | [diff] [blame] | 3328 | if (dwc->revision < DWC3_REVISION_220A && |
| 3329 | !dwc->dis_metastability_quirk) |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 3330 | dev_info(dwc->dev, "changing max_speed on rev %08x\n", |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3331 | dwc->revision); |
| 3332 | |
| 3333 | dwc->gadget.max_speed = dwc->maximum_speed; |
| 3334 | |
| 3335 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3336 | * REVISIT: Here we should clear all pending IRQs to be |
| 3337 | * sure we're starting from a well known location. |
| 3338 | */ |
| 3339 | |
Bryan O'Donoghue | f3bcfc7 | 2017-01-31 20:58:11 +0000 | [diff] [blame] | 3340 | ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3341 | if (ret) |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3342 | goto err3; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3343 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3344 | ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); |
| 3345 | if (ret) { |
| 3346 | dev_err(dwc->dev, "failed to register udc\n"); |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3347 | goto err4; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3348 | } |
| 3349 | |
Roger Quadros | 169e3b6 | 2019-01-10 17:04:28 +0200 | [diff] [blame] | 3350 | dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed); |
| 3351 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3352 | return 0; |
Felipe Balbi | 4199c5f | 2017-04-07 14:09:13 +0300 | [diff] [blame] | 3353 | |
| 3354 | err4: |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3355 | dwc3_gadget_free_endpoints(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3356 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3357 | err3: |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3358 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
| 3359 | dwc->bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3360 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3361 | err2: |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 3362 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3363 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3364 | err1: |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 3365 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3366 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 3367 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3368 | err0: |
| 3369 | return ret; |
| 3370 | } |
| 3371 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3372 | /* -------------------------------------------------------------------------- */ |
| 3373 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3374 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 3375 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3376 | usb_del_gadget_udc(&dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3377 | dwc3_gadget_free_endpoints(dwc); |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 3378 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3379 | dwc->bounce_addr); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 3380 | kfree(dwc->setup_buf); |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 3381 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3382 | dwc->ep0_trb, dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3383 | } |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3384 | |
Felipe Balbi | 0b0231a | 2014-10-07 10:19:23 -0500 | [diff] [blame] | 3385 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3386 | { |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 3387 | if (!dwc->gadget_driver) |
| 3388 | return 0; |
| 3389 | |
Roger Quadros | 1551e35 | 2017-02-15 14:16:26 +0200 | [diff] [blame] | 3390 | dwc3_gadget_run_stop(dwc, false, false); |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3391 | dwc3_disconnect_gadget(dwc); |
| 3392 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3393 | |
Bo He | 01c1088 | 2019-01-14 09:48:32 +0200 | [diff] [blame] | 3394 | synchronize_irq(dwc->irq_gadget); |
| 3395 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3396 | return 0; |
| 3397 | } |
| 3398 | |
| 3399 | int dwc3_gadget_resume(struct dwc3 *dwc) |
| 3400 | { |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3401 | int ret; |
| 3402 | |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 3403 | if (!dwc->gadget_driver) |
| 3404 | return 0; |
| 3405 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3406 | ret = __dwc3_gadget_start(dwc); |
| 3407 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3408 | goto err0; |
| 3409 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3410 | ret = dwc3_gadget_run_stop(dwc, true, false); |
| 3411 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3412 | goto err1; |
| 3413 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3414 | return 0; |
| 3415 | |
| 3416 | err1: |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3417 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3418 | |
| 3419 | err0: |
| 3420 | return ret; |
| 3421 | } |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3422 | |
| 3423 | void dwc3_gadget_process_pending_events(struct dwc3 *dwc) |
| 3424 | { |
| 3425 | if (dwc->pending_events) { |
| 3426 | dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); |
| 3427 | dwc->pending_events = false; |
| 3428 | enable_irq(dwc->irq_gadget); |
| 3429 | } |
| 3430 | } |