1 // SPDX-License-Identifier: GPL-2.0
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>
10 #include <linux/iopoll.h>
11 #include <linux/usb/composite.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 */
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
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,
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
:
44 case MU3D_EP0_STATE_TX
:
46 case MU3D_EP0_STATE_RX
:
48 case MU3D_EP0_STATE_TX_END
:
50 case MU3D_EP0_STATE_STALL
:
57 static void ep0_req_giveback(struct mtu3
*mtu
, struct usb_request
*req
)
59 mtu3_req_complete(mtu
->ep0
, req
, 0);
63 forward_to_driver(struct mtu3
*mtu
, const struct usb_ctrlrequest
*setup
)
69 if (!mtu
->gadget_driver
|| !mtu
->async_callbacks
)
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
);
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
;
85 dev_dbg(mep
->mtu
->dev
, "%s: ep%din, len=%d, buf=%p\n",
86 __func__
, mep
->epnum
, len
, src
);
89 iowrite32_rep(fifo
, src
, len
>> 2);
93 writew(*(u16
*)&src
[index
], fifo
);
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
;
106 dev_dbg(mep
->mtu
->dev
, "%s: ep%dout len=%d buf=%p\n",
107 __func__
, mep
->epnum
, len
, dst
);
110 ioread32_rep(fifo
, dst
, len
>> 2);
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
;
141 /* EP0_SENTSTALL is W1C */
142 csr
= mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
;
144 csr
|= EP0_SENDSTALL
| pktrdy
;
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
;
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
;
174 struct usb_set_sel_req sel
;
176 memcpy(&sel
, req
->buf
, sizeof(sel
));
178 mreq
= to_mtu3_request(req
);
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
)
188 u16 length
= le16_to_cpu(setup
->wLength
);
190 if (unlikely(length
!= 6)) {
191 dev_err(mtu
->dev
, "%s wrong wLength:%d\n",
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;
206 ep0_get_status(struct mtu3
*mtu
, const struct usb_ctrlrequest
*setup
)
208 struct mtu3_ep
*mep
= NULL
;
210 u8 result
[2] = {0, 0};
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
);
228 case USB_RECIP_INTERFACE
:
229 /* status of function remote wakeup, forward request */
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
) {
244 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
249 if (mep
->flags
& MTU3_EP_STALL
)
250 result
[0] |= 1 << USB_ENDPOINT_HALT
;
254 /* class, vendor, etc ... delegate */
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
);
276 static int handle_test_mode(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
278 void __iomem
*mbase
= mtu
->mac_base
;
282 switch (le16_to_cpu(setup
->wIndex
) >> 8) {
284 dev_dbg(mtu
->dev
, "USB_TEST_J\n");
285 mtu
->test_mode_nr
= TEST_J_MODE
;
288 dev_dbg(mtu
->dev
, "USB_TEST_K\n");
289 mtu
->test_mode_nr
= TEST_K_MODE
;
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
;
295 case USB_TEST_PACKET
:
296 dev_dbg(mtu
->dev
, "USB_TEST_PACKET\n");
297 mtu
->test_mode_nr
= TEST_PACKET_MODE
;
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
;
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
;
332 switch (le16_to_cpu(setup
->wValue
)) {
333 case USB_DEVICE_REMOTE_WAKEUP
:
334 mtu
->may_wakeup
= !!set
;
337 case USB_DEVICE_TEST_MODE
:
338 if (!set
|| (mtu
->g
.speed
!= USB_SPEED_HIGH
) ||
339 (le16_to_cpu(setup
->wIndex
) & 0xff))
342 handled
= handle_test_mode(mtu
, setup
);
344 case USB_DEVICE_U1_ENABLE
:
345 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
346 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
349 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
351 lpc
|= SW_U1_REQUEST_ENABLE
;
353 lpc
&= ~SW_U1_REQUEST_ENABLE
;
354 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
356 mtu
->u1_enable
= !!set
;
359 case USB_DEVICE_U2_ENABLE
:
360 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
361 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
364 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
366 lpc
|= SW_U2_REQUEST_ENABLE
;
368 lpc
&= ~SW_U2_REQUEST_ENABLE
;
369 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
371 mtu
->u2_enable
= !!set
;
381 static int ep0_handle_feature(struct mtu3
*mtu
,
382 struct usb_ctrlrequest
*setup
, bool set
)
385 int handled
= -EINVAL
;
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
);
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
);
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
)
413 is_in
= index
& USB_DIR_IN
;
414 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
419 /* ignore request if endpoint is wedged */
420 if (mep
->flags
& MTU3_EP_WEDGE
)
423 mtu3_ep_stall_set(mep
, set
);
426 /* class, vendor, etc ... delegate */
434 * handle all control requests can be handled
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
;
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
);
464 usb_gadget_set_state(&mtu
->g
, USB_STATE_ADDRESS
);
466 usb_gadget_set_state(&mtu
->g
, USB_STATE_DEFAULT
);
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
480 usb_gadget_set_state(&mtu
->g
,
485 case USB_REQ_CLEAR_FEATURE
:
486 handled
= ep0_handle_feature(mtu
, setup
, 0);
488 case USB_REQ_SET_FEATURE
:
489 handled
= ep0_handle_feature(mtu
, setup
, 1);
491 case USB_REQ_GET_STATUS
:
492 handled
= ep0_get_status(mtu
, setup
);
494 case USB_REQ_SET_SEL
:
495 handled
= ep0_set_sel(mtu
, setup
);
497 case USB_REQ_SET_ISOCH_DELAY
:
501 /* delegate SET_CONFIGURATION, etc */
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
;
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 */
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
);
532 req
->status
= -EOVERFLOW
;
535 ep0_read_fifo(mtu
->ep0
, buf
, count
);
536 req
->actual
+= count
;
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
));
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 */
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
;
572 dev_dbg(mtu
->dev
, "%s\n", __func__
);
577 maxp
= mtu
->g
.ep0
->maxpacket
;
578 req
= &mreq
->request
;
581 src
= (u8
*)req
->buf
+ req
->actual
;
582 count
= min(maxp
, req
->length
- req
->actual
);
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
;
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
;
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
);
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
;
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
;
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
));
656 else if (handled
> 0)
659 handled
= forward_to_driver(mtu
, &setup
);
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
);
671 if (mtu
->test_mode
) {
672 ; /* nothing to do */
673 } else if (handled
== USB_GADGET_DELAYED_STATUS
) {
675 mreq
= next_ep0_request(mtu
);
677 /* already asked us to continue delayed status */
678 ep0_do_status_stage(mtu
);
679 ep0_req_giveback(mtu
, &mreq
->request
);
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
);
696 irqreturn_t
mtu3_ep0_isr(struct mtu3
*mtu
)
698 void __iomem
*mbase
= mtu
->mac_base
;
699 struct mtu3_request
*mreq
;
701 irqreturn_t ret
= IRQ_NONE
;
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
)))
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
);
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) {
738 case MU3D_EP0_STATE_RX
:
739 /* irq on set rxpktrdy */
740 if (csr
& EP0_RXPKTRDY
) {
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
);
751 ep0_req_giveback(mtu
, &mreq
->request
);
753 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
755 dev_dbg(mtu
->dev
, "ep0_state: %s\n", decode_ep0_state(mtu
));
757 case MU3D_EP0_STATE_SETUP
:
758 if (!(csr
& EP0_SETUPPKTRDY
))
761 len
= mtu3_readl(mbase
, U3D_RXCOUNT0
);
763 dev_err(mtu
->dev
, "SETUP packet len %d != 8 ?\n", len
);
767 ep0_handle_setup(mtu
);
772 ep0_stall_set(mtu
->ep0
, true, 0);
781 static int mtu3_ep0_enable(struct usb_ep
*ep
,
782 const struct usb_endpoint_descriptor
*desc
)
788 static int mtu3_ep0_disable(struct usb_ep
*ep
)
794 static int ep0_queue(struct mtu3_ep
*mep
, struct mtu3_request
*mreq
)
796 struct mtu3
*mtu
= mep
->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 */
811 dev_err(mtu
->dev
, "%s, error in ep0 state %s\n", __func__
,
812 decode_ep0_state(mtu
));
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 */
824 if (!list_empty(&mep
->req_list
))
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
)
836 static int mtu3_ep0_queue(struct usb_ep
*ep
,
837 struct usb_request
*req
, gfp_t gfp
)
840 struct mtu3_request
*mreq
;
848 mep
= to_mtu3_ep(ep
);
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
);
858 static int mtu3_ep0_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
860 /* we just won't support this */
864 static int mtu3_ep0_halt(struct usb_ep
*ep
, int value
)
874 mep
= to_mtu3_ep(ep
);
877 dev_dbg(mtu
->dev
, "%s\n", __func__
);
879 spin_lock_irqsave(&mtu
->lock
, flags
);
881 if (!list_empty(&mep
->req_list
)) {
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);
898 dev_dbg(mtu
->dev
, "ep0 can't halt in state %s\n",
899 decode_ep0_state(mtu
));
904 spin_unlock_irqrestore(&mtu
->lock
, flags
);
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
,