blob: 7f5bdb318f89ca8ca75b105fad142aeec14571b8 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 *
Felipe Balbi5945f782013-06-30 14:15:11 +030010 * 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 Balbi72246da2011-08-19 18:10:58 +030013 *
Felipe Balbi5945f782013-06-30 14:15:11 +030014 * 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 Balbi72246da2011-08-19 18:10:58 +030018 */
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 Siewior5bdb1dc2011-11-02 13:30:45 +010032#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030033
34#include "core.h"
Felipe Balbi80977dc2014-08-19 16:37:22 -050035#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030036#include "gadget.h"
37#include "io.h"
38
Felipe Balbi788a23f2012-05-21 14:22:41 +030039static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbia0807882012-05-04 13:03:54 +030040static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
41 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010042
Felipe Balbid686a5f2017-04-07 13:47:49 +030043static void dwc3_ep0_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbi7931ec82016-12-20 13:57:32 +020044 dma_addr_t buf_dma, u32 len, u32 type, bool chain)
Felipe Balbi72246da2011-08-19 18:10:58 +030045{
Felipe Balbif6bafc62012-02-06 11:04:53 +020046 struct dwc3_trb *trb;
Felipe Balbid686a5f2017-04-07 13:47:49 +030047 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +030048
Felipe Balbid686a5f2017-04-07 13:47:49 +030049 dwc = dep->dwc;
Felipe Balbi53fd8812016-04-04 15:33:41 +030050 trb = &dwc->ep0_trb[dep->trb_enqueue];
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053051
52 if (chain)
Felipe Balbi53fd8812016-04-04 15:33:41 +030053 dep->trb_enqueue++;
Felipe Balbi72246da2011-08-19 18:10:58 +030054
Felipe Balbif6bafc62012-02-06 11:04:53 +020055 trb->bpl = lower_32_bits(buf_dma);
56 trb->bph = upper_32_bits(buf_dma);
57 trb->size = len;
58 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030059
Felipe Balbif6bafc62012-02-06 11:04:53 +020060 trb->ctrl |= (DWC3_TRB_CTRL_HWO
Felipe Balbif6bafc62012-02-06 11:04:53 +020061 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030062
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053063 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 Balbi7931ec82016-12-20 13:57:32 +020069 trace_dwc3_prepare_trb(dep, trb);
70}
71
Felipe Balbid686a5f2017-04-07 13:47:49 +030072static int dwc3_ep0_start_trans(struct dwc3_ep *dep)
Felipe Balbi7931ec82016-12-20 13:57:32 +020073{
74 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbid686a5f2017-04-07 13:47:49 +030075 struct dwc3 *dwc;
Felipe Balbi7931ec82016-12-20 13:57:32 +020076 int ret;
77
Felipe Balbi7931ec82016-12-20 13:57:32 +020078 if (dep->flags & DWC3_EP_BUSY)
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053079 return 0;
80
Felipe Balbid686a5f2017-04-07 13:47:49 +030081 dwc = dep->dwc;
82
Felipe Balbi72246da2011-08-19 18:10:58 +030083 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030084 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
85 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030086
Felipe Balbi2cd47182016-04-12 16:42:43 +030087 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, &params);
Felipe Balbi8566cd12016-09-26 11:16:39 +030088 if (ret < 0)
Felipe Balbi72246da2011-08-19 18:10:58 +030089 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +030090
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030091 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi2eb88012016-04-12 16:53:39 +030092 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi1ddcb212011-08-30 15:52:17 +030093 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
94
Felipe Balbi72246da2011-08-19 18:10:58 +030095 return 0;
96}
97
98static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
99 struct dwc3_request *req)
100{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100101 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300102
103 req->request.actual = 0;
104 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300105 req->epnum = dep->number;
106
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200107 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300108
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300109 /*
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 Balbic7fcdeb2011-08-27 22:28:36 +0300119 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300120
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300121 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300122
Felipe Balbi68d8a782011-12-29 06:32:29 +0200123 if (dwc->ep0state != EP0_DATA_PHASE) {
124 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300125 return 0;
126 }
Felipe Balbia6829702011-08-27 22:18:09 +0300127
Felipe Balbia0807882012-05-04 13:03:54 +0300128 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
129
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300130 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
131 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300132
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 Balbi7125d582012-07-19 21:05:08 +0300141 unsigned direction;
142
143 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100144 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300145 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200146
147 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300148 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300149
150 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300151 }
152
Felipe Balbifca8892a2012-07-19 09:05:35 +0300153 /*
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 Balbi35f75692012-07-19 08:49:01 +0300196 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300197}
198
199int 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 Balbi72246da2011-08-19 18:10:58 +0300210 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200211 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200212 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
213 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300214 ret = -ESHUTDOWN;
215 goto out;
216 }
217
218 /* we share one TRB for ep0/1 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200219 if (!list_empty(&dep->pending_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300220 ret = -EBUSY;
221 goto out;
222 }
223
Felipe Balbi72246da2011-08-19 18:10:58 +0300224 ret = __dwc3_gadget_ep0_queue(dep, req);
225
226out:
227 spin_unlock_irqrestore(&dwc->lock, flags);
228
229 return ret;
230}
231
232static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
233{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300234 struct dwc3_ep *dep;
235
236 /* reinitialize physical ep1 */
237 dep = dwc->eps[1];
238 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300239
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300241 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500242 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200243 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100244 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300245
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200246 if (!list_empty(&dep->pending_list)) {
Felipe Balbid7422202011-09-08 18:17:12 +0300247 struct dwc3_request *req;
248
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200249 req = next_request(&dep->pending_list);
Felipe Balbid7422202011-09-08 18:17:12 +0300250 dwc3_gadget_giveback(dep, req, -ECONNRESET);
251 }
252
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300253 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300254 dwc3_ep0_out_start(dwc);
255}
256
Felipe Balbi33fb6912014-09-24 10:46:46 -0500257int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530258{
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 Balbi33fb6912014-09-24 10:46:46 -0500267int 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 Balbi72246da2011-08-19 18:10:58 +0300281void dwc3_ep0_out_start(struct dwc3 *dwc)
282{
Felipe Balbid686a5f2017-04-07 13:47:49 +0300283 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300284 int ret;
285
Baolin Wangbb014732016-10-14 17:11:33 +0800286 complete(&dwc->ep0_in_setup);
287
Felipe Balbid686a5f2017-04-07 13:47:49 +0300288 dep = dwc->eps[0];
289 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 8,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530290 DWC3_TRBCTL_CONTROL_SETUP, false);
Felipe Balbid686a5f2017-04-07 13:47:49 +0300291 ret = dwc3_ep0_start_trans(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 WARN_ON(ret < 0);
293}
294
Felipe Balbi72246da2011-08-19 18:10:58 +0300295static 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 Siewior8ee62702011-10-18 19:13:29 +0200312static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300313{
Felipe Balbi72246da2011-08-19 18:10:58 +0300314}
Felipe Balbi72246da2011-08-19 18:10:58 +0300315/*
316 * ch 9.4.5
317 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200318static int dwc3_ep0_handle_status(struct dwc3 *dwc,
319 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300320{
321 struct dwc3_ep *dep;
322 u32 recip;
Felipe Balbi9b0a1f92017-06-08 13:16:18 +0300323 u32 value;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200324 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300325 u16 usb_status = 0;
326 __le16 *response_pkt;
327
Felipe Balbi9b0a1f92017-06-08 13:16:18 +0300328 /* We don't support PTM_STATUS */
329 value = le16_to_cpu(ctrl->wValue);
330 if (value != 0)
331 return -EINVAL;
332
Felipe Balbi72246da2011-08-19 18:10:58 +0300333 recip = ctrl->bRequestType & USB_RECIP_MASK;
334 switch (recip) {
335 case USB_RECIP_DEVICE:
336 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200337 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 */
Peter Chenbcdea502015-01-28 16:32:40 +0800339 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200340
John Younee5cd412016-02-05 17:08:45 -0800341 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
342 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200343 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 Balbi72246da2011-08-19 18:10:58 +0300350 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 Balbi25b8ff62011-11-04 12:32:47 +0200362 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300363
364 if (dep->flags & DWC3_EP_STALL)
365 usb_status = 1 << USB_ENDPOINT_HALT;
366 break;
367 default:
368 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700369 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300370
371 response_pkt = (__le16 *) dwc->setup_buf;
372 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200373
374 dep = dwc->eps[0];
375 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100376 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200377 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100378 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200379
380 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300381}
382
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300383static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
384 int set)
Felipe Balbi72246da2011-08-19 18:10:58 +0300385{
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300386 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300387
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300388 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 Balbifdba5aa2013-01-25 11:28:19 +0200393
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300394 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 Balbi72246da2011-08-19 18:10:58 +0300400
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300401 return 0;
402}
Felipe Balbi72246da2011-08-19 18:10:58 +0300403
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300404static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
405 int set)
406{
407 u32 reg;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200408
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200409
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300410 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 Siewiore6a3b5e2011-09-13 17:54:39 +0200415
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300416 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 Balbi72246da2011-08-19 18:10:58 +0300422
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300423 return 0;
424}
Felipe Balbi72246da2011-08-19 18:10:58 +0300425
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300426static 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 Balbi72246da2011-08-19 18:10:58 +0300442 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300443 default:
444 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700445 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300446
Felipe Balbi72246da2011-08-19 18:10:58 +0300447 return 0;
448}
449
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300450static 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
488static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
489 struct usb_ctrlrequest *ctrl, int set)
490{
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300491 u32 wValue;
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300492 int ret = 0;
493
494 wValue = le16_to_cpu(ctrl->wValue);
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300495
496 switch (wValue) {
497 case USB_INTRF_FUNC_SUSPEND:
Arnd Bergmann1c404b52016-11-16 16:37:30 +0100498 /*
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 Balbi39e07ff2016-10-03 12:55:29 +0300505 break;
506 default:
507 ret = -EINVAL;
508 }
509
510 return ret;
511}
512
513static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
514 struct usb_ctrlrequest *ctrl, int set)
515{
516 struct dwc3_ep *dep;
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300517 u32 wValue;
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300518 int ret;
519
520 wValue = le16_to_cpu(ctrl->wValue);
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300521
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
542static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
543 struct usb_ctrlrequest *ctrl, int set)
544{
545 u32 recip;
546 int ret;
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300547
548 recip = ctrl->bRequestType & USB_RECIP_MASK;
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300549
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 Balbi72246da2011-08-19 18:10:58 +0300567static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
568{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200569 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 u32 addr;
571 u32 reg;
572
573 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300574 if (addr > 127) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200575 dev_err(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300577 }
578
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200579 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200580 dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300581 return -EINVAL;
582 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300583
Felipe Balbi26460212011-09-30 10:58:36 +0300584 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 Balbi72246da2011-08-19 18:10:58 +0300588
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200589 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200590 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200591 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200592 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300593
Felipe Balbi26460212011-09-30 10:58:36 +0300594 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300595}
596
597static 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
607static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
608{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200609 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 u32 cfg;
611 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530612 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300613
614 cfg = le16_to_cpu(ctrl->wValue);
615
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200616 switch (state) {
617 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300619
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200620 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 ret = dwc3_ep0_delegate_req(dwc, ctrl);
622 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200623 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300624
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 Balbi14cd5922011-12-19 13:01:52 +0200634
Pratyush Anande274a312012-07-02 10:21:54 +0530635 /*
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 Balbi457e84b2012-01-18 18:04:09 +0200642 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300643 break;
644
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200645 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300647 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200648 usb_gadget_set_state(&dwc->gadget,
649 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100651 default:
652 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300653 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100654 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300655}
656
Felipe Balbi865e09e2012-04-24 16:19:49 +0300657static 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 Youn501058e2016-05-23 11:32:40 -0700668 __le16 u2sel;
669 __le16 u2pel;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300670 } __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 Balbic8cf7af2012-05-31 11:00:28 +0300678 dwc->u2sel = le16_to_cpu(timing.u2sel);
679 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300680
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
701static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
702{
703 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200704 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300705 u16 wLength;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300706
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200707 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300708 return -EINVAL;
709
Felipe Balbi865e09e2012-04-24 16:19:49 +0300710 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 Balbic12a0d82012-04-25 10:45:05 +0300735static 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 Balbi72246da2011-08-19 18:10:58 +0300757static 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 Balbi72246da2011-08-19 18:10:58 +0300763 ret = dwc3_ep0_handle_status(dwc, ctrl);
764 break;
765 case USB_REQ_CLEAR_FEATURE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300766 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
767 break;
768 case USB_REQ_SET_FEATURE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
770 break;
771 case USB_REQ_SET_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 ret = dwc3_ep0_set_address(dwc, ctrl);
773 break;
774 case USB_REQ_SET_CONFIGURATION:
Felipe Balbi72246da2011-08-19 18:10:58 +0300775 ret = dwc3_ep0_set_config(dwc, ctrl);
776 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300777 case USB_REQ_SET_SEL:
Felipe Balbi865e09e2012-04-24 16:19:49 +0300778 ret = dwc3_ep0_set_sel(dwc, ctrl);
779 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300780 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbic12a0d82012-04-25 10:45:05 +0300781 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
782 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300783 default:
Felipe Balbi72246da2011-08-19 18:10:58 +0300784 ret = dwc3_ep0_delegate_req(dwc, ctrl);
785 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700786 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300787
788 return ret;
789}
790
791static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
792 const struct dwc3_event_depevt *event)
793{
Felipe Balbi7d5e6502017-04-07 13:34:21 +0300794 struct usb_ctrlrequest *ctrl = (void *) dwc->ep0_trb;
Felipe Balbief21ede2012-05-31 10:29:49 +0300795 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300796 u32 len;
797
798 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300799 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300800
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500801 trace_dwc3_ctrl_req(ctrl);
802
Felipe Balbi72246da2011-08-19 18:10:58 +0300803 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300804 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300805 dwc->three_stage_setup = false;
806 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300807 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
808 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300809 dwc->three_stage_setup = true;
810 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300811 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
812 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300813
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 Siewior5bdb1dc2011-11-02 13:30:45 +0100819 if (ret == USB_GADGET_DELAYED_STATUS)
820 dwc->delayed_status = true;
821
Felipe Balbief21ede2012-05-31 10:29:49 +0300822out:
823 if (ret < 0)
824 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300825}
826
827static 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 Balbif6bafc62012-02-06 11:04:53 +0200832 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200833 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530834 u32 transferred = 0;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300835 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200836 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 u8 epnum;
838
839 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200840 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300841
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300842 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200843 trb = dwc->ep0_trb;
Felipe Balbiccb072d2014-10-01 12:20:29 -0500844 trace_dwc3_complete_trb(ep0, trb);
845
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200846 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600847 if (!r)
848 return;
849
Felipe Balbifca8892a2012-07-19 09:05:35 +0300850 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
851 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600852 dwc->setup_packet_pending = true;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300853 if (r)
854 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
855
856 return;
857 }
858
Felipe Balbi6856d302014-09-30 11:43:20 -0500859 ur = &r->request;
860
Felipe Balbif6bafc62012-02-06 11:04:53 +0200861 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300862 transferred = ur->length - length;
863 ur->actual += transferred;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530864
Felipe Balbid6e5a542017-04-07 16:34:38 +0300865 if ((IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
866 ur->length && ur->zero) || dwc->ep0_bounced) {
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300867 trb++;
868 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
Felipe Balbid6e5a542017-04-07 16:34:38 +0300869 trace_dwc3_complete_trb(ep0, trb);
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300870 ep0->trb_enqueue = 0;
871 dwc->ep0_bounced = false;
Felipe Balbia6829702011-08-27 22:18:09 +0300872 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300873
Felipe Balbid6e5a542017-04-07 16:34:38 +0300874 if ((epnum & 1) && ur->actual < ur->length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300875 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbid6e5a542017-04-07 16:34:38 +0300876 else
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500877 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300878}
879
Felipe Balbi85a78102012-05-31 12:32:37 +0300880static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300881 const struct dwc3_event_depevt *event)
882{
883 struct dwc3_request *r;
884 struct dwc3_ep *dep;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300885 struct dwc3_trb *trb;
886 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300887
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300888 dep = dwc->eps[0];
Felipe Balbifca8892a2012-07-19 09:05:35 +0300889 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300890
Felipe Balbiccb072d2014-10-01 12:20:29 -0500891 trace_dwc3_complete_trb(dep, trb);
892
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200893 if (!list_empty(&dep->pending_list)) {
894 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300895
896 dwc3_gadget_giveback(dep, r, 0);
897 }
898
Gerard Cauvy3b637362012-02-10 12:21:18 +0200899 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 Balbi5eb30ce2016-11-03 14:07:51 +0200904 dev_err(dwc->dev, "invalid test #%d\n",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200905 dwc->test_mode_nr);
906 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300907 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200908 }
909 }
910
Felipe Balbifca8892a2012-07-19 09:05:35 +0300911 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200912 if (status == DWC3_TRBSTS_SETUP_PENDING)
Felipe Balbib5d335e2015-11-16 16:20:34 -0600913 dwc->setup_packet_pending = true;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300914
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300915 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300916 dwc3_ep0_out_start(dwc);
917}
918
919static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
920 const struct dwc3_event_depevt *event)
921{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300922 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
923
924 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300925 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300926 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300927
Felipe Balbi72246da2011-08-19 18:10:58 +0300928 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300929 case EP0_SETUP_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300930 dwc3_ep0_inspect_setup(dwc, event);
931 break;
932
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300933 case EP0_DATA_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300934 dwc3_ep0_complete_data(dwc, event);
935 break;
936
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300937 case EP0_STATUS_PHASE:
Felipe Balbi85a78102012-05-31 12:32:37 +0300938 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300940 default:
941 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300942 }
943}
944
Felipe Balbia0807882012-05-04 13:03:54 +0300945static 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 Balbid686a5f2017-04-07 13:47:49 +0300953 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530954 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbid686a5f2017-04-07 13:47:49 +0300955 ret = dwc3_ep0_start_trans(dep);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300956 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300957 && (dep->number == 0)) {
Andrew Mortonc390b032013-03-08 09:42:50 +0200958 u32 maxpacket;
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300959 u32 rem;
Felipe Balbia0807882012-05-04 13:03:54 +0300960
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530961 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
962 &req->request, dep->number);
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200963 if (ret)
Felipe Balbia0807882012-05-04 13:03:54 +0300964 return;
Felipe Balbia0807882012-05-04 13:03:54 +0300965
Andrew Mortonc390b032013-03-08 09:42:50 +0200966 maxpacket = dep->endpoint.maxpacket;
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300967 rem = req->request.length % maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +0300968 dwc->ep0_bounced = true;
969
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300970 /* 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 Balbi55168472017-09-11 10:45:12 +0300976 req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
977
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300978 /* 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 Balbid686a5f2017-04-07 13:47:49 +0300982 false);
983 ret = dwc3_ep0_start_trans(dep);
Felipe Balbid6e5a542017-04-07 16:34:38 +0300984 } else if (IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
985 req->request.length && req->request.zero) {
986 u32 maxpacket;
Felipe Balbid6e5a542017-04-07 16:34:38 +0300987
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 Balbid6e5a542017-04-07 16:34:38 +0300994
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 Balbi55168472017-09-11 10:45:12 +03001001 req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
1002
Felipe Balbid6e5a542017-04-07 16:34:38 +03001003 /* 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 Balbia0807882012-05-04 13:03:54 +03001008 } else {
Arnd Bergmannd64ff402016-11-17 17:13:47 +05301009 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1010 &req->request, dep->number);
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001011 if (ret)
Felipe Balbia0807882012-05-04 13:03:54 +03001012 return;
Felipe Balbia0807882012-05-04 13:03:54 +03001013
Felipe Balbid686a5f2017-04-07 13:47:49 +03001014 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301015 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1016 false);
Felipe Balbi55168472017-09-11 10:45:12 +03001017
1018 req->trb = &dwc->ep0_trb[dep->trb_enqueue];
1019
Felipe Balbid686a5f2017-04-07 13:47:49 +03001020 ret = dwc3_ep0_start_trans(dep);
Felipe Balbia0807882012-05-04 13:03:54 +03001021 }
1022
1023 WARN_ON(ret < 0);
1024}
1025
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001026static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001027{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001028 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001029 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001030
1031 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1032 : DWC3_TRBCTL_CONTROL_STATUS2;
1033
Felipe Balbid686a5f2017-04-07 13:47:49 +03001034 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 0, type, false);
1035 return dwc3_ep0_start_trans(dep);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001036}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001037
Felipe Balbi788a23f2012-05-21 14:22:41 +03001038static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001039{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001040 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001041}
1042
Felipe Balbi788a23f2012-05-21 14:22:41 +03001043static 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 Balbi2e3db062012-07-19 09:26:59 +03001051static 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(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001064 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi2e3db062012-07-19 09:26:59 +03001065 WARN_ON_ONCE(ret);
1066 dep->resource_index = 0;
1067}
1068
Felipe Balbi72246da2011-08-19 18:10:58 +03001069static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1070 const struct dwc3_event_depevt *event)
1071{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001072 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001073 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001074 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001075 * 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 Balbi55f3fba2011-09-08 18:27:33 +03001078 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001079 * 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 Balbi55f3fba2011-09-08 18:27:33 +03001082 */
1083 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001084 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1085
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001086 dev_err(dwc->dev, "unexpected direction for Data Phase\n");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001087 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001088 dwc3_ep0_stall_and_restart(dwc);
1089 return;
1090 }
1091
Felipe Balbi72246da2011-08-19 18:10:58 +03001092 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001093
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001094 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001095 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1096 return;
1097
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001098 dwc->ep0state = EP0_STATUS_PHASE;
1099
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001100 if (dwc->delayed_status) {
Baolin Wang53896792017-01-14 16:40:39 +08001101 struct dwc3_ep *dep = dwc->eps[0];
1102
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001103 WARN_ON_ONCE(event->endpoint_number != 1);
Baolin Wang53896792017-01-14 16:40:39 +08001104 /*
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 Siewior5bdb1dc2011-11-02 13:30:45 +01001116 return;
1117 }
1118
Felipe Balbi788a23f2012-05-21 14:22:41 +03001119 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001120 }
1121}
1122
1123void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001124 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001125{
Felipe Balbi72246da2011-08-19 18:10:58 +03001126 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}