blob: bb0b2fb729965e272b8c489d5f1ee9566fdf5cdb [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 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/platform_device.h>
43#include <linux/pm_runtime.h>
44#include <linux/interrupt.h>
45#include <linux/io.h>
46#include <linux/list.h>
47#include <linux/dma-mapping.h>
48
49#include <linux/usb/ch9.h>
50#include <linux/usb/gadget.h>
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010051#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030052
53#include "core.h"
54#include "gadget.h"
55#include "io.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053056#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030057
Felipe Balbi9610cd22012-05-21 14:22:41 +030058static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbidb0af402012-05-04 13:03:54 +030059static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
60 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010061
Felipe Balbi72246da2011-08-19 18:10:58 +030062static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
63{
64 switch (state) {
65 case EP0_UNCONNECTED:
66 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030067 case EP0_SETUP_PHASE:
68 return "Setup Phase";
69 case EP0_DATA_PHASE:
70 return "Data Phase";
71 case EP0_STATUS_PHASE:
72 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030073 default:
74 return "UNKNOWN";
75 }
76}
77
78static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030079 u32 len, u32 type)
Felipe Balbi72246da2011-08-19 18:10:58 +030080{
81 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020082 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030083 struct dwc3_ep *dep;
84
85 int ret;
86
87 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030088 if (dep->flags & DWC3_EP_BUSY) {
89 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
90 return 0;
91 }
Felipe Balbi72246da2011-08-19 18:10:58 +030092
Felipe Balbif6bafc62012-02-06 11:04:53 +020093 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030094
Felipe Balbif6bafc62012-02-06 11:04:53 +020095 trb->bpl = lower_32_bits(buf_dma);
96 trb->bph = upper_32_bits(buf_dma);
97 trb->size = len;
98 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030099
Felipe Balbif6bafc62012-02-06 11:04:53 +0200100 trb->ctrl |= (DWC3_TRB_CTRL_HWO
101 | DWC3_TRB_CTRL_LST
102 | DWC3_TRB_CTRL_IOC
103 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +0300104
105 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300106 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
107 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300108
109 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
110 DWC3_DEPCMD_STARTTRANSFER, &params);
111 if (ret < 0) {
112 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
113 return ret;
114 }
115
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300116 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi4959cfc2012-06-06 12:04:13 +0300117 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300118 dep->number);
119
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300120 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
121
Felipe Balbi72246da2011-08-19 18:10:58 +0300122 return 0;
123}
124
125static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
126 struct dwc3_request *req)
127{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100128 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300129
130 req->request.actual = 0;
131 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300132 req->epnum = dep->number;
133
134 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300135
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300136 /*
137 * Gadget driver might not be quick enough to queue a request
138 * before we get a Transfer Not Ready event on this endpoint.
139 *
140 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
141 * flag is set, it's telling us that as soon as Gadget queues the
142 * required request, we should kick the transfer here because the
143 * IRQ we were waiting for is long gone.
144 */
145 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300146 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300147
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300148 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300149
Felipe Balbi68d8a782011-12-29 06:32:29 +0200150 if (dwc->ep0state != EP0_DATA_PHASE) {
151 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300152 return 0;
153 }
Felipe Balbia6829702011-08-27 22:18:09 +0300154
Felipe Balbidb0af402012-05-04 13:03:54 +0300155 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
156
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300157 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
158 DWC3_EP0_DIR_IN);
Felipe Balbi929646c2012-07-19 08:51:13 +0300159
160 return 0;
161 }
162
163 /*
164 * In case gadget driver asked us to delay the STATUS phase,
165 * handle it here.
166 */
167 if (dwc->delayed_status) {
Felipe Balbia866ee02012-07-19 21:05:08 +0300168 unsigned direction;
169
170 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100171 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200172
173 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbia866ee02012-07-19 21:05:08 +0300174 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200175 else
176 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi929646c2012-07-19 08:51:13 +0300177
178 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300179 }
180
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300181 /*
182 * Unfortunately we have uncovered a limitation wrt the Data Phase.
183 *
184 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
185 * come before issueing Start Transfer command, but if we do, we will
186 * miss situations where the host starts another SETUP phase instead of
187 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
188 * Layer Compliance Suite.
189 *
190 * The problem surfaces due to the fact that in case of back-to-back
191 * SETUP packets there will be no XferNotReady(DATA) generated and we
192 * will be stuck waiting for XferNotReady(DATA) forever.
193 *
194 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
195 * it tells us to start Data Phase right away. It also mentions that if
196 * we receive a SETUP phase instead of the DATA phase, core will issue
197 * XferComplete for the DATA phase, before actually initiating it in
198 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
199 * can only be used to print some debugging logs, as the core expects
200 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
201 * just so it completes right away, without transferring anything and,
202 * only then, we can go back to the SETUP phase.
203 *
204 * Because of this scenario, SNPS decided to change the programming
205 * model of control transfers and support on-demand transfers only for
206 * the STATUS phase. To fix the issue we have now, we will always wait
207 * for gadget driver to queue the DATA phase's struct usb_request, then
208 * start it right away.
209 *
210 * If we're actually in a 2-stage transfer, we will wait for
211 * XferNotReady(STATUS).
212 */
213 if (dwc->three_stage_setup) {
214 unsigned direction;
215
216 direction = dwc->ep0_expect_in;
217 dwc->ep0state = EP0_DATA_PHASE;
218
219 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
220
221 dep->flags &= ~DWC3_EP0_DIR_IN;
222 }
223
Felipe Balbi2640f342012-07-19 08:49:01 +0300224 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300225}
226
227int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
228 gfp_t gfp_flags)
229{
230 struct dwc3_request *req = to_dwc3_request(request);
231 struct dwc3_ep *dep = to_dwc3_ep(ep);
232 struct dwc3 *dwc = dep->dwc;
233
234 unsigned long flags;
235
236 int ret;
237
Felipe Balbi72246da2011-08-19 18:10:58 +0300238 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200239 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
241 request, dep->name);
242 ret = -ESHUTDOWN;
243 goto out;
244 }
245
246 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200247 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300248 ret = -EBUSY;
249 goto out;
250 }
251
252 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
253 request, dep->name, request->length,
254 dwc3_ep0_state_string(dwc->ep0state));
255
256 ret = __dwc3_gadget_ep0_queue(dep, req);
257
258out:
259 spin_unlock_irqrestore(&dwc->lock, flags);
260
261 return ret;
262}
263
264static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
265{
Felipe Balbi0c421632012-07-23 09:07:41 +0300266 struct dwc3_ep *dep;
267
268 /* reinitialize physical ep1 */
269 dep = dwc->eps[1];
270 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300271
Felipe Balbi72246da2011-08-19 18:10:58 +0300272 /* stall is always issued on EP0 */
Felipe Balbi0c421632012-07-23 09:07:41 +0300273 dep = dwc->eps[0];
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200274 __dwc3_gadget_ep_set_halt(dep, 1);
275 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100276 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300277
278 if (!list_empty(&dep->request_list)) {
279 struct dwc3_request *req;
280
281 req = next_request(&dep->request_list);
282 dwc3_gadget_giveback(dep, req, -ECONNRESET);
283 }
284
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300285 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300286 dwc3_ep0_out_start(dwc);
287}
288
Pratyush Anandeb840752012-06-25 22:40:43 +0530289int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
290{
291 struct dwc3_ep *dep = to_dwc3_ep(ep);
292 struct dwc3 *dwc = dep->dwc;
293
294 dwc3_ep0_stall_and_restart(dwc);
295
296 return 0;
297}
298
Felipe Balbi72246da2011-08-19 18:10:58 +0300299void dwc3_ep0_out_start(struct dwc3 *dwc)
300{
Felipe Balbi72246da2011-08-19 18:10:58 +0300301 int ret;
302
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300303 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
304 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 WARN_ON(ret < 0);
306}
307
Felipe Balbi72246da2011-08-19 18:10:58 +0300308static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
309{
310 struct dwc3_ep *dep;
311 u32 windex = le16_to_cpu(wIndex_le);
312 u32 epnum;
313
314 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
315 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
316 epnum |= 1;
317
318 dep = dwc->eps[epnum];
319 if (dep->flags & DWC3_EP_ENABLED)
320 return dep;
321
322 return NULL;
323}
324
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200325static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300326{
Felipe Balbi72246da2011-08-19 18:10:58 +0300327}
Felipe Balbi72246da2011-08-19 18:10:58 +0300328/*
329 * ch 9.4.5
330 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200331static int dwc3_ep0_handle_status(struct dwc3 *dwc,
332 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300333{
334 struct dwc3_ep *dep;
335 u32 recip;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200336 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 u16 usb_status = 0;
338 __le16 *response_pkt;
339
340 recip = ctrl->bRequestType & USB_RECIP_MASK;
341 switch (recip) {
342 case USB_RECIP_DEVICE:
343 /*
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200344 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300345 */
346 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200347
348 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
349 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
350 if (reg & DWC3_DCTL_INITU1ENA)
351 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
352 if (reg & DWC3_DCTL_INITU2ENA)
353 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
354 }
355
Felipe Balbi72246da2011-08-19 18:10:58 +0300356 break;
357
358 case USB_RECIP_INTERFACE:
359 /*
360 * Function Remote Wake Capable D0
361 * Function Remote Wakeup D1
362 */
363 break;
364
365 case USB_RECIP_ENDPOINT:
366 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
367 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200368 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300369
370 if (dep->flags & DWC3_EP_STALL)
371 usb_status = 1 << USB_ENDPOINT_HALT;
372 break;
373 default:
374 return -EINVAL;
375 };
376
377 response_pkt = (__le16 *) dwc->setup_buf;
378 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200379
380 dep = dwc->eps[0];
381 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100382 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200383 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100384 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200385
386 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300387}
388
389static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
390 struct usb_ctrlrequest *ctrl, int set)
391{
392 struct dwc3_ep *dep;
393 u32 recip;
394 u32 wValue;
395 u32 wIndex;
Felipe Balbi72246da2011-08-19 18:10:58 +0300396 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300397
398 wValue = le16_to_cpu(ctrl->wValue);
399 wIndex = le16_to_cpu(ctrl->wIndex);
400 recip = ctrl->bRequestType & USB_RECIP_MASK;
401 switch (recip) {
402 case USB_RECIP_DEVICE:
403
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200404 switch (wValue) {
405 case USB_DEVICE_REMOTE_WAKEUP:
406 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407 /*
408 * 9.4.1 says only only for SS, in AddressState only for
409 * default control pipe
410 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300411 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
413 return -EINVAL;
414 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
415 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200417
Felipe Balbi72246da2011-08-19 18:10:58 +0300418 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200419 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
420 return -EINVAL;
421 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
422 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200424
Felipe Balbi72246da2011-08-19 18:10:58 +0300425 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200426 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300427 break;
428
429 case USB_DEVICE_TEST_MODE:
430 if ((wIndex & 0xff) != 0)
431 return -EINVAL;
432 if (!set)
433 return -EINVAL;
434
Gerard Cauvy3b637362012-02-10 12:21:18 +0200435 dwc->test_mode_nr = wIndex >> 8;
436 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200437 break;
438 default:
439 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300440 }
441 break;
442
443 case USB_RECIP_INTERFACE:
444 switch (wValue) {
445 case USB_INTRF_FUNC_SUSPEND:
446 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
447 /* XXX enable Low power suspend */
448 ;
449 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
450 /* XXX enable remote wakeup */
451 ;
452 break;
453 default:
454 return -EINVAL;
455 }
456 break;
457
458 case USB_RECIP_ENDPOINT:
459 switch (wValue) {
460 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800461 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300462 if (!dep)
463 return -EINVAL;
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +0530464
465 if (!set && (dep->flags & DWC3_EP_WEDGE))
466 return 0;
467
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 ret = __dwc3_gadget_ep_set_halt(dep, set);
469 if (ret)
470 return -EINVAL;
471 break;
472 default:
473 return -EINVAL;
474 }
475 break;
476
477 default:
478 return -EINVAL;
479 };
480
Felipe Balbi72246da2011-08-19 18:10:58 +0300481 return 0;
482}
483
484static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
485{
Felipe Balbi72246da2011-08-19 18:10:58 +0300486 u32 addr;
487 u32 reg;
488
489 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300490 if (addr > 127) {
491 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300492 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300493 }
494
495 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
496 dev_dbg(dwc->dev, "trying to set address when configured\n");
497 return -EINVAL;
498 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
Felipe Balbi26460212011-09-30 10:58:36 +0300500 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
501 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
502 reg |= DWC3_DCFG_DEVADDR(addr);
503 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300504
Felipe Balbi26460212011-09-30 10:58:36 +0300505 if (addr)
506 dwc->dev_state = DWC3_ADDRESS_STATE;
507 else
508 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300509
Felipe Balbi26460212011-09-30 10:58:36 +0300510 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300511}
512
513static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
514{
515 int ret;
516
517 spin_unlock(&dwc->lock);
518 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
519 spin_lock(&dwc->lock);
520 return ret;
521}
522
523static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
524{
525 u32 cfg;
526 int ret;
527
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300528 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300529 cfg = le16_to_cpu(ctrl->wValue);
530
531 switch (dwc->dev_state) {
532 case DWC3_DEFAULT_STATE:
533 return -EINVAL;
534 break;
535
536 case DWC3_ADDRESS_STATE:
537 ret = dwc3_ep0_delegate_req(dwc, ctrl);
538 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200539 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 dwc->dev_state = DWC3_CONFIGURED_STATE;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200541 dwc->resize_fifos = true;
542 dev_dbg(dwc->dev, "resize fifos flag SET\n");
543 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 break;
545
546 case DWC3_CONFIGURED_STATE:
547 ret = dwc3_ep0_delegate_req(dwc, ctrl);
548 if (!cfg)
549 dwc->dev_state = DWC3_ADDRESS_STATE;
550 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100551 default:
552 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100554 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555}
556
Felipe Balbi9e788d62012-04-24 16:19:49 +0300557static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
558{
559 struct dwc3_ep *dep = to_dwc3_ep(ep);
560 struct dwc3 *dwc = dep->dwc;
561
562 u32 param = 0;
563 u32 reg;
564
565 struct timing {
566 u8 u1sel;
567 u8 u1pel;
568 u16 u2sel;
569 u16 u2pel;
570 } __packed timing;
571
572 int ret;
573
574 memcpy(&timing, req->buf, sizeof(timing));
575
576 dwc->u1sel = timing.u1sel;
577 dwc->u1pel = timing.u1pel;
Felipe Balbi87619212012-05-31 11:00:28 +0300578 dwc->u2sel = le16_to_cpu(timing.u2sel);
579 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi9e788d62012-04-24 16:19:49 +0300580
581 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
582 if (reg & DWC3_DCTL_INITU2ENA)
583 param = dwc->u2pel;
584 if (reg & DWC3_DCTL_INITU1ENA)
585 param = dwc->u1pel;
586
587 /*
588 * According to Synopsys Databook, if parameter is
589 * greater than 125, a value of zero should be
590 * programmed in the register.
591 */
592 if (param > 125)
593 param = 0;
594
595 /* now that we have the time, issue DGCMD Set Sel */
596 ret = dwc3_send_gadget_generic_command(dwc,
597 DWC3_DGCMD_SET_PERIODIC_PAR, param);
598 WARN_ON(ret < 0);
599}
600
601static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
602{
603 struct dwc3_ep *dep;
604 u16 wLength;
605 u16 wValue;
606
607 if (dwc->dev_state == DWC3_DEFAULT_STATE)
608 return -EINVAL;
609
610 wValue = le16_to_cpu(ctrl->wValue);
611 wLength = le16_to_cpu(ctrl->wLength);
612
613 if (wLength != 6) {
614 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
615 wLength);
616 return -EINVAL;
617 }
618
619 /*
620 * To handle Set SEL we need to receive 6 bytes from Host. So let's
621 * queue a usb_request for 6 bytes.
622 *
623 * Remember, though, this controller can't handle non-wMaxPacketSize
624 * aligned transfers on the OUT direction, so we queue a request for
625 * wMaxPacketSize instead.
626 */
627 dep = dwc->eps[0];
628 dwc->ep0_usb_req.dep = dep;
629 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
630 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
631 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
632
633 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
634}
635
Felipe Balbi395c3492012-04-25 10:45:05 +0300636static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
637{
638 u16 wLength;
639 u16 wValue;
640 u16 wIndex;
641
642 wValue = le16_to_cpu(ctrl->wValue);
643 wLength = le16_to_cpu(ctrl->wLength);
644 wIndex = le16_to_cpu(ctrl->wIndex);
645
646 if (wIndex || wLength)
647 return -EINVAL;
648
649 /*
650 * REVISIT It's unclear from Databook what to do with this
651 * value. For now, just cache it.
652 */
653 dwc->isoch_delay = wValue;
654
655 return 0;
656}
657
Felipe Balbi72246da2011-08-19 18:10:58 +0300658static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
659{
660 int ret;
661
662 switch (ctrl->bRequest) {
663 case USB_REQ_GET_STATUS:
664 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
665 ret = dwc3_ep0_handle_status(dwc, ctrl);
666 break;
667 case USB_REQ_CLEAR_FEATURE:
668 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
669 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
670 break;
671 case USB_REQ_SET_FEATURE:
672 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
673 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
674 break;
675 case USB_REQ_SET_ADDRESS:
676 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
677 ret = dwc3_ep0_set_address(dwc, ctrl);
678 break;
679 case USB_REQ_SET_CONFIGURATION:
680 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
681 ret = dwc3_ep0_set_config(dwc, ctrl);
682 break;
Felipe Balbi9e788d62012-04-24 16:19:49 +0300683 case USB_REQ_SET_SEL:
684 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
685 ret = dwc3_ep0_set_sel(dwc, ctrl);
686 break;
Felipe Balbi395c3492012-04-25 10:45:05 +0300687 case USB_REQ_SET_ISOCH_DELAY:
688 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
689 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
690 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300691 default:
692 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
693 ret = dwc3_ep0_delegate_req(dwc, ctrl);
694 break;
695 };
696
697 return ret;
698}
699
700static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
701 const struct dwc3_event_depevt *event)
702{
703 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbi65811b62012-05-31 10:29:49 +0300704 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 u32 len;
706
707 if (!dwc->gadget_driver)
Felipe Balbi65811b62012-05-31 10:29:49 +0300708 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300709
710 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300711 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300712 dwc->three_stage_setup = false;
713 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300714 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
715 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300716 dwc->three_stage_setup = true;
717 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300718 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
719 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300720
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530721 dbg_setup(0x00, ctrl);
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
723 ret = dwc3_ep0_std_request(dwc, ctrl);
724 else
725 ret = dwc3_ep0_delegate_req(dwc, ctrl);
726
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100727 if (ret == USB_GADGET_DELAYED_STATUS)
728 dwc->delayed_status = true;
729
Felipe Balbi65811b62012-05-31 10:29:49 +0300730out:
731 if (ret < 0)
732 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300733}
734
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530735bool zlp_required;
Felipe Balbi72246da2011-08-19 18:10:58 +0300736static void dwc3_ep0_complete_data(struct dwc3 *dwc,
737 const struct dwc3_event_depevt *event)
738{
739 struct dwc3_request *r = NULL;
740 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200741 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200742 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300743 u32 transferred;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300744 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200745 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 u8 epnum;
747
748 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200749 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300750
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300751 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
752
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200753 r = next_request(&ep0->request_list);
Vijayavardhan Vennapusa4ecb5e82013-04-29 17:23:29 +0530754 if (r == NULL)
755 return;
756
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200757 ur = &r->request;
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530758 if ((epnum & 1) && ur->zero &&
759 (ur->length % ep0->endpoint.maxpacket == 0)) {
760 zlp_required = true;
761 ur->zero = false;
762 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300763
Felipe Balbif6bafc62012-02-06 11:04:53 +0200764 trb = dwc->ep0_trb;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300765
766 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
767 if (status == DWC3_TRBSTS_SETUP_PENDING) {
768 dev_dbg(dwc->dev, "Setup Pending received\n");
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530769 zlp_required = false;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300770
771 if (r)
772 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
773
774 return;
775 }
776
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530777 if (zlp_required)
778 return;
779
Felipe Balbif6bafc62012-02-06 11:04:53 +0200780 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300781
Felipe Balbia6829702011-08-27 22:18:09 +0300782 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500783 unsigned transfer_size = ur->length;
784 unsigned maxp = ep0->endpoint.maxpacket;
785
786 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300787 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500788 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300789 memcpy(ur->buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300790 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200791 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300792 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300793
Felipe Balbicd423dd2012-03-21 11:44:00 +0200794 ur->actual += transferred;
795
Felipe Balbi72246da2011-08-19 18:10:58 +0300796 if ((epnum & 1) && ur->actual < ur->length) {
797 /* for some reason we did not get everything out */
798
799 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300800 } else {
801 /*
802 * handle the case where we have to send a zero packet. This
803 * seems to be case when req.length > maxpacket. Could it be?
804 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200806 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300807 }
808}
809
Felipe Balbied8c3982012-05-31 12:32:37 +0300810static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300811 const struct dwc3_event_depevt *event)
812{
813 struct dwc3_request *r;
814 struct dwc3_ep *dep;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300815 struct dwc3_trb *trb;
816 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300817
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300818 dep = dwc->eps[0];
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300819 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300820
821 if (!list_empty(&dep->request_list)) {
822 r = next_request(&dep->request_list);
823
824 dwc3_gadget_giveback(dep, r, 0);
825 }
826
Gerard Cauvy3b637362012-02-10 12:21:18 +0200827 if (dwc->test_mode) {
828 int ret;
829
830 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
831 if (ret < 0) {
832 dev_dbg(dwc->dev, "Invalid Test #%d\n",
833 dwc->test_mode_nr);
834 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi0ed27b12012-06-25 19:30:49 +0300835 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200836 }
837 }
838
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300839 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
840 if (status == DWC3_TRBSTS_SETUP_PENDING)
841 dev_dbg(dwc->dev, "Setup Pending received\n");
842
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530843 dbg_print(dep->number, "DONE", status, "STATUS");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300844 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300845 dwc3_ep0_out_start(dwc);
846}
847
848static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
849 const struct dwc3_event_depevt *event)
850{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300851 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
852
853 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi4959cfc2012-06-06 12:04:13 +0300854 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300855 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300856
Felipe Balbi72246da2011-08-19 18:10:58 +0300857 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300858 case EP0_SETUP_PHASE:
859 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 dwc3_ep0_inspect_setup(dwc, event);
861 break;
862
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300863 case EP0_DATA_PHASE:
864 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300865 dwc3_ep0_complete_data(dwc, event);
866 break;
867
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300868 case EP0_STATUS_PHASE:
869 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbied8c3982012-05-31 12:32:37 +0300870 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300872 default:
873 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 }
875}
876
Felipe Balbidb0af402012-05-04 13:03:54 +0300877static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
878 struct dwc3_ep *dep, struct dwc3_request *req)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300879{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300880 int ret;
881
Felipe Balbidb0af402012-05-04 13:03:54 +0300882 req->direction = !!dep->number;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300883
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300884 if (req->request.length == 0) {
Felipe Balbidb0af402012-05-04 13:03:54 +0300885 ret = dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300886 dwc->ctrl_req_addr, 0,
887 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbi0a1730a2012-05-04 13:08:22 +0300888 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbidb0af402012-05-04 13:03:54 +0300889 && (dep->number == 0)) {
890 u32 transfer_size;
891
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200892 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
Felipe Balbidb0af402012-05-04 13:03:54 +0300893 dep->number);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200894 if (ret) {
895 dev_dbg(dwc->dev, "failed to map request\n");
896 return;
897 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300898
Felipe Balbidb0af402012-05-04 13:03:54 +0300899 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
900
901 transfer_size = roundup(req->request.length,
902 (u32) dep->endpoint.maxpacket);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300903
904 dwc->ep0_bounced = true;
905
906 /*
Felipe Balbidb0af402012-05-04 13:03:54 +0300907 * REVISIT in case request length is bigger than
908 * DWC3_EP0_BOUNCE_SIZE we will need two chained
909 * TRBs to handle the transfer.
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300910 */
Felipe Balbidb0af402012-05-04 13:03:54 +0300911 ret = dwc3_ep0_start_trans(dwc, dep->number,
912 dwc->ep0_bounce_addr, transfer_size,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300913 DWC3_TRBCTL_CONTROL_DATA);
914 } else {
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200915 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
Felipe Balbidb0af402012-05-04 13:03:54 +0300916 dep->number);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200917 if (ret) {
918 dev_dbg(dwc->dev, "failed to map request\n");
919 return;
920 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300921
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530922 if (dep->number &&
923 !(req->request.length % dwc->gadget.ep0->maxpacket))
924 req->request.zero = true;
925
Felipe Balbidb0af402012-05-04 13:03:54 +0300926 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
927 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300928 }
929
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530930 dbg_queue(dep->number, &req->request, ret);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300931 WARN_ON(ret < 0);
932}
933
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100934static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300935{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100936 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300937 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300938
939 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
940 : DWC3_TRBCTL_CONTROL_STATUS2;
941
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100942 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300943 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100944}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300945
Felipe Balbi9610cd22012-05-21 14:22:41 +0300946static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100947{
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530948 int ret;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200949 if (dwc->resize_fifos) {
950 dev_dbg(dwc->dev, "starting to resize fifos\n");
951 dwc3_gadget_resize_tx_fifos(dwc);
952 dwc->resize_fifos = 0;
953 }
954
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530955 ret = dwc3_ep0_start_control_status(dep);
956 dbg_print(dep->number, "QUEUE", ret, "STATUS");
957 WARN_ON(ret);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300958}
959
Felipe Balbi9610cd22012-05-21 14:22:41 +0300960static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
961 const struct dwc3_event_depevt *event)
962{
963 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
964
965 __dwc3_ep0_do_control_status(dwc, dep);
966}
967
Felipe Balbia7e8d652012-07-19 09:26:59 +0300968static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
969{
970 struct dwc3_gadget_ep_cmd_params params;
971 u32 cmd;
972 int ret;
973
974 if (!dep->resource_index)
975 return;
976
977 cmd = DWC3_DEPCMD_ENDTRANSFER;
978 cmd |= DWC3_DEPCMD_CMDIOC;
979 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
980 memset(&params, 0, sizeof(params));
981 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
982 WARN_ON_ONCE(ret);
983 dep->resource_index = 0;
984}
985
Felipe Balbi72246da2011-08-19 18:10:58 +0300986static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
987 const struct dwc3_event_depevt *event)
988{
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530989 u8 epnum;
990 int ret;
991
Felipe Balbidf62df52011-10-14 15:11:49 +0300992 dwc->setup_packet_pending = true;
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +0530993 epnum = event->endpoint_number;
Felipe Balbidf62df52011-10-14 15:11:49 +0300994
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300995 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300996 case DEPEVT_STATUS_CONTROL_DATA:
997 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300998
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300999 /*
Felipe Balbia7e8d652012-07-19 09:26:59 +03001000 * We already have a DATA transfer in the controller's cache,
1001 * if we receive a XferNotReady(DATA) we will ignore it, unless
1002 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001003 *
Felipe Balbia7e8d652012-07-19 09:26:59 +03001004 * In that case, we must issue END_TRANSFER command to the Data
1005 * Phase we already have started and issue SetStall on the
1006 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001007 */
1008 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbia7e8d652012-07-19 09:26:59 +03001009 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1010
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001011 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
Felipe Balbia7e8d652012-07-19 09:26:59 +03001012 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001013 dwc3_ep0_stall_and_restart(dwc);
1014 return;
1015 }
1016
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +05301017 if (zlp_required) {
1018 zlp_required = false;
1019 ret = dwc3_ep0_start_trans(dwc, epnum,
1020 dwc->ctrl_req_addr, 0,
1021 DWC3_TRBCTL_CONTROL_DATA);
1022 dbg_event(epnum, "ZLP", ret);
1023 WARN_ON(ret < 0);
1024 }
1025
Felipe Balbi72246da2011-08-19 18:10:58 +03001026 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001027
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001028 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbicb84c5e2012-07-23 09:09:32 +03001029 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1030 return;
1031
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001032 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001033
Vijayavardhan Vennapusa3d07bce2013-03-27 15:16:27 +05301034 zlp_required = false;
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001035 dwc->ep0state = EP0_STATUS_PHASE;
1036
Vijayavardhan Vennapusab8964f82012-10-10 16:50:35 +05301037 if (dwc->delayed_status &&
1038 list_empty(&dwc->eps[0]->request_list)) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001039 WARN_ON_ONCE(event->endpoint_number != 1);
1040 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
1041 return;
1042 }
Vijayavardhan Vennapusab8964f82012-10-10 16:50:35 +05301043 dwc->delayed_status = false;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001044
Felipe Balbi9610cd22012-05-21 14:22:41 +03001045 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001046 }
1047}
1048
1049void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001050 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001051{
1052 u8 epnum = event->endpoint_number;
1053
1054 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1055 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001056 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001057 dwc3_ep0_state_string(dwc->ep0state));
1058
1059 switch (event->endpoint_event) {
1060 case DWC3_DEPEVT_XFERCOMPLETE:
1061 dwc3_ep0_xfer_complete(dwc, event);
1062 break;
1063
1064 case DWC3_DEPEVT_XFERNOTREADY:
1065 dwc3_ep0_xfernotready(dwc, event);
1066 break;
1067
1068 case DWC3_DEPEVT_XFERINPROGRESS:
1069 case DWC3_DEPEVT_RXTXFIFOEVT:
1070 case DWC3_DEPEVT_STREAMEVT:
1071 case DWC3_DEPEVT_EPCMDCMPLT:
1072 break;
1073 }
1074}