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