blob: 900627cf860385b83dc40d266c898866a015b19a [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
56static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
57 const struct dwc3_event_depevt *event);
58
59static 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{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300128 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300129
130 req->request.actual = 0;
131 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300132 req->epnum = dep->number;
133
134 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300135
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300136 /*
137 * Gadget driver might not be quick enough to queue a request
138 * before we get a Transfer Not Ready event on this endpoint.
139 *
140 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
141 * flag is set, it's telling us that as soon as Gadget queues the
142 * required request, we should kick the transfer here because the
143 * IRQ we were waiting for is long gone.
144 */
145 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
146 struct dwc3 *dwc = dep->dwc;
147 unsigned direction;
148 u32 type;
Felipe Balbia6829702011-08-27 22:18:09 +0300149
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300150 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300151
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300152 if (dwc->ep0state == EP0_STATUS_PHASE) {
153 type = dwc->three_stage_setup
154 ? DWC3_TRBCTL_CONTROL_STATUS3
155 : DWC3_TRBCTL_CONTROL_STATUS2;
156 } else if (dwc->ep0state == EP0_DATA_PHASE) {
157 type = DWC3_TRBCTL_CONTROL_DATA;
158 } else {
159 /* should never happen */
160 WARN_ON(1);
161 return 0;
162 }
Felipe Balbia6829702011-08-27 22:18:09 +0300163
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300164 ret = dwc3_ep0_start_trans(dwc, direction,
165 req->request.dma, req->request.length, type);
166 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
167 DWC3_EP0_DIR_IN);
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 */
193 if (!list_empty(&dwc->eps[0]->request_list) ||
194 !list_empty(&dwc->eps[1]->request_list) ||
195 dwc->ep0_status_pending) {
196 ret = -EBUSY;
197 goto out;
198 }
199
200 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
201 request, dep->name, request->length,
202 dwc3_ep0_state_string(dwc->ep0state));
203
204 ret = __dwc3_gadget_ep0_queue(dep, req);
205
206out:
207 spin_unlock_irqrestore(&dwc->lock, flags);
208
209 return ret;
210}
211
212static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
213{
Felipe Balbid7422202011-09-08 18:17:12 +0300214 struct dwc3_ep *dep = dwc->eps[0];
215
Felipe Balbi72246da2011-08-19 18:10:58 +0300216 /* stall is always issued on EP0 */
217 __dwc3_gadget_ep_set_halt(dwc->eps[0], 1);
Felipe Balbi76cb3232011-08-30 15:54:53 +0300218 dwc->eps[0]->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300219
220 if (!list_empty(&dep->request_list)) {
221 struct dwc3_request *req;
222
223 req = next_request(&dep->request_list);
224 dwc3_gadget_giveback(dep, req, -ECONNRESET);
225 }
226
Sebastian Andrzej Siewior0cc7a512011-10-18 19:13:28 +0200227 dwc->ep0_status_pending = 0;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300228 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300229 dwc3_ep0_out_start(dwc);
230}
231
232void dwc3_ep0_out_start(struct dwc3 *dwc)
233{
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 int ret;
235
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300236 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
237 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300238 WARN_ON(ret < 0);
239}
240
Felipe Balbi72246da2011-08-19 18:10:58 +0300241static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
242{
243 struct dwc3_ep *dep;
244 u32 windex = le16_to_cpu(wIndex_le);
245 u32 epnum;
246
247 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
248 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
249 epnum |= 1;
250
251 dep = dwc->eps[epnum];
252 if (dep->flags & DWC3_EP_ENABLED)
253 return dep;
254
255 return NULL;
256}
257
258static void dwc3_ep0_send_status_response(struct dwc3 *dwc)
259{
Felipe Balbib673cf32011-08-31 11:51:43 +0300260 dwc3_ep0_start_trans(dwc, 1, dwc->setup_buf_addr,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300261 dwc->ep0_usb_req.length,
262 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbi72246da2011-08-19 18:10:58 +0300263}
264
265/*
266 * ch 9.4.5
267 */
268static int dwc3_ep0_handle_status(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
269{
270 struct dwc3_ep *dep;
271 u32 recip;
272 u16 usb_status = 0;
273 __le16 *response_pkt;
274
275 recip = ctrl->bRequestType & USB_RECIP_MASK;
276 switch (recip) {
277 case USB_RECIP_DEVICE:
278 /*
279 * We are self-powered. U1/U2/LTM will be set later
280 * once we handle this states. RemoteWakeup is 0 on SS
281 */
282 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
283 break;
284
285 case USB_RECIP_INTERFACE:
286 /*
287 * Function Remote Wake Capable D0
288 * Function Remote Wakeup D1
289 */
290 break;
291
292 case USB_RECIP_ENDPOINT:
293 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
294 if (!dep)
295 return -EINVAL;
296
297 if (dep->flags & DWC3_EP_STALL)
298 usb_status = 1 << USB_ENDPOINT_HALT;
299 break;
300 default:
301 return -EINVAL;
302 };
303
304 response_pkt = (__le16 *) dwc->setup_buf;
305 *response_pkt = cpu_to_le16(usb_status);
306 dwc->ep0_usb_req.length = sizeof(*response_pkt);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300307 dwc->ep0_status_pending = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300308
309 return 0;
310}
311
312static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
313 struct usb_ctrlrequest *ctrl, int set)
314{
315 struct dwc3_ep *dep;
316 u32 recip;
317 u32 wValue;
318 u32 wIndex;
319 u32 reg;
320 int ret;
321 u32 mode;
322
323 wValue = le16_to_cpu(ctrl->wValue);
324 wIndex = le16_to_cpu(ctrl->wIndex);
325 recip = ctrl->bRequestType & USB_RECIP_MASK;
326 switch (recip) {
327 case USB_RECIP_DEVICE:
328
329 /*
330 * 9.4.1 says only only for SS, in AddressState only for
331 * default control pipe
332 */
333 switch (wValue) {
334 case USB_DEVICE_U1_ENABLE:
335 case USB_DEVICE_U2_ENABLE:
336 case USB_DEVICE_LTM_ENABLE:
337 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
338 return -EINVAL;
339 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
340 return -EINVAL;
341 }
342
343 /* XXX add U[12] & LTM */
344 switch (wValue) {
345 case USB_DEVICE_REMOTE_WAKEUP:
346 break;
347 case USB_DEVICE_U1_ENABLE:
348 break;
349 case USB_DEVICE_U2_ENABLE:
350 break;
351 case USB_DEVICE_LTM_ENABLE:
352 break;
353
354 case USB_DEVICE_TEST_MODE:
355 if ((wIndex & 0xff) != 0)
356 return -EINVAL;
357 if (!set)
358 return -EINVAL;
359
360 mode = wIndex >> 8;
361 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
362 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
363
364 switch (mode) {
365 case TEST_J:
366 case TEST_K:
367 case TEST_SE0_NAK:
368 case TEST_PACKET:
369 case TEST_FORCE_EN:
370 reg |= mode << 1;
371 break;
372 default:
373 return -EINVAL;
374 }
375 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
376 break;
377 default:
378 return -EINVAL;
379 }
380 break;
381
382 case USB_RECIP_INTERFACE:
383 switch (wValue) {
384 case USB_INTRF_FUNC_SUSPEND:
385 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
386 /* XXX enable Low power suspend */
387 ;
388 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
389 /* XXX enable remote wakeup */
390 ;
391 break;
392 default:
393 return -EINVAL;
394 }
395 break;
396
397 case USB_RECIP_ENDPOINT:
398 switch (wValue) {
399 case USB_ENDPOINT_HALT:
400
401 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
402 if (!dep)
403 return -EINVAL;
404 ret = __dwc3_gadget_ep_set_halt(dep, set);
405 if (ret)
406 return -EINVAL;
407 break;
408 default:
409 return -EINVAL;
410 }
411 break;
412
413 default:
414 return -EINVAL;
415 };
416
Felipe Balbi72246da2011-08-19 18:10:58 +0300417 return 0;
418}
419
420static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
421{
Felipe Balbi72246da2011-08-19 18:10:58 +0300422 u32 addr;
423 u32 reg;
424
425 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300426 if (addr > 127) {
427 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300428 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300429 }
430
431 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
432 dev_dbg(dwc->dev, "trying to set address when configured\n");
433 return -EINVAL;
434 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300435
Felipe Balbi26460212011-09-30 10:58:36 +0300436 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
437 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
438 reg |= DWC3_DCFG_DEVADDR(addr);
439 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300440
Felipe Balbi26460212011-09-30 10:58:36 +0300441 if (addr)
442 dwc->dev_state = DWC3_ADDRESS_STATE;
443 else
444 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300445
Felipe Balbi26460212011-09-30 10:58:36 +0300446 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300447}
448
449static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
450{
451 int ret;
452
453 spin_unlock(&dwc->lock);
454 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
455 spin_lock(&dwc->lock);
456 return ret;
457}
458
459static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
460{
461 u32 cfg;
462 int ret;
463
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300464 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 cfg = le16_to_cpu(ctrl->wValue);
466
467 switch (dwc->dev_state) {
468 case DWC3_DEFAULT_STATE:
469 return -EINVAL;
470 break;
471
472 case DWC3_ADDRESS_STATE:
473 ret = dwc3_ep0_delegate_req(dwc, ctrl);
474 /* if the cfg matches and the cfg is non zero */
475 if (!ret && cfg)
476 dwc->dev_state = DWC3_CONFIGURED_STATE;
477 break;
478
479 case DWC3_CONFIGURED_STATE:
480 ret = dwc3_ep0_delegate_req(dwc, ctrl);
481 if (!cfg)
482 dwc->dev_state = DWC3_ADDRESS_STATE;
483 break;
484 }
485 return 0;
486}
487
488static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
489{
490 int ret;
491
492 switch (ctrl->bRequest) {
493 case USB_REQ_GET_STATUS:
494 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
495 ret = dwc3_ep0_handle_status(dwc, ctrl);
496 break;
497 case USB_REQ_CLEAR_FEATURE:
498 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
499 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
500 break;
501 case USB_REQ_SET_FEATURE:
502 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
503 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
504 break;
505 case USB_REQ_SET_ADDRESS:
506 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
507 ret = dwc3_ep0_set_address(dwc, ctrl);
508 break;
509 case USB_REQ_SET_CONFIGURATION:
510 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
511 ret = dwc3_ep0_set_config(dwc, ctrl);
512 break;
513 default:
514 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
515 ret = dwc3_ep0_delegate_req(dwc, ctrl);
516 break;
517 };
518
519 return ret;
520}
521
522static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
523 const struct dwc3_event_depevt *event)
524{
525 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
526 int ret;
527 u32 len;
528
529 if (!dwc->gadget_driver)
530 goto err;
531
532 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300533 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300534 dwc->three_stage_setup = false;
535 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300536 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
537 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300538 dwc->three_stage_setup = true;
539 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300540 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
541 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
543 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
544 ret = dwc3_ep0_std_request(dwc, ctrl);
545 else
546 ret = dwc3_ep0_delegate_req(dwc, ctrl);
547
548 if (ret >= 0)
549 return;
550
551err:
552 dwc3_ep0_stall_and_restart(dwc);
553}
554
555static void dwc3_ep0_complete_data(struct dwc3 *dwc,
556 const struct dwc3_event_depevt *event)
557{
558 struct dwc3_request *r = NULL;
559 struct usb_request *ur;
560 struct dwc3_trb trb;
561 struct dwc3_ep *dep;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300562 u32 transferred;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563 u8 epnum;
564
565 epnum = event->endpoint_number;
566 dep = dwc->eps[epnum];
567
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300568 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
569
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 if (!dwc->ep0_status_pending) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300571 r = next_request(&dwc->eps[0]->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 ur = &r->request;
573 } else {
574 ur = &dwc->ep0_usb_req;
575 dwc->ep0_status_pending = 0;
576 }
577
578 dwc3_trb_to_nat(dwc->ep0_trb, &trb);
579
Felipe Balbia6829702011-08-27 22:18:09 +0300580 if (dwc->ep0_bounced) {
581 struct dwc3_ep *ep0 = dwc->eps[0];
582
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300583 transferred = min_t(u32, ur->length,
584 ep0->endpoint.maxpacket - trb.length);
Felipe Balbia6829702011-08-27 22:18:09 +0300585 memcpy(ur->buf, dwc->ep0_bounce, transferred);
586 dwc->ep0_bounced = false;
587 } else {
588 transferred = ur->length - trb.length;
589 ur->actual += transferred;
590 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
592 if ((epnum & 1) && ur->actual < ur->length) {
593 /* for some reason we did not get everything out */
594
595 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 } else {
597 /*
598 * handle the case where we have to send a zero packet. This
599 * seems to be case when req.length > maxpacket. Could it be?
600 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 if (r)
602 dwc3_gadget_giveback(dep, r, 0);
603 }
604}
605
606static void dwc3_ep0_complete_req(struct dwc3 *dwc,
607 const struct dwc3_event_depevt *event)
608{
609 struct dwc3_request *r;
610 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300611
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300612 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300613
614 if (!list_empty(&dep->request_list)) {
615 r = next_request(&dep->request_list);
616
617 dwc3_gadget_giveback(dep, r, 0);
618 }
619
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300620 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 dwc3_ep0_out_start(dwc);
622}
623
624static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
625 const struct dwc3_event_depevt *event)
626{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300627 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
628
629 dep->flags &= ~DWC3_EP_BUSY;
630
Felipe Balbi72246da2011-08-19 18:10:58 +0300631 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300632 case EP0_SETUP_PHASE:
633 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 dwc3_ep0_inspect_setup(dwc, event);
635 break;
636
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300637 case EP0_DATA_PHASE:
638 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300639 dwc3_ep0_complete_data(dwc, event);
640 break;
641
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300642 case EP0_STATUS_PHASE:
643 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 dwc3_ep0_complete_req(dwc, event);
645 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300646 default:
647 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300648 }
649}
650
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300651static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
652 const struct dwc3_event_depevt *event)
653{
654 dwc->ep0state = EP0_SETUP_PHASE;
655 dwc3_ep0_out_start(dwc);
656}
657
658static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
659 const struct dwc3_event_depevt *event)
660{
661 struct dwc3_ep *dep;
662 struct dwc3_request *req;
663 int ret;
664
665 dep = dwc->eps[0];
666 dwc->ep0state = EP0_DATA_PHASE;
667
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300668 if (dwc->ep0_status_pending) {
669 dwc3_ep0_send_status_response(dwc);
670 return;
671 }
672
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300673 if (list_empty(&dep->request_list)) {
674 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
675 dep->flags |= DWC3_EP_PENDING_REQUEST;
676
677 if (event->endpoint_number)
678 dep->flags |= DWC3_EP0_DIR_IN;
679 return;
680 }
681
682 req = next_request(&dep->request_list);
683 req->direction = !!event->endpoint_number;
684
685 dwc->ep0state = EP0_DATA_PHASE;
686 if (req->request.length == 0) {
687 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
688 dwc->ctrl_req_addr, 0,
689 DWC3_TRBCTL_CONTROL_DATA);
690 } else if ((req->request.length % dep->endpoint.maxpacket)
691 && (event->endpoint_number == 0)) {
692 dwc3_map_buffer_to_dma(req);
693
694 WARN_ON(req->request.length > dep->endpoint.maxpacket);
695
696 dwc->ep0_bounced = true;
697
698 /*
699 * REVISIT in case request length is bigger than EP0
700 * wMaxPacketSize, we will need two chained TRBs to handle
701 * the transfer.
702 */
703 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
704 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
705 DWC3_TRBCTL_CONTROL_DATA);
706 } else {
707 dwc3_map_buffer_to_dma(req);
708
709 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
710 req->request.dma, req->request.length,
711 DWC3_TRBCTL_CONTROL_DATA);
712 }
713
714 WARN_ON(ret < 0);
715}
716
717static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
718 const struct dwc3_event_depevt *event)
719{
720 u32 type;
721 int ret;
722
723 dwc->ep0state = EP0_STATUS_PHASE;
724
725 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
726 : DWC3_TRBCTL_CONTROL_STATUS2;
727
728 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
729 dwc->ctrl_req_addr, 0, type);
730
731 WARN_ON(ret < 0);
732}
733
Felipe Balbi72246da2011-08-19 18:10:58 +0300734static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
735 const struct dwc3_event_depevt *event)
736{
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300737 /*
738 * This part is very tricky: If we has just handled
739 * XferNotReady(Setup) and we're now expecting a
740 * XferComplete but, instead, we receive another
741 * XferNotReady(Setup), we should STALL and restart
742 * the state machine.
743 *
744 * In all other cases, we just continue waiting
745 * for the XferComplete event.
746 *
747 * We are a little bit unsafe here because we're
748 * not trying to ensure that last event was, indeed,
749 * XferNotReady(Setup).
750 *
751 * Still, we don't expect any condition where that
752 * should happen and, even if it does, it would be
753 * another error condition.
754 */
755 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
756 switch (event->status) {
757 case DEPEVT_STATUS_CONTROL_SETUP:
758 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
759 dwc3_ep0_stall_and_restart(dwc);
760 break;
761 case DEPEVT_STATUS_CONTROL_DATA:
762 /* FALLTHROUGH */
763 case DEPEVT_STATUS_CONTROL_STATUS:
764 /* FALLTHROUGH */
765 default:
766 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
767 }
768
769 return;
770 }
771
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300772 switch (event->status) {
773 case DEPEVT_STATUS_CONTROL_SETUP:
774 dev_vdbg(dwc->dev, "Control Setup\n");
775 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300776 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300777
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300778 case DEPEVT_STATUS_CONTROL_DATA:
779 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300780
781 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
810 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
811 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300812 dwc->ep0_next_event,
813 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300814
815 dwc3_ep0_stall_and_restart(dwc);
816 return;
817 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300818 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300819 }
820}
821
822void dwc3_ep0_interrupt(struct dwc3 *dwc,
823 const const struct dwc3_event_depevt *event)
824{
825 u8 epnum = event->endpoint_number;
826
827 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
828 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300829 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300830 dwc3_ep0_state_string(dwc->ep0state));
831
832 switch (event->endpoint_event) {
833 case DWC3_DEPEVT_XFERCOMPLETE:
834 dwc3_ep0_xfer_complete(dwc, event);
835 break;
836
837 case DWC3_DEPEVT_XFERNOTREADY:
838 dwc3_ep0_xfernotready(dwc, event);
839 break;
840
841 case DWC3_DEPEVT_XFERINPROGRESS:
842 case DWC3_DEPEVT_RXTXFIFOEVT:
843 case DWC3_DEPEVT_STREAMEVT:
844 case DWC3_DEPEVT_EPCMDCMPLT:
845 break;
846 }
847}