forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/usb/dwc3/gadget.c
....@@ -2,7 +2,7 @@
22 /*
33 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
44 *
5
- * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5
+ * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
66 *
77 * Authors: Felipe Balbi <balbi@ti.com>,
88 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
....@@ -46,18 +46,18 @@
4646 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
4747
4848 switch (mode) {
49
- case TEST_J:
50
- case TEST_K:
51
- case TEST_SE0_NAK:
52
- case TEST_PACKET:
53
- case TEST_FORCE_EN:
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:
5454 reg |= mode << 1;
5555 break;
5656 default:
5757 return -EINVAL;
5858 }
5959
60
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
60
+ dwc3_gadget_dctl_write_safe(dwc, reg);
6161
6262 return 0;
6363 }
....@@ -95,7 +95,7 @@
9595 * Wait until device controller is ready. Only applies to 1.94a and
9696 * later RTL.
9797 */
98
- if (dwc->revision >= DWC3_REVISION_194A) {
98
+ if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
9999 while (--retries) {
100100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101101 if (reg & DWC3_DSTS_DCNRD)
....@@ -111,6 +111,9 @@
111111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113113
114
+ /* set no action before sending new link state change */
115
+ dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116
+
114117 /* set requested state */
115118 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116119 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
....@@ -119,7 +122,7 @@
119122 * The following code is racy when called from dwc3_gadget_wakeup,
120123 * and is not needed, at least on newer versions
121124 */
122
- if (dwc->revision >= DWC3_REVISION_194A)
125
+ if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
123126 return 0;
124127
125128 /* wait for a change in DSTS */
....@@ -135,182 +138,6 @@
135138
136139 return -ETIMEDOUT;
137140 }
138
-
139
-#ifdef CONFIG_ARCH_ROCKCHIP
140
-/**
141
- * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
142
- * @dwc: pointer to our context structure
143
- *
144
- * This function will a best effort FIFO allocation in order
145
- * to improve FIFO usage and throughput, while still allowing
146
- * us to enable as many endpoints as possible.
147
- *
148
- * Keep in mind that this operation will be highly dependent
149
- * on the configured size for RAM1 - which contains TxFifo -,
150
- * the amount of endpoints enabled on coreConsultant tool, and
151
- * the width of the Master Bus.
152
- *
153
- * In the ideal world, we would always be able to satisfy the
154
- * following equation:
155
- *
156
- * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
157
- * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
158
- *
159
- * Unfortunately, due to many variables that's not always the case.
160
- */
161
-static int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
162
-{
163
- int last_fifo_depth;
164
- int fifo_size, total_size, total_resize = 0;
165
- int mdwidth;
166
- u8 num, fifo_number, num_in_eps;
167
-
168
- /*
169
- * Only support Tx fifos resize for gadget speed <= high speed
170
- * for the time being and do fifo resize operation only once
171
- * when connect done event occurs, because if resize Tx fifos
172
- * during controller transfer data, it may cause controller
173
- * run into abnormal and unrecoverable state.
174
- */
175
- if (!dwc->needs_fifo_resize || dwc->fifo_resize_status)
176
- return 0;
177
-
178
- num_in_eps = DWC3_NUM_IN_EPS(&dwc->hwparams);
179
- mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
180
- /* MDWIDTH is represented in bits, we need it in bytes */
181
- mdwidth >>= 3;
182
- fifo_number = 0;
183
- /* Get the Tx FIFO 0 size and depth */
184
- fifo_size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
185
- last_fifo_depth = DWC3_GTXFIFOSIZ_TXFSTADDR(fifo_size) >> 16;
186
- /* Get the Tx FIFO (num_in_eps - 1) size and depth */
187
- fifo_size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num_in_eps - 1));
188
- /* Get the Tx FIFOs total size */
189
- total_size = (DWC3_GTXFIFOSIZ_TXFSTADDR(fifo_size) >> 16) +
190
- DWC3_GTXFIFOSIZ_TXFDEF(fifo_size) - last_fifo_depth;
191
-
192
- for (num = 0; num < dwc->num_eps; num++) {
193
- struct dwc3_ep *dep = dwc->eps[num];
194
- int mult = 1, maxpacket = 512;
195
- int tmp;
196
-
197
- /* Skip out endpoints */
198
- if (!dep || !dep->direction)
199
- continue;
200
-
201
- switch (dep->endpoint.transfer_type) {
202
- case USB_ENDPOINT_XFER_CONTROL:
203
- if (!dep->endpoint.caps.type_control) {
204
- dev_dbg(dwc->dev, "%s may not be used\n",
205
- dep->name);
206
- goto out;
207
- }
208
-
209
- mult = 1;
210
- if (dwc->gadget.speed <= USB_SPEED_HIGH)
211
- maxpacket = 64;
212
- else
213
- maxpacket = 512;
214
- break;
215
- case USB_ENDPOINT_XFER_ISOC:
216
- if (!dep->endpoint.caps.type_iso) {
217
- dev_WARN(dwc->dev, "%s not support isoc type\n",
218
- dep->name);
219
- goto out;
220
- }
221
-
222
- /*
223
- * Set enough tx fifos for Isochronous endpoints
224
- * to get better performance and more compliance
225
- * with bus latency.
226
- */
227
- maxpacket = dep->endpoint.maxpacket;
228
- if (dwc->gadget.speed <= USB_SPEED_HIGH)
229
- mult = dep->endpoint.mult;
230
- else
231
- mult = dep->endpoint.mult *
232
- dep->endpoint.maxburst;
233
- mult = mult > 0 ? mult * 2 : 3;
234
- if (mult > 6)
235
- mult = 6;
236
- break;
237
- case USB_ENDPOINT_XFER_BULK:
238
- if (!dep->endpoint.caps.type_bulk) {
239
- dev_WARN(dwc->dev, "%s not support bulk type\n",
240
- dep->name);
241
- goto out;
242
- }
243
-
244
- /*
245
- * Set enough tx fifos for Bulk endpoints to get
246
- * better transmission performance.
247
- */
248
- mult = 3;
249
- if (dwc->gadget.speed <= USB_SPEED_HIGH) {
250
- maxpacket = 512;
251
- } else {
252
- if (dep->endpoint.maxburst > mult) {
253
- mult = dep->endpoint.maxburst;
254
- if (mult > 6)
255
- mult = 6;
256
- }
257
- maxpacket = 1024;
258
- }
259
- break;
260
- case USB_ENDPOINT_XFER_INT:
261
- /* Bulk endpoints handle interrupt transfers. */
262
- if (!dep->endpoint.caps.type_int &&
263
- !dep->endpoint.caps.type_bulk) {
264
- dev_WARN(dwc->dev, "%s not support int type\n",
265
- dep->name);
266
- goto out;
267
- }
268
-
269
- /*
270
- * REVIST: we assume that the maxpacket of interrupt
271
- * endpoint is 64 Bytes for MTP and the other functions.
272
- */
273
- mult = 1;
274
- maxpacket = 64;
275
- break;
276
- default:
277
- /*
278
- * This is only possible with faulty memory
279
- * because we checked it already.
280
- */
281
- dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
282
- dep->endpoint.transfer_type);
283
- goto out;
284
- }
285
-
286
- tmp = mult * (maxpacket + mdwidth);
287
- tmp += mdwidth;
288
-
289
- fifo_size = DIV_ROUND_UP(tmp, mdwidth);
290
- total_resize += fifo_size;
291
- fifo_size |= (last_fifo_depth << 16);
292
-
293
- if (total_resize > total_size) {
294
- dev_WARN(dwc->dev, "Tx FIFO resize overflow!\n");
295
- break;
296
- }
297
-
298
- dev_dbg(dwc->dev, "%s: FIFO Addr %04x Size %d\n",
299
- dep->name, last_fifo_depth, fifo_size & 0xffff);
300
-
301
- dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
302
- fifo_size);
303
-
304
- last_fifo_depth += (fifo_size & 0xffff);
305
- fifo_number++;
306
- }
307
-
308
-out:
309
- dwc->fifo_resize_status = true;
310
-
311
- return 0;
312
-}
313
-#endif
314141
315142 /**
316143 * dwc3_ep_inc_trb - increment a trb index.
....@@ -400,7 +227,8 @@
400227 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
401228 * and wait for its completion.
402229 */
403
-int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
230
+int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
231
+ u32 param)
404232 {
405233 u32 timeout = 500;
406234 int status = 0;
....@@ -441,7 +269,7 @@
441269 * Caller should handle locking. This function will issue @cmd with given
442270 * @params to @dep and wait for its completion.
443271 */
444
-int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
272
+int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
445273 struct dwc3_gadget_ep_cmd_params *params)
446274 {
447275 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
....@@ -463,7 +291,8 @@
463291 *
464292 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
465293 */
466
- if (dwc->gadget.speed <= USB_SPEED_HIGH) {
294
+ if (dwc->gadget->speed <= USB_SPEED_HIGH ||
295
+ DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
467296 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
468297 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
469298 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
....@@ -482,19 +311,38 @@
482311 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
483312 int link_state;
484313
314
+ /*
315
+ * Initiate remote wakeup if the link state is in U3 when
316
+ * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
317
+ * link state is in U1/U2, no remote wakeup is needed. The Start
318
+ * Transfer command will initiate the link recovery.
319
+ */
485320 link_state = dwc3_gadget_get_link_state(dwc);
486
- if (link_state == DWC3_LINK_STATE_U1 ||
487
- link_state == DWC3_LINK_STATE_U2 ||
488
- link_state == DWC3_LINK_STATE_U3) {
321
+ switch (link_state) {
322
+ case DWC3_LINK_STATE_U2:
323
+ if (dwc->gadget->speed >= USB_SPEED_SUPER)
324
+ break;
325
+
326
+ fallthrough;
327
+ case DWC3_LINK_STATE_U3:
489328 ret = __dwc3_gadget_wakeup(dwc);
490329 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
491330 ret);
331
+ break;
492332 }
493333 }
494334
495
- dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
496
- dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
497
- dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
335
+ /*
336
+ * For some commands such as Update Transfer command, DEPCMDPARn
337
+ * registers are reserved. Since the driver often sends Update Transfer
338
+ * command, don't write to DEPCMDPARn to avoid register write delays and
339
+ * improve performance.
340
+ */
341
+ if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) {
342
+ dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
343
+ dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
344
+ dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
345
+ }
498346
499347 /*
500348 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
....@@ -518,6 +366,14 @@
518366 cmd |= DWC3_DEPCMD_CMDACT;
519367
520368 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
369
+
370
+ if (!(cmd & DWC3_DEPCMD_CMDACT) ||
371
+ (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
372
+ !(cmd & DWC3_DEPCMD_CMDIOC))) {
373
+ ret = 0;
374
+ goto skip_status;
375
+ }
376
+
521377 do {
522378 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
523379 if (!(reg & DWC3_DEPCMD_CMDACT)) {
....@@ -528,6 +384,8 @@
528384 ret = 0;
529385 break;
530386 case DEPEVT_TRANSFER_NO_RESOURCE:
387
+ dev_WARN(dwc->dev, "No resource for %s\n",
388
+ dep->name);
531389 ret = -EINVAL;
532390 break;
533391 case DEPEVT_TRANSFER_BUS_EXPIRY:
....@@ -557,11 +415,15 @@
557415 cmd_status = -ETIMEDOUT;
558416 }
559417
418
+skip_status:
560419 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
561420
562
- if (ret == 0 && DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
563
- dep->flags |= DWC3_EP_TRANSFER_STARTED;
564
- dwc3_gadget_ep_get_transfer_index(dep);
421
+ if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
422
+ if (ret == 0)
423
+ dep->flags |= DWC3_EP_TRANSFER_STARTED;
424
+
425
+ if (ret != -ETIMEDOUT)
426
+ dwc3_gadget_ep_get_transfer_index(dep);
565427 }
566428
567429 if (saved_config) {
....@@ -572,6 +434,7 @@
572434
573435 return ret;
574436 }
437
+EXPORT_SYMBOL_GPL(dwc3_send_gadget_ep_cmd);
575438
576439 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
577440 {
....@@ -587,8 +450,9 @@
587450 * IN transfers due to a mishandled error condition. Synopsys
588451 * STAR 9000614252.
589452 */
590
- if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
591
- (dwc->gadget.speed >= USB_SPEED_SUPER))
453
+ if (dep->direction &&
454
+ !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
455
+ (dwc->gadget->speed >= USB_SPEED_SUPER))
592456 cmd |= DWC3_DEPCMD_CLEARPENDIN;
593457
594458 memset(&params, 0, sizeof(params));
....@@ -728,8 +592,9 @@
728592 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
729593
730594 /* Burst size is only needed in SuperSpeed mode */
731
- if (dwc->gadget.speed >= USB_SPEED_SUPER) {
595
+ if (dwc->gadget->speed >= USB_SPEED_SUPER) {
732596 u32 burst = dep->endpoint.maxburst;
597
+
733598 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
734599 }
735600
....@@ -745,6 +610,7 @@
745610
746611 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
747612 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
613
+ | DWC3_DEPCFG_XFER_COMPLETE_EN
748614 | DWC3_DEPCFG_STREAM_EVENT_EN;
749615 dep->stream_capable = true;
750616 }
....@@ -781,7 +647,7 @@
781647 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
782648
783649 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
784
- dwc->gadget.speed == USB_SPEED_FULL)
650
+ dwc->gadget->speed == USB_SPEED_FULL)
785651 dep->interval = desc->bInterval;
786652 else
787653 dep->interval = 1 << (desc->bInterval - 1);
....@@ -790,6 +656,335 @@
790656 }
791657
792658 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
659
+}
660
+
661
+/**
662
+ * dwc3_gadget_get_tx_fifos_size - Get the txfifos total size
663
+ * @dwc: pointer to the DWC3 context
664
+ *
665
+ * 3-RAM configuration:
666
+ * RAM0 depth = Descriptor Cache depth
667
+ * RAM1 depth = TxFIFOs depth
668
+ * RAM2 depth = RxFIFOs depth
669
+ *
670
+ * 2-RAM configuration:
671
+ * RAM0 depth = Descriptor Cache depth + RxFIFOs depth
672
+ * RAM1 depth = TxFIFOs depth
673
+ *
674
+ * 1-RAM configuration:
675
+ * RAM0 depth = Descriptor Cache depth + RxFIFOs depth + TxFIFOs depth
676
+ */
677
+static int dwc3_gadget_get_tx_fifos_size(struct dwc3 *dwc)
678
+{
679
+ int txfifo_depth = 0;
680
+ int ram0_depth, rxfifo_size;
681
+
682
+ /* Get the depth of the TxFIFOs */
683
+ if (DWC3_NUM_RAMS(dwc->hwparams.hwparams1) > 1) {
684
+ /* For 2 or 3-RAM, RAM1 contains TxFIFOs */
685
+ txfifo_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
686
+ } else {
687
+ /* For 1-RAM, RAM0 contains Descriptor Cache, RxFIFOs, and TxFIFOs */
688
+ ram0_depth = DWC3_GHWPARAMS6_RAM0_DEPTH(dwc->hwparams.hwparams6);
689
+
690
+ /* All OUT endpoints share a single RxFIFO space */
691
+ rxfifo_size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
692
+ if (DWC3_IP_IS(DWC3))
693
+ txfifo_depth = ram0_depth - DWC3_GRXFIFOSIZ_RXFDEP(rxfifo_size);
694
+ else
695
+ txfifo_depth = ram0_depth - DWC31_GRXFIFOSIZ_RXFDEP(rxfifo_size);
696
+
697
+ /* The value of GRxFIFOSIZ0[31:16] is the depth of Descriptor Cache */
698
+ txfifo_depth -= DWC3_GRXFIFOSIZ_RXFSTADDR(rxfifo_size) >> 16;
699
+ }
700
+
701
+ return txfifo_depth;
702
+}
703
+
704
+/**
705
+ * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
706
+ * @dwc: pointer to the DWC3 context
707
+ * @nfifos: number of fifos to calculate for
708
+ *
709
+ * Calculates the size value based on the equation below:
710
+ *
711
+ * DWC3 revision 280A and prior:
712
+ * fifo_size = mult * (max_packet / mdwidth) + 1;
713
+ *
714
+ * DWC3 revision 290A and onwards:
715
+ * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
716
+ *
717
+ * The max packet size is set to 1024, as the txfifo requirements mainly apply
718
+ * to super speed USB use cases. However, it is safe to overestimate the fifo
719
+ * allocations for other scenarios, i.e. high speed USB.
720
+ */
721
+static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
722
+{
723
+ int max_packet = 1024;
724
+ int fifo_size;
725
+ int mdwidth;
726
+
727
+ mdwidth = dwc3_mdwidth(dwc);
728
+
729
+ /* MDWIDTH is represented in bits, we need it in bytes */
730
+ mdwidth >>= 3;
731
+
732
+ if (DWC3_VER_IS_PRIOR(DWC3, 290A))
733
+ fifo_size = mult * (max_packet / mdwidth) + 1;
734
+ else
735
+ fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
736
+ return fifo_size;
737
+}
738
+
739
+/**
740
+ * dwc3_gadget_clear_tx_fifo_size - Clears txfifo allocation
741
+ * @dwc: pointer to the DWC3 context
742
+ *
743
+ * Iterates through all the endpoint registers and clears the previous txfifo
744
+ * allocations.
745
+ */
746
+void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
747
+{
748
+ struct dwc3_ep *dep;
749
+ int fifo_depth;
750
+ int size;
751
+ int num;
752
+
753
+ if (!dwc->do_fifo_resize)
754
+ return;
755
+
756
+ /* Read ep0IN related TXFIFO size */
757
+ dep = dwc->eps[1];
758
+ size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
759
+ if (DWC3_IP_IS(DWC3))
760
+ fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
761
+ else
762
+ fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
763
+
764
+ dwc->last_fifo_depth = fifo_depth;
765
+ /* Clear existing TXFIFO for all IN eps except ep0 */
766
+ for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
767
+ num += 2) {
768
+ dep = dwc->eps[num];
769
+ /* Don't change TXFRAMNUM on usb31 version */
770
+ size = DWC3_IP_IS(DWC3) ? 0 :
771
+ dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
772
+ DWC31_GTXFIFOSIZ_TXFRAMNUM;
773
+
774
+ dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
775
+ dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
776
+ }
777
+ dwc->num_ep_resized = 0;
778
+}
779
+
780
+/**
781
+ * __dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for Rockchip platform
782
+ *
783
+ * @dep: pointer to dwc3_ep structure
784
+ *
785
+ * According to the different USB transfer type and Speed,
786
+ * this function will a best effort FIFO allocation in order
787
+ * to improve FIFO usage and throughput, while still allowing
788
+ * us to enable as many endpoints as possible.
789
+ */
790
+static int __dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
791
+{
792
+ struct dwc3 *dwc = dep->dwc;
793
+ u32 fifo_0_start, last_fifo_depth, ram1_depth;
794
+ u32 fifo_size, maxpacket, mdwidth, mult;
795
+ u32 tmp;
796
+
797
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
798
+ /*
799
+ * Set enough tx fifos for Isochronous endpoints to get better
800
+ * performance and more compliance with bus latency.
801
+ */
802
+ maxpacket = dep->endpoint.maxpacket;
803
+ if (gadget_is_superspeed(dwc->gadget))
804
+ mult = dep->endpoint.mult * dep->endpoint.maxburst;
805
+ else
806
+ mult = dep->endpoint.mult;
807
+
808
+ mult = mult > 0 ? mult * 2 : 3;
809
+ if (mult > 6)
810
+ mult = 6;
811
+ } else if (usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
812
+ /*
813
+ * Set enough tx fifos for Bulk endpoints to get
814
+ * better transmission performance.
815
+ */
816
+ mult = 3;
817
+ if (gadget_is_superspeed(dwc->gadget)) {
818
+ if (dep->endpoint.maxburst > mult) {
819
+ mult = dep->endpoint.maxburst;
820
+ if (mult > 6)
821
+ mult = 6;
822
+ }
823
+ maxpacket = 1024;
824
+ } else {
825
+ maxpacket = 512;
826
+ }
827
+ } else if (usb_endpoint_xfer_int(dep->endpoint.desc)) {
828
+ /*
829
+ * REVIST: we assume that the maxpacket of interrupt
830
+ * endpoint is 64 Bytes for MTP and the other functions.
831
+ */
832
+ mult = 1;
833
+ maxpacket = 64;
834
+ } else {
835
+ goto out;
836
+ }
837
+
838
+ mdwidth = dwc3_mdwidth(dwc);
839
+ mdwidth >>= 3; /* bits convert to bytes */
840
+ ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
841
+ last_fifo_depth = dwc->last_fifo_depth;
842
+
843
+ /* Calculate the fifo size for this EP */
844
+ tmp = mult * (maxpacket + mdwidth);
845
+ tmp += mdwidth;
846
+ fifo_size = DIV_ROUND_UP(tmp, mdwidth);
847
+
848
+ /* Check if TXFIFOs start at non-zero addr */
849
+ tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
850
+ fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
851
+ fifo_size |= (fifo_0_start + (last_fifo_depth << 16));
852
+
853
+ if (DWC3_IP_IS(DWC3))
854
+ last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
855
+ else
856
+ last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
857
+
858
+ /* Check fifo size allocation doesn't exceed available RAM size. */
859
+ if (last_fifo_depth >= ram1_depth) {
860
+ dev_err(dwc->dev, "Fifosize(0x%x) > RAM size(0x%x) %s depth(0x%x)\n",
861
+ last_fifo_depth, ram1_depth,
862
+ dep->endpoint.name, fifo_size & 0xfff);
863
+ return -ENOMEM;
864
+ }
865
+
866
+ dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
867
+ dep->flags |= DWC3_EP_TXFIFO_RESIZED;
868
+ dwc->last_fifo_depth = last_fifo_depth;
869
+ dwc->num_ep_resized++;
870
+
871
+out:
872
+ return 0;
873
+}
874
+
875
+/*
876
+ * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
877
+ * @dwc: pointer to our context structure
878
+ *
879
+ * This function will a best effort FIFO allocation in order
880
+ * to improve FIFO usage and throughput, while still allowing
881
+ * us to enable as many endpoints as possible.
882
+ *
883
+ * Keep in mind that this operation will be highly dependent
884
+ * on the configured size for RAM1 - which contains TxFifo -,
885
+ * the amount of endpoints enabled on coreConsultant tool, and
886
+ * the width of the Master Bus.
887
+ *
888
+ * In general, FIFO depths are represented with the following equation:
889
+ *
890
+ * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
891
+ *
892
+ * In conjunction with dwc3_gadget_check_config(), this resizing logic will
893
+ * ensure that all endpoints will have enough internal memory for one max
894
+ * packet per endpoint.
895
+ */
896
+static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
897
+{
898
+ struct dwc3 *dwc = dep->dwc;
899
+ int fifo_0_start;
900
+ int ram1_depth;
901
+ int fifo_size;
902
+ int min_depth;
903
+ int num_in_ep;
904
+ int remaining;
905
+ int num_fifos = 1;
906
+ int fifo;
907
+ int tmp;
908
+
909
+ if (!dwc->do_fifo_resize)
910
+ return 0;
911
+
912
+ /* resize IN endpoints except ep0 */
913
+ if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
914
+ return 0;
915
+
916
+ /* bail if already resized */
917
+ if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
918
+ return 0;
919
+
920
+ if (IS_REACHABLE(CONFIG_ARCH_ROCKCHIP))
921
+ return __dwc3_gadget_resize_tx_fifos(dep);
922
+
923
+ ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
924
+
925
+ if ((dep->endpoint.maxburst > 1 &&
926
+ usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
927
+ usb_endpoint_xfer_isoc(dep->endpoint.desc))
928
+ num_fifos = 3;
929
+
930
+ if (dep->endpoint.maxburst > 6 &&
931
+ (usb_endpoint_xfer_bulk(dep->endpoint.desc) ||
932
+ usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31))
933
+ num_fifos = dwc->tx_fifo_resize_max_num;
934
+
935
+ /* FIFO size for a single buffer */
936
+ fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
937
+
938
+ /* Calculate the number of remaining EPs w/o any FIFO */
939
+ num_in_ep = dwc->max_cfg_eps;
940
+ num_in_ep -= dwc->num_ep_resized;
941
+
942
+ /* Reserve at least one FIFO for the number of IN EPs */
943
+ min_depth = num_in_ep * (fifo + 1);
944
+ remaining = ram1_depth - min_depth - dwc->last_fifo_depth;
945
+ remaining = max_t(int, 0, remaining);
946
+ /*
947
+ * We've already reserved 1 FIFO per EP, so check what we can fit in
948
+ * addition to it. If there is not enough remaining space, allocate
949
+ * all the remaining space to the EP.
950
+ */
951
+ fifo_size = (num_fifos - 1) * fifo;
952
+ if (remaining < fifo_size)
953
+ fifo_size = remaining;
954
+
955
+ fifo_size += fifo;
956
+ /* Last increment according to the TX FIFO size equation */
957
+ fifo_size++;
958
+
959
+ /* Check if TXFIFOs start at non-zero addr */
960
+ tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
961
+ fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
962
+
963
+ fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
964
+ if (DWC3_IP_IS(DWC3))
965
+ dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
966
+ else
967
+ dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
968
+
969
+ /* Check fifo size allocation doesn't exceed available RAM size. */
970
+ if (dwc->last_fifo_depth >= ram1_depth) {
971
+ dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
972
+ dwc->last_fifo_depth, ram1_depth,
973
+ dep->endpoint.name, fifo_size);
974
+ if (DWC3_IP_IS(DWC3))
975
+ fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
976
+ else
977
+ fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
978
+
979
+ dwc->last_fifo_depth -= fifo_size;
980
+ return -ENOMEM;
981
+ }
982
+
983
+ dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
984
+ dep->flags |= DWC3_EP_TXFIFO_RESIZED;
985
+ dwc->num_ep_resized++;
986
+
987
+ return 0;
793988 }
794989
795990 /**
....@@ -809,6 +1004,10 @@
8091004 int ret;
8101005
8111006 if (!(dep->flags & DWC3_EP_ENABLED)) {
1007
+ ret = dwc3_gadget_resize_tx_fifos(dep);
1008
+ if (ret)
1009
+ return ret;
1010
+
8121011 ret = dwc3_gadget_start_config(dep);
8131012 if (ret)
8141013 return ret;
....@@ -829,12 +1028,13 @@
8291028 reg |= DWC3_DALEPENA_EP(dep->number);
8301029 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
8311030
1031
+ dep->trb_dequeue = 0;
1032
+ dep->trb_enqueue = 0;
1033
+
8321034 if (usb_endpoint_xfer_control(desc))
8331035 goto out;
8341036
8351037 /* Initialize the TRB ring */
836
- dep->trb_dequeue = 0;
837
- dep->trb_enqueue = 0;
8381038 memset(dep->trb_pool, 0,
8391039 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
8401040
....@@ -852,7 +1052,7 @@
8521052 * Issue StartTransfer here with no-op TRB so we can always rely on No
8531053 * Response Update Transfer command.
8541054 */
855
- if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
1055
+ if (usb_endpoint_xfer_bulk(desc) ||
8561056 usb_endpoint_xfer_int(desc)) {
8571057 struct dwc3_gadget_ep_cmd_params params;
8581058 struct dwc3_trb *trb;
....@@ -871,6 +1071,37 @@
8711071 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
8721072 if (ret < 0)
8731073 return ret;
1074
+
1075
+ if (dep->stream_capable) {
1076
+ /*
1077
+ * For streams, at start, there maybe a race where the
1078
+ * host primes the endpoint before the function driver
1079
+ * queues a request to initiate a stream. In that case,
1080
+ * the controller will not see the prime to generate the
1081
+ * ERDY and start stream. To workaround this, issue a
1082
+ * no-op TRB as normal, but end it immediately. As a
1083
+ * result, when the function driver queues the request,
1084
+ * the next START_TRANSFER command will cause the
1085
+ * controller to generate an ERDY to initiate the
1086
+ * stream.
1087
+ */
1088
+ dwc3_stop_active_transfer(dep, true, true);
1089
+
1090
+ /*
1091
+ * All stream eps will reinitiate stream on NoStream
1092
+ * rejection until we can determine that the host can
1093
+ * prime after the first transfer.
1094
+ *
1095
+ * However, if the controller is capable of
1096
+ * TXF_FLUSH_BYPASS, then IN direction endpoints will
1097
+ * automatically restart the stream without the driver
1098
+ * initiation.
1099
+ */
1100
+ if (!dep->direction ||
1101
+ !(dwc->hwparams.hwparams9 &
1102
+ DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
1103
+ dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
1104
+ }
8741105 }
8751106
8761107 out:
....@@ -879,31 +1110,33 @@
8791110 return 0;
8801111 }
8811112
882
-static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
883
- bool interrupt);
884
-static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
1113
+void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
8851114 {
8861115 struct dwc3_request *req;
8871116
8881117 dwc3_stop_active_transfer(dep, true, false);
8891118
1119
+ /* If endxfer is delayed, avoid unmapping requests */
1120
+ if (dep->flags & DWC3_EP_DELAY_STOP)
1121
+ return;
1122
+
8901123 /* - giveback all requests to gadget driver */
8911124 while (!list_empty(&dep->started_list)) {
8921125 req = next_request(&dep->started_list);
8931126
894
- dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1127
+ dwc3_gadget_giveback(dep, req, status);
8951128 }
8961129
8971130 while (!list_empty(&dep->pending_list)) {
8981131 req = next_request(&dep->pending_list);
8991132
900
- dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1133
+ dwc3_gadget_giveback(dep, req, status);
9011134 }
9021135
9031136 while (!list_empty(&dep->cancelled_list)) {
9041137 req = next_request(&dep->cancelled_list);
9051138
906
- dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1139
+ dwc3_gadget_giveback(dep, req, status);
9071140 }
9081141 }
9091142
....@@ -921,10 +1154,9 @@
9211154 {
9221155 struct dwc3 *dwc = dep->dwc;
9231156 u32 reg;
1157
+ u32 mask;
9241158
9251159 trace_dwc3_gadget_ep_disable(dep);
926
-
927
- dwc3_remove_requests(dwc, dep);
9281160
9291161 /* make sure HW endpoint isn't stalled */
9301162 if (dep->flags & DWC3_EP_STALL)
....@@ -934,9 +1166,19 @@
9341166 reg &= ~DWC3_DALEPENA_EP(dep->number);
9351167 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
9361168
1169
+ dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
1170
+
9371171 dep->stream_capable = false;
9381172 dep->type = 0;
939
- dep->flags = 0;
1173
+ mask = DWC3_EP_TXFIFO_RESIZED;
1174
+ /*
1175
+ * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is
1176
+ * set. Do not clear DEP flags, so that the end transfer command will
1177
+ * be reattempted during the next SETUP stage.
1178
+ */
1179
+ if (dep->flags & DWC3_EP_DELAY_STOP)
1180
+ mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
1181
+ dep->flags &= mask;
9401182
9411183 /* Clear out the ep descriptors for non-ep0 */
9421184 if (dep->number > 1) {
....@@ -1099,15 +1341,49 @@
10991341 return trbs_left;
11001342 }
11011343
1102
-static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
1103
- dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
1104
- unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
1344
+/**
1345
+ * dwc3_prepare_one_trb - setup one TRB from one request
1346
+ * @dep: endpoint for which this request is prepared
1347
+ * @req: dwc3_request pointer
1348
+ * @trb_length: buffer size of the TRB
1349
+ * @chain: should this TRB be chained to the next?
1350
+ * @node: only for isochronous endpoints. First TRB needs different type.
1351
+ * @use_bounce_buffer: set to use bounce buffer
1352
+ * @must_interrupt: set to interrupt on TRB completion
1353
+ */
1354
+static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1355
+ struct dwc3_request *req, unsigned int trb_length,
1356
+ unsigned int chain, unsigned int node, bool use_bounce_buffer,
1357
+ bool must_interrupt)
11051358 {
1359
+ struct dwc3_trb *trb;
1360
+ dma_addr_t dma;
1361
+ unsigned int stream_id = req->request.stream_id;
1362
+ unsigned int short_not_ok = req->request.short_not_ok;
1363
+ unsigned int no_interrupt = req->request.no_interrupt;
1364
+ unsigned int is_last = req->request.is_last;
11061365 struct dwc3 *dwc = dep->dwc;
1107
- struct usb_gadget *gadget = &dwc->gadget;
1366
+ struct usb_gadget *gadget = dwc->gadget;
11081367 enum usb_device_speed speed = gadget->speed;
11091368
1110
- trb->size = DWC3_TRB_SIZE_LENGTH(length);
1369
+ if (use_bounce_buffer)
1370
+ dma = dep->dwc->bounce_addr;
1371
+ else if (req->request.num_sgs > 0)
1372
+ dma = sg_dma_address(req->start_sg);
1373
+ else
1374
+ dma = req->request.dma;
1375
+
1376
+ trb = &dep->trb_pool[dep->trb_enqueue];
1377
+
1378
+ if (!req->trb) {
1379
+ dwc3_gadget_move_started_request(req);
1380
+ req->trb = trb;
1381
+ req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1382
+ }
1383
+
1384
+ req->num_trbs++;
1385
+
1386
+ trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
11111387 trb->bpl = lower_32_bits(dma);
11121388 trb->bph = upper_32_bits(dma);
11131389
....@@ -1147,10 +1423,10 @@
11471423 unsigned int mult = 2;
11481424 unsigned int maxp = usb_endpoint_maxp(ep->desc);
11491425
1150
- if (length <= (2 * maxp))
1426
+ if (req->request.length <= (2 * maxp))
11511427 mult--;
11521428
1153
- if (length <= maxp)
1429
+ if (req->request.length <= maxp)
11541430 mult--;
11551431
11561432 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
....@@ -1159,8 +1435,8 @@
11591435 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
11601436 }
11611437
1162
- /* always enable Interrupt on Missed ISOC */
1163
- trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1438
+ if (!no_interrupt && !chain)
1439
+ trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
11641440 break;
11651441
11661442 case USB_ENDPOINT_XFER_BULK:
....@@ -1188,12 +1464,13 @@
11881464 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
11891465 }
11901466
1191
- if ((!no_interrupt && !chain) ||
1192
- (dwc3_calc_trbs_left(dep) == 1))
1467
+ if ((!no_interrupt && !chain) || must_interrupt)
11931468 trb->ctrl |= DWC3_TRB_CTRL_IOC;
11941469
11951470 if (chain)
11961471 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1472
+ else if (dep->stream_capable && is_last)
1473
+ trb->ctrl |= DWC3_TRB_CTRL_LST;
11971474
11981475 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
11991476 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
....@@ -1218,54 +1495,69 @@
12181495 trace_dwc3_prepare_trb(dep, trb);
12191496 }
12201497
1221
-/**
1222
- * dwc3_prepare_one_trb - setup one TRB from one request
1223
- * @dep: endpoint for which this request is prepared
1224
- * @req: dwc3_request pointer
1225
- * @trb_length: buffer size of the TRB
1226
- * @chain: should this TRB be chained to the next?
1227
- * @node: only for isochronous endpoints. First TRB needs different type.
1228
- */
1229
-static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1230
- struct dwc3_request *req, unsigned int trb_length,
1231
- unsigned chain, unsigned node)
1498
+static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
12321499 {
1233
- struct dwc3_trb *trb;
1234
- dma_addr_t dma;
1235
- unsigned stream_id = req->request.stream_id;
1236
- unsigned short_not_ok = req->request.short_not_ok;
1237
- unsigned no_interrupt = req->request.no_interrupt;
1500
+ unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1501
+ unsigned int rem = req->request.length % maxp;
12381502
1239
- if (req->request.num_sgs > 0)
1240
- dma = sg_dma_address(req->start_sg);
1241
- else
1242
- dma = req->request.dma;
1503
+ if ((req->request.length && req->request.zero && !rem &&
1504
+ !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1505
+ (!req->direction && rem))
1506
+ return true;
12431507
1244
- trb = &dep->trb_pool[dep->trb_enqueue];
1245
-
1246
- if (!req->trb) {
1247
- dwc3_gadget_move_started_request(req);
1248
- req->trb = trb;
1249
- req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1250
- }
1251
-
1252
- req->num_trbs++;
1253
-
1254
- __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
1255
- stream_id, short_not_ok, no_interrupt);
1508
+ return false;
12561509 }
12571510
1258
-static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1511
+/**
1512
+ * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1513
+ * @dep: The endpoint that the request belongs to
1514
+ * @req: The request to prepare
1515
+ * @entry_length: The last SG entry size
1516
+ * @node: Indicates whether this is not the first entry (for isoc only)
1517
+ *
1518
+ * Return the number of TRBs prepared.
1519
+ */
1520
+static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1521
+ struct dwc3_request *req, unsigned int entry_length,
1522
+ unsigned int node)
1523
+{
1524
+ unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1525
+ unsigned int rem = req->request.length % maxp;
1526
+ unsigned int num_trbs = 1;
1527
+
1528
+ if (dwc3_needs_extra_trb(dep, req))
1529
+ num_trbs++;
1530
+
1531
+ if (dwc3_calc_trbs_left(dep) < num_trbs)
1532
+ return 0;
1533
+
1534
+ req->needs_extra_trb = num_trbs > 1;
1535
+
1536
+ /* Prepare a normal TRB */
1537
+ if (req->direction || req->request.length)
1538
+ dwc3_prepare_one_trb(dep, req, entry_length,
1539
+ req->needs_extra_trb, node, false, false);
1540
+
1541
+ /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1542
+ if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1543
+ dwc3_prepare_one_trb(dep, req,
1544
+ req->direction ? 0 : maxp - rem,
1545
+ false, 1, true, false);
1546
+
1547
+ return num_trbs;
1548
+}
1549
+
1550
+static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
12591551 struct dwc3_request *req)
12601552 {
12611553 struct scatterlist *sg = req->start_sg;
12621554 struct scatterlist *s;
12631555 int i;
12641556 unsigned int length = req->request.length;
1265
- unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1266
- unsigned int rem = length % maxp;
12671557 unsigned int remaining = req->request.num_mapped_sgs
12681558 - req->num_queued_sgs;
1559
+ unsigned int num_trbs = req->num_trbs;
1560
+ bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
12691561
12701562 /*
12711563 * If we resume preparing the request, then get the remaining length of
....@@ -1275,8 +1567,10 @@
12751567 length -= sg_dma_len(s);
12761568
12771569 for_each_sg(sg, s, remaining, i) {
1570
+ unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
12781571 unsigned int trb_length;
1279
- unsigned chain = true;
1572
+ bool must_interrupt = false;
1573
+ bool last_sg = false;
12801574
12811575 trb_length = min_t(unsigned int, length, sg_dma_len(s));
12821576
....@@ -1290,56 +1584,28 @@
12901584 * mapped sg.
12911585 */
12921586 if ((i == remaining - 1) || !length)
1293
- chain = false;
1587
+ last_sg = true;
12941588
1295
- if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1296
- struct dwc3 *dwc = dep->dwc;
1297
- struct dwc3_trb *trb;
1589
+ if (!num_trbs_left)
1590
+ break;
12981591
1299
- req->needs_extra_trb = true;
1300
-
1301
- /* prepare normal TRB */
1302
- dwc3_prepare_one_trb(dep, req, trb_length, true, i);
1303
-
1304
- /* Now prepare one extra TRB to align transfer size */
1305
- trb = &dep->trb_pool[dep->trb_enqueue];
1306
- req->num_trbs++;
1307
- __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1308
- maxp - rem, false, 1,
1309
- req->request.stream_id,
1310
- req->request.short_not_ok,
1311
- req->request.no_interrupt);
1312
- } else if (req->request.zero && req->request.length &&
1313
- !usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1314
- !rem && !chain) {
1315
- struct dwc3 *dwc = dep->dwc;
1316
- struct dwc3_trb *trb;
1317
-
1318
- req->needs_extra_trb = true;
1319
-
1320
- /* Prepare normal TRB */
1321
- dwc3_prepare_one_trb(dep, req, trb_length, true, i);
1322
-
1323
- /* Prepare one extra TRB to handle ZLP */
1324
- trb = &dep->trb_pool[dep->trb_enqueue];
1325
- req->num_trbs++;
1326
- __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1327
- !req->direction, 1,
1328
- req->request.stream_id,
1329
- req->request.short_not_ok,
1330
- req->request.no_interrupt);
1331
-
1332
- /* Prepare one more TRB to handle MPS alignment */
1333
- if (!req->direction) {
1334
- trb = &dep->trb_pool[dep->trb_enqueue];
1335
- req->num_trbs++;
1336
- __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp,
1337
- false, 1, req->request.stream_id,
1338
- req->request.short_not_ok,
1339
- req->request.no_interrupt);
1340
- }
1592
+ if (last_sg) {
1593
+ if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1594
+ break;
13411595 } else {
1342
- dwc3_prepare_one_trb(dep, req, trb_length, chain, i);
1596
+ /*
1597
+ * Look ahead to check if we have enough TRBs for the
1598
+ * next SG entry. If not, set interrupt on this TRB to
1599
+ * resume preparing the next SG entry when more TRBs are
1600
+ * free.
1601
+ */
1602
+ if (num_trbs_left == 1 || (needs_extra_trb &&
1603
+ num_trbs_left <= 2 &&
1604
+ sg_dma_len(sg_next(s)) >= length))
1605
+ must_interrupt = true;
1606
+
1607
+ dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1608
+ must_interrupt);
13431609 }
13441610
13451611 /*
....@@ -1349,7 +1615,7 @@
13491615 * we have free trbs we can continue queuing from where we
13501616 * previously stopped
13511617 */
1352
- if (chain)
1618
+ if (!last_sg)
13531619 req->start_sg = sg_next(s);
13541620
13551621 req->num_queued_sgs++;
....@@ -1365,65 +1631,17 @@
13651631 break;
13661632 }
13671633
1368
- if (!dwc3_calc_trbs_left(dep))
1634
+ if (must_interrupt)
13691635 break;
13701636 }
1637
+
1638
+ return req->num_trbs - num_trbs;
13711639 }
13721640
1373
-static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1641
+static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
13741642 struct dwc3_request *req)
13751643 {
1376
- unsigned int length = req->request.length;
1377
- unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1378
- unsigned int rem = length % maxp;
1379
-
1380
- if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
1381
- struct dwc3 *dwc = dep->dwc;
1382
- struct dwc3_trb *trb;
1383
-
1384
- req->needs_extra_trb = true;
1385
-
1386
- /* prepare normal TRB */
1387
- dwc3_prepare_one_trb(dep, req, length, true, 0);
1388
-
1389
- /* Now prepare one extra TRB to align transfer size */
1390
- trb = &dep->trb_pool[dep->trb_enqueue];
1391
- req->num_trbs++;
1392
- __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1393
- false, 1, req->request.stream_id,
1394
- req->request.short_not_ok,
1395
- req->request.no_interrupt);
1396
- } else if (req->request.zero && req->request.length &&
1397
- !usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1398
- (IS_ALIGNED(req->request.length, maxp))) {
1399
- struct dwc3 *dwc = dep->dwc;
1400
- struct dwc3_trb *trb;
1401
-
1402
- req->needs_extra_trb = true;
1403
-
1404
- /* prepare normal TRB */
1405
- dwc3_prepare_one_trb(dep, req, length, true, 0);
1406
-
1407
- /* Prepare one extra TRB to handle ZLP */
1408
- trb = &dep->trb_pool[dep->trb_enqueue];
1409
- req->num_trbs++;
1410
- __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1411
- !req->direction, 1, req->request.stream_id,
1412
- req->request.short_not_ok,
1413
- req->request.no_interrupt);
1414
-
1415
- /* Prepare one more TRB to handle MPS alignment for OUT */
1416
- if (!req->direction) {
1417
- trb = &dep->trb_pool[dep->trb_enqueue];
1418
- req->num_trbs++;
1419
- __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp,
1420
- false, 1, req->request.stream_id,
1421
- req->request.short_not_ok,
1422
- req->request.no_interrupt);
1423
- }
1424
- } else {
1425
- dwc3_prepare_one_trb(dep, req, length, false, 0);
1426
- }
1644
+ return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
14271645 }
14281646
14291647 /*
....@@ -1433,10 +1651,13 @@
14331651 * The function goes through the requests list and sets up TRBs for the
14341652 * transfers. The function returns once there are no more TRBs available or
14351653 * it runs out of requests.
1654
+ *
1655
+ * Returns the number of TRBs prepared or negative errno.
14361656 */
1437
-static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1657
+static int dwc3_prepare_trbs(struct dwc3_ep *dep)
14381658 {
14391659 struct dwc3_request *req, *n;
1660
+ int ret = 0;
14401661
14411662 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
14421663
....@@ -1451,35 +1672,58 @@
14511672 * break things.
14521673 */
14531674 list_for_each_entry(req, &dep->started_list, list) {
1454
- if (req->num_pending_sgs > 0)
1455
- dwc3_prepare_one_trb_sg(dep, req);
1675
+ if (req->num_pending_sgs > 0) {
1676
+ ret = dwc3_prepare_trbs_sg(dep, req);
1677
+ if (!ret || req->num_pending_sgs)
1678
+ return ret;
1679
+ }
14561680
14571681 if (!dwc3_calc_trbs_left(dep))
1458
- return;
1682
+ return ret;
1683
+
1684
+ /*
1685
+ * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1686
+ * burst capability may try to read and use TRBs beyond the
1687
+ * active transfer instead of stopping.
1688
+ */
1689
+ if (dep->stream_capable && req->request.is_last)
1690
+ return ret;
14591691 }
14601692
14611693 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
14621694 struct dwc3 *dwc = dep->dwc;
1463
- int ret;
14641695
14651696 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
14661697 dep->direction);
14671698 if (ret)
1468
- return;
1699
+ return ret;
14691700
14701701 req->sg = req->request.sg;
14711702 req->start_sg = req->sg;
14721703 req->num_queued_sgs = 0;
14731704 req->num_pending_sgs = req->request.num_mapped_sgs;
14741705
1475
- if (req->num_pending_sgs > 0)
1476
- dwc3_prepare_one_trb_sg(dep, req);
1477
- else
1478
- dwc3_prepare_one_trb_linear(dep, req);
1706
+ if (req->num_pending_sgs > 0) {
1707
+ ret = dwc3_prepare_trbs_sg(dep, req);
1708
+ if (req->num_pending_sgs)
1709
+ return ret;
1710
+ } else {
1711
+ ret = dwc3_prepare_trbs_linear(dep, req);
1712
+ }
14791713
1480
- if (!dwc3_calc_trbs_left(dep))
1481
- return;
1714
+ if (!ret || !dwc3_calc_trbs_left(dep))
1715
+ return ret;
1716
+
1717
+ /*
1718
+ * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1719
+ * burst capability may try to read and use TRBs beyond the
1720
+ * active transfer instead of stopping.
1721
+ */
1722
+ if (dep->stream_capable && req->request.is_last)
1723
+ return ret;
14821724 }
1725
+
1726
+ return ret;
14831727 }
14841728
14851729 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
....@@ -1492,12 +1736,24 @@
14921736 int ret;
14931737 u32 cmd;
14941738
1495
- if (!dwc3_calc_trbs_left(dep))
1496
- return 0;
1739
+ /*
1740
+ * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1741
+ * This happens when we need to stop and restart a transfer such as in
1742
+ * the case of reinitiating a stream or retrying an isoc transfer.
1743
+ */
1744
+ ret = dwc3_prepare_trbs(dep);
1745
+ if (ret < 0)
1746
+ return ret;
14971747
14981748 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
14991749
1500
- dwc3_prepare_trbs(dep);
1750
+ /*
1751
+ * If there's no new TRB prepared and we don't need to restart a
1752
+ * transfer, there's no need to update the transfer.
1753
+ */
1754
+ if (!ret && !starting)
1755
+ return ret;
1756
+
15011757 req = next_request(&dep->started_list);
15021758 if (!req) {
15031759 dep->flags |= DWC3_EP_PENDING_REQUEST;
....@@ -1525,22 +1781,13 @@
15251781 if (ret < 0) {
15261782 struct dwc3_request *tmp;
15271783
1528
- /*
1529
- * Isochronous endpoints request needs to
1530
- * return directly and retry to transfer next
1531
- * time. Otherwise, it will fail to giveback
1532
- * the req to the udc gadget driver.
1533
- */
1534
- if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1535
- return ret;
1536
-
15371784 if (ret == -EAGAIN)
15381785 return ret;
15391786
15401787 dwc3_stop_active_transfer(dep, true, true);
15411788
15421789 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1543
- dwc3_gadget_move_cancelled_request(req);
1790
+ dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
15441791
15451792 /* If ep isn't started, then there's no end transfer pending */
15461793 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
....@@ -1548,6 +1795,9 @@
15481795
15491796 return ret;
15501797 }
1798
+
1799
+ if (dep->stream_capable && req->request.is_last)
1800
+ dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
15511801
15521802 return 0;
15531803 }
....@@ -1558,6 +1808,50 @@
15581808
15591809 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
15601810 return DWC3_DSTS_SOFFN(reg);
1811
+}
1812
+
1813
+/**
1814
+ * __dwc3_stop_active_transfer - stop the current active transfer
1815
+ * @dep: isoc endpoint
1816
+ * @force: set forcerm bit in the command
1817
+ * @interrupt: command complete interrupt after End Transfer command
1818
+ *
1819
+ * When setting force, the ForceRM bit will be set. In that case
1820
+ * the controller won't update the TRB progress on command
1821
+ * completion. It also won't clear the HWO bit in the TRB.
1822
+ * The command will also not complete immediately in that case.
1823
+ */
1824
+static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
1825
+{
1826
+ struct dwc3_gadget_ep_cmd_params params;
1827
+ u32 cmd;
1828
+ int ret;
1829
+
1830
+ cmd = DWC3_DEPCMD_ENDTRANSFER;
1831
+ cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1832
+ cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
1833
+ cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1834
+ memset(&params, 0, sizeof(params));
1835
+ ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1836
+ /*
1837
+ * If the End Transfer command was timed out while the device is
1838
+ * not in SETUP phase, it's possible that an incoming Setup packet
1839
+ * may prevent the command's completion. Let's retry when the
1840
+ * ep0state returns to EP0_SETUP_PHASE.
1841
+ */
1842
+ if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) {
1843
+ dep->flags |= DWC3_EP_DELAY_STOP;
1844
+ return 0;
1845
+ }
1846
+ WARN_ON_ONCE(ret);
1847
+ dep->resource_index = 0;
1848
+
1849
+ if (!interrupt)
1850
+ dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1851
+ else
1852
+ dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1853
+
1854
+ return ret;
15611855 }
15621856
15631857 /**
....@@ -1617,7 +1911,7 @@
16171911 * Check if we can start isoc transfer on the next interval or
16181912 * 4 uframes in the future with BIT[15:14] as dep->combo_num
16191913 */
1620
- test_frame_number = dep->frame_number & 0x3fff;
1914
+ test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
16211915 test_frame_number |= dep->combo_num << 14;
16221916 test_frame_number += max_t(u32, 4, dep->interval);
16231917
....@@ -1664,7 +1958,7 @@
16641958 else if (test0 && test1)
16651959 dep->combo_num = 0;
16661960
1667
- dep->frame_number &= 0x3fff;
1961
+ dep->frame_number &= DWC3_FRNUMBER_MASK;
16681962 dep->frame_number |= dep->combo_num << 14;
16691963 dep->frame_number += max_t(u32, 4, dep->interval);
16701964
....@@ -1682,26 +1976,24 @@
16821976 int ret;
16831977 int i;
16841978
1685
- if (list_empty(&dep->pending_list)) {
1979
+ if (list_empty(&dep->pending_list) &&
1980
+ list_empty(&dep->started_list)) {
16861981 dep->flags |= DWC3_EP_PENDING_REQUEST;
16871982 return -EAGAIN;
16881983 }
16891984
1690
- if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1691
- (dwc->revision <= DWC3_USB31_REVISION_160A ||
1692
- (dwc->revision == DWC3_USB31_REVISION_170A &&
1693
- dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1694
- dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1695
-
1696
- if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction)
1985
+ if (!dwc->dis_start_transfer_quirk &&
1986
+ (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1987
+ DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1988
+ if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
16971989 return dwc3_gadget_start_isoc_quirk(dep);
16981990 }
16991991
17001992 if (desc->bInterval <= 14 &&
1701
- dwc->gadget.speed >= USB_SPEED_HIGH) {
1993
+ dwc->gadget->speed >= USB_SPEED_HIGH) {
17021994 u32 frame = __dwc3_gadget_get_frame(dwc);
17031995 bool rollover = frame <
1704
- (dep->frame_number & 0x3fff);
1996
+ (dep->frame_number & DWC3_FRNUMBER_MASK);
17051997
17061998 /*
17071999 * frame_number is set from XferNotReady and may be already
....@@ -1712,54 +2004,46 @@
17122004 * rollover has happened since XferNotReady.
17132005 */
17142006
1715
- dep->frame_number = (dep->frame_number & ~0x3fff) |
2007
+ dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
17162008 frame;
17172009 if (rollover)
17182010 dep->frame_number += BIT(14);
17192011 }
17202012
17212013 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1722
- dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
2014
+ int future_interval = i + 1;
2015
+
2016
+ /* Give the controller at least 500us to schedule transfers */
2017
+ if (desc->bInterval < 3)
2018
+ future_interval += 3 - desc->bInterval;
2019
+
2020
+ dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval);
17232021
17242022 ret = __dwc3_gadget_kick_transfer(dep);
17252023 if (ret != -EAGAIN)
17262024 break;
17272025 }
17282026
1729
- return ret;
1730
-}
1731
-
1732
-static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1733
-{
1734
- int i;
1735
-
17362027 /*
1737
- * If request was already started, this means we had to
1738
- * stop the transfer. With that we also need to ignore
1739
- * all TRBs used by the request, however TRBs can only
1740
- * be modified after completion of END_TRANSFER
1741
- * command. So what we do here is that we wait for
1742
- * END_TRANSFER completion and only after that, we jump
1743
- * over TRBs by clearing HWO and incrementing dequeue
1744
- * pointer.
2028
+ * After a number of unsuccessful start attempts due to bus-expiry
2029
+ * status, issue END_TRANSFER command and retry on the next XferNotReady
2030
+ * event.
17452031 */
1746
- for (i = 0; i < req->num_trbs; i++) {
1747
- struct dwc3_trb *trb;
1748
-
1749
- trb = &dep->trb_pool[dep->trb_dequeue];
1750
- trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1751
- dwc3_ep_inc_deq(dep);
2032
+ if (ret == -EAGAIN) {
2033
+ ret = __dwc3_stop_active_transfer(dep, false, true);
2034
+ if (ret)
2035
+ dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
17522036 }
17532037
1754
- req->num_trbs = 0;
2038
+ return ret;
17552039 }
17562040
17572041 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
17582042 {
17592043 struct dwc3 *dwc = dep->dwc;
17602044
1761
- if (!dep->endpoint.desc) {
1762
- dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
2045
+ if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
2046
+ dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
17632047 dep->name);
17642048 return -ESHUTDOWN;
17652049 }
....@@ -1770,14 +2054,8 @@
17702054
17712055 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
17722056 "%s: request %pK already in flight\n",
1773
- dep->name, &req->request)) {
1774
- if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1775
- dwc3_gadget_ep_skip_trbs(dep, req);
1776
- req->status = DWC3_REQUEST_STATUS_COMPLETED;
1777
- dwc3_gadget_del_and_unmap_request(dep, req, -EINVAL);
1778
- }
2057
+ dep->name, &req->request))
17792058 return -EINVAL;
1780
- }
17812059
17822060 pm_runtime_get(dwc->dev);
17832061
....@@ -1789,8 +2067,17 @@
17892067 list_add_tail(&req->list, &dep->pending_list);
17902068 req->status = DWC3_REQUEST_STATUS_QUEUED;
17912069
1792
- /* Start the transfer only after the END_TRANSFER is completed */
1793
- if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
2070
+ if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
2071
+ return 0;
2072
+
2073
+ /*
2074
+ * Start the transfer only after the END_TRANSFER is completed
2075
+ * and endpoint STALL is cleared.
2076
+ */
2077
+ if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2078
+ (dep->flags & DWC3_EP_WEDGE) ||
2079
+ (dep->flags & DWC3_EP_DELAY_STOP) ||
2080
+ (dep->flags & DWC3_EP_STALL)) {
17942081 dep->flags |= DWC3_EP_DELAY_START;
17952082 return 0;
17962083 }
....@@ -1804,14 +2091,11 @@
18042091 * errors which will force us issue EndTransfer command.
18052092 */
18062093 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1807
- if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1808
- !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1809
- return 0;
1810
-
1811
- if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1812
- if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2094
+ if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2095
+ if ((dep->flags & DWC3_EP_PENDING_REQUEST))
18132096 return __dwc3_gadget_start_isoc(dep);
1814
- }
2097
+
2098
+ return 0;
18152099 }
18162100 }
18172101
....@@ -1838,14 +2122,64 @@
18382122 return ret;
18392123 }
18402124
2125
+static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
2126
+{
2127
+ int i;
2128
+
2129
+ /* If req->trb is not set, then the request has not started */
2130
+ if (!req->trb)
2131
+ return;
2132
+
2133
+ /*
2134
+ * If request was already started, this means we had to
2135
+ * stop the transfer. With that we also need to ignore
2136
+ * all TRBs used by the request, however TRBs can only
2137
+ * be modified after completion of END_TRANSFER
2138
+ * command. So what we do here is that we wait for
2139
+ * END_TRANSFER completion and only after that, we jump
2140
+ * over TRBs by clearing HWO and incrementing dequeue
2141
+ * pointer.
2142
+ */
2143
+ for (i = 0; i < req->num_trbs; i++) {
2144
+ struct dwc3_trb *trb;
2145
+
2146
+ trb = &dep->trb_pool[dep->trb_dequeue];
2147
+ trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2148
+ dwc3_ep_inc_deq(dep);
2149
+ }
2150
+
2151
+ req->num_trbs = 0;
2152
+}
2153
+
18412154 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
18422155 {
18432156 struct dwc3_request *req;
1844
- struct dwc3_request *tmp;
2157
+ struct dwc3 *dwc = dep->dwc;
18452158
1846
- list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
2159
+ while (!list_empty(&dep->cancelled_list)) {
2160
+ req = next_request(&dep->cancelled_list);
18472161 dwc3_gadget_ep_skip_trbs(dep, req);
1848
- dwc3_gadget_giveback(dep, req, -ECONNRESET);
2162
+ switch (req->status) {
2163
+ case DWC3_REQUEST_STATUS_DISCONNECTED:
2164
+ dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
2165
+ break;
2166
+ case DWC3_REQUEST_STATUS_DEQUEUED:
2167
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2168
+ break;
2169
+ case DWC3_REQUEST_STATUS_STALLED:
2170
+ dwc3_gadget_giveback(dep, req, -EPIPE);
2171
+ break;
2172
+ default:
2173
+ dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
2174
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2175
+ break;
2176
+ }
2177
+ /*
2178
+ * The endpoint is disabled, let the dwc3_remove_requests()
2179
+ * handle the cleanup.
2180
+ */
2181
+ if (!dep->endpoint.desc)
2182
+ break;
18492183 }
18502184 }
18512185
....@@ -1865,40 +2199,45 @@
18652199
18662200 spin_lock_irqsave(&dwc->lock, flags);
18672201
1868
- list_for_each_entry(r, &dep->pending_list, list) {
2202
+ list_for_each_entry(r, &dep->cancelled_list, list) {
18692203 if (r == req)
1870
- break;
2204
+ goto out;
18712205 }
18722206
1873
- if (r != req) {
1874
- list_for_each_entry(r, &dep->started_list, list) {
1875
- if (r == req)
1876
- break;
2207
+ list_for_each_entry(r, &dep->pending_list, list) {
2208
+ if (r == req) {
2209
+ dwc3_gadget_ep_skip_trbs(dep, req);
2210
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2211
+ goto out;
18772212 }
2213
+ }
2214
+
2215
+ list_for_each_entry(r, &dep->started_list, list) {
18782216 if (r == req) {
18792217 /* wait until it is processed */
18802218 dwc3_stop_active_transfer(dep, true, true);
18812219
1882
- if (!r->trb)
1883
- goto out0;
2220
+ /*
2221
+ * Remove any started request if the transfer is
2222
+ * cancelled.
2223
+ */
2224
+ dwc3_gadget_move_cancelled_request(r, DWC3_REQUEST_STATUS_DEQUEUED);
18842225
1885
- dwc3_gadget_move_cancelled_request(req);
1886
- if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1887
- goto out0;
1888
- else
1889
- goto out1;
2226
+ dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
2227
+
2228
+ if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2229
+ dwc3_gadget_ep_skip_trbs(dep, req);
2230
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2231
+ }
2232
+
2233
+ goto out;
18902234 }
1891
- dev_err(dwc->dev, "request %pK was not queued to %s\n",
1892
- request, ep->name);
1893
- ret = -EINVAL;
1894
- goto out0;
18952235 }
18962236
1897
-out1:
1898
- dwc3_gadget_ep_skip_trbs(dep, req);
1899
- dwc3_gadget_giveback(dep, req, -ECONNRESET);
1900
-
1901
-out0:
2237
+ dev_err(dwc->dev, "request %pK was not queued to %s\n",
2238
+ request, ep->name);
2239
+ ret = -EINVAL;
2240
+out:
19022241 spin_unlock_irqrestore(&dwc->lock, flags);
19032242
19042243 return ret;
....@@ -1909,6 +2248,7 @@
19092248 struct dwc3_gadget_ep_cmd_params params;
19102249 struct dwc3 *dwc = dep->dwc;
19112250 int ret;
2251
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
19122252
19132253 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
19142254 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
....@@ -1920,8 +2260,8 @@
19202260 if (value) {
19212261 struct dwc3_trb *trb;
19222262
1923
- unsigned transfer_in_flight;
1924
- unsigned started;
2263
+ unsigned int transfer_in_flight;
2264
+ unsigned int started;
19252265
19262266 if (dep->number > 1)
19272267 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
....@@ -1944,13 +2284,44 @@
19442284 else
19452285 dep->flags |= DWC3_EP_STALL;
19462286 } else {
2287
+ /*
2288
+ * Don't issue CLEAR_STALL command to control endpoints. The
2289
+ * controller automatically clears the STALL when it receives
2290
+ * the SETUP token.
2291
+ */
2292
+ if (dep->number <= 1) {
2293
+ dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2294
+ return 0;
2295
+ }
2296
+
2297
+ dwc3_stop_active_transfer(dep, true, true);
2298
+
2299
+ if (!list_empty(&dep->started_list))
2300
+ dep->flags |= DWC3_EP_DELAY_START;
2301
+
2302
+ if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
2303
+ (dep->flags & DWC3_EP_DELAY_STOP)) {
2304
+ dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
2305
+ if (protocol)
2306
+ vdwc->clear_stall_protocol = dep->number;
2307
+
2308
+ return 0;
2309
+ }
19472310
19482311 ret = dwc3_send_clear_stall_ep_cmd(dep);
1949
- if (ret)
2312
+ if (ret) {
19502313 dev_err(dwc->dev, "failed to clear STALL on %s\n",
19512314 dep->name);
1952
- else
1953
- dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2315
+ return ret;
2316
+ }
2317
+
2318
+ dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2319
+
2320
+ if ((dep->flags & DWC3_EP_DELAY_START) &&
2321
+ !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2322
+ __dwc3_gadget_kick_transfer(dep);
2323
+
2324
+ dep->flags &= ~DWC3_EP_DELAY_START;
19542325 }
19552326
19562327 return ret;
....@@ -2050,7 +2421,6 @@
20502421 link_state = DWC3_DSTS_USBLNKST(reg);
20512422
20522423 switch (link_state) {
2053
- case DWC3_LINK_STATE_U0:
20542424 case DWC3_LINK_STATE_RESET:
20552425 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
20562426 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
....@@ -2062,15 +2432,6 @@
20622432 return -EINVAL;
20632433 }
20642434
2065
- /*
2066
- * dwc3 gadget wakeup from host resume signal
2067
- * when the whole system enter suspend.
2068
- */
2069
- if (link_state == DWC3_LINK_STATE_U0) {
2070
- dwc->link_state = link_state;
2071
- return 0;
2072
- }
2073
-
20742435 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
20752436 if (ret < 0) {
20762437 dev_err(dwc->dev, "failed to put link in Recovery\n");
....@@ -2078,7 +2439,7 @@
20782439 }
20792440
20802441 /* Recent versions do this automatically */
2081
- if (dwc->revision < DWC3_REVISION_194A) {
2442
+ if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
20822443 /* write zeroes to Link Change Request */
20832444 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
20842445 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
....@@ -2130,28 +2491,140 @@
21302491 return 0;
21312492 }
21322493
2494
+static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2495
+{
2496
+ u32 epnum;
2497
+
2498
+ for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2499
+ struct dwc3_ep *dep;
2500
+
2501
+ dep = dwc->eps[epnum];
2502
+ if (!dep)
2503
+ continue;
2504
+
2505
+ dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
2506
+ }
2507
+}
2508
+
2509
+static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2510
+{
2511
+ enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2512
+ u32 reg;
2513
+
2514
+ if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2515
+ ssp_rate = dwc->max_ssp_rate;
2516
+
2517
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2518
+ reg &= ~DWC3_DCFG_SPEED_MASK;
2519
+ reg &= ~DWC3_DCFG_NUMLANES(~0);
2520
+
2521
+ if (ssp_rate == USB_SSP_GEN_1x2)
2522
+ reg |= DWC3_DCFG_SUPERSPEED;
2523
+ else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2524
+ reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2525
+
2526
+ if (ssp_rate != USB_SSP_GEN_2x1 &&
2527
+ dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2528
+ reg |= DWC3_DCFG_NUMLANES(1);
2529
+
2530
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2531
+}
2532
+
2533
+static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2534
+{
2535
+ enum usb_device_speed speed;
2536
+ u32 reg;
2537
+
2538
+ speed = dwc->gadget_max_speed;
2539
+ if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
2540
+ speed = dwc->maximum_speed;
2541
+
2542
+ if (speed == USB_SPEED_SUPER_PLUS &&
2543
+ DWC3_IP_IS(DWC32)) {
2544
+ __dwc3_gadget_set_ssp_rate(dwc);
2545
+ return;
2546
+ }
2547
+
2548
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2549
+ reg &= ~(DWC3_DCFG_SPEED_MASK);
2550
+
2551
+ /*
2552
+ * WORKAROUND: DWC3 revision < 2.20a have an issue
2553
+ * which would cause metastability state on Run/Stop
2554
+ * bit if we try to force the IP to USB2-only mode.
2555
+ *
2556
+ * Because of that, we cannot configure the IP to any
2557
+ * speed other than the SuperSpeed
2558
+ *
2559
+ * Refers to:
2560
+ *
2561
+ * STAR#9000525659: Clock Domain Crossing on DCTL in
2562
+ * USB 2.0 Mode
2563
+ */
2564
+ if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2565
+ !dwc->dis_metastability_quirk) {
2566
+ reg |= DWC3_DCFG_SUPERSPEED;
2567
+ } else {
2568
+ switch (speed) {
2569
+ case USB_SPEED_LOW:
2570
+ reg |= DWC3_DCFG_LOWSPEED;
2571
+ break;
2572
+ case USB_SPEED_FULL:
2573
+ reg |= DWC3_DCFG_FULLSPEED;
2574
+ break;
2575
+ case USB_SPEED_HIGH:
2576
+ reg |= DWC3_DCFG_HIGHSPEED;
2577
+ break;
2578
+ case USB_SPEED_SUPER:
2579
+ reg |= DWC3_DCFG_SUPERSPEED;
2580
+ break;
2581
+ case USB_SPEED_SUPER_PLUS:
2582
+ if (DWC3_IP_IS(DWC3))
2583
+ reg |= DWC3_DCFG_SUPERSPEED;
2584
+ else
2585
+ reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2586
+ break;
2587
+ default:
2588
+ dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2589
+
2590
+ if (DWC3_IP_IS(DWC3))
2591
+ reg |= DWC3_DCFG_SUPERSPEED;
2592
+ else
2593
+ reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2594
+ }
2595
+ }
2596
+
2597
+ if (DWC3_IP_IS(DWC32) &&
2598
+ speed > USB_SPEED_UNKNOWN &&
2599
+ speed < USB_SPEED_SUPER_PLUS)
2600
+ reg &= ~DWC3_DCFG_NUMLANES(~0);
2601
+
2602
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2603
+}
2604
+
21332605 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
21342606 {
21352607 u32 reg;
2136
- u32 timeout = 500;
2608
+ u32 timeout = 2000;
21372609
21382610 if (pm_runtime_suspended(dwc->dev))
21392611 return 0;
21402612
21412613 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
21422614 if (is_on) {
2143
- if (dwc->revision <= DWC3_REVISION_187A) {
2615
+ if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
21442616 reg &= ~DWC3_DCTL_TRGTULST_MASK;
21452617 reg |= DWC3_DCTL_TRGTULST_RX_DET;
21462618 }
21472619
2148
- if (dwc->revision >= DWC3_REVISION_194A)
2620
+ if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
21492621 reg &= ~DWC3_DCTL_KEEP_CONNECT;
21502622 reg |= DWC3_DCTL_RUN_STOP;
21512623
21522624 if (dwc->has_hibernation)
21532625 reg |= DWC3_DCTL_KEEP_CONNECT;
21542626
2627
+ __dwc3_gadget_set_speed(dwc);
21552628 dwc->pullups_connected = true;
21562629 } else {
21572630 reg &= ~DWC3_DCTL_RUN_STOP;
....@@ -2162,9 +2635,10 @@
21622635 dwc->pullups_connected = false;
21632636 }
21642637
2165
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2638
+ dwc3_gadget_dctl_write_safe(dwc, reg);
21662639
21672640 do {
2641
+ usleep_range(1000, 2000);
21682642 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
21692643 reg &= DWC3_DSTS_DEVCTRLHLT;
21702644 } while (--timeout && !(!is_on ^ !reg));
....@@ -2175,42 +2649,125 @@
21752649 return 0;
21762650 }
21772651
2178
-static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2179
-{
2180
- struct dwc3 *dwc = gadget_to_dwc(g);
2181
- unsigned long flags;
2182
- int ret;
2652
+static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2653
+static void __dwc3_gadget_stop(struct dwc3 *dwc);
2654
+static int __dwc3_gadget_start(struct dwc3 *dwc);
21832655
2184
- is_on = !!is_on;
2656
+static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
2657
+{
2658
+ unsigned long flags;
2659
+
2660
+ spin_lock_irqsave(&dwc->lock, flags);
2661
+ dwc->connected = false;
21852662
21862663 /*
21872664 * Per databook, when we want to stop the gadget, if a control transfer
21882665 * is still in process, complete it and get the core into setup phase.
21892666 */
2190
- if (!is_on && dwc->ep0state != EP0_SETUP_PHASE &&
2667
+ if (dwc->ep0state != EP0_SETUP_PHASE &&
21912668 dwc->ep0state != EP0_UNCONNECTED) {
2669
+ int ret;
2670
+
2671
+ if (dwc->delayed_status)
2672
+ dwc3_ep0_send_delayed_status(dwc);
2673
+
21922674 reinit_completion(&dwc->ep0_in_setup);
21932675
2676
+ spin_unlock_irqrestore(&dwc->lock, flags);
21942677 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
21952678 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2679
+ spin_lock_irqsave(&dwc->lock, flags);
21962680 if (ret == 0)
21972681 dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
21982682 }
21992683
2200
- spin_lock_irqsave(&dwc->lock, flags);
2201
- ret = dwc3_gadget_run_stop(dwc, is_on, false);
2684
+ /*
2685
+ * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
2686
+ * Section 4.1.8 Table 4-7, it states that for a device-initiated
2687
+ * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2688
+ * command for any active transfers" before clearing the RunStop
2689
+ * bit.
2690
+ */
2691
+ dwc3_stop_active_transfers(dwc);
2692
+ __dwc3_gadget_stop(dwc);
22022693 spin_unlock_irqrestore(&dwc->lock, flags);
2694
+
2695
+ /*
2696
+ * Note: if the GEVNTCOUNT indicates events in the event buffer, the
2697
+ * driver needs to acknowledge them before the controller can halt.
2698
+ * Simply let the interrupt handler acknowledges and handle the
2699
+ * remaining event generated by the controller while polling for
2700
+ * DSTS.DEVCTLHLT.
2701
+ */
2702
+ return dwc3_gadget_run_stop(dwc, false, false);
2703
+}
2704
+
2705
+static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2706
+{
2707
+ struct dwc3 *dwc = gadget_to_dwc(g);
2708
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
2709
+ int ret;
2710
+
2711
+ is_on = !!is_on;
2712
+
2713
+ vdwc->softconnect = is_on;
2714
+
2715
+ /*
2716
+ * Avoid issuing a runtime resume if the device is already in the
2717
+ * suspended state during gadget disconnect. DWC3 gadget was already
2718
+ * halted/stopped during runtime suspend.
2719
+ */
2720
+ if (!is_on) {
2721
+ pm_runtime_barrier(dwc->dev);
2722
+ if (pm_runtime_suspended(dwc->dev))
2723
+ return 0;
2724
+ }
2725
+
2726
+ /*
2727
+ * Check the return value for successful resume, or error. For a
2728
+ * successful resume, the DWC3 runtime PM resume routine will handle
2729
+ * the run stop sequence, so avoid duplicate operations here.
2730
+ */
2731
+ ret = pm_runtime_get_sync(dwc->dev);
2732
+ if (!ret || ret < 0) {
2733
+ pm_runtime_put(dwc->dev);
2734
+ return 0;
2735
+ }
2736
+
2737
+ if (dwc->pullups_connected == is_on) {
2738
+ pm_runtime_put(dwc->dev);
2739
+ return 0;
2740
+ }
2741
+
2742
+ synchronize_irq(dwc->irq_gadget);
2743
+
2744
+ if (!is_on) {
2745
+ ret = dwc3_gadget_soft_disconnect(dwc);
2746
+ } else {
2747
+ /*
2748
+ * In the Synopsys DWC_usb31 1.90a programming guide section
2749
+ * 4.1.9, it specifies that for a reconnect after a
2750
+ * device-initiated disconnect requires a core soft reset
2751
+ * (DCTL.CSftRst) before enabling the run/stop bit.
2752
+ */
2753
+ dwc3_core_soft_reset(dwc);
2754
+
2755
+ dwc3_event_buffers_setup(dwc);
2756
+ __dwc3_gadget_start(dwc);
2757
+ ret = dwc3_gadget_run_stop(dwc, true, false);
2758
+ }
2759
+
2760
+ pm_runtime_put(dwc->dev);
22032761
22042762 return ret;
22052763 }
22062764
2207
-void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2765
+static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
22082766 {
22092767 u32 reg;
22102768
22112769 /* Enable all but Start and End of Frame IRQs */
2212
- reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2213
- DWC3_DEVTEN_EVNTOVERFLOWEN |
2770
+ reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
22142771 DWC3_DEVTEN_CMDCMPLTEN |
22152772 DWC3_DEVTEN_ERRTICERREN |
22162773 DWC3_DEVTEN_WKUPEVTEN |
....@@ -2218,17 +2775,17 @@
22182775 DWC3_DEVTEN_USBRSTEN |
22192776 DWC3_DEVTEN_DISCONNEVTEN);
22202777
2221
- if (dwc->revision < DWC3_REVISION_250A)
2778
+ if (DWC3_VER_IS_PRIOR(DWC3, 250A))
22222779 reg |= DWC3_DEVTEN_ULSTCNGEN;
22232780
22242781 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2225
- if (dwc->revision >= DWC3_REVISION_230A)
2226
- reg |= DWC3_DEVTEN_EOPFEN;
2782
+ if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2783
+ reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
22272784
22282785 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
22292786 }
22302787
2231
-void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2788
+static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
22322789 {
22332790 /* mask all interrupts */
22342791 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
....@@ -2266,7 +2823,7 @@
22662823 u32 reg;
22672824
22682825 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2269
- mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2826
+ mdwidth = dwc3_mdwidth(dwc);
22702827
22712828 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
22722829 nump = min_t(u32, nump, 16);
....@@ -2283,6 +2840,15 @@
22832840 struct dwc3_ep *dep;
22842841 int ret = 0;
22852842 u32 reg;
2843
+
2844
+ /*
2845
+ * If the DWC3 is in runtime suspend, the clocks maybe
2846
+ * disabled, so avoid enable the DWC3 endpoints here.
2847
+ * The DWC3 runtime PM resume routine will handle the
2848
+ * gadget start sequence.
2849
+ */
2850
+ if (pm_runtime_suspended(dwc->dev))
2851
+ return ret;
22862852
22872853 /*
22882854 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
....@@ -2303,19 +2869,31 @@
23032869 * bursts of data without going through any sort of endpoint throttling.
23042870 */
23052871 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2306
- if (dwc3_is_usb31(dwc))
2307
- reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2308
- else
2872
+ if (DWC3_IP_IS(DWC3))
23092873 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2874
+ else
2875
+ reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
23102876
23112877 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
23122878
23132879 dwc3_gadget_setup_nump(dwc);
23142880
2881
+ /*
2882
+ * Currently the controller handles single stream only. So, Ignore
2883
+ * Packet Pending bit for stream selection and don't search for another
2884
+ * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2885
+ * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2886
+ * the stream performance.
2887
+ */
2888
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2889
+ reg |= DWC3_DCFG_IGNSTRMPP;
2890
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2891
+
23152892 /* Start with SuperSpeed Default */
23162893 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
23172894
23182895 dep = dwc->eps[0];
2896
+ dep->flags = 0;
23192897 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
23202898 if (ret) {
23212899 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
....@@ -2323,6 +2901,7 @@
23232901 }
23242902
23252903 dep = dwc->eps[1];
2904
+ dep->flags = 0;
23262905 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
23272906 if (ret) {
23282907 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
....@@ -2331,6 +2910,7 @@
23312910
23322911 /* begin to receive SETUP packets */
23332912 dwc->ep0state = EP0_SETUP_PHASE;
2913
+ dwc->ep0_bounced = false;
23342914 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
23352915 dwc->delayed_status = false;
23362916 dwc3_ep0_out_start(dwc);
....@@ -2366,17 +2946,13 @@
23662946 spin_lock_irqsave(&dwc->lock, flags);
23672947 if (dwc->gadget_driver) {
23682948 dev_err(dwc->dev, "%s is already bound to %s\n",
2369
- dwc->gadget.name,
2949
+ dwc->gadget->name,
23702950 dwc->gadget_driver->driver.name);
23712951 ret = -EBUSY;
23722952 goto err1;
23732953 }
23742954
23752955 dwc->gadget_driver = driver;
2376
-
2377
- if (pm_runtime_active(dwc->dev))
2378
- __dwc3_gadget_start(dwc);
2379
-
23802956 spin_unlock_irqrestore(&dwc->lock, flags);
23812957
23822958 return 0;
....@@ -2402,28 +2978,59 @@
24022978 unsigned long flags;
24032979
24042980 spin_lock_irqsave(&dwc->lock, flags);
2405
-
24062981 if (!dwc->gadget_driver) {
24072982 spin_unlock_irqrestore(&dwc->lock, flags);
24082983 dev_warn(dwc->dev, "%s is already stopped\n",
2409
- dwc->gadget.name);
2410
- goto out0;
2984
+ dwc->gadget->name);
2985
+ goto out;
24112986 }
2412
-
2413
- if (pm_runtime_suspended(dwc->dev))
2414
- goto out1;
2415
-
2416
- __dwc3_gadget_stop(dwc);
2417
-
2418
-out1:
2419
- dwc->fifo_resize_status = false;
24202987 dwc->gadget_driver = NULL;
2988
+ dwc->max_cfg_eps = 0;
24212989 spin_unlock_irqrestore(&dwc->lock, flags);
24222990
24232991 free_irq(dwc->irq_gadget, dwc->ev_buf);
24242992
2425
-out0:
2993
+out:
24262994 return 0;
2995
+}
2996
+
2997
+static void dwc3_gadget_config_params(struct usb_gadget *g,
2998
+ struct usb_dcd_config_params *params)
2999
+{
3000
+ struct dwc3 *dwc = gadget_to_dwc(g);
3001
+
3002
+ params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
3003
+ params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
3004
+
3005
+ /* Recommended BESL */
3006
+ if (!dwc->dis_enblslpm_quirk) {
3007
+ /*
3008
+ * If the recommended BESL baseline is 0 or if the BESL deep is
3009
+ * less than 2, Microsoft's Windows 10 host usb stack will issue
3010
+ * a usb reset immediately after it receives the extended BOS
3011
+ * descriptor and the enumeration will fail. To maintain
3012
+ * compatibility with the Windows' usb stack, let's set the
3013
+ * recommended BESL baseline to 1 and clamp the BESL deep to be
3014
+ * within 2 to 15.
3015
+ */
3016
+ params->besl_baseline = 1;
3017
+ if (dwc->is_utmi_l1_suspend)
3018
+ params->besl_deep =
3019
+ clamp_t(u8, dwc->hird_threshold, 2, 15);
3020
+ }
3021
+
3022
+ /* U1 Device exit Latency */
3023
+ if (dwc->dis_u1_entry_quirk)
3024
+ params->bU1devExitLat = 0;
3025
+ else
3026
+ params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
3027
+
3028
+ /* U2 Device exit Latency */
3029
+ if (dwc->dis_u2_entry_quirk)
3030
+ params->bU2DevExitLat = 0;
3031
+ else
3032
+ params->bU2DevExitLat =
3033
+ cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
24273034 }
24283035
24293036 static void dwc3_gadget_set_speed(struct usb_gadget *g,
....@@ -2431,67 +3038,94 @@
24313038 {
24323039 struct dwc3 *dwc = gadget_to_dwc(g);
24333040 unsigned long flags;
2434
- u32 reg;
2435
-
2436
- /*
2437
- * To prevent Android 10 from trying to call UDC and failed constantly
2438
- * while dwc3 is suspended, we let the UDC node always exist.
2439
- * If not return here, it may cause crashes.
2440
- */
2441
- if (pm_runtime_suspended(dwc->dev))
2442
- return;
24433041
24443042 spin_lock_irqsave(&dwc->lock, flags);
2445
- reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2446
- reg &= ~(DWC3_DCFG_SPEED_MASK);
3043
+ dwc->gadget_max_speed = speed;
3044
+ spin_unlock_irqrestore(&dwc->lock, flags);
3045
+}
24473046
2448
- /*
2449
- * WORKAROUND: DWC3 revision < 2.20a have an issue
2450
- * which would cause metastability state on Run/Stop
2451
- * bit if we try to force the IP to USB2-only mode.
2452
- *
2453
- * Because of that, we cannot configure the IP to any
2454
- * speed other than the SuperSpeed
2455
- *
2456
- * Refers to:
2457
- *
2458
- * STAR#9000525659: Clock Domain Crossing on DCTL in
2459
- * USB 2.0 Mode
2460
- */
2461
- if (dwc->revision < DWC3_REVISION_220A &&
2462
- !dwc->dis_metastability_quirk) {
2463
- reg |= DWC3_DCFG_SUPERSPEED;
2464
- } else {
2465
- switch (speed) {
2466
- case USB_SPEED_LOW:
2467
- reg |= DWC3_DCFG_LOWSPEED;
2468
- break;
2469
- case USB_SPEED_FULL:
2470
- reg |= DWC3_DCFG_FULLSPEED;
2471
- break;
2472
- case USB_SPEED_HIGH:
2473
- reg |= DWC3_DCFG_HIGHSPEED;
2474
- break;
2475
- case USB_SPEED_SUPER:
2476
- reg |= DWC3_DCFG_SUPERSPEED;
2477
- break;
2478
- case USB_SPEED_SUPER_PLUS:
2479
- if (dwc3_is_usb31(dwc))
2480
- reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2481
- else
2482
- reg |= DWC3_DCFG_SUPERSPEED;
2483
- break;
2484
- default:
2485
- dev_err(dwc->dev, "invalid speed (%d)\n", speed);
3047
+static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
3048
+ enum usb_ssp_rate rate)
3049
+{
3050
+ struct dwc3 *dwc = gadget_to_dwc(g);
3051
+ unsigned long flags;
24863052
2487
- if (dwc->revision & DWC3_REVISION_IS_DWC31)
2488
- reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2489
- else
2490
- reg |= DWC3_DCFG_SUPERSPEED;
2491
- }
3053
+ spin_lock_irqsave(&dwc->lock, flags);
3054
+ dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
3055
+ dwc->gadget_ssp_rate = rate;
3056
+ spin_unlock_irqrestore(&dwc->lock, flags);
3057
+}
3058
+
3059
+static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
3060
+{
3061
+ struct dwc3 *dwc = gadget_to_dwc(g);
3062
+ union power_supply_propval val = {0};
3063
+ int ret;
3064
+
3065
+ if (dwc->usb2_phy)
3066
+ return usb_phy_set_power(dwc->usb2_phy, mA);
3067
+
3068
+ if (!dwc->usb_psy)
3069
+ return -EOPNOTSUPP;
3070
+
3071
+ val.intval = 1000 * mA;
3072
+ ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
3073
+
3074
+ return ret;
3075
+}
3076
+
3077
+/**
3078
+ * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
3079
+ * @g: pointer to the USB gadget
3080
+ *
3081
+ * Used to record the maximum number of endpoints being used in a USB composite
3082
+ * device. (across all configurations) This is to be used in the calculation
3083
+ * of the TXFIFO sizes when resizing internal memory for individual endpoints.
3084
+ * It will help ensured that the resizing logic reserves enough space for at
3085
+ * least one max packet.
3086
+ */
3087
+static int dwc3_gadget_check_config(struct usb_gadget *g)
3088
+{
3089
+ struct dwc3 *dwc = gadget_to_dwc(g);
3090
+ struct usb_ep *ep;
3091
+ int fifo_size = 0;
3092
+ int ram1_depth;
3093
+ int ep_num = 0;
3094
+
3095
+ if (!dwc->do_fifo_resize)
3096
+ return 0;
3097
+
3098
+ list_for_each_entry(ep, &g->ep_list, ep_list) {
3099
+ /* Only interested in the IN endpoints */
3100
+ if (ep->claimed && (ep->address & USB_DIR_IN))
3101
+ ep_num++;
24923102 }
2493
- dwc3_writel(dwc->regs, DWC3_DCFG, reg);
24943103
3104
+ if (ep_num <= dwc->max_cfg_eps)
3105
+ return 0;
3106
+
3107
+ /* Update the max number of eps in the composition */
3108
+ dwc->max_cfg_eps = ep_num;
3109
+
3110
+ fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
3111
+ /* Based on the equation, increment by one for every ep */
3112
+ fifo_size += dwc->max_cfg_eps;
3113
+
3114
+ /* Check if we can fit a single fifo per endpoint */
3115
+ ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
3116
+ if (fifo_size > ram1_depth)
3117
+ return -ENOMEM;
3118
+
3119
+ return 0;
3120
+}
3121
+
3122
+static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
3123
+{
3124
+ struct dwc3 *dwc = gadget_to_dwc(g);
3125
+ unsigned long flags;
3126
+
3127
+ spin_lock_irqsave(&dwc->lock, flags);
3128
+ dwc->async_callbacks = enable;
24953129 spin_unlock_irqrestore(&dwc->lock, flags);
24963130 }
24973131
....@@ -2503,6 +3137,11 @@
25033137 .udc_start = dwc3_gadget_start,
25043138 .udc_stop = dwc3_gadget_stop,
25053139 .udc_set_speed = dwc3_gadget_set_speed,
3140
+ .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
3141
+ .get_config_params = dwc3_gadget_config_params,
3142
+ .vbus_draw = dwc3_gadget_vbus_draw,
3143
+ .check_config = dwc3_gadget_check_config,
3144
+ .udc_async_callbacks = dwc3_gadget_async_callbacks,
25063145 };
25073146
25083147 /* -------------------------------------------------------------------------- */
....@@ -2515,7 +3154,7 @@
25153154 dep->endpoint.maxburst = 1;
25163155 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
25173156 if (!dep->direction)
2518
- dwc->gadget.ep0 = &dep->endpoint;
3157
+ dwc->gadget->ep0 = &dep->endpoint;
25193158
25203159 dep->endpoint.caps.type_control = true;
25213160
....@@ -2525,48 +3164,62 @@
25253164 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
25263165 {
25273166 struct dwc3 *dwc = dep->dwc;
2528
- int mdwidth;
3167
+ u32 mdwidth;
25293168 int size;
3169
+ int maxpacket;
25303170
2531
- mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
3171
+ mdwidth = dwc3_mdwidth(dwc);
3172
+
25323173 /* MDWIDTH is represented in bits, we need it in bytes */
25333174 mdwidth /= 8;
25343175
25353176 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2536
- if (dwc3_is_usb31(dwc))
2537
- size = DWC31_GTXFIFOSIZ_TXFDEF(size);
3177
+ if (DWC3_IP_IS(DWC3))
3178
+ size = DWC3_GTXFIFOSIZ_TXFDEP(size);
25383179 else
2539
- size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2540
-
2541
- /* FIFO Depth is in MDWDITH bytes. Multiply */
2542
- size *= mdwidth;
3180
+ size = DWC31_GTXFIFOSIZ_TXFDEP(size);
25433181
25443182 /*
2545
- * To meet performance requirement, a minimum TxFIFO size of 3x
2546
- * MaxPacketSize is recommended for endpoints that support burst and a
2547
- * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2548
- * support burst. Use those numbers and we can calculate the max packet
2549
- * limit as below.
3183
+ * maxpacket size is determined as part of the following, after assuming
3184
+ * a mult value of one maxpacket:
3185
+ * DWC3 revision 280A and prior:
3186
+ * fifo_size = mult * (max_packet / mdwidth) + 1;
3187
+ * maxpacket = mdwidth * (fifo_size - 1);
3188
+ *
3189
+ * DWC3 revision 290A and onwards:
3190
+ * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
3191
+ * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth;
25503192 */
2551
- if (dwc->maximum_speed >= USB_SPEED_SUPER)
2552
- size /= 3;
3193
+ if (DWC3_VER_IS_PRIOR(DWC3, 290A))
3194
+ maxpacket = mdwidth * (size - 1);
25533195 else
2554
- size /= 2;
3196
+ maxpacket = mdwidth * ((size - 1) - 1) - mdwidth;
25553197
3198
+
3199
+ /*
3200
+ * To meet performance requirement, a minimum TxFIFO size of 2x
3201
+ * MaxPacketSize is recommended for endpoints that support for
3202
+ * Rockchip platform with UVC function.
3203
+ */
3204
+ if (IS_REACHABLE(CONFIG_ARCH_ROCKCHIP) &&
3205
+ (dwc->maximum_speed >= USB_SPEED_HIGH))
3206
+ maxpacket /= 2;
3207
+
3208
+ /* Functionally, space for one max packet is sufficient */
3209
+ size = min_t(int, maxpacket, 1024);
25563210 /*
25573211 * If enable tx fifos resize, set each in ep maxpacket
25583212 * to 1024, it can avoid being dependent on the default
25593213 * fifo size, and more flexible use of endpoints.
25603214 */
2561
- if (dwc->needs_fifo_resize)
3215
+ if (dwc->do_fifo_resize)
25623216 size = 1024;
2563
-
25643217 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
25653218
2566
- dep->endpoint.max_streams = 15;
3219
+ dep->endpoint.max_streams = 16;
25673220 dep->endpoint.ops = &dwc3_gadget_ep_ops;
25683221 list_add_tail(&dep->endpoint.ep_list,
2569
- &dwc->gadget.ep_list);
3222
+ &dwc->gadget->ep_list);
25703223 dep->endpoint.caps.type_iso = true;
25713224 dep->endpoint.caps.type_bulk = true;
25723225 dep->endpoint.caps.type_int = true;
....@@ -2577,20 +3230,20 @@
25773230 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
25783231 {
25793232 struct dwc3 *dwc = dep->dwc;
2580
- int mdwidth;
3233
+ u32 mdwidth;
25813234 int size;
25823235
2583
- mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
3236
+ mdwidth = dwc3_mdwidth(dwc);
25843237
25853238 /* MDWIDTH is represented in bits, convert to bytes */
25863239 mdwidth /= 8;
25873240
25883241 /* All OUT endpoints share a single RxFIFO space */
25893242 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2590
- if (dwc3_is_usb31(dwc))
2591
- size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2592
- else
3243
+ if (DWC3_IP_IS(DWC3))
25933244 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
3245
+ else
3246
+ size = DWC31_GRXFIFOSIZ_RXFDEP(size);
25943247
25953248 /* FIFO depth is in MDWDITH bytes */
25963249 size *= mdwidth;
....@@ -2610,10 +3263,10 @@
26103263 size /= 3;
26113264
26123265 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2613
- dep->endpoint.max_streams = 15;
3266
+ dep->endpoint.max_streams = 16;
26143267 dep->endpoint.ops = &dwc3_gadget_ep_ops;
26153268 list_add_tail(&dep->endpoint.ep_list,
2616
- &dwc->gadget.ep_list);
3269
+ &dwc->gadget->ep_list);
26173270 dep->endpoint.caps.type_iso = true;
26183271 dep->endpoint.caps.type_bulk = true;
26193272 dep->endpoint.caps.type_int = true;
....@@ -2627,20 +3280,20 @@
26273280 bool direction = epnum & 1;
26283281 int ret;
26293282 u8 num = epnum >> 1;
2630
- u8 num_in_eps, num_out_eps;
2631
-
2632
- num_in_eps = DWC3_NUM_IN_EPS(&dwc->hwparams);
2633
- num_out_eps = dwc->num_eps - num_in_eps;
3283
+ u8 num_in_eps, num_out_eps, min_eps;
26343284
26353285 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
26363286 if (!dep)
26373287 return -ENOMEM;
26383288
3289
+ num_in_eps = DWC3_NUM_IN_EPS(&dwc->hwparams);
3290
+ num_out_eps = dwc->num_eps - num_in_eps;
3291
+ min_eps = min_t(u8, num_in_eps, num_out_eps);
3292
+
26393293 /* reconfig direction and num if num_out_eps != num_in_eps */
2640
- if ((!direction && ((epnum >> 1) + 1) > num_out_eps) ||
2641
- (direction && ((epnum >> 1) + 1) > num_in_eps)) {
2642
- direction = !direction;
2643
- num = num + (epnum & 1);
3294
+ if (num + 1 > min_eps && num_in_eps != num_out_eps) {
3295
+ num = epnum - min_eps;
3296
+ direction = num + 1 > num_out_eps ? 1 : 0;
26443297 }
26453298
26463299 dep->dwc = dwc;
....@@ -2659,12 +3312,7 @@
26593312 if (!(dep->number > 1)) {
26603313 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
26613314 dep->endpoint.comp_desc = NULL;
2662
-#ifdef CONFIG_ARCH_ROCKCHIP
2663
- dep->endpoint.transfer_type = USB_ENDPOINT_XFER_CONTROL;
2664
-#endif
26653315 }
2666
-
2667
- spin_lock_init(&dep->lock);
26683316
26693317 if (num == 0)
26703318 ret = dwc3_gadget_init_control_endpoint(dep);
....@@ -2692,7 +3340,7 @@
26923340 {
26933341 u8 epnum;
26943342
2695
- INIT_LIST_HEAD(&dwc->gadget.ep_list);
3343
+ INIT_LIST_HEAD(&dwc->gadget->ep_list);
26963344
26973345 for (epnum = 0; epnum < total; epnum++) {
26983346 int ret;
....@@ -2773,12 +3421,12 @@
27733421 }
27743422
27753423 /*
2776
- * If we're dealing with unaligned size OUT transfer, we will be left
2777
- * with one TRB pending in the ring. We need to manually clear HWO bit
2778
- * from that TRB.
3424
+ * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
3425
+ * this TRB points to the bounce buffer address, it's a MPS alignment
3426
+ * TRB. Don't add it to req->remaining calculation.
27793427 */
2780
-
2781
- if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
3428
+ if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
3429
+ trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
27823430 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
27833431 return 1;
27843432 }
....@@ -2790,6 +3438,10 @@
27903438 return 1;
27913439
27923440 if (event->status & DEPEVT_STATUS_SHORT && !chain)
3441
+ return 1;
3442
+
3443
+ if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
3444
+ DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
27933445 return 1;
27943446
27953447 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
....@@ -2845,6 +3497,7 @@
28453497 struct dwc3_request *req, int status)
28463498 {
28473499 struct dwc3 *dwc = dep->dwc;
3500
+ int request_status;
28483501 int ret;
28493502
28503503 if (req->request.num_mapped_sgs)
....@@ -2856,35 +3509,26 @@
28563509
28573510 req->request.actual = req->request.length - req->remaining;
28583511
2859
- if (!dwc3_gadget_ep_request_completed(req) &&
2860
- !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3512
+ if (!dwc3_gadget_ep_request_completed(req))
28613513 goto out;
28623514
28633515 if (req->needs_extra_trb) {
2864
- unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
2865
-
28663516 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
28673517 status);
2868
-
2869
- /* Reclaim MPS padding TRB for ZLP */
2870
- if (!req->direction && req->request.zero && req->request.length &&
2871
- !usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2872
- (IS_ALIGNED(req->request.length, maxp)))
2873
- ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, status);
2874
-
28753518 req->needs_extra_trb = false;
28763519 }
28773520
2878
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
2879
- usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2880
- /*
2881
- * unmap isoc request and move the request
2882
- * to the pending list to wait for kicking
2883
- * transfer again.
2884
- */
3521
+ /*
3522
+ * If MISS ISOC happens, we need to move the req from started_list
3523
+ * to cancelled_list, then unmap the req and clear the HWO of trb.
3524
+ * Later in the dwc3_gadget_endpoint_trbs_complete(), it will move
3525
+ * the req from the cancelled_list to the pending_list, and restart
3526
+ * the req for isoc transfer.
3527
+ */
3528
+ if (status == -EXDEV && usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
28853529 req->remaining = 0;
28863530 req->needs_extra_trb = false;
2887
- dwc3_gadget_move_cancelled_request(req);
3531
+ dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
28883532 if (req->trb) {
28893533 usb_gadget_unmap_request_by_dev(dwc->sysdev,
28903534 &req->request,
....@@ -2893,11 +3537,38 @@
28933537 req->trb = NULL;
28943538 }
28953539 ret = 0;
2896
-
28973540 goto out;
28983541 }
28993542
2900
- dwc3_gadget_giveback(dep, req, status);
3543
+ /*
3544
+ * The event status only reflects the status of the TRB with IOC set.
3545
+ * For the requests that don't set interrupt on completion, the driver
3546
+ * needs to check and return the status of the completed TRBs associated
3547
+ * with the request. Use the status of the last TRB of the request.
3548
+ */
3549
+ if (req->request.no_interrupt) {
3550
+ struct dwc3_trb *trb;
3551
+
3552
+ trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
3553
+ switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
3554
+ case DWC3_TRBSTS_MISSED_ISOC:
3555
+ /* Isoc endpoint only */
3556
+ request_status = -EXDEV;
3557
+ break;
3558
+ case DWC3_TRB_STS_XFER_IN_PROG:
3559
+ /* Applicable when End Transfer with ForceRM=0 */
3560
+ case DWC3_TRBSTS_SETUP_PENDING:
3561
+ /* Control endpoint only */
3562
+ case DWC3_TRBSTS_OK:
3563
+ default:
3564
+ request_status = 0;
3565
+ break;
3566
+ }
3567
+ } else {
3568
+ request_status = status;
3569
+ }
3570
+
3571
+ dwc3_gadget_giveback(dep, req, request_status);
29013572
29023573 out:
29033574 return ret;
....@@ -2907,14 +3578,20 @@
29073578 const struct dwc3_event_depevt *event, int status)
29083579 {
29093580 struct dwc3_request *req;
2910
- struct dwc3_request *tmp;
29113581
2912
- list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
3582
+ while (!list_empty(&dep->started_list)) {
29133583 int ret;
29143584
3585
+ req = next_request(&dep->started_list);
29153586 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
29163587 req, status);
29173588 if (ret)
3589
+ break;
3590
+ /*
3591
+ * The endpoint is disabled, let the dwc3_remove_requests()
3592
+ * handle the cleanup.
3593
+ */
3594
+ if (!dep->endpoint.desc)
29183595 break;
29193596 }
29203597 }
....@@ -2922,6 +3599,11 @@
29223599 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
29233600 {
29243601 struct dwc3_request *req;
3602
+ struct dwc3 *dwc = dep->dwc;
3603
+
3604
+ if (!dep->endpoint.desc || !dwc->pullups_connected ||
3605
+ !dwc->connected)
3606
+ return false;
29253607
29263608 if (!list_empty(&dep->pending_list))
29273609 return true;
....@@ -2943,49 +3625,58 @@
29433625 dep->frame_number = event->parameters;
29443626 }
29453627
2946
-static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2947
- const struct dwc3_event_depevt *event)
3628
+static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
3629
+ const struct dwc3_event_depevt *event, int status)
29483630 {
29493631 struct dwc3 *dwc = dep->dwc;
2950
- unsigned status = 0;
2951
- struct dwc3_request *req;
2952
- struct dwc3_request *tmp;
2953
-
2954
- dwc3_gadget_endpoint_frame_from_event(dep, event);
2955
-
2956
- if (event->status & DEPEVT_STATUS_BUSERR)
2957
- status = -ECONNRESET;
2958
-
2959
- if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2960
- status = -EXDEV;
3632
+ struct dwc3_request *req, *tmp;
3633
+ bool no_started_trb = true;
29613634
29623635 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
29633636
2964
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
3637
+ if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3638
+ goto out;
3639
+
3640
+ if (!dep->endpoint.desc)
3641
+ return no_started_trb;
3642
+
3643
+ /*
3644
+ * If MISS ISOC happens, we need to do the following three steps
3645
+ * to restart the reqs in the cancelled_list and pending_list
3646
+ * in order.
3647
+ * Step1. Move all the reqs from pending_list to the tail of
3648
+ * cancelled_list.
3649
+ * Step2. Move all the reqs from cancelled_list to the tail
3650
+ * of pending_list.
3651
+ * Step3. Stop and restart an isoc transfer.
3652
+ */
3653
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && status == -EXDEV &&
29653654 !list_empty(&dep->cancelled_list) &&
29663655 !list_empty(&dep->pending_list)) {
29673656 list_for_each_entry_safe(req, tmp, &dep->pending_list, list)
2968
- dwc3_gadget_move_cancelled_request(req);
3657
+ dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
29693658 }
29703659
2971
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
3660
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && status == -EXDEV &&
29723661 !list_empty(&dep->cancelled_list)) {
29733662 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list)
29743663 dwc3_gadget_move_queued_request(req);
29753664 }
29763665
2977
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
2978
- list_empty(&dep->started_list))
3666
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3667
+ list_empty(&dep->started_list) &&
3668
+ (list_empty(&dep->pending_list) || status == -EXDEV))
29793669 dwc3_stop_active_transfer(dep, true, true);
2980
- else if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2981
- dwc3_gadget_ep_should_continue(dep))
2982
- __dwc3_gadget_kick_transfer(dep);
3670
+ else if (dwc3_gadget_ep_should_continue(dep))
3671
+ if (__dwc3_gadget_kick_transfer(dep) == 0)
3672
+ no_started_trb = false;
29833673
3674
+out:
29843675 /*
29853676 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
29863677 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
29873678 */
2988
- if (dwc->revision < DWC3_REVISION_183A) {
3679
+ if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
29893680 u32 reg;
29903681 int i;
29913682
....@@ -2996,7 +3687,7 @@
29963687 continue;
29973688
29983689 if (!list_empty(&dep->started_list))
2999
- return;
3690
+ return no_started_trb;
30003691 }
30013692
30023693 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
....@@ -3005,13 +3696,171 @@
30053696
30063697 dwc->u1u2 = 0;
30073698 }
3699
+
3700
+ return no_started_trb;
3701
+}
3702
+
3703
+static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3704
+ const struct dwc3_event_depevt *event)
3705
+{
3706
+ int status = 0;
3707
+
3708
+ if (!dep->endpoint.desc)
3709
+ return;
3710
+
3711
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3712
+ dwc3_gadget_endpoint_frame_from_event(dep, event);
3713
+
3714
+ if (event->status & DEPEVT_STATUS_BUSERR)
3715
+ status = -ECONNRESET;
3716
+
3717
+ if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3718
+ status = -EXDEV;
3719
+
3720
+ dwc3_gadget_endpoint_trbs_complete(dep, event, status);
3721
+}
3722
+
3723
+static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3724
+ const struct dwc3_event_depevt *event)
3725
+{
3726
+ int status = 0;
3727
+
3728
+ dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3729
+
3730
+ if (event->status & DEPEVT_STATUS_BUSERR)
3731
+ status = -ECONNRESET;
3732
+
3733
+ if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3734
+ dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
30083735 }
30093736
30103737 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
30113738 const struct dwc3_event_depevt *event)
30123739 {
30133740 dwc3_gadget_endpoint_frame_from_event(dep, event);
3741
+
3742
+ /*
3743
+ * The XferNotReady event is generated only once before the endpoint
3744
+ * starts. It will be generated again when END_TRANSFER command is
3745
+ * issued. For some controller versions, the XferNotReady event may be
3746
+ * generated while the END_TRANSFER command is still in process. Ignore
3747
+ * it and wait for the next XferNotReady event after the command is
3748
+ * completed.
3749
+ */
3750
+ if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3751
+ return;
3752
+
30143753 (void) __dwc3_gadget_start_isoc(dep);
3754
+}
3755
+
3756
+static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3757
+ const struct dwc3_event_depevt *event)
3758
+{
3759
+ u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3760
+
3761
+ if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3762
+ return;
3763
+
3764
+ /*
3765
+ * The END_TRANSFER command will cause the controller to generate a
3766
+ * NoStream Event, and it's not due to the host DP NoStream rejection.
3767
+ * Ignore the next NoStream event.
3768
+ */
3769
+ if (dep->stream_capable)
3770
+ dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3771
+
3772
+ dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3773
+ dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3774
+ dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3775
+
3776
+ if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3777
+ struct dwc3 *dwc = dep->dwc;
3778
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
3779
+
3780
+ dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3781
+ if (dwc3_send_clear_stall_ep_cmd(dep)) {
3782
+ struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3783
+
3784
+ dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3785
+ if (dwc->delayed_status)
3786
+ __dwc3_gadget_ep0_set_halt(ep0, 1);
3787
+ return;
3788
+ }
3789
+
3790
+ dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3791
+ if (vdwc->clear_stall_protocol == dep->number)
3792
+ dwc3_ep0_send_delayed_status(dwc);
3793
+ }
3794
+
3795
+ if ((dep->flags & DWC3_EP_DELAY_START) &&
3796
+ !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3797
+ __dwc3_gadget_kick_transfer(dep);
3798
+
3799
+ dep->flags &= ~DWC3_EP_DELAY_START;
3800
+}
3801
+
3802
+static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3803
+ const struct dwc3_event_depevt *event)
3804
+{
3805
+ struct dwc3 *dwc = dep->dwc;
3806
+
3807
+ if (event->status == DEPEVT_STREAMEVT_FOUND) {
3808
+ dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3809
+ goto out;
3810
+ }
3811
+
3812
+ /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3813
+ switch (event->parameters) {
3814
+ case DEPEVT_STREAM_PRIME:
3815
+ /*
3816
+ * If the host can properly transition the endpoint state from
3817
+ * idle to prime after a NoStream rejection, there's no need to
3818
+ * force restarting the endpoint to reinitiate the stream. To
3819
+ * simplify the check, assume the host follows the USB spec if
3820
+ * it primed the endpoint more than once.
3821
+ */
3822
+ if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3823
+ if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3824
+ dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3825
+ else
3826
+ dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3827
+ }
3828
+
3829
+ break;
3830
+ case DEPEVT_STREAM_NOSTREAM:
3831
+ if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3832
+ !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3833
+ !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3834
+ break;
3835
+
3836
+ /*
3837
+ * If the host rejects a stream due to no active stream, by the
3838
+ * USB and xHCI spec, the endpoint will be put back to idle
3839
+ * state. When the host is ready (buffer added/updated), it will
3840
+ * prime the endpoint to inform the usb device controller. This
3841
+ * triggers the device controller to issue ERDY to restart the
3842
+ * stream. However, some hosts don't follow this and keep the
3843
+ * endpoint in the idle state. No prime will come despite host
3844
+ * streams are updated, and the device controller will not be
3845
+ * triggered to generate ERDY to move the next stream data. To
3846
+ * workaround this and maintain compatibility with various
3847
+ * hosts, force to reinitate the stream until the host is ready
3848
+ * instead of waiting for the host to prime the endpoint.
3849
+ */
3850
+ if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3851
+ unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3852
+
3853
+ dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3854
+ } else {
3855
+ dep->flags |= DWC3_EP_DELAY_START;
3856
+ dwc3_stop_active_transfer(dep, true, true);
3857
+ return;
3858
+ }
3859
+ break;
3860
+ }
3861
+
3862
+out:
3863
+ dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
30153864 }
30163865
30173866 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
....@@ -3019,25 +3868,20 @@
30193868 {
30203869 struct dwc3_ep *dep;
30213870 u8 epnum = event->endpoint_number;
3022
- u8 cmd;
30233871
30243872 dep = dwc->eps[epnum];
30253873
30263874 if (!(dep->flags & DWC3_EP_ENABLED)) {
3027
- if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
3875
+ if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED))
30283876 return;
30293877
30303878 /* Handle only EPCMDCMPLT when EP disabled */
3031
- if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3879
+ if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) &&
3880
+ !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE))
30323881 return;
30333882 }
30343883
30353884 if (epnum == 0 || epnum == 1) {
3036
- if (!dwc->connected &&
3037
- event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE) {
3038
- reinit_completion(&dwc->discon_done);
3039
- dwc->connected = true;
3040
- }
30413885 dwc3_ep0_interrupt(dwc, event);
30423886 return;
30433887 }
....@@ -3050,21 +3894,14 @@
30503894 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
30513895 break;
30523896 case DWC3_DEPEVT_EPCMDCMPLT:
3053
- cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3054
-
3055
- if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
3056
- dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3057
- dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3058
- dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3059
- if ((dep->flags & DWC3_EP_DELAY_START) &&
3060
- !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3061
- __dwc3_gadget_kick_transfer(dep);
3062
-
3063
- dep->flags &= ~DWC3_EP_DELAY_START;
3064
- }
3897
+ dwc3_gadget_endpoint_command_complete(dep, event);
3898
+ break;
3899
+ case DWC3_DEPEVT_XFERCOMPLETE:
3900
+ dwc3_gadget_endpoint_transfer_complete(dep, event);
30653901 break;
30663902 case DWC3_DEPEVT_STREAMEVT:
3067
- case DWC3_DEPEVT_XFERCOMPLETE:
3903
+ dwc3_gadget_endpoint_stream_event(dep, event);
3904
+ break;
30683905 case DWC3_DEPEVT_RXTXFIFOEVT:
30693906 break;
30703907 }
....@@ -3072,27 +3909,27 @@
30723909
30733910 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
30743911 {
3075
- if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3912
+ if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
30763913 spin_unlock(&dwc->lock);
3077
- dwc->gadget_driver->disconnect(&dwc->gadget);
3914
+ dwc->gadget_driver->disconnect(dwc->gadget);
30783915 spin_lock(&dwc->lock);
30793916 }
30803917 }
30813918
30823919 static void dwc3_suspend_gadget(struct dwc3 *dwc)
30833920 {
3084
- if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
3921
+ if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
30853922 spin_unlock(&dwc->lock);
3086
- dwc->gadget_driver->suspend(&dwc->gadget);
3923
+ dwc->gadget_driver->suspend(dwc->gadget);
30873924 spin_lock(&dwc->lock);
30883925 }
30893926 }
30903927
30913928 static void dwc3_resume_gadget(struct dwc3 *dwc)
30923929 {
3093
- if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3930
+ if (dwc->async_callbacks && dwc->gadget_driver->resume) {
30943931 spin_unlock(&dwc->lock);
3095
- dwc->gadget_driver->resume(&dwc->gadget);
3932
+ dwc->gadget_driver->resume(dwc->gadget);
30963933 spin_lock(&dwc->lock);
30973934 }
30983935 }
....@@ -3102,23 +3939,43 @@
31023939 if (!dwc->gadget_driver)
31033940 return;
31043941
3105
- if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
3942
+ if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
31063943 spin_unlock(&dwc->lock);
3107
- usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
3944
+ usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
31083945 spin_lock(&dwc->lock);
31093946 }
31103947 }
31113948
3112
-static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3949
+void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
31133950 bool interrupt)
31143951 {
3115
- struct dwc3_gadget_ep_cmd_params params;
3116
- u32 cmd;
3117
- int ret;
3952
+ struct dwc3 *dwc = dep->dwc;
3953
+
3954
+ /*
3955
+ * Only issue End Transfer command to the control endpoint of a started
3956
+ * Data Phase. Typically we should only do so in error cases such as
3957
+ * invalid/unexpected direction as described in the control transfer
3958
+ * flow of the programming guide.
3959
+ */
3960
+ if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
3961
+ return;
31183962
31193963 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3964
+ (dep->flags & DWC3_EP_DELAY_STOP) ||
31203965 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
31213966 return;
3967
+
3968
+ /*
3969
+ * If a Setup packet is received but yet to DMA out, the controller will
3970
+ * not process the End Transfer command of any endpoint. Polling of its
3971
+ * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
3972
+ * timeout. Delay issuing the End Transfer command until the Setup TRB is
3973
+ * prepared.
3974
+ */
3975
+ if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) {
3976
+ dep->flags |= DWC3_EP_DELAY_STOP;
3977
+ return;
3978
+ }
31223979
31233980 /*
31243981 * NOTICE: We are violating what the Databook says about the
....@@ -3147,20 +4004,9 @@
31474004 * This mode is NOT available on the DWC_usb31 IP.
31484005 */
31494006
3150
- cmd = DWC3_DEPCMD_ENDTRANSFER;
3151
- cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3152
- cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
3153
- cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3154
- memset(&params, 0, sizeof(params));
3155
- ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
3156
- WARN_ON_ONCE(ret);
3157
- dep->resource_index = 0;
3158
-
3159
- if (!interrupt)
3160
- dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3161
- else
3162
- dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
4007
+ __dwc3_stop_active_transfer(dep, force, interrupt);
31634008 }
4009
+EXPORT_SYMBOL_GPL(dwc3_stop_active_transfer);
31644010
31654011 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
31664012 {
....@@ -3188,30 +4034,36 @@
31884034 {
31894035 int reg;
31904036
4037
+ dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
4038
+
31914039 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
31924040 reg &= ~DWC3_DCTL_INITU1ENA;
3193
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3194
-
31954041 reg &= ~DWC3_DCTL_INITU2ENA;
3196
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4042
+ dwc3_gadget_dctl_write_safe(dwc, reg);
4043
+
4044
+ dwc->connected = false;
31974045
31984046 dwc3_disconnect_gadget(dwc);
31994047
3200
- dwc->gadget.speed = USB_SPEED_UNKNOWN;
4048
+ dwc->gadget->speed = USB_SPEED_UNKNOWN;
32014049 dwc->setup_packet_pending = false;
3202
- usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
4050
+ usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
32034051
3204
- dwc->connected = false;
3205
- complete(&dwc->discon_done);
4052
+ if (dwc->ep0state != EP0_SETUP_PHASE) {
4053
+ unsigned int dir;
4054
+
4055
+ dir = !!dwc->ep0_expect_in;
4056
+ if (dwc->ep0state == EP0_DATA_PHASE)
4057
+ dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
4058
+ else
4059
+ dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
4060
+ dwc3_ep0_stall_and_restart(dwc);
4061
+ }
32064062 }
32074063
32084064 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
32094065 {
32104066 u32 reg;
3211
-
3212
- if (of_device_is_compatible(dwc->dev->parent->of_node,
3213
- "rockchip,rk3399-dwc3"))
3214
- phy_calibrate(dwc->usb2_generic_phy);
32154067
32164068 /*
32174069 * Ideally, dwc3_reset_gadget() would trigger the function
....@@ -3248,16 +4100,45 @@
32484100 * STAR#9000466709: RTL: Device : Disconnect event not
32494101 * generated if setup packet pending in FIFO
32504102 */
3251
- if (dwc->revision < DWC3_REVISION_188A) {
4103
+ if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
32524104 if (dwc->setup_packet_pending)
32534105 dwc3_gadget_disconnect_interrupt(dwc);
32544106 }
32554107
32564108 dwc3_reset_gadget(dwc);
32574109
4110
+ /*
4111
+ * From SNPS databook section 8.1.2, the EP0 should be in setup
4112
+ * phase. So ensure that EP0 is in setup phase by issuing a stall
4113
+ * and restart if EP0 is not in setup phase.
4114
+ */
4115
+ if (dwc->ep0state != EP0_SETUP_PHASE) {
4116
+ unsigned int dir;
4117
+
4118
+ dir = !!dwc->ep0_expect_in;
4119
+ if (dwc->ep0state == EP0_DATA_PHASE)
4120
+ dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
4121
+ else
4122
+ dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
4123
+
4124
+ dwc->eps[0]->trb_enqueue = 0;
4125
+ dwc->eps[1]->trb_enqueue = 0;
4126
+
4127
+ dwc3_ep0_stall_and_restart(dwc);
4128
+ }
4129
+
4130
+ /*
4131
+ * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
4132
+ * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
4133
+ * needs to ensure that it sends "a DEPENDXFER command for any active
4134
+ * transfers."
4135
+ */
4136
+ dwc3_stop_active_transfers(dwc);
4137
+ dwc->connected = true;
4138
+
32584139 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
32594140 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3260
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4141
+ dwc3_gadget_dctl_write_safe(dwc, reg);
32614142 dwc->test_mode = false;
32624143 dwc3_clear_stall_all_ep(dwc);
32634144
....@@ -3272,11 +4153,21 @@
32724153 struct dwc3_ep *dep;
32734154 int ret;
32744155 u32 reg;
4156
+ u8 lanes = 1;
32754157 u8 speed;
4158
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
4159
+
4160
+ if (!vdwc->softconnect)
4161
+ return;
32764162
32774163 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
32784164 speed = reg & DWC3_DSTS_CONNECTSPD;
32794165 dwc->speed = speed;
4166
+
4167
+ if (DWC3_IP_IS(DWC32))
4168
+ lanes = DWC3_DSTS_CONNLANES(reg) + 1;
4169
+
4170
+ dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
32804171
32814172 /*
32824173 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
....@@ -3290,8 +4181,13 @@
32904181 switch (speed) {
32914182 case DWC3_DSTS_SUPERSPEED_PLUS:
32924183 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3293
- dwc->gadget.ep0->maxpacket = 512;
3294
- dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
4184
+ dwc->gadget->ep0->maxpacket = 512;
4185
+ dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4186
+
4187
+ if (lanes > 1)
4188
+ dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
4189
+ else
4190
+ dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
32954191 break;
32964192 case DWC3_DSTS_SUPERSPEED:
32974193 /*
....@@ -3307,35 +4203,41 @@
33074203 * STAR#9000483510: RTL: SS : USB3 reset event may
33084204 * not be generated always when the link enters poll
33094205 */
3310
- if (dwc->revision < DWC3_REVISION_190A)
4206
+ if (DWC3_VER_IS_PRIOR(DWC3, 190A))
33114207 dwc3_gadget_reset_interrupt(dwc);
33124208
33134209 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3314
- dwc->gadget.ep0->maxpacket = 512;
3315
- dwc->gadget.speed = USB_SPEED_SUPER;
4210
+ dwc->gadget->ep0->maxpacket = 512;
4211
+ dwc->gadget->speed = USB_SPEED_SUPER;
4212
+
4213
+ if (lanes > 1) {
4214
+ dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4215
+ dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
4216
+ }
33164217 break;
33174218 case DWC3_DSTS_HIGHSPEED:
33184219 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3319
- dwc->gadget.ep0->maxpacket = 64;
3320
- dwc->gadget.speed = USB_SPEED_HIGH;
4220
+ dwc->gadget->ep0->maxpacket = 64;
4221
+ dwc->gadget->speed = USB_SPEED_HIGH;
33214222 break;
33224223 case DWC3_DSTS_FULLSPEED:
33234224 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3324
- dwc->gadget.ep0->maxpacket = 64;
3325
- dwc->gadget.speed = USB_SPEED_FULL;
4225
+ dwc->gadget->ep0->maxpacket = 64;
4226
+ dwc->gadget->speed = USB_SPEED_FULL;
33264227 break;
33274228 case DWC3_DSTS_LOWSPEED:
33284229 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3329
- dwc->gadget.ep0->maxpacket = 8;
3330
- dwc->gadget.speed = USB_SPEED_LOW;
4230
+ dwc->gadget->ep0->maxpacket = 8;
4231
+ dwc->gadget->speed = USB_SPEED_LOW;
33314232 break;
33324233 }
33334234
3334
- dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
4235
+ dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
33354236
33364237 /* Enable USB2 LPM Capability */
33374238
3338
- if ((dwc->revision > DWC3_REVISION_194A) &&
4239
+ if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
4240
+ !dwc->usb2_gadget_lpm_disable &&
33394241 (speed != DWC3_DSTS_SUPERSPEED) &&
33404242 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
33414243 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
....@@ -3345,7 +4247,8 @@
33454247 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
33464248 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
33474249
3348
- reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
4250
+ reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
4251
+ (dwc->is_utmi_l1_suspend << 4));
33494252
33504253 /*
33514254 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
....@@ -3353,23 +4256,24 @@
33534256 * BESL value in the LPM token is less than or equal to LPM
33544257 * NYET threshold.
33554258 */
3356
- WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3357
- && dwc->has_lpm_erratum,
4259
+ WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
33584260 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
33594261
3360
- if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
4262
+ if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
33614263 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
33624264
3363
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4265
+ dwc3_gadget_dctl_write_safe(dwc, reg);
33644266 } else {
4267
+ if (dwc->usb2_gadget_lpm_disable) {
4268
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4269
+ reg &= ~DWC3_DCFG_LPM_CAP;
4270
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4271
+ }
4272
+
33654273 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
33664274 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3367
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4275
+ dwc3_gadget_dctl_write_safe(dwc, reg);
33684276 }
3369
-
3370
-#ifdef CONFIG_ARCH_ROCKCHIP
3371
- dwc3_gadget_resize_tx_fifos(dwc);
3372
-#endif
33734277
33744278 dep = dwc->eps[0];
33754279 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
....@@ -3394,21 +4298,18 @@
33944298 */
33954299 }
33964300
3397
-static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo)
4301
+static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
33984302 {
3399
- enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
34004303 /*
34014304 * TODO take core out of low power mode when that's
34024305 * implemented.
34034306 */
34044307
3405
- if (dwc->gadget_driver && dwc->gadget_driver->resume && dwc->uwk_en) {
4308
+ if (dwc->async_callbacks && dwc->gadget_driver->resume) {
34064309 spin_unlock(&dwc->lock);
3407
- dwc->gadget_driver->resume(&dwc->gadget);
4310
+ dwc->gadget_driver->resume(dwc->gadget);
34084311 spin_lock(&dwc->lock);
34094312 }
3410
-
3411
- dwc->link_state = next;
34124313 }
34134314
34144315 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
....@@ -3435,7 +4336,7 @@
34354336 * operational mode
34364337 */
34374338 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3438
- if ((dwc->revision < DWC3_REVISION_250A) &&
4339
+ if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
34394340 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
34404341 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
34414342 (next == DWC3_LINK_STATE_RESUME)) {
....@@ -3461,7 +4362,7 @@
34614362 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
34624363 * core send LGO_Ux entering U0
34634364 */
3464
- if (dwc->revision < DWC3_REVISION_183A) {
4365
+ if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
34654366 if (next == DWC3_LINK_STATE_U0) {
34664367 u32 u1u2;
34674368 u32 reg;
....@@ -3480,7 +4381,7 @@
34804381
34814382 reg &= ~u1u2;
34824383
3483
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4384
+ dwc3_gadget_dctl_write_safe(dwc, reg);
34844385 break;
34854386 default:
34864387 /* do nothing */
....@@ -3514,8 +4415,7 @@
35144415 {
35154416 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
35164417
3517
- if (dwc->link_state != next && next == DWC3_LINK_STATE_U3 &&
3518
- dwc->uwk_en)
4418
+ if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
35194419 dwc3_suspend_gadget(dwc);
35204420
35214421 dwc->link_state = next;
....@@ -3561,8 +4461,7 @@
35614461 dwc3_gadget_conndone_interrupt(dwc);
35624462 break;
35634463 case DWC3_DEVICE_EVENT_WAKEUP:
3564
- dev_dbg(dwc->dev, "device wakeup\n");
3565
- dwc3_gadget_wakeup_interrupt(dwc, event->event_info);
4464
+ dwc3_gadget_wakeup_interrupt(dwc);
35664465 break;
35674466 case DWC3_DEVICE_EVENT_HIBER_REQ:
35684467 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
....@@ -3574,15 +4473,14 @@
35744473 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
35754474 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
35764475 break;
3577
- case DWC3_DEVICE_EVENT_EOPF:
4476
+ case DWC3_DEVICE_EVENT_SUSPEND:
35784477 /* It changed to be suspend event for version 2.30a and above */
3579
- if (dwc->revision >= DWC3_REVISION_230A) {
4478
+ if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
35804479 /*
35814480 * Ignore suspend event until the gadget enters into
35824481 * USB_STATE_CONFIGURED state.
35834482 */
3584
- dev_dbg(dwc->dev, "device suspend\n");
3585
- if (dwc->gadget.state >= USB_STATE_CONFIGURED)
4483
+ if (dwc->gadget->state >= USB_STATE_CONFIGURED)
35864484 dwc3_gadget_suspend_interrupt(dwc,
35874485 event->event_info);
35884486 }
....@@ -3615,7 +4513,6 @@
36154513 struct dwc3 *dwc = evt->dwc;
36164514 irqreturn_t ret = IRQ_NONE;
36174515 int left;
3618
- u32 reg;
36194516
36204517 left = evt->count;
36214518
....@@ -3643,18 +4540,19 @@
36434540 }
36444541
36454542 evt->count = 0;
3646
- evt->flags &= ~DWC3_EVENT_PENDING;
36474543 ret = IRQ_HANDLED;
36484544
36494545 /* Unmask interrupt */
3650
- reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3651
- reg &= ~DWC3_GEVNTSIZ_INTMASK;
3652
- dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
4546
+ dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4547
+ DWC3_GEVNTSIZ_SIZE(evt->length));
36534548
36544549 if (dwc->imod_interval) {
36554550 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
36564551 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
36574552 }
4553
+
4554
+ /* Keep the clearing of DWC3_EVENT_PENDING at the end */
4555
+ evt->flags &= ~DWC3_EVENT_PENDING;
36584556
36594557 return ret;
36604558 }
....@@ -3680,7 +4578,6 @@
36804578 struct dwc3 *dwc = evt->dwc;
36814579 u32 amount;
36824580 u32 count;
3683
- u32 reg;
36844581
36854582 if (pm_runtime_suspended(dwc->dev)) {
36864583 pm_runtime_get(dwc->dev);
....@@ -3707,9 +4604,8 @@
37074604 evt->flags |= DWC3_EVENT_PENDING;
37084605
37094606 /* Mask interrupt */
3710
- reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3711
- reg |= DWC3_GEVNTSIZ_INTMASK;
3712
- dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
4607
+ dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4608
+ DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length));
37134609
37144610 amount = min(count, evt->length - evt->lpos);
37154611 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
....@@ -3734,14 +4630,14 @@
37344630 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
37354631 int irq;
37364632
3737
- irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
4633
+ irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
37384634 if (irq > 0)
37394635 goto out;
37404636
37414637 if (irq == -EPROBE_DEFER)
37424638 goto out;
37434639
3744
- irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
4640
+ irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
37454641 if (irq > 0)
37464642 goto out;
37474643
....@@ -3752,14 +4648,18 @@
37524648 if (irq > 0)
37534649 goto out;
37544650
3755
- if (irq != -EPROBE_DEFER)
3756
- dev_err(dwc->dev, "missing peripheral IRQ\n");
3757
-
37584651 if (!irq)
37594652 irq = -EINVAL;
37604653
37614654 out:
37624655 return irq;
4656
+}
4657
+
4658
+static void dwc_gadget_release(struct device *dev)
4659
+{
4660
+ struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
4661
+
4662
+ kfree(gadget);
37634663 }
37644664
37654665 /**
....@@ -3772,6 +4672,7 @@
37724672 {
37734673 int ret;
37744674 int irq;
4675
+ struct device *dev;
37754676
37764677 irq = dwc3_gadget_get_irq(dwc);
37774678 if (irq < 0) {
....@@ -3804,17 +4705,22 @@
38044705 }
38054706
38064707 init_completion(&dwc->ep0_in_setup);
3807
- init_completion(&dwc->discon_done);
4708
+ dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4709
+ if (!dwc->gadget) {
4710
+ ret = -ENOMEM;
4711
+ goto err3;
4712
+ }
38084713
3809
- dwc->gadget.ops = &dwc3_gadget_ops;
3810
- dwc->gadget.speed = USB_SPEED_UNKNOWN;
3811
- dwc->gadget.sg_supported = true;
3812
- dwc->gadget.name = "dwc3-gadget";
3813
-#ifdef CONFIG_ARCH_ROCKCHIP
3814
- dwc->gadget.lpm_capable = false;
3815
-#else
3816
- dwc->gadget.lpm_capable = true;
3817
-#endif
4714
+
4715
+ usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
4716
+ dev = &dwc->gadget->dev;
4717
+ dev->platform_data = dwc;
4718
+ dwc->gadget->ops = &dwc3_gadget_ops;
4719
+ dwc->gadget->speed = USB_SPEED_UNKNOWN;
4720
+ dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
4721
+ dwc->gadget->sg_supported = true;
4722
+ dwc->gadget->name = "dwc3-gadget";
4723
+ dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
38184724
38194725 /*
38204726 * FIXME We might be setting max_speed to <SUPER, however versions
....@@ -3832,12 +4738,13 @@
38324738 * is less than super speed because we don't have means, yet, to tell
38334739 * composite.c that we are USB 2.0 + LPM ECN.
38344740 */
3835
- if (dwc->revision < DWC3_REVISION_220A &&
4741
+ if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
38364742 !dwc->dis_metastability_quirk)
38374743 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
38384744 dwc->revision);
38394745
3840
- dwc->gadget.max_speed = dwc->maximum_speed;
4746
+ dwc->gadget->max_speed = dwc->maximum_speed;
4747
+ dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
38414748
38424749 /*
38434750 * REVISIT: Here we should clear all pending IRQs to be
....@@ -3846,21 +4753,26 @@
38464753
38474754 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
38484755 if (ret)
3849
- goto err3;
3850
-
3851
- ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3852
- if (ret) {
3853
- dev_err(dwc->dev, "failed to register udc\n");
38544756 goto err4;
4757
+
4758
+ ret = usb_add_gadget(dwc->gadget);
4759
+ if (ret) {
4760
+ dev_err(dwc->dev, "failed to add gadget\n");
4761
+ goto err5;
38554762 }
38564763
3857
- dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
4764
+ if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4765
+ dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4766
+ else
4767
+ dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
38584768
38594769 return 0;
38604770
3861
-err4:
4771
+err5:
38624772 dwc3_gadget_free_endpoints(dwc);
3863
-
4773
+err4:
4774
+ usb_put_gadget(dwc->gadget);
4775
+ dwc->gadget = NULL;
38644776 err3:
38654777 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
38664778 dwc->bounce_addr);
....@@ -3880,8 +4792,12 @@
38804792
38814793 void dwc3_gadget_exit(struct dwc3 *dwc)
38824794 {
3883
- usb_del_gadget_udc(&dwc->gadget);
4795
+ if (!dwc->gadget)
4796
+ return;
4797
+
4798
+ usb_del_gadget(dwc->gadget);
38844799 dwc3_gadget_free_endpoints(dwc);
4800
+ usb_put_gadget(dwc->gadget);
38854801 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
38864802 dwc->bounce_addr);
38874803 kfree(dwc->setup_buf);
....@@ -3891,21 +4807,27 @@
38914807
38924808 int dwc3_gadget_suspend(struct dwc3 *dwc)
38934809 {
4810
+ unsigned long flags;
4811
+
38944812 if (!dwc->gadget_driver)
38954813 return 0;
38964814
38974815 dwc3_gadget_run_stop(dwc, false, false);
4816
+
4817
+ spin_lock_irqsave(&dwc->lock, flags);
38984818 dwc3_disconnect_gadget(dwc);
38994819 __dwc3_gadget_stop(dwc);
4820
+ spin_unlock_irqrestore(&dwc->lock, flags);
39004821
39014822 return 0;
39024823 }
39034824
39044825 int dwc3_gadget_resume(struct dwc3 *dwc)
39054826 {
4827
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
39064828 int ret;
39074829
3908
- if (!dwc->gadget_driver)
4830
+ if (!dwc->gadget_driver || !vdwc->softconnect)
39094831 return 0;
39104832
39114833 ret = __dwc3_gadget_start(dwc);