blob: c20e30c8b69545f9ecbb661a96e63d420e1e436a [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;
79 struct dwc3_trb_hw *trb_hw;
80 struct dwc3_trb trb;
81 struct dwc3_ep *dep;
82
83 int ret;
84
85 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030086 if (dep->flags & DWC3_EP_BUSY) {
87 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
88 return 0;
89 }
Felipe Balbi72246da2011-08-19 18:10:58 +030090
91 trb_hw = dwc->ep0_trb;
92 memset(&trb, 0, sizeof(trb));
93
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030094 trb.trbctl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030095 trb.bplh = buf_dma;
96 trb.length = len;
97
98 trb.hwo = 1;
99 trb.lst = 1;
100 trb.ioc = 1;
101 trb.isp_imi = 1;
102
103 dwc3_trb_to_hw(&trb, trb_hw);
104
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 Balbi72246da2011-08-19 18:10:58 +0300117 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
118 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 Balbic7fcdeb2011-08-27 22:28:36 +0300129 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300130
131 req->request.actual = 0;
132 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300133 req->epnum = dep->number;
134
135 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300136
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300137 /*
138 * Gadget driver might not be quick enough to queue a request
139 * before we get a Transfer Not Ready event on this endpoint.
140 *
141 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
142 * flag is set, it's telling us that as soon as Gadget queues the
143 * required request, we should kick the transfer here because the
144 * IRQ we were waiting for is long gone.
145 */
146 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300147 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300148
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300149 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300150
Felipe Balbi68d8a782011-12-29 06:32:29 +0200151 if (dwc->ep0state != EP0_DATA_PHASE) {
152 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300153 return 0;
154 }
Felipe Balbia6829702011-08-27 22:18:09 +0300155
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300156 ret = dwc3_ep0_start_trans(dwc, direction,
Felipe Balbi68d8a782011-12-29 06:32:29 +0200157 req->request.dma, req->request.length,
158 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300159 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
160 DWC3_EP0_DIR_IN);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200161 } else if (dwc->delayed_status) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100162 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200163
164 if (dwc->ep0state == EP0_STATUS_PHASE)
165 dwc3_ep0_do_control_status(dwc, 1);
166 else
167 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300168 }
169
170 return ret;
171}
172
173int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
174 gfp_t gfp_flags)
175{
176 struct dwc3_request *req = to_dwc3_request(request);
177 struct dwc3_ep *dep = to_dwc3_ep(ep);
178 struct dwc3 *dwc = dep->dwc;
179
180 unsigned long flags;
181
182 int ret;
183
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 spin_lock_irqsave(&dwc->lock, flags);
185 if (!dep->desc) {
186 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
187 request, dep->name);
188 ret = -ESHUTDOWN;
189 goto out;
190 }
191
192 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200193 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300194 ret = -EBUSY;
195 goto out;
196 }
197
198 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
199 request, dep->name, request->length,
200 dwc3_ep0_state_string(dwc->ep0state));
201
202 ret = __dwc3_gadget_ep0_queue(dep, req);
203
204out:
205 spin_unlock_irqrestore(&dwc->lock, flags);
206
207 return ret;
208}
209
210static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
211{
Felipe Balbid7422202011-09-08 18:17:12 +0300212 struct dwc3_ep *dep = dwc->eps[0];
213
Felipe Balbi72246da2011-08-19 18:10:58 +0300214 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200215 __dwc3_gadget_ep_set_halt(dep, 1);
216 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100217 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300218
219 if (!list_empty(&dep->request_list)) {
220 struct dwc3_request *req;
221
222 req = next_request(&dep->request_list);
223 dwc3_gadget_giveback(dep, req, -ECONNRESET);
224 }
225
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300226 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 dwc3_ep0_out_start(dwc);
228}
229
230void dwc3_ep0_out_start(struct dwc3 *dwc)
231{
Felipe Balbi72246da2011-08-19 18:10:58 +0300232 int ret;
233
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300234 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
235 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300236 WARN_ON(ret < 0);
237}
238
Felipe Balbi72246da2011-08-19 18:10:58 +0300239static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
240{
241 struct dwc3_ep *dep;
242 u32 windex = le16_to_cpu(wIndex_le);
243 u32 epnum;
244
245 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
246 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
247 epnum |= 1;
248
249 dep = dwc->eps[epnum];
250 if (dep->flags & DWC3_EP_ENABLED)
251 return dep;
252
253 return NULL;
254}
255
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200256static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300257{
Felipe Balbi72246da2011-08-19 18:10:58 +0300258}
Felipe Balbi72246da2011-08-19 18:10:58 +0300259/*
260 * ch 9.4.5
261 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200262static int dwc3_ep0_handle_status(struct dwc3 *dwc,
263 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300264{
265 struct dwc3_ep *dep;
266 u32 recip;
267 u16 usb_status = 0;
268 __le16 *response_pkt;
269
270 recip = ctrl->bRequestType & USB_RECIP_MASK;
271 switch (recip) {
272 case USB_RECIP_DEVICE:
273 /*
274 * We are self-powered. U1/U2/LTM will be set later
275 * once we handle this states. RemoteWakeup is 0 on SS
276 */
277 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
278 break;
279
280 case USB_RECIP_INTERFACE:
281 /*
282 * Function Remote Wake Capable D0
283 * Function Remote Wakeup D1
284 */
285 break;
286
287 case USB_RECIP_ENDPOINT:
288 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
289 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200290 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300291
292 if (dep->flags & DWC3_EP_STALL)
293 usb_status = 1 << USB_ENDPOINT_HALT;
294 break;
295 default:
296 return -EINVAL;
297 };
298
299 response_pkt = (__le16 *) dwc->setup_buf;
300 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200301
302 dep = dwc->eps[0];
303 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100304 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
305 dwc->ep0_usb_req.request.dma = dwc->setup_buf_addr;
306 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200307
308 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300309}
310
311static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
312 struct usb_ctrlrequest *ctrl, int set)
313{
314 struct dwc3_ep *dep;
315 u32 recip;
316 u32 wValue;
317 u32 wIndex;
Felipe Balbi72246da2011-08-19 18:10:58 +0300318 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300319
320 wValue = le16_to_cpu(ctrl->wValue);
321 wIndex = le16_to_cpu(ctrl->wIndex);
322 recip = ctrl->bRequestType & USB_RECIP_MASK;
323 switch (recip) {
324 case USB_RECIP_DEVICE:
325
326 /*
327 * 9.4.1 says only only for SS, in AddressState only for
328 * default control pipe
329 */
330 switch (wValue) {
331 case USB_DEVICE_U1_ENABLE:
332 case USB_DEVICE_U2_ENABLE:
333 case USB_DEVICE_LTM_ENABLE:
334 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
335 return -EINVAL;
336 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
337 return -EINVAL;
338 }
339
340 /* XXX add U[12] & LTM */
341 switch (wValue) {
342 case USB_DEVICE_REMOTE_WAKEUP:
343 break;
344 case USB_DEVICE_U1_ENABLE:
345 break;
346 case USB_DEVICE_U2_ENABLE:
347 break;
348 case USB_DEVICE_LTM_ENABLE:
349 break;
350
351 case USB_DEVICE_TEST_MODE:
352 if ((wIndex & 0xff) != 0)
353 return -EINVAL;
354 if (!set)
355 return -EINVAL;
356
Gerard Cauvy3b637362012-02-10 12:21:18 +0200357 dwc->test_mode_nr = wIndex >> 8;
358 dwc->test_mode = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300359 }
360 break;
361
362 case USB_RECIP_INTERFACE:
363 switch (wValue) {
364 case USB_INTRF_FUNC_SUSPEND:
365 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
366 /* XXX enable Low power suspend */
367 ;
368 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
369 /* XXX enable remote wakeup */
370 ;
371 break;
372 default:
373 return -EINVAL;
374 }
375 break;
376
377 case USB_RECIP_ENDPOINT:
378 switch (wValue) {
379 case USB_ENDPOINT_HALT:
Sebastian Andrzej Siewior1e7618d2011-10-24 12:09:39 +0300380 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300381 if (!dep)
382 return -EINVAL;
383 ret = __dwc3_gadget_ep_set_halt(dep, set);
384 if (ret)
385 return -EINVAL;
386 break;
387 default:
388 return -EINVAL;
389 }
390 break;
391
392 default:
393 return -EINVAL;
394 };
395
Felipe Balbi72246da2011-08-19 18:10:58 +0300396 return 0;
397}
398
399static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
400{
Felipe Balbi72246da2011-08-19 18:10:58 +0300401 u32 addr;
402 u32 reg;
403
404 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300405 if (addr > 127) {
406 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300407 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300408 }
409
410 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
411 dev_dbg(dwc->dev, "trying to set address when configured\n");
412 return -EINVAL;
413 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300414
Felipe Balbi26460212011-09-30 10:58:36 +0300415 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
416 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
417 reg |= DWC3_DCFG_DEVADDR(addr);
418 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300419
Felipe Balbi26460212011-09-30 10:58:36 +0300420 if (addr)
421 dwc->dev_state = DWC3_ADDRESS_STATE;
422 else
423 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
Felipe Balbi26460212011-09-30 10:58:36 +0300425 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300426}
427
428static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
429{
430 int ret;
431
432 spin_unlock(&dwc->lock);
433 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
434 spin_lock(&dwc->lock);
435 return ret;
436}
437
438static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
439{
440 u32 cfg;
441 int ret;
442
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300443 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300444 cfg = le16_to_cpu(ctrl->wValue);
445
446 switch (dwc->dev_state) {
447 case DWC3_DEFAULT_STATE:
448 return -EINVAL;
449 break;
450
451 case DWC3_ADDRESS_STATE:
452 ret = dwc3_ep0_delegate_req(dwc, ctrl);
453 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200454 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300455 dwc->dev_state = DWC3_CONFIGURED_STATE;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200456 dwc->resize_fifos = true;
457 dev_dbg(dwc->dev, "resize fifos flag SET\n");
458 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300459 break;
460
461 case DWC3_CONFIGURED_STATE:
462 ret = dwc3_ep0_delegate_req(dwc, ctrl);
463 if (!cfg)
464 dwc->dev_state = DWC3_ADDRESS_STATE;
465 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100466 default:
467 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100469 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470}
471
472static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
473{
474 int ret;
475
476 switch (ctrl->bRequest) {
477 case USB_REQ_GET_STATUS:
478 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
479 ret = dwc3_ep0_handle_status(dwc, ctrl);
480 break;
481 case USB_REQ_CLEAR_FEATURE:
482 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
483 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
484 break;
485 case USB_REQ_SET_FEATURE:
486 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
487 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
488 break;
489 case USB_REQ_SET_ADDRESS:
490 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
491 ret = dwc3_ep0_set_address(dwc, ctrl);
492 break;
493 case USB_REQ_SET_CONFIGURATION:
494 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
495 ret = dwc3_ep0_set_config(dwc, ctrl);
496 break;
497 default:
498 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
499 ret = dwc3_ep0_delegate_req(dwc, ctrl);
500 break;
501 };
502
503 return ret;
504}
505
506static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
507 const struct dwc3_event_depevt *event)
508{
509 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
510 int ret;
511 u32 len;
512
513 if (!dwc->gadget_driver)
514 goto err;
515
516 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300517 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300518 dwc->three_stage_setup = false;
519 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300520 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
521 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300522 dwc->three_stage_setup = true;
523 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300524 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
525 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300526
527 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
528 ret = dwc3_ep0_std_request(dwc, ctrl);
529 else
530 ret = dwc3_ep0_delegate_req(dwc, ctrl);
531
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100532 if (ret == USB_GADGET_DELAYED_STATUS)
533 dwc->delayed_status = true;
534
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 if (ret >= 0)
536 return;
537
538err:
539 dwc3_ep0_stall_and_restart(dwc);
540}
541
542static void dwc3_ep0_complete_data(struct dwc3 *dwc,
543 const struct dwc3_event_depevt *event)
544{
545 struct dwc3_request *r = NULL;
546 struct usb_request *ur;
547 struct dwc3_trb trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200548 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300549 u32 transferred;
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 u8 epnum;
551
552 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200553 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300554
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300555 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
556
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200557 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200558 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300559
560 dwc3_trb_to_nat(dwc->ep0_trb, &trb);
561
Felipe Balbia6829702011-08-27 22:18:09 +0300562 if (dwc->ep0_bounced) {
Felipe Balbia6829702011-08-27 22:18:09 +0300563
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300564 transferred = min_t(u32, ur->length,
565 ep0->endpoint.maxpacket - trb.length);
Felipe Balbia6829702011-08-27 22:18:09 +0300566 memcpy(ur->buf, dwc->ep0_bounce, transferred);
567 dwc->ep0_bounced = false;
568 } else {
569 transferred = ur->length - trb.length;
570 ur->actual += transferred;
571 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300572
573 if ((epnum & 1) && ur->actual < ur->length) {
574 /* for some reason we did not get everything out */
575
576 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 } else {
578 /*
579 * handle the case where we have to send a zero packet. This
580 * seems to be case when req.length > maxpacket. Could it be?
581 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200583 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 }
585}
586
587static void dwc3_ep0_complete_req(struct dwc3 *dwc,
588 const struct dwc3_event_depevt *event)
589{
590 struct dwc3_request *r;
591 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300593 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300594
595 if (!list_empty(&dep->request_list)) {
596 r = next_request(&dep->request_list);
597
598 dwc3_gadget_giveback(dep, r, 0);
599 }
600
Gerard Cauvy3b637362012-02-10 12:21:18 +0200601 if (dwc->test_mode) {
602 int ret;
603
604 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
605 if (ret < 0) {
606 dev_dbg(dwc->dev, "Invalid Test #%d\n",
607 dwc->test_mode_nr);
608 dwc3_ep0_stall_and_restart(dwc);
609 }
610 }
611
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300612 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 dwc3_ep0_out_start(dwc);
614}
615
616static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
617 const struct dwc3_event_depevt *event)
618{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300619 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
620
621 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbidf62df52011-10-14 15:11:49 +0300622 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300623
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300625 case EP0_SETUP_PHASE:
626 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300627 dwc3_ep0_inspect_setup(dwc, event);
628 break;
629
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300630 case EP0_DATA_PHASE:
631 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300632 dwc3_ep0_complete_data(dwc, event);
633 break;
634
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300635 case EP0_STATUS_PHASE:
636 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300637 dwc3_ep0_complete_req(dwc, event);
638 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300639 default:
640 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 }
642}
643
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300644static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
645 const struct dwc3_event_depevt *event)
646{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300647 dwc3_ep0_out_start(dwc);
648}
649
650static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
651 const struct dwc3_event_depevt *event)
652{
653 struct dwc3_ep *dep;
654 struct dwc3_request *req;
655 int ret;
656
657 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300658
659 if (list_empty(&dep->request_list)) {
660 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
661 dep->flags |= DWC3_EP_PENDING_REQUEST;
662
663 if (event->endpoint_number)
664 dep->flags |= DWC3_EP0_DIR_IN;
665 return;
666 }
667
668 req = next_request(&dep->request_list);
669 req->direction = !!event->endpoint_number;
670
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300671 if (req->request.length == 0) {
672 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
673 dwc->ctrl_req_addr, 0,
674 DWC3_TRBCTL_CONTROL_DATA);
675 } else if ((req->request.length % dep->endpoint.maxpacket)
676 && (event->endpoint_number == 0)) {
677 dwc3_map_buffer_to_dma(req);
678
679 WARN_ON(req->request.length > dep->endpoint.maxpacket);
680
681 dwc->ep0_bounced = true;
682
683 /*
684 * REVISIT in case request length is bigger than EP0
685 * wMaxPacketSize, we will need two chained TRBs to handle
686 * the transfer.
687 */
688 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
689 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
690 DWC3_TRBCTL_CONTROL_DATA);
691 } else {
692 dwc3_map_buffer_to_dma(req);
693
694 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
695 req->request.dma, req->request.length,
696 DWC3_TRBCTL_CONTROL_DATA);
697 }
698
699 WARN_ON(ret < 0);
700}
701
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100702static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300703{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100704 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300705 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300706
707 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
708 : DWC3_TRBCTL_CONTROL_STATUS2;
709
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100710 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300711 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100712}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300713
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100714static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
715{
716 struct dwc3_ep *dep = dwc->eps[epnum];
717
Felipe Balbi457e84b2012-01-18 18:04:09 +0200718 if (dwc->resize_fifos) {
719 dev_dbg(dwc->dev, "starting to resize fifos\n");
720 dwc3_gadget_resize_tx_fifos(dwc);
721 dwc->resize_fifos = 0;
722 }
723
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100724 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300725}
726
Felipe Balbi72246da2011-08-19 18:10:58 +0300727static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
728 const struct dwc3_event_depevt *event)
729{
Felipe Balbidf62df52011-10-14 15:11:49 +0300730 dwc->setup_packet_pending = true;
731
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300732 /*
733 * This part is very tricky: If we has just handled
734 * XferNotReady(Setup) and we're now expecting a
735 * XferComplete but, instead, we receive another
736 * XferNotReady(Setup), we should STALL and restart
737 * the state machine.
738 *
739 * In all other cases, we just continue waiting
740 * for the XferComplete event.
741 *
742 * We are a little bit unsafe here because we're
743 * not trying to ensure that last event was, indeed,
744 * XferNotReady(Setup).
745 *
746 * Still, we don't expect any condition where that
747 * should happen and, even if it does, it would be
748 * another error condition.
749 */
750 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
751 switch (event->status) {
752 case DEPEVT_STATUS_CONTROL_SETUP:
753 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
754 dwc3_ep0_stall_and_restart(dwc);
755 break;
756 case DEPEVT_STATUS_CONTROL_DATA:
757 /* FALLTHROUGH */
758 case DEPEVT_STATUS_CONTROL_STATUS:
759 /* FALLTHROUGH */
760 default:
761 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
762 }
763
764 return;
765 }
766
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300767 switch (event->status) {
768 case DEPEVT_STATUS_CONTROL_SETUP:
769 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100770
771 dwc->ep0state = EP0_SETUP_PHASE;
772
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300773 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300774 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300775
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300776 case DEPEVT_STATUS_CONTROL_DATA:
777 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300778
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100779 dwc->ep0state = EP0_DATA_PHASE;
780
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300781 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
782 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300783 dwc->ep0_next_event,
784 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300785
786 dwc3_ep0_stall_and_restart(dwc);
787 return;
788 }
789
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300790 /*
791 * One of the possible error cases is when Host _does_
792 * request for Data Phase, but it does so on the wrong
793 * direction.
794 *
795 * Here, we already know ep0_next_event is DATA (see above),
796 * so we only need to check for direction.
797 */
798 if (dwc->ep0_expect_in != event->endpoint_number) {
799 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
800 dwc3_ep0_stall_and_restart(dwc);
801 return;
802 }
803
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300804 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300806
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300807 case DEPEVT_STATUS_CONTROL_STATUS:
808 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300809
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100810 dwc->ep0state = EP0_STATUS_PHASE;
811
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300812 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
813 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300814 dwc->ep0_next_event,
815 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300816
817 dwc3_ep0_stall_and_restart(dwc);
818 return;
819 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100820
821 if (dwc->delayed_status) {
822 WARN_ON_ONCE(event->endpoint_number != 1);
823 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
824 return;
825 }
826
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100827 dwc3_ep0_do_control_status(dwc, event->endpoint_number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300828 }
829}
830
831void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +0200832 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +0300833{
834 u8 epnum = event->endpoint_number;
835
836 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
837 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300838 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300839 dwc3_ep0_state_string(dwc->ep0state));
840
841 switch (event->endpoint_event) {
842 case DWC3_DEPEVT_XFERCOMPLETE:
843 dwc3_ep0_xfer_complete(dwc, event);
844 break;
845
846 case DWC3_DEPEVT_XFERNOTREADY:
847 dwc3_ep0_xfernotready(dwc, event);
848 break;
849
850 case DWC3_DEPEVT_XFERINPROGRESS:
851 case DWC3_DEPEVT_RXTXFIFOEVT:
852 case DWC3_DEPEVT_STREAMEVT:
853 case DWC3_DEPEVT_EPCMDCMPLT:
854 break;
855 }
856}