blob: 3103a8f4b7e52b81e8b009bcff43f8225692399d [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 Balbi72246da2011-08-19 18:10:58 +0300102 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500103 dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed",
104 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300105 return ret;
106 }
107
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300108 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi2eb88012016-04-12 16:53:39 +0300109 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300110 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
111
Felipe Balbi72246da2011-08-19 18:10:58 +0300112 return 0;
113}
114
115static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
116 struct dwc3_request *req)
117{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100118 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300119
120 req->request.actual = 0;
121 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300122 req->epnum = dep->number;
123
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200124 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300125
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300126 /*
127 * Gadget driver might not be quick enough to queue a request
128 * before we get a Transfer Not Ready event on this endpoint.
129 *
130 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
131 * flag is set, it's telling us that as soon as Gadget queues the
132 * required request, we should kick the transfer here because the
133 * IRQ we were waiting for is long gone.
134 */
135 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300136 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300137
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300138 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300139
Felipe Balbi68d8a782011-12-29 06:32:29 +0200140 if (dwc->ep0state != EP0_DATA_PHASE) {
141 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300142 return 0;
143 }
Felipe Balbia6829702011-08-27 22:18:09 +0300144
Felipe Balbia0807882012-05-04 13:03:54 +0300145 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
146
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300147 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
148 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300149
150 return 0;
151 }
152
153 /*
154 * In case gadget driver asked us to delay the STATUS phase,
155 * handle it here.
156 */
157 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300158 unsigned direction;
159
160 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100161 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300162 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200163
164 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300165 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200166 else
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500167 dwc3_trace(trace_dwc3_ep0,
168 "too early for delayed status");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300169
170 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300171 }
172
Felipe Balbifca88922012-07-19 09:05:35 +0300173 /*
174 * Unfortunately we have uncovered a limitation wrt the Data Phase.
175 *
176 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
177 * come before issueing Start Transfer command, but if we do, we will
178 * miss situations where the host starts another SETUP phase instead of
179 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
180 * Layer Compliance Suite.
181 *
182 * The problem surfaces due to the fact that in case of back-to-back
183 * SETUP packets there will be no XferNotReady(DATA) generated and we
184 * will be stuck waiting for XferNotReady(DATA) forever.
185 *
186 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
187 * it tells us to start Data Phase right away. It also mentions that if
188 * we receive a SETUP phase instead of the DATA phase, core will issue
189 * XferComplete for the DATA phase, before actually initiating it in
190 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
191 * can only be used to print some debugging logs, as the core expects
192 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
193 * just so it completes right away, without transferring anything and,
194 * only then, we can go back to the SETUP phase.
195 *
196 * Because of this scenario, SNPS decided to change the programming
197 * model of control transfers and support on-demand transfers only for
198 * the STATUS phase. To fix the issue we have now, we will always wait
199 * for gadget driver to queue the DATA phase's struct usb_request, then
200 * start it right away.
201 *
202 * If we're actually in a 2-stage transfer, we will wait for
203 * XferNotReady(STATUS).
204 */
205 if (dwc->three_stage_setup) {
206 unsigned direction;
207
208 direction = dwc->ep0_expect_in;
209 dwc->ep0state = EP0_DATA_PHASE;
210
211 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
212
213 dep->flags &= ~DWC3_EP0_DIR_IN;
214 }
215
Felipe Balbi35f75692012-07-19 08:49:01 +0300216 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300217}
218
219int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
220 gfp_t gfp_flags)
221{
222 struct dwc3_request *req = to_dwc3_request(request);
223 struct dwc3_ep *dep = to_dwc3_ep(ep);
224 struct dwc3 *dwc = dep->dwc;
225
226 unsigned long flags;
227
228 int ret;
229
Felipe Balbi72246da2011-08-19 18:10:58 +0300230 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200231 if (!dep->endpoint.desc) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500232 dwc3_trace(trace_dwc3_ep0,
233 "trying to queue request %p to disabled %s",
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 request, dep->name);
235 ret = -ESHUTDOWN;
236 goto out;
237 }
238
239 /* we share one TRB for ep0/1 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200240 if (!list_empty(&dep->pending_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300241 ret = -EBUSY;
242 goto out;
243 }
244
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500245 dwc3_trace(trace_dwc3_ep0,
246 "queueing request %p to %s length %d state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +0300247 request, dep->name, request->length,
248 dwc3_ep0_state_string(dwc->ep0state));
249
250 ret = __dwc3_gadget_ep0_queue(dep, req);
251
252out:
253 spin_unlock_irqrestore(&dwc->lock, flags);
254
255 return ret;
256}
257
258static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
259{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300260 struct dwc3_ep *dep;
261
262 /* reinitialize physical ep1 */
263 dep = dwc->eps[1];
264 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300265
Felipe Balbi72246da2011-08-19 18:10:58 +0300266 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300267 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500268 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200269 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100270 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300271
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200272 if (!list_empty(&dep->pending_list)) {
Felipe Balbid7422202011-09-08 18:17:12 +0300273 struct dwc3_request *req;
274
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200275 req = next_request(&dep->pending_list);
Felipe Balbid7422202011-09-08 18:17:12 +0300276 dwc3_gadget_giveback(dep, req, -ECONNRESET);
277 }
278
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300279 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 dwc3_ep0_out_start(dwc);
281}
282
Felipe Balbi33fb6912014-09-24 10:46:46 -0500283int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530284{
285 struct dwc3_ep *dep = to_dwc3_ep(ep);
286 struct dwc3 *dwc = dep->dwc;
287
288 dwc3_ep0_stall_and_restart(dwc);
289
290 return 0;
291}
292
Felipe Balbi33fb6912014-09-24 10:46:46 -0500293int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
294{
295 struct dwc3_ep *dep = to_dwc3_ep(ep);
296 struct dwc3 *dwc = dep->dwc;
297 unsigned long flags;
298 int ret;
299
300 spin_lock_irqsave(&dwc->lock, flags);
301 ret = __dwc3_gadget_ep0_set_halt(ep, value);
302 spin_unlock_irqrestore(&dwc->lock, flags);
303
304 return ret;
305}
306
Felipe Balbi72246da2011-08-19 18:10:58 +0300307void dwc3_ep0_out_start(struct dwc3 *dwc)
308{
Felipe Balbi72246da2011-08-19 18:10:58 +0300309 int ret;
310
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300311 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530312 DWC3_TRBCTL_CONTROL_SETUP, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300313 WARN_ON(ret < 0);
314}
315
Felipe Balbi72246da2011-08-19 18:10:58 +0300316static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
317{
318 struct dwc3_ep *dep;
319 u32 windex = le16_to_cpu(wIndex_le);
320 u32 epnum;
321
322 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
323 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
324 epnum |= 1;
325
326 dep = dwc->eps[epnum];
327 if (dep->flags & DWC3_EP_ENABLED)
328 return dep;
329
330 return NULL;
331}
332
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200333static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300334{
Felipe Balbi72246da2011-08-19 18:10:58 +0300335}
Felipe Balbi72246da2011-08-19 18:10:58 +0300336/*
337 * ch 9.4.5
338 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200339static int dwc3_ep0_handle_status(struct dwc3 *dwc,
340 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300341{
342 struct dwc3_ep *dep;
343 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200344 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300345 u16 usb_status = 0;
346 __le16 *response_pkt;
347
348 recip = ctrl->bRequestType & USB_RECIP_MASK;
349 switch (recip) {
350 case USB_RECIP_DEVICE:
351 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200352 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300353 */
Peter Chenbcdea502015-01-28 16:32:40 +0800354 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200355
John Younee5cd412016-02-05 17:08:45 -0800356 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
357 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200358 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
359 if (reg & DWC3_DCTL_INITU1ENA)
360 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
361 if (reg & DWC3_DCTL_INITU2ENA)
362 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
363 }
364
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 break;
366
367 case USB_RECIP_INTERFACE:
368 /*
369 * Function Remote Wake Capable D0
370 * Function Remote Wakeup D1
371 */
372 break;
373
374 case USB_RECIP_ENDPOINT:
375 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
376 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200377 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300378
379 if (dep->flags & DWC3_EP_STALL)
380 usb_status = 1 << USB_ENDPOINT_HALT;
381 break;
382 default:
383 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700384 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300385
386 response_pkt = (__le16 *) dwc->setup_buf;
387 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200388
389 dep = dwc->eps[0];
390 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100391 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200392 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100393 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200394
395 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300396}
397
398static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
399 struct usb_ctrlrequest *ctrl, int set)
400{
401 struct dwc3_ep *dep;
402 u32 recip;
403 u32 wValue;
404 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200405 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 int ret;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200407 enum usb_device_state state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408
409 wValue = le16_to_cpu(ctrl->wValue);
410 wIndex = le16_to_cpu(ctrl->wIndex);
411 recip = ctrl->bRequestType & USB_RECIP_MASK;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200412 state = dwc->gadget.state;
413
Felipe Balbi72246da2011-08-19 18:10:58 +0300414 switch (recip) {
415 case USB_RECIP_DEVICE:
416
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200417 switch (wValue) {
418 case USB_DEVICE_REMOTE_WAKEUP:
419 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300420 /*
421 * 9.4.1 says only only for SS, in AddressState only for
422 * default control pipe
423 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 case USB_DEVICE_U1_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200425 if (state != USB_STATE_CONFIGURED)
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 return -EINVAL;
John Younee5cd412016-02-05 17:08:45 -0800427 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
428 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300430
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200431 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
432 if (set)
433 reg |= DWC3_DCTL_INITU1ENA;
434 else
435 reg &= ~DWC3_DCTL_INITU1ENA;
436 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200438
Felipe Balbi72246da2011-08-19 18:10:58 +0300439 case USB_DEVICE_U2_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200440 if (state != USB_STATE_CONFIGURED)
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200441 return -EINVAL;
John Younee5cd412016-02-05 17:08:45 -0800442 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
443 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200444 return -EINVAL;
445
446 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
447 if (set)
448 reg |= DWC3_DCTL_INITU2ENA;
449 else
450 reg &= ~DWC3_DCTL_INITU2ENA;
451 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200453
Felipe Balbi72246da2011-08-19 18:10:58 +0300454 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200455 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
457 case USB_DEVICE_TEST_MODE:
458 if ((wIndex & 0xff) != 0)
459 return -EINVAL;
460 if (!set)
461 return -EINVAL;
462
Fei Yangae411412016-04-20 09:08:43 +0300463 switch (wIndex >> 8) {
464 case TEST_J:
465 case TEST_K:
466 case TEST_SE0_NAK:
467 case TEST_PACKET:
468 case TEST_FORCE_EN:
469 dwc->test_mode_nr = wIndex >> 8;
470 dwc->test_mode = true;
471 break;
472 default:
473 return -EINVAL;
474 }
Gerard Cauvyecb07792012-03-16 16:20:10 +0200475 break;
476 default:
477 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 }
479 break;
480
481 case USB_RECIP_INTERFACE:
482 switch (wValue) {
483 case USB_INTRF_FUNC_SUSPEND:
484 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
485 /* XXX enable Low power suspend */
486 ;
487 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
488 /* XXX enable remote wakeup */
489 ;
490 break;
491 default:
492 return -EINVAL;
493 }
494 break;
495
496 case USB_RECIP_ENDPOINT:
497 switch (wValue) {
498 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800499 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300500 if (!dep)
501 return -EINVAL;
Alan Sterna535d812013-11-01 12:05:12 -0400502 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
503 break;
Felipe Balbi7a608552014-09-24 14:19:52 -0500504 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300505 if (ret)
506 return -EINVAL;
507 break;
508 default:
509 return -EINVAL;
510 }
511 break;
512
513 default:
514 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700515 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300516
Felipe Balbi72246da2011-08-19 18:10:58 +0300517 return 0;
518}
519
520static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
521{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200522 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300523 u32 addr;
524 u32 reg;
525
526 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300527 if (addr > 127) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500528 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300529 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300530 }
531
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200532 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500533 dwc3_trace(trace_dwc3_ep0,
534 "trying to set address when configured");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300535 return -EINVAL;
536 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300537
Felipe Balbi26460212011-09-30 10:58:36 +0300538 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
539 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
540 reg |= DWC3_DCFG_DEVADDR(addr);
541 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200543 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200544 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200545 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200546 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300547
Felipe Balbi26460212011-09-30 10:58:36 +0300548 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549}
550
551static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
552{
553 int ret;
554
555 spin_unlock(&dwc->lock);
556 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
557 spin_lock(&dwc->lock);
558 return ret;
559}
560
561static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
562{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200563 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 u32 cfg;
565 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530566 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567
568 cfg = le16_to_cpu(ctrl->wValue);
569
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200570 switch (state) {
571 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200574 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300575 ret = dwc3_ep0_delegate_req(dwc, ctrl);
576 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200577 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300578
579 /*
580 * only change state if set_config has already
581 * been processed. If gadget driver returns
582 * USB_GADGET_DELAYED_STATUS, we will wait
583 * to change the state on the next usb_ep_queue()
584 */
585 if (ret == 0)
586 usb_gadget_set_state(&dwc->gadget,
587 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200588
Pratyush Anande274a312012-07-02 10:21:54 +0530589 /*
590 * Enable transition to U1/U2 state when
591 * nothing is pending from application.
592 */
593 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
594 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
595 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200596 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 break;
598
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200599 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300600 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300601 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200602 usb_gadget_set_state(&dwc->gadget,
603 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100605 default:
606 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100608 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300609}
610
Felipe Balbi865e09e2012-04-24 16:19:49 +0300611static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
612{
613 struct dwc3_ep *dep = to_dwc3_ep(ep);
614 struct dwc3 *dwc = dep->dwc;
615
616 u32 param = 0;
617 u32 reg;
618
619 struct timing {
620 u8 u1sel;
621 u8 u1pel;
622 u16 u2sel;
623 u16 u2pel;
624 } __packed timing;
625
626 int ret;
627
628 memcpy(&timing, req->buf, sizeof(timing));
629
630 dwc->u1sel = timing.u1sel;
631 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300632 dwc->u2sel = le16_to_cpu(timing.u2sel);
633 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300634
635 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
636 if (reg & DWC3_DCTL_INITU2ENA)
637 param = dwc->u2pel;
638 if (reg & DWC3_DCTL_INITU1ENA)
639 param = dwc->u1pel;
640
641 /*
642 * According to Synopsys Databook, if parameter is
643 * greater than 125, a value of zero should be
644 * programmed in the register.
645 */
646 if (param > 125)
647 param = 0;
648
649 /* now that we have the time, issue DGCMD Set Sel */
650 ret = dwc3_send_gadget_generic_command(dwc,
651 DWC3_DGCMD_SET_PERIODIC_PAR, param);
652 WARN_ON(ret < 0);
653}
654
655static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
656{
657 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200658 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300659 u16 wLength;
660 u16 wValue;
661
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200662 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300663 return -EINVAL;
664
665 wValue = le16_to_cpu(ctrl->wValue);
666 wLength = le16_to_cpu(ctrl->wLength);
667
668 if (wLength != 6) {
669 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
670 wLength);
671 return -EINVAL;
672 }
673
674 /*
675 * To handle Set SEL we need to receive 6 bytes from Host. So let's
676 * queue a usb_request for 6 bytes.
677 *
678 * Remember, though, this controller can't handle non-wMaxPacketSize
679 * aligned transfers on the OUT direction, so we queue a request for
680 * wMaxPacketSize instead.
681 */
682 dep = dwc->eps[0];
683 dwc->ep0_usb_req.dep = dep;
684 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
685 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
686 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
687
688 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
689}
690
Felipe Balbic12a0d82012-04-25 10:45:05 +0300691static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
692{
693 u16 wLength;
694 u16 wValue;
695 u16 wIndex;
696
697 wValue = le16_to_cpu(ctrl->wValue);
698 wLength = le16_to_cpu(ctrl->wLength);
699 wIndex = le16_to_cpu(ctrl->wIndex);
700
701 if (wIndex || wLength)
702 return -EINVAL;
703
704 /*
705 * REVISIT It's unclear from Databook what to do with this
706 * value. For now, just cache it.
707 */
708 dwc->isoch_delay = wValue;
709
710 return 0;
711}
712
Felipe Balbi72246da2011-08-19 18:10:58 +0300713static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
714{
715 int ret;
716
717 switch (ctrl->bRequest) {
718 case USB_REQ_GET_STATUS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500719 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 ret = dwc3_ep0_handle_status(dwc, ctrl);
721 break;
722 case USB_REQ_CLEAR_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500723 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300724 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
725 break;
726 case USB_REQ_SET_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500727 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
729 break;
730 case USB_REQ_SET_ADDRESS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500731 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300732 ret = dwc3_ep0_set_address(dwc, ctrl);
733 break;
734 case USB_REQ_SET_CONFIGURATION:
Felipe Balbidab716c2014-09-24 11:22:23 -0500735 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
Felipe Balbi72246da2011-08-19 18:10:58 +0300736 ret = dwc3_ep0_set_config(dwc, ctrl);
737 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300738 case USB_REQ_SET_SEL:
Felipe Balbidab716c2014-09-24 11:22:23 -0500739 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300740 ret = dwc3_ep0_set_sel(dwc, ctrl);
741 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300742 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbidab716c2014-09-24 11:22:23 -0500743 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300744 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
745 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 default:
Felipe Balbidab716c2014-09-24 11:22:23 -0500747 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
Felipe Balbi72246da2011-08-19 18:10:58 +0300748 ret = dwc3_ep0_delegate_req(dwc, ctrl);
749 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700750 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300751
752 return ret;
753}
754
755static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
756 const struct dwc3_event_depevt *event)
757{
758 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300759 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 u32 len;
761
762 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300763 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300764
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500765 trace_dwc3_ctrl_req(ctrl);
766
Felipe Balbi72246da2011-08-19 18:10:58 +0300767 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300768 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300769 dwc->three_stage_setup = false;
770 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300771 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
772 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300773 dwc->three_stage_setup = true;
774 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300775 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
776 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300777
778 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
779 ret = dwc3_ep0_std_request(dwc, ctrl);
780 else
781 ret = dwc3_ep0_delegate_req(dwc, ctrl);
782
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100783 if (ret == USB_GADGET_DELAYED_STATUS)
784 dwc->delayed_status = true;
785
Felipe Balbief21ede2012-05-31 10:29:49 +0300786out:
787 if (ret < 0)
788 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300789}
790
791static void dwc3_ep0_complete_data(struct dwc3 *dwc,
792 const struct dwc3_event_depevt *event)
793{
794 struct dwc3_request *r = NULL;
795 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200797 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530798 unsigned transfer_size = 0;
799 unsigned maxp;
800 unsigned remaining_ur_length;
801 void *buf;
802 u32 transferred = 0;
Felipe Balbifca88922012-07-19 09:05:35 +0300803 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200804 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 u8 epnum;
806
807 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200808 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300809
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300810 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
811
Felipe Balbif6bafc62012-02-06 11:04:53 +0200812 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300813
Felipe Balbiccb072d2014-10-01 12:20:29 -0500814 trace_dwc3_complete_trb(ep0, trb);
815
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200816 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600817 if (!r)
818 return;
819
Felipe Balbifca88922012-07-19 09:05:35 +0300820 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
821 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600822 dwc->setup_packet_pending = true;
823
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500824 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300825
826 if (r)
827 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
828
829 return;
830 }
831
Felipe Balbi6856d302014-09-30 11:43:20 -0500832 ur = &r->request;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530833 buf = ur->buf;
834 remaining_ur_length = ur->length;
Felipe Balbi6856d302014-09-30 11:43:20 -0500835
Felipe Balbif6bafc62012-02-06 11:04:53 +0200836 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300837
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530838 maxp = ep0->endpoint.maxpacket;
839
Felipe Balbia6829702011-08-27 22:18:09 +0300840 if (dwc->ep0_bounced) {
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530841 /*
842 * Handle the first TRB before handling the bounce buffer if
843 * the request length is greater than the bounce buffer size
844 */
845 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
846 transfer_size = ALIGN(ur->length - maxp, maxp);
847 transferred = transfer_size - length;
848 buf = (u8 *)buf + transferred;
849 ur->actual += transferred;
850 remaining_ur_length -= transferred;
851
852 trb++;
853 length = trb->size & DWC3_TRB_SIZE_MASK;
854
Felipe Balbi53fd8812016-04-04 15:33:41 +0300855 ep0->trb_enqueue = 0;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530856 }
857
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530858 transfer_size = roundup((ur->length - transfer_size),
859 maxp);
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530860
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530861 transferred = min_t(u32, remaining_ur_length,
862 transfer_size - length);
863 memcpy(buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300864 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200865 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300866 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300867
Felipe Balbicd423dd2012-03-21 11:44:00 +0200868 ur->actual += transferred;
869
Felipe Balbi72246da2011-08-19 18:10:58 +0300870 if ((epnum & 1) && ur->actual < ur->length) {
871 /* for some reason we did not get everything out */
872
873 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500875 dwc3_gadget_giveback(ep0, r, 0);
876
877 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
878 ur->length && ur->zero) {
879 int ret;
880
881 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
882
883 ret = dwc3_ep0_start_trans(dwc, epnum,
884 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530885 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500886 WARN_ON(ret < 0);
887 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300888 }
889}
890
Felipe Balbi85a78102012-05-31 12:32:37 +0300891static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300892 const struct dwc3_event_depevt *event)
893{
894 struct dwc3_request *r;
895 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300896 struct dwc3_trb *trb;
897 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300899 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300900 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
Felipe Balbiccb072d2014-10-01 12:20:29 -0500902 trace_dwc3_complete_trb(dep, trb);
903
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200904 if (!list_empty(&dep->pending_list)) {
905 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300906
907 dwc3_gadget_giveback(dep, r, 0);
908 }
909
Gerard Cauvy3b637362012-02-10 12:21:18 +0200910 if (dwc->test_mode) {
911 int ret;
912
913 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
914 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500915 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200916 dwc->test_mode_nr);
917 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300918 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200919 }
920 }
921
Felipe Balbifca88922012-07-19 09:05:35 +0300922 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbib5d335e2015-11-16 16:20:34 -0600923 if (status == DWC3_TRBSTS_SETUP_PENDING) {
924 dwc->setup_packet_pending = true;
Felipe Balbidab716c2014-09-24 11:22:23 -0500925 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbib5d335e2015-11-16 16:20:34 -0600926 }
Felipe Balbifca88922012-07-19 09:05:35 +0300927
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300928 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300929 dwc3_ep0_out_start(dwc);
930}
931
932static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
933 const struct dwc3_event_depevt *event)
934{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300935 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
936
937 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300938 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300939 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300940
Felipe Balbi72246da2011-08-19 18:10:58 +0300941 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300942 case EP0_SETUP_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500943 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300944 dwc3_ep0_inspect_setup(dwc, event);
945 break;
946
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300947 case EP0_DATA_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500948 dwc3_trace(trace_dwc3_ep0, "Data Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300949 dwc3_ep0_complete_data(dwc, event);
950 break;
951
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300952 case EP0_STATUS_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500953 dwc3_trace(trace_dwc3_ep0, "Status Phase");
Felipe Balbi85a78102012-05-31 12:32:37 +0300954 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300955 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300956 default:
957 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 }
959}
960
Felipe Balbia0807882012-05-04 13:03:54 +0300961static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
962 struct dwc3_ep *dep, struct dwc3_request *req)
963{
964 int ret;
965
966 req->direction = !!dep->number;
967
968 if (req->request.length == 0) {
969 ret = dwc3_ep0_start_trans(dwc, dep->number,
970 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530971 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300972 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300973 && (dep->number == 0)) {
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530974 u32 transfer_size = 0;
Andrew Mortonc390b032013-03-08 09:42:50 +0200975 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +0300976
977 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
978 dep->number);
979 if (ret) {
Felipe Balbiacc38c42015-11-16 16:08:09 -0600980 dwc3_trace(trace_dwc3_ep0, "failed to map request\n");
Felipe Balbia0807882012-05-04 13:03:54 +0300981 return;
982 }
983
Andrew Mortonc390b032013-03-08 09:42:50 +0200984 maxpacket = dep->endpoint.maxpacket;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530985
986 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
987 transfer_size = ALIGN(req->request.length - maxpacket,
988 maxpacket);
989 ret = dwc3_ep0_start_trans(dwc, dep->number,
990 req->request.dma,
991 transfer_size,
992 DWC3_TRBCTL_CONTROL_DATA,
993 true);
994 }
995
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530996 transfer_size = roundup((req->request.length - transfer_size),
997 maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +0300998
999 dwc->ep0_bounced = true;
1000
Felipe Balbia0807882012-05-04 13:03:54 +03001001 ret = dwc3_ep0_start_trans(dwc, dep->number,
1002 dwc->ep0_bounce_addr, transfer_size,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301003 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbia0807882012-05-04 13:03:54 +03001004 } else {
1005 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1006 dep->number);
1007 if (ret) {
Felipe Balbiacc38c42015-11-16 16:08:09 -06001008 dwc3_trace(trace_dwc3_ep0, "failed to map request\n");
Felipe Balbia0807882012-05-04 13:03:54 +03001009 return;
1010 }
1011
1012 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301013 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1014 false);
Felipe Balbia0807882012-05-04 13:03:54 +03001015 }
1016
1017 WARN_ON(ret < 0);
1018}
1019
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001020static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001021{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001022 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001023 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001024
1025 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1026 : DWC3_TRBCTL_CONTROL_STATUS2;
1027
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001028 return dwc3_ep0_start_trans(dwc, dep->number,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301029 dwc->ctrl_req_addr, 0, type, false);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001030}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001031
Felipe Balbi788a23f2012-05-21 14:22:41 +03001032static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001033{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001034 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001035}
1036
Felipe Balbi788a23f2012-05-21 14:22:41 +03001037static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1038 const struct dwc3_event_depevt *event)
1039{
1040 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1041
1042 __dwc3_ep0_do_control_status(dwc, dep);
1043}
1044
Felipe Balbi2e3db062012-07-19 09:26:59 +03001045static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1046{
1047 struct dwc3_gadget_ep_cmd_params params;
1048 u32 cmd;
1049 int ret;
1050
1051 if (!dep->resource_index)
1052 return;
1053
1054 cmd = DWC3_DEPCMD_ENDTRANSFER;
1055 cmd |= DWC3_DEPCMD_CMDIOC;
1056 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1057 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001058 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi2e3db062012-07-19 09:26:59 +03001059 WARN_ON_ONCE(ret);
1060 dep->resource_index = 0;
1061}
1062
Felipe Balbi72246da2011-08-19 18:10:58 +03001063static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1064 const struct dwc3_event_depevt *event)
1065{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001066 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001067 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001068 dwc3_trace(trace_dwc3_ep0, "Control Data");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001069
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001070 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001071 * We already have a DATA transfer in the controller's cache,
1072 * if we receive a XferNotReady(DATA) we will ignore it, unless
1073 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001074 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001075 * In that case, we must issue END_TRANSFER command to the Data
1076 * Phase we already have started and issue SetStall on the
1077 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001078 */
1079 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001080 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1081
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001082 dwc3_trace(trace_dwc3_ep0,
1083 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001084 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001085 dwc3_ep0_stall_and_restart(dwc);
1086 return;
1087 }
1088
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001090
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001091 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001092 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1093 return;
1094
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001095 dwc3_trace(trace_dwc3_ep0, "Control Status");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001096
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001097 dwc->ep0state = EP0_STATUS_PHASE;
1098
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001099 if (dwc->delayed_status) {
1100 WARN_ON_ONCE(event->endpoint_number != 1);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001101 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001102 return;
1103 }
1104
Felipe Balbi788a23f2012-05-21 14:22:41 +03001105 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001106 }
1107}
1108
1109void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001110 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001111{
1112 u8 epnum = event->endpoint_number;
1113
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001114 dwc3_trace(trace_dwc3_ep0, "%s while ep%d%s in state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +03001115 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001116 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001117 dwc3_ep0_state_string(dwc->ep0state));
1118
1119 switch (event->endpoint_event) {
1120 case DWC3_DEPEVT_XFERCOMPLETE:
1121 dwc3_ep0_xfer_complete(dwc, event);
1122 break;
1123
1124 case DWC3_DEPEVT_XFERNOTREADY:
1125 dwc3_ep0_xfernotready(dwc, event);
1126 break;
1127
1128 case DWC3_DEPEVT_XFERINPROGRESS:
1129 case DWC3_DEPEVT_RXTXFIFOEVT:
1130 case DWC3_DEPEVT_STREAMEVT:
1131 case DWC3_DEPEVT_EPCMDCMPLT:
1132 break;
1133 }
1134}