blob: d6bfc73dedbd952cd92b2b83693dc4a236913d6f [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;
129 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300130 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300131
132 req->request.actual = 0;
133 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300134 req->epnum = dep->number;
135
136 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300137
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300138 /*
139 * Gadget driver might not be quick enough to queue a request
140 * before we get a Transfer Not Ready event on this endpoint.
141 *
142 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
143 * flag is set, it's telling us that as soon as Gadget queues the
144 * required request, we should kick the transfer here because the
145 * IRQ we were waiting for is long gone.
146 */
147 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300148 unsigned direction;
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);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100168
169 } else if (dwc->delayed_status && (dwc->ep0state == EP0_STATUS_PHASE)) {
170 dwc->delayed_status = false;
171 dwc3_ep0_do_control_status(dwc, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300172 }
173
174 return ret;
175}
176
177int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
178 gfp_t gfp_flags)
179{
180 struct dwc3_request *req = to_dwc3_request(request);
181 struct dwc3_ep *dep = to_dwc3_ep(ep);
182 struct dwc3 *dwc = dep->dwc;
183
184 unsigned long flags;
185
186 int ret;
187
Felipe Balbi72246da2011-08-19 18:10:58 +0300188 spin_lock_irqsave(&dwc->lock, flags);
189 if (!dep->desc) {
190 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
191 request, dep->name);
192 ret = -ESHUTDOWN;
193 goto out;
194 }
195
196 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200197 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300198 ret = -EBUSY;
199 goto out;
200 }
201
202 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
203 request, dep->name, request->length,
204 dwc3_ep0_state_string(dwc->ep0state));
205
206 ret = __dwc3_gadget_ep0_queue(dep, req);
207
208out:
209 spin_unlock_irqrestore(&dwc->lock, flags);
210
211 return ret;
212}
213
214static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
215{
Felipe Balbid7422202011-09-08 18:17:12 +0300216 struct dwc3_ep *dep = dwc->eps[0];
217
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200219 __dwc3_gadget_ep_set_halt(dep, 1);
220 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100221 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300222
223 if (!list_empty(&dep->request_list)) {
224 struct dwc3_request *req;
225
226 req = next_request(&dep->request_list);
227 dwc3_gadget_giveback(dep, req, -ECONNRESET);
228 }
229
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300230 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300231 dwc3_ep0_out_start(dwc);
232}
233
234void dwc3_ep0_out_start(struct dwc3 *dwc)
235{
Felipe Balbi72246da2011-08-19 18:10:58 +0300236 int ret;
237
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300238 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
239 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 WARN_ON(ret < 0);
241}
242
Felipe Balbi72246da2011-08-19 18:10:58 +0300243static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
244{
245 struct dwc3_ep *dep;
246 u32 windex = le16_to_cpu(wIndex_le);
247 u32 epnum;
248
249 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
250 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
251 epnum |= 1;
252
253 dep = dwc->eps[epnum];
254 if (dep->flags & DWC3_EP_ENABLED)
255 return dep;
256
257 return NULL;
258}
259
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200260static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300261{
Felipe Balbi72246da2011-08-19 18:10:58 +0300262}
Felipe Balbi72246da2011-08-19 18:10:58 +0300263/*
264 * ch 9.4.5
265 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200266static int dwc3_ep0_handle_status(struct dwc3 *dwc,
267 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300268{
269 struct dwc3_ep *dep;
270 u32 recip;
271 u16 usb_status = 0;
272 __le16 *response_pkt;
273
274 recip = ctrl->bRequestType & USB_RECIP_MASK;
275 switch (recip) {
276 case USB_RECIP_DEVICE:
277 /*
278 * We are self-powered. U1/U2/LTM will be set later
279 * once we handle this states. RemoteWakeup is 0 on SS
280 */
281 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
282 break;
283
284 case USB_RECIP_INTERFACE:
285 /*
286 * Function Remote Wake Capable D0
287 * Function Remote Wakeup D1
288 */
289 break;
290
291 case USB_RECIP_ENDPOINT:
292 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
293 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200294 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300295
296 if (dep->flags & DWC3_EP_STALL)
297 usb_status = 1 << USB_ENDPOINT_HALT;
298 break;
299 default:
300 return -EINVAL;
301 };
302
303 response_pkt = (__le16 *) dwc->setup_buf;
304 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200305
306 dep = dwc->eps[0];
307 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100308 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
309 dwc->ep0_usb_req.request.dma = dwc->setup_buf_addr;
310 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200311
312 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300313}
314
315static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
316 struct usb_ctrlrequest *ctrl, int set)
317{
318 struct dwc3_ep *dep;
319 u32 recip;
320 u32 wValue;
321 u32 wIndex;
322 u32 reg;
323 int ret;
324 u32 mode;
325
326 wValue = le16_to_cpu(ctrl->wValue);
327 wIndex = le16_to_cpu(ctrl->wIndex);
328 recip = ctrl->bRequestType & USB_RECIP_MASK;
329 switch (recip) {
330 case USB_RECIP_DEVICE:
331
332 /*
333 * 9.4.1 says only only for SS, in AddressState only for
334 * default control pipe
335 */
336 switch (wValue) {
337 case USB_DEVICE_U1_ENABLE:
338 case USB_DEVICE_U2_ENABLE:
339 case USB_DEVICE_LTM_ENABLE:
340 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
341 return -EINVAL;
342 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
343 return -EINVAL;
344 }
345
346 /* XXX add U[12] & LTM */
347 switch (wValue) {
348 case USB_DEVICE_REMOTE_WAKEUP:
349 break;
350 case USB_DEVICE_U1_ENABLE:
351 break;
352 case USB_DEVICE_U2_ENABLE:
353 break;
354 case USB_DEVICE_LTM_ENABLE:
355 break;
356
357 case USB_DEVICE_TEST_MODE:
358 if ((wIndex & 0xff) != 0)
359 return -EINVAL;
360 if (!set)
361 return -EINVAL;
362
363 mode = wIndex >> 8;
364 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
365 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
366
367 switch (mode) {
368 case TEST_J:
369 case TEST_K:
370 case TEST_SE0_NAK:
371 case TEST_PACKET:
372 case TEST_FORCE_EN:
373 reg |= mode << 1;
374 break;
375 default:
376 return -EINVAL;
377 }
378 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
379 break;
380 default:
381 return -EINVAL;
382 }
383 break;
384
385 case USB_RECIP_INTERFACE:
386 switch (wValue) {
387 case USB_INTRF_FUNC_SUSPEND:
388 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
389 /* XXX enable Low power suspend */
390 ;
391 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
392 /* XXX enable remote wakeup */
393 ;
394 break;
395 default:
396 return -EINVAL;
397 }
398 break;
399
400 case USB_RECIP_ENDPOINT:
401 switch (wValue) {
402 case USB_ENDPOINT_HALT:
Sebastian Andrzej Siewior1e7618d2011-10-24 12:09:39 +0300403 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300404 if (!dep)
405 return -EINVAL;
406 ret = __dwc3_gadget_ep_set_halt(dep, set);
407 if (ret)
408 return -EINVAL;
409 break;
410 default:
411 return -EINVAL;
412 }
413 break;
414
415 default:
416 return -EINVAL;
417 };
418
Felipe Balbi72246da2011-08-19 18:10:58 +0300419 return 0;
420}
421
422static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
423{
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 u32 addr;
425 u32 reg;
426
427 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300428 if (addr > 127) {
429 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300431 }
432
433 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
434 dev_dbg(dwc->dev, "trying to set address when configured\n");
435 return -EINVAL;
436 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300437
Felipe Balbi26460212011-09-30 10:58:36 +0300438 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
439 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
440 reg |= DWC3_DCFG_DEVADDR(addr);
441 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300442
Felipe Balbi26460212011-09-30 10:58:36 +0300443 if (addr)
444 dwc->dev_state = DWC3_ADDRESS_STATE;
445 else
446 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300447
Felipe Balbi26460212011-09-30 10:58:36 +0300448 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449}
450
451static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
452{
453 int ret;
454
455 spin_unlock(&dwc->lock);
456 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
457 spin_lock(&dwc->lock);
458 return ret;
459}
460
461static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
462{
463 u32 cfg;
464 int ret;
465
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300466 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300467 cfg = le16_to_cpu(ctrl->wValue);
468
469 switch (dwc->dev_state) {
470 case DWC3_DEFAULT_STATE:
471 return -EINVAL;
472 break;
473
474 case DWC3_ADDRESS_STATE:
475 ret = dwc3_ep0_delegate_req(dwc, ctrl);
476 /* if the cfg matches and the cfg is non zero */
477 if (!ret && cfg)
478 dwc->dev_state = DWC3_CONFIGURED_STATE;
479 break;
480
481 case DWC3_CONFIGURED_STATE:
482 ret = dwc3_ep0_delegate_req(dwc, ctrl);
483 if (!cfg)
484 dwc->dev_state = DWC3_ADDRESS_STATE;
485 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100486 default:
487 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100489 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300490}
491
492static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
493{
494 int ret;
495
496 switch (ctrl->bRequest) {
497 case USB_REQ_GET_STATUS:
498 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
499 ret = dwc3_ep0_handle_status(dwc, ctrl);
500 break;
501 case USB_REQ_CLEAR_FEATURE:
502 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
503 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
504 break;
505 case USB_REQ_SET_FEATURE:
506 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
507 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
508 break;
509 case USB_REQ_SET_ADDRESS:
510 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
511 ret = dwc3_ep0_set_address(dwc, ctrl);
512 break;
513 case USB_REQ_SET_CONFIGURATION:
514 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
515 ret = dwc3_ep0_set_config(dwc, ctrl);
516 break;
517 default:
518 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
519 ret = dwc3_ep0_delegate_req(dwc, ctrl);
520 break;
521 };
522
523 return ret;
524}
525
526static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
527 const struct dwc3_event_depevt *event)
528{
529 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
530 int ret;
531 u32 len;
532
533 if (!dwc->gadget_driver)
534 goto err;
535
536 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300537 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300538 dwc->three_stage_setup = false;
539 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300540 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
541 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300542 dwc->three_stage_setup = true;
543 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300544 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
545 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300546
547 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
548 ret = dwc3_ep0_std_request(dwc, ctrl);
549 else
550 ret = dwc3_ep0_delegate_req(dwc, ctrl);
551
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100552 if (ret == USB_GADGET_DELAYED_STATUS)
553 dwc->delayed_status = true;
554
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 if (ret >= 0)
556 return;
557
558err:
559 dwc3_ep0_stall_and_restart(dwc);
560}
561
562static void dwc3_ep0_complete_data(struct dwc3 *dwc,
563 const struct dwc3_event_depevt *event)
564{
565 struct dwc3_request *r = NULL;
566 struct usb_request *ur;
567 struct dwc3_trb trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200568 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300569 u32 transferred;
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 u8 epnum;
571
572 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200573 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300574
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300575 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
576
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200577 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200578 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
580 dwc3_trb_to_nat(dwc->ep0_trb, &trb);
581
Felipe Balbia6829702011-08-27 22:18:09 +0300582 if (dwc->ep0_bounced) {
Felipe Balbia6829702011-08-27 22:18:09 +0300583
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300584 transferred = min_t(u32, ur->length,
585 ep0->endpoint.maxpacket - trb.length);
Felipe Balbia6829702011-08-27 22:18:09 +0300586 memcpy(ur->buf, dwc->ep0_bounce, transferred);
587 dwc->ep0_bounced = false;
588 } else {
589 transferred = ur->length - trb.length;
590 ur->actual += transferred;
591 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
593 if ((epnum & 1) && ur->actual < ur->length) {
594 /* for some reason we did not get everything out */
595
596 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 } else {
598 /*
599 * handle the case where we have to send a zero packet. This
600 * seems to be case when req.length > maxpacket. Could it be?
601 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300602 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200603 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 }
605}
606
607static void dwc3_ep0_complete_req(struct dwc3 *dwc,
608 const struct dwc3_event_depevt *event)
609{
610 struct dwc3_request *r;
611 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300612
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300613 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300614
615 if (!list_empty(&dep->request_list)) {
616 r = next_request(&dep->request_list);
617
618 dwc3_gadget_giveback(dep, r, 0);
619 }
620
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300621 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 dwc3_ep0_out_start(dwc);
623}
624
625static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
626 const struct dwc3_event_depevt *event)
627{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300628 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
629
630 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbidf62df52011-10-14 15:11:49 +0300631 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300632
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300634 case EP0_SETUP_PHASE:
635 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 dwc3_ep0_inspect_setup(dwc, event);
637 break;
638
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300639 case EP0_DATA_PHASE:
640 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 dwc3_ep0_complete_data(dwc, event);
642 break;
643
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300644 case EP0_STATUS_PHASE:
645 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 dwc3_ep0_complete_req(dwc, event);
647 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300648 default:
649 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 }
651}
652
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300653static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
654 const struct dwc3_event_depevt *event)
655{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300656 dwc3_ep0_out_start(dwc);
657}
658
659static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
660 const struct dwc3_event_depevt *event)
661{
662 struct dwc3_ep *dep;
663 struct dwc3_request *req;
664 int ret;
665
666 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300667
668 if (list_empty(&dep->request_list)) {
669 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
670 dep->flags |= DWC3_EP_PENDING_REQUEST;
671
672 if (event->endpoint_number)
673 dep->flags |= DWC3_EP0_DIR_IN;
674 return;
675 }
676
677 req = next_request(&dep->request_list);
678 req->direction = !!event->endpoint_number;
679
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300680 if (req->request.length == 0) {
681 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
682 dwc->ctrl_req_addr, 0,
683 DWC3_TRBCTL_CONTROL_DATA);
684 } else if ((req->request.length % dep->endpoint.maxpacket)
685 && (event->endpoint_number == 0)) {
686 dwc3_map_buffer_to_dma(req);
687
688 WARN_ON(req->request.length > dep->endpoint.maxpacket);
689
690 dwc->ep0_bounced = true;
691
692 /*
693 * REVISIT in case request length is bigger than EP0
694 * wMaxPacketSize, we will need two chained TRBs to handle
695 * the transfer.
696 */
697 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
698 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
699 DWC3_TRBCTL_CONTROL_DATA);
700 } else {
701 dwc3_map_buffer_to_dma(req);
702
703 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
704 req->request.dma, req->request.length,
705 DWC3_TRBCTL_CONTROL_DATA);
706 }
707
708 WARN_ON(ret < 0);
709}
710
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100711static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300712{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100713 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300714 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300715
716 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
717 : DWC3_TRBCTL_CONTROL_STATUS2;
718
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100719 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300720 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100721}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300722
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100723static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
724{
725 struct dwc3_ep *dep = dwc->eps[epnum];
726
727 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300728}
729
Felipe Balbi72246da2011-08-19 18:10:58 +0300730static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
731 const struct dwc3_event_depevt *event)
732{
Felipe Balbidf62df52011-10-14 15:11:49 +0300733 dwc->setup_packet_pending = true;
734
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300735 /*
736 * This part is very tricky: If we has just handled
737 * XferNotReady(Setup) and we're now expecting a
738 * XferComplete but, instead, we receive another
739 * XferNotReady(Setup), we should STALL and restart
740 * the state machine.
741 *
742 * In all other cases, we just continue waiting
743 * for the XferComplete event.
744 *
745 * We are a little bit unsafe here because we're
746 * not trying to ensure that last event was, indeed,
747 * XferNotReady(Setup).
748 *
749 * Still, we don't expect any condition where that
750 * should happen and, even if it does, it would be
751 * another error condition.
752 */
753 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
754 switch (event->status) {
755 case DEPEVT_STATUS_CONTROL_SETUP:
756 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
757 dwc3_ep0_stall_and_restart(dwc);
758 break;
759 case DEPEVT_STATUS_CONTROL_DATA:
760 /* FALLTHROUGH */
761 case DEPEVT_STATUS_CONTROL_STATUS:
762 /* FALLTHROUGH */
763 default:
764 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
765 }
766
767 return;
768 }
769
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300770 switch (event->status) {
771 case DEPEVT_STATUS_CONTROL_SETUP:
772 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100773
774 dwc->ep0state = EP0_SETUP_PHASE;
775
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300776 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300778
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300779 case DEPEVT_STATUS_CONTROL_DATA:
780 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300781
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100782 dwc->ep0state = EP0_DATA_PHASE;
783
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300784 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
785 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300786 dwc->ep0_next_event,
787 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300788
789 dwc3_ep0_stall_and_restart(dwc);
790 return;
791 }
792
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300793 /*
794 * One of the possible error cases is when Host _does_
795 * request for Data Phase, but it does so on the wrong
796 * direction.
797 *
798 * Here, we already know ep0_next_event is DATA (see above),
799 * so we only need to check for direction.
800 */
801 if (dwc->ep0_expect_in != event->endpoint_number) {
802 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
803 dwc3_ep0_stall_and_restart(dwc);
804 return;
805 }
806
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300807 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300809
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300810 case DEPEVT_STATUS_CONTROL_STATUS:
811 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300812
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100813 dwc->ep0state = EP0_STATUS_PHASE;
814
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300815 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
816 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300817 dwc->ep0_next_event,
818 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300819
820 dwc3_ep0_stall_and_restart(dwc);
821 return;
822 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100823
824 if (dwc->delayed_status) {
825 WARN_ON_ONCE(event->endpoint_number != 1);
826 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
827 return;
828 }
829
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100830 dwc3_ep0_do_control_status(dwc, event->endpoint_number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300831 }
832}
833
834void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +0200835 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +0300836{
837 u8 epnum = event->endpoint_number;
838
839 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
840 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300841 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300842 dwc3_ep0_state_string(dwc->ep0state));
843
844 switch (event->endpoint_event) {
845 case DWC3_DEPEVT_XFERCOMPLETE:
846 dwc3_ep0_xfer_complete(dwc, event);
847 break;
848
849 case DWC3_DEPEVT_XFERNOTREADY:
850 dwc3_ep0_xfernotready(dwc, event);
851 break;
852
853 case DWC3_DEPEVT_XFERINPROGRESS:
854 case DWC3_DEPEVT_RXTXFIFOEVT:
855 case DWC3_DEPEVT_STREAMEVT:
856 case DWC3_DEPEVT_EPCMDCMPLT:
857 break;
858 }
859}