39 #include <linux/kernel.h>
40 #include <linux/slab.h>
46 #include <linux/list.h>
49 #include <linux/usb/ch9.h>
57 static void __dwc3_ep0_do_control_status(
struct dwc3 *dwc,
struct dwc3_ep *dep);
58 static void __dwc3_ep0_do_control_data(
struct dwc3 *dwc,
71 return "Status Phase";
77 static int dwc3_ep0_start_trans(
struct dwc3 *dwc,
u8 epnum,
dma_addr_t buf_dma,
86 dep = dwc->
eps[epnum];
111 dev_dbg(dwc->
dev,
"failed to send STARTTRANSFER command\n");
124 static int __dwc3_gadget_ep0_queue(
struct dwc3_ep *dep,
150 dev_WARN(dwc->
dev,
"Unexpected pending request\n");
154 __dwc3_ep0_do_control_data(dwc, dwc->
eps[direction], req);
173 __dwc3_ep0_do_control_status(dwc, dwc->
eps[direction]);
175 dev_dbg(dwc->
dev,
"too early for delayed status\n");
218 __dwc3_ep0_do_control_data(dwc, dwc->
eps[direction], req);
239 dev_dbg(dwc->
dev,
"trying to queue request %p to disabled %s\n",
251 dev_vdbg(dwc->
dev,
"queueing request %p to %s length %d, state '%s'\n",
253 dwc3_ep0_state_string(dwc->
ep0state));
255 ret = __dwc3_gadget_ep0_queue(dep, req);
258 spin_unlock_irqrestore(&dwc->
lock, flags);
263 static void dwc3_ep0_stall_and_restart(
struct dwc3 *dwc)
293 dwc3_ep0_stall_and_restart(dwc);
317 dep = dwc->
eps[epnum];
330 static int dwc3_ep0_handle_status(
struct dwc3 *dwc,
365 dep = dwc3_wIndex_to_dep(dwc, ctrl->
wIndex);
381 dwc->
ep0_usb_req.request.length =
sizeof(*response_pkt);
383 dwc->
ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
385 return __dwc3_gadget_ep0_queue(dep, &dwc->
ep0_usb_req);
388 static int dwc3_ep0_handle_feature(
struct dwc3 *dwc,
421 reg &= ~DWC3_DCTL_INITU1ENA;
444 if ((wIndex & 0xff) != 0)
475 dep = dwc3_wIndex_to_dep(dwc, wIndex);
501 dev_dbg(dwc->
dev,
"invalid device address %d\n", addr);
506 dev_dbg(dwc->
dev,
"trying to set address when configured\n");
527 spin_unlock(&dwc->
lock);
529 spin_lock(&dwc->
lock);
548 ret = dwc3_ep0_delegate_req(dwc, ctrl);
566 ret = dwc3_ep0_delegate_req(dwc, ctrl);
593 memcpy(&timing, req->
buf,
sizeof(timing));
595 dwc->
u1sel = timing.u1sel;
596 dwc->
u1pel = timing.u1pel;
603 if (reg & DWC3_DCTL_INITU1ENA)
633 dev_err(dwc->
dev,
"Set SEL should be 6 bytes, got %d\n",
650 dwc->
ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
652 return __dwc3_gadget_ep0_queue(dep, &dwc->
ep0_usb_req);
665 if (wIndex || wLength)
684 ret = dwc3_ep0_handle_status(dwc, ctrl);
688 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
692 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
696 ret = dwc3_ep0_set_address(dwc, ctrl);
700 ret = dwc3_ep0_set_config(dwc, ctrl);
704 ret = dwc3_ep0_set_sel(dwc, ctrl);
708 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
711 dev_vdbg(dwc->
dev,
"Forwarding to gadget driver\n");
712 ret = dwc3_ep0_delegate_req(dwc, ctrl);
719 static void dwc3_ep0_inspect_setup(
struct dwc3 *dwc,
741 ret = dwc3_ep0_std_request(dwc, ctrl);
743 ret = dwc3_ep0_delegate_req(dwc, ctrl);
750 dwc3_ep0_stall_and_restart(dwc);
753 static void dwc3_ep0_complete_data(
struct dwc3 *dwc,
765 epnum =
event->endpoint_number;
777 dev_dbg(dwc->
dev,
"Setup Pending received\n");
789 unsigned maxp = ep0->
endpoint.maxpacket;
791 transfer_size += (maxp - (transfer_size % maxp));
793 transfer_size - length);
799 ur->
actual += transferred;
804 dwc3_ep0_stall_and_restart(dwc);
815 static void dwc3_ep0_complete_status(
struct dwc3 *dwc,
839 dwc3_ep0_stall_and_restart(dwc);
846 dev_dbg(dwc->
dev,
"Setup Pending received\n");
852 static void dwc3_ep0_xfer_complete(
struct dwc3 *dwc,
855 struct dwc3_ep *dep = dwc->
eps[
event->endpoint_number];
864 dwc3_ep0_inspect_setup(dwc, event);
869 dwc3_ep0_complete_data(dwc, event);
874 dwc3_ep0_complete_status(dwc, event);
881 static void __dwc3_ep0_do_control_data(
struct dwc3 *dwc,
888 if (req->
request.length == 0) {
889 ret = dwc3_ep0_start_trans(dwc, dep->
number,
915 ret = dwc3_ep0_start_trans(dwc, dep->
number,
926 ret = dwc3_ep0_start_trans(dwc, dep->
number, req->
request.dma,
933 static int dwc3_ep0_start_control_status(
struct dwc3_ep *dep)
941 return dwc3_ep0_start_trans(dwc, dep->
number,
945 static void __dwc3_ep0_do_control_status(
struct dwc3 *dwc,
struct dwc3_ep *dep)
948 dev_dbg(dwc->
dev,
"starting to resize fifos\n");
953 WARN_ON(dwc3_ep0_start_control_status(dep));
956 static void dwc3_ep0_do_control_status(
struct dwc3 *dwc,
959 struct dwc3_ep *dep = dwc->
eps[
event->endpoint_number];
961 __dwc3_ep0_do_control_status(dwc, dep);
964 static void dwc3_ep0_end_control_data(
struct dwc3 *dwc,
struct dwc3_ep *dep)
982 static void dwc3_ep0_xfernotready(
struct dwc3 *dwc,
1003 dev_vdbg(dwc->
dev,
"Wrong direction for Data phase\n");
1004 dwc3_ep0_end_control_data(dwc, dep);
1005 dwc3_ep0_stall_and_restart(dwc);
1021 dev_vdbg(dwc->
dev,
"Mass Storage delayed status\n");
1025 dwc3_ep0_do_control_status(dwc, event);
1032 u8 epnum =
event->endpoint_number;
1034 dev_dbg(dwc->
dev,
"%s while ep%d%s in state '%s'\n",
1036 epnum >> 1, (epnum & 1) ?
"in" :
"out",
1037 dwc3_ep0_state_string(dwc->
ep0state));
1041 dwc3_ep0_xfer_complete(dwc, event);
1045 dwc3_ep0_xfernotready(dwc, event);