blob: 333278c56690b30ccd292dda9f9d05c2cb90b686 [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>
51
52#include "core.h"
53#include "gadget.h"
54#include "io.h"
55
Felipe Balbi72246da2011-08-19 18:10:58 +030056static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
57{
58 switch (state) {
59 case EP0_UNCONNECTED:
60 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030061 case EP0_SETUP_PHASE:
62 return "Setup Phase";
63 case EP0_DATA_PHASE:
64 return "Data Phase";
65 case EP0_STATUS_PHASE:
66 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030067 default:
68 return "UNKNOWN";
69 }
70}
71
72static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030073 u32 len, u32 type)
Felipe Balbi72246da2011-08-19 18:10:58 +030074{
75 struct dwc3_gadget_ep_cmd_params params;
76 struct dwc3_trb_hw *trb_hw;
77 struct dwc3_trb trb;
78 struct dwc3_ep *dep;
79
80 int ret;
81
82 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030083 if (dep->flags & DWC3_EP_BUSY) {
84 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
85 return 0;
86 }
Felipe Balbi72246da2011-08-19 18:10:58 +030087
88 trb_hw = dwc->ep0_trb;
89 memset(&trb, 0, sizeof(trb));
90
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030091 trb.trbctl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030092 trb.bplh = buf_dma;
93 trb.length = len;
94
95 trb.hwo = 1;
96 trb.lst = 1;
97 trb.ioc = 1;
98 trb.isp_imi = 1;
99
100 dwc3_trb_to_hw(&trb, trb_hw);
101
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{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300125 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300126
127 req->request.actual = 0;
128 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300129 req->epnum = dep->number;
130
131 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300132
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300133 /*
134 * Gadget driver might not be quick enough to queue a request
135 * before we get a Transfer Not Ready event on this endpoint.
136 *
137 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
138 * flag is set, it's telling us that as soon as Gadget queues the
139 * required request, we should kick the transfer here because the
140 * IRQ we were waiting for is long gone.
141 */
142 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
143 struct dwc3 *dwc = dep->dwc;
144 unsigned direction;
145 u32 type;
Felipe Balbia6829702011-08-27 22:18:09 +0300146
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300147 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300148
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300149 if (dwc->ep0state == EP0_STATUS_PHASE) {
150 type = dwc->three_stage_setup
151 ? DWC3_TRBCTL_CONTROL_STATUS3
152 : DWC3_TRBCTL_CONTROL_STATUS2;
153 } else if (dwc->ep0state == EP0_DATA_PHASE) {
154 type = DWC3_TRBCTL_CONTROL_DATA;
155 } else {
156 /* should never happen */
157 WARN_ON(1);
158 return 0;
159 }
Felipe Balbia6829702011-08-27 22:18:09 +0300160
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300161 ret = dwc3_ep0_start_trans(dwc, direction,
162 req->request.dma, req->request.length, type);
163 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
164 DWC3_EP0_DIR_IN);
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;
Felipe Balbid7422202011-09-08 18:17:12 +0300214
215 if (!list_empty(&dep->request_list)) {
216 struct dwc3_request *req;
217
218 req = next_request(&dep->request_list);
219 dwc3_gadget_giveback(dep, req, -ECONNRESET);
220 }
221
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300222 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300223 dwc3_ep0_out_start(dwc);
224}
225
226void dwc3_ep0_out_start(struct dwc3 *dwc)
227{
Felipe Balbi72246da2011-08-19 18:10:58 +0300228 int ret;
229
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300230 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
231 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300232 WARN_ON(ret < 0);
233}
234
Felipe Balbi72246da2011-08-19 18:10:58 +0300235static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
236{
237 struct dwc3_ep *dep;
238 u32 windex = le16_to_cpu(wIndex_le);
239 u32 epnum;
240
241 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
242 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
243 epnum |= 1;
244
245 dep = dwc->eps[epnum];
246 if (dep->flags & DWC3_EP_ENABLED)
247 return dep;
248
249 return NULL;
250}
251
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200252static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300253{
Felipe Balbi72246da2011-08-19 18:10:58 +0300254}
Felipe Balbi72246da2011-08-19 18:10:58 +0300255/*
256 * ch 9.4.5
257 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200258static int dwc3_ep0_handle_status(struct dwc3 *dwc,
259 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300260{
261 struct dwc3_ep *dep;
262 u32 recip;
263 u16 usb_status = 0;
264 __le16 *response_pkt;
265
266 recip = ctrl->bRequestType & USB_RECIP_MASK;
267 switch (recip) {
268 case USB_RECIP_DEVICE:
269 /*
270 * We are self-powered. U1/U2/LTM will be set later
271 * once we handle this states. RemoteWakeup is 0 on SS
272 */
273 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
274 break;
275
276 case USB_RECIP_INTERFACE:
277 /*
278 * Function Remote Wake Capable D0
279 * Function Remote Wakeup D1
280 */
281 break;
282
283 case USB_RECIP_ENDPOINT:
284 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
285 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200286 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
288 if (dep->flags & DWC3_EP_STALL)
289 usb_status = 1 << USB_ENDPOINT_HALT;
290 break;
291 default:
292 return -EINVAL;
293 };
294
295 response_pkt = (__le16 *) dwc->setup_buf;
296 *response_pkt = cpu_to_le16(usb_status);
297 dwc->ep0_usb_req.length = sizeof(*response_pkt);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200298 dwc->ep0_usb_req.dma = dwc->setup_buf_addr;
299 dwc->ep0_usb_req.complete = dwc3_ep0_status_cmpl;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200300 return usb_ep_queue(&dwc->eps[0]->endpoint, &dwc->ep0_usb_req,
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200301 GFP_ATOMIC);
Felipe Balbi72246da2011-08-19 18:10:58 +0300302}
303
304static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
305 struct usb_ctrlrequest *ctrl, int set)
306{
307 struct dwc3_ep *dep;
308 u32 recip;
309 u32 wValue;
310 u32 wIndex;
311 u32 reg;
312 int ret;
313 u32 mode;
314
315 wValue = le16_to_cpu(ctrl->wValue);
316 wIndex = le16_to_cpu(ctrl->wIndex);
317 recip = ctrl->bRequestType & USB_RECIP_MASK;
318 switch (recip) {
319 case USB_RECIP_DEVICE:
320
321 /*
322 * 9.4.1 says only only for SS, in AddressState only for
323 * default control pipe
324 */
325 switch (wValue) {
326 case USB_DEVICE_U1_ENABLE:
327 case USB_DEVICE_U2_ENABLE:
328 case USB_DEVICE_LTM_ENABLE:
329 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
330 return -EINVAL;
331 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
332 return -EINVAL;
333 }
334
335 /* XXX add U[12] & LTM */
336 switch (wValue) {
337 case USB_DEVICE_REMOTE_WAKEUP:
338 break;
339 case USB_DEVICE_U1_ENABLE:
340 break;
341 case USB_DEVICE_U2_ENABLE:
342 break;
343 case USB_DEVICE_LTM_ENABLE:
344 break;
345
346 case USB_DEVICE_TEST_MODE:
347 if ((wIndex & 0xff) != 0)
348 return -EINVAL;
349 if (!set)
350 return -EINVAL;
351
352 mode = wIndex >> 8;
353 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
354 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
355
356 switch (mode) {
357 case TEST_J:
358 case TEST_K:
359 case TEST_SE0_NAK:
360 case TEST_PACKET:
361 case TEST_FORCE_EN:
362 reg |= mode << 1;
363 break;
364 default:
365 return -EINVAL;
366 }
367 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
368 break;
369 default:
370 return -EINVAL;
371 }
372 break;
373
374 case USB_RECIP_INTERFACE:
375 switch (wValue) {
376 case USB_INTRF_FUNC_SUSPEND:
377 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
378 /* XXX enable Low power suspend */
379 ;
380 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
381 /* XXX enable remote wakeup */
382 ;
383 break;
384 default:
385 return -EINVAL;
386 }
387 break;
388
389 case USB_RECIP_ENDPOINT:
390 switch (wValue) {
391 case USB_ENDPOINT_HALT:
Sebastian Andrzej Siewior1e7618d2011-10-24 12:09:39 +0300392 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300393 if (!dep)
394 return -EINVAL;
395 ret = __dwc3_gadget_ep_set_halt(dep, set);
396 if (ret)
397 return -EINVAL;
398 break;
399 default:
400 return -EINVAL;
401 }
402 break;
403
404 default:
405 return -EINVAL;
406 };
407
Felipe Balbi72246da2011-08-19 18:10:58 +0300408 return 0;
409}
410
411static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
412{
Felipe Balbi72246da2011-08-19 18:10:58 +0300413 u32 addr;
414 u32 reg;
415
416 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300417 if (addr > 127) {
418 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300419 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300420 }
421
422 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
423 dev_dbg(dwc->dev, "trying to set address when configured\n");
424 return -EINVAL;
425 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300426
Felipe Balbi26460212011-09-30 10:58:36 +0300427 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
428 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
429 reg |= DWC3_DCFG_DEVADDR(addr);
430 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300431
Felipe Balbi26460212011-09-30 10:58:36 +0300432 if (addr)
433 dwc->dev_state = DWC3_ADDRESS_STATE;
434 else
435 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300436
Felipe Balbi26460212011-09-30 10:58:36 +0300437 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300438}
439
440static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
441{
442 int ret;
443
444 spin_unlock(&dwc->lock);
445 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
446 spin_lock(&dwc->lock);
447 return ret;
448}
449
450static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
451{
452 u32 cfg;
453 int ret;
454
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300455 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456 cfg = le16_to_cpu(ctrl->wValue);
457
458 switch (dwc->dev_state) {
459 case DWC3_DEFAULT_STATE:
460 return -EINVAL;
461 break;
462
463 case DWC3_ADDRESS_STATE:
464 ret = dwc3_ep0_delegate_req(dwc, ctrl);
465 /* if the cfg matches and the cfg is non zero */
466 if (!ret && cfg)
467 dwc->dev_state = DWC3_CONFIGURED_STATE;
468 break;
469
470 case DWC3_CONFIGURED_STATE:
471 ret = dwc3_ep0_delegate_req(dwc, ctrl);
472 if (!cfg)
473 dwc->dev_state = DWC3_ADDRESS_STATE;
474 break;
475 }
476 return 0;
477}
478
479static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
480{
481 int ret;
482
483 switch (ctrl->bRequest) {
484 case USB_REQ_GET_STATUS:
485 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
486 ret = dwc3_ep0_handle_status(dwc, ctrl);
487 break;
488 case USB_REQ_CLEAR_FEATURE:
489 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
490 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
491 break;
492 case USB_REQ_SET_FEATURE:
493 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
494 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
495 break;
496 case USB_REQ_SET_ADDRESS:
497 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
498 ret = dwc3_ep0_set_address(dwc, ctrl);
499 break;
500 case USB_REQ_SET_CONFIGURATION:
501 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
502 ret = dwc3_ep0_set_config(dwc, ctrl);
503 break;
504 default:
505 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
506 ret = dwc3_ep0_delegate_req(dwc, ctrl);
507 break;
508 };
509
510 return ret;
511}
512
513static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
514 const struct dwc3_event_depevt *event)
515{
516 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
517 int ret;
518 u32 len;
519
520 if (!dwc->gadget_driver)
521 goto err;
522
523 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300524 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300525 dwc->three_stage_setup = false;
526 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300527 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
528 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300529 dwc->three_stage_setup = true;
530 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300531 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
532 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300533
534 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
535 ret = dwc3_ep0_std_request(dwc, ctrl);
536 else
537 ret = dwc3_ep0_delegate_req(dwc, ctrl);
538
539 if (ret >= 0)
540 return;
541
542err:
543 dwc3_ep0_stall_and_restart(dwc);
544}
545
546static void dwc3_ep0_complete_data(struct dwc3 *dwc,
547 const struct dwc3_event_depevt *event)
548{
549 struct dwc3_request *r = NULL;
550 struct usb_request *ur;
551 struct dwc3_trb trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200552 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300553 u32 transferred;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 u8 epnum;
555
556 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200557 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300558
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300559 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
560
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200561 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200562 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
564 dwc3_trb_to_nat(dwc->ep0_trb, &trb);
565
Felipe Balbia6829702011-08-27 22:18:09 +0300566 if (dwc->ep0_bounced) {
Felipe Balbia6829702011-08-27 22:18:09 +0300567
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300568 transferred = min_t(u32, ur->length,
569 ep0->endpoint.maxpacket - trb.length);
Felipe Balbia6829702011-08-27 22:18:09 +0300570 memcpy(ur->buf, dwc->ep0_bounce, transferred);
571 dwc->ep0_bounced = false;
572 } else {
573 transferred = ur->length - trb.length;
574 ur->actual += transferred;
575 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300576
577 if ((epnum & 1) && ur->actual < ur->length) {
578 /* for some reason we did not get everything out */
579
580 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 } else {
582 /*
583 * handle the case where we have to send a zero packet. This
584 * seems to be case when req.length > maxpacket. Could it be?
585 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200587 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588 }
589}
590
591static void dwc3_ep0_complete_req(struct dwc3 *dwc,
592 const struct dwc3_event_depevt *event)
593{
594 struct dwc3_request *r;
595 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300597 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
599 if (!list_empty(&dep->request_list)) {
600 r = next_request(&dep->request_list);
601
602 dwc3_gadget_giveback(dep, r, 0);
603 }
604
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300605 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 dwc3_ep0_out_start(dwc);
607}
608
609static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
610 const struct dwc3_event_depevt *event)
611{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300612 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
613
614 dep->flags &= ~DWC3_EP_BUSY;
615
Felipe Balbi72246da2011-08-19 18:10:58 +0300616 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300617 case EP0_SETUP_PHASE:
618 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300619 dwc3_ep0_inspect_setup(dwc, event);
620 break;
621
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300622 case EP0_DATA_PHASE:
623 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 dwc3_ep0_complete_data(dwc, event);
625 break;
626
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300627 case EP0_STATUS_PHASE:
628 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300629 dwc3_ep0_complete_req(dwc, event);
630 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300631 default:
632 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 }
634}
635
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300636static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
637 const struct dwc3_event_depevt *event)
638{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300639 dwc3_ep0_out_start(dwc);
640}
641
642static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
643 const struct dwc3_event_depevt *event)
644{
645 struct dwc3_ep *dep;
646 struct dwc3_request *req;
647 int ret;
648
649 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300650
651 if (list_empty(&dep->request_list)) {
652 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
653 dep->flags |= DWC3_EP_PENDING_REQUEST;
654
655 if (event->endpoint_number)
656 dep->flags |= DWC3_EP0_DIR_IN;
657 return;
658 }
659
660 req = next_request(&dep->request_list);
661 req->direction = !!event->endpoint_number;
662
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300663 if (req->request.length == 0) {
664 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
665 dwc->ctrl_req_addr, 0,
666 DWC3_TRBCTL_CONTROL_DATA);
667 } else if ((req->request.length % dep->endpoint.maxpacket)
668 && (event->endpoint_number == 0)) {
669 dwc3_map_buffer_to_dma(req);
670
671 WARN_ON(req->request.length > dep->endpoint.maxpacket);
672
673 dwc->ep0_bounced = true;
674
675 /*
676 * REVISIT in case request length is bigger than EP0
677 * wMaxPacketSize, we will need two chained TRBs to handle
678 * the transfer.
679 */
680 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
681 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
682 DWC3_TRBCTL_CONTROL_DATA);
683 } else {
684 dwc3_map_buffer_to_dma(req);
685
686 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
687 req->request.dma, req->request.length,
688 DWC3_TRBCTL_CONTROL_DATA);
689 }
690
691 WARN_ON(ret < 0);
692}
693
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100694static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300695{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100696 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300697 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300698
699 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
700 : DWC3_TRBCTL_CONTROL_STATUS2;
701
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100702 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300703 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100704}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300705
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100706static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
707{
708 struct dwc3_ep *dep = dwc->eps[epnum];
709
710 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300711}
712
Felipe Balbi72246da2011-08-19 18:10:58 +0300713static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
714 const struct dwc3_event_depevt *event)
715{
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300716 /*
717 * This part is very tricky: If we has just handled
718 * XferNotReady(Setup) and we're now expecting a
719 * XferComplete but, instead, we receive another
720 * XferNotReady(Setup), we should STALL and restart
721 * the state machine.
722 *
723 * In all other cases, we just continue waiting
724 * for the XferComplete event.
725 *
726 * We are a little bit unsafe here because we're
727 * not trying to ensure that last event was, indeed,
728 * XferNotReady(Setup).
729 *
730 * Still, we don't expect any condition where that
731 * should happen and, even if it does, it would be
732 * another error condition.
733 */
734 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
735 switch (event->status) {
736 case DEPEVT_STATUS_CONTROL_SETUP:
737 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
738 dwc3_ep0_stall_and_restart(dwc);
739 break;
740 case DEPEVT_STATUS_CONTROL_DATA:
741 /* FALLTHROUGH */
742 case DEPEVT_STATUS_CONTROL_STATUS:
743 /* FALLTHROUGH */
744 default:
745 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
746 }
747
748 return;
749 }
750
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300751 switch (event->status) {
752 case DEPEVT_STATUS_CONTROL_SETUP:
753 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100754
755 dwc->ep0state = EP0_SETUP_PHASE;
756
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300757 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300759
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300760 case DEPEVT_STATUS_CONTROL_DATA:
761 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300762
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100763 dwc->ep0state = EP0_DATA_PHASE;
764
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300765 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
766 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300767 dwc->ep0_next_event,
768 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300769
770 dwc3_ep0_stall_and_restart(dwc);
771 return;
772 }
773
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300774 /*
775 * One of the possible error cases is when Host _does_
776 * request for Data Phase, but it does so on the wrong
777 * direction.
778 *
779 * Here, we already know ep0_next_event is DATA (see above),
780 * so we only need to check for direction.
781 */
782 if (dwc->ep0_expect_in != event->endpoint_number) {
783 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
784 dwc3_ep0_stall_and_restart(dwc);
785 return;
786 }
787
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300788 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300790
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300791 case DEPEVT_STATUS_CONTROL_STATUS:
792 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300793
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100794 dwc->ep0state = EP0_STATUS_PHASE;
795
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300796 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
797 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300798 dwc->ep0_next_event,
799 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300800
801 dwc3_ep0_stall_and_restart(dwc);
802 return;
803 }
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100804 dwc3_ep0_do_control_status(dwc, event->endpoint_number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 }
806}
807
808void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +0200809 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +0300810{
811 u8 epnum = event->endpoint_number;
812
813 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
814 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300815 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300816 dwc3_ep0_state_string(dwc->ep0state));
817
818 switch (event->endpoint_event) {
819 case DWC3_DEPEVT_XFERCOMPLETE:
820 dwc3_ep0_xfer_complete(dwc, event);
821 break;
822
823 case DWC3_DEPEVT_XFERNOTREADY:
824 dwc3_ep0_xfernotready(dwc, event);
825 break;
826
827 case DWC3_DEPEVT_XFERINPROGRESS:
828 case DWC3_DEPEVT_RXTXFIFOEVT:
829 case DWC3_DEPEVT_STREAMEVT:
830 case DWC3_DEPEVT_EPCMDCMPLT:
831 break;
832 }
833}