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
mtu3_req_complete(struct mtu3_ep * mep,struct usb_request * req,int status)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
nuke(struct mtu3_ep * mep,const int status)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
mtu3_ep_enable(struct mtu3_ep * mep)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);
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
mtu3_ep_disable(struct mtu3_ep * mep)132 static 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
mtu3_gadget_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)152 static 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
198 error:
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
mtu3_gadget_ep_disable(struct usb_ep * ep)207 static 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
mtu3_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)233 struct 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
mtu3_free_request(struct usb_ep * ep,struct usb_request * req)251 void 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
mtu3_gadget_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)259 static 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
315 error:
316 spin_unlock_irqrestore(&mtu->lock, flags);
317 trace_mtu3_gadget_queue(mreq);
318
319 return ret;
320 }
321
mtu3_gadget_dequeue(struct usb_ep * ep,struct usb_request * req)322 static 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
353 done:
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 */
mtu3_gadget_ep_set_halt(struct usb_ep * ep,int value)363 static 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
401 done:
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 */
mtu3_gadget_ep_set_wedge(struct usb_ep * ep)409 static 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
418 static 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
mtu3_gadget_get_frame(struct usb_gadget * gadget)429 static 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
mtu3_gadget_wakeup(struct usb_gadget * gadget)436 static int mtu3_gadget_wakeup(struct usb_gadget *gadget)
437 {
438 struct mtu3 *mtu = gadget_to_mtu3(gadget);
439 unsigned long flags;
440
441 dev_dbg(mtu->dev, "%s\n", __func__);
442
443 /* remote wakeup feature is not enabled by host */
444 if (!mtu->may_wakeup)
445 return -EOPNOTSUPP;
446
447 spin_lock_irqsave(&mtu->lock, flags);
448 if (mtu->g.speed >= USB_SPEED_SUPER) {
449 mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT);
450 } else {
451 mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
452 spin_unlock_irqrestore(&mtu->lock, flags);
453 usleep_range(10000, 11000);
454 spin_lock_irqsave(&mtu->lock, flags);
455 mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
456 }
457 spin_unlock_irqrestore(&mtu->lock, flags);
458 return 0;
459 }
460
mtu3_gadget_set_self_powered(struct usb_gadget * gadget,int is_selfpowered)461 static int mtu3_gadget_set_self_powered(struct usb_gadget *gadget,
462 int is_selfpowered)
463 {
464 struct mtu3 *mtu = gadget_to_mtu3(gadget);
465
466 mtu->is_self_powered = !!is_selfpowered;
467 return 0;
468 }
469
mtu3_gadget_pullup(struct usb_gadget * gadget,int is_on)470 static int mtu3_gadget_pullup(struct usb_gadget *gadget, int is_on)
471 {
472 struct mtu3 *mtu = gadget_to_mtu3(gadget);
473 unsigned long flags;
474
475 dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__,
476 is_on ? "on" : "off", mtu->is_active ? "" : "in");
477
478 /* we'd rather not pullup unless the device is active. */
479 spin_lock_irqsave(&mtu->lock, flags);
480
481 is_on = !!is_on;
482 if (!mtu->is_active) {
483 /* save it for mtu3_start() to process the request */
484 mtu->softconnect = is_on;
485 } else if (is_on != mtu->softconnect) {
486 mtu->softconnect = is_on;
487 mtu3_dev_on_off(mtu, is_on);
488 }
489
490 spin_unlock_irqrestore(&mtu->lock, flags);
491
492 return 0;
493 }
494
mtu3_gadget_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver)495 static int mtu3_gadget_start(struct usb_gadget *gadget,
496 struct usb_gadget_driver *driver)
497 {
498 struct mtu3 *mtu = gadget_to_mtu3(gadget);
499 unsigned long flags;
500
501 if (mtu->gadget_driver) {
502 dev_err(mtu->dev, "%s is already bound to %s\n",
503 mtu->g.name, mtu->gadget_driver->driver.name);
504 return -EBUSY;
505 }
506
507 dev_dbg(mtu->dev, "bind driver %s\n", driver->function);
508
509 spin_lock_irqsave(&mtu->lock, flags);
510
511 mtu->softconnect = 0;
512 mtu->gadget_driver = driver;
513
514 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
515 mtu3_start(mtu);
516
517 spin_unlock_irqrestore(&mtu->lock, flags);
518
519 return 0;
520 }
521
stop_activity(struct mtu3 * mtu)522 static void stop_activity(struct mtu3 *mtu)
523 {
524 struct usb_gadget_driver *driver = mtu->gadget_driver;
525 int i;
526
527 /* don't disconnect if it's not connected */
528 if (mtu->g.speed == USB_SPEED_UNKNOWN)
529 driver = NULL;
530 else
531 mtu->g.speed = USB_SPEED_UNKNOWN;
532
533 /* deactivate the hardware */
534 if (mtu->softconnect) {
535 mtu->softconnect = 0;
536 mtu3_dev_on_off(mtu, 0);
537 }
538
539 /*
540 * killing any outstanding requests will quiesce the driver;
541 * then report disconnect
542 */
543 nuke(mtu->ep0, -ESHUTDOWN);
544 for (i = 1; i < mtu->num_eps; i++) {
545 nuke(mtu->in_eps + i, -ESHUTDOWN);
546 nuke(mtu->out_eps + i, -ESHUTDOWN);
547 }
548
549 if (driver) {
550 spin_unlock(&mtu->lock);
551 driver->disconnect(&mtu->g);
552 spin_lock(&mtu->lock);
553 }
554 }
555
mtu3_gadget_stop(struct usb_gadget * g)556 static int mtu3_gadget_stop(struct usb_gadget *g)
557 {
558 struct mtu3 *mtu = gadget_to_mtu3(g);
559 unsigned long flags;
560
561 dev_dbg(mtu->dev, "%s\n", __func__);
562
563 spin_lock_irqsave(&mtu->lock, flags);
564
565 stop_activity(mtu);
566 mtu->gadget_driver = NULL;
567
568 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
569 mtu3_stop(mtu);
570
571 spin_unlock_irqrestore(&mtu->lock, flags);
572
573 synchronize_irq(mtu->irq);
574 return 0;
575 }
576
577 static void
mtu3_gadget_set_speed(struct usb_gadget * g,enum usb_device_speed speed)578 mtu3_gadget_set_speed(struct usb_gadget *g, enum usb_device_speed speed)
579 {
580 struct mtu3 *mtu = gadget_to_mtu3(g);
581 unsigned long flags;
582
583 dev_dbg(mtu->dev, "%s %s\n", __func__, usb_speed_string(speed));
584
585 spin_lock_irqsave(&mtu->lock, flags);
586 mtu3_set_speed(mtu, speed);
587 spin_unlock_irqrestore(&mtu->lock, flags);
588 }
589
590 static const struct usb_gadget_ops mtu3_gadget_ops = {
591 .get_frame = mtu3_gadget_get_frame,
592 .wakeup = mtu3_gadget_wakeup,
593 .set_selfpowered = mtu3_gadget_set_self_powered,
594 .pullup = mtu3_gadget_pullup,
595 .udc_start = mtu3_gadget_start,
596 .udc_stop = mtu3_gadget_stop,
597 .udc_set_speed = mtu3_gadget_set_speed,
598 };
599
mtu3_state_reset(struct mtu3 * mtu)600 static void mtu3_state_reset(struct mtu3 *mtu)
601 {
602 mtu->address = 0;
603 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
604 mtu->may_wakeup = 0;
605 mtu->u1_enable = 0;
606 mtu->u2_enable = 0;
607 mtu->delayed_status = false;
608 mtu->test_mode = false;
609 }
610
init_hw_ep(struct mtu3 * mtu,struct mtu3_ep * mep,u32 epnum,u32 is_in)611 static void init_hw_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
612 u32 epnum, u32 is_in)
613 {
614 mep->epnum = epnum;
615 mep->mtu = mtu;
616 mep->is_in = is_in;
617
618 INIT_LIST_HEAD(&mep->req_list);
619
620 sprintf(mep->name, "ep%d%s", epnum,
621 !epnum ? "" : (is_in ? "in" : "out"));
622
623 mep->ep.name = mep->name;
624 INIT_LIST_HEAD(&mep->ep.ep_list);
625
626 /* initialize maxpacket as SS */
627 if (!epnum) {
628 usb_ep_set_maxpacket_limit(&mep->ep, 512);
629 mep->ep.caps.type_control = true;
630 mep->ep.ops = &mtu3_ep0_ops;
631 mtu->g.ep0 = &mep->ep;
632 } else {
633 usb_ep_set_maxpacket_limit(&mep->ep, 1024);
634 mep->ep.caps.type_iso = true;
635 mep->ep.caps.type_bulk = true;
636 mep->ep.caps.type_int = true;
637 mep->ep.ops = &mtu3_ep_ops;
638 list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list);
639 }
640
641 dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name,
642 mep->ep.maxpacket);
643
644 if (!epnum) {
645 mep->ep.caps.dir_in = true;
646 mep->ep.caps.dir_out = true;
647 } else if (is_in) {
648 mep->ep.caps.dir_in = true;
649 } else {
650 mep->ep.caps.dir_out = true;
651 }
652 }
653
mtu3_gadget_init_eps(struct mtu3 * mtu)654 static void mtu3_gadget_init_eps(struct mtu3 *mtu)
655 {
656 u8 epnum;
657
658 /* initialize endpoint list just once */
659 INIT_LIST_HEAD(&(mtu->g.ep_list));
660
661 dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n",
662 __func__, mtu->num_eps);
663
664 init_hw_ep(mtu, mtu->ep0, 0, 0);
665 for (epnum = 1; epnum < mtu->num_eps; epnum++) {
666 init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1);
667 init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0);
668 }
669 }
670
mtu3_gadget_setup(struct mtu3 * mtu)671 int mtu3_gadget_setup(struct mtu3 *mtu)
672 {
673 mtu->g.ops = &mtu3_gadget_ops;
674 mtu->g.max_speed = mtu->max_speed;
675 mtu->g.speed = USB_SPEED_UNKNOWN;
676 mtu->g.sg_supported = 0;
677 mtu->g.name = MTU3_DRIVER_NAME;
678 mtu->is_active = 0;
679 mtu->delayed_status = false;
680
681 mtu3_gadget_init_eps(mtu);
682
683 return usb_add_gadget_udc(mtu->dev, &mtu->g);
684 }
685
mtu3_gadget_cleanup(struct mtu3 * mtu)686 void mtu3_gadget_cleanup(struct mtu3 *mtu)
687 {
688 usb_del_gadget_udc(&mtu->g);
689 }
690
mtu3_gadget_resume(struct mtu3 * mtu)691 void mtu3_gadget_resume(struct mtu3 *mtu)
692 {
693 dev_dbg(mtu->dev, "gadget RESUME\n");
694 if (mtu->gadget_driver && mtu->gadget_driver->resume) {
695 spin_unlock(&mtu->lock);
696 mtu->gadget_driver->resume(&mtu->g);
697 spin_lock(&mtu->lock);
698 }
699 }
700
701 /* called when SOF packets stop for 3+ msec or enters U3 */
mtu3_gadget_suspend(struct mtu3 * mtu)702 void mtu3_gadget_suspend(struct mtu3 *mtu)
703 {
704 dev_dbg(mtu->dev, "gadget SUSPEND\n");
705 if (mtu->gadget_driver && mtu->gadget_driver->suspend) {
706 spin_unlock(&mtu->lock);
707 mtu->gadget_driver->suspend(&mtu->g);
708 spin_lock(&mtu->lock);
709 }
710 }
711
712 /* called when VBUS drops below session threshold, and in other cases */
mtu3_gadget_disconnect(struct mtu3 * mtu)713 void mtu3_gadget_disconnect(struct mtu3 *mtu)
714 {
715 dev_dbg(mtu->dev, "gadget DISCONNECT\n");
716 if (mtu->gadget_driver && mtu->gadget_driver->disconnect) {
717 spin_unlock(&mtu->lock);
718 mtu->gadget_driver->disconnect(&mtu->g);
719 spin_lock(&mtu->lock);
720 }
721
722 mtu3_state_reset(mtu);
723 usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED);
724 }
725
mtu3_gadget_reset(struct mtu3 * mtu)726 void mtu3_gadget_reset(struct mtu3 *mtu)
727 {
728 dev_dbg(mtu->dev, "gadget RESET\n");
729
730 /* report disconnect, if we didn't flush EP state */
731 if (mtu->g.speed != USB_SPEED_UNKNOWN)
732 mtu3_gadget_disconnect(mtu);
733 else
734 mtu3_state_reset(mtu);
735 }
736