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