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