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]; |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 259 | __dwc3_gadget_ep_set_halt(dep, 1, false); |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 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 | |
Felipe Balbi | 33fb691 | 2014-09-24 10:46:46 -0500 | [diff] [blame] | 274 | int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 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 | 33fb691 | 2014-09-24 10:46:46 -0500 | [diff] [blame] | 284 | int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
| 285 | { |
| 286 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 287 | struct dwc3 *dwc = dep->dwc; |
| 288 | unsigned long flags; |
| 289 | int ret; |
| 290 | |
| 291 | spin_lock_irqsave(&dwc->lock, flags); |
| 292 | ret = __dwc3_gadget_ep0_set_halt(ep, value); |
| 293 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 298 | void dwc3_ep0_out_start(struct dwc3 *dwc) |
| 299 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 300 | int ret; |
| 301 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 302 | ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, |
| 303 | DWC3_TRBCTL_CONTROL_SETUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 304 | WARN_ON(ret < 0); |
| 305 | } |
| 306 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 307 | static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) |
| 308 | { |
| 309 | struct dwc3_ep *dep; |
| 310 | u32 windex = le16_to_cpu(wIndex_le); |
| 311 | u32 epnum; |
| 312 | |
| 313 | epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1; |
| 314 | if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) |
| 315 | epnum |= 1; |
| 316 | |
| 317 | dep = dwc->eps[epnum]; |
| 318 | if (dep->flags & DWC3_EP_ENABLED) |
| 319 | return dep; |
| 320 | |
| 321 | return NULL; |
| 322 | } |
| 323 | |
Sebastian Andrzej Siewior | 8ee6270 | 2011-10-18 19:13:29 +0200 | [diff] [blame] | 324 | 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] | 325 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 326 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 327 | /* |
| 328 | * ch 9.4.5 |
| 329 | */ |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 330 | static int dwc3_ep0_handle_status(struct dwc3 *dwc, |
| 331 | struct usb_ctrlrequest *ctrl) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 332 | { |
| 333 | struct dwc3_ep *dep; |
| 334 | u32 recip; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 335 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 336 | u16 usb_status = 0; |
| 337 | __le16 *response_pkt; |
| 338 | |
| 339 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 340 | switch (recip) { |
| 341 | case USB_RECIP_DEVICE: |
| 342 | /* |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 343 | * 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] | 344 | */ |
| 345 | usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 346 | |
| 347 | if (dwc->speed == DWC3_DSTS_SUPERSPEED) { |
| 348 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 349 | if (reg & DWC3_DCTL_INITU1ENA) |
| 350 | usb_status |= 1 << USB_DEV_STAT_U1_ENABLED; |
| 351 | if (reg & DWC3_DCTL_INITU2ENA) |
| 352 | usb_status |= 1 << USB_DEV_STAT_U2_ENABLED; |
| 353 | } |
| 354 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 355 | break; |
| 356 | |
| 357 | case USB_RECIP_INTERFACE: |
| 358 | /* |
| 359 | * Function Remote Wake Capable D0 |
| 360 | * Function Remote Wakeup D1 |
| 361 | */ |
| 362 | break; |
| 363 | |
| 364 | case USB_RECIP_ENDPOINT: |
| 365 | dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex); |
| 366 | if (!dep) |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 367 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 368 | |
| 369 | if (dep->flags & DWC3_EP_STALL) |
| 370 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 371 | break; |
| 372 | default: |
| 373 | return -EINVAL; |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 374 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 375 | |
| 376 | response_pkt = (__le16 *) dwc->setup_buf; |
| 377 | *response_pkt = cpu_to_le16(usb_status); |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 378 | |
| 379 | dep = dwc->eps[0]; |
| 380 | dwc->ep0_usb_req.dep = dep; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 381 | dwc->ep0_usb_req.request.length = sizeof(*response_pkt); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 382 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
Sebastian Andrzej Siewior | e0ce0b0 | 2011-11-25 12:03:46 +0100 | [diff] [blame] | 383 | dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl; |
Felipe Balbi | e261779 | 2011-11-29 10:35:47 +0200 | [diff] [blame] | 384 | |
| 385 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int dwc3_ep0_handle_feature(struct dwc3 *dwc, |
| 389 | struct usb_ctrlrequest *ctrl, int set) |
| 390 | { |
| 391 | struct dwc3_ep *dep; |
| 392 | u32 recip; |
| 393 | u32 wValue; |
| 394 | u32 wIndex; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 395 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 396 | int ret; |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 397 | enum usb_device_state state; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 398 | |
| 399 | wValue = le16_to_cpu(ctrl->wValue); |
| 400 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 401 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 402 | state = dwc->gadget.state; |
| 403 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 404 | switch (recip) { |
| 405 | case USB_RECIP_DEVICE: |
| 406 | |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 407 | switch (wValue) { |
| 408 | case USB_DEVICE_REMOTE_WAKEUP: |
| 409 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 410 | /* |
| 411 | * 9.4.1 says only only for SS, in AddressState only for |
| 412 | * default control pipe |
| 413 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 414 | case USB_DEVICE_U1_ENABLE: |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 415 | if (state != USB_STATE_CONFIGURED) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 416 | return -EINVAL; |
| 417 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 418 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 419 | |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 420 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 421 | if (set) |
| 422 | reg |= DWC3_DCTL_INITU1ENA; |
| 423 | else |
| 424 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 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_U2_ENABLE: |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 429 | if (state != USB_STATE_CONFIGURED) |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 430 | return -EINVAL; |
| 431 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 435 | if (set) |
| 436 | reg |= DWC3_DCTL_INITU2ENA; |
| 437 | else |
| 438 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 439 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 440 | break; |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 441 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 442 | case USB_DEVICE_LTM_ENABLE: |
Sebastian Andrzej Siewior | e6a3b5e | 2011-09-13 17:54:39 +0200 | [diff] [blame] | 443 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 444 | break; |
| 445 | |
| 446 | case USB_DEVICE_TEST_MODE: |
| 447 | if ((wIndex & 0xff) != 0) |
| 448 | return -EINVAL; |
| 449 | if (!set) |
| 450 | return -EINVAL; |
| 451 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 452 | dwc->test_mode_nr = wIndex >> 8; |
| 453 | dwc->test_mode = true; |
Gerard Cauvy | ecb0779 | 2012-03-16 16:20:10 +0200 | [diff] [blame] | 454 | break; |
| 455 | default: |
| 456 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 457 | } |
| 458 | break; |
| 459 | |
| 460 | case USB_RECIP_INTERFACE: |
| 461 | switch (wValue) { |
| 462 | case USB_INTRF_FUNC_SUSPEND: |
| 463 | if (wIndex & USB_INTRF_FUNC_SUSPEND_LP) |
| 464 | /* XXX enable Low power suspend */ |
| 465 | ; |
| 466 | if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) |
| 467 | /* XXX enable remote wakeup */ |
| 468 | ; |
| 469 | break; |
| 470 | default: |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | break; |
| 474 | |
| 475 | case USB_RECIP_ENDPOINT: |
| 476 | switch (wValue) { |
| 477 | case USB_ENDPOINT_HALT: |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 478 | dep = dwc3_wIndex_to_dep(dwc, wIndex); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 479 | if (!dep) |
| 480 | return -EINVAL; |
Alan Stern | a535d81 | 2013-11-01 12:05:12 -0400 | [diff] [blame] | 481 | if (set == 0 && (dep->flags & DWC3_EP_WEDGE)) |
| 482 | break; |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 483 | ret = __dwc3_gadget_ep_set_halt(dep, set, true); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 484 | if (ret) |
| 485 | return -EINVAL; |
| 486 | break; |
| 487 | default: |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | break; |
| 491 | |
| 492 | default: |
| 493 | return -EINVAL; |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 494 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 495 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 500 | { |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 501 | enum usb_device_state state = dwc->gadget.state; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 502 | u32 addr; |
| 503 | u32 reg; |
| 504 | |
| 505 | addr = le16_to_cpu(ctrl->wValue); |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 506 | if (addr > 127) { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 507 | dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 508 | return -EINVAL; |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 509 | } |
| 510 | |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 511 | if (state == USB_STATE_CONFIGURED) { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 512 | dwc3_trace(trace_dwc3_ep0, |
| 513 | "trying to set address when configured"); |
Felipe Balbi | f96a6ec | 2011-10-15 21:37:35 +0300 | [diff] [blame] | 514 | return -EINVAL; |
| 515 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 516 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 517 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 518 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 519 | reg |= DWC3_DCFG_DEVADDR(addr); |
| 520 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 521 | |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 522 | if (addr) |
Felipe Balbi | 14cd592 | 2011-12-19 13:01:52 +0200 | [diff] [blame] | 523 | usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS); |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 524 | else |
Felipe Balbi | 14cd592 | 2011-12-19 13:01:52 +0200 | [diff] [blame] | 525 | usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 526 | |
Felipe Balbi | 2646021 | 2011-09-30 10:58:36 +0300 | [diff] [blame] | 527 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 531 | { |
| 532 | int ret; |
| 533 | |
| 534 | spin_unlock(&dwc->lock); |
| 535 | ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); |
| 536 | spin_lock(&dwc->lock); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 541 | { |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 542 | enum usb_device_state state = dwc->gadget.state; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 543 | u32 cfg; |
| 544 | int ret; |
Pratyush Anand | e274a31 | 2012-07-02 10:21:54 +0530 | [diff] [blame] | 545 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 546 | |
Paul Zimmerman | b23c843 | 2011-09-30 10:58:42 +0300 | [diff] [blame] | 547 | dwc->start_config_issued = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 548 | cfg = le16_to_cpu(ctrl->wValue); |
| 549 | |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 550 | switch (state) { |
| 551 | case USB_STATE_DEFAULT: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 552 | return -EINVAL; |
| 553 | break; |
| 554 | |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 555 | case USB_STATE_ADDRESS: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 556 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 557 | /* if the cfg matches and the cfg is non zero */ |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 558 | if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { |
Felipe Balbi | 7c81290 | 2013-07-22 12:41:47 +0300 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * only change state if set_config has already |
| 562 | * been processed. If gadget driver returns |
| 563 | * USB_GADGET_DELAYED_STATUS, we will wait |
| 564 | * to change the state on the next usb_ep_queue() |
| 565 | */ |
| 566 | if (ret == 0) |
| 567 | usb_gadget_set_state(&dwc->gadget, |
| 568 | USB_STATE_CONFIGURED); |
Felipe Balbi | 14cd592 | 2011-12-19 13:01:52 +0200 | [diff] [blame] | 569 | |
Pratyush Anand | e274a31 | 2012-07-02 10:21:54 +0530 | [diff] [blame] | 570 | /* |
| 571 | * Enable transition to U1/U2 state when |
| 572 | * nothing is pending from application. |
| 573 | */ |
| 574 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 575 | reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA); |
| 576 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 577 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 578 | dwc->resize_fifos = true; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 579 | dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET"); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 580 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 581 | break; |
| 582 | |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 583 | case USB_STATE_CONFIGURED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 584 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
Felipe Balbi | 7a42d83 | 2013-07-22 12:31:31 +0300 | [diff] [blame] | 585 | if (!cfg && !ret) |
Felipe Balbi | 14cd592 | 2011-12-19 13:01:52 +0200 | [diff] [blame] | 586 | usb_gadget_set_state(&dwc->gadget, |
| 587 | USB_STATE_ADDRESS); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 588 | break; |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 589 | default: |
| 590 | ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 591 | } |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 592 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 593 | } |
| 594 | |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 595 | static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req) |
| 596 | { |
| 597 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 598 | struct dwc3 *dwc = dep->dwc; |
| 599 | |
| 600 | u32 param = 0; |
| 601 | u32 reg; |
| 602 | |
| 603 | struct timing { |
| 604 | u8 u1sel; |
| 605 | u8 u1pel; |
| 606 | u16 u2sel; |
| 607 | u16 u2pel; |
| 608 | } __packed timing; |
| 609 | |
| 610 | int ret; |
| 611 | |
| 612 | memcpy(&timing, req->buf, sizeof(timing)); |
| 613 | |
| 614 | dwc->u1sel = timing.u1sel; |
| 615 | dwc->u1pel = timing.u1pel; |
Felipe Balbi | c8cf7af | 2012-05-31 11:00:28 +0300 | [diff] [blame] | 616 | dwc->u2sel = le16_to_cpu(timing.u2sel); |
| 617 | dwc->u2pel = le16_to_cpu(timing.u2pel); |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 618 | |
| 619 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 620 | if (reg & DWC3_DCTL_INITU2ENA) |
| 621 | param = dwc->u2pel; |
| 622 | if (reg & DWC3_DCTL_INITU1ENA) |
| 623 | param = dwc->u1pel; |
| 624 | |
| 625 | /* |
| 626 | * According to Synopsys Databook, if parameter is |
| 627 | * greater than 125, a value of zero should be |
| 628 | * programmed in the register. |
| 629 | */ |
| 630 | if (param > 125) |
| 631 | param = 0; |
| 632 | |
| 633 | /* now that we have the time, issue DGCMD Set Sel */ |
| 634 | ret = dwc3_send_gadget_generic_command(dwc, |
| 635 | DWC3_DGCMD_SET_PERIODIC_PAR, param); |
| 636 | WARN_ON(ret < 0); |
| 637 | } |
| 638 | |
| 639 | static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 640 | { |
| 641 | struct dwc3_ep *dep; |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 642 | enum usb_device_state state = dwc->gadget.state; |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 643 | u16 wLength; |
| 644 | u16 wValue; |
| 645 | |
Felipe Balbi | fdba5aa | 2013-01-25 11:28:19 +0200 | [diff] [blame] | 646 | if (state == USB_STATE_DEFAULT) |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 647 | return -EINVAL; |
| 648 | |
| 649 | wValue = le16_to_cpu(ctrl->wValue); |
| 650 | wLength = le16_to_cpu(ctrl->wLength); |
| 651 | |
| 652 | if (wLength != 6) { |
| 653 | dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n", |
| 654 | wLength); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * To handle Set SEL we need to receive 6 bytes from Host. So let's |
| 660 | * queue a usb_request for 6 bytes. |
| 661 | * |
| 662 | * Remember, though, this controller can't handle non-wMaxPacketSize |
| 663 | * aligned transfers on the OUT direction, so we queue a request for |
| 664 | * wMaxPacketSize instead. |
| 665 | */ |
| 666 | dep = dwc->eps[0]; |
| 667 | dwc->ep0_usb_req.dep = dep; |
| 668 | dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket; |
| 669 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
| 670 | dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl; |
| 671 | |
| 672 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); |
| 673 | } |
| 674 | |
Felipe Balbi | c12a0d8 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 675 | static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 676 | { |
| 677 | u16 wLength; |
| 678 | u16 wValue; |
| 679 | u16 wIndex; |
| 680 | |
| 681 | wValue = le16_to_cpu(ctrl->wValue); |
| 682 | wLength = le16_to_cpu(ctrl->wLength); |
| 683 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 684 | |
| 685 | if (wIndex || wLength) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | /* |
| 689 | * REVISIT It's unclear from Databook what to do with this |
| 690 | * value. For now, just cache it. |
| 691 | */ |
| 692 | dwc->isoch_delay = wValue; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 697 | static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
| 698 | { |
| 699 | int ret; |
| 700 | |
| 701 | switch (ctrl->bRequest) { |
| 702 | case USB_REQ_GET_STATUS: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 703 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 704 | ret = dwc3_ep0_handle_status(dwc, ctrl); |
| 705 | break; |
| 706 | case USB_REQ_CLEAR_FEATURE: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 707 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 708 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 0); |
| 709 | break; |
| 710 | case USB_REQ_SET_FEATURE: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 711 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 712 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 1); |
| 713 | break; |
| 714 | case USB_REQ_SET_ADDRESS: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 715 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 716 | ret = dwc3_ep0_set_address(dwc, ctrl); |
| 717 | break; |
| 718 | case USB_REQ_SET_CONFIGURATION: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 719 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 720 | ret = dwc3_ep0_set_config(dwc, ctrl); |
| 721 | break; |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 722 | case USB_REQ_SET_SEL: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 723 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL\n"); |
Felipe Balbi | 865e09e | 2012-04-24 16:19:49 +0300 | [diff] [blame] | 724 | ret = dwc3_ep0_set_sel(dwc, ctrl); |
| 725 | break; |
Felipe Balbi | c12a0d8 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 726 | case USB_REQ_SET_ISOCH_DELAY: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 727 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY\n"); |
Felipe Balbi | c12a0d8 | 2012-04-25 10:45:05 +0300 | [diff] [blame] | 728 | ret = dwc3_ep0_set_isoch_delay(dwc, ctrl); |
| 729 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 730 | default: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 731 | dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 732 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 733 | break; |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 734 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | static void dwc3_ep0_inspect_setup(struct dwc3 *dwc, |
| 740 | const struct dwc3_event_depevt *event) |
| 741 | { |
| 742 | struct usb_ctrlrequest *ctrl = dwc->ctrl_req; |
Felipe Balbi | ef21ede | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 743 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 744 | u32 len; |
| 745 | |
| 746 | if (!dwc->gadget_driver) |
Felipe Balbi | ef21ede | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 747 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 748 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 749 | trace_dwc3_ctrl_req(ctrl); |
| 750 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 751 | len = le16_to_cpu(ctrl->wLength); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 752 | if (!len) { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 753 | dwc->three_stage_setup = false; |
| 754 | dwc->ep0_expect_in = false; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 755 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 756 | } else { |
Felipe Balbi | d95b09b | 2011-09-30 10:58:37 +0300 | [diff] [blame] | 757 | dwc->three_stage_setup = true; |
| 758 | dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 759 | dwc->ep0_next_event = DWC3_EP0_NRDY_DATA; |
| 760 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 761 | |
| 762 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
| 763 | ret = dwc3_ep0_std_request(dwc, ctrl); |
| 764 | else |
| 765 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
| 766 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 767 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 768 | dwc->delayed_status = true; |
| 769 | |
Felipe Balbi | ef21ede | 2012-05-31 10:29:49 +0300 | [diff] [blame] | 770 | out: |
| 771 | if (ret < 0) |
| 772 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static void dwc3_ep0_complete_data(struct dwc3 *dwc, |
| 776 | const struct dwc3_event_depevt *event) |
| 777 | { |
| 778 | struct dwc3_request *r = NULL; |
| 779 | struct usb_request *ur; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 780 | struct dwc3_trb *trb; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 781 | struct dwc3_ep *ep0; |
Felipe Balbi | c611ccb | 2011-08-27 02:30:33 +0300 | [diff] [blame] | 782 | u32 transferred; |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 783 | u32 status; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 784 | u32 length; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 785 | u8 epnum; |
| 786 | |
| 787 | epnum = event->endpoint_number; |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 788 | ep0 = dwc->eps[0]; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 789 | |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 790 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
| 791 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 792 | trb = dwc->ep0_trb; |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 793 | |
| 794 | status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 795 | if (status == DWC3_TRBSTS_SETUP_PENDING) { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 796 | dwc3_trace(trace_dwc3_ep0, "Setup Pending received"); |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 797 | |
| 798 | if (r) |
| 799 | dwc3_gadget_giveback(ep0, r, -ECONNRESET); |
| 800 | |
| 801 | return; |
| 802 | } |
| 803 | |
Felipe Balbi | 6856d30 | 2014-09-30 11:43:20 -0500 | [diff] [blame^] | 804 | r = next_request(&ep0->request_list); |
| 805 | if (!r) |
| 806 | return; |
| 807 | |
| 808 | ur = &r->request; |
| 809 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 810 | length = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 811 | |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 812 | if (dwc->ep0_bounced) { |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 813 | unsigned transfer_size = ur->length; |
| 814 | unsigned maxp = ep0->endpoint.maxpacket; |
| 815 | |
| 816 | transfer_size += (maxp - (transfer_size % maxp)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 817 | transferred = min_t(u32, ur->length, |
Moiz Sonasath | 566ccdd | 2012-03-14 00:44:56 -0500 | [diff] [blame] | 818 | transfer_size - length); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 819 | memcpy(ur->buf, dwc->ep0_bounce, transferred); |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 820 | } else { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 821 | transferred = ur->length - length; |
Felipe Balbi | a682970 | 2011-08-27 22:18:09 +0300 | [diff] [blame] | 822 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 823 | |
Felipe Balbi | cd423dd | 2012-03-21 11:44:00 +0200 | [diff] [blame] | 824 | ur->actual += transferred; |
| 825 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 826 | if ((epnum & 1) && ur->actual < ur->length) { |
| 827 | /* for some reason we did not get everything out */ |
| 828 | |
| 829 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 830 | } else { |
| 831 | /* |
| 832 | * handle the case where we have to send a zero packet. This |
| 833 | * seems to be case when req.length > maxpacket. Could it be? |
| 834 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 835 | if (r) |
Sebastian Andrzej Siewior | c2da2ff | 2011-10-20 19:04:16 +0200 | [diff] [blame] | 836 | dwc3_gadget_giveback(ep0, r, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Felipe Balbi | 85a7810 | 2012-05-31 12:32:37 +0300 | [diff] [blame] | 840 | static void dwc3_ep0_complete_status(struct dwc3 *dwc, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 841 | const struct dwc3_event_depevt *event) |
| 842 | { |
| 843 | struct dwc3_request *r; |
| 844 | struct dwc3_ep *dep; |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 845 | struct dwc3_trb *trb; |
| 846 | u32 status; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 847 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 848 | dep = dwc->eps[0]; |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 849 | trb = dwc->ep0_trb; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 850 | |
| 851 | if (!list_empty(&dep->request_list)) { |
| 852 | r = next_request(&dep->request_list); |
| 853 | |
| 854 | dwc3_gadget_giveback(dep, r, 0); |
| 855 | } |
| 856 | |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 857 | if (dwc->test_mode) { |
| 858 | int ret; |
| 859 | |
| 860 | ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr); |
| 861 | if (ret < 0) { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 862 | dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d", |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 863 | dwc->test_mode_nr); |
| 864 | dwc3_ep0_stall_and_restart(dwc); |
Felipe Balbi | 5c81aba | 2012-06-25 19:30:49 +0300 | [diff] [blame] | 865 | return; |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 869 | status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 870 | if (status == DWC3_TRBSTS_SETUP_PENDING) |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 871 | dwc3_trace(trace_dwc3_ep0, "Setup Pending received\n"); |
Felipe Balbi | fca8892a | 2012-07-19 09:05:35 +0300 | [diff] [blame] | 872 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 873 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 874 | dwc3_ep0_out_start(dwc); |
| 875 | } |
| 876 | |
| 877 | static void dwc3_ep0_xfer_complete(struct dwc3 *dwc, |
| 878 | const struct dwc3_event_depevt *event) |
| 879 | { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 880 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 881 | |
| 882 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 883 | dep->resource_index = 0; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 884 | dwc->setup_packet_pending = false; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 885 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 886 | switch (dwc->ep0state) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 887 | case EP0_SETUP_PHASE: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 888 | dwc3_trace(trace_dwc3_ep0, "Setup Phase"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 889 | dwc3_ep0_inspect_setup(dwc, event); |
| 890 | break; |
| 891 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 892 | case EP0_DATA_PHASE: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 893 | dwc3_trace(trace_dwc3_ep0, "Data Phase"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 894 | dwc3_ep0_complete_data(dwc, event); |
| 895 | break; |
| 896 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 897 | case EP0_STATUS_PHASE: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 898 | dwc3_trace(trace_dwc3_ep0, "Status Phase"); |
Felipe Balbi | 85a7810 | 2012-05-31 12:32:37 +0300 | [diff] [blame] | 899 | dwc3_ep0_complete_status(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 900 | break; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 901 | default: |
| 902 | WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 906 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
| 907 | struct dwc3_ep *dep, struct dwc3_request *req) |
| 908 | { |
| 909 | int ret; |
| 910 | |
| 911 | req->direction = !!dep->number; |
| 912 | |
| 913 | if (req->request.length == 0) { |
| 914 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
| 915 | dwc->ctrl_req_addr, 0, |
| 916 | DWC3_TRBCTL_CONTROL_DATA); |
Felipe Balbi | c74c6d4 | 2012-05-04 13:08:22 +0300 | [diff] [blame] | 917 | } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 918 | && (dep->number == 0)) { |
Andrew Morton | c390b03 | 2013-03-08 09:42:50 +0200 | [diff] [blame] | 919 | u32 transfer_size; |
| 920 | u32 maxpacket; |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 921 | |
| 922 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 923 | dep->number); |
| 924 | if (ret) { |
| 925 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE); |
| 930 | |
Andrew Morton | c390b03 | 2013-03-08 09:42:50 +0200 | [diff] [blame] | 931 | maxpacket = dep->endpoint.maxpacket; |
| 932 | transfer_size = roundup(req->request.length, maxpacket); |
Felipe Balbi | a080788 | 2012-05-04 13:03:54 +0300 | [diff] [blame] | 933 | |
| 934 | dwc->ep0_bounced = true; |
| 935 | |
| 936 | /* |
| 937 | * REVISIT in case request length is bigger than |
| 938 | * DWC3_EP0_BOUNCE_SIZE we will need two chained |
| 939 | * TRBs to handle the transfer. |
| 940 | */ |
| 941 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
| 942 | dwc->ep0_bounce_addr, transfer_size, |
| 943 | DWC3_TRBCTL_CONTROL_DATA); |
| 944 | } else { |
| 945 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 946 | dep->number); |
| 947 | if (ret) { |
| 948 | dev_dbg(dwc->dev, "failed to map request\n"); |
| 949 | return; |
| 950 | } |
| 951 | |
| 952 | ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma, |
| 953 | req->request.length, DWC3_TRBCTL_CONTROL_DATA); |
| 954 | } |
| 955 | |
| 956 | WARN_ON(ret < 0); |
| 957 | } |
| 958 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 959 | static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 960 | { |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 961 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 962 | u32 type; |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 963 | |
| 964 | type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3 |
| 965 | : DWC3_TRBCTL_CONTROL_STATUS2; |
| 966 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 967 | return dwc3_ep0_start_trans(dwc, dep->number, |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 968 | dwc->ctrl_req_addr, 0, type); |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 969 | } |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 970 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 971 | 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] | 972 | { |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 973 | if (dwc->resize_fifos) { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 974 | dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs"); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 975 | dwc3_gadget_resize_tx_fifos(dwc); |
| 976 | dwc->resize_fifos = 0; |
| 977 | } |
| 978 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 979 | WARN_ON(dwc3_ep0_start_control_status(dep)); |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 980 | } |
| 981 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 982 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, |
| 983 | const struct dwc3_event_depevt *event) |
| 984 | { |
| 985 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
| 986 | |
| 987 | __dwc3_ep0_do_control_status(dwc, dep); |
| 988 | } |
| 989 | |
Felipe Balbi | 2e3db06 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 990 | static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 991 | { |
| 992 | struct dwc3_gadget_ep_cmd_params params; |
| 993 | u32 cmd; |
| 994 | int ret; |
| 995 | |
| 996 | if (!dep->resource_index) |
| 997 | return; |
| 998 | |
| 999 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
| 1000 | cmd |= DWC3_DEPCMD_CMDIOC; |
| 1001 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
| 1002 | memset(¶ms, 0, sizeof(params)); |
| 1003 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 1004 | WARN_ON_ONCE(ret); |
| 1005 | dep->resource_index = 0; |
| 1006 | } |
| 1007 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1008 | static void dwc3_ep0_xfernotready(struct dwc3 *dwc, |
| 1009 | const struct dwc3_event_depevt *event) |
| 1010 | { |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 1011 | dwc->setup_packet_pending = true; |
| 1012 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1013 | switch (event->status) { |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1014 | case DEPEVT_STATUS_CONTROL_DATA: |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1015 | dwc3_trace(trace_dwc3_ep0, "Control Data"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1016 | |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1017 | /* |
Felipe Balbi | 2e3db06 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1018 | * We already have a DATA transfer in the controller's cache, |
| 1019 | * if we receive a XferNotReady(DATA) we will ignore it, unless |
| 1020 | * it's for the wrong direction. |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1021 | * |
Felipe Balbi | 2e3db06 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1022 | * In that case, we must issue END_TRANSFER command to the Data |
| 1023 | * Phase we already have started and issue SetStall on the |
| 1024 | * control endpoint. |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1025 | */ |
| 1026 | if (dwc->ep0_expect_in != event->endpoint_number) { |
Felipe Balbi | 2e3db06 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1027 | struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in]; |
| 1028 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1029 | dwc3_trace(trace_dwc3_ep0, |
| 1030 | "Wrong direction for Data phase"); |
Felipe Balbi | 2e3db06 | 2012-07-19 09:26:59 +0300 | [diff] [blame] | 1031 | dwc3_ep0_end_control_data(dwc, dep); |
Felipe Balbi | 55f3fba | 2011-09-08 18:27:33 +0300 | [diff] [blame] | 1032 | dwc3_ep0_stall_and_restart(dwc); |
| 1033 | return; |
| 1034 | } |
| 1035 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1036 | break; |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1037 | |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 1038 | case DEPEVT_STATUS_CONTROL_STATUS: |
Felipe Balbi | 77fa6df | 2012-07-23 09:09:32 +0300 | [diff] [blame] | 1039 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) |
| 1040 | return; |
| 1041 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1042 | dwc3_trace(trace_dwc3_ep0, "Control Status"); |
Felipe Balbi | 1ddcb21 | 2011-08-30 15:52:17 +0300 | [diff] [blame] | 1043 | |
Sebastian Andrzej Siewior | f0f2b2a | 2011-11-02 13:30:44 +0100 | [diff] [blame] | 1044 | dwc->ep0state = EP0_STATUS_PHASE; |
| 1045 | |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 1046 | if (dwc->delayed_status) { |
| 1047 | WARN_ON_ONCE(event->endpoint_number != 1); |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1048 | dwc3_trace(trace_dwc3_ep0, "Delayed Status"); |
Sebastian Andrzej Siewior | 5bdb1dc | 2011-11-02 13:30:45 +0100 | [diff] [blame] | 1049 | return; |
| 1050 | } |
| 1051 | |
Felipe Balbi | 788a23f | 2012-05-21 14:22:41 +0300 | [diff] [blame] | 1052 | dwc3_ep0_do_control_status(dwc, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | void dwc3_ep0_interrupt(struct dwc3 *dwc, |
Felipe Balbi | 8becf27 | 2011-11-04 12:40:05 +0200 | [diff] [blame] | 1057 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1058 | { |
| 1059 | u8 epnum = event->endpoint_number; |
| 1060 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1061 | 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] | 1062 | dwc3_ep_event_string(event->endpoint_event), |
Sebastian Andrzej Siewior | b147f35 | 2011-09-30 10:58:40 +0300 | [diff] [blame] | 1063 | epnum >> 1, (epnum & 1) ? "in" : "out", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1064 | dwc3_ep0_state_string(dwc->ep0state)); |
| 1065 | |
| 1066 | switch (event->endpoint_event) { |
| 1067 | case DWC3_DEPEVT_XFERCOMPLETE: |
| 1068 | dwc3_ep0_xfer_complete(dwc, event); |
| 1069 | break; |
| 1070 | |
| 1071 | case DWC3_DEPEVT_XFERNOTREADY: |
| 1072 | dwc3_ep0_xfernotready(dwc, event); |
| 1073 | break; |
| 1074 | |
| 1075 | case DWC3_DEPEVT_XFERINPROGRESS: |
| 1076 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 1077 | case DWC3_DEPEVT_STREAMEVT: |
| 1078 | case DWC3_DEPEVT_EPCMDCMPLT: |
| 1079 | break; |
| 1080 | } |
| 1081 | } |