1
2
3
4
5
6
7
8
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
21#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
23#include <linux/usb/composite.h>
24
25#include "core.h"
26#include "debug.h"
27#include "gadget.h"
28#include "io.h"
29
30static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
31static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
32 struct dwc3_ep *dep, struct dwc3_request *req);
33
34static void dwc3_ep0_prepare_one_trb(struct dwc3_ep *dep,
35 dma_addr_t buf_dma, u32 len, u32 type, bool chain)
36{
37 struct dwc3_trb *trb;
38 struct dwc3 *dwc;
39
40 dwc = dep->dwc;
41 trb = &dwc->ep0_trb[dep->trb_enqueue];
42
43 if (chain)
44 dep->trb_enqueue++;
45
46 trb->bpl = lower_32_bits(buf_dma);
47 trb->bph = upper_32_bits(buf_dma);
48 trb->size = len;
49 trb->ctrl = type;
50
51 trb->ctrl |= (DWC3_TRB_CTRL_HWO
52 | DWC3_TRB_CTRL_ISP_IMI);
53
54 if (chain)
55 trb->ctrl |= DWC3_TRB_CTRL_CHN;
56 else
57 trb->ctrl |= (DWC3_TRB_CTRL_IOC
58 | DWC3_TRB_CTRL_LST);
59
60 trace_dwc3_prepare_trb(dep, trb);
61}
62
63static int dwc3_ep0_start_trans(struct dwc3_ep *dep)
64{
65 struct dwc3_gadget_ep_cmd_params params;
66 struct dwc3 *dwc;
67 int ret;
68
69 if (dep->flags & DWC3_EP_TRANSFER_STARTED)
70 return 0;
71
72 dwc = dep->dwc;
73
74 memset(¶ms, 0, sizeof(params));
75 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
76 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
77
78 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, ¶ms);
79 if (ret < 0)
80 return ret;
81
82 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
83
84 return 0;
85}
86
87static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
88 struct dwc3_request *req)
89{
90 struct dwc3 *dwc = dep->dwc;
91
92 req->request.actual = 0;
93 req->request.status = -EINPROGRESS;
94 req->epnum = dep->number;
95
96 list_add_tail(&req->list, &dep->pending_list);
97
98
99
100
101
102
103
104
105
106
107 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
108 unsigned int direction;
109
110 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
111
112 if (dwc->ep0state != EP0_DATA_PHASE) {
113 dev_WARN(dwc->dev, "Unexpected pending request\n");
114 return 0;
115 }
116
117 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
118
119 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
120 DWC3_EP0_DIR_IN);
121
122 return 0;
123 }
124
125
126
127
128
129 if (dwc->delayed_status) {
130 unsigned int direction;
131
132 direction = !dwc->ep0_expect_in;
133 dwc->delayed_status = false;
134 usb_gadget_set_state(dwc->gadget, USB_STATE_CONFIGURED);
135
136 if (dwc->ep0state == EP0_STATUS_PHASE)
137 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
138
139 return 0;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 if (dwc->three_stage_setup) {
175 unsigned int direction;
176
177 direction = dwc->ep0_expect_in;
178 dwc->ep0state = EP0_DATA_PHASE;
179
180 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
181
182 dep->flags &= ~DWC3_EP0_DIR_IN;
183 }
184
185 return 0;
186}
187
188int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
189 gfp_t gfp_flags)
190{
191 struct dwc3_request *req = to_dwc3_request(request);
192 struct dwc3_ep *dep = to_dwc3_ep(ep);
193 struct dwc3 *dwc = dep->dwc;
194
195 unsigned long flags;
196
197 int ret;
198
199 spin_lock_irqsave(&dwc->lock, flags);
200 if (!dep->endpoint.desc || !dwc->pullups_connected) {
201 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
202 dep->name);
203 ret = -ESHUTDOWN;
204 goto out;
205 }
206
207
208 if (!list_empty(&dep->pending_list)) {
209 ret = -EBUSY;
210 goto out;
211 }
212
213 ret = __dwc3_gadget_ep0_queue(dep, req);
214
215out:
216 spin_unlock_irqrestore(&dwc->lock, flags);
217
218 return ret;
219}
220
221static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
222{
223 struct dwc3_ep *dep;
224
225
226 dep = dwc->eps[1];
227 dep->flags = DWC3_EP_ENABLED;
228
229
230 dep = dwc->eps[0];
231 __dwc3_gadget_ep_set_halt(dep, 1, false);
232 dep->flags = DWC3_EP_ENABLED;
233 dwc->delayed_status = false;
234
235 if (!list_empty(&dep->pending_list)) {
236 struct dwc3_request *req;
237
238 req = next_request(&dep->pending_list);
239 dwc3_gadget_giveback(dep, req, -ECONNRESET);
240 }
241
242 dwc->ep0state = EP0_SETUP_PHASE;
243 dwc3_ep0_out_start(dwc);
244}
245
246int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
247{
248 struct dwc3_ep *dep = to_dwc3_ep(ep);
249 struct dwc3 *dwc = dep->dwc;
250
251 dwc3_ep0_stall_and_restart(dwc);
252
253 return 0;
254}
255
256int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
257{
258 struct dwc3_ep *dep = to_dwc3_ep(ep);
259 struct dwc3 *dwc = dep->dwc;
260 unsigned long flags;
261 int ret;
262
263 spin_lock_irqsave(&dwc->lock, flags);
264 ret = __dwc3_gadget_ep0_set_halt(ep, value);
265 spin_unlock_irqrestore(&dwc->lock, flags);
266
267 return ret;
268}
269
270void dwc3_ep0_out_start(struct dwc3 *dwc)
271{
272 struct dwc3_ep *dep;
273 int ret;
274
275 complete(&dwc->ep0_in_setup);
276
277 dep = dwc->eps[0];
278 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 8,
279 DWC3_TRBCTL_CONTROL_SETUP, false);
280 ret = dwc3_ep0_start_trans(dep);
281 WARN_ON(ret < 0);
282}
283
284static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
285{
286 struct dwc3_ep *dep;
287 u32 windex = le16_to_cpu(wIndex_le);
288 u32 epnum;
289
290 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
291 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
292 epnum |= 1;
293
294 dep = dwc->eps[epnum];
295 if (dep->flags & DWC3_EP_ENABLED)
296 return dep;
297
298 return NULL;
299}
300
301static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
302{
303}
304
305
306
307static int dwc3_ep0_handle_status(struct dwc3 *dwc,
308 struct usb_ctrlrequest *ctrl)
309{
310 struct dwc3_ep *dep;
311 u32 recip;
312 u32 value;
313 u32 reg;
314 u16 usb_status = 0;
315 __le16 *response_pkt;
316
317
318 value = le16_to_cpu(ctrl->wValue);
319 if (value != 0)
320 return -EINVAL;
321
322 recip = ctrl->bRequestType & USB_RECIP_MASK;
323 switch (recip) {
324 case USB_RECIP_DEVICE:
325
326
327
328 usb_status |= dwc->gadget->is_selfpowered;
329
330 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
331 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
332 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
333 if (reg & DWC3_DCTL_INITU1ENA)
334 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
335 if (reg & DWC3_DCTL_INITU2ENA)
336 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
337 }
338
339
340
341
342 usb_status |= dwc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
343
344 break;
345
346 case USB_RECIP_INTERFACE:
347
348
349
350
351 break;
352
353 case USB_RECIP_ENDPOINT:
354 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
355 if (!dep)
356 return -EINVAL;
357
358 if (dep->flags & DWC3_EP_STALL)
359 usb_status = 1 << USB_ENDPOINT_HALT;
360 break;
361 default:
362 return -EINVAL;
363 }
364
365 response_pkt = (__le16 *) dwc->setup_buf;
366 *response_pkt = cpu_to_le16(usb_status);
367
368 dep = dwc->eps[0];
369 dwc->ep0_usb_req.dep = dep;
370 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
371 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
372 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
373
374 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
375}
376
377static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
378 int set)
379{
380 u32 reg;
381
382 if (state != USB_STATE_CONFIGURED)
383 return -EINVAL;
384 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
385 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
386 return -EINVAL;
387 if (set && dwc->dis_u1_entry_quirk)
388 return -EINVAL;
389
390 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
391 if (set)
392 reg |= DWC3_DCTL_INITU1ENA;
393 else
394 reg &= ~DWC3_DCTL_INITU1ENA;
395 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
396
397 return 0;
398}
399
400static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
401 int set)
402{
403 u32 reg;
404
405
406 if (state != USB_STATE_CONFIGURED)
407 return -EINVAL;
408 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
409 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
410 return -EINVAL;
411 if (set && dwc->dis_u2_entry_quirk)
412 return -EINVAL;
413
414 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
415 if (set)
416 reg |= DWC3_DCTL_INITU2ENA;
417 else
418 reg &= ~DWC3_DCTL_INITU2ENA;
419 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
420
421 return 0;
422}
423
424static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
425 u32 wIndex, int set)
426{
427 if ((wIndex & 0xff) != 0)
428 return -EINVAL;
429 if (!set)
430 return -EINVAL;
431
432 switch (wIndex >> 8) {
433 case USB_TEST_J:
434 case USB_TEST_K:
435 case USB_TEST_SE0_NAK:
436 case USB_TEST_PACKET:
437 case USB_TEST_FORCE_ENABLE:
438 dwc->test_mode_nr = wIndex >> 8;
439 dwc->test_mode = true;
440 break;
441 default:
442 return -EINVAL;
443 }
444
445 return 0;
446}
447
448static int dwc3_ep0_handle_device(struct dwc3 *dwc,
449 struct usb_ctrlrequest *ctrl, int set)
450{
451 enum usb_device_state state;
452 u32 wValue;
453 u32 wIndex;
454 int ret = 0;
455
456 wValue = le16_to_cpu(ctrl->wValue);
457 wIndex = le16_to_cpu(ctrl->wIndex);
458 state = dwc->gadget->state;
459
460 switch (wValue) {
461 case USB_DEVICE_REMOTE_WAKEUP:
462 if (set)
463 dwc->remote_wakeup = 1;
464 else
465 dwc->remote_wakeup = 0;
466
467 break;
468
469
470
471
472 case USB_DEVICE_U1_ENABLE:
473 ret = dwc3_ep0_handle_u1(dwc, state, set);
474 break;
475 case USB_DEVICE_U2_ENABLE:
476 ret = dwc3_ep0_handle_u2(dwc, state, set);
477 break;
478 case USB_DEVICE_LTM_ENABLE:
479 ret = -EINVAL;
480 break;
481 case USB_DEVICE_TEST_MODE:
482 ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
483 break;
484 case USB_DEVICE_B_HNP_ENABLE:
485 if (set) {
486 if (dwc->gadget->host_request_flag) {
487 struct usb_phy *phy =
488 usb_get_phy(USB_PHY_TYPE_USB3);
489
490 dwc->gadget->b_hnp_enable = 0;
491 dwc->gadget->host_request_flag = 0;
492 otg_start_hnp(phy->otg);
493 usb_put_phy(phy);
494 } else {
495 dwc->gadget->b_hnp_enable = 1;
496 }
497 } else
498 return -EINVAL;
499 break;
500
501 case USB_DEVICE_A_HNP_SUPPORT:
502
503 dev_dbg(dwc->dev,
504 "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
505 break;
506
507 case USB_DEVICE_A_ALT_HNP_SUPPORT:
508
509 dev_dbg(dwc->dev,
510 "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
511 break;
512 default:
513 ret = -EINVAL;
514 }
515
516 return ret;
517}
518
519static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
520 struct usb_ctrlrequest *ctrl, int set)
521{
522 u32 wValue;
523 int ret = 0;
524
525 wValue = le16_to_cpu(ctrl->wValue);
526
527 switch (wValue) {
528 case USB_INTRF_FUNC_SUSPEND:
529
530
531
532
533
534
535
536 break;
537 default:
538 ret = -EINVAL;
539 }
540
541 return ret;
542}
543
544static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
545 struct usb_ctrlrequest *ctrl, int set)
546{
547 struct dwc3_ep *dep;
548 u32 wValue;
549 int ret;
550
551 wValue = le16_to_cpu(ctrl->wValue);
552
553 switch (wValue) {
554 case USB_ENDPOINT_HALT:
555 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
556 if (!dep)
557 return -EINVAL;
558
559 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
560 break;
561
562 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
563 if (ret)
564 return -EINVAL;
565
566
567 if (!set && (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
568 return USB_GADGET_DELAYED_STATUS;
569
570 break;
571 default:
572 return -EINVAL;
573 }
574
575 return 0;
576}
577
578static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
579 struct usb_ctrlrequest *ctrl, int set)
580{
581 u32 recip;
582 int ret;
583
584 recip = ctrl->bRequestType & USB_RECIP_MASK;
585
586 switch (recip) {
587 case USB_RECIP_DEVICE:
588 ret = dwc3_ep0_handle_device(dwc, ctrl, set);
589 break;
590 case USB_RECIP_INTERFACE:
591 ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
592 break;
593 case USB_RECIP_ENDPOINT:
594 ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
595 break;
596 default:
597 ret = -EINVAL;
598 }
599
600 return ret;
601}
602
603static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
604{
605 enum usb_device_state state = dwc->gadget->state;
606 u32 addr;
607 u32 reg;
608
609 addr = le16_to_cpu(ctrl->wValue);
610 if (addr > 127) {
611 dev_err(dwc->dev, "invalid device address %d\n", addr);
612 return -EINVAL;
613 }
614
615 if (state == USB_STATE_CONFIGURED) {
616 dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
617 return -EINVAL;
618 }
619
620 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
621 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
622 reg |= DWC3_DCFG_DEVADDR(addr);
623 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
624
625 if (addr)
626 usb_gadget_set_state(dwc->gadget, USB_STATE_ADDRESS);
627 else
628 usb_gadget_set_state(dwc->gadget, USB_STATE_DEFAULT);
629
630 return 0;
631}
632
633static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
634{
635 int ret;
636
637 spin_unlock(&dwc->lock);
638 ret = dwc->gadget_driver->setup(dwc->gadget, ctrl);
639 spin_lock(&dwc->lock);
640 return ret;
641}
642
643static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
644{
645 enum usb_device_state state = dwc->gadget->state;
646 u32 cfg;
647 int ret;
648 u32 reg;
649
650 cfg = le16_to_cpu(ctrl->wValue);
651
652 switch (state) {
653 case USB_STATE_DEFAULT:
654 return -EINVAL;
655
656 case USB_STATE_ADDRESS:
657 ret = dwc3_ep0_delegate_req(dwc, ctrl);
658
659 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
660
661
662
663
664
665
666
667 if (ret == 0)
668 usb_gadget_set_state(dwc->gadget,
669 USB_STATE_CONFIGURED);
670
671
672
673
674
675 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
676 if (!dwc->dis_u1_entry_quirk)
677 reg |= DWC3_DCTL_ACCEPTU1ENA;
678 if (!dwc->dis_u2_entry_quirk)
679 reg |= DWC3_DCTL_ACCEPTU2ENA;
680 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
681 }
682 break;
683
684 case USB_STATE_CONFIGURED:
685 ret = dwc3_ep0_delegate_req(dwc, ctrl);
686 if (!cfg && !ret)
687 usb_gadget_set_state(dwc->gadget,
688 USB_STATE_ADDRESS);
689 break;
690 default:
691 ret = -EINVAL;
692 }
693 return ret;
694}
695
696static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
697{
698 struct dwc3_ep *dep = to_dwc3_ep(ep);
699 struct dwc3 *dwc = dep->dwc;
700
701 u32 param = 0;
702 u32 reg;
703
704 struct timing {
705 u8 u1sel;
706 u8 u1pel;
707 __le16 u2sel;
708 __le16 u2pel;
709 } __packed timing;
710
711 int ret;
712
713 memcpy(&timing, req->buf, sizeof(timing));
714
715 dwc->u1sel = timing.u1sel;
716 dwc->u1pel = timing.u1pel;
717 dwc->u2sel = le16_to_cpu(timing.u2sel);
718 dwc->u2pel = le16_to_cpu(timing.u2pel);
719
720 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
721 if (reg & DWC3_DCTL_INITU2ENA)
722 param = dwc->u2pel;
723 if (reg & DWC3_DCTL_INITU1ENA)
724 param = dwc->u1pel;
725
726
727
728
729
730
731 if (param > 125)
732 param = 0;
733
734
735 ret = dwc3_send_gadget_generic_command(dwc,
736 DWC3_DGCMD_SET_PERIODIC_PAR, param);
737 WARN_ON(ret < 0);
738}
739
740static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
741{
742 struct dwc3_ep *dep;
743 enum usb_device_state state = dwc->gadget->state;
744 u16 wLength;
745
746 if (state == USB_STATE_DEFAULT)
747 return -EINVAL;
748
749 wLength = le16_to_cpu(ctrl->wLength);
750
751 if (wLength != 6) {
752 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
753 wLength);
754 return -EINVAL;
755 }
756
757
758
759
760
761
762
763
764
765 dep = dwc->eps[0];
766 dwc->ep0_usb_req.dep = dep;
767 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
768 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
769 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
770
771 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
772}
773
774static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
775{
776 u16 wLength;
777 u16 wValue;
778 u16 wIndex;
779
780 wValue = le16_to_cpu(ctrl->wValue);
781 wLength = le16_to_cpu(ctrl->wLength);
782 wIndex = le16_to_cpu(ctrl->wIndex);
783
784 if (wIndex || wLength)
785 return -EINVAL;
786
787 dwc->gadget->isoch_delay = wValue;
788
789 return 0;
790}
791
792static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
793{
794 int ret;
795
796 switch (ctrl->bRequest) {
797 case USB_REQ_GET_STATUS:
798 if (le16_to_cpu(ctrl->wIndex) == OTG_STS_SELECTOR)
799 ret = dwc3_ep0_delegate_req(dwc, ctrl);
800 else
801 ret = dwc3_ep0_handle_status(dwc, ctrl);
802 break;
803 case USB_REQ_CLEAR_FEATURE:
804 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
805 break;
806 case USB_REQ_SET_FEATURE:
807 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
808 break;
809 case USB_REQ_SET_ADDRESS:
810 ret = dwc3_ep0_set_address(dwc, ctrl);
811 break;
812 case USB_REQ_SET_CONFIGURATION:
813 ret = dwc3_ep0_set_config(dwc, ctrl);
814 break;
815 case USB_REQ_SET_SEL:
816 ret = dwc3_ep0_set_sel(dwc, ctrl);
817 break;
818 case USB_REQ_SET_ISOCH_DELAY:
819 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
820 break;
821 default:
822 ret = dwc3_ep0_delegate_req(dwc, ctrl);
823 break;
824 }
825
826 return ret;
827}
828
829static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
830 const struct dwc3_event_depevt *event)
831{
832 struct usb_ctrlrequest *ctrl = (void *) dwc->ep0_trb;
833 int ret = -EINVAL;
834 u32 len;
835
836 if (!dwc->gadget_driver)
837 goto out;
838
839 trace_dwc3_ctrl_req(ctrl);
840
841 len = le16_to_cpu(ctrl->wLength);
842 if (!len) {
843 dwc->three_stage_setup = false;
844 dwc->ep0_expect_in = false;
845 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
846 } else {
847 dwc->three_stage_setup = true;
848 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
849 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
850 }
851
852 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
853 ret = dwc3_ep0_std_request(dwc, ctrl);
854 else
855 ret = dwc3_ep0_delegate_req(dwc, ctrl);
856
857 if (ret == USB_GADGET_DELAYED_STATUS)
858 dwc->delayed_status = true;
859
860out:
861 if (ret < 0)
862 dwc3_ep0_stall_and_restart(dwc);
863}
864
865static void dwc3_ep0_complete_data(struct dwc3 *dwc,
866 const struct dwc3_event_depevt *event)
867{
868 struct dwc3_request *r;
869 struct usb_request *ur;
870 struct dwc3_trb *trb;
871 struct dwc3_ep *ep0;
872 u32 transferred = 0;
873 u32 status;
874 u32 length;
875 u8 epnum;
876
877 epnum = event->endpoint_number;
878 ep0 = dwc->eps[0];
879
880 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
881 trb = dwc->ep0_trb;
882 trace_dwc3_complete_trb(ep0, trb);
883
884 r = next_request(&ep0->pending_list);
885 if (!r)
886 return;
887
888 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
889 if (status == DWC3_TRBSTS_SETUP_PENDING) {
890 dwc->setup_packet_pending = true;
891 if (r)
892 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
893
894 return;
895 }
896
897 ur = &r->request;
898
899 length = trb->size & DWC3_TRB_SIZE_MASK;
900 transferred = ur->length - length;
901 ur->actual += transferred;
902
903 if ((IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
904 ur->length && ur->zero) || dwc->ep0_bounced) {
905 trb++;
906 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
907 trace_dwc3_complete_trb(ep0, trb);
908
909 if (r->direction)
910 dwc->eps[1]->trb_enqueue = 0;
911 else
912 dwc->eps[0]->trb_enqueue = 0;
913
914 dwc->ep0_bounced = false;
915 }
916
917 if ((epnum & 1) && ur->actual < ur->length)
918 dwc3_ep0_stall_and_restart(dwc);
919 else
920 dwc3_gadget_giveback(ep0, r, 0);
921}
922
923static void dwc3_ep0_complete_status(struct dwc3 *dwc,
924 const struct dwc3_event_depevt *event)
925{
926 struct dwc3_request *r;
927 struct dwc3_ep *dep;
928 struct dwc3_trb *trb;
929 u32 status;
930
931 dep = dwc->eps[0];
932 trb = dwc->ep0_trb;
933
934 trace_dwc3_complete_trb(dep, trb);
935
936 if (!list_empty(&dep->pending_list)) {
937 r = next_request(&dep->pending_list);
938
939 dwc3_gadget_giveback(dep, r, 0);
940 }
941
942 if (dwc->test_mode) {
943 int ret;
944
945 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
946 if (ret < 0) {
947 dev_err(dwc->dev, "invalid test #%d\n",
948 dwc->test_mode_nr);
949 dwc3_ep0_stall_and_restart(dwc);
950 return;
951 }
952 }
953
954 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
955 if (status == DWC3_TRBSTS_SETUP_PENDING)
956 dwc->setup_packet_pending = true;
957
958 dwc->ep0state = EP0_SETUP_PHASE;
959 dwc3_ep0_out_start(dwc);
960}
961
962static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
963 const struct dwc3_event_depevt *event)
964{
965 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
966
967 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
968 dep->resource_index = 0;
969 dwc->setup_packet_pending = false;
970
971 switch (dwc->ep0state) {
972 case EP0_SETUP_PHASE:
973 dwc3_ep0_inspect_setup(dwc, event);
974 break;
975
976 case EP0_DATA_PHASE:
977 dwc3_ep0_complete_data(dwc, event);
978 break;
979
980 case EP0_STATUS_PHASE:
981 dwc3_ep0_complete_status(dwc, event);
982 break;
983 default:
984 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
985 }
986}
987
988static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
989 struct dwc3_ep *dep, struct dwc3_request *req)
990{
991 unsigned int trb_length = 0;
992 int ret;
993
994 req->direction = !!dep->number;
995
996 if (req->request.length == 0) {
997 if (!req->direction)
998 trb_length = dep->endpoint.maxpacket;
999
1000 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr, trb_length,
1001 DWC3_TRBCTL_CONTROL_DATA, false);
1002 ret = dwc3_ep0_start_trans(dep);
1003 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
1004 && (dep->number == 0)) {
1005 u32 maxpacket;
1006 u32 rem;
1007
1008 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1009 &req->request, dep->number);
1010 if (ret)
1011 return;
1012
1013 maxpacket = dep->endpoint.maxpacket;
1014 rem = req->request.length % maxpacket;
1015 dwc->ep0_bounced = true;
1016
1017
1018 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1019 req->request.length,
1020 DWC3_TRBCTL_CONTROL_DATA,
1021 true);
1022
1023 req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
1024
1025
1026 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
1027 maxpacket - rem,
1028 DWC3_TRBCTL_CONTROL_DATA,
1029 false);
1030 ret = dwc3_ep0_start_trans(dep);
1031 } else if (IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
1032 req->request.length && req->request.zero) {
1033
1034 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1035 &req->request, dep->number);
1036 if (ret)
1037 return;
1038
1039
1040 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1041 req->request.length,
1042 DWC3_TRBCTL_CONTROL_DATA,
1043 true);
1044
1045 req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
1046
1047 if (!req->direction)
1048 trb_length = dep->endpoint.maxpacket;
1049
1050
1051 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
1052 trb_length, DWC3_TRBCTL_CONTROL_DATA,
1053 false);
1054 ret = dwc3_ep0_start_trans(dep);
1055 } else {
1056 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1057 &req->request, dep->number);
1058 if (ret)
1059 return;
1060
1061 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1062 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1063 false);
1064
1065 req->trb = &dwc->ep0_trb[dep->trb_enqueue];
1066
1067 ret = dwc3_ep0_start_trans(dep);
1068 }
1069
1070 WARN_ON(ret < 0);
1071}
1072
1073static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
1074{
1075 struct dwc3 *dwc = dep->dwc;
1076 u32 type;
1077
1078 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1079 : DWC3_TRBCTL_CONTROL_STATUS2;
1080
1081 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 0, type, false);
1082 return dwc3_ep0_start_trans(dep);
1083}
1084
1085static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
1086{
1087 WARN_ON(dwc3_ep0_start_control_status(dep));
1088}
1089
1090static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1091 const struct dwc3_event_depevt *event)
1092{
1093 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1094
1095 __dwc3_ep0_do_control_status(dwc, dep);
1096}
1097
1098void dwc3_ep0_send_delayed_status(struct dwc3 *dwc)
1099{
1100 unsigned int direction = !dwc->ep0_expect_in;
1101
1102 dwc->delayed_status = false;
1103
1104 if (dwc->ep0state != EP0_STATUS_PHASE)
1105 return;
1106
1107 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
1108}
1109
1110static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1111{
1112 struct dwc3_gadget_ep_cmd_params params;
1113 u32 cmd;
1114 int ret;
1115
1116 if (!dep->resource_index)
1117 return;
1118
1119 cmd = DWC3_DEPCMD_ENDTRANSFER;
1120 cmd |= DWC3_DEPCMD_CMDIOC;
1121 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1122 memset(¶ms, 0, sizeof(params));
1123 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1124 WARN_ON_ONCE(ret);
1125 dep->resource_index = 0;
1126}
1127
1128static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1129 const struct dwc3_event_depevt *event)
1130{
1131 switch (event->status) {
1132 case DEPEVT_STATUS_CONTROL_DATA:
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142 if (dwc->ep0_expect_in != event->endpoint_number) {
1143 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1144
1145 dev_err(dwc->dev, "unexpected direction for Data Phase\n");
1146 dwc3_ep0_end_control_data(dwc, dep);
1147 dwc3_ep0_stall_and_restart(dwc);
1148 return;
1149 }
1150
1151 break;
1152
1153 case DEPEVT_STATUS_CONTROL_STATUS:
1154 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1155 return;
1156
1157 dwc->ep0state = EP0_STATUS_PHASE;
1158
1159 if (dwc->delayed_status) {
1160 struct dwc3_ep *dep = dwc->eps[0];
1161
1162 WARN_ON_ONCE(event->endpoint_number != 1);
1163
1164
1165
1166
1167
1168 if (!list_empty(&dep->pending_list)) {
1169 dwc->delayed_status = false;
1170 usb_gadget_set_state(dwc->gadget,
1171 USB_STATE_CONFIGURED);
1172 dwc3_ep0_do_control_status(dwc, event);
1173 }
1174
1175 return;
1176 }
1177
1178 dwc3_ep0_do_control_status(dwc, event);
1179 }
1180}
1181
1182void dwc3_ep0_interrupt(struct dwc3 *dwc,
1183 const struct dwc3_event_depevt *event)
1184{
1185 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1186 u8 cmd;
1187
1188 switch (event->endpoint_event) {
1189 case DWC3_DEPEVT_XFERCOMPLETE:
1190 dwc3_ep0_xfer_complete(dwc, event);
1191 break;
1192
1193 case DWC3_DEPEVT_XFERNOTREADY:
1194 dwc3_ep0_xfernotready(dwc, event);
1195 break;
1196
1197 case DWC3_DEPEVT_XFERINPROGRESS:
1198 case DWC3_DEPEVT_RXTXFIFOEVT:
1199 case DWC3_DEPEVT_STREAMEVT:
1200 break;
1201 case DWC3_DEPEVT_EPCMDCMPLT:
1202 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
1203
1204 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
1205 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
1206 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1207 }
1208 break;
1209 }
1210}
1211