blob: 2d2f582068cb0b7b9b90e9347de747af3b2f93ec [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/list.h>
27#include <linux/dma-mapping.h>
28
29#include <linux/usb/ch9.h>
30#include <linux/usb/gadget.h>
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010031#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030032
33#include "core.h"
Felipe Balbi80977dc2014-08-19 16:37:22 -050034#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030035#include "gadget.h"
36#include "io.h"
37
Felipe Balbi788a23f2012-05-21 14:22:41 +030038static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbia0807882012-05-04 13:03:54 +030039static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
40 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010041
Felipe Balbi72246da2011-08-19 18:10:58 +030042static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
43{
44 switch (state) {
45 case EP0_UNCONNECTED:
46 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030047 case EP0_SETUP_PHASE:
48 return "Setup Phase";
49 case EP0_DATA_PHASE:
50 return "Data Phase";
51 case EP0_STATUS_PHASE:
52 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030053 default:
54 return "UNKNOWN";
55 }
56}
57
58static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +053059 u32 len, u32 type, bool chain)
Felipe Balbi72246da2011-08-19 18:10:58 +030060{
61 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020062 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030063 struct dwc3_ep *dep;
64
65 int ret;
66
67 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030068 if (dep->flags & DWC3_EP_BUSY) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -050069 dwc3_trace(trace_dwc3_ep0, "%s still busy", dep->name);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030070 return 0;
71 }
Felipe Balbi72246da2011-08-19 18:10:58 +030072
Felipe Balbi53fd8812016-04-04 15:33:41 +030073 trb = &dwc->ep0_trb[dep->trb_enqueue];
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053074
75 if (chain)
Felipe Balbi53fd8812016-04-04 15:33:41 +030076 dep->trb_enqueue++;
Felipe Balbi72246da2011-08-19 18:10:58 +030077
Felipe Balbif6bafc62012-02-06 11:04:53 +020078 trb->bpl = lower_32_bits(buf_dma);
79 trb->bph = upper_32_bits(buf_dma);
80 trb->size = len;
81 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030082
Felipe Balbif6bafc62012-02-06 11:04:53 +020083 trb->ctrl |= (DWC3_TRB_CTRL_HWO
Felipe Balbif6bafc62012-02-06 11:04:53 +020084 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030085
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053086 if (chain)
87 trb->ctrl |= DWC3_TRB_CTRL_CHN;
88 else
89 trb->ctrl |= (DWC3_TRB_CTRL_IOC
90 | DWC3_TRB_CTRL_LST);
91
92 if (chain)
93 return 0;
94
Felipe Balbi72246da2011-08-19 18:10:58 +030095 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030096 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
97 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030098
Felipe Balbiccb072d2014-10-01 12:20:29 -050099 trace_dwc3_prepare_trb(dep, trb);
100
Felipe Balbi2cd47182016-04-12 16:42:43 +0300101 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, &params);
Felipe Balbi8566cd12016-09-26 11:16:39 +0300102 if (ret < 0)
Felipe Balbi72246da2011-08-19 18:10:58 +0300103 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300104
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300105 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi2eb88012016-04-12 16:53:39 +0300106 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300107 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
108
Felipe Balbi72246da2011-08-19 18:10:58 +0300109 return 0;
110}
111
112static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
113 struct dwc3_request *req)
114{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100115 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300116
117 req->request.actual = 0;
118 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300119 req->epnum = dep->number;
120
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200121 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300122
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300123 /*
124 * Gadget driver might not be quick enough to queue a request
125 * before we get a Transfer Not Ready event on this endpoint.
126 *
127 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
128 * flag is set, it's telling us that as soon as Gadget queues the
129 * required request, we should kick the transfer here because the
130 * IRQ we were waiting for is long gone.
131 */
132 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300133 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300134
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300135 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300136
Felipe Balbi68d8a782011-12-29 06:32:29 +0200137 if (dwc->ep0state != EP0_DATA_PHASE) {
138 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300139 return 0;
140 }
Felipe Balbia6829702011-08-27 22:18:09 +0300141
Felipe Balbia0807882012-05-04 13:03:54 +0300142 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
143
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300144 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
145 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300146
147 return 0;
148 }
149
150 /*
151 * In case gadget driver asked us to delay the STATUS phase,
152 * handle it here.
153 */
154 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300155 unsigned direction;
156
157 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100158 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300159 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200160
161 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300162 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200163 else
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500164 dwc3_trace(trace_dwc3_ep0,
165 "too early for delayed status");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300166
167 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300168 }
169
Felipe Balbifca88922012-07-19 09:05:35 +0300170 /*
171 * Unfortunately we have uncovered a limitation wrt the Data Phase.
172 *
173 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
174 * come before issueing Start Transfer command, but if we do, we will
175 * miss situations where the host starts another SETUP phase instead of
176 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
177 * Layer Compliance Suite.
178 *
179 * The problem surfaces due to the fact that in case of back-to-back
180 * SETUP packets there will be no XferNotReady(DATA) generated and we
181 * will be stuck waiting for XferNotReady(DATA) forever.
182 *
183 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
184 * it tells us to start Data Phase right away. It also mentions that if
185 * we receive a SETUP phase instead of the DATA phase, core will issue
186 * XferComplete for the DATA phase, before actually initiating it in
187 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
188 * can only be used to print some debugging logs, as the core expects
189 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
190 * just so it completes right away, without transferring anything and,
191 * only then, we can go back to the SETUP phase.
192 *
193 * Because of this scenario, SNPS decided to change the programming
194 * model of control transfers and support on-demand transfers only for
195 * the STATUS phase. To fix the issue we have now, we will always wait
196 * for gadget driver to queue the DATA phase's struct usb_request, then
197 * start it right away.
198 *
199 * If we're actually in a 2-stage transfer, we will wait for
200 * XferNotReady(STATUS).
201 */
202 if (dwc->three_stage_setup) {
203 unsigned direction;
204
205 direction = dwc->ep0_expect_in;
206 dwc->ep0state = EP0_DATA_PHASE;
207
208 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
209
210 dep->flags &= ~DWC3_EP0_DIR_IN;
211 }
212
Felipe Balbi35f75692012-07-19 08:49:01 +0300213 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300214}
215
216int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
217 gfp_t gfp_flags)
218{
219 struct dwc3_request *req = to_dwc3_request(request);
220 struct dwc3_ep *dep = to_dwc3_ep(ep);
221 struct dwc3 *dwc = dep->dwc;
222
223 unsigned long flags;
224
225 int ret;
226
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200228 if (!dep->endpoint.desc) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500229 dwc3_trace(trace_dwc3_ep0,
230 "trying to queue request %p to disabled %s",
Felipe Balbi72246da2011-08-19 18:10:58 +0300231 request, dep->name);
232 ret = -ESHUTDOWN;
233 goto out;
234 }
235
236 /* we share one TRB for ep0/1 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200237 if (!list_empty(&dep->pending_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300238 ret = -EBUSY;
239 goto out;
240 }
241
Felipe Balbi72246da2011-08-19 18:10:58 +0300242 ret = __dwc3_gadget_ep0_queue(dep, req);
243
244out:
245 spin_unlock_irqrestore(&dwc->lock, flags);
246
247 return ret;
248}
249
250static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
251{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300252 struct dwc3_ep *dep;
253
254 /* reinitialize physical ep1 */
255 dep = dwc->eps[1];
256 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300257
Felipe Balbi72246da2011-08-19 18:10:58 +0300258 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300259 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500260 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200261 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100262 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300263
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200264 if (!list_empty(&dep->pending_list)) {
Felipe Balbid7422202011-09-08 18:17:12 +0300265 struct dwc3_request *req;
266
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200267 req = next_request(&dep->pending_list);
Felipe Balbid7422202011-09-08 18:17:12 +0300268 dwc3_gadget_giveback(dep, req, -ECONNRESET);
269 }
270
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300271 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300272 dwc3_ep0_out_start(dwc);
273}
274
Felipe Balbi33fb6912014-09-24 10:46:46 -0500275int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530276{
277 struct dwc3_ep *dep = to_dwc3_ep(ep);
278 struct dwc3 *dwc = dep->dwc;
279
280 dwc3_ep0_stall_and_restart(dwc);
281
282 return 0;
283}
284
Felipe Balbi33fb6912014-09-24 10:46:46 -0500285int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
286{
287 struct dwc3_ep *dep = to_dwc3_ep(ep);
288 struct dwc3 *dwc = dep->dwc;
289 unsigned long flags;
290 int ret;
291
292 spin_lock_irqsave(&dwc->lock, flags);
293 ret = __dwc3_gadget_ep0_set_halt(ep, value);
294 spin_unlock_irqrestore(&dwc->lock, flags);
295
296 return ret;
297}
298
Felipe Balbi72246da2011-08-19 18:10:58 +0300299void dwc3_ep0_out_start(struct dwc3 *dwc)
300{
Felipe Balbi72246da2011-08-19 18:10:58 +0300301 int ret;
302
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300303 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530304 DWC3_TRBCTL_CONTROL_SETUP, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 WARN_ON(ret < 0);
306}
307
Felipe Balbi72246da2011-08-19 18:10:58 +0300308static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
309{
310 struct dwc3_ep *dep;
311 u32 windex = le16_to_cpu(wIndex_le);
312 u32 epnum;
313
314 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
315 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
316 epnum |= 1;
317
318 dep = dwc->eps[epnum];
319 if (dep->flags & DWC3_EP_ENABLED)
320 return dep;
321
322 return NULL;
323}
324
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200325static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300326{
Felipe Balbi72246da2011-08-19 18:10:58 +0300327}
Felipe Balbi72246da2011-08-19 18:10:58 +0300328/*
329 * ch 9.4.5
330 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200331static int dwc3_ep0_handle_status(struct dwc3 *dwc,
332 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300333{
334 struct dwc3_ep *dep;
335 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200336 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 u16 usb_status = 0;
338 __le16 *response_pkt;
339
340 recip = ctrl->bRequestType & USB_RECIP_MASK;
341 switch (recip) {
342 case USB_RECIP_DEVICE:
343 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200344 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300345 */
Peter Chenbcdea502015-01-28 16:32:40 +0800346 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200347
John Younee5cd412016-02-05 17:08:45 -0800348 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
349 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200350 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
351 if (reg & DWC3_DCTL_INITU1ENA)
352 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
353 if (reg & DWC3_DCTL_INITU2ENA)
354 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
355 }
356
Felipe Balbi72246da2011-08-19 18:10:58 +0300357 break;
358
359 case USB_RECIP_INTERFACE:
360 /*
361 * Function Remote Wake Capable D0
362 * Function Remote Wakeup D1
363 */
364 break;
365
366 case USB_RECIP_ENDPOINT:
367 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
368 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200369 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300370
371 if (dep->flags & DWC3_EP_STALL)
372 usb_status = 1 << USB_ENDPOINT_HALT;
373 break;
374 default:
375 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700376 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300377
378 response_pkt = (__le16 *) dwc->setup_buf;
379 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200380
381 dep = dwc->eps[0];
382 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100383 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200384 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100385 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200386
387 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300388}
389
390static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
391 struct usb_ctrlrequest *ctrl, int set)
392{
393 struct dwc3_ep *dep;
394 u32 recip;
395 u32 wValue;
396 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200397 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300398 int ret;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200399 enum usb_device_state state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300400
401 wValue = le16_to_cpu(ctrl->wValue);
402 wIndex = le16_to_cpu(ctrl->wIndex);
403 recip = ctrl->bRequestType & USB_RECIP_MASK;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200404 state = dwc->gadget.state;
405
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 switch (recip) {
407 case USB_RECIP_DEVICE:
408
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200409 switch (wValue) {
410 case USB_DEVICE_REMOTE_WAKEUP:
411 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 /*
413 * 9.4.1 says only only for SS, in AddressState only for
414 * default control pipe
415 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 case USB_DEVICE_U1_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200417 if (state != USB_STATE_CONFIGURED)
Felipe Balbi72246da2011-08-19 18:10:58 +0300418 return -EINVAL;
John Younee5cd412016-02-05 17:08:45 -0800419 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
420 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300422
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200423 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
424 if (set)
425 reg |= DWC3_DCTL_INITU1ENA;
426 else
427 reg &= ~DWC3_DCTL_INITU1ENA;
428 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200430
Felipe Balbi72246da2011-08-19 18:10:58 +0300431 case USB_DEVICE_U2_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200432 if (state != USB_STATE_CONFIGURED)
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200433 return -EINVAL;
John Younee5cd412016-02-05 17:08:45 -0800434 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
435 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200436 return -EINVAL;
437
438 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
439 if (set)
440 reg |= DWC3_DCTL_INITU2ENA;
441 else
442 reg &= ~DWC3_DCTL_INITU2ENA;
443 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300444 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200445
Felipe Balbi72246da2011-08-19 18:10:58 +0300446 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200447 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300448
449 case USB_DEVICE_TEST_MODE:
450 if ((wIndex & 0xff) != 0)
451 return -EINVAL;
452 if (!set)
453 return -EINVAL;
454
Fei Yangae411412016-04-20 09:08:43 +0300455 switch (wIndex >> 8) {
456 case TEST_J:
457 case TEST_K:
458 case TEST_SE0_NAK:
459 case TEST_PACKET:
460 case TEST_FORCE_EN:
461 dwc->test_mode_nr = wIndex >> 8;
462 dwc->test_mode = true;
463 break;
464 default:
465 return -EINVAL;
466 }
Gerard Cauvyecb07792012-03-16 16:20:10 +0200467 break;
468 default:
469 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 }
471 break;
472
473 case USB_RECIP_INTERFACE:
474 switch (wValue) {
475 case USB_INTRF_FUNC_SUSPEND:
476 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
477 /* XXX enable Low power suspend */
478 ;
479 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
480 /* XXX enable remote wakeup */
481 ;
482 break;
483 default:
484 return -EINVAL;
485 }
486 break;
487
488 case USB_RECIP_ENDPOINT:
489 switch (wValue) {
490 case USB_ENDPOINT_HALT:
John Youn958b9fa2016-05-23 11:32:38 -0700491 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300492 if (!dep)
493 return -EINVAL;
Alan Sterna535d812013-11-01 12:05:12 -0400494 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
495 break;
Felipe Balbi7a608552014-09-24 14:19:52 -0500496 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300497 if (ret)
498 return -EINVAL;
499 break;
500 default:
501 return -EINVAL;
502 }
503 break;
504
505 default:
506 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700507 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300508
Felipe Balbi72246da2011-08-19 18:10:58 +0300509 return 0;
510}
511
512static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
513{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200514 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300515 u32 addr;
516 u32 reg;
517
518 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300519 if (addr > 127) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500520 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300521 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300522 }
523
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200524 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500525 dwc3_trace(trace_dwc3_ep0,
526 "trying to set address when configured");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300527 return -EINVAL;
528 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300529
Felipe Balbi26460212011-09-30 10:58:36 +0300530 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
531 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
532 reg |= DWC3_DCFG_DEVADDR(addr);
533 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200535 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200536 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200537 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200538 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300539
Felipe Balbi26460212011-09-30 10:58:36 +0300540 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541}
542
543static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
544{
545 int ret;
546
547 spin_unlock(&dwc->lock);
548 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
549 spin_lock(&dwc->lock);
550 return ret;
551}
552
553static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
554{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200555 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300556 u32 cfg;
557 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530558 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300559
560 cfg = le16_to_cpu(ctrl->wValue);
561
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200562 switch (state) {
563 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300565
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200566 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 ret = dwc3_ep0_delegate_req(dwc, ctrl);
568 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200569 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300570
571 /*
572 * only change state if set_config has already
573 * been processed. If gadget driver returns
574 * USB_GADGET_DELAYED_STATUS, we will wait
575 * to change the state on the next usb_ep_queue()
576 */
577 if (ret == 0)
578 usb_gadget_set_state(&dwc->gadget,
579 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200580
Pratyush Anande274a312012-07-02 10:21:54 +0530581 /*
582 * Enable transition to U1/U2 state when
583 * nothing is pending from application.
584 */
585 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
586 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
587 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200588 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 break;
590
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200591 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300593 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200594 usb_gadget_set_state(&dwc->gadget,
595 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100597 default:
598 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100600 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300601}
602
Felipe Balbi865e09e2012-04-24 16:19:49 +0300603static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
604{
605 struct dwc3_ep *dep = to_dwc3_ep(ep);
606 struct dwc3 *dwc = dep->dwc;
607
608 u32 param = 0;
609 u32 reg;
610
611 struct timing {
612 u8 u1sel;
613 u8 u1pel;
John Youn501058e2016-05-23 11:32:40 -0700614 __le16 u2sel;
615 __le16 u2pel;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300616 } __packed timing;
617
618 int ret;
619
620 memcpy(&timing, req->buf, sizeof(timing));
621
622 dwc->u1sel = timing.u1sel;
623 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300624 dwc->u2sel = le16_to_cpu(timing.u2sel);
625 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300626
627 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
628 if (reg & DWC3_DCTL_INITU2ENA)
629 param = dwc->u2pel;
630 if (reg & DWC3_DCTL_INITU1ENA)
631 param = dwc->u1pel;
632
633 /*
634 * According to Synopsys Databook, if parameter is
635 * greater than 125, a value of zero should be
636 * programmed in the register.
637 */
638 if (param > 125)
639 param = 0;
640
641 /* now that we have the time, issue DGCMD Set Sel */
642 ret = dwc3_send_gadget_generic_command(dwc,
643 DWC3_DGCMD_SET_PERIODIC_PAR, param);
644 WARN_ON(ret < 0);
645}
646
647static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
648{
649 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200650 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300651 u16 wLength;
652 u16 wValue;
653
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200654 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300655 return -EINVAL;
656
657 wValue = le16_to_cpu(ctrl->wValue);
658 wLength = le16_to_cpu(ctrl->wLength);
659
660 if (wLength != 6) {
661 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
662 wLength);
663 return -EINVAL;
664 }
665
666 /*
667 * To handle Set SEL we need to receive 6 bytes from Host. So let's
668 * queue a usb_request for 6 bytes.
669 *
670 * Remember, though, this controller can't handle non-wMaxPacketSize
671 * aligned transfers on the OUT direction, so we queue a request for
672 * wMaxPacketSize instead.
673 */
674 dep = dwc->eps[0];
675 dwc->ep0_usb_req.dep = dep;
676 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
677 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
678 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
679
680 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
681}
682
Felipe Balbic12a0d82012-04-25 10:45:05 +0300683static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
684{
685 u16 wLength;
686 u16 wValue;
687 u16 wIndex;
688
689 wValue = le16_to_cpu(ctrl->wValue);
690 wLength = le16_to_cpu(ctrl->wLength);
691 wIndex = le16_to_cpu(ctrl->wIndex);
692
693 if (wIndex || wLength)
694 return -EINVAL;
695
696 /*
697 * REVISIT It's unclear from Databook what to do with this
698 * value. For now, just cache it.
699 */
700 dwc->isoch_delay = wValue;
701
702 return 0;
703}
704
Felipe Balbi72246da2011-08-19 18:10:58 +0300705static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
706{
707 int ret;
708
709 switch (ctrl->bRequest) {
710 case USB_REQ_GET_STATUS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500711 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300712 ret = dwc3_ep0_handle_status(dwc, ctrl);
713 break;
714 case USB_REQ_CLEAR_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500715 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
717 break;
718 case USB_REQ_SET_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500719 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
721 break;
722 case USB_REQ_SET_ADDRESS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500723 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300724 ret = dwc3_ep0_set_address(dwc, ctrl);
725 break;
726 case USB_REQ_SET_CONFIGURATION:
Felipe Balbidab716c2014-09-24 11:22:23 -0500727 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 ret = dwc3_ep0_set_config(dwc, ctrl);
729 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300730 case USB_REQ_SET_SEL:
Felipe Balbidab716c2014-09-24 11:22:23 -0500731 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300732 ret = dwc3_ep0_set_sel(dwc, ctrl);
733 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300734 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbidab716c2014-09-24 11:22:23 -0500735 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300736 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
737 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 default:
Felipe Balbidab716c2014-09-24 11:22:23 -0500739 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 ret = dwc3_ep0_delegate_req(dwc, ctrl);
741 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700742 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300743
744 return ret;
745}
746
747static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
748 const struct dwc3_event_depevt *event)
749{
750 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300751 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 u32 len;
753
754 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300755 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300756
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500757 trace_dwc3_ctrl_req(ctrl);
758
Felipe Balbi72246da2011-08-19 18:10:58 +0300759 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300760 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300761 dwc->three_stage_setup = false;
762 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300763 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
764 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300765 dwc->three_stage_setup = true;
766 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300767 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
768 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300769
770 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
771 ret = dwc3_ep0_std_request(dwc, ctrl);
772 else
773 ret = dwc3_ep0_delegate_req(dwc, ctrl);
774
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100775 if (ret == USB_GADGET_DELAYED_STATUS)
776 dwc->delayed_status = true;
777
Felipe Balbief21ede2012-05-31 10:29:49 +0300778out:
779 if (ret < 0)
780 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300781}
782
783static void dwc3_ep0_complete_data(struct dwc3 *dwc,
784 const struct dwc3_event_depevt *event)
785{
786 struct dwc3_request *r = NULL;
787 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200788 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200789 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530790 unsigned transfer_size = 0;
791 unsigned maxp;
792 unsigned remaining_ur_length;
793 void *buf;
794 u32 transferred = 0;
Felipe Balbifca88922012-07-19 09:05:35 +0300795 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 u8 epnum;
798
799 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200800 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300801
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300802 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
803
Felipe Balbif6bafc62012-02-06 11:04:53 +0200804 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300805
Felipe Balbiccb072d2014-10-01 12:20:29 -0500806 trace_dwc3_complete_trb(ep0, trb);
807
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200808 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600809 if (!r)
810 return;
811
Felipe Balbifca88922012-07-19 09:05:35 +0300812 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
813 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600814 dwc->setup_packet_pending = true;
815
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500816 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300817
818 if (r)
819 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
820
821 return;
822 }
823
Felipe Balbi6856d302014-09-30 11:43:20 -0500824 ur = &r->request;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530825 buf = ur->buf;
826 remaining_ur_length = ur->length;
Felipe Balbi6856d302014-09-30 11:43:20 -0500827
Felipe Balbif6bafc62012-02-06 11:04:53 +0200828 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300829
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530830 maxp = ep0->endpoint.maxpacket;
831
Felipe Balbia6829702011-08-27 22:18:09 +0300832 if (dwc->ep0_bounced) {
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530833 /*
834 * Handle the first TRB before handling the bounce buffer if
835 * the request length is greater than the bounce buffer size
836 */
837 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
838 transfer_size = ALIGN(ur->length - maxp, maxp);
839 transferred = transfer_size - length;
840 buf = (u8 *)buf + transferred;
841 ur->actual += transferred;
842 remaining_ur_length -= transferred;
843
844 trb++;
845 length = trb->size & DWC3_TRB_SIZE_MASK;
846
Felipe Balbi53fd8812016-04-04 15:33:41 +0300847 ep0->trb_enqueue = 0;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530848 }
849
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530850 transfer_size = roundup((ur->length - transfer_size),
851 maxp);
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530852
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530853 transferred = min_t(u32, remaining_ur_length,
854 transfer_size - length);
855 memcpy(buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300856 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200857 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300858 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300859
Felipe Balbicd423dd2012-03-21 11:44:00 +0200860 ur->actual += transferred;
861
Felipe Balbi72246da2011-08-19 18:10:58 +0300862 if ((epnum & 1) && ur->actual < ur->length) {
863 /* for some reason we did not get everything out */
864
865 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300866 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500867 dwc3_gadget_giveback(ep0, r, 0);
868
869 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
870 ur->length && ur->zero) {
871 int ret;
872
873 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
874
875 ret = dwc3_ep0_start_trans(dwc, epnum,
876 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530877 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500878 WARN_ON(ret < 0);
879 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 }
881}
882
Felipe Balbi85a78102012-05-31 12:32:37 +0300883static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 const struct dwc3_event_depevt *event)
885{
886 struct dwc3_request *r;
887 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300888 struct dwc3_trb *trb;
889 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300890
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300891 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300892 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300893
Felipe Balbiccb072d2014-10-01 12:20:29 -0500894 trace_dwc3_complete_trb(dep, trb);
895
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200896 if (!list_empty(&dep->pending_list)) {
897 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
899 dwc3_gadget_giveback(dep, r, 0);
900 }
901
Gerard Cauvy3b637362012-02-10 12:21:18 +0200902 if (dwc->test_mode) {
903 int ret;
904
905 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
906 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500907 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200908 dwc->test_mode_nr);
909 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300910 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200911 }
912 }
913
Felipe Balbifca88922012-07-19 09:05:35 +0300914 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbib5d335e2015-11-16 16:20:34 -0600915 if (status == DWC3_TRBSTS_SETUP_PENDING) {
916 dwc->setup_packet_pending = true;
Felipe Balbidab716c2014-09-24 11:22:23 -0500917 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbib5d335e2015-11-16 16:20:34 -0600918 }
Felipe Balbifca88922012-07-19 09:05:35 +0300919
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300920 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300921 dwc3_ep0_out_start(dwc);
922}
923
924static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
925 const struct dwc3_event_depevt *event)
926{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300927 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
928
929 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300930 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300931 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300932
Felipe Balbi72246da2011-08-19 18:10:58 +0300933 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300934 case EP0_SETUP_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300935 dwc3_ep0_inspect_setup(dwc, event);
936 break;
937
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300938 case EP0_DATA_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 dwc3_ep0_complete_data(dwc, event);
940 break;
941
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300942 case EP0_STATUS_PHASE:
Felipe Balbi85a78102012-05-31 12:32:37 +0300943 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300944 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300945 default:
946 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 }
948}
949
Felipe Balbia0807882012-05-04 13:03:54 +0300950static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
951 struct dwc3_ep *dep, struct dwc3_request *req)
952{
953 int ret;
954
955 req->direction = !!dep->number;
956
957 if (req->request.length == 0) {
958 ret = dwc3_ep0_start_trans(dwc, dep->number,
959 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530960 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300961 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300962 && (dep->number == 0)) {
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530963 u32 transfer_size = 0;
Andrew Mortonc390b032013-03-08 09:42:50 +0200964 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +0300965
966 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
967 dep->number);
968 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +0300969 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +0300970 return;
971 }
972
Andrew Mortonc390b032013-03-08 09:42:50 +0200973 maxpacket = dep->endpoint.maxpacket;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530974
975 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
976 transfer_size = ALIGN(req->request.length - maxpacket,
977 maxpacket);
978 ret = dwc3_ep0_start_trans(dwc, dep->number,
979 req->request.dma,
980 transfer_size,
981 DWC3_TRBCTL_CONTROL_DATA,
982 true);
983 }
984
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530985 transfer_size = roundup((req->request.length - transfer_size),
986 maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +0300987
988 dwc->ep0_bounced = true;
989
Felipe Balbia0807882012-05-04 13:03:54 +0300990 ret = dwc3_ep0_start_trans(dwc, dep->number,
991 dwc->ep0_bounce_addr, transfer_size,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530992 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbia0807882012-05-04 13:03:54 +0300993 } else {
994 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
995 dep->number);
996 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +0300997 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +0300998 return;
999 }
1000
1001 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301002 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1003 false);
Felipe Balbia0807882012-05-04 13:03:54 +03001004 }
1005
1006 WARN_ON(ret < 0);
1007}
1008
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001009static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001010{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001011 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001012 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001013
1014 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1015 : DWC3_TRBCTL_CONTROL_STATUS2;
1016
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001017 return dwc3_ep0_start_trans(dwc, dep->number,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301018 dwc->ctrl_req_addr, 0, type, false);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001019}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001020
Felipe Balbi788a23f2012-05-21 14:22:41 +03001021static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001022{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001023 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001024}
1025
Felipe Balbi788a23f2012-05-21 14:22:41 +03001026static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1027 const struct dwc3_event_depevt *event)
1028{
1029 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1030
1031 __dwc3_ep0_do_control_status(dwc, dep);
1032}
1033
Felipe Balbi2e3db062012-07-19 09:26:59 +03001034static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1035{
1036 struct dwc3_gadget_ep_cmd_params params;
1037 u32 cmd;
1038 int ret;
1039
1040 if (!dep->resource_index)
1041 return;
1042
1043 cmd = DWC3_DEPCMD_ENDTRANSFER;
1044 cmd |= DWC3_DEPCMD_CMDIOC;
1045 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1046 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001047 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi2e3db062012-07-19 09:26:59 +03001048 WARN_ON_ONCE(ret);
1049 dep->resource_index = 0;
1050}
1051
Felipe Balbi72246da2011-08-19 18:10:58 +03001052static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1053 const struct dwc3_event_depevt *event)
1054{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001055 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001056 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001057 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001058 * We already have a DATA transfer in the controller's cache,
1059 * if we receive a XferNotReady(DATA) we will ignore it, unless
1060 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001061 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001062 * In that case, we must issue END_TRANSFER command to the Data
1063 * Phase we already have started and issue SetStall on the
1064 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001065 */
1066 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001067 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1068
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001069 dwc3_trace(trace_dwc3_ep0,
1070 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001071 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001072 dwc3_ep0_stall_and_restart(dwc);
1073 return;
1074 }
1075
Felipe Balbi72246da2011-08-19 18:10:58 +03001076 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001077
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001078 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001079 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1080 return;
1081
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001082 dwc->ep0state = EP0_STATUS_PHASE;
1083
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001084 if (dwc->delayed_status) {
1085 WARN_ON_ONCE(event->endpoint_number != 1);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001086 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001087 return;
1088 }
1089
Felipe Balbi788a23f2012-05-21 14:22:41 +03001090 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001091 }
1092}
1093
1094void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001095 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001096{
Felipe Balbif75cacc2016-05-23 11:10:08 +03001097 dwc3_trace(trace_dwc3_ep0, "%s: state '%s'",
1098 dwc3_ep_event_string(event),
Felipe Balbi72246da2011-08-19 18:10:58 +03001099 dwc3_ep0_state_string(dwc->ep0state));
1100
1101 switch (event->endpoint_event) {
1102 case DWC3_DEPEVT_XFERCOMPLETE:
1103 dwc3_ep0_xfer_complete(dwc, event);
1104 break;
1105
1106 case DWC3_DEPEVT_XFERNOTREADY:
1107 dwc3_ep0_xfernotready(dwc, event);
1108 break;
1109
1110 case DWC3_DEPEVT_XFERINPROGRESS:
1111 case DWC3_DEPEVT_RXTXFIFOEVT:
1112 case DWC3_DEPEVT_STREAMEVT:
1113 case DWC3_DEPEVT_EPCMDCMPLT:
1114 break;
1115 }
1116}