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