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