hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
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.
....@@ -353,6 +180,7 @@
353180 list_del(&req->list);
354181 req->remaining = 0;
355182 req->needs_extra_trb = false;
183
+ req->num_trbs = 0;
356184
357185 if (req->request.status == -EINPROGRESS)
358186 req->request.status = status;
....@@ -400,7 +228,8 @@
400228 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
401229 * and wait for its completion.
402230 */
403
-int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
231
+int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
232
+ u32 param)
404233 {
405234 u32 timeout = 500;
406235 int status = 0;
....@@ -441,7 +270,7 @@
441270 * Caller should handle locking. This function will issue @cmd with given
442271 * @params to @dep and wait for its completion.
443272 */
444
-int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
273
+int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
445274 struct dwc3_gadget_ep_cmd_params *params)
446275 {
447276 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
....@@ -463,7 +292,8 @@
463292 *
464293 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
465294 */
466
- if (dwc->gadget.speed <= USB_SPEED_HIGH) {
295
+ if (dwc->gadget->speed <= USB_SPEED_HIGH ||
296
+ DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
467297 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
468298 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
469299 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
....@@ -482,19 +312,38 @@
482312 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
483313 int link_state;
484314
315
+ /*
316
+ * Initiate remote wakeup if the link state is in U3 when
317
+ * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
318
+ * link state is in U1/U2, no remote wakeup is needed. The Start
319
+ * Transfer command will initiate the link recovery.
320
+ */
485321 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) {
322
+ switch (link_state) {
323
+ case DWC3_LINK_STATE_U2:
324
+ if (dwc->gadget->speed >= USB_SPEED_SUPER)
325
+ break;
326
+
327
+ fallthrough;
328
+ case DWC3_LINK_STATE_U3:
489329 ret = __dwc3_gadget_wakeup(dwc);
490330 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
491331 ret);
332
+ break;
492333 }
493334 }
494335
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);
336
+ /*
337
+ * For some commands such as Update Transfer command, DEPCMDPARn
338
+ * registers are reserved. Since the driver often sends Update Transfer
339
+ * command, don't write to DEPCMDPARn to avoid register write delays and
340
+ * improve performance.
341
+ */
342
+ if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) {
343
+ dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
344
+ dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
345
+ dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
346
+ }
498347
499348 /*
500349 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
....@@ -518,6 +367,14 @@
518367 cmd |= DWC3_DEPCMD_CMDACT;
519368
520369 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
370
+
371
+ if (!(cmd & DWC3_DEPCMD_CMDACT) ||
372
+ (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
373
+ !(cmd & DWC3_DEPCMD_CMDIOC))) {
374
+ ret = 0;
375
+ goto skip_status;
376
+ }
377
+
521378 do {
522379 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
523380 if (!(reg & DWC3_DEPCMD_CMDACT)) {
....@@ -528,6 +385,8 @@
528385 ret = 0;
529386 break;
530387 case DEPEVT_TRANSFER_NO_RESOURCE:
388
+ dev_WARN(dwc->dev, "No resource for %s\n",
389
+ dep->name);
531390 ret = -EINVAL;
532391 break;
533392 case DEPEVT_TRANSFER_BUS_EXPIRY:
....@@ -557,11 +416,15 @@
557416 cmd_status = -ETIMEDOUT;
558417 }
559418
419
+skip_status:
560420 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
561421
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);
422
+ if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
423
+ if (ret == 0)
424
+ dep->flags |= DWC3_EP_TRANSFER_STARTED;
425
+
426
+ if (ret != -ETIMEDOUT)
427
+ dwc3_gadget_ep_get_transfer_index(dep);
565428 }
566429
567430 if (saved_config) {
....@@ -572,6 +435,7 @@
572435
573436 return ret;
574437 }
438
+EXPORT_SYMBOL_GPL(dwc3_send_gadget_ep_cmd);
575439
576440 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
577441 {
....@@ -587,8 +451,9 @@
587451 * IN transfers due to a mishandled error condition. Synopsys
588452 * STAR 9000614252.
589453 */
590
- if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
591
- (dwc->gadget.speed >= USB_SPEED_SUPER))
454
+ if (dep->direction &&
455
+ !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
456
+ (dwc->gadget->speed >= USB_SPEED_SUPER))
592457 cmd |= DWC3_DEPCMD_CLEARPENDIN;
593458
594459 memset(&params, 0, sizeof(params));
....@@ -728,8 +593,9 @@
728593 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
729594
730595 /* Burst size is only needed in SuperSpeed mode */
731
- if (dwc->gadget.speed >= USB_SPEED_SUPER) {
596
+ if (dwc->gadget->speed >= USB_SPEED_SUPER) {
732597 u32 burst = dep->endpoint.maxburst;
598
+
733599 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
734600 }
735601
....@@ -745,6 +611,7 @@
745611
746612 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
747613 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
614
+ | DWC3_DEPCFG_XFER_COMPLETE_EN
748615 | DWC3_DEPCFG_STREAM_EVENT_EN;
749616 dep->stream_capable = true;
750617 }
....@@ -781,7 +648,7 @@
781648 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
782649
783650 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
784
- dwc->gadget.speed == USB_SPEED_FULL)
651
+ dwc->gadget->speed == USB_SPEED_FULL)
785652 dep->interval = desc->bInterval;
786653 else
787654 dep->interval = 1 << (desc->bInterval - 1);
....@@ -790,6 +657,335 @@
790657 }
791658
792659 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
660
+}
661
+
662
+/**
663
+ * dwc3_gadget_get_tx_fifos_size - Get the txfifos total size
664
+ * @dwc: pointer to the DWC3 context
665
+ *
666
+ * 3-RAM configuration:
667
+ * RAM0 depth = Descriptor Cache depth
668
+ * RAM1 depth = TxFIFOs depth
669
+ * RAM2 depth = RxFIFOs depth
670
+ *
671
+ * 2-RAM configuration:
672
+ * RAM0 depth = Descriptor Cache depth + RxFIFOs depth
673
+ * RAM1 depth = TxFIFOs depth
674
+ *
675
+ * 1-RAM configuration:
676
+ * RAM0 depth = Descriptor Cache depth + RxFIFOs depth + TxFIFOs depth
677
+ */
678
+static int dwc3_gadget_get_tx_fifos_size(struct dwc3 *dwc)
679
+{
680
+ int txfifo_depth = 0;
681
+ int ram0_depth, rxfifo_size;
682
+
683
+ /* Get the depth of the TxFIFOs */
684
+ if (DWC3_NUM_RAMS(dwc->hwparams.hwparams1) > 1) {
685
+ /* For 2 or 3-RAM, RAM1 contains TxFIFOs */
686
+ txfifo_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
687
+ } else {
688
+ /* For 1-RAM, RAM0 contains Descriptor Cache, RxFIFOs, and TxFIFOs */
689
+ ram0_depth = DWC3_GHWPARAMS6_RAM0_DEPTH(dwc->hwparams.hwparams6);
690
+
691
+ /* All OUT endpoints share a single RxFIFO space */
692
+ rxfifo_size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
693
+ if (DWC3_IP_IS(DWC3))
694
+ txfifo_depth = ram0_depth - DWC3_GRXFIFOSIZ_RXFDEP(rxfifo_size);
695
+ else
696
+ txfifo_depth = ram0_depth - DWC31_GRXFIFOSIZ_RXFDEP(rxfifo_size);
697
+
698
+ /* The value of GRxFIFOSIZ0[31:16] is the depth of Descriptor Cache */
699
+ txfifo_depth -= DWC3_GRXFIFOSIZ_RXFSTADDR(rxfifo_size) >> 16;
700
+ }
701
+
702
+ return txfifo_depth;
703
+}
704
+
705
+/**
706
+ * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
707
+ * @dwc: pointer to the DWC3 context
708
+ * @nfifos: number of fifos to calculate for
709
+ *
710
+ * Calculates the size value based on the equation below:
711
+ *
712
+ * DWC3 revision 280A and prior:
713
+ * fifo_size = mult * (max_packet / mdwidth) + 1;
714
+ *
715
+ * DWC3 revision 290A and onwards:
716
+ * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
717
+ *
718
+ * The max packet size is set to 1024, as the txfifo requirements mainly apply
719
+ * to super speed USB use cases. However, it is safe to overestimate the fifo
720
+ * allocations for other scenarios, i.e. high speed USB.
721
+ */
722
+static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
723
+{
724
+ int max_packet = 1024;
725
+ int fifo_size;
726
+ int mdwidth;
727
+
728
+ mdwidth = dwc3_mdwidth(dwc);
729
+
730
+ /* MDWIDTH is represented in bits, we need it in bytes */
731
+ mdwidth >>= 3;
732
+
733
+ if (DWC3_VER_IS_PRIOR(DWC3, 290A))
734
+ fifo_size = mult * (max_packet / mdwidth) + 1;
735
+ else
736
+ fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
737
+ return fifo_size;
738
+}
739
+
740
+/**
741
+ * dwc3_gadget_clear_tx_fifo_size - Clears txfifo allocation
742
+ * @dwc: pointer to the DWC3 context
743
+ *
744
+ * Iterates through all the endpoint registers and clears the previous txfifo
745
+ * allocations.
746
+ */
747
+void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
748
+{
749
+ struct dwc3_ep *dep;
750
+ int fifo_depth;
751
+ int size;
752
+ int num;
753
+
754
+ if (!dwc->do_fifo_resize)
755
+ return;
756
+
757
+ /* Read ep0IN related TXFIFO size */
758
+ dep = dwc->eps[1];
759
+ size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
760
+ if (DWC3_IP_IS(DWC3))
761
+ fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
762
+ else
763
+ fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
764
+
765
+ dwc->last_fifo_depth = fifo_depth;
766
+ /* Clear existing TXFIFO for all IN eps except ep0 */
767
+ for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
768
+ num += 2) {
769
+ dep = dwc->eps[num];
770
+ /* Don't change TXFRAMNUM on usb31 version */
771
+ size = DWC3_IP_IS(DWC3) ? 0 :
772
+ dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
773
+ DWC31_GTXFIFOSIZ_TXFRAMNUM;
774
+
775
+ dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
776
+ dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
777
+ }
778
+ dwc->num_ep_resized = 0;
779
+}
780
+
781
+/**
782
+ * __dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for Rockchip platform
783
+ *
784
+ * @dep: pointer to dwc3_ep structure
785
+ *
786
+ * According to the different USB transfer type and Speed,
787
+ * this function will a best effort FIFO allocation in order
788
+ * to improve FIFO usage and throughput, while still allowing
789
+ * us to enable as many endpoints as possible.
790
+ */
791
+static int __dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
792
+{
793
+ struct dwc3 *dwc = dep->dwc;
794
+ u32 fifo_0_start, last_fifo_depth, ram1_depth;
795
+ u32 fifo_size, maxpacket, mdwidth, mult;
796
+ u32 tmp;
797
+
798
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
799
+ /*
800
+ * Set enough tx fifos for Isochronous endpoints to get better
801
+ * performance and more compliance with bus latency.
802
+ */
803
+ maxpacket = dep->endpoint.maxpacket;
804
+ if (gadget_is_superspeed(dwc->gadget))
805
+ mult = dep->endpoint.mult * dep->endpoint.maxburst;
806
+ else
807
+ mult = dep->endpoint.mult;
808
+
809
+ mult = mult > 0 ? mult * 2 : 3;
810
+ if (mult > 6)
811
+ mult = 6;
812
+ } else if (usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
813
+ /*
814
+ * Set enough tx fifos for Bulk endpoints to get
815
+ * better transmission performance.
816
+ */
817
+ mult = 3;
818
+ if (gadget_is_superspeed(dwc->gadget)) {
819
+ if (dep->endpoint.maxburst > mult) {
820
+ mult = dep->endpoint.maxburst;
821
+ if (mult > 6)
822
+ mult = 6;
823
+ }
824
+ maxpacket = 1024;
825
+ } else {
826
+ maxpacket = 512;
827
+ }
828
+ } else if (usb_endpoint_xfer_int(dep->endpoint.desc)) {
829
+ /*
830
+ * REVIST: we assume that the maxpacket of interrupt
831
+ * endpoint is 64 Bytes for MTP and the other functions.
832
+ */
833
+ mult = 1;
834
+ maxpacket = 64;
835
+ } else {
836
+ goto out;
837
+ }
838
+
839
+ mdwidth = dwc3_mdwidth(dwc);
840
+ mdwidth >>= 3; /* bits convert to bytes */
841
+ ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
842
+ last_fifo_depth = dwc->last_fifo_depth;
843
+
844
+ /* Calculate the fifo size for this EP */
845
+ tmp = mult * (maxpacket + mdwidth);
846
+ tmp += mdwidth;
847
+ fifo_size = DIV_ROUND_UP(tmp, mdwidth);
848
+
849
+ /* Check if TXFIFOs start at non-zero addr */
850
+ tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
851
+ fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
852
+ fifo_size |= (fifo_0_start + (last_fifo_depth << 16));
853
+
854
+ if (DWC3_IP_IS(DWC3))
855
+ last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
856
+ else
857
+ last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
858
+
859
+ /* Check fifo size allocation doesn't exceed available RAM size. */
860
+ if (last_fifo_depth >= ram1_depth) {
861
+ dev_err(dwc->dev, "Fifosize(0x%x) > RAM size(0x%x) %s depth(0x%x)\n",
862
+ last_fifo_depth, ram1_depth,
863
+ dep->endpoint.name, fifo_size & 0xfff);
864
+ return -ENOMEM;
865
+ }
866
+
867
+ dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
868
+ dep->flags |= DWC3_EP_TXFIFO_RESIZED;
869
+ dwc->last_fifo_depth = last_fifo_depth;
870
+ dwc->num_ep_resized++;
871
+
872
+out:
873
+ return 0;
874
+}
875
+
876
+/*
877
+ * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
878
+ * @dwc: pointer to our context structure
879
+ *
880
+ * This function will a best effort FIFO allocation in order
881
+ * to improve FIFO usage and throughput, while still allowing
882
+ * us to enable as many endpoints as possible.
883
+ *
884
+ * Keep in mind that this operation will be highly dependent
885
+ * on the configured size for RAM1 - which contains TxFifo -,
886
+ * the amount of endpoints enabled on coreConsultant tool, and
887
+ * the width of the Master Bus.
888
+ *
889
+ * In general, FIFO depths are represented with the following equation:
890
+ *
891
+ * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
892
+ *
893
+ * In conjunction with dwc3_gadget_check_config(), this resizing logic will
894
+ * ensure that all endpoints will have enough internal memory for one max
895
+ * packet per endpoint.
896
+ */
897
+static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
898
+{
899
+ struct dwc3 *dwc = dep->dwc;
900
+ int fifo_0_start;
901
+ int ram1_depth;
902
+ int fifo_size;
903
+ int min_depth;
904
+ int num_in_ep;
905
+ int remaining;
906
+ int num_fifos = 1;
907
+ int fifo;
908
+ int tmp;
909
+
910
+ if (!dwc->do_fifo_resize)
911
+ return 0;
912
+
913
+ /* resize IN endpoints except ep0 */
914
+ if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
915
+ return 0;
916
+
917
+ /* bail if already resized */
918
+ if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
919
+ return 0;
920
+
921
+ if (IS_REACHABLE(CONFIG_ARCH_ROCKCHIP))
922
+ return __dwc3_gadget_resize_tx_fifos(dep);
923
+
924
+ ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
925
+
926
+ if ((dep->endpoint.maxburst > 1 &&
927
+ usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
928
+ usb_endpoint_xfer_isoc(dep->endpoint.desc))
929
+ num_fifos = 3;
930
+
931
+ if (dep->endpoint.maxburst > 6 &&
932
+ (usb_endpoint_xfer_bulk(dep->endpoint.desc) ||
933
+ usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31))
934
+ num_fifos = dwc->tx_fifo_resize_max_num;
935
+
936
+ /* FIFO size for a single buffer */
937
+ fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
938
+
939
+ /* Calculate the number of remaining EPs w/o any FIFO */
940
+ num_in_ep = dwc->max_cfg_eps;
941
+ num_in_ep -= dwc->num_ep_resized;
942
+
943
+ /* Reserve at least one FIFO for the number of IN EPs */
944
+ min_depth = num_in_ep * (fifo + 1);
945
+ remaining = ram1_depth - min_depth - dwc->last_fifo_depth;
946
+ remaining = max_t(int, 0, remaining);
947
+ /*
948
+ * We've already reserved 1 FIFO per EP, so check what we can fit in
949
+ * addition to it. If there is not enough remaining space, allocate
950
+ * all the remaining space to the EP.
951
+ */
952
+ fifo_size = (num_fifos - 1) * fifo;
953
+ if (remaining < fifo_size)
954
+ fifo_size = remaining;
955
+
956
+ fifo_size += fifo;
957
+ /* Last increment according to the TX FIFO size equation */
958
+ fifo_size++;
959
+
960
+ /* Check if TXFIFOs start at non-zero addr */
961
+ tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
962
+ fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
963
+
964
+ fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
965
+ if (DWC3_IP_IS(DWC3))
966
+ dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
967
+ else
968
+ dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
969
+
970
+ /* Check fifo size allocation doesn't exceed available RAM size. */
971
+ if (dwc->last_fifo_depth >= ram1_depth) {
972
+ dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
973
+ dwc->last_fifo_depth, ram1_depth,
974
+ dep->endpoint.name, fifo_size);
975
+ if (DWC3_IP_IS(DWC3))
976
+ fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
977
+ else
978
+ fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
979
+
980
+ dwc->last_fifo_depth -= fifo_size;
981
+ return -ENOMEM;
982
+ }
983
+
984
+ dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
985
+ dep->flags |= DWC3_EP_TXFIFO_RESIZED;
986
+ dwc->num_ep_resized++;
987
+
988
+ return 0;
793989 }
794990
795991 /**
....@@ -809,6 +1005,10 @@
8091005 int ret;
8101006
8111007 if (!(dep->flags & DWC3_EP_ENABLED)) {
1008
+ ret = dwc3_gadget_resize_tx_fifos(dep);
1009
+ if (ret)
1010
+ return ret;
1011
+
8121012 ret = dwc3_gadget_start_config(dep);
8131013 if (ret)
8141014 return ret;
....@@ -829,12 +1029,13 @@
8291029 reg |= DWC3_DALEPENA_EP(dep->number);
8301030 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
8311031
1032
+ dep->trb_dequeue = 0;
1033
+ dep->trb_enqueue = 0;
1034
+
8321035 if (usb_endpoint_xfer_control(desc))
8331036 goto out;
8341037
8351038 /* Initialize the TRB ring */
836
- dep->trb_dequeue = 0;
837
- dep->trb_enqueue = 0;
8381039 memset(dep->trb_pool, 0,
8391040 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
8401041
....@@ -852,7 +1053,7 @@
8521053 * Issue StartTransfer here with no-op TRB so we can always rely on No
8531054 * Response Update Transfer command.
8541055 */
855
- if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
1056
+ if (usb_endpoint_xfer_bulk(desc) ||
8561057 usb_endpoint_xfer_int(desc)) {
8571058 struct dwc3_gadget_ep_cmd_params params;
8581059 struct dwc3_trb *trb;
....@@ -871,6 +1072,37 @@
8711072 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
8721073 if (ret < 0)
8731074 return ret;
1075
+
1076
+ if (dep->stream_capable) {
1077
+ /*
1078
+ * For streams, at start, there maybe a race where the
1079
+ * host primes the endpoint before the function driver
1080
+ * queues a request to initiate a stream. In that case,
1081
+ * the controller will not see the prime to generate the
1082
+ * ERDY and start stream. To workaround this, issue a
1083
+ * no-op TRB as normal, but end it immediately. As a
1084
+ * result, when the function driver queues the request,
1085
+ * the next START_TRANSFER command will cause the
1086
+ * controller to generate an ERDY to initiate the
1087
+ * stream.
1088
+ */
1089
+ dwc3_stop_active_transfer(dep, true, true);
1090
+
1091
+ /*
1092
+ * All stream eps will reinitiate stream on NoStream
1093
+ * rejection until we can determine that the host can
1094
+ * prime after the first transfer.
1095
+ *
1096
+ * However, if the controller is capable of
1097
+ * TXF_FLUSH_BYPASS, then IN direction endpoints will
1098
+ * automatically restart the stream without the driver
1099
+ * initiation.
1100
+ */
1101
+ if (!dep->direction ||
1102
+ !(dwc->hwparams.hwparams9 &
1103
+ DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
1104
+ dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
1105
+ }
8741106 }
8751107
8761108 out:
....@@ -879,31 +1111,33 @@
8791111 return 0;
8801112 }
8811113
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)
1114
+void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
8851115 {
8861116 struct dwc3_request *req;
8871117
8881118 dwc3_stop_active_transfer(dep, true, false);
8891119
1120
+ /* If endxfer is delayed, avoid unmapping requests */
1121
+ if (dep->flags & DWC3_EP_DELAY_STOP)
1122
+ return;
1123
+
8901124 /* - giveback all requests to gadget driver */
8911125 while (!list_empty(&dep->started_list)) {
8921126 req = next_request(&dep->started_list);
8931127
894
- dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1128
+ dwc3_gadget_giveback(dep, req, status);
8951129 }
8961130
8971131 while (!list_empty(&dep->pending_list)) {
8981132 req = next_request(&dep->pending_list);
8991133
900
- dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1134
+ dwc3_gadget_giveback(dep, req, status);
9011135 }
9021136
9031137 while (!list_empty(&dep->cancelled_list)) {
9041138 req = next_request(&dep->cancelled_list);
9051139
906
- dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1140
+ dwc3_gadget_giveback(dep, req, status);
9071141 }
9081142 }
9091143
....@@ -921,10 +1155,9 @@
9211155 {
9221156 struct dwc3 *dwc = dep->dwc;
9231157 u32 reg;
1158
+ u32 mask;
9241159
9251160 trace_dwc3_gadget_ep_disable(dep);
926
-
927
- dwc3_remove_requests(dwc, dep);
9281161
9291162 /* make sure HW endpoint isn't stalled */
9301163 if (dep->flags & DWC3_EP_STALL)
....@@ -934,9 +1167,19 @@
9341167 reg &= ~DWC3_DALEPENA_EP(dep->number);
9351168 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
9361169
1170
+ dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
1171
+
9371172 dep->stream_capable = false;
9381173 dep->type = 0;
939
- dep->flags = 0;
1174
+ mask = DWC3_EP_TXFIFO_RESIZED;
1175
+ /*
1176
+ * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is
1177
+ * set. Do not clear DEP flags, so that the end transfer command will
1178
+ * be reattempted during the next SETUP stage.
1179
+ */
1180
+ if (dep->flags & DWC3_EP_DELAY_STOP)
1181
+ mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
1182
+ dep->flags &= mask;
9401183
9411184 /* Clear out the ep descriptors for non-ep0 */
9421185 if (dep->number > 1) {
....@@ -1099,15 +1342,49 @@
10991342 return trbs_left;
11001343 }
11011344
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)
1345
+/**
1346
+ * dwc3_prepare_one_trb - setup one TRB from one request
1347
+ * @dep: endpoint for which this request is prepared
1348
+ * @req: dwc3_request pointer
1349
+ * @trb_length: buffer size of the TRB
1350
+ * @chain: should this TRB be chained to the next?
1351
+ * @node: only for isochronous endpoints. First TRB needs different type.
1352
+ * @use_bounce_buffer: set to use bounce buffer
1353
+ * @must_interrupt: set to interrupt on TRB completion
1354
+ */
1355
+static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1356
+ struct dwc3_request *req, unsigned int trb_length,
1357
+ unsigned int chain, unsigned int node, bool use_bounce_buffer,
1358
+ bool must_interrupt)
11051359 {
1360
+ struct dwc3_trb *trb;
1361
+ dma_addr_t dma;
1362
+ unsigned int stream_id = req->request.stream_id;
1363
+ unsigned int short_not_ok = req->request.short_not_ok;
1364
+ unsigned int no_interrupt = req->request.no_interrupt;
1365
+ unsigned int is_last = req->request.is_last;
11061366 struct dwc3 *dwc = dep->dwc;
1107
- struct usb_gadget *gadget = &dwc->gadget;
1367
+ struct usb_gadget *gadget = dwc->gadget;
11081368 enum usb_device_speed speed = gadget->speed;
11091369
1110
- trb->size = DWC3_TRB_SIZE_LENGTH(length);
1370
+ if (use_bounce_buffer)
1371
+ dma = dep->dwc->bounce_addr;
1372
+ else if (req->request.num_sgs > 0)
1373
+ dma = sg_dma_address(req->start_sg);
1374
+ else
1375
+ dma = req->request.dma;
1376
+
1377
+ trb = &dep->trb_pool[dep->trb_enqueue];
1378
+
1379
+ if (!req->trb) {
1380
+ dwc3_gadget_move_started_request(req);
1381
+ req->trb = trb;
1382
+ req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1383
+ }
1384
+
1385
+ req->num_trbs++;
1386
+
1387
+ trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
11111388 trb->bpl = lower_32_bits(dma);
11121389 trb->bph = upper_32_bits(dma);
11131390
....@@ -1147,10 +1424,10 @@
11471424 unsigned int mult = 2;
11481425 unsigned int maxp = usb_endpoint_maxp(ep->desc);
11491426
1150
- if (length <= (2 * maxp))
1427
+ if (req->request.length <= (2 * maxp))
11511428 mult--;
11521429
1153
- if (length <= maxp)
1430
+ if (req->request.length <= maxp)
11541431 mult--;
11551432
11561433 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
....@@ -1159,8 +1436,8 @@
11591436 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
11601437 }
11611438
1162
- /* always enable Interrupt on Missed ISOC */
1163
- trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1439
+ if (!no_interrupt && !chain)
1440
+ trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
11641441 break;
11651442
11661443 case USB_ENDPOINT_XFER_BULK:
....@@ -1188,12 +1465,13 @@
11881465 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
11891466 }
11901467
1191
- if ((!no_interrupt && !chain) ||
1192
- (dwc3_calc_trbs_left(dep) == 1))
1468
+ if ((!no_interrupt && !chain) || must_interrupt)
11931469 trb->ctrl |= DWC3_TRB_CTRL_IOC;
11941470
11951471 if (chain)
11961472 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1473
+ else if (dep->stream_capable && is_last)
1474
+ trb->ctrl |= DWC3_TRB_CTRL_LST;
11971475
11981476 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
11991477 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
....@@ -1218,54 +1496,69 @@
12181496 trace_dwc3_prepare_trb(dep, trb);
12191497 }
12201498
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)
1499
+static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
12321500 {
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;
1501
+ unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1502
+ unsigned int rem = req->request.length % maxp;
12381503
1239
- if (req->request.num_sgs > 0)
1240
- dma = sg_dma_address(req->start_sg);
1241
- else
1242
- dma = req->request.dma;
1504
+ if ((req->request.length && req->request.zero && !rem &&
1505
+ !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1506
+ (!req->direction && rem))
1507
+ return true;
12431508
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);
1509
+ return false;
12561510 }
12571511
1258
-static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1512
+/**
1513
+ * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1514
+ * @dep: The endpoint that the request belongs to
1515
+ * @req: The request to prepare
1516
+ * @entry_length: The last SG entry size
1517
+ * @node: Indicates whether this is not the first entry (for isoc only)
1518
+ *
1519
+ * Return the number of TRBs prepared.
1520
+ */
1521
+static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1522
+ struct dwc3_request *req, unsigned int entry_length,
1523
+ unsigned int node)
1524
+{
1525
+ unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1526
+ unsigned int rem = req->request.length % maxp;
1527
+ unsigned int num_trbs = 1;
1528
+
1529
+ if (dwc3_needs_extra_trb(dep, req))
1530
+ num_trbs++;
1531
+
1532
+ if (dwc3_calc_trbs_left(dep) < num_trbs)
1533
+ return 0;
1534
+
1535
+ req->needs_extra_trb = num_trbs > 1;
1536
+
1537
+ /* Prepare a normal TRB */
1538
+ if (req->direction || req->request.length)
1539
+ dwc3_prepare_one_trb(dep, req, entry_length,
1540
+ req->needs_extra_trb, node, false, false);
1541
+
1542
+ /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1543
+ if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1544
+ dwc3_prepare_one_trb(dep, req,
1545
+ req->direction ? 0 : maxp - rem,
1546
+ false, 1, true, false);
1547
+
1548
+ return num_trbs;
1549
+}
1550
+
1551
+static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
12591552 struct dwc3_request *req)
12601553 {
12611554 struct scatterlist *sg = req->start_sg;
12621555 struct scatterlist *s;
12631556 int i;
12641557 unsigned int length = req->request.length;
1265
- unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1266
- unsigned int rem = length % maxp;
12671558 unsigned int remaining = req->request.num_mapped_sgs
12681559 - req->num_queued_sgs;
1560
+ unsigned int num_trbs = req->num_trbs;
1561
+ bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
12691562
12701563 /*
12711564 * If we resume preparing the request, then get the remaining length of
....@@ -1275,8 +1568,10 @@
12751568 length -= sg_dma_len(s);
12761569
12771570 for_each_sg(sg, s, remaining, i) {
1571
+ unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
12781572 unsigned int trb_length;
1279
- unsigned chain = true;
1573
+ bool must_interrupt = false;
1574
+ bool last_sg = false;
12801575
12811576 trb_length = min_t(unsigned int, length, sg_dma_len(s));
12821577
....@@ -1290,56 +1585,28 @@
12901585 * mapped sg.
12911586 */
12921587 if ((i == remaining - 1) || !length)
1293
- chain = false;
1588
+ last_sg = true;
12941589
1295
- if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1296
- struct dwc3 *dwc = dep->dwc;
1297
- struct dwc3_trb *trb;
1590
+ if (!num_trbs_left)
1591
+ break;
12981592
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
- }
1593
+ if (last_sg) {
1594
+ if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1595
+ break;
13411596 } else {
1342
- dwc3_prepare_one_trb(dep, req, trb_length, chain, i);
1597
+ /*
1598
+ * Look ahead to check if we have enough TRBs for the
1599
+ * next SG entry. If not, set interrupt on this TRB to
1600
+ * resume preparing the next SG entry when more TRBs are
1601
+ * free.
1602
+ */
1603
+ if (num_trbs_left == 1 || (needs_extra_trb &&
1604
+ num_trbs_left <= 2 &&
1605
+ sg_dma_len(sg_next(s)) >= length))
1606
+ must_interrupt = true;
1607
+
1608
+ dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1609
+ must_interrupt);
13431610 }
13441611
13451612 /*
....@@ -1349,7 +1616,7 @@
13491616 * we have free trbs we can continue queuing from where we
13501617 * previously stopped
13511618 */
1352
- if (chain)
1619
+ if (!last_sg)
13531620 req->start_sg = sg_next(s);
13541621
13551622 req->num_queued_sgs++;
....@@ -1365,65 +1632,17 @@
13651632 break;
13661633 }
13671634
1368
- if (!dwc3_calc_trbs_left(dep))
1635
+ if (must_interrupt)
13691636 break;
13701637 }
1638
+
1639
+ return req->num_trbs - num_trbs;
13711640 }
13721641
1373
-static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1642
+static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
13741643 struct dwc3_request *req)
13751644 {
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
- }
1645
+ return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
14271646 }
14281647
14291648 /*
....@@ -1433,10 +1652,13 @@
14331652 * The function goes through the requests list and sets up TRBs for the
14341653 * transfers. The function returns once there are no more TRBs available or
14351654 * it runs out of requests.
1655
+ *
1656
+ * Returns the number of TRBs prepared or negative errno.
14361657 */
1437
-static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1658
+static int dwc3_prepare_trbs(struct dwc3_ep *dep)
14381659 {
14391660 struct dwc3_request *req, *n;
1661
+ int ret = 0;
14401662
14411663 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
14421664
....@@ -1451,35 +1673,58 @@
14511673 * break things.
14521674 */
14531675 list_for_each_entry(req, &dep->started_list, list) {
1454
- if (req->num_pending_sgs > 0)
1455
- dwc3_prepare_one_trb_sg(dep, req);
1676
+ if (req->num_pending_sgs > 0) {
1677
+ ret = dwc3_prepare_trbs_sg(dep, req);
1678
+ if (!ret || req->num_pending_sgs)
1679
+ return ret;
1680
+ }
14561681
14571682 if (!dwc3_calc_trbs_left(dep))
1458
- return;
1683
+ return ret;
1684
+
1685
+ /*
1686
+ * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1687
+ * burst capability may try to read and use TRBs beyond the
1688
+ * active transfer instead of stopping.
1689
+ */
1690
+ if (dep->stream_capable && req->request.is_last)
1691
+ return ret;
14591692 }
14601693
14611694 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
14621695 struct dwc3 *dwc = dep->dwc;
1463
- int ret;
14641696
14651697 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
14661698 dep->direction);
14671699 if (ret)
1468
- return;
1700
+ return ret;
14691701
14701702 req->sg = req->request.sg;
14711703 req->start_sg = req->sg;
14721704 req->num_queued_sgs = 0;
14731705 req->num_pending_sgs = req->request.num_mapped_sgs;
14741706
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);
1707
+ if (req->num_pending_sgs > 0) {
1708
+ ret = dwc3_prepare_trbs_sg(dep, req);
1709
+ if (req->num_pending_sgs)
1710
+ return ret;
1711
+ } else {
1712
+ ret = dwc3_prepare_trbs_linear(dep, req);
1713
+ }
14791714
1480
- if (!dwc3_calc_trbs_left(dep))
1481
- return;
1715
+ if (!ret || !dwc3_calc_trbs_left(dep))
1716
+ return ret;
1717
+
1718
+ /*
1719
+ * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1720
+ * burst capability may try to read and use TRBs beyond the
1721
+ * active transfer instead of stopping.
1722
+ */
1723
+ if (dep->stream_capable && req->request.is_last)
1724
+ return ret;
14821725 }
1726
+
1727
+ return ret;
14831728 }
14841729
14851730 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
....@@ -1492,12 +1737,24 @@
14921737 int ret;
14931738 u32 cmd;
14941739
1495
- if (!dwc3_calc_trbs_left(dep))
1496
- return 0;
1740
+ /*
1741
+ * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1742
+ * This happens when we need to stop and restart a transfer such as in
1743
+ * the case of reinitiating a stream or retrying an isoc transfer.
1744
+ */
1745
+ ret = dwc3_prepare_trbs(dep);
1746
+ if (ret < 0)
1747
+ return ret;
14971748
14981749 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
14991750
1500
- dwc3_prepare_trbs(dep);
1751
+ /*
1752
+ * If there's no new TRB prepared and we don't need to restart a
1753
+ * transfer, there's no need to update the transfer.
1754
+ */
1755
+ if (!ret && !starting)
1756
+ return ret;
1757
+
15011758 req = next_request(&dep->started_list);
15021759 if (!req) {
15031760 dep->flags |= DWC3_EP_PENDING_REQUEST;
....@@ -1525,22 +1782,13 @@
15251782 if (ret < 0) {
15261783 struct dwc3_request *tmp;
15271784
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
-
15371785 if (ret == -EAGAIN)
15381786 return ret;
15391787
15401788 dwc3_stop_active_transfer(dep, true, true);
15411789
15421790 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1543
- dwc3_gadget_move_cancelled_request(req);
1791
+ dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
15441792
15451793 /* If ep isn't started, then there's no end transfer pending */
15461794 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
....@@ -1548,6 +1796,9 @@
15481796
15491797 return ret;
15501798 }
1799
+
1800
+ if (dep->stream_capable && req->request.is_last)
1801
+ dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
15511802
15521803 return 0;
15531804 }
....@@ -1558,6 +1809,55 @@
15581809
15591810 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
15601811 return DWC3_DSTS_SOFFN(reg);
1812
+}
1813
+
1814
+/**
1815
+ * __dwc3_stop_active_transfer - stop the current active transfer
1816
+ * @dep: isoc endpoint
1817
+ * @force: set forcerm bit in the command
1818
+ * @interrupt: command complete interrupt after End Transfer command
1819
+ *
1820
+ * When setting force, the ForceRM bit will be set. In that case
1821
+ * the controller won't update the TRB progress on command
1822
+ * completion. It also won't clear the HWO bit in the TRB.
1823
+ * The command will also not complete immediately in that case.
1824
+ */
1825
+static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
1826
+{
1827
+ struct dwc3 *dwc = dep->dwc;
1828
+ struct dwc3_gadget_ep_cmd_params params;
1829
+ u32 cmd;
1830
+ int ret;
1831
+
1832
+ cmd = DWC3_DEPCMD_ENDTRANSFER;
1833
+ cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1834
+ cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
1835
+ cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1836
+ memset(&params, 0, sizeof(params));
1837
+ ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1838
+ /*
1839
+ * If the End Transfer command was timed out while the device is
1840
+ * not in SETUP phase, it's possible that an incoming Setup packet
1841
+ * may prevent the command's completion. Let's retry when the
1842
+ * ep0state returns to EP0_SETUP_PHASE.
1843
+ */
1844
+ if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) {
1845
+ dep->flags |= DWC3_EP_DELAY_STOP;
1846
+ return 0;
1847
+ }
1848
+ WARN_ON_ONCE(ret);
1849
+ dep->resource_index = 0;
1850
+
1851
+ if (!interrupt) {
1852
+ if (!DWC3_IP_IS(DWC3) || DWC3_VER_IS_PRIOR(DWC3, 310A))
1853
+ mdelay(1);
1854
+ dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1855
+ } else {
1856
+ dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1857
+ }
1858
+
1859
+ dep->flags &= ~DWC3_EP_DELAY_STOP;
1860
+ return ret;
15611861 }
15621862
15631863 /**
....@@ -1617,7 +1917,7 @@
16171917 * Check if we can start isoc transfer on the next interval or
16181918 * 4 uframes in the future with BIT[15:14] as dep->combo_num
16191919 */
1620
- test_frame_number = dep->frame_number & 0x3fff;
1920
+ test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
16211921 test_frame_number |= dep->combo_num << 14;
16221922 test_frame_number += max_t(u32, 4, dep->interval);
16231923
....@@ -1664,7 +1964,7 @@
16641964 else if (test0 && test1)
16651965 dep->combo_num = 0;
16661966
1667
- dep->frame_number &= 0x3fff;
1967
+ dep->frame_number &= DWC3_FRNUMBER_MASK;
16681968 dep->frame_number |= dep->combo_num << 14;
16691969 dep->frame_number += max_t(u32, 4, dep->interval);
16701970
....@@ -1682,26 +1982,24 @@
16821982 int ret;
16831983 int i;
16841984
1685
- if (list_empty(&dep->pending_list)) {
1985
+ if (list_empty(&dep->pending_list) &&
1986
+ list_empty(&dep->started_list)) {
16861987 dep->flags |= DWC3_EP_PENDING_REQUEST;
16871988 return -EAGAIN;
16881989 }
16891990
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)
1991
+ if (!dwc->dis_start_transfer_quirk &&
1992
+ (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1993
+ DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1994
+ if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
16971995 return dwc3_gadget_start_isoc_quirk(dep);
16981996 }
16991997
17001998 if (desc->bInterval <= 14 &&
1701
- dwc->gadget.speed >= USB_SPEED_HIGH) {
1999
+ dwc->gadget->speed >= USB_SPEED_HIGH) {
17022000 u32 frame = __dwc3_gadget_get_frame(dwc);
17032001 bool rollover = frame <
1704
- (dep->frame_number & 0x3fff);
2002
+ (dep->frame_number & DWC3_FRNUMBER_MASK);
17052003
17062004 /*
17072005 * frame_number is set from XferNotReady and may be already
....@@ -1712,54 +2010,46 @@
17122010 * rollover has happened since XferNotReady.
17132011 */
17142012
1715
- dep->frame_number = (dep->frame_number & ~0x3fff) |
2013
+ dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
17162014 frame;
17172015 if (rollover)
17182016 dep->frame_number += BIT(14);
17192017 }
17202018
17212019 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1722
- dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
2020
+ int future_interval = i + 1;
2021
+
2022
+ /* Give the controller at least 500us to schedule transfers */
2023
+ if (desc->bInterval < 3)
2024
+ future_interval += 3 - desc->bInterval;
2025
+
2026
+ dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval);
17232027
17242028 ret = __dwc3_gadget_kick_transfer(dep);
17252029 if (ret != -EAGAIN)
17262030 break;
17272031 }
17282032
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
-
17362033 /*
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.
2034
+ * After a number of unsuccessful start attempts due to bus-expiry
2035
+ * status, issue END_TRANSFER command and retry on the next XferNotReady
2036
+ * event.
17452037 */
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);
2038
+ if (ret == -EAGAIN) {
2039
+ ret = __dwc3_stop_active_transfer(dep, false, true);
2040
+ if (ret)
2041
+ dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
17522042 }
17532043
1754
- req->num_trbs = 0;
2044
+ return ret;
17552045 }
17562046
17572047 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
17582048 {
17592049 struct dwc3 *dwc = dep->dwc;
17602050
1761
- if (!dep->endpoint.desc) {
1762
- dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
2051
+ if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
2052
+ dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
17632053 dep->name);
17642054 return -ESHUTDOWN;
17652055 }
....@@ -1770,14 +2060,8 @@
17702060
17712061 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
17722062 "%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
- }
2063
+ dep->name, &req->request))
17792064 return -EINVAL;
1780
- }
17812065
17822066 pm_runtime_get(dwc->dev);
17832067
....@@ -1789,8 +2073,17 @@
17892073 list_add_tail(&req->list, &dep->pending_list);
17902074 req->status = DWC3_REQUEST_STATUS_QUEUED;
17912075
1792
- /* Start the transfer only after the END_TRANSFER is completed */
1793
- if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
2076
+ if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
2077
+ return 0;
2078
+
2079
+ /*
2080
+ * Start the transfer only after the END_TRANSFER is completed
2081
+ * and endpoint STALL is cleared.
2082
+ */
2083
+ if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2084
+ (dep->flags & DWC3_EP_WEDGE) ||
2085
+ (dep->flags & DWC3_EP_DELAY_STOP) ||
2086
+ (dep->flags & DWC3_EP_STALL)) {
17942087 dep->flags |= DWC3_EP_DELAY_START;
17952088 return 0;
17962089 }
....@@ -1804,14 +2097,11 @@
18042097 * errors which will force us issue EndTransfer command.
18052098 */
18062099 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)) {
2100
+ if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2101
+ if ((dep->flags & DWC3_EP_PENDING_REQUEST))
18132102 return __dwc3_gadget_start_isoc(dep);
1814
- }
2103
+
2104
+ return 0;
18152105 }
18162106 }
18172107
....@@ -1838,14 +2128,64 @@
18382128 return ret;
18392129 }
18402130
2131
+static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
2132
+{
2133
+ int i;
2134
+
2135
+ /* If req->trb is not set, then the request has not started */
2136
+ if (!req->trb)
2137
+ return;
2138
+
2139
+ /*
2140
+ * If request was already started, this means we had to
2141
+ * stop the transfer. With that we also need to ignore
2142
+ * all TRBs used by the request, however TRBs can only
2143
+ * be modified after completion of END_TRANSFER
2144
+ * command. So what we do here is that we wait for
2145
+ * END_TRANSFER completion and only after that, we jump
2146
+ * over TRBs by clearing HWO and incrementing dequeue
2147
+ * pointer.
2148
+ */
2149
+ for (i = 0; i < req->num_trbs; i++) {
2150
+ struct dwc3_trb *trb;
2151
+
2152
+ trb = &dep->trb_pool[dep->trb_dequeue];
2153
+ trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2154
+ dwc3_ep_inc_deq(dep);
2155
+ }
2156
+
2157
+ req->num_trbs = 0;
2158
+}
2159
+
18412160 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
18422161 {
18432162 struct dwc3_request *req;
1844
- struct dwc3_request *tmp;
2163
+ struct dwc3 *dwc = dep->dwc;
18452164
1846
- list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
2165
+ while (!list_empty(&dep->cancelled_list)) {
2166
+ req = next_request(&dep->cancelled_list);
18472167 dwc3_gadget_ep_skip_trbs(dep, req);
1848
- dwc3_gadget_giveback(dep, req, -ECONNRESET);
2168
+ switch (req->status) {
2169
+ case DWC3_REQUEST_STATUS_DISCONNECTED:
2170
+ dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
2171
+ break;
2172
+ case DWC3_REQUEST_STATUS_DEQUEUED:
2173
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2174
+ break;
2175
+ case DWC3_REQUEST_STATUS_STALLED:
2176
+ dwc3_gadget_giveback(dep, req, -EPIPE);
2177
+ break;
2178
+ default:
2179
+ dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
2180
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2181
+ break;
2182
+ }
2183
+ /*
2184
+ * The endpoint is disabled, let the dwc3_remove_requests()
2185
+ * handle the cleanup.
2186
+ */
2187
+ if (!dep->endpoint.desc)
2188
+ break;
18492189 }
18502190 }
18512191
....@@ -1865,40 +2205,45 @@
18652205
18662206 spin_lock_irqsave(&dwc->lock, flags);
18672207
1868
- list_for_each_entry(r, &dep->pending_list, list) {
2208
+ list_for_each_entry(r, &dep->cancelled_list, list) {
18692209 if (r == req)
1870
- break;
2210
+ goto out;
18712211 }
18722212
1873
- if (r != req) {
1874
- list_for_each_entry(r, &dep->started_list, list) {
1875
- if (r == req)
1876
- break;
2213
+ list_for_each_entry(r, &dep->pending_list, list) {
2214
+ if (r == req) {
2215
+ dwc3_gadget_ep_skip_trbs(dep, req);
2216
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2217
+ goto out;
18772218 }
2219
+ }
2220
+
2221
+ list_for_each_entry(r, &dep->started_list, list) {
18782222 if (r == req) {
18792223 /* wait until it is processed */
18802224 dwc3_stop_active_transfer(dep, true, true);
18812225
1882
- if (!r->trb)
1883
- goto out0;
2226
+ /*
2227
+ * Remove any started request if the transfer is
2228
+ * cancelled.
2229
+ */
2230
+ dwc3_gadget_move_cancelled_request(r, DWC3_REQUEST_STATUS_DEQUEUED);
18842231
1885
- dwc3_gadget_move_cancelled_request(req);
1886
- if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1887
- goto out0;
1888
- else
1889
- goto out1;
2232
+ dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
2233
+
2234
+ if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2235
+ dwc3_gadget_ep_skip_trbs(dep, req);
2236
+ dwc3_gadget_giveback(dep, req, -ECONNRESET);
2237
+ }
2238
+
2239
+ goto out;
18902240 }
1891
- dev_err(dwc->dev, "request %pK was not queued to %s\n",
1892
- request, ep->name);
1893
- ret = -EINVAL;
1894
- goto out0;
18952241 }
18962242
1897
-out1:
1898
- dwc3_gadget_ep_skip_trbs(dep, req);
1899
- dwc3_gadget_giveback(dep, req, -ECONNRESET);
1900
-
1901
-out0:
2243
+ dev_err(dwc->dev, "request %pK was not queued to %s\n",
2244
+ request, ep->name);
2245
+ ret = -EINVAL;
2246
+out:
19022247 spin_unlock_irqrestore(&dwc->lock, flags);
19032248
19042249 return ret;
....@@ -1909,6 +2254,7 @@
19092254 struct dwc3_gadget_ep_cmd_params params;
19102255 struct dwc3 *dwc = dep->dwc;
19112256 int ret;
2257
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
19122258
19132259 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
19142260 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
....@@ -1920,8 +2266,8 @@
19202266 if (value) {
19212267 struct dwc3_trb *trb;
19222268
1923
- unsigned transfer_in_flight;
1924
- unsigned started;
2269
+ unsigned int transfer_in_flight;
2270
+ unsigned int started;
19252271
19262272 if (dep->number > 1)
19272273 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
....@@ -1944,13 +2290,44 @@
19442290 else
19452291 dep->flags |= DWC3_EP_STALL;
19462292 } else {
2293
+ /*
2294
+ * Don't issue CLEAR_STALL command to control endpoints. The
2295
+ * controller automatically clears the STALL when it receives
2296
+ * the SETUP token.
2297
+ */
2298
+ if (dep->number <= 1) {
2299
+ dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2300
+ return 0;
2301
+ }
2302
+
2303
+ dwc3_stop_active_transfer(dep, true, true);
2304
+
2305
+ if (!list_empty(&dep->started_list))
2306
+ dep->flags |= DWC3_EP_DELAY_START;
2307
+
2308
+ if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
2309
+ (dep->flags & DWC3_EP_DELAY_STOP)) {
2310
+ dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
2311
+ if (protocol)
2312
+ vdwc->clear_stall_protocol = dep->number;
2313
+
2314
+ return 0;
2315
+ }
19472316
19482317 ret = dwc3_send_clear_stall_ep_cmd(dep);
1949
- if (ret)
2318
+ if (ret) {
19502319 dev_err(dwc->dev, "failed to clear STALL on %s\n",
19512320 dep->name);
1952
- else
1953
- dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2321
+ return ret;
2322
+ }
2323
+
2324
+ dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2325
+
2326
+ if ((dep->flags & DWC3_EP_DELAY_START) &&
2327
+ !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2328
+ __dwc3_gadget_kick_transfer(dep);
2329
+
2330
+ dep->flags &= ~DWC3_EP_DELAY_START;
19542331 }
19552332
19562333 return ret;
....@@ -2050,7 +2427,6 @@
20502427 link_state = DWC3_DSTS_USBLNKST(reg);
20512428
20522429 switch (link_state) {
2053
- case DWC3_LINK_STATE_U0:
20542430 case DWC3_LINK_STATE_RESET:
20552431 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
20562432 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
....@@ -2062,15 +2438,6 @@
20622438 return -EINVAL;
20632439 }
20642440
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
-
20742441 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
20752442 if (ret < 0) {
20762443 dev_err(dwc->dev, "failed to put link in Recovery\n");
....@@ -2078,7 +2445,7 @@
20782445 }
20792446
20802447 /* Recent versions do this automatically */
2081
- if (dwc->revision < DWC3_REVISION_194A) {
2448
+ if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
20822449 /* write zeroes to Link Change Request */
20832450 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
20842451 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
....@@ -2130,28 +2497,140 @@
21302497 return 0;
21312498 }
21322499
2500
+static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2501
+{
2502
+ u32 epnum;
2503
+
2504
+ for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2505
+ struct dwc3_ep *dep;
2506
+
2507
+ dep = dwc->eps[epnum];
2508
+ if (!dep)
2509
+ continue;
2510
+
2511
+ dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
2512
+ }
2513
+}
2514
+
2515
+static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2516
+{
2517
+ enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2518
+ u32 reg;
2519
+
2520
+ if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2521
+ ssp_rate = dwc->max_ssp_rate;
2522
+
2523
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2524
+ reg &= ~DWC3_DCFG_SPEED_MASK;
2525
+ reg &= ~DWC3_DCFG_NUMLANES(~0);
2526
+
2527
+ if (ssp_rate == USB_SSP_GEN_1x2)
2528
+ reg |= DWC3_DCFG_SUPERSPEED;
2529
+ else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2530
+ reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2531
+
2532
+ if (ssp_rate != USB_SSP_GEN_2x1 &&
2533
+ dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2534
+ reg |= DWC3_DCFG_NUMLANES(1);
2535
+
2536
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2537
+}
2538
+
2539
+static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2540
+{
2541
+ enum usb_device_speed speed;
2542
+ u32 reg;
2543
+
2544
+ speed = dwc->gadget_max_speed;
2545
+ if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
2546
+ speed = dwc->maximum_speed;
2547
+
2548
+ if (speed == USB_SPEED_SUPER_PLUS &&
2549
+ DWC3_IP_IS(DWC32)) {
2550
+ __dwc3_gadget_set_ssp_rate(dwc);
2551
+ return;
2552
+ }
2553
+
2554
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2555
+ reg &= ~(DWC3_DCFG_SPEED_MASK);
2556
+
2557
+ /*
2558
+ * WORKAROUND: DWC3 revision < 2.20a have an issue
2559
+ * which would cause metastability state on Run/Stop
2560
+ * bit if we try to force the IP to USB2-only mode.
2561
+ *
2562
+ * Because of that, we cannot configure the IP to any
2563
+ * speed other than the SuperSpeed
2564
+ *
2565
+ * Refers to:
2566
+ *
2567
+ * STAR#9000525659: Clock Domain Crossing on DCTL in
2568
+ * USB 2.0 Mode
2569
+ */
2570
+ if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2571
+ !dwc->dis_metastability_quirk) {
2572
+ reg |= DWC3_DCFG_SUPERSPEED;
2573
+ } else {
2574
+ switch (speed) {
2575
+ case USB_SPEED_LOW:
2576
+ reg |= DWC3_DCFG_LOWSPEED;
2577
+ break;
2578
+ case USB_SPEED_FULL:
2579
+ reg |= DWC3_DCFG_FULLSPEED;
2580
+ break;
2581
+ case USB_SPEED_HIGH:
2582
+ reg |= DWC3_DCFG_HIGHSPEED;
2583
+ break;
2584
+ case USB_SPEED_SUPER:
2585
+ reg |= DWC3_DCFG_SUPERSPEED;
2586
+ break;
2587
+ case USB_SPEED_SUPER_PLUS:
2588
+ if (DWC3_IP_IS(DWC3))
2589
+ reg |= DWC3_DCFG_SUPERSPEED;
2590
+ else
2591
+ reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2592
+ break;
2593
+ default:
2594
+ dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2595
+
2596
+ if (DWC3_IP_IS(DWC3))
2597
+ reg |= DWC3_DCFG_SUPERSPEED;
2598
+ else
2599
+ reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2600
+ }
2601
+ }
2602
+
2603
+ if (DWC3_IP_IS(DWC32) &&
2604
+ speed > USB_SPEED_UNKNOWN &&
2605
+ speed < USB_SPEED_SUPER_PLUS)
2606
+ reg &= ~DWC3_DCFG_NUMLANES(~0);
2607
+
2608
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2609
+}
2610
+
21332611 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
21342612 {
21352613 u32 reg;
2136
- u32 timeout = 500;
2614
+ u32 timeout = 2000;
21372615
21382616 if (pm_runtime_suspended(dwc->dev))
21392617 return 0;
21402618
21412619 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
21422620 if (is_on) {
2143
- if (dwc->revision <= DWC3_REVISION_187A) {
2621
+ if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
21442622 reg &= ~DWC3_DCTL_TRGTULST_MASK;
21452623 reg |= DWC3_DCTL_TRGTULST_RX_DET;
21462624 }
21472625
2148
- if (dwc->revision >= DWC3_REVISION_194A)
2626
+ if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
21492627 reg &= ~DWC3_DCTL_KEEP_CONNECT;
21502628 reg |= DWC3_DCTL_RUN_STOP;
21512629
21522630 if (dwc->has_hibernation)
21532631 reg |= DWC3_DCTL_KEEP_CONNECT;
21542632
2633
+ __dwc3_gadget_set_speed(dwc);
21552634 dwc->pullups_connected = true;
21562635 } else {
21572636 reg &= ~DWC3_DCTL_RUN_STOP;
....@@ -2162,9 +2641,10 @@
21622641 dwc->pullups_connected = false;
21632642 }
21642643
2165
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2644
+ dwc3_gadget_dctl_write_safe(dwc, reg);
21662645
21672646 do {
2647
+ usleep_range(1000, 2000);
21682648 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
21692649 reg &= DWC3_DSTS_DEVCTRLHLT;
21702650 } while (--timeout && !(!is_on ^ !reg));
....@@ -2175,42 +2655,156 @@
21752655 return 0;
21762656 }
21772657
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;
2658
+static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2659
+static void __dwc3_gadget_stop(struct dwc3 *dwc);
2660
+static int __dwc3_gadget_start(struct dwc3 *dwc);
21832661
2184
- is_on = !!is_on;
2662
+static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
2663
+{
2664
+ unsigned long flags;
2665
+ int ret;
2666
+
2667
+ spin_lock_irqsave(&dwc->lock, flags);
2668
+ dwc->connected = false;
2669
+
2670
+ /*
2671
+ * Attempt to end pending SETUP status phase, and not wait for the
2672
+ * function to do so.
2673
+ */
2674
+ if (dwc->delayed_status)
2675
+ dwc3_ep0_send_delayed_status(dwc);
2676
+
2677
+ /*
2678
+ * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
2679
+ * Section 4.1.8 Table 4-7, it states that for a device-initiated
2680
+ * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2681
+ * command for any active transfers" before clearing the RunStop
2682
+ * bit.
2683
+ */
2684
+ dwc3_stop_active_transfers(dwc);
2685
+ spin_unlock_irqrestore(&dwc->lock, flags);
21852686
21862687 /*
21872688 * Per databook, when we want to stop the gadget, if a control transfer
21882689 * is still in process, complete it and get the core into setup phase.
2690
+ * In case the host is unresponsive to a SETUP transaction, forcefully
2691
+ * stall the transfer, and move back to the SETUP phase, so that any
2692
+ * pending endxfers can be executed.
21892693 */
2190
- if (!is_on && dwc->ep0state != EP0_SETUP_PHASE &&
2694
+ if (dwc->ep0state != EP0_SETUP_PHASE &&
21912695 dwc->ep0state != EP0_UNCONNECTED) {
21922696 reinit_completion(&dwc->ep0_in_setup);
21932697
21942698 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
21952699 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2196
- if (ret == 0)
2197
- dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
2700
+ if (ret == 0) {
2701
+ unsigned int dir;
2702
+
2703
+ dev_warn(dwc->dev, "wait for SETUP phase timed out\n");
2704
+ spin_lock_irqsave(&dwc->lock, flags);
2705
+ dir = !!dwc->ep0_expect_in;
2706
+ if (dwc->ep0state == EP0_DATA_PHASE)
2707
+ dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
2708
+ else
2709
+ dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
2710
+ dwc3_ep0_stall_and_restart(dwc);
2711
+ spin_unlock_irqrestore(&dwc->lock, flags);
2712
+ }
21982713 }
21992714
2715
+ /*
2716
+ * Note: if the GEVNTCOUNT indicates events in the event buffer, the
2717
+ * driver needs to acknowledge them before the controller can halt.
2718
+ * Simply let the interrupt handler acknowledges and handle the
2719
+ * remaining event generated by the controller while polling for
2720
+ * DSTS.DEVCTLHLT.
2721
+ */
2722
+ ret = dwc3_gadget_run_stop(dwc, false, false);
2723
+
2724
+ /*
2725
+ * Stop the gadget after controller is halted, so that if needed, the
2726
+ * events to update EP0 state can still occur while the run/stop
2727
+ * routine polls for the halted state. DEVTEN is cleared as part of
2728
+ * gadget stop.
2729
+ */
22002730 spin_lock_irqsave(&dwc->lock, flags);
2201
- ret = dwc3_gadget_run_stop(dwc, is_on, false);
2731
+ __dwc3_gadget_stop(dwc);
22022732 spin_unlock_irqrestore(&dwc->lock, flags);
22032733
22042734 return ret;
22052735 }
22062736
2207
-void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2737
+static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2738
+{
2739
+ struct dwc3 *dwc = gadget_to_dwc(g);
2740
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
2741
+ int ret;
2742
+
2743
+ is_on = !!is_on;
2744
+
2745
+ vdwc->softconnect = is_on;
2746
+
2747
+ /*
2748
+ * Avoid issuing a runtime resume if the device is already in the
2749
+ * suspended state during gadget disconnect. DWC3 gadget was already
2750
+ * halted/stopped during runtime suspend.
2751
+ */
2752
+ if (!is_on) {
2753
+ pm_runtime_barrier(dwc->dev);
2754
+ if (pm_runtime_suspended(dwc->dev))
2755
+ return 0;
2756
+ }
2757
+
2758
+ /*
2759
+ * Check the return value for successful resume, or error. For a
2760
+ * successful resume, the DWC3 runtime PM resume routine will handle
2761
+ * the run stop sequence, so avoid duplicate operations here.
2762
+ */
2763
+ ret = pm_runtime_get_sync(dwc->dev);
2764
+ if (!ret || ret < 0) {
2765
+ pm_runtime_put(dwc->dev);
2766
+ if (ret < 0)
2767
+ pm_runtime_set_suspended(dwc->dev);
2768
+ return ret;
2769
+ }
2770
+
2771
+ if (dwc->pullups_connected == is_on) {
2772
+ pm_runtime_put(dwc->dev);
2773
+ return 0;
2774
+ }
2775
+
2776
+ synchronize_irq(dwc->irq_gadget);
2777
+
2778
+ if (!is_on) {
2779
+ ret = dwc3_gadget_soft_disconnect(dwc);
2780
+ } else {
2781
+ /*
2782
+ * In the Synopsys DWC_usb31 1.90a programming guide section
2783
+ * 4.1.9, it specifies that for a reconnect after a
2784
+ * device-initiated disconnect requires a core soft reset
2785
+ * (DCTL.CSftRst) before enabling the run/stop bit.
2786
+ */
2787
+ ret = dwc3_core_soft_reset(dwc);
2788
+ if (ret)
2789
+ goto done;
2790
+
2791
+ dwc3_event_buffers_setup(dwc);
2792
+ __dwc3_gadget_start(dwc);
2793
+ ret = dwc3_gadget_run_stop(dwc, true, false);
2794
+ }
2795
+
2796
+done:
2797
+ pm_runtime_put(dwc->dev);
2798
+
2799
+ return ret;
2800
+}
2801
+
2802
+static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
22082803 {
22092804 u32 reg;
22102805
22112806 /* Enable all but Start and End of Frame IRQs */
2212
- reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2213
- DWC3_DEVTEN_EVNTOVERFLOWEN |
2807
+ reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
22142808 DWC3_DEVTEN_CMDCMPLTEN |
22152809 DWC3_DEVTEN_ERRTICERREN |
22162810 DWC3_DEVTEN_WKUPEVTEN |
....@@ -2218,17 +2812,17 @@
22182812 DWC3_DEVTEN_USBRSTEN |
22192813 DWC3_DEVTEN_DISCONNEVTEN);
22202814
2221
- if (dwc->revision < DWC3_REVISION_250A)
2815
+ if (DWC3_VER_IS_PRIOR(DWC3, 250A))
22222816 reg |= DWC3_DEVTEN_ULSTCNGEN;
22232817
22242818 /* 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;
2819
+ if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2820
+ reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
22272821
22282822 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
22292823 }
22302824
2231
-void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2825
+static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
22322826 {
22332827 /* mask all interrupts */
22342828 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
....@@ -2266,7 +2860,7 @@
22662860 u32 reg;
22672861
22682862 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2269
- mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2863
+ mdwidth = dwc3_mdwidth(dwc);
22702864
22712865 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
22722866 nump = min_t(u32, nump, 16);
....@@ -2283,6 +2877,15 @@
22832877 struct dwc3_ep *dep;
22842878 int ret = 0;
22852879 u32 reg;
2880
+
2881
+ /*
2882
+ * If the DWC3 is in runtime suspend, the clocks maybe
2883
+ * disabled, so avoid enable the DWC3 endpoints here.
2884
+ * The DWC3 runtime PM resume routine will handle the
2885
+ * gadget start sequence.
2886
+ */
2887
+ if (pm_runtime_suspended(dwc->dev))
2888
+ return ret;
22862889
22872890 /*
22882891 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
....@@ -2303,19 +2906,31 @@
23032906 * bursts of data without going through any sort of endpoint throttling.
23042907 */
23052908 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2306
- if (dwc3_is_usb31(dwc))
2307
- reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2308
- else
2909
+ if (DWC3_IP_IS(DWC3))
23092910 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2911
+ else
2912
+ reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
23102913
23112914 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
23122915
23132916 dwc3_gadget_setup_nump(dwc);
23142917
2918
+ /*
2919
+ * Currently the controller handles single stream only. So, Ignore
2920
+ * Packet Pending bit for stream selection and don't search for another
2921
+ * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2922
+ * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2923
+ * the stream performance.
2924
+ */
2925
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2926
+ reg |= DWC3_DCFG_IGNSTRMPP;
2927
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2928
+
23152929 /* Start with SuperSpeed Default */
23162930 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
23172931
23182932 dep = dwc->eps[0];
2933
+ dep->flags = 0;
23192934 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
23202935 if (ret) {
23212936 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
....@@ -2323,6 +2938,7 @@
23232938 }
23242939
23252940 dep = dwc->eps[1];
2941
+ dep->flags = 0;
23262942 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
23272943 if (ret) {
23282944 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
....@@ -2331,6 +2947,7 @@
23312947
23322948 /* begin to receive SETUP packets */
23332949 dwc->ep0state = EP0_SETUP_PHASE;
2950
+ dwc->ep0_bounced = false;
23342951 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
23352952 dwc->delayed_status = false;
23362953 dwc3_ep0_out_start(dwc);
....@@ -2366,17 +2983,13 @@
23662983 spin_lock_irqsave(&dwc->lock, flags);
23672984 if (dwc->gadget_driver) {
23682985 dev_err(dwc->dev, "%s is already bound to %s\n",
2369
- dwc->gadget.name,
2986
+ dwc->gadget->name,
23702987 dwc->gadget_driver->driver.name);
23712988 ret = -EBUSY;
23722989 goto err1;
23732990 }
23742991
23752992 dwc->gadget_driver = driver;
2376
-
2377
- if (pm_runtime_active(dwc->dev))
2378
- __dwc3_gadget_start(dwc);
2379
-
23802993 spin_unlock_irqrestore(&dwc->lock, flags);
23812994
23822995 return 0;
....@@ -2402,28 +3015,59 @@
24023015 unsigned long flags;
24033016
24043017 spin_lock_irqsave(&dwc->lock, flags);
2405
-
24063018 if (!dwc->gadget_driver) {
24073019 spin_unlock_irqrestore(&dwc->lock, flags);
24083020 dev_warn(dwc->dev, "%s is already stopped\n",
2409
- dwc->gadget.name);
2410
- goto out0;
3021
+ dwc->gadget->name);
3022
+ goto out;
24113023 }
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;
24203024 dwc->gadget_driver = NULL;
3025
+ dwc->max_cfg_eps = 0;
24213026 spin_unlock_irqrestore(&dwc->lock, flags);
24223027
24233028 free_irq(dwc->irq_gadget, dwc->ev_buf);
24243029
2425
-out0:
3030
+out:
24263031 return 0;
3032
+}
3033
+
3034
+static void dwc3_gadget_config_params(struct usb_gadget *g,
3035
+ struct usb_dcd_config_params *params)
3036
+{
3037
+ struct dwc3 *dwc = gadget_to_dwc(g);
3038
+
3039
+ params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
3040
+ params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
3041
+
3042
+ /* Recommended BESL */
3043
+ if (!dwc->dis_enblslpm_quirk) {
3044
+ /*
3045
+ * If the recommended BESL baseline is 0 or if the BESL deep is
3046
+ * less than 2, Microsoft's Windows 10 host usb stack will issue
3047
+ * a usb reset immediately after it receives the extended BOS
3048
+ * descriptor and the enumeration will fail. To maintain
3049
+ * compatibility with the Windows' usb stack, let's set the
3050
+ * recommended BESL baseline to 1 and clamp the BESL deep to be
3051
+ * within 2 to 15.
3052
+ */
3053
+ params->besl_baseline = 1;
3054
+ if (dwc->is_utmi_l1_suspend)
3055
+ params->besl_deep =
3056
+ clamp_t(u8, dwc->hird_threshold, 2, 15);
3057
+ }
3058
+
3059
+ /* U1 Device exit Latency */
3060
+ if (dwc->dis_u1_entry_quirk)
3061
+ params->bU1devExitLat = 0;
3062
+ else
3063
+ params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
3064
+
3065
+ /* U2 Device exit Latency */
3066
+ if (dwc->dis_u2_entry_quirk)
3067
+ params->bU2DevExitLat = 0;
3068
+ else
3069
+ params->bU2DevExitLat =
3070
+ cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
24273071 }
24283072
24293073 static void dwc3_gadget_set_speed(struct usb_gadget *g,
....@@ -2431,67 +3075,94 @@
24313075 {
24323076 struct dwc3 *dwc = gadget_to_dwc(g);
24333077 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;
24433078
24443079 spin_lock_irqsave(&dwc->lock, flags);
2445
- reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2446
- reg &= ~(DWC3_DCFG_SPEED_MASK);
3080
+ dwc->gadget_max_speed = speed;
3081
+ spin_unlock_irqrestore(&dwc->lock, flags);
3082
+}
24473083
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);
3084
+static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
3085
+ enum usb_ssp_rate rate)
3086
+{
3087
+ struct dwc3 *dwc = gadget_to_dwc(g);
3088
+ unsigned long flags;
24863089
2487
- if (dwc->revision & DWC3_REVISION_IS_DWC31)
2488
- reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2489
- else
2490
- reg |= DWC3_DCFG_SUPERSPEED;
2491
- }
3090
+ spin_lock_irqsave(&dwc->lock, flags);
3091
+ dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
3092
+ dwc->gadget_ssp_rate = rate;
3093
+ spin_unlock_irqrestore(&dwc->lock, flags);
3094
+}
3095
+
3096
+static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
3097
+{
3098
+ struct dwc3 *dwc = gadget_to_dwc(g);
3099
+ union power_supply_propval val = {0};
3100
+ int ret;
3101
+
3102
+ if (dwc->usb2_phy)
3103
+ return usb_phy_set_power(dwc->usb2_phy, mA);
3104
+
3105
+ if (!dwc->usb_psy)
3106
+ return -EOPNOTSUPP;
3107
+
3108
+ val.intval = 1000 * mA;
3109
+ ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
3110
+
3111
+ return ret;
3112
+}
3113
+
3114
+/**
3115
+ * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
3116
+ * @g: pointer to the USB gadget
3117
+ *
3118
+ * Used to record the maximum number of endpoints being used in a USB composite
3119
+ * device. (across all configurations) This is to be used in the calculation
3120
+ * of the TXFIFO sizes when resizing internal memory for individual endpoints.
3121
+ * It will help ensured that the resizing logic reserves enough space for at
3122
+ * least one max packet.
3123
+ */
3124
+static int dwc3_gadget_check_config(struct usb_gadget *g)
3125
+{
3126
+ struct dwc3 *dwc = gadget_to_dwc(g);
3127
+ struct usb_ep *ep;
3128
+ int fifo_size = 0;
3129
+ int ram1_depth;
3130
+ int ep_num = 0;
3131
+
3132
+ if (!dwc->do_fifo_resize)
3133
+ return 0;
3134
+
3135
+ list_for_each_entry(ep, &g->ep_list, ep_list) {
3136
+ /* Only interested in the IN endpoints */
3137
+ if (ep->claimed && (ep->address & USB_DIR_IN))
3138
+ ep_num++;
24923139 }
2493
- dwc3_writel(dwc->regs, DWC3_DCFG, reg);
24943140
3141
+ if (ep_num <= dwc->max_cfg_eps)
3142
+ return 0;
3143
+
3144
+ /* Update the max number of eps in the composition */
3145
+ dwc->max_cfg_eps = ep_num;
3146
+
3147
+ fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
3148
+ /* Based on the equation, increment by one for every ep */
3149
+ fifo_size += dwc->max_cfg_eps;
3150
+
3151
+ /* Check if we can fit a single fifo per endpoint */
3152
+ ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
3153
+ if (fifo_size > ram1_depth)
3154
+ return -ENOMEM;
3155
+
3156
+ return 0;
3157
+}
3158
+
3159
+static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
3160
+{
3161
+ struct dwc3 *dwc = gadget_to_dwc(g);
3162
+ unsigned long flags;
3163
+
3164
+ spin_lock_irqsave(&dwc->lock, flags);
3165
+ dwc->async_callbacks = enable;
24953166 spin_unlock_irqrestore(&dwc->lock, flags);
24963167 }
24973168
....@@ -2503,6 +3174,11 @@
25033174 .udc_start = dwc3_gadget_start,
25043175 .udc_stop = dwc3_gadget_stop,
25053176 .udc_set_speed = dwc3_gadget_set_speed,
3177
+ .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
3178
+ .get_config_params = dwc3_gadget_config_params,
3179
+ .vbus_draw = dwc3_gadget_vbus_draw,
3180
+ .check_config = dwc3_gadget_check_config,
3181
+ .udc_async_callbacks = dwc3_gadget_async_callbacks,
25063182 };
25073183
25083184 /* -------------------------------------------------------------------------- */
....@@ -2515,7 +3191,7 @@
25153191 dep->endpoint.maxburst = 1;
25163192 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
25173193 if (!dep->direction)
2518
- dwc->gadget.ep0 = &dep->endpoint;
3194
+ dwc->gadget->ep0 = &dep->endpoint;
25193195
25203196 dep->endpoint.caps.type_control = true;
25213197
....@@ -2525,48 +3201,62 @@
25253201 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
25263202 {
25273203 struct dwc3 *dwc = dep->dwc;
2528
- int mdwidth;
3204
+ u32 mdwidth;
25293205 int size;
3206
+ int maxpacket;
25303207
2531
- mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
3208
+ mdwidth = dwc3_mdwidth(dwc);
3209
+
25323210 /* MDWIDTH is represented in bits, we need it in bytes */
25333211 mdwidth /= 8;
25343212
25353213 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2536
- if (dwc3_is_usb31(dwc))
2537
- size = DWC31_GTXFIFOSIZ_TXFDEF(size);
3214
+ if (DWC3_IP_IS(DWC3))
3215
+ size = DWC3_GTXFIFOSIZ_TXFDEP(size);
25383216 else
2539
- size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2540
-
2541
- /* FIFO Depth is in MDWDITH bytes. Multiply */
2542
- size *= mdwidth;
3217
+ size = DWC31_GTXFIFOSIZ_TXFDEP(size);
25433218
25443219 /*
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.
3220
+ * maxpacket size is determined as part of the following, after assuming
3221
+ * a mult value of one maxpacket:
3222
+ * DWC3 revision 280A and prior:
3223
+ * fifo_size = mult * (max_packet / mdwidth) + 1;
3224
+ * maxpacket = mdwidth * (fifo_size - 1);
3225
+ *
3226
+ * DWC3 revision 290A and onwards:
3227
+ * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
3228
+ * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth;
25503229 */
2551
- if (dwc->maximum_speed >= USB_SPEED_SUPER)
2552
- size /= 3;
3230
+ if (DWC3_VER_IS_PRIOR(DWC3, 290A))
3231
+ maxpacket = mdwidth * (size - 1);
25533232 else
2554
- size /= 2;
3233
+ maxpacket = mdwidth * ((size - 1) - 1) - mdwidth;
25553234
3235
+
3236
+ /*
3237
+ * To meet performance requirement, a minimum TxFIFO size of 2x
3238
+ * MaxPacketSize is recommended for endpoints that support for
3239
+ * Rockchip platform with UVC function.
3240
+ */
3241
+ if (IS_REACHABLE(CONFIG_ARCH_ROCKCHIP) &&
3242
+ (dwc->maximum_speed >= USB_SPEED_HIGH))
3243
+ maxpacket /= 2;
3244
+
3245
+ /* Functionally, space for one max packet is sufficient */
3246
+ size = min_t(int, maxpacket, 1024);
25563247 /*
25573248 * If enable tx fifos resize, set each in ep maxpacket
25583249 * to 1024, it can avoid being dependent on the default
25593250 * fifo size, and more flexible use of endpoints.
25603251 */
2561
- if (dwc->needs_fifo_resize)
3252
+ if (dwc->do_fifo_resize)
25623253 size = 1024;
2563
-
25643254 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
25653255
2566
- dep->endpoint.max_streams = 15;
3256
+ dep->endpoint.max_streams = 16;
25673257 dep->endpoint.ops = &dwc3_gadget_ep_ops;
25683258 list_add_tail(&dep->endpoint.ep_list,
2569
- &dwc->gadget.ep_list);
3259
+ &dwc->gadget->ep_list);
25703260 dep->endpoint.caps.type_iso = true;
25713261 dep->endpoint.caps.type_bulk = true;
25723262 dep->endpoint.caps.type_int = true;
....@@ -2577,20 +3267,20 @@
25773267 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
25783268 {
25793269 struct dwc3 *dwc = dep->dwc;
2580
- int mdwidth;
3270
+ u32 mdwidth;
25813271 int size;
25823272
2583
- mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
3273
+ mdwidth = dwc3_mdwidth(dwc);
25843274
25853275 /* MDWIDTH is represented in bits, convert to bytes */
25863276 mdwidth /= 8;
25873277
25883278 /* All OUT endpoints share a single RxFIFO space */
25893279 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2590
- if (dwc3_is_usb31(dwc))
2591
- size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2592
- else
3280
+ if (DWC3_IP_IS(DWC3))
25933281 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
3282
+ else
3283
+ size = DWC31_GRXFIFOSIZ_RXFDEP(size);
25943284
25953285 /* FIFO depth is in MDWDITH bytes */
25963286 size *= mdwidth;
....@@ -2610,10 +3300,10 @@
26103300 size /= 3;
26113301
26123302 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2613
- dep->endpoint.max_streams = 15;
3303
+ dep->endpoint.max_streams = 16;
26143304 dep->endpoint.ops = &dwc3_gadget_ep_ops;
26153305 list_add_tail(&dep->endpoint.ep_list,
2616
- &dwc->gadget.ep_list);
3306
+ &dwc->gadget->ep_list);
26173307 dep->endpoint.caps.type_iso = true;
26183308 dep->endpoint.caps.type_bulk = true;
26193309 dep->endpoint.caps.type_int = true;
....@@ -2627,20 +3317,20 @@
26273317 bool direction = epnum & 1;
26283318 int ret;
26293319 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;
3320
+ u8 num_in_eps, num_out_eps, min_eps;
26343321
26353322 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
26363323 if (!dep)
26373324 return -ENOMEM;
26383325
3326
+ num_in_eps = DWC3_NUM_IN_EPS(&dwc->hwparams);
3327
+ num_out_eps = dwc->num_eps - num_in_eps;
3328
+ min_eps = min_t(u8, num_in_eps, num_out_eps);
3329
+
26393330 /* 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);
3331
+ if (num + 1 > min_eps && num_in_eps != num_out_eps) {
3332
+ num = epnum - min_eps;
3333
+ direction = num + 1 > num_out_eps ? 1 : 0;
26443334 }
26453335
26463336 dep->dwc = dwc;
....@@ -2659,12 +3349,7 @@
26593349 if (!(dep->number > 1)) {
26603350 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
26613351 dep->endpoint.comp_desc = NULL;
2662
-#ifdef CONFIG_ARCH_ROCKCHIP
2663
- dep->endpoint.transfer_type = USB_ENDPOINT_XFER_CONTROL;
2664
-#endif
26653352 }
2666
-
2667
- spin_lock_init(&dep->lock);
26683353
26693354 if (num == 0)
26703355 ret = dwc3_gadget_init_control_endpoint(dep);
....@@ -2692,7 +3377,7 @@
26923377 {
26933378 u8 epnum;
26943379
2695
- INIT_LIST_HEAD(&dwc->gadget.ep_list);
3380
+ INIT_LIST_HEAD(&dwc->gadget->ep_list);
26963381
26973382 for (epnum = 0; epnum < total; epnum++) {
26983383 int ret;
....@@ -2773,12 +3458,12 @@
27733458 }
27743459
27753460 /*
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.
3461
+ * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
3462
+ * this TRB points to the bounce buffer address, it's a MPS alignment
3463
+ * TRB. Don't add it to req->remaining calculation.
27793464 */
2780
-
2781
- if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
3465
+ if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
3466
+ trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
27823467 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
27833468 return 1;
27843469 }
....@@ -2790,6 +3475,10 @@
27903475 return 1;
27913476
27923477 if (event->status & DEPEVT_STATUS_SHORT && !chain)
3478
+ return 1;
3479
+
3480
+ if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
3481
+ DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
27933482 return 1;
27943483
27953484 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
....@@ -2845,6 +3534,7 @@
28453534 struct dwc3_request *req, int status)
28463535 {
28473536 struct dwc3 *dwc = dep->dwc;
3537
+ int request_status;
28483538 int ret;
28493539
28503540 if (req->request.num_mapped_sgs)
....@@ -2856,35 +3546,26 @@
28563546
28573547 req->request.actual = req->request.length - req->remaining;
28583548
2859
- if (!dwc3_gadget_ep_request_completed(req) &&
2860
- !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3549
+ if (!dwc3_gadget_ep_request_completed(req))
28613550 goto out;
28623551
28633552 if (req->needs_extra_trb) {
2864
- unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
2865
-
28663553 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
28673554 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
-
28753555 req->needs_extra_trb = false;
28763556 }
28773557
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
- */
3558
+ /*
3559
+ * If MISS ISOC happens, we need to move the req from started_list
3560
+ * to cancelled_list, then unmap the req and clear the HWO of trb.
3561
+ * Later in the dwc3_gadget_endpoint_trbs_complete(), it will move
3562
+ * the req from the cancelled_list to the pending_list, and restart
3563
+ * the req for isoc transfer.
3564
+ */
3565
+ if (status == -EXDEV && usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
28853566 req->remaining = 0;
28863567 req->needs_extra_trb = false;
2887
- dwc3_gadget_move_cancelled_request(req);
3568
+ dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
28883569 if (req->trb) {
28893570 usb_gadget_unmap_request_by_dev(dwc->sysdev,
28903571 &req->request,
....@@ -2893,11 +3574,38 @@
28933574 req->trb = NULL;
28943575 }
28953576 ret = 0;
2896
-
28973577 goto out;
28983578 }
28993579
2900
- dwc3_gadget_giveback(dep, req, status);
3580
+ /*
3581
+ * The event status only reflects the status of the TRB with IOC set.
3582
+ * For the requests that don't set interrupt on completion, the driver
3583
+ * needs to check and return the status of the completed TRBs associated
3584
+ * with the request. Use the status of the last TRB of the request.
3585
+ */
3586
+ if (req->request.no_interrupt) {
3587
+ struct dwc3_trb *trb;
3588
+
3589
+ trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
3590
+ switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
3591
+ case DWC3_TRBSTS_MISSED_ISOC:
3592
+ /* Isoc endpoint only */
3593
+ request_status = -EXDEV;
3594
+ break;
3595
+ case DWC3_TRB_STS_XFER_IN_PROG:
3596
+ /* Applicable when End Transfer with ForceRM=0 */
3597
+ case DWC3_TRBSTS_SETUP_PENDING:
3598
+ /* Control endpoint only */
3599
+ case DWC3_TRBSTS_OK:
3600
+ default:
3601
+ request_status = 0;
3602
+ break;
3603
+ }
3604
+ } else {
3605
+ request_status = status;
3606
+ }
3607
+
3608
+ dwc3_gadget_giveback(dep, req, request_status);
29013609
29023610 out:
29033611 return ret;
....@@ -2907,14 +3615,20 @@
29073615 const struct dwc3_event_depevt *event, int status)
29083616 {
29093617 struct dwc3_request *req;
2910
- struct dwc3_request *tmp;
29113618
2912
- list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
3619
+ while (!list_empty(&dep->started_list)) {
29133620 int ret;
29143621
3622
+ req = next_request(&dep->started_list);
29153623 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
29163624 req, status);
29173625 if (ret)
3626
+ break;
3627
+ /*
3628
+ * The endpoint is disabled, let the dwc3_remove_requests()
3629
+ * handle the cleanup.
3630
+ */
3631
+ if (!dep->endpoint.desc)
29183632 break;
29193633 }
29203634 }
....@@ -2922,6 +3636,11 @@
29223636 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
29233637 {
29243638 struct dwc3_request *req;
3639
+ struct dwc3 *dwc = dep->dwc;
3640
+
3641
+ if (!dep->endpoint.desc || !dwc->pullups_connected ||
3642
+ !dwc->connected)
3643
+ return false;
29253644
29263645 if (!list_empty(&dep->pending_list))
29273646 return true;
....@@ -2943,49 +3662,58 @@
29433662 dep->frame_number = event->parameters;
29443663 }
29453664
2946
-static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2947
- const struct dwc3_event_depevt *event)
3665
+static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
3666
+ const struct dwc3_event_depevt *event, int status)
29483667 {
29493668 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;
3669
+ struct dwc3_request *req, *tmp;
3670
+ bool no_started_trb = true;
29613671
29623672 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
29633673
2964
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
3674
+ if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3675
+ goto out;
3676
+
3677
+ if (!dep->endpoint.desc)
3678
+ return no_started_trb;
3679
+
3680
+ /*
3681
+ * If MISS ISOC happens, we need to do the following three steps
3682
+ * to restart the reqs in the cancelled_list and pending_list
3683
+ * in order.
3684
+ * Step1. Move all the reqs from pending_list to the tail of
3685
+ * cancelled_list.
3686
+ * Step2. Move all the reqs from cancelled_list to the tail
3687
+ * of pending_list.
3688
+ * Step3. Stop and restart an isoc transfer.
3689
+ */
3690
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && status == -EXDEV &&
29653691 !list_empty(&dep->cancelled_list) &&
29663692 !list_empty(&dep->pending_list)) {
29673693 list_for_each_entry_safe(req, tmp, &dep->pending_list, list)
2968
- dwc3_gadget_move_cancelled_request(req);
3694
+ dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
29693695 }
29703696
2971
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
3697
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && status == -EXDEV &&
29723698 !list_empty(&dep->cancelled_list)) {
29733699 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list)
29743700 dwc3_gadget_move_queued_request(req);
29753701 }
29763702
2977
- if (event->status & DEPEVT_STATUS_MISSED_ISOC &&
2978
- list_empty(&dep->started_list))
3703
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3704
+ list_empty(&dep->started_list) &&
3705
+ (list_empty(&dep->pending_list) || status == -EXDEV))
29793706 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);
3707
+ else if (dwc3_gadget_ep_should_continue(dep))
3708
+ if (__dwc3_gadget_kick_transfer(dep) == 0)
3709
+ no_started_trb = false;
29833710
3711
+out:
29843712 /*
29853713 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
29863714 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
29873715 */
2988
- if (dwc->revision < DWC3_REVISION_183A) {
3716
+ if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
29893717 u32 reg;
29903718 int i;
29913719
....@@ -2996,7 +3724,7 @@
29963724 continue;
29973725
29983726 if (!list_empty(&dep->started_list))
2999
- return;
3727
+ return no_started_trb;
30003728 }
30013729
30023730 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
....@@ -3005,13 +3733,171 @@
30053733
30063734 dwc->u1u2 = 0;
30073735 }
3736
+
3737
+ return no_started_trb;
3738
+}
3739
+
3740
+static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3741
+ const struct dwc3_event_depevt *event)
3742
+{
3743
+ int status = 0;
3744
+
3745
+ if (!dep->endpoint.desc)
3746
+ return;
3747
+
3748
+ if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3749
+ dwc3_gadget_endpoint_frame_from_event(dep, event);
3750
+
3751
+ if (event->status & DEPEVT_STATUS_BUSERR)
3752
+ status = -ECONNRESET;
3753
+
3754
+ if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3755
+ status = -EXDEV;
3756
+
3757
+ dwc3_gadget_endpoint_trbs_complete(dep, event, status);
3758
+}
3759
+
3760
+static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3761
+ const struct dwc3_event_depevt *event)
3762
+{
3763
+ int status = 0;
3764
+
3765
+ dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3766
+
3767
+ if (event->status & DEPEVT_STATUS_BUSERR)
3768
+ status = -ECONNRESET;
3769
+
3770
+ if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3771
+ dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
30083772 }
30093773
30103774 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
30113775 const struct dwc3_event_depevt *event)
30123776 {
30133777 dwc3_gadget_endpoint_frame_from_event(dep, event);
3778
+
3779
+ /*
3780
+ * The XferNotReady event is generated only once before the endpoint
3781
+ * starts. It will be generated again when END_TRANSFER command is
3782
+ * issued. For some controller versions, the XferNotReady event may be
3783
+ * generated while the END_TRANSFER command is still in process. Ignore
3784
+ * it and wait for the next XferNotReady event after the command is
3785
+ * completed.
3786
+ */
3787
+ if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3788
+ return;
3789
+
30143790 (void) __dwc3_gadget_start_isoc(dep);
3791
+}
3792
+
3793
+static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3794
+ const struct dwc3_event_depevt *event)
3795
+{
3796
+ u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3797
+
3798
+ if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3799
+ return;
3800
+
3801
+ /*
3802
+ * The END_TRANSFER command will cause the controller to generate a
3803
+ * NoStream Event, and it's not due to the host DP NoStream rejection.
3804
+ * Ignore the next NoStream event.
3805
+ */
3806
+ if (dep->stream_capable)
3807
+ dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3808
+
3809
+ dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3810
+ dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3811
+ dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3812
+
3813
+ if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3814
+ struct dwc3 *dwc = dep->dwc;
3815
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
3816
+
3817
+ dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3818
+ if (dwc3_send_clear_stall_ep_cmd(dep)) {
3819
+ struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3820
+
3821
+ dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3822
+ if (dwc->delayed_status)
3823
+ __dwc3_gadget_ep0_set_halt(ep0, 1);
3824
+ return;
3825
+ }
3826
+
3827
+ dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3828
+ if (vdwc->clear_stall_protocol == dep->number)
3829
+ dwc3_ep0_send_delayed_status(dwc);
3830
+ }
3831
+
3832
+ if ((dep->flags & DWC3_EP_DELAY_START) &&
3833
+ !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3834
+ __dwc3_gadget_kick_transfer(dep);
3835
+
3836
+ dep->flags &= ~DWC3_EP_DELAY_START;
3837
+}
3838
+
3839
+static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3840
+ const struct dwc3_event_depevt *event)
3841
+{
3842
+ struct dwc3 *dwc = dep->dwc;
3843
+
3844
+ if (event->status == DEPEVT_STREAMEVT_FOUND) {
3845
+ dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3846
+ goto out;
3847
+ }
3848
+
3849
+ /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3850
+ switch (event->parameters) {
3851
+ case DEPEVT_STREAM_PRIME:
3852
+ /*
3853
+ * If the host can properly transition the endpoint state from
3854
+ * idle to prime after a NoStream rejection, there's no need to
3855
+ * force restarting the endpoint to reinitiate the stream. To
3856
+ * simplify the check, assume the host follows the USB spec if
3857
+ * it primed the endpoint more than once.
3858
+ */
3859
+ if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3860
+ if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3861
+ dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3862
+ else
3863
+ dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3864
+ }
3865
+
3866
+ break;
3867
+ case DEPEVT_STREAM_NOSTREAM:
3868
+ if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3869
+ !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3870
+ !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3871
+ break;
3872
+
3873
+ /*
3874
+ * If the host rejects a stream due to no active stream, by the
3875
+ * USB and xHCI spec, the endpoint will be put back to idle
3876
+ * state. When the host is ready (buffer added/updated), it will
3877
+ * prime the endpoint to inform the usb device controller. This
3878
+ * triggers the device controller to issue ERDY to restart the
3879
+ * stream. However, some hosts don't follow this and keep the
3880
+ * endpoint in the idle state. No prime will come despite host
3881
+ * streams are updated, and the device controller will not be
3882
+ * triggered to generate ERDY to move the next stream data. To
3883
+ * workaround this and maintain compatibility with various
3884
+ * hosts, force to reinitate the stream until the host is ready
3885
+ * instead of waiting for the host to prime the endpoint.
3886
+ */
3887
+ if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3888
+ unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3889
+
3890
+ dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3891
+ } else {
3892
+ dep->flags |= DWC3_EP_DELAY_START;
3893
+ dwc3_stop_active_transfer(dep, true, true);
3894
+ return;
3895
+ }
3896
+ break;
3897
+ }
3898
+
3899
+out:
3900
+ dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
30153901 }
30163902
30173903 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
....@@ -3019,25 +3905,20 @@
30193905 {
30203906 struct dwc3_ep *dep;
30213907 u8 epnum = event->endpoint_number;
3022
- u8 cmd;
30233908
30243909 dep = dwc->eps[epnum];
30253910
30263911 if (!(dep->flags & DWC3_EP_ENABLED)) {
3027
- if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
3912
+ if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED))
30283913 return;
30293914
30303915 /* Handle only EPCMDCMPLT when EP disabled */
3031
- if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3916
+ if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) &&
3917
+ !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE))
30323918 return;
30333919 }
30343920
30353921 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
- }
30413922 dwc3_ep0_interrupt(dwc, event);
30423923 return;
30433924 }
....@@ -3050,21 +3931,14 @@
30503931 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
30513932 break;
30523933 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
- }
3934
+ dwc3_gadget_endpoint_command_complete(dep, event);
3935
+ break;
3936
+ case DWC3_DEPEVT_XFERCOMPLETE:
3937
+ dwc3_gadget_endpoint_transfer_complete(dep, event);
30653938 break;
30663939 case DWC3_DEPEVT_STREAMEVT:
3067
- case DWC3_DEPEVT_XFERCOMPLETE:
3940
+ dwc3_gadget_endpoint_stream_event(dep, event);
3941
+ break;
30683942 case DWC3_DEPEVT_RXTXFIFOEVT:
30693943 break;
30703944 }
....@@ -3072,27 +3946,27 @@
30723946
30733947 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
30743948 {
3075
- if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3949
+ if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
30763950 spin_unlock(&dwc->lock);
3077
- dwc->gadget_driver->disconnect(&dwc->gadget);
3951
+ dwc->gadget_driver->disconnect(dwc->gadget);
30783952 spin_lock(&dwc->lock);
30793953 }
30803954 }
30813955
30823956 static void dwc3_suspend_gadget(struct dwc3 *dwc)
30833957 {
3084
- if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
3958
+ if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
30853959 spin_unlock(&dwc->lock);
3086
- dwc->gadget_driver->suspend(&dwc->gadget);
3960
+ dwc->gadget_driver->suspend(dwc->gadget);
30873961 spin_lock(&dwc->lock);
30883962 }
30893963 }
30903964
30913965 static void dwc3_resume_gadget(struct dwc3 *dwc)
30923966 {
3093
- if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3967
+ if (dwc->async_callbacks && dwc->gadget_driver->resume) {
30943968 spin_unlock(&dwc->lock);
3095
- dwc->gadget_driver->resume(&dwc->gadget);
3969
+ dwc->gadget_driver->resume(dwc->gadget);
30963970 spin_lock(&dwc->lock);
30973971 }
30983972 }
....@@ -3102,23 +3976,45 @@
31023976 if (!dwc->gadget_driver)
31033977 return;
31043978
3105
- if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
3979
+ if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
31063980 spin_unlock(&dwc->lock);
3107
- usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
3981
+ usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
31083982 spin_lock(&dwc->lock);
31093983 }
31103984 }
31113985
3112
-static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3986
+void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
31133987 bool interrupt)
31143988 {
3115
- struct dwc3_gadget_ep_cmd_params params;
3116
- u32 cmd;
3117
- int ret;
3989
+ struct dwc3 *dwc = dep->dwc;
3990
+
3991
+ /*
3992
+ * Only issue End Transfer command to the control endpoint of a started
3993
+ * Data Phase. Typically we should only do so in error cases such as
3994
+ * invalid/unexpected direction as described in the control transfer
3995
+ * flow of the programming guide.
3996
+ */
3997
+ if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
3998
+ return;
3999
+
4000
+ if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP))
4001
+ return;
31184002
31194003 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
31204004 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
31214005 return;
4006
+
4007
+ /*
4008
+ * If a Setup packet is received but yet to DMA out, the controller will
4009
+ * not process the End Transfer command of any endpoint. Polling of its
4010
+ * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
4011
+ * timeout. Delay issuing the End Transfer command until the Setup TRB is
4012
+ * prepared.
4013
+ */
4014
+ if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) {
4015
+ dep->flags |= DWC3_EP_DELAY_STOP;
4016
+ return;
4017
+ }
31224018
31234019 /*
31244020 * NOTICE: We are violating what the Databook says about the
....@@ -3144,23 +4040,16 @@
31444040 * enabled, the EndTransfer command will have completed upon
31454041 * returning from this function.
31464042 *
3147
- * This mode is NOT available on the DWC_usb31 IP.
4043
+ * This mode is NOT available on the DWC_usb31 IP. In this
4044
+ * case, if the IOC bit is not set, then delay by 1ms
4045
+ * after issuing the EndTransfer command. This allows for the
4046
+ * controller to handle the command completely before DWC3
4047
+ * remove requests attempts to unmap USB request buffers.
31484048 */
31494049
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;
4050
+ __dwc3_stop_active_transfer(dep, force, interrupt);
31634051 }
4052
+EXPORT_SYMBOL_GPL(dwc3_stop_active_transfer);
31644053
31654054 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
31664055 {
....@@ -3188,30 +4077,36 @@
31884077 {
31894078 int reg;
31904079
4080
+ dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
4081
+
31914082 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
31924083 reg &= ~DWC3_DCTL_INITU1ENA;
3193
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3194
-
31954084 reg &= ~DWC3_DCTL_INITU2ENA;
3196
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4085
+ dwc3_gadget_dctl_write_safe(dwc, reg);
4086
+
4087
+ dwc->connected = false;
31974088
31984089 dwc3_disconnect_gadget(dwc);
31994090
3200
- dwc->gadget.speed = USB_SPEED_UNKNOWN;
4091
+ dwc->gadget->speed = USB_SPEED_UNKNOWN;
32014092 dwc->setup_packet_pending = false;
3202
- usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
4093
+ usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
32034094
3204
- dwc->connected = false;
3205
- complete(&dwc->discon_done);
4095
+ if (dwc->ep0state != EP0_SETUP_PHASE) {
4096
+ unsigned int dir;
4097
+
4098
+ dir = !!dwc->ep0_expect_in;
4099
+ if (dwc->ep0state == EP0_DATA_PHASE)
4100
+ dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
4101
+ else
4102
+ dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
4103
+ dwc3_ep0_stall_and_restart(dwc);
4104
+ }
32064105 }
32074106
32084107 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
32094108 {
32104109 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);
32154110
32164111 /*
32174112 * Ideally, dwc3_reset_gadget() would trigger the function
....@@ -3248,16 +4143,45 @@
32484143 * STAR#9000466709: RTL: Device : Disconnect event not
32494144 * generated if setup packet pending in FIFO
32504145 */
3251
- if (dwc->revision < DWC3_REVISION_188A) {
4146
+ if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
32524147 if (dwc->setup_packet_pending)
32534148 dwc3_gadget_disconnect_interrupt(dwc);
32544149 }
32554150
32564151 dwc3_reset_gadget(dwc);
32574152
4153
+ /*
4154
+ * From SNPS databook section 8.1.2, the EP0 should be in setup
4155
+ * phase. So ensure that EP0 is in setup phase by issuing a stall
4156
+ * and restart if EP0 is not in setup phase.
4157
+ */
4158
+ if (dwc->ep0state != EP0_SETUP_PHASE) {
4159
+ unsigned int dir;
4160
+
4161
+ dir = !!dwc->ep0_expect_in;
4162
+ if (dwc->ep0state == EP0_DATA_PHASE)
4163
+ dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
4164
+ else
4165
+ dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
4166
+
4167
+ dwc->eps[0]->trb_enqueue = 0;
4168
+ dwc->eps[1]->trb_enqueue = 0;
4169
+
4170
+ dwc3_ep0_stall_and_restart(dwc);
4171
+ }
4172
+
4173
+ /*
4174
+ * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
4175
+ * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
4176
+ * needs to ensure that it sends "a DEPENDXFER command for any active
4177
+ * transfers."
4178
+ */
4179
+ dwc3_stop_active_transfers(dwc);
4180
+ dwc->connected = true;
4181
+
32584182 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
32594183 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3260
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4184
+ dwc3_gadget_dctl_write_safe(dwc, reg);
32614185 dwc->test_mode = false;
32624186 dwc3_clear_stall_all_ep(dwc);
32634187
....@@ -3272,11 +4196,21 @@
32724196 struct dwc3_ep *dep;
32734197 int ret;
32744198 u32 reg;
4199
+ u8 lanes = 1;
32754200 u8 speed;
4201
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
4202
+
4203
+ if (!vdwc->softconnect)
4204
+ return;
32764205
32774206 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
32784207 speed = reg & DWC3_DSTS_CONNECTSPD;
32794208 dwc->speed = speed;
4209
+
4210
+ if (DWC3_IP_IS(DWC32))
4211
+ lanes = DWC3_DSTS_CONNLANES(reg) + 1;
4212
+
4213
+ dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
32804214
32814215 /*
32824216 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
....@@ -3290,8 +4224,13 @@
32904224 switch (speed) {
32914225 case DWC3_DSTS_SUPERSPEED_PLUS:
32924226 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3293
- dwc->gadget.ep0->maxpacket = 512;
3294
- dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
4227
+ dwc->gadget->ep0->maxpacket = 512;
4228
+ dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4229
+
4230
+ if (lanes > 1)
4231
+ dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
4232
+ else
4233
+ dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
32954234 break;
32964235 case DWC3_DSTS_SUPERSPEED:
32974236 /*
....@@ -3307,35 +4246,41 @@
33074246 * STAR#9000483510: RTL: SS : USB3 reset event may
33084247 * not be generated always when the link enters poll
33094248 */
3310
- if (dwc->revision < DWC3_REVISION_190A)
4249
+ if (DWC3_VER_IS_PRIOR(DWC3, 190A))
33114250 dwc3_gadget_reset_interrupt(dwc);
33124251
33134252 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3314
- dwc->gadget.ep0->maxpacket = 512;
3315
- dwc->gadget.speed = USB_SPEED_SUPER;
4253
+ dwc->gadget->ep0->maxpacket = 512;
4254
+ dwc->gadget->speed = USB_SPEED_SUPER;
4255
+
4256
+ if (lanes > 1) {
4257
+ dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4258
+ dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
4259
+ }
33164260 break;
33174261 case DWC3_DSTS_HIGHSPEED:
33184262 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3319
- dwc->gadget.ep0->maxpacket = 64;
3320
- dwc->gadget.speed = USB_SPEED_HIGH;
4263
+ dwc->gadget->ep0->maxpacket = 64;
4264
+ dwc->gadget->speed = USB_SPEED_HIGH;
33214265 break;
33224266 case DWC3_DSTS_FULLSPEED:
33234267 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3324
- dwc->gadget.ep0->maxpacket = 64;
3325
- dwc->gadget.speed = USB_SPEED_FULL;
4268
+ dwc->gadget->ep0->maxpacket = 64;
4269
+ dwc->gadget->speed = USB_SPEED_FULL;
33264270 break;
33274271 case DWC3_DSTS_LOWSPEED:
33284272 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3329
- dwc->gadget.ep0->maxpacket = 8;
3330
- dwc->gadget.speed = USB_SPEED_LOW;
4273
+ dwc->gadget->ep0->maxpacket = 8;
4274
+ dwc->gadget->speed = USB_SPEED_LOW;
33314275 break;
33324276 }
33334277
3334
- dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
4278
+ dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
33354279
33364280 /* Enable USB2 LPM Capability */
33374281
3338
- if ((dwc->revision > DWC3_REVISION_194A) &&
4282
+ if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
4283
+ !dwc->usb2_gadget_lpm_disable &&
33394284 (speed != DWC3_DSTS_SUPERSPEED) &&
33404285 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
33414286 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
....@@ -3345,7 +4290,8 @@
33454290 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
33464291 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
33474292
3348
- reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
4293
+ reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
4294
+ (dwc->is_utmi_l1_suspend << 4));
33494295
33504296 /*
33514297 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
....@@ -3353,23 +4299,24 @@
33534299 * BESL value in the LPM token is less than or equal to LPM
33544300 * NYET threshold.
33554301 */
3356
- WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3357
- && dwc->has_lpm_erratum,
4302
+ WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
33584303 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
33594304
3360
- if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
4305
+ if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
33614306 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
33624307
3363
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4308
+ dwc3_gadget_dctl_write_safe(dwc, reg);
33644309 } else {
4310
+ if (dwc->usb2_gadget_lpm_disable) {
4311
+ reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4312
+ reg &= ~DWC3_DCFG_LPM_CAP;
4313
+ dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4314
+ }
4315
+
33654316 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
33664317 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3367
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4318
+ dwc3_gadget_dctl_write_safe(dwc, reg);
33684319 }
3369
-
3370
-#ifdef CONFIG_ARCH_ROCKCHIP
3371
- dwc3_gadget_resize_tx_fifos(dwc);
3372
-#endif
33734320
33744321 dep = dwc->eps[0];
33754322 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
....@@ -3394,21 +4341,18 @@
33944341 */
33954342 }
33964343
3397
-static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo)
4344
+static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
33984345 {
3399
- enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
34004346 /*
34014347 * TODO take core out of low power mode when that's
34024348 * implemented.
34034349 */
34044350
3405
- if (dwc->gadget_driver && dwc->gadget_driver->resume && dwc->uwk_en) {
4351
+ if (dwc->async_callbacks && dwc->gadget_driver->resume) {
34064352 spin_unlock(&dwc->lock);
3407
- dwc->gadget_driver->resume(&dwc->gadget);
4353
+ dwc->gadget_driver->resume(dwc->gadget);
34084354 spin_lock(&dwc->lock);
34094355 }
3410
-
3411
- dwc->link_state = next;
34124356 }
34134357
34144358 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
....@@ -3435,7 +4379,7 @@
34354379 * operational mode
34364380 */
34374381 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3438
- if ((dwc->revision < DWC3_REVISION_250A) &&
4382
+ if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
34394383 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
34404384 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
34414385 (next == DWC3_LINK_STATE_RESUME)) {
....@@ -3461,7 +4405,7 @@
34614405 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
34624406 * core send LGO_Ux entering U0
34634407 */
3464
- if (dwc->revision < DWC3_REVISION_183A) {
4408
+ if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
34654409 if (next == DWC3_LINK_STATE_U0) {
34664410 u32 u1u2;
34674411 u32 reg;
....@@ -3480,7 +4424,7 @@
34804424
34814425 reg &= ~u1u2;
34824426
3483
- dwc3_writel(dwc->regs, DWC3_DCTL, reg);
4427
+ dwc3_gadget_dctl_write_safe(dwc, reg);
34844428 break;
34854429 default:
34864430 /* do nothing */
....@@ -3514,8 +4458,7 @@
35144458 {
35154459 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
35164460
3517
- if (dwc->link_state != next && next == DWC3_LINK_STATE_U3 &&
3518
- dwc->uwk_en)
4461
+ if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
35194462 dwc3_suspend_gadget(dwc);
35204463
35214464 dwc->link_state = next;
....@@ -3561,8 +4504,7 @@
35614504 dwc3_gadget_conndone_interrupt(dwc);
35624505 break;
35634506 case DWC3_DEVICE_EVENT_WAKEUP:
3564
- dev_dbg(dwc->dev, "device wakeup\n");
3565
- dwc3_gadget_wakeup_interrupt(dwc, event->event_info);
4507
+ dwc3_gadget_wakeup_interrupt(dwc);
35664508 break;
35674509 case DWC3_DEVICE_EVENT_HIBER_REQ:
35684510 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
....@@ -3574,18 +4516,10 @@
35744516 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
35754517 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
35764518 break;
3577
- case DWC3_DEVICE_EVENT_EOPF:
4519
+ case DWC3_DEVICE_EVENT_SUSPEND:
35784520 /* It changed to be suspend event for version 2.30a and above */
3579
- if (dwc->revision >= DWC3_REVISION_230A) {
3580
- /*
3581
- * Ignore suspend event until the gadget enters into
3582
- * USB_STATE_CONFIGURED state.
3583
- */
3584
- dev_dbg(dwc->dev, "device suspend\n");
3585
- if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3586
- dwc3_gadget_suspend_interrupt(dwc,
3587
- event->event_info);
3588
- }
4521
+ if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
4522
+ dwc3_gadget_suspend_interrupt(dwc, event->event_info);
35894523 break;
35904524 case DWC3_DEVICE_EVENT_SOF:
35914525 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
....@@ -3615,7 +4549,6 @@
36154549 struct dwc3 *dwc = evt->dwc;
36164550 irqreturn_t ret = IRQ_NONE;
36174551 int left;
3618
- u32 reg;
36194552
36204553 left = evt->count;
36214554
....@@ -3643,18 +4576,19 @@
36434576 }
36444577
36454578 evt->count = 0;
3646
- evt->flags &= ~DWC3_EVENT_PENDING;
36474579 ret = IRQ_HANDLED;
36484580
36494581 /* 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);
4582
+ dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4583
+ DWC3_GEVNTSIZ_SIZE(evt->length));
36534584
36544585 if (dwc->imod_interval) {
36554586 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
36564587 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
36574588 }
4589
+
4590
+ /* Keep the clearing of DWC3_EVENT_PENDING at the end */
4591
+ evt->flags &= ~DWC3_EVENT_PENDING;
36584592
36594593 return ret;
36604594 }
....@@ -3680,12 +4614,16 @@
36804614 struct dwc3 *dwc = evt->dwc;
36814615 u32 amount;
36824616 u32 count;
3683
- u32 reg;
36844617
36854618 if (pm_runtime_suspended(dwc->dev)) {
4619
+ dwc->pending_events = true;
4620
+ /*
4621
+ * Trigger runtime resume. The get() function will be balanced
4622
+ * after processing the pending events in dwc3_process_pending
4623
+ * events().
4624
+ */
36864625 pm_runtime_get(dwc->dev);
36874626 disable_irq_nosync(dwc->irq_gadget);
3688
- dwc->pending_events = true;
36894627 return IRQ_HANDLED;
36904628 }
36914629
....@@ -3707,9 +4645,8 @@
37074645 evt->flags |= DWC3_EVENT_PENDING;
37084646
37094647 /* 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);
4648
+ dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4649
+ DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length));
37134650
37144651 amount = min(count, evt->length - evt->lpos);
37154652 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
....@@ -3734,14 +4671,14 @@
37344671 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
37354672 int irq;
37364673
3737
- irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
4674
+ irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
37384675 if (irq > 0)
37394676 goto out;
37404677
37414678 if (irq == -EPROBE_DEFER)
37424679 goto out;
37434680
3744
- irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
4681
+ irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
37454682 if (irq > 0)
37464683 goto out;
37474684
....@@ -3752,14 +4689,18 @@
37524689 if (irq > 0)
37534690 goto out;
37544691
3755
- if (irq != -EPROBE_DEFER)
3756
- dev_err(dwc->dev, "missing peripheral IRQ\n");
3757
-
37584692 if (!irq)
37594693 irq = -EINVAL;
37604694
37614695 out:
37624696 return irq;
4697
+}
4698
+
4699
+static void dwc_gadget_release(struct device *dev)
4700
+{
4701
+ struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
4702
+
4703
+ kfree(gadget);
37634704 }
37644705
37654706 /**
....@@ -3772,6 +4713,7 @@
37724713 {
37734714 int ret;
37744715 int irq;
4716
+ struct device *dev;
37754717
37764718 irq = dwc3_gadget_get_irq(dwc);
37774719 if (irq < 0) {
....@@ -3804,17 +4746,22 @@
38044746 }
38054747
38064748 init_completion(&dwc->ep0_in_setup);
3807
- init_completion(&dwc->discon_done);
4749
+ dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4750
+ if (!dwc->gadget) {
4751
+ ret = -ENOMEM;
4752
+ goto err3;
4753
+ }
38084754
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
4755
+
4756
+ usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
4757
+ dev = &dwc->gadget->dev;
4758
+ dev->platform_data = dwc;
4759
+ dwc->gadget->ops = &dwc3_gadget_ops;
4760
+ dwc->gadget->speed = USB_SPEED_UNKNOWN;
4761
+ dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
4762
+ dwc->gadget->sg_supported = true;
4763
+ dwc->gadget->name = "dwc3-gadget";
4764
+ dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
38184765
38194766 /*
38204767 * FIXME We might be setting max_speed to <SUPER, however versions
....@@ -3832,12 +4779,13 @@
38324779 * is less than super speed because we don't have means, yet, to tell
38334780 * composite.c that we are USB 2.0 + LPM ECN.
38344781 */
3835
- if (dwc->revision < DWC3_REVISION_220A &&
4782
+ if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
38364783 !dwc->dis_metastability_quirk)
38374784 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
38384785 dwc->revision);
38394786
3840
- dwc->gadget.max_speed = dwc->maximum_speed;
4787
+ dwc->gadget->max_speed = dwc->maximum_speed;
4788
+ dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
38414789
38424790 /*
38434791 * REVISIT: Here we should clear all pending IRQs to be
....@@ -3846,21 +4794,26 @@
38464794
38474795 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
38484796 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");
38544797 goto err4;
4798
+
4799
+ ret = usb_add_gadget(dwc->gadget);
4800
+ if (ret) {
4801
+ dev_err(dwc->dev, "failed to add gadget\n");
4802
+ goto err5;
38554803 }
38564804
3857
- dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
4805
+ if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4806
+ dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4807
+ else
4808
+ dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
38584809
38594810 return 0;
38604811
3861
-err4:
4812
+err5:
38624813 dwc3_gadget_free_endpoints(dwc);
3863
-
4814
+err4:
4815
+ usb_put_gadget(dwc->gadget);
4816
+ dwc->gadget = NULL;
38644817 err3:
38654818 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
38664819 dwc->bounce_addr);
....@@ -3880,8 +4833,12 @@
38804833
38814834 void dwc3_gadget_exit(struct dwc3 *dwc)
38824835 {
3883
- usb_del_gadget_udc(&dwc->gadget);
4836
+ if (!dwc->gadget)
4837
+ return;
4838
+
4839
+ usb_del_gadget(dwc->gadget);
38844840 dwc3_gadget_free_endpoints(dwc);
4841
+ usb_put_gadget(dwc->gadget);
38854842 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
38864843 dwc->bounce_addr);
38874844 kfree(dwc->setup_buf);
....@@ -3891,21 +4848,27 @@
38914848
38924849 int dwc3_gadget_suspend(struct dwc3 *dwc)
38934850 {
4851
+ unsigned long flags;
4852
+
38944853 if (!dwc->gadget_driver)
38954854 return 0;
38964855
38974856 dwc3_gadget_run_stop(dwc, false, false);
4857
+
4858
+ spin_lock_irqsave(&dwc->lock, flags);
38984859 dwc3_disconnect_gadget(dwc);
38994860 __dwc3_gadget_stop(dwc);
4861
+ spin_unlock_irqrestore(&dwc->lock, flags);
39004862
39014863 return 0;
39024864 }
39034865
39044866 int dwc3_gadget_resume(struct dwc3 *dwc)
39054867 {
4868
+ struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
39064869 int ret;
39074870
3908
- if (!dwc->gadget_driver)
4871
+ if (!dwc->gadget_driver || !vdwc->softconnect)
39094872 return 0;
39104873
39114874 ret = __dwc3_gadget_start(dwc);
....@@ -3929,6 +4892,8 @@
39294892 {
39304893 if (dwc->pending_events) {
39314894 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4895
+ dwc3_thread_interrupt(dwc->irq_gadget, dwc->ev_buf);
4896
+ pm_runtime_put(dwc->dev);
39324897 dwc->pending_events = false;
39334898 enable_irq(dwc->irq_gadget);
39344899 }