Linux 4.19.133
[linux/fpc-iii.git] / drivers / usb / mtu3 / mtu3_gadget.c
blobbbcd3332471dc2217042e62527b12745c48142e6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mtu3_gadget.c - MediaTek usb3 DRD peripheral support
5 * Copyright (C) 2016 MediaTek Inc.
7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
8 */
10 #include "mtu3.h"
12 void mtu3_req_complete(struct mtu3_ep *mep,
13 struct usb_request *req, int status)
14 __releases(mep->mtu->lock)
15 __acquires(mep->mtu->lock)
17 struct mtu3_request *mreq;
18 struct mtu3 *mtu;
19 int busy = mep->busy;
21 mreq = to_mtu3_request(req);
22 list_del(&mreq->list);
23 if (mreq->request.status == -EINPROGRESS)
24 mreq->request.status = status;
26 mtu = mreq->mtu;
27 mep->busy = 1;
28 spin_unlock(&mtu->lock);
30 /* ep0 makes use of PIO, needn't unmap it */
31 if (mep->epnum)
32 usb_gadget_unmap_request(&mtu->g, req, mep->is_in);
34 dev_dbg(mtu->dev, "%s complete req: %p, sts %d, %d/%d\n", mep->name,
35 req, req->status, mreq->request.actual, mreq->request.length);
37 usb_gadget_giveback_request(&mep->ep, &mreq->request);
39 spin_lock(&mtu->lock);
40 mep->busy = busy;
43 static void nuke(struct mtu3_ep *mep, const int status)
45 struct mtu3_request *mreq = NULL;
47 mep->busy = 1;
48 if (list_empty(&mep->req_list))
49 return;
51 dev_dbg(mep->mtu->dev, "abort %s's req: sts %d\n", mep->name, status);
53 /* exclude EP0 */
54 if (mep->epnum)
55 mtu3_qmu_flush(mep);
57 while (!list_empty(&mep->req_list)) {
58 mreq = list_first_entry(&mep->req_list,
59 struct mtu3_request, list);
60 mtu3_req_complete(mep, &mreq->request, status);
64 static int mtu3_ep_enable(struct mtu3_ep *mep)
66 const struct usb_endpoint_descriptor *desc;
67 const struct usb_ss_ep_comp_descriptor *comp_desc;
68 struct mtu3 *mtu = mep->mtu;
69 u32 interval = 0;
70 u32 mult = 0;
71 u32 burst = 0;
72 int max_packet;
73 int ret;
75 desc = mep->desc;
76 comp_desc = mep->comp_desc;
77 mep->type = usb_endpoint_type(desc);
78 max_packet = usb_endpoint_maxp(desc);
79 mep->maxp = max_packet & GENMASK(10, 0);
81 switch (mtu->g.speed) {
82 case USB_SPEED_SUPER:
83 case USB_SPEED_SUPER_PLUS:
84 if (usb_endpoint_xfer_int(desc) ||
85 usb_endpoint_xfer_isoc(desc)) {
86 interval = desc->bInterval;
87 interval = clamp_val(interval, 1, 16) - 1;
88 if (usb_endpoint_xfer_isoc(desc) && comp_desc)
89 mult = comp_desc->bmAttributes;
91 if (comp_desc)
92 burst = comp_desc->bMaxBurst;
94 break;
95 case USB_SPEED_HIGH:
96 if (usb_endpoint_xfer_isoc(desc) ||
97 usb_endpoint_xfer_int(desc)) {
98 interval = desc->bInterval;
99 interval = clamp_val(interval, 1, 16) - 1;
100 burst = (max_packet & GENMASK(12, 11)) >> 11;
102 break;
103 default:
104 break; /*others are ignored */
107 dev_dbg(mtu->dev, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n",
108 __func__, mep->maxp, interval, burst, mult);
110 mep->ep.maxpacket = mep->maxp;
111 mep->ep.desc = desc;
112 mep->ep.comp_desc = comp_desc;
114 /* slot mainly affects bulk/isoc transfer, so ignore int */
115 mep->slot = usb_endpoint_xfer_int(desc) ? 0 : mtu->slot;
117 ret = mtu3_config_ep(mtu, mep, interval, burst, mult);
118 if (ret < 0)
119 return ret;
121 ret = mtu3_gpd_ring_alloc(mep);
122 if (ret < 0) {
123 mtu3_deconfig_ep(mtu, mep);
124 return ret;
127 mtu3_qmu_start(mep);
129 return 0;
132 static int mtu3_ep_disable(struct mtu3_ep *mep)
134 struct mtu3 *mtu = mep->mtu;
136 mtu3_qmu_stop(mep);
138 /* abort all pending requests */
139 nuke(mep, -ESHUTDOWN);
140 mtu3_deconfig_ep(mtu, mep);
141 mtu3_gpd_ring_free(mep);
143 mep->desc = NULL;
144 mep->ep.desc = NULL;
145 mep->comp_desc = NULL;
146 mep->type = 0;
147 mep->flags = 0;
149 return 0;
152 static int mtu3_gadget_ep_enable(struct usb_ep *ep,
153 const struct usb_endpoint_descriptor *desc)
155 struct mtu3_ep *mep;
156 struct mtu3 *mtu;
157 unsigned long flags;
158 int ret = -EINVAL;
160 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
161 pr_debug("%s invalid parameters\n", __func__);
162 return -EINVAL;
165 if (!desc->wMaxPacketSize) {
166 pr_debug("%s missing wMaxPacketSize\n", __func__);
167 return -EINVAL;
169 mep = to_mtu3_ep(ep);
170 mtu = mep->mtu;
172 /* check ep number and direction against endpoint */
173 if (usb_endpoint_num(desc) != mep->epnum)
174 return -EINVAL;
176 if (!!usb_endpoint_dir_in(desc) ^ !!mep->is_in)
177 return -EINVAL;
179 dev_dbg(mtu->dev, "%s %s\n", __func__, ep->name);
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;
187 spin_lock_irqsave(&mtu->lock, flags);
188 mep->desc = desc;
189 mep->comp_desc = ep->comp_desc;
191 ret = mtu3_ep_enable(mep);
192 if (ret)
193 goto error;
195 mep->busy = 0;
196 mep->wedged = 0;
197 mep->flags |= MTU3_EP_ENABLED;
198 mtu->active_ep++;
200 error:
201 spin_unlock_irqrestore(&mtu->lock, flags);
203 dev_dbg(mtu->dev, "%s active_ep=%d\n", __func__, mtu->active_ep);
205 return ret;
208 static int mtu3_gadget_ep_disable(struct usb_ep *ep)
210 struct mtu3_ep *mep = to_mtu3_ep(ep);
211 struct mtu3 *mtu = mep->mtu;
212 unsigned long flags;
214 dev_dbg(mtu->dev, "%s %s\n", __func__, mep->name);
216 if (!(mep->flags & MTU3_EP_ENABLED)) {
217 dev_warn(mtu->dev, "%s is already disabled\n", mep->name);
218 return 0;
221 spin_lock_irqsave(&mtu->lock, flags);
222 mtu3_ep_disable(mep);
223 mep->flags &= ~MTU3_EP_ENABLED;
224 mtu->active_ep--;
225 spin_unlock_irqrestore(&(mtu->lock), flags);
227 dev_dbg(mtu->dev, "%s active_ep=%d, mtu3 is_active=%d\n",
228 __func__, mtu->active_ep, mtu->is_active);
230 return 0;
233 struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
235 struct mtu3_ep *mep = to_mtu3_ep(ep);
236 struct mtu3_request *mreq;
238 mreq = kzalloc(sizeof(*mreq), gfp_flags);
239 if (!mreq)
240 return NULL;
242 mreq->request.dma = DMA_ADDR_INVALID;
243 mreq->epnum = mep->epnum;
244 mreq->mep = mep;
246 return &mreq->request;
249 void mtu3_free_request(struct usb_ep *ep, struct usb_request *req)
251 kfree(to_mtu3_request(req));
254 static int mtu3_gadget_queue(struct usb_ep *ep,
255 struct usb_request *req, gfp_t gfp_flags)
257 struct mtu3_ep *mep;
258 struct mtu3_request *mreq;
259 struct mtu3 *mtu;
260 unsigned long flags;
261 int ret = 0;
263 if (!ep || !req)
264 return -EINVAL;
266 if (!req->buf)
267 return -ENODATA;
269 mep = to_mtu3_ep(ep);
270 mtu = mep->mtu;
271 mreq = to_mtu3_request(req);
272 mreq->mtu = mtu;
274 if (mreq->mep != mep)
275 return -EINVAL;
277 dev_dbg(mtu->dev, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n",
278 __func__, mep->is_in ? "TX" : "RX", mreq->epnum, ep->name,
279 mreq, ep->maxpacket, mreq->request.length);
281 if (req->length > GPD_BUF_SIZE) {
282 dev_warn(mtu->dev,
283 "req length > supported MAX:%d requested:%d\n",
284 GPD_BUF_SIZE, req->length);
285 return -EOPNOTSUPP;
288 /* don't queue if the ep is down */
289 if (!mep->desc) {
290 dev_dbg(mtu->dev, "req=%p queued to %s while it's disabled\n",
291 req, ep->name);
292 return -ESHUTDOWN;
295 mreq->request.actual = 0;
296 mreq->request.status = -EINPROGRESS;
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;
304 spin_lock_irqsave(&mtu->lock, flags);
306 if (mtu3_prepare_transfer(mep)) {
307 ret = -EAGAIN;
308 goto error;
311 list_add_tail(&mreq->list, &mep->req_list);
312 mtu3_insert_gpd(mep, mreq);
313 mtu3_qmu_resume(mep);
315 error:
316 spin_unlock_irqrestore(&mtu->lock, flags);
318 return ret;
321 static int mtu3_gadget_dequeue(struct usb_ep *ep, struct usb_request *req)
323 struct mtu3_ep *mep = to_mtu3_ep(ep);
324 struct mtu3_request *mreq = to_mtu3_request(req);
325 struct mtu3_request *r;
326 unsigned long flags;
327 int ret = 0;
328 struct mtu3 *mtu = mep->mtu;
330 if (!ep || !req || mreq->mep != mep)
331 return -EINVAL;
333 dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req);
335 spin_lock_irqsave(&mtu->lock, flags);
337 list_for_each_entry(r, &mep->req_list, list) {
338 if (r == mreq)
339 break;
341 if (r != mreq) {
342 dev_dbg(mtu->dev, "req=%p not queued to %s\n", req, ep->name);
343 ret = -EINVAL;
344 goto done;
347 mtu3_qmu_flush(mep); /* REVISIT: set BPS ?? */
348 mtu3_req_complete(mep, req, -ECONNRESET);
349 mtu3_qmu_start(mep);
351 done:
352 spin_unlock_irqrestore(&mtu->lock, flags);
354 return ret;
358 * Set or clear the halt bit of an EP.
359 * A halted EP won't TX/RX any data but will queue requests.
361 static int mtu3_gadget_ep_set_halt(struct usb_ep *ep, int value)
363 struct mtu3_ep *mep = to_mtu3_ep(ep);
364 struct mtu3 *mtu = mep->mtu;
365 struct mtu3_request *mreq;
366 unsigned long flags;
367 int ret = 0;
369 if (!ep)
370 return -EINVAL;
372 dev_dbg(mtu->dev, "%s : %s...", __func__, ep->name);
374 spin_lock_irqsave(&mtu->lock, flags);
376 if (mep->type == USB_ENDPOINT_XFER_ISOC) {
377 ret = -EINVAL;
378 goto done;
381 mreq = next_request(mep);
382 if (value) {
384 * If there is not request for TX-EP, QMU will not transfer
385 * data to TX-FIFO, so no need check whether TX-FIFO
386 * holds bytes or not here
388 if (mreq) {
389 dev_dbg(mtu->dev, "req in progress, cannot halt %s\n",
390 ep->name);
391 ret = -EAGAIN;
392 goto done;
394 } else {
395 mep->wedged = 0;
398 dev_dbg(mtu->dev, "%s %s stall\n", ep->name, value ? "set" : "clear");
400 mtu3_ep_stall_set(mep, value);
402 done:
403 spin_unlock_irqrestore(&mtu->lock, flags);
405 return ret;
408 /* Sets the halt feature with the clear requests ignored */
409 static int mtu3_gadget_ep_set_wedge(struct usb_ep *ep)
411 struct mtu3_ep *mep = to_mtu3_ep(ep);
413 if (!ep)
414 return -EINVAL;
416 mep->wedged = 1;
418 return usb_ep_set_halt(ep);
421 static const struct usb_ep_ops mtu3_ep_ops = {
422 .enable = mtu3_gadget_ep_enable,
423 .disable = mtu3_gadget_ep_disable,
424 .alloc_request = mtu3_alloc_request,
425 .free_request = mtu3_free_request,
426 .queue = mtu3_gadget_queue,
427 .dequeue = mtu3_gadget_dequeue,
428 .set_halt = mtu3_gadget_ep_set_halt,
429 .set_wedge = mtu3_gadget_ep_set_wedge,
432 static int mtu3_gadget_get_frame(struct usb_gadget *gadget)
434 struct mtu3 *mtu = gadget_to_mtu3(gadget);
436 return (int)mtu3_readl(mtu->mac_base, U3D_USB20_FRAME_NUM);
439 static int mtu3_gadget_wakeup(struct usb_gadget *gadget)
441 struct mtu3 *mtu = gadget_to_mtu3(gadget);
442 unsigned long flags;
444 dev_dbg(mtu->dev, "%s\n", __func__);
446 /* remote wakeup feature is not enabled by host */
447 if (!mtu->may_wakeup)
448 return -EOPNOTSUPP;
450 spin_lock_irqsave(&mtu->lock, flags);
451 if (mtu->g.speed >= USB_SPEED_SUPER) {
452 mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT);
453 } else {
454 mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
455 spin_unlock_irqrestore(&mtu->lock, flags);
456 usleep_range(10000, 11000);
457 spin_lock_irqsave(&mtu->lock, flags);
458 mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
460 spin_unlock_irqrestore(&mtu->lock, flags);
461 return 0;
464 static int mtu3_gadget_set_self_powered(struct usb_gadget *gadget,
465 int is_selfpowered)
467 struct mtu3 *mtu = gadget_to_mtu3(gadget);
469 mtu->is_self_powered = !!is_selfpowered;
470 return 0;
473 static int mtu3_gadget_pullup(struct usb_gadget *gadget, int is_on)
475 struct mtu3 *mtu = gadget_to_mtu3(gadget);
476 unsigned long flags;
478 dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__,
479 is_on ? "on" : "off", mtu->is_active ? "" : "in");
481 /* we'd rather not pullup unless the device is active. */
482 spin_lock_irqsave(&mtu->lock, flags);
484 is_on = !!is_on;
485 if (!mtu->is_active) {
486 /* save it for mtu3_start() to process the request */
487 mtu->softconnect = is_on;
488 } else if (is_on != mtu->softconnect) {
489 mtu->softconnect = is_on;
490 mtu3_dev_on_off(mtu, is_on);
493 spin_unlock_irqrestore(&mtu->lock, flags);
495 return 0;
498 static int mtu3_gadget_start(struct usb_gadget *gadget,
499 struct usb_gadget_driver *driver)
501 struct mtu3 *mtu = gadget_to_mtu3(gadget);
502 unsigned long flags;
504 if (mtu->gadget_driver) {
505 dev_err(mtu->dev, "%s is already bound to %s\n",
506 mtu->g.name, mtu->gadget_driver->driver.name);
507 return -EBUSY;
510 dev_dbg(mtu->dev, "bind driver %s\n", driver->function);
512 spin_lock_irqsave(&mtu->lock, flags);
514 mtu->softconnect = 0;
515 mtu->gadget_driver = driver;
517 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
518 mtu3_start(mtu);
520 spin_unlock_irqrestore(&mtu->lock, flags);
522 return 0;
525 static void stop_activity(struct mtu3 *mtu)
527 struct usb_gadget_driver *driver = mtu->gadget_driver;
528 int i;
530 /* don't disconnect if it's not connected */
531 if (mtu->g.speed == USB_SPEED_UNKNOWN)
532 driver = NULL;
533 else
534 mtu->g.speed = USB_SPEED_UNKNOWN;
536 /* deactivate the hardware */
537 if (mtu->softconnect) {
538 mtu->softconnect = 0;
539 mtu3_dev_on_off(mtu, 0);
543 * killing any outstanding requests will quiesce the driver;
544 * then report disconnect
546 nuke(mtu->ep0, -ESHUTDOWN);
547 for (i = 1; i < mtu->num_eps; i++) {
548 nuke(mtu->in_eps + i, -ESHUTDOWN);
549 nuke(mtu->out_eps + i, -ESHUTDOWN);
552 if (driver) {
553 spin_unlock(&mtu->lock);
554 driver->disconnect(&mtu->g);
555 spin_lock(&mtu->lock);
559 static int mtu3_gadget_stop(struct usb_gadget *g)
561 struct mtu3 *mtu = gadget_to_mtu3(g);
562 unsigned long flags;
564 dev_dbg(mtu->dev, "%s\n", __func__);
566 spin_lock_irqsave(&mtu->lock, flags);
568 stop_activity(mtu);
569 mtu->gadget_driver = NULL;
571 if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
572 mtu3_stop(mtu);
574 spin_unlock_irqrestore(&mtu->lock, flags);
576 return 0;
579 static const struct usb_gadget_ops mtu3_gadget_ops = {
580 .get_frame = mtu3_gadget_get_frame,
581 .wakeup = mtu3_gadget_wakeup,
582 .set_selfpowered = mtu3_gadget_set_self_powered,
583 .pullup = mtu3_gadget_pullup,
584 .udc_start = mtu3_gadget_start,
585 .udc_stop = mtu3_gadget_stop,
588 static void mtu3_state_reset(struct mtu3 *mtu)
590 mtu->address = 0;
591 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
592 mtu->may_wakeup = 0;
593 mtu->u1_enable = 0;
594 mtu->u2_enable = 0;
595 mtu->delayed_status = false;
596 mtu->test_mode = false;
599 static void init_hw_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
600 u32 epnum, u32 is_in)
602 mep->epnum = epnum;
603 mep->mtu = mtu;
604 mep->is_in = is_in;
606 INIT_LIST_HEAD(&mep->req_list);
608 sprintf(mep->name, "ep%d%s", epnum,
609 !epnum ? "" : (is_in ? "in" : "out"));
611 mep->ep.name = mep->name;
612 INIT_LIST_HEAD(&mep->ep.ep_list);
614 /* initialize maxpacket as SS */
615 if (!epnum) {
616 usb_ep_set_maxpacket_limit(&mep->ep, 512);
617 mep->ep.caps.type_control = true;
618 mep->ep.ops = &mtu3_ep0_ops;
619 mtu->g.ep0 = &mep->ep;
620 } else {
621 usb_ep_set_maxpacket_limit(&mep->ep, 1024);
622 mep->ep.caps.type_iso = true;
623 mep->ep.caps.type_bulk = true;
624 mep->ep.caps.type_int = true;
625 mep->ep.ops = &mtu3_ep_ops;
626 list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list);
629 dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name,
630 mep->ep.maxpacket);
632 if (!epnum) {
633 mep->ep.caps.dir_in = true;
634 mep->ep.caps.dir_out = true;
635 } else if (is_in) {
636 mep->ep.caps.dir_in = true;
637 } else {
638 mep->ep.caps.dir_out = true;
642 static void mtu3_gadget_init_eps(struct mtu3 *mtu)
644 u8 epnum;
646 /* initialize endpoint list just once */
647 INIT_LIST_HEAD(&(mtu->g.ep_list));
649 dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n",
650 __func__, mtu->num_eps);
652 init_hw_ep(mtu, mtu->ep0, 0, 0);
653 for (epnum = 1; epnum < mtu->num_eps; epnum++) {
654 init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1);
655 init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0);
659 int mtu3_gadget_setup(struct mtu3 *mtu)
661 int ret;
663 mtu->g.ops = &mtu3_gadget_ops;
664 mtu->g.max_speed = mtu->max_speed;
665 mtu->g.speed = USB_SPEED_UNKNOWN;
666 mtu->g.sg_supported = 0;
667 mtu->g.name = MTU3_DRIVER_NAME;
668 mtu->is_active = 0;
669 mtu->delayed_status = false;
671 mtu3_gadget_init_eps(mtu);
673 ret = usb_add_gadget_udc(mtu->dev, &mtu->g);
674 if (ret)
675 dev_err(mtu->dev, "failed to register udc\n");
677 return ret;
680 void mtu3_gadget_cleanup(struct mtu3 *mtu)
682 usb_del_gadget_udc(&mtu->g);
685 void mtu3_gadget_resume(struct mtu3 *mtu)
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);
695 /* called when SOF packets stop for 3+ msec or enters U3 */
696 void mtu3_gadget_suspend(struct mtu3 *mtu)
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);
706 /* called when VBUS drops below session threshold, and in other cases */
707 void mtu3_gadget_disconnect(struct mtu3 *mtu)
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);
716 mtu3_state_reset(mtu);
717 usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED);
720 void mtu3_gadget_reset(struct mtu3 *mtu)
722 dev_dbg(mtu->dev, "gadget RESET\n");
724 /* report disconnect, if we didn't flush EP state */
725 if (mtu->g.speed != USB_SPEED_UNKNOWN)
726 mtu3_gadget_disconnect(mtu);
727 else
728 mtu3_state_reset(mtu);