xref: /openbmc/linux/drivers/usb/mtu3/mtu3_gadget.c (revision 0ad53fe3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mtu3_gadget.c - MediaTek usb3 DRD peripheral support
4  *
5  * Copyright (C) 2016 MediaTek Inc.
6  *
7  * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
8  */
9 
10 #include "mtu3.h"
11 #include "mtu3_trace.h"
12 
13 void mtu3_req_complete(struct mtu3_ep *mep,
14 		     struct usb_request *req, int status)
15 __releases(mep->mtu->lock)
16 __acquires(mep->mtu->lock)
17 {
18 	struct mtu3_request *mreq = to_mtu3_request(req);
19 	struct mtu3 *mtu = mreq->mtu;
20 
21 	list_del(&mreq->list);
22 	if (req->status == -EINPROGRESS)
23 		req->status = status;
24 
25 	trace_mtu3_req_complete(mreq);
26 	spin_unlock(&mtu->lock);
27 
28 	/* ep0 makes use of PIO, needn't unmap it */
29 	if (mep->epnum)
30 		usb_gadget_unmap_request(&mtu->g, req, mep->is_in);
31 
32 	dev_dbg(mtu->dev, "%s complete req: %p, sts %d, %d/%d\n",
33 		mep->name, req, req->status, req->actual, req->length);
34 
35 	usb_gadget_giveback_request(&mep->ep, req);
36 	spin_lock(&mtu->lock);
37 }
38 
39 static void nuke(struct mtu3_ep *mep, const int status)
40 {
41 	struct mtu3_request *mreq = NULL;
42 
43 	if (list_empty(&mep->req_list))
44 		return;
45 
46 	dev_dbg(mep->mtu->dev, "abort %s's req: sts %d\n", mep->name, status);
47 
48 	/* exclude EP0 */
49 	if (mep->epnum)
50 		mtu3_qmu_flush(mep);
51 
52 	while (!list_empty(&mep->req_list)) {
53 		mreq = list_first_entry(&mep->req_list,
54 					struct mtu3_request, list);
55 		mtu3_req_complete(mep, &mreq->request, status);
56 	}
57 }
58 
59 static int mtu3_ep_enable(struct mtu3_ep *mep)
60 {
61 	const struct usb_endpoint_descriptor *desc;
62 	const struct usb_ss_ep_comp_descriptor *comp_desc;
63 	struct mtu3 *mtu = mep->mtu;
64 	u32 interval = 0;
65 	u32 mult = 0;
66 	u32 burst = 0;
67 	int ret;
68 
69 	desc = mep->desc;
70 	comp_desc = mep->comp_desc;
71 	mep->type = usb_endpoint_type(desc);
72 	mep->maxp = usb_endpoint_maxp(desc);
73 
74 	switch (mtu->g.speed) {
75 	case USB_SPEED_SUPER:
76 	case USB_SPEED_SUPER_PLUS:
77 		if (usb_endpoint_xfer_int(desc) ||
78 				usb_endpoint_xfer_isoc(desc)) {
79 			interval = desc->bInterval;
80 			interval = clamp_val(interval, 1, 16) - 1;
81 			if (usb_endpoint_xfer_isoc(desc) && comp_desc)
82 				mult = comp_desc->bmAttributes;
83 		}
84 		if (comp_desc)
85 			burst = comp_desc->bMaxBurst;
86 
87 		break;
88 	case USB_SPEED_HIGH:
89 		if (usb_endpoint_xfer_isoc(desc) ||
90 				usb_endpoint_xfer_int(desc)) {
91 			interval = desc->bInterval;
92 			interval = clamp_val(interval, 1, 16) - 1;
93 			mult = usb_endpoint_maxp_mult(desc) - 1;
94 		}
95 		break;
96 	default:
97 		break; /*others are ignored */
98 	}
99 
100 	dev_dbg(mtu->dev, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n",
101 		__func__, mep->maxp, interval, burst, mult);
102 
103 	mep->ep.maxpacket = mep->maxp;
104 	mep->ep.desc = desc;
105 	mep->ep.comp_desc = comp_desc;
106 
107 	/* slot mainly affects bulk/isoc transfer, so ignore int */
108 	mep->slot = usb_endpoint_xfer_int(desc) ? 0 : mtu->slot;
109 
110 	ret = mtu3_config_ep(mtu, mep, interval, burst, mult);
111 	if (ret < 0)
112 		return ret;
113 
114 	ret = mtu3_gpd_ring_alloc(mep);
115 	if (ret < 0) {
116 		mtu3_deconfig_ep(mtu, mep);
117 		return ret;
118 	}
119 
120 	mtu3_qmu_start(mep);
121 
122 	return 0;
123 }
124 
125 static int mtu3_ep_disable(struct mtu3_ep *mep)
126 {
127 	struct mtu3 *mtu = mep->mtu;
128 
129 	mtu3_qmu_stop(mep);
130 
131 	/* abort all pending requests */
132 	nuke(mep, -ESHUTDOWN);
133 	mtu3_deconfig_ep(mtu, mep);
134 	mtu3_gpd_ring_free(mep);
135 
136 	mep->desc = NULL;
137 	mep->ep.desc = NULL;
138 	mep->comp_desc = NULL;
139 	mep->type = 0;
140 	mep->flags = 0;
141 
142 	return 0;
143 }
144 
145 static int mtu3_gadget_ep_enable(struct usb_ep *ep,
146 		const struct usb_endpoint_descriptor *desc)
147 {
148 	struct mtu3_ep *mep;
149 	struct mtu3 *mtu;
150 	unsigned long flags;
151 	int ret = -EINVAL;
152 
153 	if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
154 		pr_debug("%s invalid parameters\n", __func__);
155 		return -EINVAL;
156 	}
157 
158 	if (!desc->wMaxPacketSize) {
159 		pr_debug("%s missing wMaxPacketSize\n", __func__);
160 		return -EINVAL;
161 	}
162 	mep = to_mtu3_ep(ep);
163 	mtu = mep->mtu;
164 
165 	/* check ep number and direction against endpoint */
166 	if (usb_endpoint_num(desc) != mep->epnum)
167 		return -EINVAL;
168 
169 	if (!!usb_endpoint_dir_in(desc) ^ !!mep->is_in)
170 		return -EINVAL;
171 
172 	dev_dbg(mtu->dev, "%s %s\n", __func__, ep->name);
173 
174 	if (mep->flags & MTU3_EP_ENABLED) {
175 		dev_WARN_ONCE(mtu->dev, true, "%s is already enabled\n",
176 				mep->name);
177 		return 0;
178 	}
179 
180 	spin_lock_irqsave(&mtu->lock, flags);
181 	mep->desc = desc;
182 	mep->comp_desc = ep->comp_desc;
183 
184 	ret = mtu3_ep_enable(mep);
185 	if (ret)
186 		goto error;
187 
188 	mep->flags = MTU3_EP_ENABLED;
189 	mtu->active_ep++;
190 
191 error:
192 	spin_unlock_irqrestore(&mtu->lock, flags);
193 
194 	dev_dbg(mtu->dev, "%s active_ep=%d\n", __func__, mtu->active_ep);
195 	trace_mtu3_gadget_ep_enable(mep);
196 
197 	return ret;
198 }
199 
200 static int mtu3_gadget_ep_disable(struct usb_ep *ep)
201 {
202 	struct mtu3_ep *mep = to_mtu3_ep(ep);
203 	struct mtu3 *mtu = mep->mtu;
204 	unsigned long flags;
205 
206 	dev_dbg(mtu->dev, "%s %s\n", __func__, mep->name);
207 	trace_mtu3_gadget_ep_disable(mep);
208 
209 	if (!(mep->flags & MTU3_EP_ENABLED)) {
210 		dev_warn(mtu->dev, "%s is already disabled\n", mep->name);
211 		return 0;
212 	}
213 
214 	spin_lock_irqsave(&mtu->lock, flags);
215 	mtu3_ep_disable(mep);
216 	mep->flags = 0;
217 	mtu->active_ep--;
218 	spin_unlock_irqrestore(&(mtu->lock), flags);
219 
220 	dev_dbg(mtu->dev, "%s active_ep=%d, mtu3 is_active=%d\n",
221 		__func__, mtu->active_ep, mtu->is_active);
222 
223 	return 0;
224 }
225 
226 struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
227 {
228 	struct mtu3_ep *mep = to_mtu3_ep(ep);
229 	struct mtu3_request *mreq;
230 
231 	mreq = kzalloc(sizeof(*mreq), gfp_flags);
232 	if (!mreq)
233 		return NULL;
234 
235 	mreq->request.dma = DMA_ADDR_INVALID;
236 	mreq->epnum = mep->epnum;
237 	mreq->mep = mep;
238 	trace_mtu3_alloc_request(mreq);
239 
240 	return &mreq->request;
241 }
242 
243 void mtu3_free_request(struct usb_ep *ep, struct usb_request *req)
244 {
245 	struct mtu3_request *mreq = to_mtu3_request(req);
246 
247 	trace_mtu3_free_request(mreq);
248 	kfree(mreq);
249 }
250 
251 static int mtu3_gadget_queue(struct usb_ep *ep,
252 		struct usb_request *req, gfp_t gfp_flags)
253 {
254 	struct mtu3_ep *mep = to_mtu3_ep(ep);
255 	struct mtu3_request *mreq = to_mtu3_request(req);
256 	struct mtu3 *mtu = mep->mtu;
257 	unsigned long flags;
258 	int ret = 0;
259 
260 	if (!req->buf)
261 		return -ENODATA;
262 
263 	if (mreq->mep != mep)
264 		return -EINVAL;
265 
266 	dev_dbg(mtu->dev, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n",
267 		__func__, mep->is_in ? "TX" : "RX", mreq->epnum, ep->name,
268 		mreq, ep->maxpacket, mreq->request.length);
269 
270 	if (req->length > GPD_BUF_SIZE ||
271 	    (mtu->gen2cp && req->length > GPD_BUF_SIZE_EL)) {
272 		dev_warn(mtu->dev,
273 			"req length > supported MAX:%d requested:%d\n",
274 			mtu->gen2cp ? GPD_BUF_SIZE_EL : GPD_BUF_SIZE,
275 			req->length);
276 		return -EOPNOTSUPP;
277 	}
278 
279 	/* don't queue if the ep is down */
280 	if (!mep->desc) {
281 		dev_dbg(mtu->dev, "req=%p queued to %s while it's disabled\n",
282 			req, ep->name);
283 		return -ESHUTDOWN;
284 	}
285 
286 	mreq->mtu = mtu;
287 	mreq->request.actual = 0;
288 	mreq->request.status = -EINPROGRESS;
289 
290 	ret = usb_gadget_map_request(&mtu->g, req, mep->is_in);
291 	if (ret) {
292 		dev_err(mtu->dev, "dma mapping failed\n");
293 		return ret;
294 	}
295 
296 	spin_lock_irqsave(&mtu->lock, flags);
297 
298 	if (mtu3_prepare_transfer(mep)) {
299 		ret = -EAGAIN;
300 		goto error;
301 	}
302 
303 	list_add_tail(&mreq->list, &mep->req_list);
304 	mtu3_insert_gpd(mep, mreq);
305 	mtu3_qmu_resume(mep);
306 
307 error:
308 	spin_unlock_irqrestore(&mtu->lock, flags);
309 	trace_mtu3_gadget_queue(mreq);
310 
311 	return ret;
312 }
313 
314 static int mtu3_gadget_dequeue(struct usb_ep *ep, struct usb_request *req)
315 {
316 	struct mtu3_ep *mep = to_mtu3_ep(ep);
317 	struct mtu3_request *mreq = to_mtu3_request(req);
318 	struct mtu3_request *r;
319 	struct mtu3 *mtu = mep->mtu;
320 	unsigned long flags;
321 	int ret = 0;
322 
323 	if (mreq->mep != mep)
324 		return -EINVAL;
325 
326 	dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req);
327 	trace_mtu3_gadget_dequeue(mreq);
328 
329 	spin_lock_irqsave(&mtu->lock, flags);
330 
331 	list_for_each_entry(r, &mep->req_list, list) {
332 		if (r == mreq)
333 			break;
334 	}
335 	if (r != mreq) {
336 		dev_dbg(mtu->dev, "req=%p not queued to %s\n", req, ep->name);
337 		ret = -EINVAL;
338 		goto done;
339 	}
340 
341 	mtu3_qmu_flush(mep);  /* REVISIT: set BPS ?? */
342 	mtu3_req_complete(mep, req, -ECONNRESET);
343 	mtu3_qmu_start(mep);
344 
345 done:
346 	spin_unlock_irqrestore(&mtu->lock, flags);
347 
348 	return ret;
349 }
350 
351 /*
352  * Set or clear the halt bit of an EP.
353  * A halted EP won't TX/RX any data but will queue requests.
354  */
355 static int mtu3_gadget_ep_set_halt(struct usb_ep *ep, int value)
356 {
357 	struct mtu3_ep *mep = to_mtu3_ep(ep);
358 	struct mtu3 *mtu = mep->mtu;
359 	struct mtu3_request *mreq;
360 	unsigned long flags;
361 	int ret = 0;
362 
363 	dev_dbg(mtu->dev, "%s : %s...", __func__, ep->name);
364 
365 	spin_lock_irqsave(&mtu->lock, flags);
366 
367 	if (mep->type == USB_ENDPOINT_XFER_ISOC) {
368 		ret = -EINVAL;
369 		goto done;
370 	}
371 
372 	mreq = next_request(mep);
373 	if (value) {
374 		/*
375 		 * If there is not request for TX-EP, QMU will not transfer
376 		 * data to TX-FIFO, so no need check whether TX-FIFO
377 		 * holds bytes or not here
378 		 */
379 		if (mreq) {
380 			dev_dbg(mtu->dev, "req in progress, cannot halt %s\n",
381 				ep->name);
382 			ret = -EAGAIN;
383 			goto done;
384 		}
385 	} else {
386 		mep->flags &= ~MTU3_EP_WEDGE;
387 	}
388 
389 	dev_dbg(mtu->dev, "%s %s stall\n", ep->name, value ? "set" : "clear");
390 
391 	mtu3_ep_stall_set(mep, value);
392 
393 done:
394 	spin_unlock_irqrestore(&mtu->lock, flags);
395 	trace_mtu3_gadget_ep_set_halt(mep);
396 
397 	return ret;
398 }
399 
400 /* Sets the halt feature with the clear requests ignored */
401 static int mtu3_gadget_ep_set_wedge(struct usb_ep *ep)
402 {
403 	struct mtu3_ep *mep = to_mtu3_ep(ep);
404 
405 	mep->flags |= MTU3_EP_WEDGE;
406 
407 	return usb_ep_set_halt(ep);
408 }
409 
410 static const struct usb_ep_ops mtu3_ep_ops = {
411 	.enable = mtu3_gadget_ep_enable,
412 	.disable = mtu3_gadget_ep_disable,
413 	.alloc_request = mtu3_alloc_request,
414 	.free_request = mtu3_free_request,
415 	.queue = mtu3_gadget_queue,
416 	.dequeue = mtu3_gadget_dequeue,
417 	.set_halt = mtu3_gadget_ep_set_halt,
418 	.set_wedge = mtu3_gadget_ep_set_wedge,
419 };
420 
421 static int mtu3_gadget_get_frame(struct usb_gadget *gadget)
422 {
423 	struct mtu3 *mtu = gadget_to_mtu3(gadget);
424 
425 	return (int)mtu3_readl(mtu->mac_base, U3D_USB20_FRAME_NUM);
426 }
427 
428 static int mtu3_gadget_wakeup(struct usb_gadget *gadget)
429 {
430 	struct mtu3 *mtu = gadget_to_mtu3(gadget);
431 	unsigned long flags;
432 
433 	dev_dbg(mtu->dev, "%s\n", __func__);
434 
435 	/* remote wakeup feature is not enabled by host */
436 	if (!mtu->may_wakeup)
437 		return  -EOPNOTSUPP;
438 
439 	spin_lock_irqsave(&mtu->lock, flags);
440 	if (mtu->g.speed >= USB_SPEED_SUPER) {
441 		mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT);
442 	} else {
443 		mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
444 		spin_unlock_irqrestore(&mtu->lock, flags);
445 		usleep_range(10000, 11000);
446 		spin_lock_irqsave(&mtu->lock, flags);
447 		mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
448 	}
449 	spin_unlock_irqrestore(&mtu->lock, flags);
450 	return 0;
451 }
452 
453 static int mtu3_gadget_set_self_powered(struct usb_gadget *gadget,
454 		int is_selfpowered)
455 {
456 	struct mtu3 *mtu = gadget_to_mtu3(gadget);
457 
458 	mtu->is_self_powered = !!is_selfpowered;
459 	return 0;
460 }
461 
462 static int mtu3_gadget_pullup(struct usb_gadget *gadget, int is_on)
463 {
464 	struct mtu3 *mtu = gadget_to_mtu3(gadget);
465 	unsigned long flags;
466 
467 	dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__,
468 		is_on ? "on" : "off", mtu->is_active ? "" : "in");
469 
470 	pm_runtime_get_sync(mtu->dev);
471 
472 	/* we'd rather not pullup unless the device is active. */
473 	spin_lock_irqsave(&mtu->lock, flags);
474 
475 	is_on = !!is_on;
476 	if (!mtu->is_active) {
477 		/* save it for mtu3_start() to process the request */
478 		mtu->softconnect = is_on;
479 	} else if (is_on != mtu->softconnect) {
480 		mtu->softconnect = is_on;
481 		mtu3_dev_on_off(mtu, is_on);
482 	}
483 
484 	spin_unlock_irqrestore(&mtu->lock, flags);
485 	pm_runtime_put(mtu->dev);
486 
487 	return 0;
488 }
489 
490 static int mtu3_gadget_start(struct usb_gadget *gadget,
491 		struct usb_gadget_driver *driver)
492 {
493 	struct mtu3 *mtu = gadget_to_mtu3(gadget);
494 	unsigned long flags;
495 
496 	if (mtu->gadget_driver) {
497 		dev_err(mtu->dev, "%s is already bound to %s\n",
498 			mtu->g.name, mtu->gadget_driver->driver.name);
499 		return -EBUSY;
500 	}
501 
502 	dev_dbg(mtu->dev, "bind driver %s\n", driver->function);
503 	pm_runtime_get_sync(mtu->dev);
504 
505 	spin_lock_irqsave(&mtu->lock, flags);
506 
507 	mtu->softconnect = 0;
508 	mtu->gadget_driver = driver;
509 
510 	if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
511 		mtu3_start(mtu);
512 
513 	spin_unlock_irqrestore(&mtu->lock, flags);
514 	pm_runtime_put(mtu->dev);
515 
516 	return 0;
517 }
518 
519 static void stop_activity(struct mtu3 *mtu)
520 {
521 	struct usb_gadget_driver *driver = mtu->gadget_driver;
522 	int i;
523 
524 	/* don't disconnect if it's not connected */
525 	if (mtu->g.speed == USB_SPEED_UNKNOWN)
526 		driver = NULL;
527 	else
528 		mtu->g.speed = USB_SPEED_UNKNOWN;
529 
530 	/* deactivate the hardware */
531 	if (mtu->softconnect) {
532 		mtu->softconnect = 0;
533 		mtu3_dev_on_off(mtu, 0);
534 	}
535 
536 	/*
537 	 * killing any outstanding requests will quiesce the driver;
538 	 * then report disconnect
539 	 */
540 	nuke(mtu->ep0, -ESHUTDOWN);
541 	for (i = 1; i < mtu->num_eps; i++) {
542 		nuke(mtu->in_eps + i, -ESHUTDOWN);
543 		nuke(mtu->out_eps + i, -ESHUTDOWN);
544 	}
545 
546 	if (driver) {
547 		spin_unlock(&mtu->lock);
548 		driver->disconnect(&mtu->g);
549 		spin_lock(&mtu->lock);
550 	}
551 }
552 
553 static int mtu3_gadget_stop(struct usb_gadget *g)
554 {
555 	struct mtu3 *mtu = gadget_to_mtu3(g);
556 	unsigned long flags;
557 
558 	dev_dbg(mtu->dev, "%s\n", __func__);
559 
560 	spin_lock_irqsave(&mtu->lock, flags);
561 
562 	stop_activity(mtu);
563 	mtu->gadget_driver = NULL;
564 
565 	if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
566 		mtu3_stop(mtu);
567 
568 	spin_unlock_irqrestore(&mtu->lock, flags);
569 
570 	synchronize_irq(mtu->irq);
571 	return 0;
572 }
573 
574 static void
575 mtu3_gadget_set_speed(struct usb_gadget *g, enum usb_device_speed speed)
576 {
577 	struct mtu3 *mtu = gadget_to_mtu3(g);
578 	unsigned long flags;
579 
580 	dev_dbg(mtu->dev, "%s %s\n", __func__, usb_speed_string(speed));
581 
582 	spin_lock_irqsave(&mtu->lock, flags);
583 	mtu->speed = speed;
584 	spin_unlock_irqrestore(&mtu->lock, flags);
585 }
586 
587 static const struct usb_gadget_ops mtu3_gadget_ops = {
588 	.get_frame = mtu3_gadget_get_frame,
589 	.wakeup = mtu3_gadget_wakeup,
590 	.set_selfpowered = mtu3_gadget_set_self_powered,
591 	.pullup = mtu3_gadget_pullup,
592 	.udc_start = mtu3_gadget_start,
593 	.udc_stop = mtu3_gadget_stop,
594 	.udc_set_speed = mtu3_gadget_set_speed,
595 };
596 
597 static void mtu3_state_reset(struct mtu3 *mtu)
598 {
599 	mtu->address = 0;
600 	mtu->ep0_state = MU3D_EP0_STATE_SETUP;
601 	mtu->may_wakeup = 0;
602 	mtu->u1_enable = 0;
603 	mtu->u2_enable = 0;
604 	mtu->delayed_status = false;
605 	mtu->test_mode = false;
606 }
607 
608 static void init_hw_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
609 		u32 epnum, u32 is_in)
610 {
611 	mep->epnum = epnum;
612 	mep->mtu = mtu;
613 	mep->is_in = is_in;
614 
615 	INIT_LIST_HEAD(&mep->req_list);
616 
617 	sprintf(mep->name, "ep%d%s", epnum,
618 		!epnum ? "" : (is_in ? "in" : "out"));
619 
620 	mep->ep.name = mep->name;
621 	INIT_LIST_HEAD(&mep->ep.ep_list);
622 
623 	/* initialize maxpacket as SS */
624 	if (!epnum) {
625 		usb_ep_set_maxpacket_limit(&mep->ep, 512);
626 		mep->ep.caps.type_control = true;
627 		mep->ep.ops = &mtu3_ep0_ops;
628 		mtu->g.ep0 = &mep->ep;
629 	} else {
630 		usb_ep_set_maxpacket_limit(&mep->ep, 1024);
631 		mep->ep.caps.type_iso = true;
632 		mep->ep.caps.type_bulk = true;
633 		mep->ep.caps.type_int = true;
634 		mep->ep.ops = &mtu3_ep_ops;
635 		list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list);
636 	}
637 
638 	dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name,
639 		 mep->ep.maxpacket);
640 
641 	if (!epnum) {
642 		mep->ep.caps.dir_in = true;
643 		mep->ep.caps.dir_out = true;
644 	} else if (is_in) {
645 		mep->ep.caps.dir_in = true;
646 	} else {
647 		mep->ep.caps.dir_out = true;
648 	}
649 }
650 
651 static void mtu3_gadget_init_eps(struct mtu3 *mtu)
652 {
653 	u8 epnum;
654 
655 	/* initialize endpoint list just once */
656 	INIT_LIST_HEAD(&(mtu->g.ep_list));
657 
658 	dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n",
659 		__func__, mtu->num_eps);
660 
661 	init_hw_ep(mtu, mtu->ep0, 0, 0);
662 	for (epnum = 1; epnum < mtu->num_eps; epnum++) {
663 		init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1);
664 		init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0);
665 	}
666 }
667 
668 int mtu3_gadget_setup(struct mtu3 *mtu)
669 {
670 	mtu->g.ops = &mtu3_gadget_ops;
671 	mtu->g.max_speed = mtu->max_speed;
672 	mtu->g.speed = USB_SPEED_UNKNOWN;
673 	mtu->g.sg_supported = 0;
674 	mtu->g.name = MTU3_DRIVER_NAME;
675 	mtu->is_active = 0;
676 	mtu->delayed_status = false;
677 
678 	mtu3_gadget_init_eps(mtu);
679 
680 	return usb_add_gadget_udc(mtu->dev, &mtu->g);
681 }
682 
683 void mtu3_gadget_cleanup(struct mtu3 *mtu)
684 {
685 	usb_del_gadget_udc(&mtu->g);
686 }
687 
688 void mtu3_gadget_resume(struct mtu3 *mtu)
689 {
690 	dev_dbg(mtu->dev, "gadget RESUME\n");
691 	if (mtu->gadget_driver && mtu->gadget_driver->resume) {
692 		spin_unlock(&mtu->lock);
693 		mtu->gadget_driver->resume(&mtu->g);
694 		spin_lock(&mtu->lock);
695 	}
696 }
697 
698 /* called when SOF packets stop for 3+ msec or enters U3 */
699 void mtu3_gadget_suspend(struct mtu3 *mtu)
700 {
701 	dev_dbg(mtu->dev, "gadget SUSPEND\n");
702 	if (mtu->gadget_driver && mtu->gadget_driver->suspend) {
703 		spin_unlock(&mtu->lock);
704 		mtu->gadget_driver->suspend(&mtu->g);
705 		spin_lock(&mtu->lock);
706 	}
707 }
708 
709 /* called when VBUS drops below session threshold, and in other cases */
710 void mtu3_gadget_disconnect(struct mtu3 *mtu)
711 {
712 	dev_dbg(mtu->dev, "gadget DISCONNECT\n");
713 	if (mtu->gadget_driver && mtu->gadget_driver->disconnect) {
714 		spin_unlock(&mtu->lock);
715 		mtu->gadget_driver->disconnect(&mtu->g);
716 		spin_lock(&mtu->lock);
717 	}
718 
719 	mtu3_state_reset(mtu);
720 	usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED);
721 }
722 
723 void mtu3_gadget_reset(struct mtu3 *mtu)
724 {
725 	dev_dbg(mtu->dev, "gadget RESET\n");
726 
727 	/* report disconnect, if we didn't flush EP state */
728 	if (mtu->g.speed != USB_SPEED_UNKNOWN)
729 		mtu3_gadget_disconnect(mtu);
730 	else
731 		mtu3_state_reset(mtu);
732 }
733