Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

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