Merge tag 'net-6.12-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux.git] / drivers / usb / mtu3 / mtu3_gadget_ep0.c
blobe4fd1bb14a55cb9ede0e86b73dde24d0c5da7595
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mtu3_gadget_ep0.c - MediaTek USB3 DRD peripheral driver ep0 handling
5 * Copyright (c) 2016 MediaTek Inc.
7 * Author: Chunfeng.Yun <chunfeng.yun@mediatek.com>
8 */
10 #include <linux/iopoll.h>
11 #include <linux/usb/composite.h>
13 #include "mtu3.h"
14 #include "mtu3_debug.h"
15 #include "mtu3_trace.h"
17 /* ep0 is always mtu3->in_eps[0] */
18 #define next_ep0_request(mtu) next_request((mtu)->ep0)
20 /* for high speed test mode; see USB 2.0 spec 7.1.20 */
21 static const u8 mtu3_test_packet[53] = {
22 /* implicit SYNC then DATA0 to start */
24 /* JKJKJKJK x9 */
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26 /* JJKKJJKK x8 */
27 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
28 /* JJJJKKKK x8 */
29 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
30 /* JJJJJJJKKKKKKK x8 */
31 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
32 /* JJJJJJJK x8 */
33 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
34 /* JKKKKKKK x10, JK */
35 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e,
36 /* implicit CRC16 then EOP to end */
39 static char *decode_ep0_state(struct mtu3 *mtu)
41 switch (mtu->ep0_state) {
42 case MU3D_EP0_STATE_SETUP:
43 return "SETUP";
44 case MU3D_EP0_STATE_TX:
45 return "IN";
46 case MU3D_EP0_STATE_RX:
47 return "OUT";
48 case MU3D_EP0_STATE_TX_END:
49 return "TX-END";
50 case MU3D_EP0_STATE_STALL:
51 return "STALL";
52 default:
53 return "??";
57 static void ep0_req_giveback(struct mtu3 *mtu, struct usb_request *req)
59 mtu3_req_complete(mtu->ep0, req, 0);
62 static int
63 forward_to_driver(struct mtu3 *mtu, const struct usb_ctrlrequest *setup)
64 __releases(mtu->lock)
65 __acquires(mtu->lock)
67 int ret;
69 if (!mtu->gadget_driver || !mtu->async_callbacks)
70 return -EOPNOTSUPP;
72 spin_unlock(&mtu->lock);
73 ret = mtu->gadget_driver->setup(&mtu->g, setup);
74 spin_lock(&mtu->lock);
76 dev_dbg(mtu->dev, "%s ret %d\n", __func__, ret);
77 return ret;
80 static void ep0_write_fifo(struct mtu3_ep *mep, const u8 *src, u16 len)
82 void __iomem *fifo = mep->mtu->mac_base + U3D_FIFO0;
83 u16 index = 0;
85 dev_dbg(mep->mtu->dev, "%s: ep%din, len=%d, buf=%p\n",
86 __func__, mep->epnum, len, src);
88 if (len >= 4) {
89 iowrite32_rep(fifo, src, len >> 2);
90 index = len & ~0x03;
92 if (len & 0x02) {
93 writew(*(u16 *)&src[index], fifo);
94 index += 2;
96 if (len & 0x01)
97 writeb(src[index], fifo);
100 static void ep0_read_fifo(struct mtu3_ep *mep, u8 *dst, u16 len)
102 void __iomem *fifo = mep->mtu->mac_base + U3D_FIFO0;
103 u32 value;
104 u16 index = 0;
106 dev_dbg(mep->mtu->dev, "%s: ep%dout len=%d buf=%p\n",
107 __func__, mep->epnum, len, dst);
109 if (len >= 4) {
110 ioread32_rep(fifo, dst, len >> 2);
111 index = len & ~0x03;
113 if (len & 0x3) {
114 value = readl(fifo);
115 memcpy(&dst[index], &value, len & 0x3);
120 static void ep0_load_test_packet(struct mtu3 *mtu)
123 * because the length of test packet is less than max packet of HS ep0,
124 * write it into fifo directly.
126 ep0_write_fifo(mtu->ep0, mtu3_test_packet, sizeof(mtu3_test_packet));
130 * A. send STALL for setup transfer without data stage:
131 * set SENDSTALL and SETUPPKTRDY at the same time;
132 * B. send STALL for other cases:
133 * set SENDSTALL only.
135 static void ep0_stall_set(struct mtu3_ep *mep0, bool set, u32 pktrdy)
137 struct mtu3 *mtu = mep0->mtu;
138 void __iomem *mbase = mtu->mac_base;
139 u32 csr;
141 /* EP0_SENTSTALL is W1C */
142 csr = mtu3_readl(mbase, U3D_EP0CSR) & EP0_W1C_BITS;
143 if (set)
144 csr |= EP0_SENDSTALL | pktrdy;
145 else
146 csr = (csr & ~EP0_SENDSTALL) | EP0_SENTSTALL;
147 mtu3_writel(mtu->mac_base, U3D_EP0CSR, csr);
149 mtu->delayed_status = false;
150 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
152 dev_dbg(mtu->dev, "ep0: %s STALL, ep0_state: %s\n",
153 set ? "SEND" : "CLEAR", decode_ep0_state(mtu));
156 static void ep0_do_status_stage(struct mtu3 *mtu)
158 void __iomem *mbase = mtu->mac_base;
159 u32 value;
161 value = mtu3_readl(mbase, U3D_EP0CSR) & EP0_W1C_BITS;
162 mtu3_writel(mbase, U3D_EP0CSR, value | EP0_SETUPPKTRDY | EP0_DATAEND);
165 static int ep0_queue(struct mtu3_ep *mep0, struct mtu3_request *mreq);
167 static void ep0_dummy_complete(struct usb_ep *ep, struct usb_request *req)
170 static void ep0_set_sel_complete(struct usb_ep *ep, struct usb_request *req)
172 struct mtu3_request *mreq;
173 struct mtu3 *mtu;
174 struct usb_set_sel_req sel;
176 memcpy(&sel, req->buf, sizeof(sel));
178 mreq = to_mtu3_request(req);
179 mtu = mreq->mtu;
180 dev_dbg(mtu->dev, "u1sel:%d, u1pel:%d, u2sel:%d, u2pel:%d\n",
181 sel.u1_sel, sel.u1_pel, sel.u2_sel, sel.u2_pel);
184 /* queue data stage to handle 6 byte SET_SEL request */
185 static int ep0_set_sel(struct mtu3 *mtu, struct usb_ctrlrequest *setup)
187 int ret;
188 u16 length = le16_to_cpu(setup->wLength);
190 if (unlikely(length != 6)) {
191 dev_err(mtu->dev, "%s wrong wLength:%d\n",
192 __func__, length);
193 return -EINVAL;
196 mtu->ep0_req.mep = mtu->ep0;
197 mtu->ep0_req.request.length = 6;
198 mtu->ep0_req.request.buf = mtu->setup_buf;
199 mtu->ep0_req.request.complete = ep0_set_sel_complete;
200 ret = ep0_queue(mtu->ep0, &mtu->ep0_req);
202 return ret < 0 ? ret : 1;
205 static int
206 ep0_get_status(struct mtu3 *mtu, const struct usb_ctrlrequest *setup)
208 struct mtu3_ep *mep = NULL;
209 int handled = 1;
210 u8 result[2] = {0, 0};
211 u8 epnum = 0;
212 int is_in;
214 switch (setup->bRequestType & USB_RECIP_MASK) {
215 case USB_RECIP_DEVICE:
216 result[0] = mtu->is_self_powered << USB_DEVICE_SELF_POWERED;
217 result[0] |= mtu->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
219 if (mtu->g.speed >= USB_SPEED_SUPER) {
220 result[0] |= mtu->u1_enable << USB_DEV_STAT_U1_ENABLED;
221 result[0] |= mtu->u2_enable << USB_DEV_STAT_U2_ENABLED;
224 dev_dbg(mtu->dev, "%s result=%x, U1=%x, U2=%x\n", __func__,
225 result[0], mtu->u1_enable, mtu->u2_enable);
227 break;
228 case USB_RECIP_INTERFACE:
229 /* status of function remote wakeup, forward request */
230 handled = 0;
231 break;
232 case USB_RECIP_ENDPOINT:
233 epnum = (u8) le16_to_cpu(setup->wIndex);
234 is_in = epnum & USB_DIR_IN;
235 epnum &= USB_ENDPOINT_NUMBER_MASK;
237 if (epnum >= mtu->num_eps) {
238 handled = -EINVAL;
239 break;
241 if (!epnum)
242 break;
244 mep = (is_in ? mtu->in_eps : mtu->out_eps) + epnum;
245 if (!mep->desc) {
246 handled = -EINVAL;
247 break;
249 if (mep->flags & MTU3_EP_STALL)
250 result[0] |= 1 << USB_ENDPOINT_HALT;
252 break;
253 default:
254 /* class, vendor, etc ... delegate */
255 handled = 0;
256 break;
259 if (handled > 0) {
260 int ret;
262 /* prepare a data stage for GET_STATUS */
263 dev_dbg(mtu->dev, "get_status=%x\n", *(u16 *)result);
264 memcpy(mtu->setup_buf, result, sizeof(result));
265 mtu->ep0_req.mep = mtu->ep0;
266 mtu->ep0_req.request.length = 2;
267 mtu->ep0_req.request.buf = &mtu->setup_buf;
268 mtu->ep0_req.request.complete = ep0_dummy_complete;
269 ret = ep0_queue(mtu->ep0, &mtu->ep0_req);
270 if (ret < 0)
271 handled = ret;
273 return handled;
276 static int handle_test_mode(struct mtu3 *mtu, struct usb_ctrlrequest *setup)
278 void __iomem *mbase = mtu->mac_base;
279 int handled = 1;
280 u32 value;
282 switch (le16_to_cpu(setup->wIndex) >> 8) {
283 case USB_TEST_J:
284 dev_dbg(mtu->dev, "USB_TEST_J\n");
285 mtu->test_mode_nr = TEST_J_MODE;
286 break;
287 case USB_TEST_K:
288 dev_dbg(mtu->dev, "USB_TEST_K\n");
289 mtu->test_mode_nr = TEST_K_MODE;
290 break;
291 case USB_TEST_SE0_NAK:
292 dev_dbg(mtu->dev, "USB_TEST_SE0_NAK\n");
293 mtu->test_mode_nr = TEST_SE0_NAK_MODE;
294 break;
295 case USB_TEST_PACKET:
296 dev_dbg(mtu->dev, "USB_TEST_PACKET\n");
297 mtu->test_mode_nr = TEST_PACKET_MODE;
298 break;
299 default:
300 handled = -EINVAL;
301 goto out;
304 mtu->test_mode = true;
306 /* no TX completion interrupt, and need restart platform after test */
307 if (mtu->test_mode_nr == TEST_PACKET_MODE)
308 ep0_load_test_packet(mtu);
310 /* send status before entering test mode. */
311 ep0_do_status_stage(mtu);
313 /* wait for ACK status sent by host */
314 readl_poll_timeout_atomic(mbase + U3D_EP0CSR, value,
315 !(value & EP0_DATAEND), 100, 5000);
317 mtu3_writel(mbase, U3D_USB2_TEST_MODE, mtu->test_mode_nr);
319 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
321 out:
322 return handled;
325 static int ep0_handle_feature_dev(struct mtu3 *mtu,
326 struct usb_ctrlrequest *setup, bool set)
328 void __iomem *mbase = mtu->mac_base;
329 int handled = -EINVAL;
330 u32 lpc;
332 switch (le16_to_cpu(setup->wValue)) {
333 case USB_DEVICE_REMOTE_WAKEUP:
334 mtu->may_wakeup = !!set;
335 handled = 1;
336 break;
337 case USB_DEVICE_TEST_MODE:
338 if (!set || (mtu->g.speed != USB_SPEED_HIGH) ||
339 (le16_to_cpu(setup->wIndex) & 0xff))
340 break;
342 handled = handle_test_mode(mtu, setup);
343 break;
344 case USB_DEVICE_U1_ENABLE:
345 if (mtu->g.speed < USB_SPEED_SUPER ||
346 mtu->g.state != USB_STATE_CONFIGURED)
347 break;
349 lpc = mtu3_readl(mbase, U3D_LINK_POWER_CONTROL);
350 if (set)
351 lpc |= SW_U1_REQUEST_ENABLE;
352 else
353 lpc &= ~SW_U1_REQUEST_ENABLE;
354 mtu3_writel(mbase, U3D_LINK_POWER_CONTROL, lpc);
356 mtu->u1_enable = !!set;
357 handled = 1;
358 break;
359 case USB_DEVICE_U2_ENABLE:
360 if (mtu->g.speed < USB_SPEED_SUPER ||
361 mtu->g.state != USB_STATE_CONFIGURED)
362 break;
364 lpc = mtu3_readl(mbase, U3D_LINK_POWER_CONTROL);
365 if (set)
366 lpc |= SW_U2_REQUEST_ENABLE;
367 else
368 lpc &= ~SW_U2_REQUEST_ENABLE;
369 mtu3_writel(mbase, U3D_LINK_POWER_CONTROL, lpc);
371 mtu->u2_enable = !!set;
372 handled = 1;
373 break;
374 default:
375 handled = -EINVAL;
376 break;
378 return handled;
381 static int ep0_handle_feature(struct mtu3 *mtu,
382 struct usb_ctrlrequest *setup, bool set)
384 struct mtu3_ep *mep;
385 int handled = -EINVAL;
386 int is_in;
387 u16 value;
388 u16 index;
389 u8 epnum;
391 value = le16_to_cpu(setup->wValue);
392 index = le16_to_cpu(setup->wIndex);
394 switch (setup->bRequestType & USB_RECIP_MASK) {
395 case USB_RECIP_DEVICE:
396 handled = ep0_handle_feature_dev(mtu, setup, set);
397 break;
398 case USB_RECIP_INTERFACE:
399 /* superspeed only */
400 if (value == USB_INTRF_FUNC_SUSPEND &&
401 mtu->g.speed >= USB_SPEED_SUPER) {
402 /* forward the request for function suspend */
403 mtu->may_wakeup = !!(index & USB_INTRF_FUNC_SUSPEND_RW);
404 handled = 0;
406 break;
407 case USB_RECIP_ENDPOINT:
408 epnum = index & USB_ENDPOINT_NUMBER_MASK;
409 if (epnum == 0 || epnum >= mtu->num_eps ||
410 value != USB_ENDPOINT_HALT)
411 break;
413 is_in = index & USB_DIR_IN;
414 mep = (is_in ? mtu->in_eps : mtu->out_eps) + epnum;
415 if (!mep->desc)
416 break;
418 handled = 1;
419 /* ignore request if endpoint is wedged */
420 if (mep->flags & MTU3_EP_WEDGE)
421 break;
423 mtu3_ep_stall_set(mep, set);
424 break;
425 default:
426 /* class, vendor, etc ... delegate */
427 handled = 0;
428 break;
430 return handled;
434 * handle all control requests can be handled
435 * returns:
436 * negative errno - error happened
437 * zero - need delegate SETUP to gadget driver
438 * positive - already handled
440 static int handle_standard_request(struct mtu3 *mtu,
441 struct usb_ctrlrequest *setup)
443 void __iomem *mbase = mtu->mac_base;
444 enum usb_device_state state = mtu->g.state;
445 int handled = -EINVAL;
446 u32 dev_conf;
447 u16 value;
449 value = le16_to_cpu(setup->wValue);
451 /* the gadget driver handles everything except what we must handle */
452 switch (setup->bRequest) {
453 case USB_REQ_SET_ADDRESS:
454 /* change it after the status stage */
455 mtu->address = (u8) (value & 0x7f);
456 dev_dbg(mtu->dev, "set address to 0x%x\n", mtu->address);
458 dev_conf = mtu3_readl(mbase, U3D_DEVICE_CONF);
459 dev_conf &= ~DEV_ADDR_MSK;
460 dev_conf |= DEV_ADDR(mtu->address);
461 mtu3_writel(mbase, U3D_DEVICE_CONF, dev_conf);
463 if (mtu->address)
464 usb_gadget_set_state(&mtu->g, USB_STATE_ADDRESS);
465 else
466 usb_gadget_set_state(&mtu->g, USB_STATE_DEFAULT);
468 handled = 1;
469 break;
470 case USB_REQ_SET_CONFIGURATION:
471 if (state == USB_STATE_ADDRESS) {
472 usb_gadget_set_state(&mtu->g,
473 USB_STATE_CONFIGURED);
474 } else if (state == USB_STATE_CONFIGURED) {
476 * USB2 spec sec 9.4.7, if wValue is 0 then dev
477 * is moved to addressed state
479 if (!value)
480 usb_gadget_set_state(&mtu->g,
481 USB_STATE_ADDRESS);
483 handled = 0;
484 break;
485 case USB_REQ_CLEAR_FEATURE:
486 handled = ep0_handle_feature(mtu, setup, 0);
487 break;
488 case USB_REQ_SET_FEATURE:
489 handled = ep0_handle_feature(mtu, setup, 1);
490 break;
491 case USB_REQ_GET_STATUS:
492 handled = ep0_get_status(mtu, setup);
493 break;
494 case USB_REQ_SET_SEL:
495 handled = ep0_set_sel(mtu, setup);
496 break;
497 case USB_REQ_SET_ISOCH_DELAY:
498 handled = 1;
499 break;
500 default:
501 /* delegate SET_CONFIGURATION, etc */
502 handled = 0;
505 return handled;
508 /* receive an data packet (OUT) */
509 static void ep0_rx_state(struct mtu3 *mtu)
511 struct mtu3_request *mreq;
512 struct usb_request *req;
513 void __iomem *mbase = mtu->mac_base;
514 u32 maxp;
515 u32 csr;
516 u16 count = 0;
518 dev_dbg(mtu->dev, "%s\n", __func__);
520 csr = mtu3_readl(mbase, U3D_EP0CSR) & EP0_W1C_BITS;
521 mreq = next_ep0_request(mtu);
522 req = &mreq->request;
524 /* read packet and ack; or stall because of gadget driver bug */
525 if (req) {
526 void *buf = req->buf + req->actual;
527 unsigned int len = req->length - req->actual;
529 /* read the buffer */
530 count = mtu3_readl(mbase, U3D_RXCOUNT0);
531 if (count > len) {
532 req->status = -EOVERFLOW;
533 count = len;
535 ep0_read_fifo(mtu->ep0, buf, count);
536 req->actual += count;
537 csr |= EP0_RXPKTRDY;
539 maxp = mtu->g.ep0->maxpacket;
540 if (count < maxp || req->actual == req->length) {
541 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
542 dev_dbg(mtu->dev, "ep0 state: %s\n",
543 decode_ep0_state(mtu));
545 csr |= EP0_DATAEND;
546 } else {
547 req = NULL;
549 } else {
550 csr |= EP0_RXPKTRDY | EP0_SENDSTALL;
551 dev_dbg(mtu->dev, "%s: SENDSTALL\n", __func__);
554 mtu3_writel(mbase, U3D_EP0CSR, csr);
556 /* give back the request if have received all data */
557 if (req)
558 ep0_req_giveback(mtu, req);
562 /* transmitting to the host (IN) */
563 static void ep0_tx_state(struct mtu3 *mtu)
565 struct mtu3_request *mreq = next_ep0_request(mtu);
566 struct usb_request *req;
567 u32 csr;
568 u8 *src;
569 u32 count;
570 u32 maxp;
572 dev_dbg(mtu->dev, "%s\n", __func__);
574 if (!mreq)
575 return;
577 maxp = mtu->g.ep0->maxpacket;
578 req = &mreq->request;
580 /* load the data */
581 src = (u8 *)req->buf + req->actual;
582 count = min(maxp, req->length - req->actual);
583 if (count)
584 ep0_write_fifo(mtu->ep0, src, count);
586 dev_dbg(mtu->dev, "%s act=%d, len=%d, cnt=%d, maxp=%d zero=%d\n",
587 __func__, req->actual, req->length, count, maxp, req->zero);
589 req->actual += count;
591 if ((count < maxp)
592 || ((req->actual == req->length) && !req->zero))
593 mtu->ep0_state = MU3D_EP0_STATE_TX_END;
595 /* send it out, triggering a "txpktrdy cleared" irq */
596 csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR) & EP0_W1C_BITS;
597 mtu3_writel(mtu->mac_base, U3D_EP0CSR, csr | EP0_TXPKTRDY);
599 dev_dbg(mtu->dev, "%s ep0csr=0x%x\n", __func__,
600 mtu3_readl(mtu->mac_base, U3D_EP0CSR));
603 static void ep0_read_setup(struct mtu3 *mtu, struct usb_ctrlrequest *setup)
605 struct mtu3_request *mreq;
606 u32 count;
607 u32 csr;
609 csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR) & EP0_W1C_BITS;
610 count = mtu3_readl(mtu->mac_base, U3D_RXCOUNT0);
612 ep0_read_fifo(mtu->ep0, (u8 *)setup, count);
614 dev_dbg(mtu->dev, "SETUP req%02x.%02x v%04x i%04x l%04x\n",
615 setup->bRequestType, setup->bRequest,
616 le16_to_cpu(setup->wValue), le16_to_cpu(setup->wIndex),
617 le16_to_cpu(setup->wLength));
619 /* clean up any leftover transfers */
620 mreq = next_ep0_request(mtu);
621 if (mreq)
622 ep0_req_giveback(mtu, &mreq->request);
624 if (le16_to_cpu(setup->wLength) == 0) {
625 ; /* no data stage, nothing to do */
626 } else if (setup->bRequestType & USB_DIR_IN) {
627 mtu3_writel(mtu->mac_base, U3D_EP0CSR,
628 csr | EP0_SETUPPKTRDY | EP0_DPHTX);
629 mtu->ep0_state = MU3D_EP0_STATE_TX;
630 } else {
631 mtu3_writel(mtu->mac_base, U3D_EP0CSR,
632 (csr | EP0_SETUPPKTRDY) & (~EP0_DPHTX));
633 mtu->ep0_state = MU3D_EP0_STATE_RX;
637 static int ep0_handle_setup(struct mtu3 *mtu)
638 __releases(mtu->lock)
639 __acquires(mtu->lock)
641 struct usb_ctrlrequest setup;
642 struct mtu3_request *mreq;
643 int handled = 0;
645 ep0_read_setup(mtu, &setup);
646 trace_mtu3_handle_setup(&setup);
648 if ((setup.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
649 handled = handle_standard_request(mtu, &setup);
651 dev_dbg(mtu->dev, "handled %d, ep0_state: %s\n",
652 handled, decode_ep0_state(mtu));
654 if (handled < 0)
655 goto stall;
656 else if (handled > 0)
657 goto finish;
659 handled = forward_to_driver(mtu, &setup);
660 if (handled < 0) {
661 stall:
662 dev_dbg(mtu->dev, "%s stall (%d)\n", __func__, handled);
664 ep0_stall_set(mtu->ep0, true,
665 le16_to_cpu(setup.wLength) ? 0 : EP0_SETUPPKTRDY);
667 return 0;
670 finish:
671 if (mtu->test_mode) {
672 ; /* nothing to do */
673 } else if (handled == USB_GADGET_DELAYED_STATUS) {
675 mreq = next_ep0_request(mtu);
676 if (mreq) {
677 /* already asked us to continue delayed status */
678 ep0_do_status_stage(mtu);
679 ep0_req_giveback(mtu, &mreq->request);
680 } else {
681 /* do delayed STATUS stage till receive ep0_queue */
682 mtu->delayed_status = true;
684 } else if (le16_to_cpu(setup.wLength) == 0) { /* no data stage */
686 ep0_do_status_stage(mtu);
687 /* complete zlp request directly */
688 mreq = next_ep0_request(mtu);
689 if (mreq && !mreq->request.length)
690 ep0_req_giveback(mtu, &mreq->request);
693 return 0;
696 irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu)
698 void __iomem *mbase = mtu->mac_base;
699 struct mtu3_request *mreq;
700 u32 int_status;
701 irqreturn_t ret = IRQ_NONE;
702 u32 csr;
703 u32 len;
705 int_status = mtu3_readl(mbase, U3D_EPISR);
706 int_status &= mtu3_readl(mbase, U3D_EPIER);
707 mtu3_writel(mbase, U3D_EPISR, int_status); /* W1C */
709 /* only handle ep0's */
710 if (!(int_status & (EP0ISR | SETUPENDISR)))
711 return IRQ_NONE;
713 /* abort current SETUP, and process new one */
714 if (int_status & SETUPENDISR)
715 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
717 csr = mtu3_readl(mbase, U3D_EP0CSR);
719 dev_dbg(mtu->dev, "%s csr=0x%x\n", __func__, csr);
721 /* we sent a stall.. need to clear it now.. */
722 if (csr & EP0_SENTSTALL) {
723 ep0_stall_set(mtu->ep0, false, 0);
724 csr = mtu3_readl(mbase, U3D_EP0CSR);
725 ret = IRQ_HANDLED;
727 dev_dbg(mtu->dev, "ep0_state: %s\n", decode_ep0_state(mtu));
728 mtu3_dbg_trace(mtu->dev, "ep0_state %s", decode_ep0_state(mtu));
730 switch (mtu->ep0_state) {
731 case MU3D_EP0_STATE_TX:
732 /* irq on clearing txpktrdy */
733 if ((csr & EP0_FIFOFULL) == 0) {
734 ep0_tx_state(mtu);
735 ret = IRQ_HANDLED;
737 break;
738 case MU3D_EP0_STATE_RX:
739 /* irq on set rxpktrdy */
740 if (csr & EP0_RXPKTRDY) {
741 ep0_rx_state(mtu);
742 ret = IRQ_HANDLED;
744 break;
745 case MU3D_EP0_STATE_TX_END:
746 mtu3_writel(mbase, U3D_EP0CSR,
747 (csr & EP0_W1C_BITS) | EP0_DATAEND);
749 mreq = next_ep0_request(mtu);
750 if (mreq)
751 ep0_req_giveback(mtu, &mreq->request);
753 mtu->ep0_state = MU3D_EP0_STATE_SETUP;
754 ret = IRQ_HANDLED;
755 dev_dbg(mtu->dev, "ep0_state: %s\n", decode_ep0_state(mtu));
756 break;
757 case MU3D_EP0_STATE_SETUP:
758 if (!(csr & EP0_SETUPPKTRDY))
759 break;
761 len = mtu3_readl(mbase, U3D_RXCOUNT0);
762 if (len != 8) {
763 dev_err(mtu->dev, "SETUP packet len %d != 8 ?\n", len);
764 break;
767 ep0_handle_setup(mtu);
768 ret = IRQ_HANDLED;
769 break;
770 default:
771 /* can't happen */
772 ep0_stall_set(mtu->ep0, true, 0);
773 WARN_ON(1);
774 break;
777 return ret;
781 static int mtu3_ep0_enable(struct usb_ep *ep,
782 const struct usb_endpoint_descriptor *desc)
784 /* always enabled */
785 return -EINVAL;
788 static int mtu3_ep0_disable(struct usb_ep *ep)
790 /* always enabled */
791 return -EINVAL;
794 static int ep0_queue(struct mtu3_ep *mep, struct mtu3_request *mreq)
796 struct mtu3 *mtu = mep->mtu;
798 mreq->mtu = mtu;
799 mreq->request.actual = 0;
800 mreq->request.status = -EINPROGRESS;
802 dev_dbg(mtu->dev, "%s %s (ep0_state: %s), len#%d\n", __func__,
803 mep->name, decode_ep0_state(mtu), mreq->request.length);
805 switch (mtu->ep0_state) {
806 case MU3D_EP0_STATE_SETUP:
807 case MU3D_EP0_STATE_RX: /* control-OUT data */
808 case MU3D_EP0_STATE_TX: /* control-IN data */
809 break;
810 default:
811 dev_err(mtu->dev, "%s, error in ep0 state %s\n", __func__,
812 decode_ep0_state(mtu));
813 return -EINVAL;
816 if (mtu->delayed_status) {
818 mtu->delayed_status = false;
819 ep0_do_status_stage(mtu);
820 /* needn't giveback the request for handling delay STATUS */
821 return 0;
824 if (!list_empty(&mep->req_list))
825 return -EBUSY;
827 list_add_tail(&mreq->list, &mep->req_list);
829 /* sequence #1, IN ... start writing the data */
830 if (mtu->ep0_state == MU3D_EP0_STATE_TX)
831 ep0_tx_state(mtu);
833 return 0;
836 static int mtu3_ep0_queue(struct usb_ep *ep,
837 struct usb_request *req, gfp_t gfp)
839 struct mtu3_ep *mep;
840 struct mtu3_request *mreq;
841 struct mtu3 *mtu;
842 unsigned long flags;
843 int ret = 0;
845 if (!ep || !req)
846 return -EINVAL;
848 mep = to_mtu3_ep(ep);
849 mtu = mep->mtu;
850 mreq = to_mtu3_request(req);
852 spin_lock_irqsave(&mtu->lock, flags);
853 ret = ep0_queue(mep, mreq);
854 spin_unlock_irqrestore(&mtu->lock, flags);
855 return ret;
858 static int mtu3_ep0_dequeue(struct usb_ep *ep, struct usb_request *req)
860 /* we just won't support this */
861 return -EINVAL;
864 static int mtu3_ep0_halt(struct usb_ep *ep, int value)
866 struct mtu3_ep *mep;
867 struct mtu3 *mtu;
868 unsigned long flags;
869 int ret = 0;
871 if (!ep || !value)
872 return -EINVAL;
874 mep = to_mtu3_ep(ep);
875 mtu = mep->mtu;
877 dev_dbg(mtu->dev, "%s\n", __func__);
879 spin_lock_irqsave(&mtu->lock, flags);
881 if (!list_empty(&mep->req_list)) {
882 ret = -EBUSY;
883 goto cleanup;
886 switch (mtu->ep0_state) {
888 * stalls are usually issued after parsing SETUP packet, either
889 * directly in irq context from setup() or else later.
891 case MU3D_EP0_STATE_TX:
892 case MU3D_EP0_STATE_TX_END:
893 case MU3D_EP0_STATE_RX:
894 case MU3D_EP0_STATE_SETUP:
895 ep0_stall_set(mtu->ep0, true, 0);
896 break;
897 default:
898 dev_dbg(mtu->dev, "ep0 can't halt in state %s\n",
899 decode_ep0_state(mtu));
900 ret = -EINVAL;
903 cleanup:
904 spin_unlock_irqrestore(&mtu->lock, flags);
905 return ret;
908 const struct usb_ep_ops mtu3_ep0_ops = {
909 .enable = mtu3_ep0_enable,
910 .disable = mtu3_ep0_disable,
911 .alloc_request = mtu3_alloc_request,
912 .free_request = mtu3_free_request,
913 .queue = mtu3_ep0_queue,
914 .dequeue = mtu3_ep0_dequeue,
915 .set_halt = mtu3_ep0_halt,