blob: 06ecd1e6871ce7c2223525e988c866db0ab16986 [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,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030059 u32 len, u32 type)
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 Balbif6bafc62012-02-06 11:04:53 +020073 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030074
Felipe Balbif6bafc62012-02-06 11:04:53 +020075 trb->bpl = lower_32_bits(buf_dma);
76 trb->bph = upper_32_bits(buf_dma);
77 trb->size = len;
78 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030079
Felipe Balbif6bafc62012-02-06 11:04:53 +020080 trb->ctrl |= (DWC3_TRB_CTRL_HWO
81 | DWC3_TRB_CTRL_LST
82 | DWC3_TRB_CTRL_IOC
83 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030084
85 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030086 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
87 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030088
Felipe Balbiccb072d2014-10-01 12:20:29 -050089 trace_dwc3_prepare_trb(dep, trb);
90
Felipe Balbi72246da2011-08-19 18:10:58 +030091 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
92 DWC3_DEPCMD_STARTTRANSFER, &params);
93 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -050094 dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed",
95 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +030096 return ret;
97 }
98
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030099 dep->flags |= DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300100 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300101 dep->number);
102
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300103 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
104
Felipe Balbi72246da2011-08-19 18:10:58 +0300105 return 0;
106}
107
108static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
109 struct dwc3_request *req)
110{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100111 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300112
113 req->request.actual = 0;
114 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300115 req->epnum = dep->number;
116
117 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300118
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300119 /*
120 * Gadget driver might not be quick enough to queue a request
121 * before we get a Transfer Not Ready event on this endpoint.
122 *
123 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
124 * flag is set, it's telling us that as soon as Gadget queues the
125 * required request, we should kick the transfer here because the
126 * IRQ we were waiting for is long gone.
127 */
128 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300129 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300130
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300131 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300132
Felipe Balbi68d8a782011-12-29 06:32:29 +0200133 if (dwc->ep0state != EP0_DATA_PHASE) {
134 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300135 return 0;
136 }
Felipe Balbia6829702011-08-27 22:18:09 +0300137
Felipe Balbia0807882012-05-04 13:03:54 +0300138 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
139
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300140 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
141 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300142
143 return 0;
144 }
145
146 /*
147 * In case gadget driver asked us to delay the STATUS phase,
148 * handle it here.
149 */
150 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300151 unsigned direction;
152
153 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100154 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300155 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200156
157 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300158 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200159 else
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500160 dwc3_trace(trace_dwc3_ep0,
161 "too early for delayed status");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300162
163 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300164 }
165
Felipe Balbifca88922012-07-19 09:05:35 +0300166 /*
167 * Unfortunately we have uncovered a limitation wrt the Data Phase.
168 *
169 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
170 * come before issueing Start Transfer command, but if we do, we will
171 * miss situations where the host starts another SETUP phase instead of
172 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
173 * Layer Compliance Suite.
174 *
175 * The problem surfaces due to the fact that in case of back-to-back
176 * SETUP packets there will be no XferNotReady(DATA) generated and we
177 * will be stuck waiting for XferNotReady(DATA) forever.
178 *
179 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
180 * it tells us to start Data Phase right away. It also mentions that if
181 * we receive a SETUP phase instead of the DATA phase, core will issue
182 * XferComplete for the DATA phase, before actually initiating it in
183 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
184 * can only be used to print some debugging logs, as the core expects
185 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
186 * just so it completes right away, without transferring anything and,
187 * only then, we can go back to the SETUP phase.
188 *
189 * Because of this scenario, SNPS decided to change the programming
190 * model of control transfers and support on-demand transfers only for
191 * the STATUS phase. To fix the issue we have now, we will always wait
192 * for gadget driver to queue the DATA phase's struct usb_request, then
193 * start it right away.
194 *
195 * If we're actually in a 2-stage transfer, we will wait for
196 * XferNotReady(STATUS).
197 */
198 if (dwc->three_stage_setup) {
199 unsigned direction;
200
201 direction = dwc->ep0_expect_in;
202 dwc->ep0state = EP0_DATA_PHASE;
203
204 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
205
206 dep->flags &= ~DWC3_EP0_DIR_IN;
207 }
208
Felipe Balbi35f75692012-07-19 08:49:01 +0300209 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300210}
211
212int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
213 gfp_t gfp_flags)
214{
215 struct dwc3_request *req = to_dwc3_request(request);
216 struct dwc3_ep *dep = to_dwc3_ep(ep);
217 struct dwc3 *dwc = dep->dwc;
218
219 unsigned long flags;
220
221 int ret;
222
Felipe Balbi72246da2011-08-19 18:10:58 +0300223 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200224 if (!dep->endpoint.desc) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500225 dwc3_trace(trace_dwc3_ep0,
226 "trying to queue request %p to disabled %s",
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 request, dep->name);
228 ret = -ESHUTDOWN;
229 goto out;
230 }
231
232 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200233 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 ret = -EBUSY;
235 goto out;
236 }
237
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500238 dwc3_trace(trace_dwc3_ep0,
239 "queueing request %p to %s length %d state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 request, dep->name, request->length,
241 dwc3_ep0_state_string(dwc->ep0state));
242
243 ret = __dwc3_gadget_ep0_queue(dep, req);
244
245out:
246 spin_unlock_irqrestore(&dwc->lock, flags);
247
248 return ret;
249}
250
251static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
252{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300253 struct dwc3_ep *dep;
254
255 /* reinitialize physical ep1 */
256 dep = dwc->eps[1];
257 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300258
Felipe Balbi72246da2011-08-19 18:10:58 +0300259 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300260 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500261 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200262 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100263 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300264
265 if (!list_empty(&dep->request_list)) {
266 struct dwc3_request *req;
267
268 req = next_request(&dep->request_list);
269 dwc3_gadget_giveback(dep, req, -ECONNRESET);
270 }
271
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300272 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 dwc3_ep0_out_start(dwc);
274}
275
Felipe Balbi33fb6912014-09-24 10:46:46 -0500276int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530277{
278 struct dwc3_ep *dep = to_dwc3_ep(ep);
279 struct dwc3 *dwc = dep->dwc;
280
281 dwc3_ep0_stall_and_restart(dwc);
282
283 return 0;
284}
285
Felipe Balbi33fb6912014-09-24 10:46:46 -0500286int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
287{
288 struct dwc3_ep *dep = to_dwc3_ep(ep);
289 struct dwc3 *dwc = dep->dwc;
290 unsigned long flags;
291 int ret;
292
293 spin_lock_irqsave(&dwc->lock, flags);
294 ret = __dwc3_gadget_ep0_set_halt(ep, value);
295 spin_unlock_irqrestore(&dwc->lock, flags);
296
297 return ret;
298}
299
Felipe Balbi72246da2011-08-19 18:10:58 +0300300void dwc3_ep0_out_start(struct dwc3 *dwc)
301{
Felipe Balbi72246da2011-08-19 18:10:58 +0300302 int ret;
303
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300304 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
305 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300306 WARN_ON(ret < 0);
307}
308
Felipe Balbi72246da2011-08-19 18:10:58 +0300309static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
310{
311 struct dwc3_ep *dep;
312 u32 windex = le16_to_cpu(wIndex_le);
313 u32 epnum;
314
315 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
316 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
317 epnum |= 1;
318
319 dep = dwc->eps[epnum];
320 if (dep->flags & DWC3_EP_ENABLED)
321 return dep;
322
323 return NULL;
324}
325
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200326static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300327{
Felipe Balbi72246da2011-08-19 18:10:58 +0300328}
Felipe Balbi72246da2011-08-19 18:10:58 +0300329/*
330 * ch 9.4.5
331 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200332static int dwc3_ep0_handle_status(struct dwc3 *dwc,
333 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300334{
335 struct dwc3_ep *dep;
336 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200337 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 u16 usb_status = 0;
339 __le16 *response_pkt;
340
341 recip = ctrl->bRequestType & USB_RECIP_MASK;
342 switch (recip) {
343 case USB_RECIP_DEVICE:
344 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200345 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300346 */
Peter Chenbcdea502015-01-28 16:32:40 +0800347 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200348
349 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
350 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;
419 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
420 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300421
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200422 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
423 if (set)
424 reg |= DWC3_DCTL_INITU1ENA;
425 else
426 reg &= ~DWC3_DCTL_INITU1ENA;
427 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300428 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200429
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 case USB_DEVICE_U2_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200431 if (state != USB_STATE_CONFIGURED)
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200432 return -EINVAL;
433 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
434 return -EINVAL;
435
436 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
437 if (set)
438 reg |= DWC3_DCTL_INITU2ENA;
439 else
440 reg &= ~DWC3_DCTL_INITU2ENA;
441 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300442 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200443
Felipe Balbi72246da2011-08-19 18:10:58 +0300444 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200445 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300446
447 case USB_DEVICE_TEST_MODE:
448 if ((wIndex & 0xff) != 0)
449 return -EINVAL;
450 if (!set)
451 return -EINVAL;
452
Gerard Cauvy3b637362012-02-10 12:21:18 +0200453 dwc->test_mode_nr = wIndex >> 8;
454 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200455 break;
456 default:
457 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300458 }
459 break;
460
461 case USB_RECIP_INTERFACE:
462 switch (wValue) {
463 case USB_INTRF_FUNC_SUSPEND:
464 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
465 /* XXX enable Low power suspend */
466 ;
467 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
468 /* XXX enable remote wakeup */
469 ;
470 break;
471 default:
472 return -EINVAL;
473 }
474 break;
475
476 case USB_RECIP_ENDPOINT:
477 switch (wValue) {
478 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800479 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 if (!dep)
481 return -EINVAL;
Alan Sterna535d812013-11-01 12:05:12 -0400482 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
483 break;
Felipe Balbi7a608552014-09-24 14:19:52 -0500484 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300485 if (ret)
486 return -EINVAL;
487 break;
488 default:
489 return -EINVAL;
490 }
491 break;
492
493 default:
494 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700495 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300496
Felipe Balbi72246da2011-08-19 18:10:58 +0300497 return 0;
498}
499
500static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
501{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200502 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503 u32 addr;
504 u32 reg;
505
506 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300507 if (addr > 127) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500508 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300509 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300510 }
511
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200512 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500513 dwc3_trace(trace_dwc3_ep0,
514 "trying to set address when configured");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300515 return -EINVAL;
516 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
Felipe Balbi26460212011-09-30 10:58:36 +0300518 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
519 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
520 reg |= DWC3_DCFG_DEVADDR(addr);
521 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300522
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200523 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200524 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200525 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200526 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300527
Felipe Balbi26460212011-09-30 10:58:36 +0300528 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300529}
530
531static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
532{
533 int ret;
534
535 spin_unlock(&dwc->lock);
536 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
537 spin_lock(&dwc->lock);
538 return ret;
539}
540
541static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
542{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200543 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 u32 cfg;
545 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530546 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300547
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300548 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549 cfg = le16_to_cpu(ctrl->wValue);
550
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200551 switch (state) {
552 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200555 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300556 ret = dwc3_ep0_delegate_req(dwc, ctrl);
557 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200558 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300559
560 /*
561 * only change state if set_config has already
562 * been processed. If gadget driver returns
563 * USB_GADGET_DELAYED_STATUS, we will wait
564 * to change the state on the next usb_ep_queue()
565 */
566 if (ret == 0)
567 usb_gadget_set_state(&dwc->gadget,
568 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200569
Pratyush Anande274a312012-07-02 10:21:54 +0530570 /*
571 * Enable transition to U1/U2 state when
572 * nothing is pending from application.
573 */
574 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
575 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
576 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
577
Felipe Balbi457e84b2012-01-18 18:04:09 +0200578 dwc->resize_fifos = true;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500579 dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200580 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 break;
582
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200583 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300585 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200586 usb_gadget_set_state(&dwc->gadget,
587 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100589 default:
590 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100592 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300593}
594
Felipe Balbi865e09e2012-04-24 16:19:49 +0300595static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
596{
597 struct dwc3_ep *dep = to_dwc3_ep(ep);
598 struct dwc3 *dwc = dep->dwc;
599
600 u32 param = 0;
601 u32 reg;
602
603 struct timing {
604 u8 u1sel;
605 u8 u1pel;
606 u16 u2sel;
607 u16 u2pel;
608 } __packed timing;
609
610 int ret;
611
612 memcpy(&timing, req->buf, sizeof(timing));
613
614 dwc->u1sel = timing.u1sel;
615 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300616 dwc->u2sel = le16_to_cpu(timing.u2sel);
617 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300618
619 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
620 if (reg & DWC3_DCTL_INITU2ENA)
621 param = dwc->u2pel;
622 if (reg & DWC3_DCTL_INITU1ENA)
623 param = dwc->u1pel;
624
625 /*
626 * According to Synopsys Databook, if parameter is
627 * greater than 125, a value of zero should be
628 * programmed in the register.
629 */
630 if (param > 125)
631 param = 0;
632
633 /* now that we have the time, issue DGCMD Set Sel */
634 ret = dwc3_send_gadget_generic_command(dwc,
635 DWC3_DGCMD_SET_PERIODIC_PAR, param);
636 WARN_ON(ret < 0);
637}
638
639static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
640{
641 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200642 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300643 u16 wLength;
644 u16 wValue;
645
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200646 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300647 return -EINVAL;
648
649 wValue = le16_to_cpu(ctrl->wValue);
650 wLength = le16_to_cpu(ctrl->wLength);
651
652 if (wLength != 6) {
653 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
654 wLength);
655 return -EINVAL;
656 }
657
658 /*
659 * To handle Set SEL we need to receive 6 bytes from Host. So let's
660 * queue a usb_request for 6 bytes.
661 *
662 * Remember, though, this controller can't handle non-wMaxPacketSize
663 * aligned transfers on the OUT direction, so we queue a request for
664 * wMaxPacketSize instead.
665 */
666 dep = dwc->eps[0];
667 dwc->ep0_usb_req.dep = dep;
668 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
669 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
670 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
671
672 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
673}
674
Felipe Balbic12a0d82012-04-25 10:45:05 +0300675static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
676{
677 u16 wLength;
678 u16 wValue;
679 u16 wIndex;
680
681 wValue = le16_to_cpu(ctrl->wValue);
682 wLength = le16_to_cpu(ctrl->wLength);
683 wIndex = le16_to_cpu(ctrl->wIndex);
684
685 if (wIndex || wLength)
686 return -EINVAL;
687
688 /*
689 * REVISIT It's unclear from Databook what to do with this
690 * value. For now, just cache it.
691 */
692 dwc->isoch_delay = wValue;
693
694 return 0;
695}
696
Felipe Balbi72246da2011-08-19 18:10:58 +0300697static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
698{
699 int ret;
700
701 switch (ctrl->bRequest) {
702 case USB_REQ_GET_STATUS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500703 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300704 ret = dwc3_ep0_handle_status(dwc, ctrl);
705 break;
706 case USB_REQ_CLEAR_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500707 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
709 break;
710 case USB_REQ_SET_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500711 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300712 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
713 break;
714 case USB_REQ_SET_ADDRESS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500715 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 ret = dwc3_ep0_set_address(dwc, ctrl);
717 break;
718 case USB_REQ_SET_CONFIGURATION:
Felipe Balbidab716c2014-09-24 11:22:23 -0500719 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 ret = dwc3_ep0_set_config(dwc, ctrl);
721 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300722 case USB_REQ_SET_SEL:
Felipe Balbidab716c2014-09-24 11:22:23 -0500723 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300724 ret = dwc3_ep0_set_sel(dwc, ctrl);
725 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300726 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbidab716c2014-09-24 11:22:23 -0500727 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300728 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
729 break;
John Younaebda612001-09-17 00:00:00 -0700730 case USB_REQ_SET_INTERFACE:
731 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_INTERFACE");
732 dwc->start_config_issued = false;
733 /* Fall through */
Felipe Balbi72246da2011-08-19 18:10:58 +0300734 default:
Felipe Balbidab716c2014-09-24 11:22:23 -0500735 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
Felipe Balbi72246da2011-08-19 18:10:58 +0300736 ret = dwc3_ep0_delegate_req(dwc, ctrl);
737 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700738 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300739
740 return ret;
741}
742
743static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
744 const struct dwc3_event_depevt *event)
745{
746 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300747 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300748 u32 len;
749
750 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300751 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300752
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500753 trace_dwc3_ctrl_req(ctrl);
754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300756 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300757 dwc->three_stage_setup = false;
758 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300759 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
760 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300761 dwc->three_stage_setup = true;
762 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300763 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
764 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300765
766 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
767 ret = dwc3_ep0_std_request(dwc, ctrl);
768 else
769 ret = dwc3_ep0_delegate_req(dwc, ctrl);
770
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100771 if (ret == USB_GADGET_DELAYED_STATUS)
772 dwc->delayed_status = true;
773
Felipe Balbief21ede2012-05-31 10:29:49 +0300774out:
775 if (ret < 0)
776 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300777}
778
779static void dwc3_ep0_complete_data(struct dwc3 *dwc,
780 const struct dwc3_event_depevt *event)
781{
782 struct dwc3_request *r = NULL;
783 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200784 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200785 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300786 u32 transferred;
Felipe Balbifca88922012-07-19 09:05:35 +0300787 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200788 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 u8 epnum;
790
791 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200792 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300793
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300794 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
795
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300797
Felipe Balbiccb072d2014-10-01 12:20:29 -0500798 trace_dwc3_complete_trb(ep0, trb);
799
Felipe Balbi520fe762014-11-10 14:39:44 -0600800 r = next_request(&ep0->request_list);
801 if (!r)
802 return;
803
Felipe Balbifca88922012-07-19 09:05:35 +0300804 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
805 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500806 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300807
808 if (r)
809 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
810
811 return;
812 }
813
Felipe Balbi6856d302014-09-30 11:43:20 -0500814 ur = &r->request;
815
Felipe Balbif6bafc62012-02-06 11:04:53 +0200816 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300817
Felipe Balbia6829702011-08-27 22:18:09 +0300818 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500819 unsigned transfer_size = ur->length;
820 unsigned maxp = ep0->endpoint.maxpacket;
821
822 transfer_size += (maxp - (transfer_size % maxp));
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530823
824 /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */
825 if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
826 transfer_size = DWC3_EP0_BOUNCE_SIZE;
827
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300828 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500829 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300830 memcpy(ur->buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300831 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200832 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300833 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300834
Felipe Balbicd423dd2012-03-21 11:44:00 +0200835 ur->actual += transferred;
836
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 if ((epnum & 1) && ur->actual < ur->length) {
838 /* for some reason we did not get everything out */
839
840 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300841 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500842 dwc3_gadget_giveback(ep0, r, 0);
843
844 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
845 ur->length && ur->zero) {
846 int ret;
847
848 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
849
850 ret = dwc3_ep0_start_trans(dwc, epnum,
851 dwc->ctrl_req_addr, 0,
852 DWC3_TRBCTL_CONTROL_DATA);
853 WARN_ON(ret < 0);
854 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300855 }
856}
857
Felipe Balbi85a78102012-05-31 12:32:37 +0300858static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300859 const struct dwc3_event_depevt *event)
860{
861 struct dwc3_request *r;
862 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300863 struct dwc3_trb *trb;
864 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300865
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300866 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300867 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300868
Felipe Balbiccb072d2014-10-01 12:20:29 -0500869 trace_dwc3_complete_trb(dep, trb);
870
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 if (!list_empty(&dep->request_list)) {
872 r = next_request(&dep->request_list);
873
874 dwc3_gadget_giveback(dep, r, 0);
875 }
876
Gerard Cauvy3b637362012-02-10 12:21:18 +0200877 if (dwc->test_mode) {
878 int ret;
879
880 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
881 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500882 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200883 dwc->test_mode_nr);
884 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300885 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200886 }
887 }
888
Felipe Balbifca88922012-07-19 09:05:35 +0300889 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
890 if (status == DWC3_TRBSTS_SETUP_PENDING)
Felipe Balbidab716c2014-09-24 11:22:23 -0500891 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300892
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300893 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300894 dwc3_ep0_out_start(dwc);
895}
896
897static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
898 const struct dwc3_event_depevt *event)
899{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300900 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
901
902 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300903 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300904 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300905
Felipe Balbi72246da2011-08-19 18:10:58 +0300906 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300907 case EP0_SETUP_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500908 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300909 dwc3_ep0_inspect_setup(dwc, event);
910 break;
911
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300912 case EP0_DATA_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500913 dwc3_trace(trace_dwc3_ep0, "Data Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300914 dwc3_ep0_complete_data(dwc, event);
915 break;
916
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300917 case EP0_STATUS_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500918 dwc3_trace(trace_dwc3_ep0, "Status Phase");
Felipe Balbi85a78102012-05-31 12:32:37 +0300919 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300920 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300921 default:
922 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300923 }
924}
925
Felipe Balbia0807882012-05-04 13:03:54 +0300926static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
927 struct dwc3_ep *dep, struct dwc3_request *req)
928{
929 int ret;
930
931 req->direction = !!dep->number;
932
933 if (req->request.length == 0) {
934 ret = dwc3_ep0_start_trans(dwc, dep->number,
935 dwc->ctrl_req_addr, 0,
936 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300937 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300938 && (dep->number == 0)) {
Andrew Mortonc390b032013-03-08 09:42:50 +0200939 u32 transfer_size;
940 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +0300941
942 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
943 dep->number);
944 if (ret) {
945 dev_dbg(dwc->dev, "failed to map request\n");
946 return;
947 }
948
Andrew Mortonc390b032013-03-08 09:42:50 +0200949 maxpacket = dep->endpoint.maxpacket;
950 transfer_size = roundup(req->request.length, maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +0300951
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530952 if (transfer_size > DWC3_EP0_BOUNCE_SIZE) {
953 dev_WARN(dwc->dev, "bounce buf can't handle req len\n");
954 transfer_size = DWC3_EP0_BOUNCE_SIZE;
955 }
956
Felipe Balbia0807882012-05-04 13:03:54 +0300957 dwc->ep0_bounced = true;
958
959 /*
960 * REVISIT in case request length is bigger than
961 * DWC3_EP0_BOUNCE_SIZE we will need two chained
962 * TRBs to handle the transfer.
963 */
964 ret = dwc3_ep0_start_trans(dwc, dep->number,
965 dwc->ep0_bounce_addr, transfer_size,
966 DWC3_TRBCTL_CONTROL_DATA);
967 } else {
968 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
969 dep->number);
970 if (ret) {
971 dev_dbg(dwc->dev, "failed to map request\n");
972 return;
973 }
974
975 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
976 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
977 }
978
979 WARN_ON(ret < 0);
980}
981
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100982static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300983{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100984 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300985 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300986
987 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
988 : DWC3_TRBCTL_CONTROL_STATUS2;
989
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100990 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300991 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100992}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300993
Felipe Balbi788a23f2012-05-21 14:22:41 +0300994static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100995{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200996 if (dwc->resize_fifos) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500997 dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200998 dwc3_gadget_resize_tx_fifos(dwc);
999 dwc->resize_fifos = 0;
1000 }
1001
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001002 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001003}
1004
Felipe Balbi788a23f2012-05-21 14:22:41 +03001005static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1006 const struct dwc3_event_depevt *event)
1007{
1008 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1009
1010 __dwc3_ep0_do_control_status(dwc, dep);
1011}
1012
Felipe Balbi2e3db062012-07-19 09:26:59 +03001013static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1014{
1015 struct dwc3_gadget_ep_cmd_params params;
1016 u32 cmd;
1017 int ret;
1018
1019 if (!dep->resource_index)
1020 return;
1021
1022 cmd = DWC3_DEPCMD_ENDTRANSFER;
1023 cmd |= DWC3_DEPCMD_CMDIOC;
1024 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1025 memset(&params, 0, sizeof(params));
1026 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1027 WARN_ON_ONCE(ret);
1028 dep->resource_index = 0;
1029}
1030
Felipe Balbi72246da2011-08-19 18:10:58 +03001031static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1032 const struct dwc3_event_depevt *event)
1033{
Felipe Balbidf62df52011-10-14 15:11:49 +03001034 dwc->setup_packet_pending = true;
1035
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001036 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001037 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001038 dwc3_trace(trace_dwc3_ep0, "Control Data");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001039
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001040 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001041 * We already have a DATA transfer in the controller's cache,
1042 * if we receive a XferNotReady(DATA) we will ignore it, unless
1043 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001044 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001045 * In that case, we must issue END_TRANSFER command to the Data
1046 * Phase we already have started and issue SetStall on the
1047 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001048 */
1049 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001050 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1051
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001052 dwc3_trace(trace_dwc3_ep0,
1053 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001054 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001055 dwc3_ep0_stall_and_restart(dwc);
1056 return;
1057 }
1058
Felipe Balbi72246da2011-08-19 18:10:58 +03001059 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001060
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001061 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001062 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1063 return;
1064
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001065 dwc3_trace(trace_dwc3_ep0, "Control Status");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001066
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001067 dwc->ep0state = EP0_STATUS_PHASE;
1068
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001069 if (dwc->delayed_status) {
1070 WARN_ON_ONCE(event->endpoint_number != 1);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001071 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001072 return;
1073 }
1074
Felipe Balbi788a23f2012-05-21 14:22:41 +03001075 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001076 }
1077}
1078
1079void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001080 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001081{
1082 u8 epnum = event->endpoint_number;
1083
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001084 dwc3_trace(trace_dwc3_ep0, "%s while ep%d%s in state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +03001085 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001086 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001087 dwc3_ep0_state_string(dwc->ep0state));
1088
1089 switch (event->endpoint_event) {
1090 case DWC3_DEPEVT_XFERCOMPLETE:
1091 dwc3_ep0_xfer_complete(dwc, event);
1092 break;
1093
1094 case DWC3_DEPEVT_XFERNOTREADY:
1095 dwc3_ep0_xfernotready(dwc, event);
1096 break;
1097
1098 case DWC3_DEPEVT_XFERINPROGRESS:
1099 case DWC3_DEPEVT_RXTXFIFOEVT:
1100 case DWC3_DEPEVT_STREAMEVT:
1101 case DWC3_DEPEVT_EPCMDCMPLT:
1102 break;
1103 }
1104}