blob: 3b9e6ac616dc36376e31adf6c6d4f8f61a686b94 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 */
10
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/platform_device.h>
15#include <linux/pm_runtime.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/list.h>
19#include <linux/dma-mapping.h>
20#include <linux/cputype.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24#include <linux/usb/composite.h>
25
26#include "core.h"
27#include "debug.h"
28#include "gadget.h"
29#include "io.h"
30
31static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
32static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
33 struct dwc3_ep *dep, struct dwc3_request *req);
34
35static void dwc3_ep0_prepare_one_trb(struct dwc3_ep *dep,
36 dma_addr_t buf_dma, u32 len, u32 type, bool chain)
37{
38 struct dwc3_trb *trb;
39 struct dwc3 *dwc;
40
41 dwc = dep->dwc;
42
43 if ((dep->trb_enqueue > 1) || (chain && (dep->trb_enqueue != 0))) {
44 pr_err("!!!!error: ep%d trb_enqueue: %d, len: %d, type: %d\n", dep->number, dep->trb_enqueue, len, type);
45 WARN(1, "ep0 trb error\n");
46 }
47
48 trb = &dwc->ep0_trb[dep->trb_enqueue];
49
50 if (chain) {
51 dep->trb_enqueue++;
52 dep->trb_enqueue = dep->trb_enqueue & (DWC3_TRB_NUM - 1);
53 }
54 trb->bpl = lower_32_bits(buf_dma);
55 trb->bph = upper_32_bits(buf_dma);
56 trb->size = len;
57 trb->ctrl = type;
58
59 trb->ctrl |= (DWC3_TRB_CTRL_HWO
60 | DWC3_TRB_CTRL_ISP_IMI);
61
62 if (chain)
63 trb->ctrl |= DWC3_TRB_CTRL_CHN;
64 else
65 trb->ctrl |= (DWC3_TRB_CTRL_IOC
66 | DWC3_TRB_CTRL_LST);
67
68 trace_dwc3_prepare_trb(dep, trb);
69}
70
71static int dwc3_ep0_start_trans(struct dwc3_ep *dep)
72{
73 struct dwc3_gadget_ep_cmd_params params;
74 struct dwc3 *dwc;
75 int ret;
76
77 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
78 pr_info("!!!!!ep%d STARTED\n", dep->number);
79 return 0;
80 }
81 dwc = dep->dwc;
82
83 memset(&params, 0, sizeof(params));
84 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
85 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
86
87 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, &params);
88 if (ret < 0) {
89 dev_err(dwc->dev, "ep%d start xfer failed, ret: %d\n", dep->number, ret);
90 return ret;
91 }
92
93 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
94
95 return 0;
96}
97
98static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
99 struct dwc3_request *req)
100{
101 struct dwc3 *dwc = dep->dwc;
102
103 req->request.actual = 0;
104 req->request.status = -EINPROGRESS;
105 req->epnum = dep->number;
106
107 list_add_tail(&req->list, &dep->pending_list);
108
109 /*
110 * Gadget driver might not be quick enough to queue a request
111 * before we get a Transfer Not Ready event on this endpoint.
112 *
113 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
114 * flag is set, it's telling us that as soon as Gadget queues the
115 * required request, we should kick the transfer here because the
116 * IRQ we were waiting for is long gone.
117 */
118 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
119 unsigned direction;
120
121 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
122
123 if (dwc->ep0state != EP0_DATA_PHASE) {
124 dev_WARN(dwc->dev, "Unexpected pending request\n");
125 return 0;
126 }
127
128 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
129
130 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
131 DWC3_EP0_DIR_IN);
132
133 return 0;
134 }
135
136 /*
137 * In case gadget driver asked us to delay the STATUS phase,
138 * handle it here.
139 */
140 if (dwc->delayed_status) {
141 unsigned direction;
142
143 direction = !dwc->ep0_expect_in;
144 dwc->delayed_status = false;
145 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
146
147 if (dwc->ep0state == EP0_STATUS_PHASE)
148 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
149
150 return 0;
151 }
152
153 /*
154 * Unfortunately we have uncovered a limitation wrt the Data Phase.
155 *
156 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
157 * come before issueing Start Transfer command, but if we do, we will
158 * miss situations where the host starts another SETUP phase instead of
159 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
160 * Layer Compliance Suite.
161 *
162 * The problem surfaces due to the fact that in case of back-to-back
163 * SETUP packets there will be no XferNotReady(DATA) generated and we
164 * will be stuck waiting for XferNotReady(DATA) forever.
165 *
166 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
167 * it tells us to start Data Phase right away. It also mentions that if
168 * we receive a SETUP phase instead of the DATA phase, core will issue
169 * XferComplete for the DATA phase, before actually initiating it in
170 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
171 * can only be used to print some debugging logs, as the core expects
172 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
173 * just so it completes right away, without transferring anything and,
174 * only then, we can go back to the SETUP phase.
175 *
176 * Because of this scenario, SNPS decided to change the programming
177 * model of control transfers and support on-demand transfers only for
178 * the STATUS phase. To fix the issue we have now, we will always wait
179 * for gadget driver to queue the DATA phase's struct usb_request, then
180 * start it right away.
181 *
182 * If we're actually in a 2-stage transfer, we will wait for
183 * XferNotReady(STATUS).
184 */
185 if (dwc->three_stage_setup) {
186 unsigned direction;
187
188 direction = dwc->ep0_expect_in;
189 dwc->ep0state = EP0_DATA_PHASE;
190
191 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
192
193 dep->flags &= ~DWC3_EP0_DIR_IN;
194 }
195
196 return 0;
197}
198
199int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
200 gfp_t gfp_flags)
201{
202 struct dwc3_request *req = to_dwc3_request(request);
203 struct dwc3_ep *dep = to_dwc3_ep(ep);
204 struct dwc3 *dwc = dep->dwc;
205
206 unsigned long flags;
207
208 int ret;
209
210 spin_lock_irqsave(&dwc->lock, flags);
211 if (!dep->endpoint.desc || !dwc->pullups_connected) {
212 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
213 dep->name);
214 ret = -ESHUTDOWN;
215 goto out;
216 }
217
218#if defined(CONFIG_USB_ANDROID_DETECT_HOST_OS)
219 if (os_detect_is_done()) {
220 dev_err(dwc->dev,
221 "os detection is done, skip ep:%s req: %d\n", dep->name, request->length);
222 ret = -ESHUTDOWN;
223 goto out;
224 }
225#endif
226
227 /* we share one TRB for ep0/1 */
228 if (!list_empty(&dep->pending_list)) {
229 ret = -EBUSY;
230 dev_err(dwc->dev, "ep%d still pending\n", dep->number);
231 goto out;
232 }
233
234 ret = __dwc3_gadget_ep0_queue(dep, req);
235
236out:
237 spin_unlock_irqrestore(&dwc->lock, flags);
238
239 return ret;
240}
241
242#if defined(CONFIG_USB_ANDROID_DETECT_HOST_OS)
243static void dwc3_reset_ep0(struct dwc3 *dwc)
244{
245 struct dwc3_ep *dep;
246
247 /* reinitialize physical ep1 */
248 dep = dwc->eps[1];
249 dep->flags = DWC3_EP_ENABLED;
250
251 /* stall is always issued on EP0 */
252 dep = dwc->eps[0];
253 dep->flags = DWC3_EP_ENABLED;
254 dwc->delayed_status = false;
255
256 if (!list_empty(&dep->pending_list)) {
257 struct dwc3_request *req;
258
259 req = next_request(&dep->pending_list);
260 dwc3_gadget_giveback(dep, req, -ECONNRESET);
261 }
262}
263#endif
264
265static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
266{
267 struct dwc3_ep *dep;
268
269 /* reinitialize physical ep1 */
270 dep = dwc->eps[1];
271 dep->flags = DWC3_EP_ENABLED;
272
273 /* stall is always issued on EP0 */
274 dep = dwc->eps[0];
275 __dwc3_gadget_ep_set_halt(dep, 1, false);
276 dep->flags = DWC3_EP_ENABLED;
277 dwc->delayed_status = false;
278
279 if (!list_empty(&dep->pending_list)) {
280 struct dwc3_request *req;
281
282 req = next_request(&dep->pending_list);
283 if (!dwc->connected)
284 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
285 else
286 dwc3_gadget_giveback(dep, req, -ECONNRESET);
287 }
288
289 dwc->ep0state = EP0_SETUP_PHASE;
290 dwc3_ep0_out_start(dwc);
291}
292
293int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
294{
295 struct dwc3_ep *dep = to_dwc3_ep(ep);
296 struct dwc3 *dwc = dep->dwc;
297
298 dwc3_ep0_stall_and_restart(dwc);
299
300 return 0;
301}
302
303int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
304{
305 struct dwc3_ep *dep = to_dwc3_ep(ep);
306 struct dwc3 *dwc = dep->dwc;
307 unsigned long flags;
308 int ret;
309
310 spin_lock_irqsave(&dwc->lock, flags);
311 ret = __dwc3_gadget_ep0_set_halt(ep, value);
312 spin_unlock_irqrestore(&dwc->lock, flags);
313
314 return ret;
315}
316
317void dwc3_ep0_out_start(struct dwc3 *dwc)
318{
319 struct dwc3_ep *dep;
320 int ret;
321
322 complete(&dwc->ep0_in_setup);
323
324 dep = dwc->eps[0];
325 dwc3_ep0_prepare_one_trb(dep, dwc->ctrl_req_addr, 8,
326 DWC3_TRBCTL_CONTROL_SETUP, false);
327 ret = dwc3_ep0_start_trans(dep);
328 WARN_ON(ret < 0);
329}
330
331static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
332{
333 struct dwc3_ep *dep;
334 u32 windex = le16_to_cpu(wIndex_le);
335 u32 epnum;
336
337 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
338 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
339 epnum |= 1;
340
341 dep = dwc->eps[epnum];
342 if (dep == NULL) {
343 dev_err(dwc->dev, "ep%d NULL\n", epnum);
344 return NULL;
345 }
346
347 if (dep->flags & DWC3_EP_ENABLED)
348 return dep;
349
350 dev_err(dwc->dev, "ep%d not enabled, flags: %x\n", epnum, dep->flags);
351 return NULL;
352}
353
354static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
355{
356}
357/*
358 * ch 9.4.5
359 */
360static int dwc3_ep0_handle_status(struct dwc3 *dwc,
361 struct usb_ctrlrequest *ctrl)
362{
363 struct dwc3_ep *dep;
364 u32 recip;
365 u32 value;
366 u32 reg;
367 u16 usb_status = 0;
368 __le16 *response_pkt;
369
370 /* We don't support PTM_STATUS */
371 value = le16_to_cpu(ctrl->wValue);
372 if (value != 0)
373 return -EINVAL;
374
375 recip = ctrl->bRequestType & USB_RECIP_MASK;
376 switch (recip) {
377 case USB_RECIP_DEVICE:
378 /*
379 * LTM will be set once we know how to set this in HW.
380 */
381 usb_status |= dwc->gadget.is_selfpowered;
382
383 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
384 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
385 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
386 if (reg & DWC3_DCTL_INITU1ENA)
387 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
388 if (reg & DWC3_DCTL_INITU2ENA)
389 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
390 }
391
392 break;
393
394 case USB_RECIP_INTERFACE:
395 /*
396 * Function Remote Wake Capable D0
397 * Function Remote Wakeup D1
398 */
399 break;
400
401 case USB_RECIP_ENDPOINT:
402 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
403 if (!dep) {
404 dev_err(dwc->dev, "usb err: dep is NULL\n");
405 return -EINVAL;
406 }
407 if (dep->flags & DWC3_EP_STALL)
408 usb_status = 1 << USB_ENDPOINT_HALT;
409 break;
410 default:
411 dev_err(dwc->dev, "%s recip: %x\n", __func__, recip);
412 return -EINVAL;
413 }
414
415 response_pkt = (__le16 *) dwc->setup_buf;
416 *response_pkt = cpu_to_le16(usb_status);
417
418 dep = dwc->eps[0];
419 dwc->ep0_usb_req.dep = dep;
420 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
421 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
422 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
423
424 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
425}
426
427static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
428 int set)
429{
430 u32 reg;
431
432 if (state != USB_STATE_CONFIGURED)
433 return -EINVAL;
434 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
435 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
436 return -EINVAL;
437 if (set && dwc->dis_u1_entry_quirk)
438 return -EINVAL;
439
440 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
441 if (cpu_is_asr1901() || cpu_is_asr1906()) {
442 reg &= ~DWC3_DCTL_INITU1ENA;
443 } else {
444 if (set)
445 reg |= DWC3_DCTL_INITU1ENA;
446 else
447 reg &= ~DWC3_DCTL_INITU1ENA;
448 }
449 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
450
451 return 0;
452}
453
454static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
455 int set)
456{
457 u32 reg;
458
459
460 if (state != USB_STATE_CONFIGURED)
461 return -EINVAL;
462 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
463 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
464 return -EINVAL;
465 if (set && dwc->dis_u2_entry_quirk)
466 return -EINVAL;
467
468 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
469 if (cpu_is_asr1901() || cpu_is_asr1906()) {
470 reg &= ~DWC3_DCTL_INITU2ENA;
471 } else {
472 if (set)
473 reg |= DWC3_DCTL_INITU2ENA;
474 else
475 reg &= ~DWC3_DCTL_INITU2ENA;
476 }
477 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
478
479 return 0;
480}
481
482static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
483 u32 wIndex, int set)
484{
485 if ((wIndex & 0xff) != 0)
486 return -EINVAL;
487 if (!set)
488 return -EINVAL;
489
490 switch (wIndex >> 8) {
491 case TEST_J:
492 case TEST_K:
493 case TEST_SE0_NAK:
494 case TEST_PACKET:
495 case TEST_FORCE_EN:
496 dwc->test_mode_nr = wIndex >> 8;
497 dwc->test_mode = true;
498 break;
499 default:
500 return -EINVAL;
501 }
502
503 return 0;
504}
505
506static int dwc3_ep0_handle_device(struct dwc3 *dwc,
507 struct usb_ctrlrequest *ctrl, int set)
508{
509 enum usb_device_state state;
510 u32 wValue;
511 u32 wIndex;
512 int ret = 0;
513
514 wValue = le16_to_cpu(ctrl->wValue);
515 wIndex = le16_to_cpu(ctrl->wIndex);
516 state = dwc->gadget.state;
517
518 switch (wValue) {
519 case USB_DEVICE_REMOTE_WAKEUP:
520 break;
521 /*
522 * 9.4.1 says only only for SS, in AddressState only for
523 * default control pipe
524 */
525 case USB_DEVICE_U1_ENABLE:
526 ret = dwc3_ep0_handle_u1(dwc, state, set);
527 break;
528 case USB_DEVICE_U2_ENABLE:
529 ret = dwc3_ep0_handle_u2(dwc, state, set);
530 break;
531 case USB_DEVICE_LTM_ENABLE:
532 ret = -EINVAL;
533 break;
534 case USB_DEVICE_TEST_MODE:
535 ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
536 break;
537 default:
538 ret = -EINVAL;
539 }
540
541 return ret;
542}
543
544static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
545 struct usb_ctrlrequest *ctrl, int set)
546{
547 u32 wValue;
548 int ret = 0;
549
550 wValue = le16_to_cpu(ctrl->wValue);
551
552 switch (wValue) {
553 case USB_INTRF_FUNC_SUSPEND:
554 /*
555 * REVISIT: Ideally we would enable some low power mode here,
556 * however it's unclear what we should be doing here.
557 *
558 * For now, we're not doing anything, just making sure we return
559 * 0 so USB Command Verifier tests pass without any errors.
560 */
561 break;
562 default:
563 ret = -EINVAL;
564 }
565
566 return ret;
567}
568
569static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
570 struct usb_ctrlrequest *ctrl, int set)
571{
572 struct dwc3_ep *dep;
573 u32 wValue;
574 int ret;
575
576 wValue = le16_to_cpu(ctrl->wValue);
577
578 switch (wValue) {
579 case USB_ENDPOINT_HALT:
580 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
581 if (!dep)
582 return -EINVAL;
583
584 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
585 break;
586
587 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
588 if (ret) {
589 dev_err(dwc->dev, "set ep%d halt %d\n", dep->number, ret);
590 return -EINVAL;
591 }
592
593 /* ClearFeature(Halt) may need delayed status */
594 if (!set && (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
595 return USB_GADGET_DELAYED_STATUS;
596
597 break;
598 default:
599 return -EINVAL;
600 }
601
602 return 0;
603}
604
605static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
606 struct usb_ctrlrequest *ctrl, int set)
607{
608 u32 recip;
609 int ret;
610
611 recip = ctrl->bRequestType & USB_RECIP_MASK;
612
613 switch (recip) {
614 case USB_RECIP_DEVICE:
615 ret = dwc3_ep0_handle_device(dwc, ctrl, set);
616 break;
617 case USB_RECIP_INTERFACE:
618 ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
619 break;
620 case USB_RECIP_ENDPOINT:
621 ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
622 break;
623 default:
624 ret = -EINVAL;
625 }
626
627 return ret;
628}
629
630static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
631{
632 enum usb_device_state state = dwc->gadget.state;
633 u32 addr;
634 u32 reg;
635
636 addr = le16_to_cpu(ctrl->wValue);
637 if (addr > 127) {
638 dev_err(dwc->dev, "invalid device address %d\n", addr);
639 return -EINVAL;
640 }
641
642 if (state == USB_STATE_CONFIGURED) {
643 dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
644 return -EINVAL;
645 }
646
647 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
648 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
649 reg |= DWC3_DCFG_DEVADDR(addr);
650 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
651
652 if (addr) {
653 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
654 dwc3_report_sdp_charger(dwc);
655 } else {
656 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
657 }
658 return 0;
659}
660
661static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
662{
663 int ret;
664
665 spin_unlock(&dwc->lock);
666 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
667 spin_lock(&dwc->lock);
668 return ret;
669}
670
671static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
672{
673 enum usb_device_state state = dwc->gadget.state;
674 u32 cfg;
675 int ret;
676 u32 reg;
677
678 cfg = le16_to_cpu(ctrl->wValue);
679
680 switch (state) {
681 case USB_STATE_DEFAULT:
682 dev_err(dwc->dev, "err: set cfg on state default\n");
683 return -EINVAL;
684
685 case USB_STATE_ADDRESS:
686 ret = dwc3_ep0_delegate_req(dwc, ctrl);
687 /* if the cfg matches and the cfg is non zero */
688 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
689
690 /*
691 * only change state if set_config has already
692 * been processed. If gadget driver returns
693 * USB_GADGET_DELAYED_STATUS, we will wait
694 * to change the state on the next usb_ep_queue()
695 */
696 if (ret == 0)
697 usb_gadget_set_state(&dwc->gadget,
698 USB_STATE_CONFIGURED);
699
700 /*
701 * Enable transition to U1/U2 state when
702 * nothing is pending from application.
703 */
704 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
705 if (cpu_is_asr1901() || cpu_is_asr1906()) {
706 reg &= ~(DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
707 } else {
708 if (!dwc->dis_u1_entry_quirk)
709 reg |= DWC3_DCTL_ACCEPTU1ENA;
710 if (!dwc->dis_u2_entry_quirk)
711 reg |= DWC3_DCTL_ACCEPTU2ENA;
712 }
713 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
714 }
715 break;
716
717 case USB_STATE_CONFIGURED:
718 ret = dwc3_ep0_delegate_req(dwc, ctrl);
719 if (!cfg && !ret)
720 usb_gadget_set_state(&dwc->gadget,
721 USB_STATE_ADDRESS);
722 break;
723 default:
724 ret = -EINVAL;
725 }
726 return ret;
727}
728
729static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
730{
731 struct dwc3_ep *dep = to_dwc3_ep(ep);
732 struct dwc3 *dwc = dep->dwc;
733
734 u32 param = 0;
735 u32 reg;
736
737 struct timing {
738 u8 u1sel;
739 u8 u1pel;
740 __le16 u2sel;
741 __le16 u2pel;
742 } __packed timing;
743
744 int ret;
745
746 memcpy(&timing, req->buf, sizeof(timing));
747
748 dwc->u1sel = timing.u1sel;
749 dwc->u1pel = timing.u1pel;
750 dwc->u2sel = le16_to_cpu(timing.u2sel);
751 dwc->u2pel = le16_to_cpu(timing.u2pel);
752
753 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
754 if (reg & DWC3_DCTL_INITU2ENA)
755 param = dwc->u2pel;
756 if (reg & DWC3_DCTL_INITU1ENA)
757 param = dwc->u1pel;
758
759 /*
760 * According to Synopsys Databook, if parameter is
761 * greater than 125, a value of zero should be
762 * programmed in the register.
763 */
764 if (param > 125)
765 param = 0;
766
767 /* now that we have the time, issue DGCMD Set Sel */
768 ret = dwc3_send_gadget_generic_command(dwc,
769 DWC3_DGCMD_SET_PERIODIC_PAR, param);
770 WARN_ON(ret < 0);
771}
772
773static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
774{
775 struct dwc3_ep *dep;
776 enum usb_device_state state = dwc->gadget.state;
777 u16 wLength;
778
779 if (state == USB_STATE_DEFAULT) {
780 dev_err(dwc->dev, "usb err: set sel on state default\n");
781 return -EINVAL;
782 }
783
784 wLength = le16_to_cpu(ctrl->wLength);
785
786 if (wLength != 6) {
787 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
788 wLength);
789 return -EINVAL;
790 }
791
792 /*
793 * To handle Set SEL we need to receive 6 bytes from Host. So let's
794 * queue a usb_request for 6 bytes.
795 *
796 * Remember, though, this controller can't handle non-wMaxPacketSize
797 * aligned transfers on the OUT direction, so we queue a request for
798 * wMaxPacketSize instead.
799 */
800 dep = dwc->eps[0];
801 dwc->ep0_usb_req.dep = dep;
802 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
803 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
804 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
805
806 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
807}
808
809static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
810{
811 u16 wLength;
812 u16 wValue;
813 u16 wIndex;
814
815 wValue = le16_to_cpu(ctrl->wValue);
816 wLength = le16_to_cpu(ctrl->wLength);
817 wIndex = le16_to_cpu(ctrl->wIndex);
818
819 if (wIndex || wLength)
820 return -EINVAL;
821
822 dwc->gadget.isoch_delay = wValue;
823
824 return 0;
825}
826
827static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
828{
829 int ret;
830
831 switch (ctrl->bRequest) {
832 case USB_REQ_GET_STATUS:
833 ret = dwc3_ep0_handle_status(dwc, ctrl);
834 break;
835 case USB_REQ_CLEAR_FEATURE:
836 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
837 break;
838 case USB_REQ_SET_FEATURE:
839 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
840 break;
841 case USB_REQ_SET_ADDRESS:
842 ret = dwc3_ep0_set_address(dwc, ctrl);
843 break;
844 case USB_REQ_SET_CONFIGURATION:
845 ret = dwc3_ep0_set_config(dwc, ctrl);
846 break;
847 case USB_REQ_SET_SEL:
848 ret = dwc3_ep0_set_sel(dwc, ctrl);
849 break;
850 case USB_REQ_SET_ISOCH_DELAY:
851 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
852 break;
853 default:
854 ret = dwc3_ep0_delegate_req(dwc, ctrl);
855 break;
856 }
857
858 return ret;
859}
860
861static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
862 const struct dwc3_event_depevt *event)
863{
864 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
865 int ret = -EINVAL;
866 u32 len;
867
868 if (!dwc->gadget_driver) {
869 dev_err(dwc->dev, "dwc gadget_driver NULL\n");
870 goto out;
871 }
872
873 if (!dwc->vbus_active) {
874 dev_err(dwc->dev, "!!!inspect setup: vbus_active 0\n");
875 goto out;
876 }
877
878 trace_dwc3_ctrl_req(ctrl);
879
880 len = le16_to_cpu(ctrl->wLength);
881 if (!len) {
882 dwc->three_stage_setup = false;
883 dwc->ep0_expect_in = false;
884 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
885 } else {
886 dwc->three_stage_setup = true;
887 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
888 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
889 }
890
891 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
892 ret = dwc3_ep0_std_request(dwc, ctrl);
893 else
894 ret = dwc3_ep0_delegate_req(dwc, ctrl);
895
896 if (ret == USB_GADGET_DELAYED_STATUS)
897 dwc->delayed_status = true;
898
899out:
900 if (!dwc->vbus_active) {
901 dev_err(dwc->dev, "!!!dwc setup: exit\n");
902 return;
903 }
904
905 if (ret < 0) {
906#if defined(CONFIG_USB_ANDROID_DETECT_HOST_OS)
907 if (os_detect_is_done()) {
908 dev_info(dwc->dev, "os detect: reset ep0\n");
909 dwc3_reset_ep0(dwc);
910 return;
911 }
912#endif
913 dwc3_ep0_stall_and_restart(dwc);
914 }
915}
916
917static void dwc3_ep0_complete_data(struct dwc3 *dwc,
918 const struct dwc3_event_depevt *event)
919{
920 struct dwc3_request *r;
921 struct usb_request *ur;
922 struct dwc3_trb *trb;
923 struct dwc3_ep *ep0;
924 u32 transferred = 0;
925 u32 status;
926 u32 length;
927 u8 epnum;
928
929 epnum = event->endpoint_number;
930 ep0 = dwc->eps[0];
931
932 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
933 trb = dwc->ep0_trb;
934 trace_dwc3_complete_trb(ep0, trb);
935
936 r = next_request(&ep0->pending_list);
937 if (!r)
938 return;
939
940 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
941 if (status == DWC3_TRBSTS_SETUP_PENDING) {
942 dwc->setup_packet_pending = true;
943 if (r)
944 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
945 pr_info("ep0state: %d, DWC3_TRBSTS_SETUP_PENDING\n", dwc->ep0state);
946 return;
947 }
948
949 ur = &r->request;
950
951 length = trb->size & DWC3_TRB_SIZE_MASK;
952 transferred = ur->length - length;
953 ur->actual += transferred;
954
955 if ((IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
956 ur->length && ur->zero) || dwc->ep0_bounced) {
957 trb++;
958 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
959 trace_dwc3_complete_trb(ep0, trb);
960
961 if (r->direction)
962 dwc->eps[1]->trb_enqueue = 0;
963 else
964 dwc->eps[0]->trb_enqueue = 0;
965
966 dwc->ep0_bounced = false;
967 }
968
969 if ((epnum & 1) && ur->actual < ur->length)
970 dwc3_ep0_stall_and_restart(dwc);
971 else
972 dwc3_gadget_giveback(ep0, r, 0);
973}
974
975static void dwc3_ep0_complete_status(struct dwc3 *dwc,
976 const struct dwc3_event_depevt *event)
977{
978 struct dwc3_request *r;
979 struct dwc3_ep *dep;
980 struct dwc3_trb *trb;
981 u32 status;
982
983 dep = dwc->eps[0];
984 trb = dwc->ep0_trb;
985
986 trace_dwc3_complete_trb(dep, trb);
987
988 if (!list_empty(&dep->pending_list)) {
989 r = next_request(&dep->pending_list);
990
991 dwc3_gadget_giveback(dep, r, 0);
992 }
993
994 if (dwc->test_mode) {
995 int ret;
996
997 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
998 if (ret < 0) {
999 dev_err(dwc->dev, "invalid test #%d\n",
1000 dwc->test_mode_nr);
1001 dwc3_ep0_stall_and_restart(dwc);
1002 return;
1003 }
1004 }
1005
1006 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1007 if (status == DWC3_TRBSTS_SETUP_PENDING)
1008 dwc->setup_packet_pending = true;
1009
1010 dwc->ep0state = EP0_SETUP_PHASE;
1011 dwc3_ep0_out_start(dwc);
1012}
1013
1014static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
1015 const struct dwc3_event_depevt *event)
1016{
1017 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1018
1019 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1020 dep->resource_index = 0;
1021 dwc->setup_packet_pending = false;
1022
1023 switch (dwc->ep0state) {
1024 case EP0_SETUP_PHASE:
1025 dwc3_ep0_inspect_setup(dwc, event);
1026 break;
1027
1028 case EP0_DATA_PHASE:
1029 dwc3_ep0_complete_data(dwc, event);
1030 break;
1031
1032 case EP0_STATUS_PHASE:
1033 dwc3_ep0_complete_status(dwc, event);
1034 break;
1035 default:
1036 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
1037 }
1038}
1039
1040static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
1041 struct dwc3_ep *dep, struct dwc3_request *req)
1042{
1043 unsigned int trb_length = 0;
1044 int ret;
1045
1046 req->direction = !!dep->number;
1047
1048 if (req->request.length == 0) {
1049 if (!req->direction)
1050 trb_length = dep->endpoint.maxpacket;
1051
1052 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr, trb_length,
1053 DWC3_TRBCTL_CONTROL_DATA, false);
1054 ret = dwc3_ep0_start_trans(dep);
1055 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
1056 && (dep->number == 0)) {
1057 u32 maxpacket;
1058 u32 rem;
1059
1060 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1061 &req->request, dep->number);
1062 if (ret) {
1063 dev_err(dwc->dev, "%s map req failed\n", __func__);
1064 return;
1065 }
1066 maxpacket = dep->endpoint.maxpacket;
1067 rem = req->request.length % maxpacket;
1068 dwc->ep0_bounced = true;
1069
1070 /* prepare normal TRB */
1071 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1072 req->request.length,
1073 DWC3_TRBCTL_CONTROL_DATA,
1074 true);
1075
1076 req->trb = &dwc->ep0_trb[(dep->trb_enqueue - 1) & (DWC3_TRB_NUM - 1)];
1077
1078 /* Now prepare one extra TRB to align transfer size */
1079 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
1080 maxpacket - rem,
1081 DWC3_TRBCTL_CONTROL_DATA,
1082 false);
1083 ret = dwc3_ep0_start_trans(dep);
1084 } else if (IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
1085 req->request.length && req->request.zero) {
1086
1087 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1088 &req->request, dep->number);
1089 if (ret) {
1090 dev_err(dwc->dev, "%s map req failed,len: %d\n", __func__, req->request.length);
1091 return;
1092 }
1093 /* prepare normal TRB */
1094 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1095 req->request.length,
1096 DWC3_TRBCTL_CONTROL_DATA,
1097 true);
1098
1099 req->trb = &dwc->ep0_trb[(dep->trb_enqueue - 1) & (DWC3_TRB_NUM - 1)];
1100
1101 if (!req->direction)
1102 trb_length = dep->endpoint.maxpacket;
1103
1104 /* Now prepare one extra TRB to align transfer size */
1105 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
1106 trb_length, DWC3_TRBCTL_CONTROL_DATA,
1107 false);
1108 ret = dwc3_ep0_start_trans(dep);
1109 } else {
1110 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1111 &req->request, dep->number);
1112 if (ret) {
1113 dev_err(dwc->dev, "%s map req failed,len: %d\n", __func__, req->request.length);
1114 return;
1115 }
1116
1117 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1118 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1119 false);
1120
1121 req->trb = &dwc->ep0_trb[dep->trb_enqueue];
1122
1123 ret = dwc3_ep0_start_trans(dep);
1124 }
1125
1126 WARN_ON(ret < 0);
1127}
1128
1129static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
1130{
1131 struct dwc3 *dwc = dep->dwc;
1132 u32 type;
1133
1134 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1135 : DWC3_TRBCTL_CONTROL_STATUS2;
1136
1137 dwc3_ep0_prepare_one_trb(dep, dwc->ctrl_req_addr, 0, type, false);
1138 return dwc3_ep0_start_trans(dep);
1139}
1140
1141static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
1142{
1143 WARN_ON(dwc3_ep0_start_control_status(dep));
1144}
1145
1146static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1147 const struct dwc3_event_depevt *event)
1148{
1149 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1150
1151 if (unlikely(event->endpoint_number == dwc->ep0_expect_in)) {
1152 dev_err(dwc->dev, "error event: 0x%x\n", *((u32 *)event));
1153 dep = dwc->eps[!dwc->ep0_expect_in];
1154 }
1155
1156 __dwc3_ep0_do_control_status(dwc, dep);
1157}
1158
1159void dwc3_ep0_send_delayed_status(struct dwc3 *dwc)
1160{
1161 unsigned int direction = !dwc->ep0_expect_in;
1162
1163 dwc->delayed_status = false;
1164
1165 if (dwc->ep0state != EP0_STATUS_PHASE)
1166 return;
1167
1168 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
1169}
1170
1171static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1172{
1173 struct dwc3_gadget_ep_cmd_params params;
1174 u32 cmd;
1175 int ret;
1176
1177 if (!dep->resource_index) {
1178 dev_err(dwc->dev, "usb err: resource_index = 0\n");
1179 return;
1180 }
1181 cmd = DWC3_DEPCMD_ENDTRANSFER;
1182 cmd |= DWC3_DEPCMD_CMDIOC;
1183 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1184 memset(&params, 0, sizeof(params));
1185 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1186 WARN_ON_ONCE(ret);
1187 dep->resource_index = 0;
1188}
1189
1190static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1191 const struct dwc3_event_depevt *event)
1192{
1193 switch (event->status) {
1194 case DEPEVT_STATUS_CONTROL_DATA:
1195 /*
1196 * We already have a DATA transfer in the controller's cache,
1197 * if we receive a XferNotReady(DATA) we will ignore it, unless
1198 * it's for the wrong direction.
1199 *
1200 * In that case, we must issue END_TRANSFER command to the Data
1201 * Phase we already have started and issue SetStall on the
1202 * control endpoint.
1203 */
1204 if (dwc->ep0_expect_in != event->endpoint_number) {
1205 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1206
1207 dev_err(dwc->dev, "unexpected direction for Data Phase\n");
1208 dwc3_ep0_end_control_data(dwc, dep);
1209 dwc3_ep0_stall_and_restart(dwc);
1210 return;
1211 }
1212
1213 break;
1214
1215 case DEPEVT_STATUS_CONTROL_STATUS:
1216 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1217 return;
1218
1219 dwc->ep0state = EP0_STATUS_PHASE;
1220
1221 if (dwc->delayed_status) {
1222 struct dwc3_ep *dep = dwc->eps[0];
1223
1224 WARN_ON_ONCE(event->endpoint_number != 1);
1225 /*
1226 * We should handle the delay STATUS phase here if the
1227 * request for handling delay STATUS has been queued
1228 * into the list.
1229 */
1230 if (!list_empty(&dep->pending_list)) {
1231 dwc->delayed_status = false;
1232 usb_gadget_set_state(&dwc->gadget,
1233 USB_STATE_CONFIGURED);
1234 dwc3_ep0_do_control_status(dwc, event);
1235 }
1236
1237 return;
1238 }
1239
1240 dwc3_ep0_do_control_status(dwc, event);
1241 }
1242}
1243
1244void dwc3_ep0_interrupt(struct dwc3 *dwc,
1245 const struct dwc3_event_depevt *event)
1246{
1247 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1248 u8 cmd;
1249
1250 switch (event->endpoint_event) {
1251 case DWC3_DEPEVT_XFERCOMPLETE:
1252 dwc3_ep0_xfer_complete(dwc, event);
1253 break;
1254
1255 case DWC3_DEPEVT_XFERNOTREADY:
1256 dwc3_ep0_xfernotready(dwc, event);
1257 break;
1258
1259 case DWC3_DEPEVT_XFERINPROGRESS:
1260 case DWC3_DEPEVT_RXTXFIFOEVT:
1261 case DWC3_DEPEVT_STREAMEVT:
1262 break;
1263 case DWC3_DEPEVT_EPCMDCMPLT:
1264 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
1265
1266 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
1267 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
1268 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1269 }
1270 break;
1271 }
1272}