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