blob: 73efdf6938a2acb92c0e246cc52e98b62a21188b [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"
56
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010057static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum);
58
Felipe Balbi72246da2011-08-19 18:10:58 +030059static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
60{
61 switch (state) {
62 case EP0_UNCONNECTED:
63 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030064 case EP0_SETUP_PHASE:
65 return "Setup Phase";
66 case EP0_DATA_PHASE:
67 return "Data Phase";
68 case EP0_STATUS_PHASE:
69 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030070 default:
71 return "UNKNOWN";
72 }
73}
74
75static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030076 u32 len, u32 type)
Felipe Balbi72246da2011-08-19 18:10:58 +030077{
78 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020079 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030080 struct dwc3_ep *dep;
81
82 int ret;
83
84 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030085 if (dep->flags & DWC3_EP_BUSY) {
86 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
87 return 0;
88 }
Felipe Balbi72246da2011-08-19 18:10:58 +030089
Felipe Balbif6bafc62012-02-06 11:04:53 +020090 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030091
Felipe Balbif6bafc62012-02-06 11:04:53 +020092 trb->bpl = lower_32_bits(buf_dma);
93 trb->bph = upper_32_bits(buf_dma);
94 trb->size = len;
95 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030096
Felipe Balbif6bafc62012-02-06 11:04:53 +020097 trb->ctrl |= (DWC3_TRB_CTRL_HWO
98 | DWC3_TRB_CTRL_LST
99 | DWC3_TRB_CTRL_IOC
100 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +0300101
102 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300103 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
104 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300105
106 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
107 DWC3_DEPCMD_STARTTRANSFER, &params);
108 if (ret < 0) {
109 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
110 return ret;
111 }
112
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300113 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +0300114 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
115 dep->number);
116
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300117 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
118
Felipe Balbi72246da2011-08-19 18:10:58 +0300119 return 0;
120}
121
122static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
123 struct dwc3_request *req)
124{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100125 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300126 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300127
128 req->request.actual = 0;
129 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300130 req->epnum = dep->number;
131
132 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300133
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300134 /*
135 * Gadget driver might not be quick enough to queue a request
136 * before we get a Transfer Not Ready event on this endpoint.
137 *
138 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
139 * flag is set, it's telling us that as soon as Gadget queues the
140 * required request, we should kick the transfer here because the
141 * IRQ we were waiting for is long gone.
142 */
143 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300144 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300145
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300146 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300147
Felipe Balbi68d8a782011-12-29 06:32:29 +0200148 if (dwc->ep0state != EP0_DATA_PHASE) {
149 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300150 return 0;
151 }
Felipe Balbia6829702011-08-27 22:18:09 +0300152
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300153 ret = dwc3_ep0_start_trans(dwc, direction,
Felipe Balbi68d8a782011-12-29 06:32:29 +0200154 req->request.dma, req->request.length,
155 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300156 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
157 DWC3_EP0_DIR_IN);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200158 } else if (dwc->delayed_status) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100159 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200160
161 if (dwc->ep0state == EP0_STATUS_PHASE)
162 dwc3_ep0_do_control_status(dwc, 1);
163 else
164 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300165 }
166
167 return ret;
168}
169
170int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
171 gfp_t gfp_flags)
172{
173 struct dwc3_request *req = to_dwc3_request(request);
174 struct dwc3_ep *dep = to_dwc3_ep(ep);
175 struct dwc3 *dwc = dep->dwc;
176
177 unsigned long flags;
178
179 int ret;
180
Felipe Balbi72246da2011-08-19 18:10:58 +0300181 spin_lock_irqsave(&dwc->lock, flags);
182 if (!dep->desc) {
183 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
184 request, dep->name);
185 ret = -ESHUTDOWN;
186 goto out;
187 }
188
189 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200190 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300191 ret = -EBUSY;
192 goto out;
193 }
194
195 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
196 request, dep->name, request->length,
197 dwc3_ep0_state_string(dwc->ep0state));
198
199 ret = __dwc3_gadget_ep0_queue(dep, req);
200
201out:
202 spin_unlock_irqrestore(&dwc->lock, flags);
203
204 return ret;
205}
206
207static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
208{
Felipe Balbid7422202011-09-08 18:17:12 +0300209 struct dwc3_ep *dep = dwc->eps[0];
210
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200212 __dwc3_gadget_ep_set_halt(dep, 1);
213 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100214 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300215
216 if (!list_empty(&dep->request_list)) {
217 struct dwc3_request *req;
218
219 req = next_request(&dep->request_list);
220 dwc3_gadget_giveback(dep, req, -ECONNRESET);
221 }
222
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300223 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300224 dwc3_ep0_out_start(dwc);
225}
226
227void dwc3_ep0_out_start(struct dwc3 *dwc)
228{
Felipe Balbi72246da2011-08-19 18:10:58 +0300229 int ret;
230
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300231 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
232 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300233 WARN_ON(ret < 0);
234}
235
Felipe Balbi72246da2011-08-19 18:10:58 +0300236static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
237{
238 struct dwc3_ep *dep;
239 u32 windex = le16_to_cpu(wIndex_le);
240 u32 epnum;
241
242 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
243 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
244 epnum |= 1;
245
246 dep = dwc->eps[epnum];
247 if (dep->flags & DWC3_EP_ENABLED)
248 return dep;
249
250 return NULL;
251}
252
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200253static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300254{
Felipe Balbi72246da2011-08-19 18:10:58 +0300255}
Felipe Balbi72246da2011-08-19 18:10:58 +0300256/*
257 * ch 9.4.5
258 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200259static int dwc3_ep0_handle_status(struct dwc3 *dwc,
260 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300261{
262 struct dwc3_ep *dep;
263 u32 recip;
264 u16 usb_status = 0;
265 __le16 *response_pkt;
266
267 recip = ctrl->bRequestType & USB_RECIP_MASK;
268 switch (recip) {
269 case USB_RECIP_DEVICE:
270 /*
271 * We are self-powered. U1/U2/LTM will be set later
272 * once we handle this states. RemoteWakeup is 0 on SS
273 */
274 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
275 break;
276
277 case USB_RECIP_INTERFACE:
278 /*
279 * Function Remote Wake Capable D0
280 * Function Remote Wakeup D1
281 */
282 break;
283
284 case USB_RECIP_ENDPOINT:
285 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
286 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200287 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300288
289 if (dep->flags & DWC3_EP_STALL)
290 usb_status = 1 << USB_ENDPOINT_HALT;
291 break;
292 default:
293 return -EINVAL;
294 };
295
296 response_pkt = (__le16 *) dwc->setup_buf;
297 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200298
299 dep = dwc->eps[0];
300 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100301 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
302 dwc->ep0_usb_req.request.dma = dwc->setup_buf_addr;
303 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200304
305 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300306}
307
308static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
309 struct usb_ctrlrequest *ctrl, int set)
310{
311 struct dwc3_ep *dep;
312 u32 recip;
313 u32 wValue;
314 u32 wIndex;
Felipe Balbi72246da2011-08-19 18:10:58 +0300315 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300316
317 wValue = le16_to_cpu(ctrl->wValue);
318 wIndex = le16_to_cpu(ctrl->wIndex);
319 recip = ctrl->bRequestType & USB_RECIP_MASK;
320 switch (recip) {
321 case USB_RECIP_DEVICE:
322
323 /*
324 * 9.4.1 says only only for SS, in AddressState only for
325 * default control pipe
326 */
327 switch (wValue) {
328 case USB_DEVICE_U1_ENABLE:
329 case USB_DEVICE_U2_ENABLE:
330 case USB_DEVICE_LTM_ENABLE:
331 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
332 return -EINVAL;
333 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
334 return -EINVAL;
335 }
336
337 /* XXX add U[12] & LTM */
338 switch (wValue) {
339 case USB_DEVICE_REMOTE_WAKEUP:
340 break;
341 case USB_DEVICE_U1_ENABLE:
342 break;
343 case USB_DEVICE_U2_ENABLE:
344 break;
345 case USB_DEVICE_LTM_ENABLE:
346 break;
347
348 case USB_DEVICE_TEST_MODE:
349 if ((wIndex & 0xff) != 0)
350 return -EINVAL;
351 if (!set)
352 return -EINVAL;
353
Gerard Cauvy3b637362012-02-10 12:21:18 +0200354 dwc->test_mode_nr = wIndex >> 8;
355 dwc->test_mode = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300356 }
357 break;
358
359 case USB_RECIP_INTERFACE:
360 switch (wValue) {
361 case USB_INTRF_FUNC_SUSPEND:
362 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
363 /* XXX enable Low power suspend */
364 ;
365 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
366 /* XXX enable remote wakeup */
367 ;
368 break;
369 default:
370 return -EINVAL;
371 }
372 break;
373
374 case USB_RECIP_ENDPOINT:
375 switch (wValue) {
376 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800377 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300378 if (!dep)
379 return -EINVAL;
380 ret = __dwc3_gadget_ep_set_halt(dep, set);
381 if (ret)
382 return -EINVAL;
383 break;
384 default:
385 return -EINVAL;
386 }
387 break;
388
389 default:
390 return -EINVAL;
391 };
392
Felipe Balbi72246da2011-08-19 18:10:58 +0300393 return 0;
394}
395
396static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
397{
Felipe Balbi72246da2011-08-19 18:10:58 +0300398 u32 addr;
399 u32 reg;
400
401 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300402 if (addr > 127) {
403 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300404 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300405 }
406
407 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
408 dev_dbg(dwc->dev, "trying to set address when configured\n");
409 return -EINVAL;
410 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
Felipe Balbi26460212011-09-30 10:58:36 +0300412 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
413 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
414 reg |= DWC3_DCFG_DEVADDR(addr);
415 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300416
Felipe Balbi26460212011-09-30 10:58:36 +0300417 if (addr)
418 dwc->dev_state = DWC3_ADDRESS_STATE;
419 else
420 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300421
Felipe Balbi26460212011-09-30 10:58:36 +0300422 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423}
424
425static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
426{
427 int ret;
428
429 spin_unlock(&dwc->lock);
430 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
431 spin_lock(&dwc->lock);
432 return ret;
433}
434
435static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
436{
437 u32 cfg;
438 int ret;
439
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300440 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300441 cfg = le16_to_cpu(ctrl->wValue);
442
443 switch (dwc->dev_state) {
444 case DWC3_DEFAULT_STATE:
445 return -EINVAL;
446 break;
447
448 case DWC3_ADDRESS_STATE:
449 ret = dwc3_ep0_delegate_req(dwc, ctrl);
450 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200451 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 dwc->dev_state = DWC3_CONFIGURED_STATE;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200453 dwc->resize_fifos = true;
454 dev_dbg(dwc->dev, "resize fifos flag SET\n");
455 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300456 break;
457
458 case DWC3_CONFIGURED_STATE:
459 ret = dwc3_ep0_delegate_req(dwc, ctrl);
460 if (!cfg)
461 dwc->dev_state = DWC3_ADDRESS_STATE;
462 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100463 default:
464 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100466 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300467}
468
469static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
470{
471 int ret;
472
473 switch (ctrl->bRequest) {
474 case USB_REQ_GET_STATUS:
475 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
476 ret = dwc3_ep0_handle_status(dwc, ctrl);
477 break;
478 case USB_REQ_CLEAR_FEATURE:
479 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
480 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
481 break;
482 case USB_REQ_SET_FEATURE:
483 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
484 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
485 break;
486 case USB_REQ_SET_ADDRESS:
487 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
488 ret = dwc3_ep0_set_address(dwc, ctrl);
489 break;
490 case USB_REQ_SET_CONFIGURATION:
491 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
492 ret = dwc3_ep0_set_config(dwc, ctrl);
493 break;
494 default:
495 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
496 ret = dwc3_ep0_delegate_req(dwc, ctrl);
497 break;
498 };
499
500 return ret;
501}
502
503static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
504 const struct dwc3_event_depevt *event)
505{
506 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
507 int ret;
508 u32 len;
509
510 if (!dwc->gadget_driver)
511 goto err;
512
513 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300514 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300515 dwc->three_stage_setup = false;
516 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300517 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
518 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300519 dwc->three_stage_setup = true;
520 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300521 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
522 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
524 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
525 ret = dwc3_ep0_std_request(dwc, ctrl);
526 else
527 ret = dwc3_ep0_delegate_req(dwc, ctrl);
528
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100529 if (ret == USB_GADGET_DELAYED_STATUS)
530 dwc->delayed_status = true;
531
Felipe Balbi72246da2011-08-19 18:10:58 +0300532 if (ret >= 0)
533 return;
534
535err:
536 dwc3_ep0_stall_and_restart(dwc);
537}
538
539static void dwc3_ep0_complete_data(struct dwc3 *dwc,
540 const struct dwc3_event_depevt *event)
541{
542 struct dwc3_request *r = NULL;
543 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200544 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200545 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300546 u32 transferred;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200547 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548 u8 epnum;
549
550 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200551 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300552
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300553 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
554
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200555 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200556 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
Felipe Balbif6bafc62012-02-06 11:04:53 +0200558 trb = dwc->ep0_trb;
559 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300560
Felipe Balbia6829702011-08-27 22:18:09 +0300561 if (dwc->ep0_bounced) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300562 transferred = min_t(u32, ur->length,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200563 ep0->endpoint.maxpacket - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300564 memcpy(ur->buf, dwc->ep0_bounce, transferred);
565 dwc->ep0_bounced = false;
566 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200567 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300568 ur->actual += transferred;
569 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300570
571 if ((epnum & 1) && ur->actual < ur->length) {
572 /* for some reason we did not get everything out */
573
574 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300575 } else {
576 /*
577 * handle the case where we have to send a zero packet. This
578 * seems to be case when req.length > maxpacket. Could it be?
579 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300580 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200581 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 }
583}
584
585static void dwc3_ep0_complete_req(struct dwc3 *dwc,
586 const struct dwc3_event_depevt *event)
587{
588 struct dwc3_request *r;
589 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300591 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
593 if (!list_empty(&dep->request_list)) {
594 r = next_request(&dep->request_list);
595
596 dwc3_gadget_giveback(dep, r, 0);
597 }
598
Gerard Cauvy3b637362012-02-10 12:21:18 +0200599 if (dwc->test_mode) {
600 int ret;
601
602 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
603 if (ret < 0) {
604 dev_dbg(dwc->dev, "Invalid Test #%d\n",
605 dwc->test_mode_nr);
606 dwc3_ep0_stall_and_restart(dwc);
607 }
608 }
609
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300610 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 dwc3_ep0_out_start(dwc);
612}
613
614static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
615 const struct dwc3_event_depevt *event)
616{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300617 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
618
619 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -0800620 dep->res_trans_idx = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300621 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300622
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300624 case EP0_SETUP_PHASE:
625 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 dwc3_ep0_inspect_setup(dwc, event);
627 break;
628
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300629 case EP0_DATA_PHASE:
630 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300631 dwc3_ep0_complete_data(dwc, event);
632 break;
633
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300634 case EP0_STATUS_PHASE:
635 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 dwc3_ep0_complete_req(dwc, event);
637 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300638 default:
639 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300640 }
641}
642
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300643static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
644 const struct dwc3_event_depevt *event)
645{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300646 dwc3_ep0_out_start(dwc);
647}
648
649static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
650 const struct dwc3_event_depevt *event)
651{
652 struct dwc3_ep *dep;
653 struct dwc3_request *req;
654 int ret;
655
656 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300657
658 if (list_empty(&dep->request_list)) {
659 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
660 dep->flags |= DWC3_EP_PENDING_REQUEST;
661
662 if (event->endpoint_number)
663 dep->flags |= DWC3_EP0_DIR_IN;
664 return;
665 }
666
667 req = next_request(&dep->request_list);
668 req->direction = !!event->endpoint_number;
669
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300670 if (req->request.length == 0) {
671 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
672 dwc->ctrl_req_addr, 0,
673 DWC3_TRBCTL_CONTROL_DATA);
674 } else if ((req->request.length % dep->endpoint.maxpacket)
675 && (event->endpoint_number == 0)) {
676 dwc3_map_buffer_to_dma(req);
677
678 WARN_ON(req->request.length > dep->endpoint.maxpacket);
679
680 dwc->ep0_bounced = true;
681
682 /*
683 * REVISIT in case request length is bigger than EP0
684 * wMaxPacketSize, we will need two chained TRBs to handle
685 * the transfer.
686 */
687 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
688 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
689 DWC3_TRBCTL_CONTROL_DATA);
690 } else {
691 dwc3_map_buffer_to_dma(req);
692
693 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
694 req->request.dma, req->request.length,
695 DWC3_TRBCTL_CONTROL_DATA);
696 }
697
698 WARN_ON(ret < 0);
699}
700
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100701static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300702{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100703 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300704 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300705
706 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
707 : DWC3_TRBCTL_CONTROL_STATUS2;
708
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100709 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300710 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100711}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300712
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100713static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
714{
715 struct dwc3_ep *dep = dwc->eps[epnum];
716
Felipe Balbi457e84b2012-01-18 18:04:09 +0200717 if (dwc->resize_fifos) {
718 dev_dbg(dwc->dev, "starting to resize fifos\n");
719 dwc3_gadget_resize_tx_fifos(dwc);
720 dwc->resize_fifos = 0;
721 }
722
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100723 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300724}
725
Felipe Balbi72246da2011-08-19 18:10:58 +0300726static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
727 const struct dwc3_event_depevt *event)
728{
Felipe Balbidf62df52011-10-14 15:11:49 +0300729 dwc->setup_packet_pending = true;
730
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300731 /*
732 * This part is very tricky: If we has just handled
733 * XferNotReady(Setup) and we're now expecting a
734 * XferComplete but, instead, we receive another
735 * XferNotReady(Setup), we should STALL and restart
736 * the state machine.
737 *
738 * In all other cases, we just continue waiting
739 * for the XferComplete event.
740 *
741 * We are a little bit unsafe here because we're
742 * not trying to ensure that last event was, indeed,
743 * XferNotReady(Setup).
744 *
745 * Still, we don't expect any condition where that
746 * should happen and, even if it does, it would be
747 * another error condition.
748 */
749 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
750 switch (event->status) {
751 case DEPEVT_STATUS_CONTROL_SETUP:
752 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
753 dwc3_ep0_stall_and_restart(dwc);
754 break;
755 case DEPEVT_STATUS_CONTROL_DATA:
756 /* FALLTHROUGH */
757 case DEPEVT_STATUS_CONTROL_STATUS:
758 /* FALLTHROUGH */
759 default:
760 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
761 }
762
763 return;
764 }
765
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300766 switch (event->status) {
767 case DEPEVT_STATUS_CONTROL_SETUP:
768 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100769
770 dwc->ep0state = EP0_SETUP_PHASE;
771
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300772 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300773 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300774
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300775 case DEPEVT_STATUS_CONTROL_DATA:
776 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300777
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100778 dwc->ep0state = EP0_DATA_PHASE;
779
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300780 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
781 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300782 dwc->ep0_next_event,
783 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300784
785 dwc3_ep0_stall_and_restart(dwc);
786 return;
787 }
788
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300789 /*
790 * One of the possible error cases is when Host _does_
791 * request for Data Phase, but it does so on the wrong
792 * direction.
793 *
794 * Here, we already know ep0_next_event is DATA (see above),
795 * so we only need to check for direction.
796 */
797 if (dwc->ep0_expect_in != event->endpoint_number) {
798 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
799 dwc3_ep0_stall_and_restart(dwc);
800 return;
801 }
802
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300803 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300804 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300805
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300806 case DEPEVT_STATUS_CONTROL_STATUS:
807 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300808
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100809 dwc->ep0state = EP0_STATUS_PHASE;
810
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300811 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
812 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300813 dwc->ep0_next_event,
814 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300815
816 dwc3_ep0_stall_and_restart(dwc);
817 return;
818 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100819
820 if (dwc->delayed_status) {
821 WARN_ON_ONCE(event->endpoint_number != 1);
822 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
823 return;
824 }
825
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100826 dwc3_ep0_do_control_status(dwc, event->endpoint_number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300827 }
828}
829
830void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +0200831 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +0300832{
833 u8 epnum = event->endpoint_number;
834
835 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
836 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300837 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300838 dwc3_ep0_state_string(dwc->ep0state));
839
840 switch (event->endpoint_event) {
841 case DWC3_DEPEVT_XFERCOMPLETE:
842 dwc3_ep0_xfer_complete(dwc, event);
843 break;
844
845 case DWC3_DEPEVT_XFERNOTREADY:
846 dwc3_ep0_xfernotready(dwc, event);
847 break;
848
849 case DWC3_DEPEVT_XFERINPROGRESS:
850 case DWC3_DEPEVT_RXTXFIFOEVT:
851 case DWC3_DEPEVT_STREAMEVT:
852 case DWC3_DEPEVT_EPCMDCMPLT:
853 break;
854 }
855}