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