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