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