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