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