Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1 | /** |
| 2 | * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 5 | * |
| 6 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 7 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions, and the following disclaimer, |
| 14 | * without modification. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. The names of the above-listed copyright holders may not be used |
| 19 | * to endorse or promote products derived from this software without |
| 20 | * specific prior written permission. |
| 21 | * |
| 22 | * ALTERNATIVELY, this software may be distributed under the terms of the |
| 23 | * GNU General Public License ("GPL") version 2, as published by the Free |
| 24 | * Software Foundation. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 27 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 28 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 30 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/platform_device.h> |
| 43 | #include <linux/pm_runtime.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/io.h> |
| 46 | #include <linux/list.h> |
| 47 | #include <linux/dma-mapping.h> |
| 48 | |
| 49 | #include <linux/usb/ch9.h> |
| 50 | #include <linux/usb/gadget.h> |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 51 | #include <linux/usb/composite.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 52 | |
| 53 | #include "core.h" |
| 54 | #include "gadget.h" |
| 55 | #include "io.h" |
| 56 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 57 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum); |
| 58 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 59 | static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state) |
| 60 | { |
| 61 | switch (state) { |
| 62 | case EP0_UNCONNECTED: |
| 63 | return "Unconnected"; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 64 | case EP0_SETUP_PHASE: |
| 65 | return "Setup Phase"; |
| 66 | case EP0_DATA_PHASE: |
| 67 | return "Data Phase"; |
| 68 | case EP0_STATUS_PHASE: |
| 69 | return "Status Phase"; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 70 | default: |
| 71 | return "UNKNOWN"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 76 | u32 len, u32 type) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 77 | { |
| 78 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 79 | struct dwc3_trb *trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 80 | struct dwc3_ep *dep; |
| 81 | |
| 82 | int ret; |
| 83 | |
| 84 | dep = dwc->eps[epnum]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 85 | if (dep->flags & DWC3_EP_BUSY) { |
| 86 | dev_vdbg(dwc->dev, "%s: still busy\n", dep->name); |
| 87 | return 0; |
| 88 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 89 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 90 | trb = dwc->ep0_trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 91 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 92 | trb->bpl = lower_32_bits(buf_dma); |
| 93 | trb->bph = upper_32_bits(buf_dma); |
| 94 | trb->size = len; |
| 95 | trb->ctrl = type; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 96 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 97 | trb->ctrl |= (DWC3_TRB_CTRL_HWO |
| 98 | | DWC3_TRB_CTRL_LST |
| 99 | | DWC3_TRB_CTRL_IOC |
| 100 | | DWC3_TRB_CTRL_ISP_IMI); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 101 | |
| 102 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 103 | params.param0 = upper_32_bits(dwc->ep0_trb_addr); |
| 104 | params.param1 = lower_32_bits(dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 105 | |
| 106 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 107 | DWC3_DEPCMD_STARTTRANSFER, ¶ms); |
| 108 | if (ret < 0) { |
| 109 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); |
| 110 | return ret; |
| 111 | } |
| 112 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 113 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 114 | dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc, |
| 115 | dep->number); |
| 116 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 117 | dwc->ep0_next_event = DWC3_EP0_COMPLETE; |
| 118 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, |
| 123 | struct dwc3_request *req) |
| 124 | { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 125 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 126 | int ret = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 127 | |
| 128 | req->request.actual = 0; |
| 129 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 130 | req->epnum = dep->number; |
| 131 | |
| 132 | list_add_tail(&req->list, &dep->request_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 133 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 134 | /* |
| 135 | * Gadget driver might not be quick enough to queue a request |
| 136 | * before we get a Transfer Not Ready event on this endpoint. |
| 137 | * |
| 138 | * In that case, we will set DWC3_EP_PENDING_REQUEST. When that |
| 139 | * flag is set, it's telling us that as soon as Gadget queues the |
| 140 | * required request, we should kick the transfer here because the |
| 141 | * IRQ we were waiting for is long gone. |
| 142 | */ |
| 143 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 144 | unsigned direction; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 145 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 146 | direction = !!(dep->flags & DWC3_EP0_DIR_IN); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 147 | |
Felipe Balbi | 68d8a78 | 2011-12-29 06:32:29 +0200 | [diff] [blame] | 148 | if (dwc->ep0state != EP0_DATA_PHASE) { |
| 149 | dev_WARN(dwc->dev, "Unexpected pending request\n"); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 152 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 153 | ret = dwc3_ep0_start_trans(dwc, direction, |
Felipe Balbi | 68d8a78 | 2011-12-29 06:32:29 +0200 | [diff] [blame] | 154 | req->request.dma, req->request.length, |
| 155 | DWC3_TRBCTL_CONTROL_DATA); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 156 | dep->flags &= ~(DWC3_EP_PENDING_REQUEST | |
| 157 | DWC3_EP0_DIR_IN); |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 158 | } else if (dwc->delayed_status) { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 159 | dwc->delayed_status = false; |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 160 | |
| 161 | if (dwc->ep0state == EP0_STATUS_PHASE) |
| 162 | dwc3_ep0_do_control_status(dwc, 1); |
| 163 | else |
| 164 | dev_dbg(dwc->dev, "too early for delayed status\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, |
| 171 | gfp_t gfp_flags) |
| 172 | { |
| 173 | struct dwc3_request *req = to_dwc3_request(request); |
| 174 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 175 | struct dwc3 *dwc = dep->dwc; |
| 176 | |
| 177 | unsigned long flags; |
| 178 | |
| 179 | int ret; |
| 180 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 181 | spin_lock_irqsave(&dwc->lock, flags); |
| 182 | if (!dep->desc) { |
| 183 | dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n", |
| 184 | request, dep->name); |
| 185 | ret = -ESHUTDOWN; |
| 186 | goto out; |
| 187 | } |
| 188 | |
| 189 | /* we share one TRB for ep0/1 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 190 | if (!list_empty(&dep->request_list)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 191 | ret = -EBUSY; |
| 192 | goto out; |
| 193 | } |
| 194 | |
| 195 | dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n", |
| 196 | request, dep->name, request->length, |
| 197 | dwc3_ep0_state_string(dwc->ep0state)); |
| 198 | |
| 199 | ret = __dwc3_gadget_ep0_queue(dep, req); |
| 200 | |
| 201 | out: |
| 202 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc) |
| 208 | { |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 209 | struct dwc3_ep *dep = dwc->eps[0]; |
| 210 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 211 | /* stall is always issued on EP0 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 212 | __dwc3_gadget_ep_set_halt(dep, 1); |
| 213 | dep->flags = DWC3_EP_ENABLED; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 214 | dwc->delayed_status = false; |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 215 | |
| 216 | if (!list_empty(&dep->request_list)) { |
| 217 | struct dwc3_request *req; |
| 218 | |
| 219 | req = next_request(&dep->request_list); |
| 220 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 221 | } |
| 222 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 223 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 224 | dwc3_ep0_out_start(dwc); |
| 225 | } |
| 226 | |
| 227 | void dwc3_ep0_out_start(struct dwc3 *dwc) |
| 228 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 229 | int ret; |
| 230 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 231 | ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, |
| 232 | DWC3_TRBCTL_CONTROL_SETUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 233 | WARN_ON(ret < 0); |
| 234 | } |
| 235 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 236 | static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) |
| 237 | { |
| 238 | struct dwc3_ep *dep; |
| 239 | u32 windex = le16_to_cpu(wIndex_le); |
| 240 | u32 epnum; |
| 241 | |
| 242 | epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1; |
| 243 | if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) |
| 244 | epnum |= 1; |
| 245 | |
| 246 | dep = dwc->eps[epnum]; |
| 247 | if (dep->flags & DWC3_EP_ENABLED) |
| 248 | return dep; |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 253 | static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 254 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 255 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 256 | /* |
| 257 | * ch 9.4.5 |
| 258 | */ |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 259 | static int dwc3_ep0_handle_status(struct dwc3 *dwc, |
| 260 | struct usb_ctrlrequest *ctrl) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 261 | { |
| 262 | struct dwc3_ep *dep; |
| 263 | u32 recip; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 264 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 265 | u16 usb_status = 0; |
| 266 | __le16 *response_pkt; |
| 267 | |
| 268 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 269 | switch (recip) { |
| 270 | case USB_RECIP_DEVICE: |
| 271 | /* |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 272 | * LTM will be set once we know how to set this in HW. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 273 | */ |
| 274 | usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 275 | |
| 276 | if (dwc->speed == DWC3_DSTS_SUPERSPEED) { |
| 277 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 278 | if (reg & DWC3_DCTL_INITU1ENA) |
| 279 | usb_status |= 1 << USB_DEV_STAT_U1_ENABLED; |
| 280 | if (reg & DWC3_DCTL_INITU2ENA) |
| 281 | usb_status |= 1 << USB_DEV_STAT_U2_ENABLED; |
| 282 | } |
| 283 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 284 | break; |
| 285 | |
| 286 | case USB_RECIP_INTERFACE: |
| 287 | /* |
| 288 | * Function Remote Wake Capable D0 |
| 289 | * Function Remote Wakeup D1 |
| 290 | */ |
| 291 | break; |
| 292 | |
| 293 | case USB_RECIP_ENDPOINT: |
| 294 | dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex); |
| 295 | if (!dep) |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 296 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 297 | |
| 298 | if (dep->flags & DWC3_EP_STALL) |
| 299 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 300 | break; |
| 301 | default: |
| 302 | return -EINVAL; |
| 303 | }; |
| 304 | |
| 305 | response_pkt = (__le16 *) dwc->setup_buf; |
| 306 | *response_pkt = cpu_to_le16(usb_status); |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 307 | |
| 308 | dep = dwc->eps[0]; |
| 309 | dwc->ep0_usb_req.dep = dep; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 310 | dwc->ep0_usb_req.request.length = sizeof(*response_pkt); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 311 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 312 | dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl; |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 313 | |
| 314 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static int dwc3_ep0_handle_feature(struct dwc3 *dwc, |
| 318 | struct usb_ctrlrequest *ctrl, int set) |
| 319 | { |
| 320 | struct dwc3_ep *dep; |
| 321 | u32 recip; |
| 322 | u32 wValue; |
| 323 | u32 wIndex; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 324 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 325 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 326 | |
| 327 | wValue = le16_to_cpu(ctrl->wValue); |
| 328 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 329 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 330 | switch (recip) { |
| 331 | case USB_RECIP_DEVICE: |
| 332 | |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 333 | switch (wValue) { |
| 334 | case USB_DEVICE_REMOTE_WAKEUP: |
| 335 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 336 | /* |
| 337 | * 9.4.1 says only only for SS, in AddressState only for |
| 338 | * default control pipe |
| 339 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 340 | case USB_DEVICE_U1_ENABLE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 341 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 342 | return -EINVAL; |
| 343 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 344 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 345 | |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 346 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 347 | if (set) |
| 348 | reg |= DWC3_DCTL_INITU1ENA; |
| 349 | else |
| 350 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 351 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 352 | break; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 353 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 354 | case USB_DEVICE_U2_ENABLE: |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 355 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 356 | return -EINVAL; |
| 357 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 361 | if (set) |
| 362 | reg |= DWC3_DCTL_INITU2ENA; |
| 363 | else |
| 364 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 365 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 366 | break; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 367 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 368 | case USB_DEVICE_LTM_ENABLE: |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 369 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 370 | break; |
| 371 | |
| 372 | case USB_DEVICE_TEST_MODE: |
| 373 | if ((wIndex & 0xff) != 0) |
| 374 | return -EINVAL; |
| 375 | if (!set) |
| 376 | return -EINVAL; |
| 377 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 378 | dwc->test_mode_nr = wIndex >> 8; |
| 379 | dwc->test_mode = true; |
Gerard Cauvy | ecb0779 | 2012-03-16 16:20:10 +0200 | [diff] [blame] | 380 | break; |
| 381 | default: |
| 382 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 383 | } |
| 384 | break; |
| 385 | |
| 386 | case USB_RECIP_INTERFACE: |
| 387 | switch (wValue) { |
| 388 | case USB_INTRF_FUNC_SUSPEND: |
| 389 | if (wIndex & USB_INTRF_FUNC_SUSPEND_LP) |
| 390 | /* XXX enable Low power suspend */ |
| 391 | ; |
| 392 | if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) |
| 393 | /* XXX enable remote wakeup */ |
| 394 | ; |
| 395 | break; |
| 396 | default: |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | break; |
| 400 | |
| 401 | case USB_RECIP_ENDPOINT: |
| 402 | switch (wValue) { |
| 403 | case USB_ENDPOINT_HALT: |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 404 | dep = dwc3_wIndex_to_dep(dwc, wIndex); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 405 | if (!dep) |
| 406 | return -EINVAL; |
| 407 | ret = __dwc3_gadget_ep_set_halt(dep, set); |
| 408 | if (ret) |
| 409 | return -EINVAL; |
| 410 | break; |
| 411 | default: |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | break; |
| 415 | |
| 416 | default: |
| 417 | return -EINVAL; |
| 418 | }; |
| 419 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 424 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 425 | u32 addr; |
| 426 | u32 reg; |
| 427 | |
| 428 | addr = le16_to_cpu(ctrl->wValue); |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 429 | if (addr > 127) { |
| 430 | dev_dbg(dwc->dev, "invalid device address %d\n", addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 431 | return -EINVAL; |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | if (dwc->dev_state == DWC3_CONFIGURED_STATE) { |
| 435 | dev_dbg(dwc->dev, "trying to set address when configured\n"); |
| 436 | return -EINVAL; |
| 437 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 438 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 439 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 440 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 441 | reg |= DWC3_DCFG_DEVADDR(addr); |
| 442 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 443 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 444 | if (addr) |
| 445 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 446 | else |
| 447 | dwc->dev_state = DWC3_DEFAULT_STATE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 448 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 449 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 453 | { |
| 454 | int ret; |
| 455 | |
| 456 | spin_unlock(&dwc->lock); |
| 457 | ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); |
| 458 | spin_lock(&dwc->lock); |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 463 | { |
| 464 | u32 cfg; |
| 465 | int ret; |
| 466 | |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 467 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 468 | cfg = le16_to_cpu(ctrl->wValue); |
| 469 | |
| 470 | switch (dwc->dev_state) { |
| 471 | case DWC3_DEFAULT_STATE: |
| 472 | return -EINVAL; |
| 473 | break; |
| 474 | |
| 475 | case DWC3_ADDRESS_STATE: |
| 476 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 477 | /* if the cfg matches and the cfg is non zero */ |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 478 | if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 479 | dwc->dev_state = DWC3_CONFIGURED_STATE; |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 480 | dwc->resize_fifos = true; |
| 481 | dev_dbg(dwc->dev, "resize fifos flag SET\n"); |
| 482 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 483 | break; |
| 484 | |
| 485 | case DWC3_CONFIGURED_STATE: |
| 486 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 487 | if (!cfg) |
| 488 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 489 | break; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 490 | default: |
| 491 | ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 492 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 493 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 494 | } |
| 495 | |
Felipe Balbi | 9e788d6 | 2012-04-24 16:19:49 +0300 | [diff] [blame^] | 496 | static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req) |
| 497 | { |
| 498 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 499 | struct dwc3 *dwc = dep->dwc; |
| 500 | |
| 501 | u32 param = 0; |
| 502 | u32 reg; |
| 503 | |
| 504 | struct timing { |
| 505 | u8 u1sel; |
| 506 | u8 u1pel; |
| 507 | u16 u2sel; |
| 508 | u16 u2pel; |
| 509 | } __packed timing; |
| 510 | |
| 511 | int ret; |
| 512 | |
| 513 | memcpy(&timing, req->buf, sizeof(timing)); |
| 514 | |
| 515 | dwc->u1sel = timing.u1sel; |
| 516 | dwc->u1pel = timing.u1pel; |
| 517 | dwc->u2sel = timing.u2sel; |
| 518 | dwc->u2pel = timing.u2pel; |
| 519 | |
| 520 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 521 | if (reg & DWC3_DCTL_INITU2ENA) |
| 522 | param = dwc->u2pel; |
| 523 | if (reg & DWC3_DCTL_INITU1ENA) |
| 524 | param = dwc->u1pel; |
| 525 | |
| 526 | /* |
| 527 | * According to Synopsys Databook, if parameter is |
| 528 | * greater than 125, a value of zero should be |
| 529 | * programmed in the register. |
| 530 | */ |
| 531 | if (param > 125) |
| 532 | param = 0; |
| 533 | |
| 534 | /* now that we have the time, issue DGCMD Set Sel */ |
| 535 | ret = dwc3_send_gadget_generic_command(dwc, |
| 536 | DWC3_DGCMD_SET_PERIODIC_PAR, param); |
| 537 | WARN_ON(ret < 0); |
| 538 | } |
| 539 | |
| 540 | static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 541 | { |
| 542 | struct dwc3_ep *dep; |
| 543 | u16 wLength; |
| 544 | u16 wValue; |
| 545 | |
| 546 | if (dwc->dev_state == DWC3_DEFAULT_STATE) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | wValue = le16_to_cpu(ctrl->wValue); |
| 550 | wLength = le16_to_cpu(ctrl->wLength); |
| 551 | |
| 552 | if (wLength != 6) { |
| 553 | dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n", |
| 554 | wLength); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * To handle Set SEL we need to receive 6 bytes from Host. So let's |
| 560 | * queue a usb_request for 6 bytes. |
| 561 | * |
| 562 | * Remember, though, this controller can't handle non-wMaxPacketSize |
| 563 | * aligned transfers on the OUT direction, so we queue a request for |
| 564 | * wMaxPacketSize instead. |
| 565 | */ |
| 566 | dep = dwc->eps[0]; |
| 567 | dwc->ep0_usb_req.dep = dep; |
| 568 | dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket; |
| 569 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
| 570 | dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl; |
| 571 | |
| 572 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
| 573 | } |
| 574 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 575 | static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 576 | { |
| 577 | int ret; |
| 578 | |
| 579 | switch (ctrl->bRequest) { |
| 580 | case USB_REQ_GET_STATUS: |
| 581 | dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n"); |
| 582 | ret = dwc3_ep0_handle_status(dwc, ctrl); |
| 583 | break; |
| 584 | case USB_REQ_CLEAR_FEATURE: |
| 585 | dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n"); |
| 586 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 0); |
| 587 | break; |
| 588 | case USB_REQ_SET_FEATURE: |
| 589 | dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n"); |
| 590 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 1); |
| 591 | break; |
| 592 | case USB_REQ_SET_ADDRESS: |
| 593 | dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n"); |
| 594 | ret = dwc3_ep0_set_address(dwc, ctrl); |
| 595 | break; |
| 596 | case USB_REQ_SET_CONFIGURATION: |
| 597 | dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n"); |
| 598 | ret = dwc3_ep0_set_config(dwc, ctrl); |
| 599 | break; |
Felipe Balbi | 9e788d6 | 2012-04-24 16:19:49 +0300 | [diff] [blame^] | 600 | case USB_REQ_SET_SEL: |
| 601 | dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n"); |
| 602 | ret = dwc3_ep0_set_sel(dwc, ctrl); |
| 603 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 604 | default: |
| 605 | dev_vdbg(dwc->dev, "Forwarding to gadget driver\n"); |
| 606 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 607 | break; |
| 608 | }; |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | static void dwc3_ep0_inspect_setup(struct dwc3 *dwc, |
| 614 | const struct dwc3_event_depevt *event) |
| 615 | { |
| 616 | struct usb_ctrlrequest *ctrl = dwc->ctrl_req; |
| 617 | int ret; |
| 618 | u32 len; |
| 619 | |
| 620 | if (!dwc->gadget_driver) |
| 621 | goto err; |
| 622 | |
| 623 | len = le16_to_cpu(ctrl->wLength); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 624 | if (!len) { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 625 | dwc->three_stage_setup = false; |
| 626 | dwc->ep0_expect_in = false; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 627 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 628 | } else { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 629 | dwc->three_stage_setup = true; |
| 630 | dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 631 | dwc->ep0_next_event = DWC3_EP0_NRDY_DATA; |
| 632 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 633 | |
| 634 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
| 635 | ret = dwc3_ep0_std_request(dwc, ctrl); |
| 636 | else |
| 637 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 638 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 639 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 640 | dwc->delayed_status = true; |
| 641 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 642 | if (ret >= 0) |
| 643 | return; |
| 644 | |
| 645 | err: |
| 646 | dwc3_ep0_stall_and_restart(dwc); |
| 647 | } |
| 648 | |
| 649 | static void dwc3_ep0_complete_data(struct dwc3 *dwc, |
| 650 | const struct dwc3_event_depevt *event) |
| 651 | { |
| 652 | struct dwc3_request *r = NULL; |
| 653 | struct usb_request *ur; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 654 | struct dwc3_trb *trb; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 655 | struct dwc3_ep *ep0; |
Felipe Balbi | c611ccb | 2011-08-27 02:30:33 +0300 | [diff] [blame] | 656 | u32 transferred; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 657 | u32 length; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 658 | u8 epnum; |
| 659 | |
| 660 | epnum = event->endpoint_number; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 661 | ep0 = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 662 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 663 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 664 | |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 665 | r = next_request(&ep0->request_list); |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 666 | ur = &r->request; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 667 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 668 | trb = dwc->ep0_trb; |
| 669 | length = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 670 | |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 671 | if (dwc->ep0_bounced) { |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 672 | unsigned transfer_size = ur->length; |
| 673 | unsigned maxp = ep0->endpoint.maxpacket; |
| 674 | |
| 675 | transfer_size += (maxp - (transfer_size % maxp)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 676 | transferred = min_t(u32, ur->length, |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 677 | transfer_size - length); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 678 | memcpy(ur->buf, dwc->ep0_bounce, transferred); |
| 679 | dwc->ep0_bounced = false; |
| 680 | } else { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 681 | transferred = ur->length - length; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 682 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 683 | |
Felipe Balbi | cd423dd | 2012-03-21 11:44:00 +0200 | [diff] [blame] | 684 | ur->actual += transferred; |
| 685 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 686 | if ((epnum & 1) && ur->actual < ur->length) { |
| 687 | /* for some reason we did not get everything out */ |
| 688 | |
| 689 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 690 | } else { |
| 691 | /* |
| 692 | * handle the case where we have to send a zero packet. This |
| 693 | * seems to be case when req.length > maxpacket. Could it be? |
| 694 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 695 | if (r) |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 696 | dwc3_gadget_giveback(ep0, r, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | static void dwc3_ep0_complete_req(struct dwc3 *dwc, |
| 701 | const struct dwc3_event_depevt *event) |
| 702 | { |
| 703 | struct dwc3_request *r; |
| 704 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 705 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 706 | dep = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 707 | |
| 708 | if (!list_empty(&dep->request_list)) { |
| 709 | r = next_request(&dep->request_list); |
| 710 | |
| 711 | dwc3_gadget_giveback(dep, r, 0); |
| 712 | } |
| 713 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 714 | if (dwc->test_mode) { |
| 715 | int ret; |
| 716 | |
| 717 | ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr); |
| 718 | if (ret < 0) { |
| 719 | dev_dbg(dwc->dev, "Invalid Test #%d\n", |
| 720 | dwc->test_mode_nr); |
| 721 | dwc3_ep0_stall_and_restart(dwc); |
| 722 | } |
| 723 | } |
| 724 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 725 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 726 | dwc3_ep0_out_start(dwc); |
| 727 | } |
| 728 | |
| 729 | static void dwc3_ep0_xfer_complete(struct dwc3 *dwc, |
| 730 | const struct dwc3_event_depevt *event) |
| 731 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 732 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 733 | |
| 734 | dep->flags &= ~DWC3_EP_BUSY; |
Paul Zimmerman | c2df85c | 2012-02-24 17:32:18 -0800 | [diff] [blame] | 735 | dep->res_trans_idx = 0; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 736 | dwc->setup_packet_pending = false; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 737 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 738 | switch (dwc->ep0state) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 739 | case EP0_SETUP_PHASE: |
| 740 | dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 741 | dwc3_ep0_inspect_setup(dwc, event); |
| 742 | break; |
| 743 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 744 | case EP0_DATA_PHASE: |
| 745 | dev_vdbg(dwc->dev, "Data Phase\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 746 | dwc3_ep0_complete_data(dwc, event); |
| 747 | break; |
| 748 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 749 | case EP0_STATUS_PHASE: |
| 750 | dev_vdbg(dwc->dev, "Status Phase\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 751 | dwc3_ep0_complete_req(dwc, event); |
| 752 | break; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 753 | default: |
| 754 | WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 758 | static void dwc3_ep0_do_control_setup(struct dwc3 *dwc, |
| 759 | const struct dwc3_event_depevt *event) |
| 760 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 761 | dwc3_ep0_out_start(dwc); |
| 762 | } |
| 763 | |
| 764 | static void dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 765 | const struct dwc3_event_depevt *event) |
| 766 | { |
| 767 | struct dwc3_ep *dep; |
| 768 | struct dwc3_request *req; |
| 769 | int ret; |
| 770 | |
| 771 | dep = dwc->eps[0]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 772 | |
| 773 | if (list_empty(&dep->request_list)) { |
| 774 | dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n"); |
| 775 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 776 | |
| 777 | if (event->endpoint_number) |
| 778 | dep->flags |= DWC3_EP0_DIR_IN; |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | req = next_request(&dep->request_list); |
| 783 | req->direction = !!event->endpoint_number; |
| 784 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 785 | if (req->request.length == 0) { |
| 786 | ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, |
| 787 | dwc->ctrl_req_addr, 0, |
| 788 | DWC3_TRBCTL_CONTROL_DATA); |
| 789 | } else if ((req->request.length % dep->endpoint.maxpacket) |
| 790 | && (event->endpoint_number == 0)) { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 791 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 792 | event->endpoint_number); |
| 793 | if (ret) { |
| 794 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 795 | return; |
| 796 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 797 | |
| 798 | WARN_ON(req->request.length > dep->endpoint.maxpacket); |
| 799 | |
| 800 | dwc->ep0_bounced = true; |
| 801 | |
| 802 | /* |
| 803 | * REVISIT in case request length is bigger than EP0 |
| 804 | * wMaxPacketSize, we will need two chained TRBs to handle |
| 805 | * the transfer. |
| 806 | */ |
| 807 | ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, |
| 808 | dwc->ep0_bounce_addr, dep->endpoint.maxpacket, |
| 809 | DWC3_TRBCTL_CONTROL_DATA); |
| 810 | } else { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 811 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 812 | event->endpoint_number); |
| 813 | if (ret) { |
| 814 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 815 | return; |
| 816 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 817 | |
| 818 | ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, |
| 819 | req->request.dma, req->request.length, |
| 820 | DWC3_TRBCTL_CONTROL_DATA); |
| 821 | } |
| 822 | |
| 823 | WARN_ON(ret < 0); |
| 824 | } |
| 825 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 826 | static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 827 | { |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 828 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 829 | u32 type; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 830 | |
| 831 | type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3 |
| 832 | : DWC3_TRBCTL_CONTROL_STATUS2; |
| 833 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 834 | return dwc3_ep0_start_trans(dwc, dep->number, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 835 | dwc->ctrl_req_addr, 0, type); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 836 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 837 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 838 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum) |
| 839 | { |
| 840 | struct dwc3_ep *dep = dwc->eps[epnum]; |
| 841 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 842 | if (dwc->resize_fifos) { |
| 843 | dev_dbg(dwc->dev, "starting to resize fifos\n"); |
| 844 | dwc3_gadget_resize_tx_fifos(dwc); |
| 845 | dwc->resize_fifos = 0; |
| 846 | } |
| 847 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 848 | WARN_ON(dwc3_ep0_start_control_status(dep)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 849 | } |
| 850 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 851 | static void dwc3_ep0_xfernotready(struct dwc3 *dwc, |
| 852 | const struct dwc3_event_depevt *event) |
| 853 | { |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 854 | dwc->setup_packet_pending = true; |
| 855 | |
Felipe Balbi | 9cc9bcd | 2011-10-18 18:00:26 +0300 | [diff] [blame] | 856 | /* |
| 857 | * This part is very tricky: If we has just handled |
| 858 | * XferNotReady(Setup) and we're now expecting a |
| 859 | * XferComplete but, instead, we receive another |
| 860 | * XferNotReady(Setup), we should STALL and restart |
| 861 | * the state machine. |
| 862 | * |
| 863 | * In all other cases, we just continue waiting |
| 864 | * for the XferComplete event. |
| 865 | * |
| 866 | * We are a little bit unsafe here because we're |
| 867 | * not trying to ensure that last event was, indeed, |
| 868 | * XferNotReady(Setup). |
| 869 | * |
| 870 | * Still, we don't expect any condition where that |
| 871 | * should happen and, even if it does, it would be |
| 872 | * another error condition. |
| 873 | */ |
| 874 | if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) { |
| 875 | switch (event->status) { |
| 876 | case DEPEVT_STATUS_CONTROL_SETUP: |
| 877 | dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n"); |
| 878 | dwc3_ep0_stall_and_restart(dwc); |
| 879 | break; |
| 880 | case DEPEVT_STATUS_CONTROL_DATA: |
| 881 | /* FALLTHROUGH */ |
| 882 | case DEPEVT_STATUS_CONTROL_STATUS: |
| 883 | /* FALLTHROUGH */ |
| 884 | default: |
| 885 | dev_vdbg(dwc->dev, "waiting for XferComplete\n"); |
| 886 | } |
| 887 | |
| 888 | return; |
| 889 | } |
| 890 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 891 | switch (event->status) { |
| 892 | case DEPEVT_STATUS_CONTROL_SETUP: |
| 893 | dev_vdbg(dwc->dev, "Control Setup\n"); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 894 | |
| 895 | dwc->ep0state = EP0_SETUP_PHASE; |
| 896 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 897 | dwc3_ep0_do_control_setup(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 898 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 899 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 900 | case DEPEVT_STATUS_CONTROL_DATA: |
| 901 | dev_vdbg(dwc->dev, "Control Data\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 902 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 903 | dwc->ep0state = EP0_DATA_PHASE; |
| 904 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 905 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) { |
| 906 | dev_vdbg(dwc->dev, "Expected %d got %d\n", |
Felipe Balbi | 25355be | 2011-09-30 10:58:38 +0300 | [diff] [blame] | 907 | dwc->ep0_next_event, |
| 908 | DWC3_EP0_NRDY_DATA); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 909 | |
| 910 | dwc3_ep0_stall_and_restart(dwc); |
| 911 | return; |
| 912 | } |
| 913 | |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 914 | /* |
| 915 | * One of the possible error cases is when Host _does_ |
| 916 | * request for Data Phase, but it does so on the wrong |
| 917 | * direction. |
| 918 | * |
| 919 | * Here, we already know ep0_next_event is DATA (see above), |
| 920 | * so we only need to check for direction. |
| 921 | */ |
| 922 | if (dwc->ep0_expect_in != event->endpoint_number) { |
| 923 | dev_vdbg(dwc->dev, "Wrong direction for Data phase\n"); |
| 924 | dwc3_ep0_stall_and_restart(dwc); |
| 925 | return; |
| 926 | } |
| 927 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 928 | dwc3_ep0_do_control_data(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 929 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 930 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 931 | case DEPEVT_STATUS_CONTROL_STATUS: |
| 932 | dev_vdbg(dwc->dev, "Control Status\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 933 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 934 | dwc->ep0state = EP0_STATUS_PHASE; |
| 935 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 936 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) { |
| 937 | dev_vdbg(dwc->dev, "Expected %d got %d\n", |
Felipe Balbi | 25355be | 2011-09-30 10:58:38 +0300 | [diff] [blame] | 938 | dwc->ep0_next_event, |
| 939 | DWC3_EP0_NRDY_STATUS); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 940 | |
| 941 | dwc3_ep0_stall_and_restart(dwc); |
| 942 | return; |
| 943 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 944 | |
| 945 | if (dwc->delayed_status) { |
| 946 | WARN_ON_ONCE(event->endpoint_number != 1); |
| 947 | dev_vdbg(dwc->dev, "Mass Storage delayed status\n"); |
| 948 | return; |
| 949 | } |
| 950 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 951 | dwc3_ep0_do_control_status(dwc, event->endpoint_number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
| 955 | void dwc3_ep0_interrupt(struct dwc3 *dwc, |
Felipe Balbi | 8becf27 | 2011-11-04 12:40:05 +0200 | [diff] [blame] | 956 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 957 | { |
| 958 | u8 epnum = event->endpoint_number; |
| 959 | |
| 960 | dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n", |
| 961 | dwc3_ep_event_string(event->endpoint_event), |
Sebastian Andrzej Siewior | b147f35 | 2011-09-30 10:58:40 +0300 | [diff] [blame] | 962 | epnum >> 1, (epnum & 1) ? "in" : "out", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 963 | dwc3_ep0_state_string(dwc->ep0state)); |
| 964 | |
| 965 | switch (event->endpoint_event) { |
| 966 | case DWC3_DEPEVT_XFERCOMPLETE: |
| 967 | dwc3_ep0_xfer_complete(dwc, event); |
| 968 | break; |
| 969 | |
| 970 | case DWC3_DEPEVT_XFERNOTREADY: |
| 971 | dwc3_ep0_xfernotready(dwc, event); |
| 972 | break; |
| 973 | |
| 974 | case DWC3_DEPEVT_XFERINPROGRESS: |
| 975 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 976 | case DWC3_DEPEVT_STREAMEVT: |
| 977 | case DWC3_DEPEVT_EPCMDCMPLT: |
| 978 | break; |
| 979 | } |
| 980 | } |