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