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