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