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>
15 /* ep0 is always mtu3->in_eps[0] */
16 #define next_ep0_request(mtu) next_request((mtu)->ep0)
18 /* for high speed test mode; see USB 2.0 spec 7.1.20 */
19 static const u8 mtu3_test_packet
[53] = {
20 /* implicit SYNC then DATA0 to start */
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
27 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
28 /* JJJJJJJKKKKKKK x8 */
29 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
31 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
32 /* JKKKKKKK x10, JK */
33 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e,
34 /* implicit CRC16 then EOP to end */
37 static char *decode_ep0_state(struct mtu3
*mtu
)
39 switch (mtu
->ep0_state
) {
40 case MU3D_EP0_STATE_SETUP
:
42 case MU3D_EP0_STATE_TX
:
44 case MU3D_EP0_STATE_RX
:
46 case MU3D_EP0_STATE_TX_END
:
48 case MU3D_EP0_STATE_STALL
:
55 static void ep0_req_giveback(struct mtu3
*mtu
, struct usb_request
*req
)
57 mtu3_req_complete(mtu
->ep0
, req
, 0);
61 forward_to_driver(struct mtu3
*mtu
, const struct usb_ctrlrequest
*setup
)
67 if (!mtu
->gadget_driver
)
70 spin_unlock(&mtu
->lock
);
71 ret
= mtu
->gadget_driver
->setup(&mtu
->g
, setup
);
72 spin_lock(&mtu
->lock
);
74 dev_dbg(mtu
->dev
, "%s ret %d\n", __func__
, ret
);
78 static void ep0_write_fifo(struct mtu3_ep
*mep
, const u8
*src
, u16 len
)
80 void __iomem
*fifo
= mep
->mtu
->mac_base
+ U3D_FIFO0
;
83 dev_dbg(mep
->mtu
->dev
, "%s: ep%din, len=%d, buf=%p\n",
84 __func__
, mep
->epnum
, len
, src
);
87 iowrite32_rep(fifo
, src
, len
>> 2);
91 writew(*(u16
*)&src
[index
], fifo
);
95 writeb(src
[index
], fifo
);
98 static void ep0_read_fifo(struct mtu3_ep
*mep
, u8
*dst
, u16 len
)
100 void __iomem
*fifo
= mep
->mtu
->mac_base
+ U3D_FIFO0
;
104 dev_dbg(mep
->mtu
->dev
, "%s: ep%dout len=%d buf=%p\n",
105 __func__
, mep
->epnum
, len
, dst
);
108 ioread32_rep(fifo
, dst
, len
>> 2);
113 memcpy(&dst
[index
], &value
, len
& 0x3);
118 static void ep0_load_test_packet(struct mtu3
*mtu
)
121 * because the length of test packet is less than max packet of HS ep0,
122 * write it into fifo directly.
124 ep0_write_fifo(mtu
->ep0
, mtu3_test_packet
, sizeof(mtu3_test_packet
));
128 * A. send STALL for setup transfer without data stage:
129 * set SENDSTALL and SETUPPKTRDY at the same time;
130 * B. send STALL for other cases:
131 * set SENDSTALL only.
133 static void ep0_stall_set(struct mtu3_ep
*mep0
, bool set
, u32 pktrdy
)
135 struct mtu3
*mtu
= mep0
->mtu
;
136 void __iomem
*mbase
= mtu
->mac_base
;
139 /* EP0_SENTSTALL is W1C */
140 csr
= mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
;
142 csr
|= EP0_SENDSTALL
| pktrdy
;
144 csr
= (csr
& ~EP0_SENDSTALL
) | EP0_SENTSTALL
;
145 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
, csr
);
147 mtu
->delayed_status
= false;
148 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
150 dev_dbg(mtu
->dev
, "ep0: %s STALL, ep0_state: %s\n",
151 set
? "SEND" : "CLEAR", decode_ep0_state(mtu
));
154 static int ep0_queue(struct mtu3_ep
*mep0
, struct mtu3_request
*mreq
);
156 static void ep0_dummy_complete(struct usb_ep
*ep
, struct usb_request
*req
)
159 static void ep0_set_sel_complete(struct usb_ep
*ep
, struct usb_request
*req
)
161 struct mtu3_request
*mreq
;
163 struct usb_set_sel_req sel
;
165 memcpy(&sel
, req
->buf
, sizeof(sel
));
167 mreq
= to_mtu3_request(req
);
169 dev_dbg(mtu
->dev
, "u1sel:%d, u1pel:%d, u2sel:%d, u2pel:%d\n",
170 sel
.u1_sel
, sel
.u1_pel
, sel
.u2_sel
, sel
.u2_pel
);
173 /* queue data stage to handle 6 byte SET_SEL request */
174 static int ep0_set_sel(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
177 u16 length
= le16_to_cpu(setup
->wLength
);
179 if (unlikely(length
!= 6)) {
180 dev_err(mtu
->dev
, "%s wrong wLength:%d\n",
185 mtu
->ep0_req
.mep
= mtu
->ep0
;
186 mtu
->ep0_req
.request
.length
= 6;
187 mtu
->ep0_req
.request
.buf
= mtu
->setup_buf
;
188 mtu
->ep0_req
.request
.complete
= ep0_set_sel_complete
;
189 ret
= ep0_queue(mtu
->ep0
, &mtu
->ep0_req
);
191 return ret
< 0 ? ret
: 1;
195 ep0_get_status(struct mtu3
*mtu
, const struct usb_ctrlrequest
*setup
)
197 struct mtu3_ep
*mep
= NULL
;
199 u8 result
[2] = {0, 0};
203 switch (setup
->bRequestType
& USB_RECIP_MASK
) {
204 case USB_RECIP_DEVICE
:
205 result
[0] = mtu
->is_self_powered
<< USB_DEVICE_SELF_POWERED
;
206 result
[0] |= mtu
->may_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
;
208 if (mtu
->g
.speed
>= USB_SPEED_SUPER
) {
209 result
[0] |= mtu
->u1_enable
<< USB_DEV_STAT_U1_ENABLED
;
210 result
[0] |= mtu
->u2_enable
<< USB_DEV_STAT_U2_ENABLED
;
213 dev_dbg(mtu
->dev
, "%s result=%x, U1=%x, U2=%x\n", __func__
,
214 result
[0], mtu
->u1_enable
, mtu
->u2_enable
);
217 case USB_RECIP_INTERFACE
:
219 case USB_RECIP_ENDPOINT
:
220 epnum
= (u8
) le16_to_cpu(setup
->wIndex
);
221 is_in
= epnum
& USB_DIR_IN
;
222 epnum
&= USB_ENDPOINT_NUMBER_MASK
;
224 if (epnum
>= mtu
->num_eps
) {
231 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
236 if (mep
->flags
& MTU3_EP_STALL
)
237 result
[0] |= 1 << USB_ENDPOINT_HALT
;
241 /* class, vendor, etc ... delegate */
249 /* prepare a data stage for GET_STATUS */
250 dev_dbg(mtu
->dev
, "get_status=%x\n", *(u16
*)result
);
251 memcpy(mtu
->setup_buf
, result
, sizeof(result
));
252 mtu
->ep0_req
.mep
= mtu
->ep0
;
253 mtu
->ep0_req
.request
.length
= 2;
254 mtu
->ep0_req
.request
.buf
= &mtu
->setup_buf
;
255 mtu
->ep0_req
.request
.complete
= ep0_dummy_complete
;
256 ret
= ep0_queue(mtu
->ep0
, &mtu
->ep0_req
);
263 static int handle_test_mode(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
265 void __iomem
*mbase
= mtu
->mac_base
;
269 switch (le16_to_cpu(setup
->wIndex
) >> 8) {
271 dev_dbg(mtu
->dev
, "TEST_J\n");
272 mtu
->test_mode_nr
= TEST_J_MODE
;
275 dev_dbg(mtu
->dev
, "TEST_K\n");
276 mtu
->test_mode_nr
= TEST_K_MODE
;
279 dev_dbg(mtu
->dev
, "TEST_SE0_NAK\n");
280 mtu
->test_mode_nr
= TEST_SE0_NAK_MODE
;
283 dev_dbg(mtu
->dev
, "TEST_PACKET\n");
284 mtu
->test_mode_nr
= TEST_PACKET_MODE
;
291 mtu
->test_mode
= true;
293 /* no TX completion interrupt, and need restart platform after test */
294 if (mtu
->test_mode_nr
== TEST_PACKET_MODE
)
295 ep0_load_test_packet(mtu
);
297 /* send status before entering test mode. */
298 value
= mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
;
299 mtu3_writel(mbase
, U3D_EP0CSR
, value
| EP0_SETUPPKTRDY
| EP0_DATAEND
);
301 /* wait for ACK status sent by host */
302 readl_poll_timeout_atomic(mbase
+ U3D_EP0CSR
, value
,
303 !(value
& EP0_DATAEND
), 100, 5000);
305 mtu3_writel(mbase
, U3D_USB2_TEST_MODE
, mtu
->test_mode_nr
);
307 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
313 static int ep0_handle_feature_dev(struct mtu3
*mtu
,
314 struct usb_ctrlrequest
*setup
, bool set
)
316 void __iomem
*mbase
= mtu
->mac_base
;
317 int handled
= -EINVAL
;
320 switch (le16_to_cpu(setup
->wValue
)) {
321 case USB_DEVICE_REMOTE_WAKEUP
:
322 mtu
->may_wakeup
= !!set
;
325 case USB_DEVICE_TEST_MODE
:
326 if (!set
|| (mtu
->g
.speed
!= USB_SPEED_HIGH
) ||
327 (le16_to_cpu(setup
->wIndex
) & 0xff))
330 handled
= handle_test_mode(mtu
, setup
);
332 case USB_DEVICE_U1_ENABLE
:
333 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
334 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
337 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
339 lpc
|= SW_U1_ACCEPT_ENABLE
;
341 lpc
&= ~SW_U1_ACCEPT_ENABLE
;
342 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
344 mtu
->u1_enable
= !!set
;
347 case USB_DEVICE_U2_ENABLE
:
348 if (mtu
->g
.speed
< USB_SPEED_SUPER
||
349 mtu
->g
.state
!= USB_STATE_CONFIGURED
)
352 lpc
= mtu3_readl(mbase
, U3D_LINK_POWER_CONTROL
);
354 lpc
|= SW_U2_ACCEPT_ENABLE
;
356 lpc
&= ~SW_U2_ACCEPT_ENABLE
;
357 mtu3_writel(mbase
, U3D_LINK_POWER_CONTROL
, lpc
);
359 mtu
->u2_enable
= !!set
;
369 static int ep0_handle_feature(struct mtu3
*mtu
,
370 struct usb_ctrlrequest
*setup
, bool set
)
373 int handled
= -EINVAL
;
379 value
= le16_to_cpu(setup
->wValue
);
380 index
= le16_to_cpu(setup
->wIndex
);
382 switch (setup
->bRequestType
& USB_RECIP_MASK
) {
383 case USB_RECIP_DEVICE
:
384 handled
= ep0_handle_feature_dev(mtu
, setup
, set
);
386 case USB_RECIP_INTERFACE
:
387 /* superspeed only */
388 if (value
== USB_INTRF_FUNC_SUSPEND
&&
389 mtu
->g
.speed
>= USB_SPEED_SUPER
) {
391 * forward the request because function drivers
397 case USB_RECIP_ENDPOINT
:
398 epnum
= index
& USB_ENDPOINT_NUMBER_MASK
;
399 if (epnum
== 0 || epnum
>= mtu
->num_eps
||
400 value
!= USB_ENDPOINT_HALT
)
403 is_in
= index
& USB_DIR_IN
;
404 mep
= (is_in
? mtu
->in_eps
: mtu
->out_eps
) + epnum
;
409 /* ignore request if endpoint is wedged */
413 mtu3_ep_stall_set(mep
, set
);
416 /* class, vendor, etc ... delegate */
424 * handle all control requests can be handled
426 * negative errno - error happened
427 * zero - need delegate SETUP to gadget driver
428 * positive - already handled
430 static int handle_standard_request(struct mtu3
*mtu
,
431 struct usb_ctrlrequest
*setup
)
433 void __iomem
*mbase
= mtu
->mac_base
;
434 enum usb_device_state state
= mtu
->g
.state
;
435 int handled
= -EINVAL
;
439 value
= le16_to_cpu(setup
->wValue
);
441 /* the gadget driver handles everything except what we must handle */
442 switch (setup
->bRequest
) {
443 case USB_REQ_SET_ADDRESS
:
444 /* change it after the status stage */
445 mtu
->address
= (u8
) (value
& 0x7f);
446 dev_dbg(mtu
->dev
, "set address to 0x%x\n", mtu
->address
);
448 dev_conf
= mtu3_readl(mbase
, U3D_DEVICE_CONF
);
449 dev_conf
&= ~DEV_ADDR_MSK
;
450 dev_conf
|= DEV_ADDR(mtu
->address
);
451 mtu3_writel(mbase
, U3D_DEVICE_CONF
, dev_conf
);
454 usb_gadget_set_state(&mtu
->g
, USB_STATE_ADDRESS
);
456 usb_gadget_set_state(&mtu
->g
, USB_STATE_DEFAULT
);
460 case USB_REQ_SET_CONFIGURATION
:
461 if (state
== USB_STATE_ADDRESS
) {
462 usb_gadget_set_state(&mtu
->g
,
463 USB_STATE_CONFIGURED
);
464 } else if (state
== USB_STATE_CONFIGURED
) {
466 * USB2 spec sec 9.4.7, if wValue is 0 then dev
467 * is moved to addressed state
470 usb_gadget_set_state(&mtu
->g
,
475 case USB_REQ_CLEAR_FEATURE
:
476 handled
= ep0_handle_feature(mtu
, setup
, 0);
478 case USB_REQ_SET_FEATURE
:
479 handled
= ep0_handle_feature(mtu
, setup
, 1);
481 case USB_REQ_GET_STATUS
:
482 handled
= ep0_get_status(mtu
, setup
);
484 case USB_REQ_SET_SEL
:
485 handled
= ep0_set_sel(mtu
, setup
);
487 case USB_REQ_SET_ISOCH_DELAY
:
491 /* delegate SET_CONFIGURATION, etc */
498 /* receive an data packet (OUT) */
499 static void ep0_rx_state(struct mtu3
*mtu
)
501 struct mtu3_request
*mreq
;
502 struct usb_request
*req
;
503 void __iomem
*mbase
= mtu
->mac_base
;
508 dev_dbg(mtu
->dev
, "%s\n", __func__
);
510 csr
= mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
;
511 mreq
= next_ep0_request(mtu
);
512 req
= &mreq
->request
;
514 /* read packet and ack; or stall because of gadget driver bug */
516 void *buf
= req
->buf
+ req
->actual
;
517 unsigned int len
= req
->length
- req
->actual
;
519 /* read the buffer */
520 count
= mtu3_readl(mbase
, U3D_RXCOUNT0
);
522 req
->status
= -EOVERFLOW
;
525 ep0_read_fifo(mtu
->ep0
, buf
, count
);
526 req
->actual
+= count
;
529 maxp
= mtu
->g
.ep0
->maxpacket
;
530 if (count
< maxp
|| req
->actual
== req
->length
) {
531 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
532 dev_dbg(mtu
->dev
, "ep0 state: %s\n",
533 decode_ep0_state(mtu
));
540 csr
|= EP0_RXPKTRDY
| EP0_SENDSTALL
;
541 dev_dbg(mtu
->dev
, "%s: SENDSTALL\n", __func__
);
544 mtu3_writel(mbase
, U3D_EP0CSR
, csr
);
546 /* give back the request if have received all data */
548 ep0_req_giveback(mtu
, req
);
552 /* transmitting to the host (IN) */
553 static void ep0_tx_state(struct mtu3
*mtu
)
555 struct mtu3_request
*mreq
= next_ep0_request(mtu
);
556 struct usb_request
*req
;
562 dev_dbg(mtu
->dev
, "%s\n", __func__
);
567 maxp
= mtu
->g
.ep0
->maxpacket
;
568 req
= &mreq
->request
;
571 src
= (u8
*)req
->buf
+ req
->actual
;
572 count
= min(maxp
, req
->length
- req
->actual
);
574 ep0_write_fifo(mtu
->ep0
, src
, count
);
576 dev_dbg(mtu
->dev
, "%s act=%d, len=%d, cnt=%d, maxp=%d zero=%d\n",
577 __func__
, req
->actual
, req
->length
, count
, maxp
, req
->zero
);
579 req
->actual
+= count
;
582 || ((req
->actual
== req
->length
) && !req
->zero
))
583 mtu
->ep0_state
= MU3D_EP0_STATE_TX_END
;
585 /* send it out, triggering a "txpktrdy cleared" irq */
586 csr
= mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
) & EP0_W1C_BITS
;
587 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
, csr
| EP0_TXPKTRDY
);
589 dev_dbg(mtu
->dev
, "%s ep0csr=0x%x\n", __func__
,
590 mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
));
593 static void ep0_read_setup(struct mtu3
*mtu
, struct usb_ctrlrequest
*setup
)
595 struct mtu3_request
*mreq
;
599 csr
= mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
) & EP0_W1C_BITS
;
600 count
= mtu3_readl(mtu
->mac_base
, U3D_RXCOUNT0
);
602 ep0_read_fifo(mtu
->ep0
, (u8
*)setup
, count
);
604 dev_dbg(mtu
->dev
, "SETUP req%02x.%02x v%04x i%04x l%04x\n",
605 setup
->bRequestType
, setup
->bRequest
,
606 le16_to_cpu(setup
->wValue
), le16_to_cpu(setup
->wIndex
),
607 le16_to_cpu(setup
->wLength
));
609 /* clean up any leftover transfers */
610 mreq
= next_ep0_request(mtu
);
612 ep0_req_giveback(mtu
, &mreq
->request
);
614 if (le16_to_cpu(setup
->wLength
) == 0) {
615 ; /* no data stage, nothing to do */
616 } else if (setup
->bRequestType
& USB_DIR_IN
) {
617 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
,
618 csr
| EP0_SETUPPKTRDY
| EP0_DPHTX
);
619 mtu
->ep0_state
= MU3D_EP0_STATE_TX
;
621 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
,
622 (csr
| EP0_SETUPPKTRDY
) & (~EP0_DPHTX
));
623 mtu
->ep0_state
= MU3D_EP0_STATE_RX
;
627 static int ep0_handle_setup(struct mtu3
*mtu
)
628 __releases(mtu
->lock
)
629 __acquires(mtu
->lock
)
631 struct usb_ctrlrequest setup
;
632 struct mtu3_request
*mreq
;
633 void __iomem
*mbase
= mtu
->mac_base
;
636 ep0_read_setup(mtu
, &setup
);
638 if ((setup
.bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
639 handled
= handle_standard_request(mtu
, &setup
);
641 dev_dbg(mtu
->dev
, "handled %d, ep0_state: %s\n",
642 handled
, decode_ep0_state(mtu
));
646 else if (handled
> 0)
649 handled
= forward_to_driver(mtu
, &setup
);
652 dev_dbg(mtu
->dev
, "%s stall (%d)\n", __func__
, handled
);
654 ep0_stall_set(mtu
->ep0
, true,
655 le16_to_cpu(setup
.wLength
) ? 0 : EP0_SETUPPKTRDY
);
661 if (mtu
->test_mode
) {
662 ; /* nothing to do */
663 } else if (handled
== USB_GADGET_DELAYED_STATUS
) {
664 /* handle the delay STATUS phase till receive ep_queue on ep0 */
665 mtu
->delayed_status
= true;
666 } else if (le16_to_cpu(setup
.wLength
) == 0) { /* no data stage */
668 mtu3_writel(mbase
, U3D_EP0CSR
,
669 (mtu3_readl(mbase
, U3D_EP0CSR
) & EP0_W1C_BITS
)
670 | EP0_SETUPPKTRDY
| EP0_DATAEND
);
672 /* complete zlp request directly */
673 mreq
= next_ep0_request(mtu
);
674 if (mreq
&& !mreq
->request
.length
)
675 ep0_req_giveback(mtu
, &mreq
->request
);
681 irqreturn_t
mtu3_ep0_isr(struct mtu3
*mtu
)
683 void __iomem
*mbase
= mtu
->mac_base
;
684 struct mtu3_request
*mreq
;
686 irqreturn_t ret
= IRQ_NONE
;
690 int_status
= mtu3_readl(mbase
, U3D_EPISR
);
691 int_status
&= mtu3_readl(mbase
, U3D_EPIER
);
692 mtu3_writel(mbase
, U3D_EPISR
, int_status
); /* W1C */
694 /* only handle ep0's */
695 if (!(int_status
& EP0ISR
))
698 csr
= mtu3_readl(mbase
, U3D_EP0CSR
);
700 dev_dbg(mtu
->dev
, "%s csr=0x%x\n", __func__
, csr
);
702 /* we sent a stall.. need to clear it now.. */
703 if (csr
& EP0_SENTSTALL
) {
704 ep0_stall_set(mtu
->ep0
, false, 0);
705 csr
= mtu3_readl(mbase
, U3D_EP0CSR
);
708 dev_dbg(mtu
->dev
, "ep0_state: %s\n", decode_ep0_state(mtu
));
710 switch (mtu
->ep0_state
) {
711 case MU3D_EP0_STATE_TX
:
712 /* irq on clearing txpktrdy */
713 if ((csr
& EP0_FIFOFULL
) == 0) {
718 case MU3D_EP0_STATE_RX
:
719 /* irq on set rxpktrdy */
720 if (csr
& EP0_RXPKTRDY
) {
725 case MU3D_EP0_STATE_TX_END
:
726 mtu3_writel(mbase
, U3D_EP0CSR
,
727 (csr
& EP0_W1C_BITS
) | EP0_DATAEND
);
729 mreq
= next_ep0_request(mtu
);
731 ep0_req_giveback(mtu
, &mreq
->request
);
733 mtu
->ep0_state
= MU3D_EP0_STATE_SETUP
;
735 dev_dbg(mtu
->dev
, "ep0_state: %s\n", decode_ep0_state(mtu
));
737 case MU3D_EP0_STATE_SETUP
:
738 if (!(csr
& EP0_SETUPPKTRDY
))
741 len
= mtu3_readl(mbase
, U3D_RXCOUNT0
);
743 dev_err(mtu
->dev
, "SETUP packet len %d != 8 ?\n", len
);
747 ep0_handle_setup(mtu
);
752 ep0_stall_set(mtu
->ep0
, true, 0);
761 static int mtu3_ep0_enable(struct usb_ep
*ep
,
762 const struct usb_endpoint_descriptor
*desc
)
768 static int mtu3_ep0_disable(struct usb_ep
*ep
)
774 static int ep0_queue(struct mtu3_ep
*mep
, struct mtu3_request
*mreq
)
776 struct mtu3
*mtu
= mep
->mtu
;
779 mreq
->request
.actual
= 0;
780 mreq
->request
.status
= -EINPROGRESS
;
782 dev_dbg(mtu
->dev
, "%s %s (ep0_state: %s), len#%d\n", __func__
,
783 mep
->name
, decode_ep0_state(mtu
), mreq
->request
.length
);
785 switch (mtu
->ep0_state
) {
786 case MU3D_EP0_STATE_SETUP
:
787 case MU3D_EP0_STATE_RX
: /* control-OUT data */
788 case MU3D_EP0_STATE_TX
: /* control-IN data */
791 dev_err(mtu
->dev
, "%s, error in ep0 state %s\n", __func__
,
792 decode_ep0_state(mtu
));
796 if (mtu
->delayed_status
) {
799 mtu
->delayed_status
= false;
800 csr
= mtu3_readl(mtu
->mac_base
, U3D_EP0CSR
) & EP0_W1C_BITS
;
801 csr
|= EP0_SETUPPKTRDY
| EP0_DATAEND
;
802 mtu3_writel(mtu
->mac_base
, U3D_EP0CSR
, csr
);
803 /* needn't giveback the request for handling delay STATUS */
807 if (!list_empty(&mep
->req_list
))
810 list_add_tail(&mreq
->list
, &mep
->req_list
);
812 /* sequence #1, IN ... start writing the data */
813 if (mtu
->ep0_state
== MU3D_EP0_STATE_TX
)
819 static int mtu3_ep0_queue(struct usb_ep
*ep
,
820 struct usb_request
*req
, gfp_t gfp
)
823 struct mtu3_request
*mreq
;
831 mep
= to_mtu3_ep(ep
);
833 mreq
= to_mtu3_request(req
);
835 spin_lock_irqsave(&mtu
->lock
, flags
);
836 ret
= ep0_queue(mep
, mreq
);
837 spin_unlock_irqrestore(&mtu
->lock
, flags
);
841 static int mtu3_ep0_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
843 /* we just won't support this */
847 static int mtu3_ep0_halt(struct usb_ep
*ep
, int value
)
857 mep
= to_mtu3_ep(ep
);
860 dev_dbg(mtu
->dev
, "%s\n", __func__
);
862 spin_lock_irqsave(&mtu
->lock
, flags
);
864 if (!list_empty(&mep
->req_list
)) {
869 switch (mtu
->ep0_state
) {
871 * stalls are usually issued after parsing SETUP packet, either
872 * directly in irq context from setup() or else later.
874 case MU3D_EP0_STATE_TX
:
875 case MU3D_EP0_STATE_TX_END
:
876 case MU3D_EP0_STATE_RX
:
877 case MU3D_EP0_STATE_SETUP
:
878 ep0_stall_set(mtu
->ep0
, true, 0);
881 dev_dbg(mtu
->dev
, "ep0 can't halt in state %s\n",
882 decode_ep0_state(mtu
));
887 spin_unlock_irqrestore(&mtu
->lock
, flags
);
891 const struct usb_ep_ops mtu3_ep0_ops
= {
892 .enable
= mtu3_ep0_enable
,
893 .disable
= mtu3_ep0_disable
,
894 .alloc_request
= mtu3_alloc_request
,
895 .free_request
= mtu3_free_request
,
896 .queue
= mtu3_ep0_queue
,
897 .dequeue
= mtu3_ep0_dequeue
,
898 .set_halt
= mtu3_ep0_halt
,