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