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" |
Vijayavardhan Vennapusa | ffeb26b | 2013-02-14 16:33:30 +0530 | [diff] [blame] | 56 | #include "debug.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 57 | |
Felipe Balbi | 9610cd2 | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 58 | static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep); |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 59 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 60 | struct dwc3_ep *dep, struct dwc3_request *req); |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 61 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 62 | static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state) |
| 63 | { |
| 64 | switch (state) { |
| 65 | case EP0_UNCONNECTED: |
| 66 | return "Unconnected"; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 67 | case EP0_SETUP_PHASE: |
| 68 | return "Setup Phase"; |
| 69 | case EP0_DATA_PHASE: |
| 70 | return "Data Phase"; |
| 71 | case EP0_STATUS_PHASE: |
| 72 | return "Status Phase"; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 73 | default: |
| 74 | return "UNKNOWN"; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | 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] | 79 | u32 len, u32 type) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 80 | { |
| 81 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 82 | struct dwc3_trb *trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 83 | struct dwc3_ep *dep; |
| 84 | |
| 85 | int ret; |
| 86 | |
| 87 | dep = dwc->eps[epnum]; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 88 | if (dep->flags & DWC3_EP_BUSY) { |
| 89 | dev_vdbg(dwc->dev, "%s: still busy\n", dep->name); |
| 90 | return 0; |
| 91 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 92 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 93 | trb = dwc->ep0_trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 94 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 95 | trb->bpl = lower_32_bits(buf_dma); |
| 96 | trb->bph = upper_32_bits(buf_dma); |
| 97 | trb->size = len; |
| 98 | trb->ctrl = type; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 99 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 100 | trb->ctrl |= (DWC3_TRB_CTRL_HWO |
| 101 | | DWC3_TRB_CTRL_LST |
| 102 | | DWC3_TRB_CTRL_IOC |
| 103 | | DWC3_TRB_CTRL_ISP_IMI); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 104 | |
| 105 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 106 | params.param0 = upper_32_bits(dwc->ep0_trb_addr); |
| 107 | params.param1 = lower_32_bits(dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 108 | |
| 109 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 110 | DWC3_DEPCMD_STARTTRANSFER, ¶ms); |
| 111 | if (ret < 0) { |
| 112 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 116 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 117 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 118 | dep->number); |
| 119 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 120 | dwc->ep0_next_event = DWC3_EP0_COMPLETE; |
| 121 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, |
| 126 | struct dwc3_request *req) |
| 127 | { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 128 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 129 | |
| 130 | req->request.actual = 0; |
| 131 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 132 | req->epnum = dep->number; |
| 133 | |
| 134 | list_add_tail(&req->list, &dep->request_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 135 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 136 | /* |
| 137 | * Gadget driver might not be quick enough to queue a request |
| 138 | * before we get a Transfer Not Ready event on this endpoint. |
| 139 | * |
| 140 | * In that case, we will set DWC3_EP_PENDING_REQUEST. When that |
| 141 | * flag is set, it's telling us that as soon as Gadget queues the |
| 142 | * required request, we should kick the transfer here because the |
| 143 | * IRQ we were waiting for is long gone. |
| 144 | */ |
| 145 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 146 | unsigned direction; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 147 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 148 | direction = !!(dep->flags & DWC3_EP0_DIR_IN); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 149 | |
Felipe Balbi | 68d8a78 | 2011-12-29 06:32:29 +0200 | [diff] [blame] | 150 | if (dwc->ep0state != EP0_DATA_PHASE) { |
| 151 | dev_WARN(dwc->dev, "Unexpected pending request\n"); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 154 | |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 155 | __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req); |
| 156 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 157 | dep->flags &= ~(DWC3_EP_PENDING_REQUEST | |
| 158 | DWC3_EP0_DIR_IN); |
Felipe Balbi | 929646c | 2012-07-19 08:51:13 +0300 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * In case gadget driver asked us to delay the STATUS phase, |
| 165 | * handle it here. |
| 166 | */ |
| 167 | if (dwc->delayed_status) { |
Felipe Balbi | a866ee0 | 2012-07-19 21:05:08 +0300 | [diff] [blame] | 168 | unsigned direction; |
| 169 | |
| 170 | direction = !dwc->ep0_expect_in; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 171 | dwc->delayed_status = false; |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 172 | |
| 173 | if (dwc->ep0state == EP0_STATUS_PHASE) |
Felipe Balbi | a866ee0 | 2012-07-19 21:05:08 +0300 | [diff] [blame] | 174 | __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]); |
Felipe Balbi | 68d3e66 | 2011-12-08 13:56:27 +0200 | [diff] [blame] | 175 | else |
| 176 | dev_dbg(dwc->dev, "too early for delayed status\n"); |
Felipe Balbi | 929646c | 2012-07-19 08:51:13 +0300 | [diff] [blame] | 177 | |
| 178 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 179 | } |
| 180 | |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 181 | /* |
| 182 | * Unfortunately we have uncovered a limitation wrt the Data Phase. |
| 183 | * |
| 184 | * Section 9.4 says we can wait for the XferNotReady(DATA) event to |
| 185 | * come before issueing Start Transfer command, but if we do, we will |
| 186 | * miss situations where the host starts another SETUP phase instead of |
| 187 | * the DATA phase. Such cases happen at least on TD.7.6 of the Link |
| 188 | * Layer Compliance Suite. |
| 189 | * |
| 190 | * The problem surfaces due to the fact that in case of back-to-back |
| 191 | * SETUP packets there will be no XferNotReady(DATA) generated and we |
| 192 | * will be stuck waiting for XferNotReady(DATA) forever. |
| 193 | * |
| 194 | * By looking at tables 9-13 and 9-14 of the Databook, we can see that |
| 195 | * it tells us to start Data Phase right away. It also mentions that if |
| 196 | * we receive a SETUP phase instead of the DATA phase, core will issue |
| 197 | * XferComplete for the DATA phase, before actually initiating it in |
| 198 | * the wire, with the TRB's status set to "SETUP_PENDING". Such status |
| 199 | * can only be used to print some debugging logs, as the core expects |
| 200 | * us to go through to the STATUS phase and start a CONTROL_STATUS TRB, |
| 201 | * just so it completes right away, without transferring anything and, |
| 202 | * only then, we can go back to the SETUP phase. |
| 203 | * |
| 204 | * Because of this scenario, SNPS decided to change the programming |
| 205 | * model of control transfers and support on-demand transfers only for |
| 206 | * the STATUS phase. To fix the issue we have now, we will always wait |
| 207 | * for gadget driver to queue the DATA phase's struct usb_request, then |
| 208 | * start it right away. |
| 209 | * |
| 210 | * If we're actually in a 2-stage transfer, we will wait for |
| 211 | * XferNotReady(STATUS). |
| 212 | */ |
| 213 | if (dwc->three_stage_setup) { |
| 214 | unsigned direction; |
| 215 | |
| 216 | direction = dwc->ep0_expect_in; |
| 217 | dwc->ep0state = EP0_DATA_PHASE; |
| 218 | |
| 219 | __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req); |
| 220 | |
| 221 | dep->flags &= ~DWC3_EP0_DIR_IN; |
| 222 | } |
| 223 | |
Felipe Balbi | 2640f34 | 2012-07-19 08:49:01 +0300 | [diff] [blame] | 224 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, |
| 228 | gfp_t gfp_flags) |
| 229 | { |
| 230 | struct dwc3_request *req = to_dwc3_request(request); |
| 231 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 232 | struct dwc3 *dwc = dep->dwc; |
| 233 | |
| 234 | unsigned long flags; |
| 235 | |
| 236 | int ret; |
| 237 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 238 | spin_lock_irqsave(&dwc->lock, flags); |
Ido Shayevitz | 57cdac1 | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 239 | if (!dep->endpoint.desc) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 240 | dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n", |
| 241 | request, dep->name); |
| 242 | ret = -ESHUTDOWN; |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | /* we share one TRB for ep0/1 */ |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 247 | if (!list_empty(&dep->request_list)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 248 | ret = -EBUSY; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n", |
| 253 | request, dep->name, request->length, |
| 254 | dwc3_ep0_state_string(dwc->ep0state)); |
| 255 | |
| 256 | ret = __dwc3_gadget_ep0_queue(dep, req); |
| 257 | |
| 258 | out: |
| 259 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc) |
| 265 | { |
Felipe Balbi | 0c42163 | 2012-07-23 09:07:41 +0300 | [diff] [blame] | 266 | struct dwc3_ep *dep; |
| 267 | |
| 268 | /* reinitialize physical ep1 */ |
| 269 | dep = dwc->eps[1]; |
| 270 | dep->flags = DWC3_EP_ENABLED; |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 271 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 272 | /* stall is always issued on EP0 */ |
Felipe Balbi | 0c42163 | 2012-07-23 09:07:41 +0300 | [diff] [blame] | 273 | dep = dwc->eps[0]; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 274 | __dwc3_gadget_ep_set_halt(dep, 1); |
| 275 | dep->flags = DWC3_EP_ENABLED; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 276 | dwc->delayed_status = false; |
Felipe Balbi | d742220 | 2011-09-08 18:17:12 +0300 | [diff] [blame] | 277 | |
| 278 | if (!list_empty(&dep->request_list)) { |
| 279 | struct dwc3_request *req; |
| 280 | |
| 281 | req = next_request(&dep->request_list); |
| 282 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 283 | } |
| 284 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 285 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 286 | dwc3_ep0_out_start(dwc); |
| 287 | } |
| 288 | |
Pratyush Anand | eb84075 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 289 | int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
| 290 | { |
| 291 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 292 | struct dwc3 *dwc = dep->dwc; |
| 293 | |
| 294 | dwc3_ep0_stall_and_restart(dwc); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 299 | void dwc3_ep0_out_start(struct dwc3 *dwc) |
| 300 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 301 | int ret; |
| 302 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 303 | ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, |
| 304 | DWC3_TRBCTL_CONTROL_SETUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 305 | WARN_ON(ret < 0); |
| 306 | } |
| 307 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 308 | static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) |
| 309 | { |
| 310 | struct dwc3_ep *dep; |
| 311 | u32 windex = le16_to_cpu(wIndex_le); |
| 312 | u32 epnum; |
| 313 | |
| 314 | epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1; |
| 315 | if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) |
| 316 | epnum |= 1; |
| 317 | |
| 318 | dep = dwc->eps[epnum]; |
| 319 | if (dep->flags & DWC3_EP_ENABLED) |
| 320 | return dep; |
| 321 | |
| 322 | return NULL; |
| 323 | } |
| 324 | |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 325 | 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] | 326 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 327 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 328 | /* |
| 329 | * ch 9.4.5 |
| 330 | */ |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 331 | static int dwc3_ep0_handle_status(struct dwc3 *dwc, |
| 332 | struct usb_ctrlrequest *ctrl) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 333 | { |
| 334 | struct dwc3_ep *dep; |
| 335 | u32 recip; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 336 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 337 | u16 usb_status = 0; |
| 338 | __le16 *response_pkt; |
| 339 | |
| 340 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 341 | switch (recip) { |
| 342 | case USB_RECIP_DEVICE: |
| 343 | /* |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 344 | * 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] | 345 | */ |
| 346 | usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 347 | |
| 348 | if (dwc->speed == DWC3_DSTS_SUPERSPEED) { |
| 349 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 350 | if (reg & DWC3_DCTL_INITU1ENA) |
| 351 | usb_status |= 1 << USB_DEV_STAT_U1_ENABLED; |
| 352 | if (reg & DWC3_DCTL_INITU2ENA) |
| 353 | usb_status |= 1 << USB_DEV_STAT_U2_ENABLED; |
| 354 | } |
| 355 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 356 | break; |
| 357 | |
| 358 | case USB_RECIP_INTERFACE: |
| 359 | /* |
| 360 | * Function Remote Wake Capable D0 |
| 361 | * Function Remote Wakeup D1 |
| 362 | */ |
| 363 | break; |
| 364 | |
| 365 | case USB_RECIP_ENDPOINT: |
| 366 | dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex); |
| 367 | if (!dep) |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 368 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 369 | |
| 370 | if (dep->flags & DWC3_EP_STALL) |
| 371 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 372 | break; |
| 373 | default: |
| 374 | return -EINVAL; |
| 375 | }; |
| 376 | |
| 377 | response_pkt = (__le16 *) dwc->setup_buf; |
| 378 | *response_pkt = cpu_to_le16(usb_status); |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 379 | |
| 380 | dep = dwc->eps[0]; |
| 381 | dwc->ep0_usb_req.dep = dep; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 382 | dwc->ep0_usb_req.request.length = sizeof(*response_pkt); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 383 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 384 | dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl; |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 385 | |
| 386 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static int dwc3_ep0_handle_feature(struct dwc3 *dwc, |
| 390 | struct usb_ctrlrequest *ctrl, int set) |
| 391 | { |
| 392 | struct dwc3_ep *dep; |
| 393 | u32 recip; |
| 394 | u32 wValue; |
| 395 | u32 wIndex; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 396 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 397 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 398 | |
| 399 | wValue = le16_to_cpu(ctrl->wValue); |
| 400 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 401 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 402 | switch (recip) { |
| 403 | case USB_RECIP_DEVICE: |
| 404 | |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 405 | switch (wValue) { |
| 406 | case USB_DEVICE_REMOTE_WAKEUP: |
| 407 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 408 | /* |
| 409 | * 9.4.1 says only only for SS, in AddressState only for |
| 410 | * default control pipe |
| 411 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 412 | case USB_DEVICE_U1_ENABLE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 413 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 414 | return -EINVAL; |
| 415 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 416 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 417 | |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 418 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 419 | if (set) |
| 420 | reg |= DWC3_DCTL_INITU1ENA; |
| 421 | else |
| 422 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 423 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 424 | break; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 425 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 426 | case USB_DEVICE_U2_ENABLE: |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 427 | if (dwc->dev_state != DWC3_CONFIGURED_STATE) |
| 428 | return -EINVAL; |
| 429 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 433 | if (set) |
| 434 | reg |= DWC3_DCTL_INITU2ENA; |
| 435 | else |
| 436 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 437 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 438 | break; |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 439 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 440 | case USB_DEVICE_LTM_ENABLE: |
Sebastian Andrzej Siewior | bb8b8a3 | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 441 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 442 | break; |
| 443 | |
| 444 | case USB_DEVICE_TEST_MODE: |
| 445 | if ((wIndex & 0xff) != 0) |
| 446 | return -EINVAL; |
| 447 | if (!set) |
| 448 | return -EINVAL; |
| 449 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 450 | dwc->test_mode_nr = wIndex >> 8; |
| 451 | dwc->test_mode = true; |
Gerard Cauvy | ecb0779 | 2012-03-16 16:20:10 +0200 | [diff] [blame] | 452 | break; |
| 453 | default: |
| 454 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 455 | } |
| 456 | break; |
| 457 | |
| 458 | case USB_RECIP_INTERFACE: |
| 459 | switch (wValue) { |
| 460 | case USB_INTRF_FUNC_SUSPEND: |
| 461 | if (wIndex & USB_INTRF_FUNC_SUSPEND_LP) |
| 462 | /* XXX enable Low power suspend */ |
| 463 | ; |
| 464 | if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) |
| 465 | /* XXX enable remote wakeup */ |
| 466 | ; |
| 467 | break; |
| 468 | default: |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | break; |
| 472 | |
| 473 | case USB_RECIP_ENDPOINT: |
| 474 | switch (wValue) { |
| 475 | case USB_ENDPOINT_HALT: |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 476 | dep = dwc3_wIndex_to_dep(dwc, wIndex); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 477 | if (!dep) |
| 478 | return -EINVAL; |
Vijayavardhan Vennapusa | 6008e26 | 2012-10-19 15:57:56 +0530 | [diff] [blame] | 479 | |
| 480 | if (!set && (dep->flags & DWC3_EP_WEDGE)) |
| 481 | return 0; |
| 482 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 483 | ret = __dwc3_gadget_ep_set_halt(dep, set); |
| 484 | if (ret) |
| 485 | return -EINVAL; |
| 486 | break; |
| 487 | default: |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | break; |
| 491 | |
| 492 | default: |
| 493 | return -EINVAL; |
| 494 | }; |
| 495 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 500 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 501 | u32 addr; |
| 502 | u32 reg; |
| 503 | |
| 504 | addr = le16_to_cpu(ctrl->wValue); |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 505 | if (addr > 127) { |
| 506 | dev_dbg(dwc->dev, "invalid device address %d\n", addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 507 | return -EINVAL; |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | if (dwc->dev_state == DWC3_CONFIGURED_STATE) { |
| 511 | dev_dbg(dwc->dev, "trying to set address when configured\n"); |
| 512 | return -EINVAL; |
| 513 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 514 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 515 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 516 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 517 | reg |= DWC3_DCFG_DEVADDR(addr); |
| 518 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 519 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 520 | if (addr) |
| 521 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 522 | else |
| 523 | dwc->dev_state = DWC3_DEFAULT_STATE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 524 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 525 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 529 | { |
| 530 | int ret; |
| 531 | |
| 532 | spin_unlock(&dwc->lock); |
| 533 | ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); |
| 534 | spin_lock(&dwc->lock); |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 539 | { |
| 540 | u32 cfg; |
| 541 | int ret; |
Pratyush Anand | 49917a4 | 2012-07-02 10:21:54 +0530 | [diff] [blame] | 542 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 543 | |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 544 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 545 | cfg = le16_to_cpu(ctrl->wValue); |
| 546 | |
| 547 | switch (dwc->dev_state) { |
| 548 | case DWC3_DEFAULT_STATE: |
| 549 | return -EINVAL; |
| 550 | break; |
| 551 | |
| 552 | case DWC3_ADDRESS_STATE: |
| 553 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 554 | /* if the cfg matches and the cfg is non zero */ |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 555 | if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 556 | dwc->dev_state = DWC3_CONFIGURED_STATE; |
Pratyush Anand | 49917a4 | 2012-07-02 10:21:54 +0530 | [diff] [blame] | 557 | /* |
| 558 | * Enable transition to U1/U2 state when |
| 559 | * nothing is pending from application. |
| 560 | */ |
| 561 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 562 | reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA); |
| 563 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 564 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 565 | dwc->resize_fifos = true; |
| 566 | dev_dbg(dwc->dev, "resize fifos flag SET\n"); |
| 567 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 568 | break; |
| 569 | |
| 570 | case DWC3_CONFIGURED_STATE: |
| 571 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 572 | if (!cfg) |
| 573 | dwc->dev_state = DWC3_ADDRESS_STATE; |
| 574 | break; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 575 | default: |
| 576 | ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 577 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 578 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 579 | } |
| 580 | |
Felipe Balbi | 9e788d6 | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 581 | static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req) |
| 582 | { |
| 583 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 584 | struct dwc3 *dwc = dep->dwc; |
| 585 | |
| 586 | u32 param = 0; |
| 587 | u32 reg; |
| 588 | |
| 589 | struct timing { |
| 590 | u8 u1sel; |
| 591 | u8 u1pel; |
| 592 | u16 u2sel; |
| 593 | u16 u2pel; |
| 594 | } __packed timing; |
| 595 | |
| 596 | int ret; |
| 597 | |
| 598 | memcpy(&timing, req->buf, sizeof(timing)); |
| 599 | |
| 600 | dwc->u1sel = timing.u1sel; |
| 601 | dwc->u1pel = timing.u1pel; |
Felipe Balbi | 8761921 | 2012-05-31 11:00:28 +0300 | [diff] [blame] | 602 | dwc->u2sel = le16_to_cpu(timing.u2sel); |
| 603 | dwc->u2pel = le16_to_cpu(timing.u2pel); |
Felipe Balbi | 9e788d6 | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 604 | |
| 605 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 606 | if (reg & DWC3_DCTL_INITU2ENA) |
| 607 | param = dwc->u2pel; |
| 608 | if (reg & DWC3_DCTL_INITU1ENA) |
| 609 | param = dwc->u1pel; |
| 610 | |
| 611 | /* |
| 612 | * According to Synopsys Databook, if parameter is |
| 613 | * greater than 125, a value of zero should be |
| 614 | * programmed in the register. |
| 615 | */ |
| 616 | if (param > 125) |
| 617 | param = 0; |
| 618 | |
| 619 | /* now that we have the time, issue DGCMD Set Sel */ |
| 620 | ret = dwc3_send_gadget_generic_command(dwc, |
| 621 | DWC3_DGCMD_SET_PERIODIC_PAR, param); |
| 622 | WARN_ON(ret < 0); |
| 623 | } |
| 624 | |
| 625 | static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 626 | { |
| 627 | struct dwc3_ep *dep; |
| 628 | u16 wLength; |
| 629 | u16 wValue; |
| 630 | |
| 631 | if (dwc->dev_state == DWC3_DEFAULT_STATE) |
| 632 | return -EINVAL; |
| 633 | |
| 634 | wValue = le16_to_cpu(ctrl->wValue); |
| 635 | wLength = le16_to_cpu(ctrl->wLength); |
| 636 | |
| 637 | if (wLength != 6) { |
| 638 | dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n", |
| 639 | wLength); |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * To handle Set SEL we need to receive 6 bytes from Host. So let's |
| 645 | * queue a usb_request for 6 bytes. |
| 646 | * |
| 647 | * Remember, though, this controller can't handle non-wMaxPacketSize |
| 648 | * aligned transfers on the OUT direction, so we queue a request for |
| 649 | * wMaxPacketSize instead. |
| 650 | */ |
| 651 | dep = dwc->eps[0]; |
| 652 | dwc->ep0_usb_req.dep = dep; |
| 653 | dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket; |
| 654 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
| 655 | dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl; |
| 656 | |
| 657 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
| 658 | } |
| 659 | |
Felipe Balbi | 395c349 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 660 | static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 661 | { |
| 662 | u16 wLength; |
| 663 | u16 wValue; |
| 664 | u16 wIndex; |
| 665 | |
| 666 | wValue = le16_to_cpu(ctrl->wValue); |
| 667 | wLength = le16_to_cpu(ctrl->wLength); |
| 668 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 669 | |
| 670 | if (wIndex || wLength) |
| 671 | return -EINVAL; |
| 672 | |
| 673 | /* |
| 674 | * REVISIT It's unclear from Databook what to do with this |
| 675 | * value. For now, just cache it. |
| 676 | */ |
| 677 | dwc->isoch_delay = wValue; |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 682 | static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 683 | { |
| 684 | int ret; |
| 685 | |
| 686 | switch (ctrl->bRequest) { |
| 687 | case USB_REQ_GET_STATUS: |
| 688 | dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n"); |
| 689 | ret = dwc3_ep0_handle_status(dwc, ctrl); |
| 690 | break; |
| 691 | case USB_REQ_CLEAR_FEATURE: |
| 692 | dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n"); |
| 693 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 0); |
| 694 | break; |
| 695 | case USB_REQ_SET_FEATURE: |
| 696 | dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n"); |
| 697 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 1); |
| 698 | break; |
| 699 | case USB_REQ_SET_ADDRESS: |
| 700 | dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n"); |
| 701 | ret = dwc3_ep0_set_address(dwc, ctrl); |
| 702 | break; |
| 703 | case USB_REQ_SET_CONFIGURATION: |
| 704 | dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n"); |
| 705 | ret = dwc3_ep0_set_config(dwc, ctrl); |
| 706 | break; |
Felipe Balbi | 9e788d6 | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 707 | case USB_REQ_SET_SEL: |
| 708 | dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n"); |
| 709 | ret = dwc3_ep0_set_sel(dwc, ctrl); |
| 710 | break; |
Felipe Balbi | 395c349 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 711 | case USB_REQ_SET_ISOCH_DELAY: |
| 712 | dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n"); |
| 713 | ret = dwc3_ep0_set_isoch_delay(dwc, ctrl); |
| 714 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 715 | default: |
| 716 | dev_vdbg(dwc->dev, "Forwarding to gadget driver\n"); |
| 717 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 718 | break; |
| 719 | }; |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | static void dwc3_ep0_inspect_setup(struct dwc3 *dwc, |
| 725 | const struct dwc3_event_depevt *event) |
| 726 | { |
| 727 | struct usb_ctrlrequest *ctrl = dwc->ctrl_req; |
Felipe Balbi | 65811b6 | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 728 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 729 | u32 len; |
| 730 | |
| 731 | if (!dwc->gadget_driver) |
Felipe Balbi | 65811b6 | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 732 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 733 | |
| 734 | len = le16_to_cpu(ctrl->wLength); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 735 | if (!len) { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 736 | dwc->three_stage_setup = false; |
| 737 | dwc->ep0_expect_in = false; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 738 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 739 | } else { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 740 | dwc->three_stage_setup = true; |
| 741 | dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 742 | dwc->ep0_next_event = DWC3_EP0_NRDY_DATA; |
| 743 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 744 | |
Vijayavardhan Vennapusa | ffeb26b | 2013-02-14 16:33:30 +0530 | [diff] [blame] | 745 | dbg_setup(0x00, ctrl); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 746 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
| 747 | ret = dwc3_ep0_std_request(dwc, ctrl); |
| 748 | else |
| 749 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 750 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 751 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 752 | dwc->delayed_status = true; |
| 753 | |
Felipe Balbi | 65811b6 | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 754 | out: |
| 755 | if (ret < 0) |
| 756 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 757 | } |
| 758 | |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 759 | bool zlp_required; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 760 | static void dwc3_ep0_complete_data(struct dwc3 *dwc, |
| 761 | const struct dwc3_event_depevt *event) |
| 762 | { |
| 763 | struct dwc3_request *r = NULL; |
| 764 | struct usb_request *ur; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 765 | struct dwc3_trb *trb; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 766 | struct dwc3_ep *ep0; |
Felipe Balbi | c611ccb | 2011-08-27 02:30:33 +0300 | [diff] [blame] | 767 | u32 transferred; |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 768 | u32 status; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 769 | u32 length; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 770 | u8 epnum; |
| 771 | |
| 772 | epnum = event->endpoint_number; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 773 | ep0 = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 774 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 775 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 776 | |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 777 | r = next_request(&ep0->request_list); |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 778 | ur = &r->request; |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 779 | if ((epnum & 1) && ur->zero && |
| 780 | (ur->length % ep0->endpoint.maxpacket == 0)) { |
| 781 | zlp_required = true; |
| 782 | ur->zero = false; |
| 783 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 784 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 785 | trb = dwc->ep0_trb; |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 786 | |
| 787 | status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 788 | if (status == DWC3_TRBSTS_SETUP_PENDING) { |
| 789 | dev_dbg(dwc->dev, "Setup Pending received\n"); |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 790 | zlp_required = false; |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 791 | |
| 792 | if (r) |
| 793 | dwc3_gadget_giveback(ep0, r, -ECONNRESET); |
| 794 | |
| 795 | return; |
| 796 | } |
| 797 | |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 798 | if (zlp_required) |
| 799 | return; |
| 800 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 801 | length = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 802 | |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 803 | if (dwc->ep0_bounced) { |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 804 | unsigned transfer_size = ur->length; |
| 805 | unsigned maxp = ep0->endpoint.maxpacket; |
| 806 | |
| 807 | transfer_size += (maxp - (transfer_size % maxp)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 808 | transferred = min_t(u32, ur->length, |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 809 | transfer_size - length); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 810 | memcpy(ur->buf, dwc->ep0_bounce, transferred); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 811 | } else { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 812 | transferred = ur->length - length; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 813 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 814 | |
Felipe Balbi | cd423dd | 2012-03-21 11:44:00 +0200 | [diff] [blame] | 815 | ur->actual += transferred; |
| 816 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 817 | if ((epnum & 1) && ur->actual < ur->length) { |
| 818 | /* for some reason we did not get everything out */ |
| 819 | |
| 820 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 821 | } else { |
| 822 | /* |
| 823 | * handle the case where we have to send a zero packet. This |
| 824 | * seems to be case when req.length > maxpacket. Could it be? |
| 825 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 826 | if (r) |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 827 | dwc3_gadget_giveback(ep0, r, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Felipe Balbi | ed8c398 | 2012-05-31 12:32:37 +0300 | [diff] [blame] | 831 | static void dwc3_ep0_complete_status(struct dwc3 *dwc, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 832 | const struct dwc3_event_depevt *event) |
| 833 | { |
| 834 | struct dwc3_request *r; |
| 835 | struct dwc3_ep *dep; |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 836 | struct dwc3_trb *trb; |
| 837 | u32 status; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 838 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 839 | dep = dwc->eps[0]; |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 840 | trb = dwc->ep0_trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 841 | |
| 842 | if (!list_empty(&dep->request_list)) { |
| 843 | r = next_request(&dep->request_list); |
| 844 | |
| 845 | dwc3_gadget_giveback(dep, r, 0); |
| 846 | } |
| 847 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 848 | if (dwc->test_mode) { |
| 849 | int ret; |
| 850 | |
| 851 | ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr); |
| 852 | if (ret < 0) { |
| 853 | dev_dbg(dwc->dev, "Invalid Test #%d\n", |
| 854 | dwc->test_mode_nr); |
| 855 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 0ed27b1 | 2012-06-25 19:30:49 +0300 | [diff] [blame] | 856 | return; |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Felipe Balbi | b6c4acd | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 860 | status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 861 | if (status == DWC3_TRBSTS_SETUP_PENDING) |
| 862 | dev_dbg(dwc->dev, "Setup Pending received\n"); |
| 863 | |
Vijayavardhan Vennapusa | ffeb26b | 2013-02-14 16:33:30 +0530 | [diff] [blame] | 864 | dbg_print(dep->number, "DONE", status, "STATUS"); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 865 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 866 | dwc3_ep0_out_start(dwc); |
| 867 | } |
| 868 | |
| 869 | static void dwc3_ep0_xfer_complete(struct dwc3 *dwc, |
| 870 | const struct dwc3_event_depevt *event) |
| 871 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 872 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 873 | |
| 874 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | 4959cfc | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 875 | dep->resource_index = 0; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 876 | dwc->setup_packet_pending = false; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 877 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 878 | switch (dwc->ep0state) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 879 | case EP0_SETUP_PHASE: |
| 880 | dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 881 | dwc3_ep0_inspect_setup(dwc, event); |
| 882 | break; |
| 883 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 884 | case EP0_DATA_PHASE: |
| 885 | dev_vdbg(dwc->dev, "Data Phase\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 886 | dwc3_ep0_complete_data(dwc, event); |
| 887 | break; |
| 888 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 889 | case EP0_STATUS_PHASE: |
| 890 | dev_vdbg(dwc->dev, "Status Phase\n"); |
Felipe Balbi | ed8c398 | 2012-05-31 12:32:37 +0300 | [diff] [blame] | 891 | dwc3_ep0_complete_status(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 892 | break; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 893 | default: |
| 894 | WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 898 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 899 | struct dwc3_ep *dep, struct dwc3_request *req) |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 900 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 901 | int ret; |
| 902 | |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 903 | req->direction = !!dep->number; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 904 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 905 | if (req->request.length == 0) { |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 906 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 907 | dwc->ctrl_req_addr, 0, |
| 908 | DWC3_TRBCTL_CONTROL_DATA); |
Felipe Balbi | 0a1730a | 2012-05-04 13:08:22 +0300 | [diff] [blame] | 909 | } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 910 | && (dep->number == 0)) { |
| 911 | u32 transfer_size; |
| 912 | |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 913 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 914 | dep->number); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 915 | if (ret) { |
| 916 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 917 | return; |
| 918 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 919 | |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 920 | WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE); |
| 921 | |
| 922 | transfer_size = roundup(req->request.length, |
| 923 | (u32) dep->endpoint.maxpacket); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 924 | |
| 925 | dwc->ep0_bounced = true; |
| 926 | |
| 927 | /* |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 928 | * REVISIT in case request length is bigger than |
| 929 | * DWC3_EP0_BOUNCE_SIZE we will need two chained |
| 930 | * TRBs to handle the transfer. |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 931 | */ |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 932 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
| 933 | dwc->ep0_bounce_addr, transfer_size, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 934 | DWC3_TRBCTL_CONTROL_DATA); |
| 935 | } else { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 936 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 937 | dep->number); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 938 | if (ret) { |
| 939 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 940 | return; |
| 941 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 942 | |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 943 | if (dep->number && |
| 944 | !(req->request.length % dwc->gadget.ep0->maxpacket)) |
| 945 | req->request.zero = true; |
| 946 | |
Felipe Balbi | db0af40 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 947 | ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma, |
| 948 | req->request.length, DWC3_TRBCTL_CONTROL_DATA); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 949 | } |
| 950 | |
Vijayavardhan Vennapusa | ffeb26b | 2013-02-14 16:33:30 +0530 | [diff] [blame] | 951 | dbg_queue(dep->number, &req->request, ret); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 952 | WARN_ON(ret < 0); |
| 953 | } |
| 954 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 955 | static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 956 | { |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 957 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 958 | u32 type; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 959 | |
| 960 | type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3 |
| 961 | : DWC3_TRBCTL_CONTROL_STATUS2; |
| 962 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 963 | return dwc3_ep0_start_trans(dwc, dep->number, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 964 | dwc->ctrl_req_addr, 0, type); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 965 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 966 | |
Felipe Balbi | 9610cd2 | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 967 | static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep) |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 968 | { |
Vijayavardhan Vennapusa | ffeb26b | 2013-02-14 16:33:30 +0530 | [diff] [blame] | 969 | int ret; |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 970 | if (dwc->resize_fifos) { |
| 971 | dev_dbg(dwc->dev, "starting to resize fifos\n"); |
| 972 | dwc3_gadget_resize_tx_fifos(dwc); |
| 973 | dwc->resize_fifos = 0; |
| 974 | } |
| 975 | |
Vijayavardhan Vennapusa | ffeb26b | 2013-02-14 16:33:30 +0530 | [diff] [blame] | 976 | ret = dwc3_ep0_start_control_status(dep); |
| 977 | dbg_print(dep->number, "QUEUE", ret, "STATUS"); |
| 978 | WARN_ON(ret); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 979 | } |
| 980 | |
Felipe Balbi | 9610cd2 | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 981 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, |
| 982 | const struct dwc3_event_depevt *event) |
| 983 | { |
| 984 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 985 | |
| 986 | __dwc3_ep0_do_control_status(dwc, dep); |
| 987 | } |
| 988 | |
Felipe Balbi | a7e8d65 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 989 | static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 990 | { |
| 991 | struct dwc3_gadget_ep_cmd_params params; |
| 992 | u32 cmd; |
| 993 | int ret; |
| 994 | |
| 995 | if (!dep->resource_index) |
| 996 | return; |
| 997 | |
| 998 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
| 999 | cmd |= DWC3_DEPCMD_CMDIOC; |
| 1000 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
| 1001 | memset(¶ms, 0, sizeof(params)); |
| 1002 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 1003 | WARN_ON_ONCE(ret); |
| 1004 | dep->resource_index = 0; |
| 1005 | } |
| 1006 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1007 | static void dwc3_ep0_xfernotready(struct dwc3 *dwc, |
| 1008 | const struct dwc3_event_depevt *event) |
| 1009 | { |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 1010 | u8 epnum; |
| 1011 | int ret; |
| 1012 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 1013 | dwc->setup_packet_pending = true; |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 1014 | epnum = event->endpoint_number; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 1015 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1016 | switch (event->status) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1017 | case DEPEVT_STATUS_CONTROL_DATA: |
| 1018 | dev_vdbg(dwc->dev, "Control Data\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1019 | |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1020 | /* |
Felipe Balbi | a7e8d65 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1021 | * We already have a DATA transfer in the controller's cache, |
| 1022 | * if we receive a XferNotReady(DATA) we will ignore it, unless |
| 1023 | * it's for the wrong direction. |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1024 | * |
Felipe Balbi | a7e8d65 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1025 | * In that case, we must issue END_TRANSFER command to the Data |
| 1026 | * Phase we already have started and issue SetStall on the |
| 1027 | * control endpoint. |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1028 | */ |
| 1029 | if (dwc->ep0_expect_in != event->endpoint_number) { |
Felipe Balbi | a7e8d65 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1030 | struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in]; |
| 1031 | |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1032 | dev_vdbg(dwc->dev, "Wrong direction for Data phase\n"); |
Felipe Balbi | a7e8d65 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1033 | dwc3_ep0_end_control_data(dwc, dep); |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1034 | dwc3_ep0_stall_and_restart(dwc); |
| 1035 | return; |
| 1036 | } |
| 1037 | |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 1038 | if (zlp_required) { |
| 1039 | zlp_required = false; |
| 1040 | ret = dwc3_ep0_start_trans(dwc, epnum, |
| 1041 | dwc->ctrl_req_addr, 0, |
| 1042 | DWC3_TRBCTL_CONTROL_DATA); |
| 1043 | dbg_event(epnum, "ZLP", ret); |
| 1044 | WARN_ON(ret < 0); |
| 1045 | } |
| 1046 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1047 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1048 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1049 | case DEPEVT_STATUS_CONTROL_STATUS: |
Felipe Balbi | cb84c5e | 2012-07-23 09:09:32 +0300 | [diff] [blame] | 1050 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) |
| 1051 | return; |
| 1052 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1053 | dev_vdbg(dwc->dev, "Control Status\n"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1054 | |
Vijayavardhan Vennapusa | 3d07bce | 2013-03-27 15:16:27 +0530 | [diff] [blame] | 1055 | zlp_required = false; |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 1056 | dwc->ep0state = EP0_STATUS_PHASE; |
| 1057 | |
Vijayavardhan Vennapusa | b8964f8 | 2012-10-10 16:50:35 +0530 | [diff] [blame] | 1058 | if (dwc->delayed_status && |
| 1059 | list_empty(&dwc->eps[0]->request_list)) { |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 1060 | WARN_ON_ONCE(event->endpoint_number != 1); |
| 1061 | dev_vdbg(dwc->dev, "Mass Storage delayed status\n"); |
| 1062 | return; |
| 1063 | } |
Vijayavardhan Vennapusa | b8964f8 | 2012-10-10 16:50:35 +0530 | [diff] [blame] | 1064 | dwc->delayed_status = false; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 1065 | |
Felipe Balbi | 9610cd2 | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 1066 | dwc3_ep0_do_control_status(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | void dwc3_ep0_interrupt(struct dwc3 *dwc, |
Felipe Balbi | 8becf27 | 2011-11-04 12:40:05 +0200 | [diff] [blame] | 1071 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1072 | { |
| 1073 | u8 epnum = event->endpoint_number; |
| 1074 | |
| 1075 | dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n", |
| 1076 | dwc3_ep_event_string(event->endpoint_event), |
Sebastian Andrzej Siewior | b147f35 | 2011-09-30 10:58:40 +0300 | [diff] [blame] | 1077 | epnum >> 1, (epnum & 1) ? "in" : "out", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1078 | dwc3_ep0_state_string(dwc->ep0state)); |
| 1079 | |
| 1080 | switch (event->endpoint_event) { |
| 1081 | case DWC3_DEPEVT_XFERCOMPLETE: |
| 1082 | dwc3_ep0_xfer_complete(dwc, event); |
| 1083 | break; |
| 1084 | |
| 1085 | case DWC3_DEPEVT_XFERNOTREADY: |
| 1086 | dwc3_ep0_xfernotready(dwc, event); |
| 1087 | break; |
| 1088 | |
| 1089 | case DWC3_DEPEVT_XFERINPROGRESS: |
| 1090 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 1091 | case DWC3_DEPEVT_STREAMEVT: |
| 1092 | case DWC3_DEPEVT_EPCMDCMPLT: |
| 1093 | break; |
| 1094 | } |
| 1095 | } |