blob: a1d7a87979a57cb0d181cddc46ba914e3e792af2 [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;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200396 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300397 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300398
399 wValue = le16_to_cpu(ctrl->wValue);
400 wIndex = le16_to_cpu(ctrl->wIndex);
401 recip = ctrl->bRequestType & USB_RECIP_MASK;
402 switch (recip) {
403 case USB_RECIP_DEVICE:
404
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200405 switch (wValue) {
406 case USB_DEVICE_REMOTE_WAKEUP:
407 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408 /*
409 * 9.4.1 says only only for SS, in AddressState only for
410 * default control pipe
411 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300413 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
414 return -EINVAL;
415 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
416 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300417
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200418 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
419 if (set)
420 reg |= DWC3_DCTL_INITU1ENA;
421 else
422 reg &= ~DWC3_DCTL_INITU1ENA;
423 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200425
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200427 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
428 return -EINVAL;
429 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
430 return -EINVAL;
431
432 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
433 if (set)
434 reg |= DWC3_DCTL_INITU2ENA;
435 else
436 reg &= ~DWC3_DCTL_INITU2ENA;
437 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300438 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200439
Felipe Balbi72246da2011-08-19 18:10:58 +0300440 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200441 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300442 break;
443
444 case USB_DEVICE_TEST_MODE:
445 if ((wIndex & 0xff) != 0)
446 return -EINVAL;
447 if (!set)
448 return -EINVAL;
449
Gerard Cauvy3b637362012-02-10 12:21:18 +0200450 dwc->test_mode_nr = wIndex >> 8;
451 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200452 break;
453 default:
454 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455 }
456 break;
457
458 case USB_RECIP_INTERFACE:
459 switch (wValue) {
460 case USB_INTRF_FUNC_SUSPEND:
461 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
462 /* XXX enable Low power suspend */
463 ;
464 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
465 /* XXX enable remote wakeup */
466 ;
467 break;
468 default:
469 return -EINVAL;
470 }
471 break;
472
473 case USB_RECIP_ENDPOINT:
474 switch (wValue) {
475 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800476 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300477 if (!dep)
478 return -EINVAL;
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +0530479
480 if (!set && (dep->flags & DWC3_EP_WEDGE))
481 return 0;
482
Felipe Balbi72246da2011-08-19 18:10:58 +0300483 ret = __dwc3_gadget_ep_set_halt(dep, set);
484 if (ret)
485 return -EINVAL;
486 break;
487 default:
488 return -EINVAL;
489 }
490 break;
491
492 default:
493 return -EINVAL;
494 };
495
Felipe Balbi72246da2011-08-19 18:10:58 +0300496 return 0;
497}
498
499static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
500{
Felipe Balbi72246da2011-08-19 18:10:58 +0300501 u32 addr;
502 u32 reg;
503
504 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300505 if (addr > 127) {
506 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300507 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300508 }
509
510 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
511 dev_dbg(dwc->dev, "trying to set address when configured\n");
512 return -EINVAL;
513 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
Felipe Balbi26460212011-09-30 10:58:36 +0300515 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
516 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
517 reg |= DWC3_DCFG_DEVADDR(addr);
518 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300519
Felipe Balbi26460212011-09-30 10:58:36 +0300520 if (addr)
521 dwc->dev_state = DWC3_ADDRESS_STATE;
522 else
523 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300524
Felipe Balbi26460212011-09-30 10:58:36 +0300525 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300526}
527
528static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
529{
530 int ret;
531
532 spin_unlock(&dwc->lock);
533 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
534 spin_lock(&dwc->lock);
535 return ret;
536}
537
538static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
539{
540 u32 cfg;
541 int ret;
Pratyush Anand49917a42012-07-02 10:21:54 +0530542 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300544 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545 cfg = le16_to_cpu(ctrl->wValue);
546
547 switch (dwc->dev_state) {
548 case DWC3_DEFAULT_STATE:
549 return -EINVAL;
550 break;
551
552 case DWC3_ADDRESS_STATE:
553 ret = dwc3_ep0_delegate_req(dwc, ctrl);
554 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200555 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300556 dwc->dev_state = DWC3_CONFIGURED_STATE;
Pratyush Anand49917a42012-07-02 10:21:54 +0530557 /*
558 * Enable transition to U1/U2 state when
559 * nothing is pending from application.
560 */
561 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
562 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
563 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
564
Felipe Balbi457e84b2012-01-18 18:04:09 +0200565 dwc->resize_fifos = true;
566 dev_dbg(dwc->dev, "resize fifos flag SET\n");
567 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300568 break;
569
570 case DWC3_CONFIGURED_STATE:
571 ret = dwc3_ep0_delegate_req(dwc, ctrl);
572 if (!cfg)
573 dwc->dev_state = DWC3_ADDRESS_STATE;
574 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100575 default:
576 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100578 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579}
580
Felipe Balbi9e788d62012-04-24 16:19:49 +0300581static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
582{
583 struct dwc3_ep *dep = to_dwc3_ep(ep);
584 struct dwc3 *dwc = dep->dwc;
585
586 u32 param = 0;
587 u32 reg;
588
589 struct timing {
590 u8 u1sel;
591 u8 u1pel;
592 u16 u2sel;
593 u16 u2pel;
594 } __packed timing;
595
596 int ret;
597
598 memcpy(&timing, req->buf, sizeof(timing));
599
600 dwc->u1sel = timing.u1sel;
601 dwc->u1pel = timing.u1pel;
Felipe Balbi87619212012-05-31 11:00:28 +0300602 dwc->u2sel = le16_to_cpu(timing.u2sel);
603 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi9e788d62012-04-24 16:19:49 +0300604
605 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
606 if (reg & DWC3_DCTL_INITU2ENA)
607 param = dwc->u2pel;
608 if (reg & DWC3_DCTL_INITU1ENA)
609 param = dwc->u1pel;
610
611 /*
612 * According to Synopsys Databook, if parameter is
613 * greater than 125, a value of zero should be
614 * programmed in the register.
615 */
616 if (param > 125)
617 param = 0;
618
619 /* now that we have the time, issue DGCMD Set Sel */
620 ret = dwc3_send_gadget_generic_command(dwc,
621 DWC3_DGCMD_SET_PERIODIC_PAR, param);
622 WARN_ON(ret < 0);
623}
624
625static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
626{
627 struct dwc3_ep *dep;
628 u16 wLength;
629 u16 wValue;
630
631 if (dwc->dev_state == DWC3_DEFAULT_STATE)
632 return -EINVAL;
633
634 wValue = le16_to_cpu(ctrl->wValue);
635 wLength = le16_to_cpu(ctrl->wLength);
636
637 if (wLength != 6) {
638 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
639 wLength);
640 return -EINVAL;
641 }
642
643 /*
644 * To handle Set SEL we need to receive 6 bytes from Host. So let's
645 * queue a usb_request for 6 bytes.
646 *
647 * Remember, though, this controller can't handle non-wMaxPacketSize
648 * aligned transfers on the OUT direction, so we queue a request for
649 * wMaxPacketSize instead.
650 */
651 dep = dwc->eps[0];
652 dwc->ep0_usb_req.dep = dep;
653 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
654 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
655 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
656
657 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
658}
659
Felipe Balbi395c3492012-04-25 10:45:05 +0300660static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
661{
662 u16 wLength;
663 u16 wValue;
664 u16 wIndex;
665
666 wValue = le16_to_cpu(ctrl->wValue);
667 wLength = le16_to_cpu(ctrl->wLength);
668 wIndex = le16_to_cpu(ctrl->wIndex);
669
670 if (wIndex || wLength)
671 return -EINVAL;
672
673 /*
674 * REVISIT It's unclear from Databook what to do with this
675 * value. For now, just cache it.
676 */
677 dwc->isoch_delay = wValue;
678
679 return 0;
680}
681
Felipe Balbi72246da2011-08-19 18:10:58 +0300682static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
683{
684 int ret;
685
686 switch (ctrl->bRequest) {
687 case USB_REQ_GET_STATUS:
688 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
689 ret = dwc3_ep0_handle_status(dwc, ctrl);
690 break;
691 case USB_REQ_CLEAR_FEATURE:
692 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
693 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
694 break;
695 case USB_REQ_SET_FEATURE:
696 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
697 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
698 break;
699 case USB_REQ_SET_ADDRESS:
700 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
701 ret = dwc3_ep0_set_address(dwc, ctrl);
702 break;
703 case USB_REQ_SET_CONFIGURATION:
704 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
705 ret = dwc3_ep0_set_config(dwc, ctrl);
706 break;
Felipe Balbi9e788d62012-04-24 16:19:49 +0300707 case USB_REQ_SET_SEL:
708 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
709 ret = dwc3_ep0_set_sel(dwc, ctrl);
710 break;
Felipe Balbi395c3492012-04-25 10:45:05 +0300711 case USB_REQ_SET_ISOCH_DELAY:
712 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
713 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
714 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300715 default:
716 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
717 ret = dwc3_ep0_delegate_req(dwc, ctrl);
718 break;
719 };
720
721 return ret;
722}
723
724static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
725 const struct dwc3_event_depevt *event)
726{
727 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbi65811b62012-05-31 10:29:49 +0300728 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 u32 len;
730
731 if (!dwc->gadget_driver)
Felipe Balbi65811b62012-05-31 10:29:49 +0300732 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300733
734 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300735 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300736 dwc->three_stage_setup = false;
737 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300738 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
739 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300740 dwc->three_stage_setup = true;
741 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300742 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
743 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300744
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530745 dbg_setup(0x00, ctrl);
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
747 ret = dwc3_ep0_std_request(dwc, ctrl);
748 else
749 ret = dwc3_ep0_delegate_req(dwc, ctrl);
750
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100751 if (ret == USB_GADGET_DELAYED_STATUS)
752 dwc->delayed_status = true;
753
Felipe Balbi65811b62012-05-31 10:29:49 +0300754out:
755 if (ret < 0)
756 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300757}
758
759static void dwc3_ep0_complete_data(struct dwc3 *dwc,
760 const struct dwc3_event_depevt *event)
761{
762 struct dwc3_request *r = NULL;
763 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200764 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200765 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300766 u32 transferred;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300767 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200768 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 u8 epnum;
770
771 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200772 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300773
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300774 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
775
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200776 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200777 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300778
Felipe Balbif6bafc62012-02-06 11:04:53 +0200779 trb = dwc->ep0_trb;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300780
781 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
782 if (status == DWC3_TRBSTS_SETUP_PENDING) {
783 dev_dbg(dwc->dev, "Setup Pending received\n");
784
785 if (r)
786 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
787
788 return;
789 }
790
Felipe Balbif6bafc62012-02-06 11:04:53 +0200791 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300792
Felipe Balbia6829702011-08-27 22:18:09 +0300793 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500794 unsigned transfer_size = ur->length;
795 unsigned maxp = ep0->endpoint.maxpacket;
796
797 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300798 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500799 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300800 memcpy(ur->buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300801 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200802 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300803 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300804
Felipe Balbicd423dd2012-03-21 11:44:00 +0200805 ur->actual += transferred;
806
Felipe Balbi72246da2011-08-19 18:10:58 +0300807 if ((epnum & 1) && ur->actual < ur->length) {
808 /* for some reason we did not get everything out */
809
810 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300811 } else {
812 /*
813 * handle the case where we have to send a zero packet. This
814 * seems to be case when req.length > maxpacket. Could it be?
815 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300816 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200817 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300818 }
819}
820
Felipe Balbied8c3982012-05-31 12:32:37 +0300821static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300822 const struct dwc3_event_depevt *event)
823{
824 struct dwc3_request *r;
825 struct dwc3_ep *dep;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300826 struct dwc3_trb *trb;
827 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300828
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300829 dep = dwc->eps[0];
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300830 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300831
832 if (!list_empty(&dep->request_list)) {
833 r = next_request(&dep->request_list);
834
835 dwc3_gadget_giveback(dep, r, 0);
836 }
837
Gerard Cauvy3b637362012-02-10 12:21:18 +0200838 if (dwc->test_mode) {
839 int ret;
840
841 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
842 if (ret < 0) {
843 dev_dbg(dwc->dev, "Invalid Test #%d\n",
844 dwc->test_mode_nr);
845 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi0ed27b12012-06-25 19:30:49 +0300846 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200847 }
848 }
849
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300850 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
851 if (status == DWC3_TRBSTS_SETUP_PENDING)
852 dev_dbg(dwc->dev, "Setup Pending received\n");
853
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530854 dbg_print(dep->number, "DONE", status, "STATUS");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300855 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300856 dwc3_ep0_out_start(dwc);
857}
858
859static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
860 const struct dwc3_event_depevt *event)
861{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300862 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
863
864 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi4959cfc2012-06-06 12:04:13 +0300865 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300866 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300867
Felipe Balbi72246da2011-08-19 18:10:58 +0300868 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300869 case EP0_SETUP_PHASE:
870 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 dwc3_ep0_inspect_setup(dwc, event);
872 break;
873
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300874 case EP0_DATA_PHASE:
875 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 dwc3_ep0_complete_data(dwc, event);
877 break;
878
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300879 case EP0_STATUS_PHASE:
880 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbied8c3982012-05-31 12:32:37 +0300881 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300882 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300883 default:
884 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300885 }
886}
887
Felipe Balbidb0af402012-05-04 13:03:54 +0300888static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
889 struct dwc3_ep *dep, struct dwc3_request *req)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300890{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300891 int ret;
892
Felipe Balbidb0af402012-05-04 13:03:54 +0300893 req->direction = !!dep->number;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300894
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300895 if (req->request.length == 0) {
Felipe Balbidb0af402012-05-04 13:03:54 +0300896 ret = dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300897 dwc->ctrl_req_addr, 0,
898 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbi0a1730a2012-05-04 13:08:22 +0300899 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbidb0af402012-05-04 13:03:54 +0300900 && (dep->number == 0)) {
901 u32 transfer_size;
902
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200903 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
Felipe Balbidb0af402012-05-04 13:03:54 +0300904 dep->number);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200905 if (ret) {
906 dev_dbg(dwc->dev, "failed to map request\n");
907 return;
908 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300909
Felipe Balbidb0af402012-05-04 13:03:54 +0300910 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
911
912 transfer_size = roundup(req->request.length,
913 (u32) dep->endpoint.maxpacket);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300914
915 dwc->ep0_bounced = true;
916
917 /*
Felipe Balbidb0af402012-05-04 13:03:54 +0300918 * REVISIT in case request length is bigger than
919 * DWC3_EP0_BOUNCE_SIZE we will need two chained
920 * TRBs to handle the transfer.
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300921 */
Felipe Balbidb0af402012-05-04 13:03:54 +0300922 ret = dwc3_ep0_start_trans(dwc, dep->number,
923 dwc->ep0_bounce_addr, transfer_size,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300924 DWC3_TRBCTL_CONTROL_DATA);
925 } else {
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200926 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
Felipe Balbidb0af402012-05-04 13:03:54 +0300927 dep->number);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200928 if (ret) {
929 dev_dbg(dwc->dev, "failed to map request\n");
930 return;
931 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300932
Felipe Balbidb0af402012-05-04 13:03:54 +0300933 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
934 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300935 }
936
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530937 dbg_queue(dep->number, &req->request, ret);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300938 WARN_ON(ret < 0);
939}
940
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100941static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300942{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100943 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300944 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300945
946 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
947 : DWC3_TRBCTL_CONTROL_STATUS2;
948
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100949 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300950 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100951}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300952
Felipe Balbi9610cd22012-05-21 14:22:41 +0300953static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100954{
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530955 int ret;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200956 if (dwc->resize_fifos) {
957 dev_dbg(dwc->dev, "starting to resize fifos\n");
958 dwc3_gadget_resize_tx_fifos(dwc);
959 dwc->resize_fifos = 0;
960 }
961
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530962 ret = dwc3_ep0_start_control_status(dep);
963 dbg_print(dep->number, "QUEUE", ret, "STATUS");
964 WARN_ON(ret);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300965}
966
Felipe Balbi9610cd22012-05-21 14:22:41 +0300967static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
968 const struct dwc3_event_depevt *event)
969{
970 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
971
972 __dwc3_ep0_do_control_status(dwc, dep);
973}
974
Felipe Balbia7e8d652012-07-19 09:26:59 +0300975static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
976{
977 struct dwc3_gadget_ep_cmd_params params;
978 u32 cmd;
979 int ret;
980
981 if (!dep->resource_index)
982 return;
983
984 cmd = DWC3_DEPCMD_ENDTRANSFER;
985 cmd |= DWC3_DEPCMD_CMDIOC;
986 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
987 memset(&params, 0, sizeof(params));
988 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
989 WARN_ON_ONCE(ret);
990 dep->resource_index = 0;
991}
992
Felipe Balbi72246da2011-08-19 18:10:58 +0300993static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
994 const struct dwc3_event_depevt *event)
995{
Felipe Balbidf62df52011-10-14 15:11:49 +0300996 dwc->setup_packet_pending = true;
997
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300998 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300999 case DEPEVT_STATUS_CONTROL_DATA:
1000 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001001
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001002 /*
Felipe Balbia7e8d652012-07-19 09:26:59 +03001003 * We already have a DATA transfer in the controller's cache,
1004 * if we receive a XferNotReady(DATA) we will ignore it, unless
1005 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001006 *
Felipe Balbia7e8d652012-07-19 09:26:59 +03001007 * In that case, we must issue END_TRANSFER command to the Data
1008 * Phase we already have started and issue SetStall on the
1009 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001010 */
1011 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbia7e8d652012-07-19 09:26:59 +03001012 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1013
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001014 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
Felipe Balbia7e8d652012-07-19 09:26:59 +03001015 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001016 dwc3_ep0_stall_and_restart(dwc);
1017 return;
1018 }
1019
Felipe Balbi72246da2011-08-19 18:10:58 +03001020 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001021
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001022 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbicb84c5e2012-07-23 09:09:32 +03001023 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1024 return;
1025
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001026 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001027
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001028 dwc->ep0state = EP0_STATUS_PHASE;
1029
Vijayavardhan Vennapusab8964f82012-10-10 16:50:35 +05301030 if (dwc->delayed_status &&
1031 list_empty(&dwc->eps[0]->request_list)) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001032 WARN_ON_ONCE(event->endpoint_number != 1);
1033 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
1034 return;
1035 }
Vijayavardhan Vennapusab8964f82012-10-10 16:50:35 +05301036 dwc->delayed_status = false;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001037
Felipe Balbi9610cd22012-05-21 14:22:41 +03001038 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001039 }
1040}
1041
1042void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001043 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001044{
1045 u8 epnum = event->endpoint_number;
1046
1047 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1048 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001049 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001050 dwc3_ep0_state_string(dwc->ep0state));
1051
1052 switch (event->endpoint_event) {
1053 case DWC3_DEPEVT_XFERCOMPLETE:
1054 dwc3_ep0_xfer_complete(dwc, event);
1055 break;
1056
1057 case DWC3_DEPEVT_XFERNOTREADY:
1058 dwc3_ep0_xfernotready(dwc, event);
1059 break;
1060
1061 case DWC3_DEPEVT_XFERINPROGRESS:
1062 case DWC3_DEPEVT_RXTXFIFOEVT:
1063 case DWC3_DEPEVT_STREAMEVT:
1064 case DWC3_DEPEVT_EPCMDCMPLT:
1065 break;
1066 }
1067}