blob: 24137d8563c9c9bddc718d7557532739734ca9f6 [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);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200168 } else if (dwc->delayed_status) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100169 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200170
171 if (dwc->ep0state == EP0_STATUS_PHASE)
172 dwc3_ep0_do_control_status(dwc, 1);
173 else
174 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300175 }
176
177 return ret;
178}
179
180int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
181 gfp_t gfp_flags)
182{
183 struct dwc3_request *req = to_dwc3_request(request);
184 struct dwc3_ep *dep = to_dwc3_ep(ep);
185 struct dwc3 *dwc = dep->dwc;
186
187 unsigned long flags;
188
189 int ret;
190
Felipe Balbi72246da2011-08-19 18:10:58 +0300191 spin_lock_irqsave(&dwc->lock, flags);
192 if (!dep->desc) {
193 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
194 request, dep->name);
195 ret = -ESHUTDOWN;
196 goto out;
197 }
198
199 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200200 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 ret = -EBUSY;
202 goto out;
203 }
204
205 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
206 request, dep->name, request->length,
207 dwc3_ep0_state_string(dwc->ep0state));
208
209 ret = __dwc3_gadget_ep0_queue(dep, req);
210
211out:
212 spin_unlock_irqrestore(&dwc->lock, flags);
213
214 return ret;
215}
216
217static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
218{
Felipe Balbid7422202011-09-08 18:17:12 +0300219 struct dwc3_ep *dep = dwc->eps[0];
220
Felipe Balbi72246da2011-08-19 18:10:58 +0300221 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200222 __dwc3_gadget_ep_set_halt(dep, 1);
223 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100224 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300225
226 if (!list_empty(&dep->request_list)) {
227 struct dwc3_request *req;
228
229 req = next_request(&dep->request_list);
230 dwc3_gadget_giveback(dep, req, -ECONNRESET);
231 }
232
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300233 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 dwc3_ep0_out_start(dwc);
235}
236
237void dwc3_ep0_out_start(struct dwc3 *dwc)
238{
Felipe Balbi72246da2011-08-19 18:10:58 +0300239 int ret;
240
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300241 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
242 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300243 WARN_ON(ret < 0);
244}
245
Felipe Balbi72246da2011-08-19 18:10:58 +0300246static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
247{
248 struct dwc3_ep *dep;
249 u32 windex = le16_to_cpu(wIndex_le);
250 u32 epnum;
251
252 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
253 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
254 epnum |= 1;
255
256 dep = dwc->eps[epnum];
257 if (dep->flags & DWC3_EP_ENABLED)
258 return dep;
259
260 return NULL;
261}
262
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200263static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300264{
Felipe Balbi72246da2011-08-19 18:10:58 +0300265}
Felipe Balbi72246da2011-08-19 18:10:58 +0300266/*
267 * ch 9.4.5
268 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200269static int dwc3_ep0_handle_status(struct dwc3 *dwc,
270 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300271{
272 struct dwc3_ep *dep;
273 u32 recip;
274 u16 usb_status = 0;
275 __le16 *response_pkt;
276
277 recip = ctrl->bRequestType & USB_RECIP_MASK;
278 switch (recip) {
279 case USB_RECIP_DEVICE:
280 /*
281 * We are self-powered. U1/U2/LTM will be set later
282 * once we handle this states. RemoteWakeup is 0 on SS
283 */
284 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
285 break;
286
287 case USB_RECIP_INTERFACE:
288 /*
289 * Function Remote Wake Capable D0
290 * Function Remote Wakeup D1
291 */
292 break;
293
294 case USB_RECIP_ENDPOINT:
295 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
296 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200297 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300298
299 if (dep->flags & DWC3_EP_STALL)
300 usb_status = 1 << USB_ENDPOINT_HALT;
301 break;
302 default:
303 return -EINVAL;
304 };
305
306 response_pkt = (__le16 *) dwc->setup_buf;
307 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200308
309 dep = dwc->eps[0];
310 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100311 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200312 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100313 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200314
315 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300316}
317
318static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
319 struct usb_ctrlrequest *ctrl, int set)
320{
321 struct dwc3_ep *dep;
322 u32 recip;
323 u32 wValue;
324 u32 wIndex;
325 u32 reg;
326 int ret;
327 u32 mode;
328
329 wValue = le16_to_cpu(ctrl->wValue);
330 wIndex = le16_to_cpu(ctrl->wIndex);
331 recip = ctrl->bRequestType & USB_RECIP_MASK;
332 switch (recip) {
333 case USB_RECIP_DEVICE:
334
335 /*
336 * 9.4.1 says only only for SS, in AddressState only for
337 * default control pipe
338 */
339 switch (wValue) {
340 case USB_DEVICE_U1_ENABLE:
341 case USB_DEVICE_U2_ENABLE:
342 case USB_DEVICE_LTM_ENABLE:
343 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
344 return -EINVAL;
345 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
346 return -EINVAL;
347 }
348
349 /* XXX add U[12] & LTM */
350 switch (wValue) {
351 case USB_DEVICE_REMOTE_WAKEUP:
352 break;
353 case USB_DEVICE_U1_ENABLE:
354 break;
355 case USB_DEVICE_U2_ENABLE:
356 break;
357 case USB_DEVICE_LTM_ENABLE:
358 break;
359
360 case USB_DEVICE_TEST_MODE:
361 if ((wIndex & 0xff) != 0)
362 return -EINVAL;
363 if (!set)
364 return -EINVAL;
365
366 mode = wIndex >> 8;
367 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
368 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
369
370 switch (mode) {
371 case TEST_J:
372 case TEST_K:
373 case TEST_SE0_NAK:
374 case TEST_PACKET:
375 case TEST_FORCE_EN:
376 reg |= mode << 1;
377 break;
378 default:
379 return -EINVAL;
380 }
381 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
382 break;
383 default:
384 return -EINVAL;
385 }
386 break;
387
388 case USB_RECIP_INTERFACE:
389 switch (wValue) {
390 case USB_INTRF_FUNC_SUSPEND:
391 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
392 /* XXX enable Low power suspend */
393 ;
394 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
395 /* XXX enable remote wakeup */
396 ;
397 break;
398 default:
399 return -EINVAL;
400 }
401 break;
402
403 case USB_RECIP_ENDPOINT:
404 switch (wValue) {
405 case USB_ENDPOINT_HALT:
Sebastian Andrzej Siewior1e7618d2011-10-24 12:09:39 +0300406 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300407 if (!dep)
408 return -EINVAL;
409 ret = __dwc3_gadget_ep_set_halt(dep, set);
410 if (ret)
411 return -EINVAL;
412 break;
413 default:
414 return -EINVAL;
415 }
416 break;
417
418 default:
419 return -EINVAL;
420 };
421
Felipe Balbi72246da2011-08-19 18:10:58 +0300422 return 0;
423}
424
425static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
426{
Felipe Balbi72246da2011-08-19 18:10:58 +0300427 u32 addr;
428 u32 reg;
429
430 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300431 if (addr > 127) {
432 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300433 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300434 }
435
436 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
437 dev_dbg(dwc->dev, "trying to set address when configured\n");
438 return -EINVAL;
439 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300440
Felipe Balbi26460212011-09-30 10:58:36 +0300441 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
442 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
443 reg |= DWC3_DCFG_DEVADDR(addr);
444 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300445
Felipe Balbi26460212011-09-30 10:58:36 +0300446 if (addr)
447 dwc->dev_state = DWC3_ADDRESS_STATE;
448 else
449 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300450
Felipe Balbi26460212011-09-30 10:58:36 +0300451 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300452}
453
454static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
455{
456 int ret;
457
458 spin_unlock(&dwc->lock);
459 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
460 spin_lock(&dwc->lock);
461 return ret;
462}
463
464static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
465{
466 u32 cfg;
467 int ret;
468
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300469 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 cfg = le16_to_cpu(ctrl->wValue);
471
472 switch (dwc->dev_state) {
473 case DWC3_DEFAULT_STATE:
474 return -EINVAL;
475 break;
476
477 case DWC3_ADDRESS_STATE:
478 ret = dwc3_ep0_delegate_req(dwc, ctrl);
479 /* if the cfg matches and the cfg is non zero */
480 if (!ret && cfg)
481 dwc->dev_state = DWC3_CONFIGURED_STATE;
482 break;
483
484 case DWC3_CONFIGURED_STATE:
485 ret = dwc3_ep0_delegate_req(dwc, ctrl);
486 if (!cfg)
487 dwc->dev_state = DWC3_ADDRESS_STATE;
488 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100489 default:
490 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300491 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100492 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300493}
494
495static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
496{
497 int ret;
498
499 switch (ctrl->bRequest) {
500 case USB_REQ_GET_STATUS:
501 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
502 ret = dwc3_ep0_handle_status(dwc, ctrl);
503 break;
504 case USB_REQ_CLEAR_FEATURE:
505 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
506 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
507 break;
508 case USB_REQ_SET_FEATURE:
509 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
510 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
511 break;
512 case USB_REQ_SET_ADDRESS:
513 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
514 ret = dwc3_ep0_set_address(dwc, ctrl);
515 break;
516 case USB_REQ_SET_CONFIGURATION:
517 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
518 ret = dwc3_ep0_set_config(dwc, ctrl);
519 break;
520 default:
521 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
522 ret = dwc3_ep0_delegate_req(dwc, ctrl);
523 break;
524 };
525
526 return ret;
527}
528
529static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
530 const struct dwc3_event_depevt *event)
531{
532 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
533 int ret;
534 u32 len;
535
536 if (!dwc->gadget_driver)
537 goto err;
538
539 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300540 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300541 dwc->three_stage_setup = false;
542 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300543 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
544 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300545 dwc->three_stage_setup = true;
546 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300547 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
548 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
550 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
551 ret = dwc3_ep0_std_request(dwc, ctrl);
552 else
553 ret = dwc3_ep0_delegate_req(dwc, ctrl);
554
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100555 if (ret == USB_GADGET_DELAYED_STATUS)
556 dwc->delayed_status = true;
557
Felipe Balbi72246da2011-08-19 18:10:58 +0300558 if (ret >= 0)
559 return;
560
561err:
562 dwc3_ep0_stall_and_restart(dwc);
563}
564
565static void dwc3_ep0_complete_data(struct dwc3 *dwc,
566 const struct dwc3_event_depevt *event)
567{
568 struct dwc3_request *r = NULL;
569 struct usb_request *ur;
570 struct dwc3_trb trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200571 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300572 u32 transferred;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573 u8 epnum;
574
575 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200576 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300577
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300578 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
579
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200580 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200581 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300582
583 dwc3_trb_to_nat(dwc->ep0_trb, &trb);
584
Felipe Balbia6829702011-08-27 22:18:09 +0300585 if (dwc->ep0_bounced) {
Felipe Balbia6829702011-08-27 22:18:09 +0300586
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300587 transferred = min_t(u32, ur->length,
588 ep0->endpoint.maxpacket - trb.length);
Felipe Balbia6829702011-08-27 22:18:09 +0300589 memcpy(ur->buf, dwc->ep0_bounce, transferred);
590 dwc->ep0_bounced = false;
591 } else {
592 transferred = ur->length - trb.length;
593 ur->actual += transferred;
594 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300595
596 if ((epnum & 1) && ur->actual < ur->length) {
597 /* for some reason we did not get everything out */
598
599 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300600 } else {
601 /*
602 * handle the case where we have to send a zero packet. This
603 * seems to be case when req.length > maxpacket. Could it be?
604 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300605 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200606 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 }
608}
609
610static void dwc3_ep0_complete_req(struct dwc3 *dwc,
611 const struct dwc3_event_depevt *event)
612{
613 struct dwc3_request *r;
614 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300616 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300617
618 if (!list_empty(&dep->request_list)) {
619 r = next_request(&dep->request_list);
620
621 dwc3_gadget_giveback(dep, r, 0);
622 }
623
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300624 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 dwc3_ep0_out_start(dwc);
626}
627
628static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
629 const struct dwc3_event_depevt *event)
630{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300631 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
632
633 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbidf62df52011-10-14 15:11:49 +0300634 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300635
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300637 case EP0_SETUP_PHASE:
638 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300639 dwc3_ep0_inspect_setup(dwc, event);
640 break;
641
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300642 case EP0_DATA_PHASE:
643 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 dwc3_ep0_complete_data(dwc, event);
645 break;
646
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300647 case EP0_STATUS_PHASE:
648 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 dwc3_ep0_complete_req(dwc, event);
650 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300651 default:
652 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300653 }
654}
655
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300656static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
657 const struct dwc3_event_depevt *event)
658{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300659 dwc3_ep0_out_start(dwc);
660}
661
662static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
663 const struct dwc3_event_depevt *event)
664{
665 struct dwc3_ep *dep;
666 struct dwc3_request *req;
667 int ret;
668
669 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300670
671 if (list_empty(&dep->request_list)) {
672 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
673 dep->flags |= DWC3_EP_PENDING_REQUEST;
674
675 if (event->endpoint_number)
676 dep->flags |= DWC3_EP0_DIR_IN;
677 return;
678 }
679
680 req = next_request(&dep->request_list);
681 req->direction = !!event->endpoint_number;
682
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300683 if (req->request.length == 0) {
684 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
685 dwc->ctrl_req_addr, 0,
686 DWC3_TRBCTL_CONTROL_DATA);
687 } else if ((req->request.length % dep->endpoint.maxpacket)
688 && (event->endpoint_number == 0)) {
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200689 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
690 event->endpoint_number);
691 if (ret) {
692 dev_dbg(dwc->dev, "failed to map request\n");
693 return;
694 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300695
696 WARN_ON(req->request.length > dep->endpoint.maxpacket);
697
698 dwc->ep0_bounced = true;
699
700 /*
701 * REVISIT in case request length is bigger than EP0
702 * wMaxPacketSize, we will need two chained TRBs to handle
703 * the transfer.
704 */
705 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
706 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
707 DWC3_TRBCTL_CONTROL_DATA);
708 } else {
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200709 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
710 event->endpoint_number);
711 if (ret) {
712 dev_dbg(dwc->dev, "failed to map request\n");
713 return;
714 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300715
716 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
717 req->request.dma, req->request.length,
718 DWC3_TRBCTL_CONTROL_DATA);
719 }
720
721 WARN_ON(ret < 0);
722}
723
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100724static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300725{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100726 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300727 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300728
729 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
730 : DWC3_TRBCTL_CONTROL_STATUS2;
731
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100732 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300733 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100734}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300735
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100736static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
737{
738 struct dwc3_ep *dep = dwc->eps[epnum];
739
740 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300741}
742
Felipe Balbi72246da2011-08-19 18:10:58 +0300743static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
744 const struct dwc3_event_depevt *event)
745{
Felipe Balbidf62df52011-10-14 15:11:49 +0300746 dwc->setup_packet_pending = true;
747
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300748 /*
749 * This part is very tricky: If we has just handled
750 * XferNotReady(Setup) and we're now expecting a
751 * XferComplete but, instead, we receive another
752 * XferNotReady(Setup), we should STALL and restart
753 * the state machine.
754 *
755 * In all other cases, we just continue waiting
756 * for the XferComplete event.
757 *
758 * We are a little bit unsafe here because we're
759 * not trying to ensure that last event was, indeed,
760 * XferNotReady(Setup).
761 *
762 * Still, we don't expect any condition where that
763 * should happen and, even if it does, it would be
764 * another error condition.
765 */
766 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
767 switch (event->status) {
768 case DEPEVT_STATUS_CONTROL_SETUP:
769 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
770 dwc3_ep0_stall_and_restart(dwc);
771 break;
772 case DEPEVT_STATUS_CONTROL_DATA:
773 /* FALLTHROUGH */
774 case DEPEVT_STATUS_CONTROL_STATUS:
775 /* FALLTHROUGH */
776 default:
777 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
778 }
779
780 return;
781 }
782
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300783 switch (event->status) {
784 case DEPEVT_STATUS_CONTROL_SETUP:
785 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100786
787 dwc->ep0state = EP0_SETUP_PHASE;
788
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300789 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300790 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300791
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300792 case DEPEVT_STATUS_CONTROL_DATA:
793 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300794
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100795 dwc->ep0state = EP0_DATA_PHASE;
796
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300797 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
798 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300799 dwc->ep0_next_event,
800 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300801
802 dwc3_ep0_stall_and_restart(dwc);
803 return;
804 }
805
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300806 /*
807 * One of the possible error cases is when Host _does_
808 * request for Data Phase, but it does so on the wrong
809 * direction.
810 *
811 * Here, we already know ep0_next_event is DATA (see above),
812 * so we only need to check for direction.
813 */
814 if (dwc->ep0_expect_in != event->endpoint_number) {
815 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
816 dwc3_ep0_stall_and_restart(dwc);
817 return;
818 }
819
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300820 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300821 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300822
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300823 case DEPEVT_STATUS_CONTROL_STATUS:
824 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300825
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100826 dwc->ep0state = EP0_STATUS_PHASE;
827
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300828 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
829 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300830 dwc->ep0_next_event,
831 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300832
833 dwc3_ep0_stall_and_restart(dwc);
834 return;
835 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100836
837 if (dwc->delayed_status) {
838 WARN_ON_ONCE(event->endpoint_number != 1);
839 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
840 return;
841 }
842
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100843 dwc3_ep0_do_control_status(dwc, event->endpoint_number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 }
845}
846
847void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +0200848 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +0300849{
850 u8 epnum = event->endpoint_number;
851
852 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
853 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300854 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300855 dwc3_ep0_state_string(dwc->ep0state));
856
857 switch (event->endpoint_event) {
858 case DWC3_DEPEVT_XFERCOMPLETE:
859 dwc3_ep0_xfer_complete(dwc, event);
860 break;
861
862 case DWC3_DEPEVT_XFERNOTREADY:
863 dwc3_ep0_xfernotready(dwc, event);
864 break;
865
866 case DWC3_DEPEVT_XFERINPROGRESS:
867 case DWC3_DEPEVT_RXTXFIFOEVT:
868 case DWC3_DEPEVT_STREAMEVT:
869 case DWC3_DEPEVT_EPCMDCMPLT:
870 break;
871 }
872}