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