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/usb/composite.h>
14 /* ep0 is always mtu3->in_eps[0] */
15 #define next_ep0_request(mtu) next_request((mtu)->ep0)
17 /* for high speed test mode; see USB 2.0 spec 7.1.20 */
18 static const u8 mtu3_test_packet
[53] = {
19 /* implicit SYNC then DATA0 to start */
22 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
26 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
27 /* JJJJJJJKKKKKKK x8 */
28 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
31 /* JKKKKKKK x10, JK */
32 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e,
33 /* implicit CRC16 then EOP to end */
36 static char *decode_ep0_state(struct mtu3
*mtu
)
38 switch (mtu
->ep0_state
) {
39 case MU3D_EP0_STATE_SETUP
:
41 case MU3D_EP0_STATE_TX
:
43 case MU3D_EP0_STATE_RX
:
45 case MU3D_EP0_STATE_TX_END
:
47 case MU3D_EP0_STATE_STALL
:
54 static void ep0_req_giveback(struct mtu3
*mtu
, struct usb_request
*req
)
56 mtu3_req_complete(mtu
->ep0
, req
, 0);
60 forward_to_driver(struct mtu3
*mtu
, const struct usb_ctrlrequest
*setup
)
66 if (!mtu
->gadget_driver
)
69 spin_unlock(&mtu
->lock
);
70 ret
= mtu
->gadget_driver
->setup(&mtu
->g
, setup
);
71 spin_lock(&mtu
->lock
);
73 dev_dbg(mtu
->dev
, "%s ret %d\n", __func__
, ret
);
77 static void ep0_write_fifo(struct mtu3_ep
*mep
, const u8
*src
, u16 len
)
79 void __iomem
*fifo
= mep
->mtu
->mac_base
+ U3D_FIFO0
;
82 dev_dbg(mep
->mtu
->dev
, "%s: ep%din, len=%d, buf=%p\n",
83 __func__
, mep
->epnum
, len
, src
);
86 iowrite32_rep(fifo
, src
, len
>> 2);
90 writew(*(u16
*)&src
[index
], fifo
);
94 writeb(src
[index
], fifo
);
97 static void ep0_read_fifo(struct mtu3_ep
*mep
, u8
*dst
, u16 len
)
99 void __iomem
*fifo
= mep
->mtu
->mac_base
+ U3D_FIFO0
;
103 dev_dbg(mep
->mtu
->dev
, "%s: ep%dout len=%d buf=%p\n",
104 __func__
, mep
->epnum
, len
, dst
);
107 ioread32_rep(fifo
, dst
, len
>> 2);
112 memcpy(&dst
[index
], &value
, len
& 0x3);
117 static void ep0_load_test_packet(struct mtu3
*mtu
)
120 * because the length of test packet is less than max packet of HS ep0,
121 * write it into fifo directly.
123 ep0_write_fifo(mtu
->ep0
, mtu3_test_packet
, sizeof(mtu3_test_packet
));
127 * A. send STALL for setup transfer without data stage:
128 * set SENDSTALL and SETUPPKTRDY at the same time;
129 * B. send STALL for other cases:
130 * set SENDSTALL only.
132 static void ep0_stall_set(struct mtu3_ep
*mep0
, bool set
, u32 pktrdy
)
134 struct mtu3
*mtu
= mep0
->mtu
;
135 void __iomem
*mbase
= mtu
->mac_base
;
138 /* EP0_SENTSTALL is W1C */
139 csr
= mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
;
141 csr
|= EP0_SENDSTALL
| pktrdy
;
143 csr
= (csr
& ~EP0_SENDSTALL
) | EP0_SENTSTALL
;
144 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
, csr
);
146 mtu
->delayed_status
= false;
147 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
149 dev_dbg(mtu
->dev
, "ep0: %s STALL, ep0_state: %s\n",
150 set
? "SEND" : "CLEAR", decode_ep0_state(mtu
));
153 static int ep0_queue(struct mtu3_ep
*mep0
, struct mtu3_request
*mreq
);
155 static void ep0_dummy_complete(struct usb_ep
*ep
, struct usb_request
*req
)
158 static void ep0_set_sel_complete(struct usb_ep
*ep
, struct usb_request
*req
)
160 struct mtu3_request
*mreq
;
162 struct usb_set_sel_req sel
;
164 memcpy(&sel
, req
->buf
, sizeof(sel
));
166 mreq
= to_mtu3_request(req
);
168 dev_dbg(mtu
->dev
, "u1sel:%d, u1pel:%d, u2sel:%d, u2pel:%d\n",
169 sel
.u1_sel
, sel
.u1_pel
, sel
.u2_sel
, sel
.u2_pel
);
172 /* queue data stage to handle 6 byte SET_SEL request */
173 static int ep0_set_sel(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
176 u16 length
= le16_to_cpu(setup
->wLength
);
178 if (unlikely(length
!= 6)) {
179 dev_err(mtu
->dev
, "%s wrong wLength:%d\n",
184 mtu
->ep0_req
.mep
= mtu
->ep0
;
185 mtu
->ep0_req
.request
.length
= 6;
186 mtu
->ep0_req
.request
.buf
= mtu
->setup_buf
;
187 mtu
->ep0_req
.request
.complete
= ep0_set_sel_complete
;
188 ret
= ep0_queue(mtu
->ep0
, &mtu
->ep0_req
);
190 return ret
< 0 ? ret
: 1;
194 ep0_get_status(struct mtu3
*mtu
, const struct usb_ctrlrequest
*setup
)
196 struct mtu3_ep
*mep
= NULL
;
198 u8 result
[2] = {0, 0};
202 switch (setup
->bRequestType
& USB_RECIP_MASK
) {
203 case USB_RECIP_DEVICE
:
204 result
[0] = mtu
->is_self_powered
<< USB_DEVICE_SELF_POWERED
;
205 result
[0] |= mtu
->may_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
;
207 if (mtu
->g
.speed
>= USB_SPEED_SUPER
) {
208 result
[0] |= mtu
->u1_enable
<< USB_DEV_STAT_U1_ENABLED
;
209 result
[0] |= mtu
->u2_enable
<< USB_DEV_STAT_U2_ENABLED
;
212 dev_dbg(mtu
->dev
, "%s result=%x, U1=%x, U2=%x\n", __func__
,
213 result
[0], mtu
->u1_enable
, mtu
->u2_enable
);
216 case USB_RECIP_INTERFACE
:
218 case USB_RECIP_ENDPOINT
:
219 epnum
= (u8
) le16_to_cpu(setup
->wIndex
);
220 is_in
= epnum
& USB_DIR_IN
;
221 epnum
&= USB_ENDPOINT_NUMBER_MASK
;
223 if (epnum
>= mtu
->num_eps
) {
230 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
235 if (mep
->flags
& MTU3_EP_STALL
)
236 result
[0] |= 1 << USB_ENDPOINT_HALT
;
240 /* class, vendor, etc ... delegate */
248 /* prepare a data stage for GET_STATUS */
249 dev_dbg(mtu
->dev
, "get_status=%x\n", *(u16
*)result
);
250 memcpy(mtu
->setup_buf
, result
, sizeof(result
));
251 mtu
->ep0_req
.mep
= mtu
->ep0
;
252 mtu
->ep0_req
.request
.length
= 2;
253 mtu
->ep0_req
.request
.buf
= &mtu
->setup_buf
;
254 mtu
->ep0_req
.request
.complete
= ep0_dummy_complete
;
255 ret
= ep0_queue(mtu
->ep0
, &mtu
->ep0_req
);
262 static int handle_test_mode(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
264 void __iomem
*mbase
= mtu
->mac_base
;
267 switch (le16_to_cpu(setup
->wIndex
) >> 8) {
269 dev_dbg(mtu
->dev
, "TEST_J\n");
270 mtu
->test_mode_nr
= TEST_J_MODE
;
273 dev_dbg(mtu
->dev
, "TEST_K\n");
274 mtu
->test_mode_nr
= TEST_K_MODE
;
277 dev_dbg(mtu
->dev
, "TEST_SE0_NAK\n");
278 mtu
->test_mode_nr
= TEST_SE0_NAK_MODE
;
281 dev_dbg(mtu
->dev
, "TEST_PACKET\n");
282 mtu
->test_mode_nr
= TEST_PACKET_MODE
;
289 mtu
->test_mode
= true;
291 /* no TX completion interrupt, and need restart platform after test */
292 if (mtu
->test_mode_nr
== TEST_PACKET_MODE
)
293 ep0_load_test_packet(mtu
);
295 mtu3_writel(mbase
, U3D_USB2_TEST_MODE
, mtu
->test_mode_nr
);
297 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
303 static int ep0_handle_feature_dev(struct mtu3
*mtu
,
304 struct usb_ctrlrequest
*setup
, bool set
)
306 void __iomem
*mbase
= mtu
->mac_base
;
307 int handled
= -EINVAL
;
310 switch (le16_to_cpu(setup
->wValue
)) {
311 case USB_DEVICE_REMOTE_WAKEUP
:
312 mtu
->may_wakeup
= !!set
;
315 case USB_DEVICE_TEST_MODE
:
316 if (!set
|| (mtu
->g
.speed
!= USB_SPEED_HIGH
) ||
317 (le16_to_cpu(setup
->wIndex
) & 0xff))
320 handled
= handle_test_mode(mtu
, setup
);
322 case USB_DEVICE_U1_ENABLE
:
323 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
324 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
327 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
329 lpc
|= SW_U1_ACCEPT_ENABLE
;
331 lpc
&= ~SW_U1_ACCEPT_ENABLE
;
332 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
334 mtu
->u1_enable
= !!set
;
337 case USB_DEVICE_U2_ENABLE
:
338 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
339 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
342 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
344 lpc
|= SW_U2_ACCEPT_ENABLE
;
346 lpc
&= ~SW_U2_ACCEPT_ENABLE
;
347 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
349 mtu
->u2_enable
= !!set
;
359 static int ep0_handle_feature(struct mtu3
*mtu
,
360 struct usb_ctrlrequest
*setup
, bool set
)
363 int handled
= -EINVAL
;
369 value
= le16_to_cpu(setup
->wValue
);
370 index
= le16_to_cpu(setup
->wIndex
);
372 switch (setup
->bRequestType
& USB_RECIP_MASK
) {
373 case USB_RECIP_DEVICE
:
374 handled
= ep0_handle_feature_dev(mtu
, setup
, set
);
376 case USB_RECIP_INTERFACE
:
377 /* superspeed only */
378 if (value
== USB_INTRF_FUNC_SUSPEND
&&
379 mtu
->g
.speed
>= USB_SPEED_SUPER
) {
381 * forward the request because function drivers
387 case USB_RECIP_ENDPOINT
:
388 epnum
= index
& USB_ENDPOINT_NUMBER_MASK
;
389 if (epnum
== 0 || epnum
>= mtu
->num_eps
||
390 value
!= USB_ENDPOINT_HALT
)
393 is_in
= index
& USB_DIR_IN
;
394 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
399 /* ignore request if endpoint is wedged */
403 mtu3_ep_stall_set(mep
, set
);
406 /* class, vendor, etc ... delegate */
414 * handle all control requests can be handled
416 * negative errno - error happened
417 * zero - need delegate SETUP to gadget driver
418 * positive - already handled
420 static int handle_standard_request(struct mtu3
*mtu
,
421 struct usb_ctrlrequest
*setup
)
423 void __iomem
*mbase
= mtu
->mac_base
;
424 enum usb_device_state state
= mtu
->g
.state
;
425 int handled
= -EINVAL
;
429 value
= le16_to_cpu(setup
->wValue
);
431 /* the gadget driver handles everything except what we must handle */
432 switch (setup
->bRequest
) {
433 case USB_REQ_SET_ADDRESS
:
434 /* change it after the status stage */
435 mtu
->address
= (u8
) (value
& 0x7f);
436 dev_dbg(mtu
->dev
, "set address to 0x%x\n", mtu
->address
);
438 dev_conf
= mtu3_readl(mbase
, U3D_DEVICE_CONF
);
439 dev_conf
&= ~DEV_ADDR_MSK
;
440 dev_conf
|= DEV_ADDR(mtu
->address
);
441 mtu3_writel(mbase
, U3D_DEVICE_CONF
, dev_conf
);
444 usb_gadget_set_state(&mtu
->g
, USB_STATE_ADDRESS
);
446 usb_gadget_set_state(&mtu
->g
, USB_STATE_DEFAULT
);
450 case USB_REQ_SET_CONFIGURATION
:
451 if (state
== USB_STATE_ADDRESS
) {
452 usb_gadget_set_state(&mtu
->g
,
453 USB_STATE_CONFIGURED
);
454 } else if (state
== USB_STATE_CONFIGURED
) {
456 * USB2 spec sec 9.4.7, if wValue is 0 then dev
457 * is moved to addressed state
460 usb_gadget_set_state(&mtu
->g
,
465 case USB_REQ_CLEAR_FEATURE
:
466 handled
= ep0_handle_feature(mtu
, setup
, 0);
468 case USB_REQ_SET_FEATURE
:
469 handled
= ep0_handle_feature(mtu
, setup
, 1);
471 case USB_REQ_GET_STATUS
:
472 handled
= ep0_get_status(mtu
, setup
);
474 case USB_REQ_SET_SEL
:
475 handled
= ep0_set_sel(mtu
, setup
);
477 case USB_REQ_SET_ISOCH_DELAY
:
481 /* delegate SET_CONFIGURATION, etc */
488 /* receive an data packet (OUT) */
489 static void ep0_rx_state(struct mtu3
*mtu
)
491 struct mtu3_request
*mreq
;
492 struct usb_request
*req
;
493 void __iomem
*mbase
= mtu
->mac_base
;
498 dev_dbg(mtu
->dev
, "%s\n", __func__
);
500 csr
= mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
;
501 mreq
= next_ep0_request(mtu
);
502 req
= &mreq
->request
;
504 /* read packet and ack; or stall because of gadget driver bug */
506 void *buf
= req
->buf
+ req
->actual
;
507 unsigned int len
= req
->length
- req
->actual
;
509 /* read the buffer */
510 count
= mtu3_readl(mbase
, U3D_RXCOUNT0
);
512 req
->status
= -EOVERFLOW
;
515 ep0_read_fifo(mtu
->ep0
, buf
, count
);
516 req
->actual
+= count
;
519 maxp
= mtu
->g
.ep0
->maxpacket
;
520 if (count
< maxp
|| req
->actual
== req
->length
) {
521 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
522 dev_dbg(mtu
->dev
, "ep0 state: %s\n",
523 decode_ep0_state(mtu
));
530 csr
|= EP0_RXPKTRDY
| EP0_SENDSTALL
;
531 dev_dbg(mtu
->dev
, "%s: SENDSTALL\n", __func__
);
534 mtu3_writel(mbase
, U3D_EP0CSR
, csr
);
536 /* give back the request if have received all data */
538 ep0_req_giveback(mtu
, req
);
542 /* transmitting to the host (IN) */
543 static void ep0_tx_state(struct mtu3
*mtu
)
545 struct mtu3_request
*mreq
= next_ep0_request(mtu
);
546 struct usb_request
*req
;
552 dev_dbg(mtu
->dev
, "%s\n", __func__
);
557 maxp
= mtu
->g
.ep0
->maxpacket
;
558 req
= &mreq
->request
;
561 src
= (u8
*)req
->buf
+ req
->actual
;
562 count
= min(maxp
, req
->length
- req
->actual
);
564 ep0_write_fifo(mtu
->ep0
, src
, count
);
566 dev_dbg(mtu
->dev
, "%s act=%d, len=%d, cnt=%d, maxp=%d zero=%d\n",
567 __func__
, req
->actual
, req
->length
, count
, maxp
, req
->zero
);
569 req
->actual
+= count
;
572 || ((req
->actual
== req
->length
) && !req
->zero
))
573 mtu
->ep0_state
= MU3D_EP0_STATE_TX_END
;
575 /* send it out, triggering a "txpktrdy cleared" irq */
576 csr
= mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
) & EP0_W1C_BITS
;
577 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
, csr
| EP0_TXPKTRDY
);
579 dev_dbg(mtu
->dev
, "%s ep0csr=0x%x\n", __func__
,
580 mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
));
583 static void ep0_read_setup(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
585 struct mtu3_request
*mreq
;
589 csr
= mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
) & EP0_W1C_BITS
;
590 count
= mtu3_readl(mtu
->mac_base
, U3D_RXCOUNT0
);
592 ep0_read_fifo(mtu
->ep0
, (u8
*)setup
, count
);
594 dev_dbg(mtu
->dev
, "SETUP req%02x.%02x v%04x i%04x l%04x\n",
595 setup
->bRequestType
, setup
->bRequest
,
596 le16_to_cpu(setup
->wValue
), le16_to_cpu(setup
->wIndex
),
597 le16_to_cpu(setup
->wLength
));
599 /* clean up any leftover transfers */
600 mreq
= next_ep0_request(mtu
);
602 ep0_req_giveback(mtu
, &mreq
->request
);
604 if (le16_to_cpu(setup
->wLength
) == 0) {
605 ; /* no data stage, nothing to do */
606 } else if (setup
->bRequestType
& USB_DIR_IN
) {
607 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
,
608 csr
| EP0_SETUPPKTRDY
| EP0_DPHTX
);
609 mtu
->ep0_state
= MU3D_EP0_STATE_TX
;
611 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
,
612 (csr
| EP0_SETUPPKTRDY
) & (~EP0_DPHTX
));
613 mtu
->ep0_state
= MU3D_EP0_STATE_RX
;
617 static int ep0_handle_setup(struct mtu3
*mtu
)
618 __releases(mtu
->lock
)
619 __acquires(mtu
->lock
)
621 struct usb_ctrlrequest setup
;
622 struct mtu3_request
*mreq
;
623 void __iomem
*mbase
= mtu
->mac_base
;
626 ep0_read_setup(mtu
, &setup
);
628 if ((setup
.bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
629 handled
= handle_standard_request(mtu
, &setup
);
631 dev_dbg(mtu
->dev
, "handled %d, ep0_state: %s\n",
632 handled
, decode_ep0_state(mtu
));
636 else if (handled
> 0)
639 handled
= forward_to_driver(mtu
, &setup
);
642 dev_dbg(mtu
->dev
, "%s stall (%d)\n", __func__
, handled
);
644 ep0_stall_set(mtu
->ep0
, true,
645 le16_to_cpu(setup
.wLength
) ? 0 : EP0_SETUPPKTRDY
);
651 if (mtu
->test_mode
) {
652 ; /* nothing to do */
653 } else if (handled
== USB_GADGET_DELAYED_STATUS
) {
654 /* handle the delay STATUS phase till receive ep_queue on ep0 */
655 mtu
->delayed_status
= true;
656 } else if (le16_to_cpu(setup
.wLength
) == 0) { /* no data stage */
658 mtu3_writel(mbase
, U3D_EP0CSR
,
659 (mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
)
660 | EP0_SETUPPKTRDY
| EP0_DATAEND
);
662 /* complete zlp request directly */
663 mreq
= next_ep0_request(mtu
);
664 if (mreq
&& !mreq
->request
.length
)
665 ep0_req_giveback(mtu
, &mreq
->request
);
671 irqreturn_t
mtu3_ep0_isr(struct mtu3
*mtu
)
673 void __iomem
*mbase
= mtu
->mac_base
;
674 struct mtu3_request
*mreq
;
676 irqreturn_t ret
= IRQ_NONE
;
680 int_status
= mtu3_readl(mbase
, U3D_EPISR
);
681 int_status
&= mtu3_readl(mbase
, U3D_EPIER
);
682 mtu3_writel(mbase
, U3D_EPISR
, int_status
); /* W1C */
684 /* only handle ep0's */
685 if (!(int_status
& EP0ISR
))
688 csr
= mtu3_readl(mbase
, U3D_EP0CSR
);
690 dev_dbg(mtu
->dev
, "%s csr=0x%x\n", __func__
, csr
);
692 /* we sent a stall.. need to clear it now.. */
693 if (csr
& EP0_SENTSTALL
) {
694 ep0_stall_set(mtu
->ep0
, false, 0);
695 csr
= mtu3_readl(mbase
, U3D_EP0CSR
);
698 dev_dbg(mtu
->dev
, "ep0_state: %s\n", decode_ep0_state(mtu
));
700 switch (mtu
->ep0_state
) {
701 case MU3D_EP0_STATE_TX
:
702 /* irq on clearing txpktrdy */
703 if ((csr
& EP0_FIFOFULL
) == 0) {
708 case MU3D_EP0_STATE_RX
:
709 /* irq on set rxpktrdy */
710 if (csr
& EP0_RXPKTRDY
) {
715 case MU3D_EP0_STATE_TX_END
:
716 mtu3_writel(mbase
, U3D_EP0CSR
,
717 (csr
& EP0_W1C_BITS
) | EP0_DATAEND
);
719 mreq
= next_ep0_request(mtu
);
721 ep0_req_giveback(mtu
, &mreq
->request
);
723 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
725 dev_dbg(mtu
->dev
, "ep0_state: %s\n", decode_ep0_state(mtu
));
727 case MU3D_EP0_STATE_SETUP
:
728 if (!(csr
& EP0_SETUPPKTRDY
))
731 len
= mtu3_readl(mbase
, U3D_RXCOUNT0
);
733 dev_err(mtu
->dev
, "SETUP packet len %d != 8 ?\n", len
);
737 ep0_handle_setup(mtu
);
742 ep0_stall_set(mtu
->ep0
, true, 0);
751 static int mtu3_ep0_enable(struct usb_ep
*ep
,
752 const struct usb_endpoint_descriptor
*desc
)
758 static int mtu3_ep0_disable(struct usb_ep
*ep
)
764 static int ep0_queue(struct mtu3_ep
*mep
, struct mtu3_request
*mreq
)
766 struct mtu3
*mtu
= mep
->mtu
;
769 mreq
->request
.actual
= 0;
770 mreq
->request
.status
= -EINPROGRESS
;
772 dev_dbg(mtu
->dev
, "%s %s (ep0_state: %s), len#%d\n", __func__
,
773 mep
->name
, decode_ep0_state(mtu
), mreq
->request
.length
);
775 switch (mtu
->ep0_state
) {
776 case MU3D_EP0_STATE_SETUP
:
777 case MU3D_EP0_STATE_RX
: /* control-OUT data */
778 case MU3D_EP0_STATE_TX
: /* control-IN data */
781 dev_err(mtu
->dev
, "%s, error in ep0 state %s\n", __func__
,
782 decode_ep0_state(mtu
));
786 if (mtu
->delayed_status
) {
789 mtu
->delayed_status
= false;
790 csr
= mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
) & EP0_W1C_BITS
;
791 csr
|= EP0_SETUPPKTRDY
| EP0_DATAEND
;
792 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
, csr
);
793 /* needn't giveback the request for handling delay STATUS */
797 if (!list_empty(&mep
->req_list
))
800 list_add_tail(&mreq
->list
, &mep
->req_list
);
802 /* sequence #1, IN ... start writing the data */
803 if (mtu
->ep0_state
== MU3D_EP0_STATE_TX
)
809 static int mtu3_ep0_queue(struct usb_ep
*ep
,
810 struct usb_request
*req
, gfp_t gfp
)
813 struct mtu3_request
*mreq
;
821 mep
= to_mtu3_ep(ep
);
823 mreq
= to_mtu3_request(req
);
825 spin_lock_irqsave(&mtu
->lock
, flags
);
826 ret
= ep0_queue(mep
, mreq
);
827 spin_unlock_irqrestore(&mtu
->lock
, flags
);
831 static int mtu3_ep0_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
833 /* we just won't support this */
837 static int mtu3_ep0_halt(struct usb_ep
*ep
, int value
)
847 mep
= to_mtu3_ep(ep
);
850 dev_dbg(mtu
->dev
, "%s\n", __func__
);
852 spin_lock_irqsave(&mtu
->lock
, flags
);
854 if (!list_empty(&mep
->req_list
)) {
859 switch (mtu
->ep0_state
) {
861 * stalls are usually issued after parsing SETUP packet, either
862 * directly in irq context from setup() or else later.
864 case MU3D_EP0_STATE_TX
:
865 case MU3D_EP0_STATE_TX_END
:
866 case MU3D_EP0_STATE_RX
:
867 case MU3D_EP0_STATE_SETUP
:
868 ep0_stall_set(mtu
->ep0
, true, 0);
871 dev_dbg(mtu
->dev
, "ep0 can't halt in state %s\n",
872 decode_ep0_state(mtu
));
877 spin_unlock_irqrestore(&mtu
->lock
, flags
);
881 const struct usb_ep_ops mtu3_ep0_ops
= {
882 .enable
= mtu3_ep0_enable
,
883 .disable
= mtu3_ep0_disable
,
884 .alloc_request
= mtu3_alloc_request
,
885 .free_request
= mtu3_free_request
,
886 .queue
= mtu3_ep0_queue
,
887 .dequeue
= mtu3_ep0_dequeue
,
888 .set_halt
= mtu3_ep0_halt
,