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
)
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
:
230 case USB_RECIP_ENDPOINT
:
231 epnum
= (u8
) le16_to_cpu(setup
->wIndex
);
232 is_in
= epnum
& USB_DIR_IN
;
233 epnum
&= USB_ENDPOINT_NUMBER_MASK
;
235 if (epnum
>= mtu
->num_eps
) {
242 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
247 if (mep
->flags
& MTU3_EP_STALL
)
248 result
[0] |= 1 << USB_ENDPOINT_HALT
;
252 /* class, vendor, etc ... delegate */
260 /* prepare a data stage for GET_STATUS */
261 dev_dbg(mtu
->dev
, "get_status=%x\n", *(u16
*)result
);
262 memcpy(mtu
->setup_buf
, result
, sizeof(result
));
263 mtu
->ep0_req
.mep
= mtu
->ep0
;
264 mtu
->ep0_req
.request
.length
= 2;
265 mtu
->ep0_req
.request
.buf
= &mtu
->setup_buf
;
266 mtu
->ep0_req
.request
.complete
= ep0_dummy_complete
;
267 ret
= ep0_queue(mtu
->ep0
, &mtu
->ep0_req
);
274 static int handle_test_mode(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
276 void __iomem
*mbase
= mtu
->mac_base
;
280 switch (le16_to_cpu(setup
->wIndex
) >> 8) {
282 dev_dbg(mtu
->dev
, "TEST_J\n");
283 mtu
->test_mode_nr
= TEST_J_MODE
;
286 dev_dbg(mtu
->dev
, "TEST_K\n");
287 mtu
->test_mode_nr
= TEST_K_MODE
;
290 dev_dbg(mtu
->dev
, "TEST_SE0_NAK\n");
291 mtu
->test_mode_nr
= TEST_SE0_NAK_MODE
;
294 dev_dbg(mtu
->dev
, "TEST_PACKET\n");
295 mtu
->test_mode_nr
= TEST_PACKET_MODE
;
302 mtu
->test_mode
= true;
304 /* no TX completion interrupt, and need restart platform after test */
305 if (mtu
->test_mode_nr
== TEST_PACKET_MODE
)
306 ep0_load_test_packet(mtu
);
308 /* send status before entering test mode. */
309 ep0_do_status_stage(mtu
);
311 /* wait for ACK status sent by host */
312 readl_poll_timeout_atomic(mbase
+ U3D_EP0CSR
, value
,
313 !(value
& EP0_DATAEND
), 100, 5000);
315 mtu3_writel(mbase
, U3D_USB2_TEST_MODE
, mtu
->test_mode_nr
);
317 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
323 static int ep0_handle_feature_dev(struct mtu3
*mtu
,
324 struct usb_ctrlrequest
*setup
, bool set
)
326 void __iomem
*mbase
= mtu
->mac_base
;
327 int handled
= -EINVAL
;
330 switch (le16_to_cpu(setup
->wValue
)) {
331 case USB_DEVICE_REMOTE_WAKEUP
:
332 mtu
->may_wakeup
= !!set
;
335 case USB_DEVICE_TEST_MODE
:
336 if (!set
|| (mtu
->g
.speed
!= USB_SPEED_HIGH
) ||
337 (le16_to_cpu(setup
->wIndex
) & 0xff))
340 handled
= handle_test_mode(mtu
, setup
);
342 case USB_DEVICE_U1_ENABLE
:
343 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
344 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
347 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
349 lpc
|= SW_U1_REQUEST_ENABLE
;
351 lpc
&= ~SW_U1_REQUEST_ENABLE
;
352 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
354 mtu
->u1_enable
= !!set
;
357 case USB_DEVICE_U2_ENABLE
:
358 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
359 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
362 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
364 lpc
|= SW_U2_REQUEST_ENABLE
;
366 lpc
&= ~SW_U2_REQUEST_ENABLE
;
367 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
369 mtu
->u2_enable
= !!set
;
379 static int ep0_handle_feature(struct mtu3
*mtu
,
380 struct usb_ctrlrequest
*setup
, bool set
)
383 int handled
= -EINVAL
;
389 value
= le16_to_cpu(setup
->wValue
);
390 index
= le16_to_cpu(setup
->wIndex
);
392 switch (setup
->bRequestType
& USB_RECIP_MASK
) {
393 case USB_RECIP_DEVICE
:
394 handled
= ep0_handle_feature_dev(mtu
, setup
, set
);
396 case USB_RECIP_INTERFACE
:
397 /* superspeed only */
398 if (value
== USB_INTRF_FUNC_SUSPEND
&&
399 mtu
->g
.speed
>= USB_SPEED_SUPER
) {
401 * forward the request because function drivers
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 */
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
,