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