2 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the above-listed copyright holders may not be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2, as published by the Free
25 * Software Foundation.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/platform_device.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/interrupt.h>
47 #include <linux/list.h>
48 #include <linux/dma-mapping.h>
50 #include <linux/usb/ch9.h>
51 #include <linux/usb/gadget.h>
57 static void dwc3_ep0_inspect_setup(struct dwc3
*dwc
,
58 const struct dwc3_event_depevt
*event
);
60 static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state
)
67 case EP0_IN_DATA_PHASE
:
68 return "IN Data Phase";
69 case EP0_OUT_DATA_PHASE
:
70 return "OUT Data Phase";
71 case EP0_IN_WAIT_GADGET
:
72 return "IN Wait Gadget";
73 case EP0_OUT_WAIT_GADGET
:
74 return "OUT Wait Gadget";
75 case EP0_IN_WAIT_NRDY
:
76 return "IN Wait NRDY";
77 case EP0_OUT_WAIT_NRDY
:
78 return "OUT Wait NRDY";
79 case EP0_IN_STATUS_PHASE
:
80 return "IN Status Phase";
81 case EP0_OUT_STATUS_PHASE
:
82 return "OUT Status Phase";
90 static int dwc3_ep0_start_trans(struct dwc3
*dwc
, u8 epnum
, dma_addr_t buf_dma
,
93 struct dwc3_gadget_ep_cmd_params params
;
94 struct dwc3_trb_hw
*trb_hw
;
100 dep
= dwc
->eps
[epnum
];
102 trb_hw
= dwc
->ep0_trb
;
103 memset(&trb
, 0, sizeof(trb
));
105 switch (dwc
->ep0state
) {
107 trb
.trbctl
= DWC3_TRBCTL_CONTROL_SETUP
;
110 case EP0_IN_WAIT_NRDY
:
111 case EP0_OUT_WAIT_NRDY
:
112 case EP0_IN_STATUS_PHASE
:
113 case EP0_OUT_STATUS_PHASE
:
114 if (dwc
->three_stage_setup
)
115 trb
.trbctl
= DWC3_TRBCTL_CONTROL_STATUS3
;
117 trb
.trbctl
= DWC3_TRBCTL_CONTROL_STATUS2
;
119 if (dwc
->ep0state
== EP0_IN_WAIT_NRDY
)
120 dwc
->ep0state
= EP0_IN_STATUS_PHASE
;
121 else if (dwc
->ep0state
== EP0_OUT_WAIT_NRDY
)
122 dwc
->ep0state
= EP0_OUT_STATUS_PHASE
;
125 case EP0_IN_WAIT_GADGET
:
126 dwc
->ep0state
= EP0_IN_WAIT_NRDY
;
130 case EP0_OUT_WAIT_GADGET
:
131 dwc
->ep0state
= EP0_OUT_WAIT_NRDY
;
136 case EP0_IN_DATA_PHASE
:
137 case EP0_OUT_DATA_PHASE
:
138 trb
.trbctl
= DWC3_TRBCTL_CONTROL_DATA
;
142 dev_err(dwc
->dev
, "%s() can't in state %d\n", __func__
,
155 dwc3_trb_to_hw(&trb
, trb_hw
);
157 memset(¶ms
, 0, sizeof(params
));
158 params
.param0
.depstrtxfer
.transfer_desc_addr_high
=
159 upper_32_bits(dwc
->ep0_trb_addr
);
160 params
.param1
.depstrtxfer
.transfer_desc_addr_low
=
161 lower_32_bits(dwc
->ep0_trb_addr
);
163 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
164 DWC3_DEPCMD_STARTTRANSFER
, ¶ms
);
166 dev_dbg(dwc
->dev
, "failed to send STARTTRANSFER command\n");
170 dep
->res_trans_idx
= dwc3_gadget_ep_get_transfer_index(dwc
,
176 static int __dwc3_gadget_ep0_queue(struct dwc3_ep
*dep
,
177 struct dwc3_request
*req
)
179 struct dwc3
*dwc
= dep
->dwc
;
182 req
->request
.actual
= 0;
183 req
->request
.status
= -EINPROGRESS
;
184 req
->direction
= dep
->direction
;
185 req
->epnum
= dep
->number
;
187 list_add_tail(&req
->list
, &dep
->request_list
);
188 dwc3_map_buffer_to_dma(req
);
190 ret
= dwc3_ep0_start_trans(dwc
, dep
->number
, req
->request
.dma
,
191 req
->request
.length
);
193 list_del(&req
->list
);
194 dwc3_unmap_buffer_from_dma(req
);
200 int dwc3_gadget_ep0_queue(struct usb_ep
*ep
, struct usb_request
*request
,
203 struct dwc3_request
*req
= to_dwc3_request(request
);
204 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
205 struct dwc3
*dwc
= dep
->dwc
;
211 switch (dwc
->ep0state
) {
212 case EP0_IN_DATA_PHASE
:
213 case EP0_IN_WAIT_GADGET
:
214 case EP0_IN_WAIT_NRDY
:
215 case EP0_IN_STATUS_PHASE
:
219 case EP0_OUT_DATA_PHASE
:
220 case EP0_OUT_WAIT_GADGET
:
221 case EP0_OUT_WAIT_NRDY
:
222 case EP0_OUT_STATUS_PHASE
:
229 spin_lock_irqsave(&dwc
->lock
, flags
);
231 dev_dbg(dwc
->dev
, "trying to queue request %p to disabled %s\n",
237 /* we share one TRB for ep0/1 */
238 if (!list_empty(&dwc
->eps
[0]->request_list
) ||
239 !list_empty(&dwc
->eps
[1]->request_list
) ||
240 dwc
->ep0_status_pending
) {
245 dev_vdbg(dwc
->dev
, "queueing request %p to %s length %d, state '%s'\n",
246 request
, dep
->name
, request
->length
,
247 dwc3_ep0_state_string(dwc
->ep0state
));
249 ret
= __dwc3_gadget_ep0_queue(dep
, req
);
252 spin_unlock_irqrestore(&dwc
->lock
, flags
);
257 static void dwc3_ep0_stall_and_restart(struct dwc3
*dwc
)
259 /* stall is always issued on EP0 */
260 __dwc3_gadget_ep_set_halt(dwc
->eps
[0], 1);
261 dwc
->eps
[0]->flags
&= ~DWC3_EP_STALL
;
262 dwc
->ep0state
= EP0_IDLE
;
263 dwc3_ep0_out_start(dwc
);
266 void dwc3_ep0_out_start(struct dwc3
*dwc
)
273 ret
= dwc3_ep0_start_trans(dwc
, 0, dwc
->ctrl_req_addr
, 8);
278 * Send a zero length packet for the status phase of the control transfer
280 static void dwc3_ep0_do_setup_status(struct dwc3
*dwc
,
281 const struct dwc3_event_depevt
*event
)
287 epnum
= event
->endpoint_number
;
288 dep
= dwc
->eps
[epnum
];
291 dwc
->ep0state
= EP0_IN_STATUS_PHASE
;
293 dwc
->ep0state
= EP0_OUT_STATUS_PHASE
;
296 * Not sure Why I need a buffer for a zero transfer. Maybe the
297 * HW reacts strange on a NULL pointer
299 ret
= dwc3_ep0_start_trans(dwc
, epnum
, dwc
->ctrl_req_addr
, 0);
301 dev_dbg(dwc
->dev
, "failed to start transfer, stalling\n");
302 dwc3_ep0_stall_and_restart(dwc
);
306 static struct dwc3_ep
*dwc3_wIndex_to_dep(struct dwc3
*dwc
, __le16 wIndex_le
)
309 u32 windex
= le16_to_cpu(wIndex_le
);
312 epnum
= (windex
& USB_ENDPOINT_NUMBER_MASK
) << 1;
313 if ((windex
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
316 dep
= dwc
->eps
[epnum
];
317 if (dep
->flags
& DWC3_EP_ENABLED
)
323 static void dwc3_ep0_send_status_response(struct dwc3
*dwc
)
327 if (dwc
->ep0state
== EP0_IN_DATA_PHASE
)
332 dwc3_ep0_start_trans(dwc
, epnum
, dwc
->ctrl_req_addr
,
333 dwc
->ep0_usb_req
.length
);
334 dwc
->ep0_status_pending
= 1;
340 static int dwc3_ep0_handle_status(struct dwc3
*dwc
, struct usb_ctrlrequest
*ctrl
)
345 __le16
*response_pkt
;
347 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
349 case USB_RECIP_DEVICE
:
351 * We are self-powered. U1/U2/LTM will be set later
352 * once we handle this states. RemoteWakeup is 0 on SS
354 usb_status
|= dwc
->is_selfpowered
<< USB_DEVICE_SELF_POWERED
;
357 case USB_RECIP_INTERFACE
:
359 * Function Remote Wake Capable D0
360 * Function Remote Wakeup D1
364 case USB_RECIP_ENDPOINT
:
365 dep
= dwc3_wIndex_to_dep(dwc
, ctrl
->wIndex
);
369 if (dep
->flags
& DWC3_EP_STALL
)
370 usb_status
= 1 << USB_ENDPOINT_HALT
;
376 response_pkt
= (__le16
*) dwc
->setup_buf
;
377 *response_pkt
= cpu_to_le16(usb_status
);
378 dwc
->ep0_usb_req
.length
= sizeof(*response_pkt
);
379 dwc3_ep0_send_status_response(dwc
);
384 static int dwc3_ep0_handle_feature(struct dwc3
*dwc
,
385 struct usb_ctrlrequest
*ctrl
, int set
)
395 wValue
= le16_to_cpu(ctrl
->wValue
);
396 wIndex
= le16_to_cpu(ctrl
->wIndex
);
397 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
399 case USB_RECIP_DEVICE
:
402 * 9.4.1 says only only for SS, in AddressState only for
403 * default control pipe
406 case USB_DEVICE_U1_ENABLE
:
407 case USB_DEVICE_U2_ENABLE
:
408 case USB_DEVICE_LTM_ENABLE
:
409 if (dwc
->dev_state
!= DWC3_CONFIGURED_STATE
)
411 if (dwc
->speed
!= DWC3_DSTS_SUPERSPEED
)
415 /* XXX add U[12] & LTM */
417 case USB_DEVICE_REMOTE_WAKEUP
:
419 case USB_DEVICE_U1_ENABLE
:
421 case USB_DEVICE_U2_ENABLE
:
423 case USB_DEVICE_LTM_ENABLE
:
426 case USB_DEVICE_TEST_MODE
:
427 if ((wIndex
& 0xff) != 0)
433 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
434 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
447 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
454 case USB_RECIP_INTERFACE
:
456 case USB_INTRF_FUNC_SUSPEND
:
457 if (wIndex
& USB_INTRF_FUNC_SUSPEND_LP
)
458 /* XXX enable Low power suspend */
460 if (wIndex
& USB_INTRF_FUNC_SUSPEND_RW
)
461 /* XXX enable remote wakeup */
469 case USB_RECIP_ENDPOINT
:
471 case USB_ENDPOINT_HALT
:
473 dep
= dwc3_wIndex_to_dep(dwc
, ctrl
->wIndex
);
476 ret
= __dwc3_gadget_ep_set_halt(dep
, set
);
489 dwc
->ep0state
= EP0_IN_WAIT_NRDY
;
494 static int dwc3_ep0_set_address(struct dwc3
*dwc
, struct usb_ctrlrequest
*ctrl
)
500 addr
= le16_to_cpu(ctrl
->wValue
);
504 switch (dwc
->dev_state
) {
505 case DWC3_DEFAULT_STATE
:
506 case DWC3_ADDRESS_STATE
:
508 * Not sure if we should program DevAddr now or later
510 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
511 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
512 reg
|= DWC3_DCFG_DEVADDR(addr
);
513 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
516 dwc
->dev_state
= DWC3_ADDRESS_STATE
;
518 dwc
->dev_state
= DWC3_DEFAULT_STATE
;
521 case DWC3_CONFIGURED_STATE
:
525 dwc
->ep0state
= EP0_IN_WAIT_NRDY
;
529 static int dwc3_ep0_delegate_req(struct dwc3
*dwc
, struct usb_ctrlrequest
*ctrl
)
533 spin_unlock(&dwc
->lock
);
534 ret
= dwc
->gadget_driver
->setup(&dwc
->gadget
, ctrl
);
535 spin_lock(&dwc
->lock
);
539 static int dwc3_ep0_set_config(struct dwc3
*dwc
, struct usb_ctrlrequest
*ctrl
)
544 cfg
= le16_to_cpu(ctrl
->wValue
);
546 switch (dwc
->dev_state
) {
547 case DWC3_DEFAULT_STATE
:
551 case DWC3_ADDRESS_STATE
:
552 ret
= dwc3_ep0_delegate_req(dwc
, ctrl
);
553 /* if the cfg matches and the cfg is non zero */
555 dwc
->dev_state
= DWC3_CONFIGURED_STATE
;
558 case DWC3_CONFIGURED_STATE
:
559 ret
= dwc3_ep0_delegate_req(dwc
, ctrl
);
561 dwc
->dev_state
= DWC3_ADDRESS_STATE
;
567 static int dwc3_ep0_std_request(struct dwc3
*dwc
, struct usb_ctrlrequest
*ctrl
)
571 switch (ctrl
->bRequest
) {
572 case USB_REQ_GET_STATUS
:
573 dev_vdbg(dwc
->dev
, "USB_REQ_GET_STATUS\n");
574 ret
= dwc3_ep0_handle_status(dwc
, ctrl
);
576 case USB_REQ_CLEAR_FEATURE
:
577 dev_vdbg(dwc
->dev
, "USB_REQ_CLEAR_FEATURE\n");
578 ret
= dwc3_ep0_handle_feature(dwc
, ctrl
, 0);
580 case USB_REQ_SET_FEATURE
:
581 dev_vdbg(dwc
->dev
, "USB_REQ_SET_FEATURE\n");
582 ret
= dwc3_ep0_handle_feature(dwc
, ctrl
, 1);
584 case USB_REQ_SET_ADDRESS
:
585 dev_vdbg(dwc
->dev
, "USB_REQ_SET_ADDRESS\n");
586 ret
= dwc3_ep0_set_address(dwc
, ctrl
);
588 case USB_REQ_SET_CONFIGURATION
:
589 dev_vdbg(dwc
->dev
, "USB_REQ_SET_CONFIGURATION\n");
590 ret
= dwc3_ep0_set_config(dwc
, ctrl
);
593 dev_vdbg(dwc
->dev
, "Forwarding to gadget driver\n");
594 ret
= dwc3_ep0_delegate_req(dwc
, ctrl
);
601 static void dwc3_ep0_inspect_setup(struct dwc3
*dwc
,
602 const struct dwc3_event_depevt
*event
)
604 struct usb_ctrlrequest
*ctrl
= dwc
->ctrl_req
;
608 if (!dwc
->gadget_driver
)
611 len
= le16_to_cpu(ctrl
->wLength
);
613 dwc
->ep0state
= EP0_IN_WAIT_GADGET
;
614 dwc
->three_stage_setup
= 0;
616 dwc
->three_stage_setup
= 1;
617 if (ctrl
->bRequestType
& USB_DIR_IN
)
618 dwc
->ep0state
= EP0_IN_DATA_PHASE
;
620 dwc
->ep0state
= EP0_OUT_DATA_PHASE
;
623 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
624 ret
= dwc3_ep0_std_request(dwc
, ctrl
);
626 ret
= dwc3_ep0_delegate_req(dwc
, ctrl
);
632 dwc3_ep0_stall_and_restart(dwc
);
635 static void dwc3_ep0_complete_data(struct dwc3
*dwc
,
636 const struct dwc3_event_depevt
*event
)
638 struct dwc3_request
*r
= NULL
;
639 struct usb_request
*ur
;
645 epnum
= event
->endpoint_number
;
646 dep
= dwc
->eps
[epnum
];
648 if (!dwc
->ep0_status_pending
) {
649 r
= next_request(&dep
->request_list
);
652 ur
= &dwc
->ep0_usb_req
;
653 dwc
->ep0_status_pending
= 0;
656 dwc3_trb_to_nat(dwc
->ep0_trb
, &trb
);
658 transfered
= ur
->length
- trb
.length
;
659 ur
->actual
+= transfered
;
661 if ((epnum
& 1) && ur
->actual
< ur
->length
) {
662 /* for some reason we did not get everything out */
664 dwc3_ep0_stall_and_restart(dwc
);
665 dwc3_gadget_giveback(dep
, r
, -ECONNRESET
);
668 * handle the case where we have to send a zero packet. This
669 * seems to be case when req.length > maxpacket. Could it be?
671 /* The transfer is complete, wait for HOST */
673 dwc
->ep0state
= EP0_IN_WAIT_NRDY
;
675 dwc
->ep0state
= EP0_OUT_WAIT_NRDY
;
678 dwc3_gadget_giveback(dep
, r
, 0);
682 static void dwc3_ep0_complete_req(struct dwc3
*dwc
,
683 const struct dwc3_event_depevt
*event
)
685 struct dwc3_request
*r
;
689 epnum
= event
->endpoint_number
;
690 dep
= dwc
->eps
[epnum
];
692 if (!list_empty(&dep
->request_list
)) {
693 r
= next_request(&dep
->request_list
);
695 dwc3_gadget_giveback(dep
, r
, 0);
698 dwc
->ep0state
= EP0_IDLE
;
699 dwc3_ep0_out_start(dwc
);
702 static void dwc3_ep0_xfer_complete(struct dwc3
*dwc
,
703 const struct dwc3_event_depevt
*event
)
705 switch (dwc
->ep0state
) {
707 dwc3_ep0_inspect_setup(dwc
, event
);
710 case EP0_IN_DATA_PHASE
:
711 case EP0_OUT_DATA_PHASE
:
712 dwc3_ep0_complete_data(dwc
, event
);
715 case EP0_IN_STATUS_PHASE
:
716 case EP0_OUT_STATUS_PHASE
:
717 dwc3_ep0_complete_req(dwc
, event
);
720 case EP0_IN_WAIT_NRDY
:
721 case EP0_OUT_WAIT_NRDY
:
722 case EP0_IN_WAIT_GADGET
:
723 case EP0_OUT_WAIT_GADGET
:
724 case EP0_UNCONNECTED
:
730 static void dwc3_ep0_xfernotready(struct dwc3
*dwc
,
731 const struct dwc3_event_depevt
*event
)
733 switch (dwc
->ep0state
) {
734 case EP0_IN_WAIT_GADGET
:
735 dwc
->ep0state
= EP0_IN_WAIT_NRDY
;
737 case EP0_OUT_WAIT_GADGET
:
738 dwc
->ep0state
= EP0_OUT_WAIT_NRDY
;
741 case EP0_IN_WAIT_NRDY
:
742 case EP0_OUT_WAIT_NRDY
:
743 dwc3_ep0_do_setup_status(dwc
, event
);
747 case EP0_IN_STATUS_PHASE
:
748 case EP0_OUT_STATUS_PHASE
:
749 case EP0_IN_DATA_PHASE
:
750 case EP0_OUT_DATA_PHASE
:
751 case EP0_UNCONNECTED
:
757 void dwc3_ep0_interrupt(struct dwc3
*dwc
,
758 const const struct dwc3_event_depevt
*event
)
760 u8 epnum
= event
->endpoint_number
;
762 dev_dbg(dwc
->dev
, "%s while ep%d%s in state '%s'\n",
763 dwc3_ep_event_string(event
->endpoint_event
),
764 epnum
, (epnum
& 1) ? "in" : "out",
765 dwc3_ep0_state_string(dwc
->ep0state
));
767 switch (event
->endpoint_event
) {
768 case DWC3_DEPEVT_XFERCOMPLETE
:
769 dwc3_ep0_xfer_complete(dwc
, event
);
772 case DWC3_DEPEVT_XFERNOTREADY
:
773 dwc3_ep0_xfernotready(dwc
, event
);
776 case DWC3_DEPEVT_XFERINPROGRESS
:
777 case DWC3_DEPEVT_RXTXFIFOEVT
:
778 case DWC3_DEPEVT_STREAMEVT
:
779 case DWC3_DEPEVT_EPCMDCMPLT
: