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