blob: 8d41b6a6192a803c1e565d3db2f96eabfb04cb87 [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;
Felipe Balbif6bafc62012-02-06 11:04:53 +020079 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030080 struct dwc3_ep *dep;
81
82 int ret;
83
84 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030085 if (dep->flags & DWC3_EP_BUSY) {
86 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
87 return 0;
88 }
Felipe Balbi72246da2011-08-19 18:10:58 +030089
Felipe Balbif6bafc62012-02-06 11:04:53 +020090 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030091
Felipe Balbif6bafc62012-02-06 11:04:53 +020092 trb->bpl = lower_32_bits(buf_dma);
93 trb->bph = upper_32_bits(buf_dma);
94 trb->size = len;
95 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030096
Felipe Balbif6bafc62012-02-06 11:04:53 +020097 trb->ctrl |= (DWC3_TRB_CTRL_HWO
98 | DWC3_TRB_CTRL_LST
99 | DWC3_TRB_CTRL_IOC
100 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +0300101
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{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100125 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300126 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300127
128 req->request.actual = 0;
129 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300130 req->epnum = dep->number;
131
132 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300133
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300134 /*
135 * Gadget driver might not be quick enough to queue a request
136 * before we get a Transfer Not Ready event on this endpoint.
137 *
138 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
139 * flag is set, it's telling us that as soon as Gadget queues the
140 * required request, we should kick the transfer here because the
141 * IRQ we were waiting for is long gone.
142 */
143 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300144 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300145
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300146 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300147
Felipe Balbi68d8a782011-12-29 06:32:29 +0200148 if (dwc->ep0state != EP0_DATA_PHASE) {
149 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300150 return 0;
151 }
Felipe Balbia6829702011-08-27 22:18:09 +0300152
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300153 ret = dwc3_ep0_start_trans(dwc, direction,
Felipe Balbi68d8a782011-12-29 06:32:29 +0200154 req->request.dma, req->request.length,
155 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300156 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
157 DWC3_EP0_DIR_IN);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200158 } else if (dwc->delayed_status) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100159 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200160
161 if (dwc->ep0state == EP0_STATUS_PHASE)
162 dwc3_ep0_do_control_status(dwc, 1);
163 else
164 dev_dbg(dwc->dev, "too early for delayed status\n");
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;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100214 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300215
216 if (!list_empty(&dep->request_list)) {
217 struct dwc3_request *req;
218
219 req = next_request(&dep->request_list);
220 dwc3_gadget_giveback(dep, req, -ECONNRESET);
221 }
222
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300223 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300224 dwc3_ep0_out_start(dwc);
225}
226
227void dwc3_ep0_out_start(struct dwc3 *dwc)
228{
Felipe Balbi72246da2011-08-19 18:10:58 +0300229 int ret;
230
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300231 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
232 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300233 WARN_ON(ret < 0);
234}
235
Felipe Balbi72246da2011-08-19 18:10:58 +0300236static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
237{
238 struct dwc3_ep *dep;
239 u32 windex = le16_to_cpu(wIndex_le);
240 u32 epnum;
241
242 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
243 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
244 epnum |= 1;
245
246 dep = dwc->eps[epnum];
247 if (dep->flags & DWC3_EP_ENABLED)
248 return dep;
249
250 return NULL;
251}
252
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200253static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300254{
Felipe Balbi72246da2011-08-19 18:10:58 +0300255}
Felipe Balbi72246da2011-08-19 18:10:58 +0300256/*
257 * ch 9.4.5
258 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200259static int dwc3_ep0_handle_status(struct dwc3 *dwc,
260 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300261{
262 struct dwc3_ep *dep;
263 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200264 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300265 u16 usb_status = 0;
266 __le16 *response_pkt;
267
268 recip = ctrl->bRequestType & USB_RECIP_MASK;
269 switch (recip) {
270 case USB_RECIP_DEVICE:
271 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200272 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 */
274 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200275
276 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
277 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
278 if (reg & DWC3_DCTL_INITU1ENA)
279 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
280 if (reg & DWC3_DCTL_INITU2ENA)
281 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
282 }
283
Felipe Balbi72246da2011-08-19 18:10:58 +0300284 break;
285
286 case USB_RECIP_INTERFACE:
287 /*
288 * Function Remote Wake Capable D0
289 * Function Remote Wakeup D1
290 */
291 break;
292
293 case USB_RECIP_ENDPOINT:
294 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
295 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200296 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300297
298 if (dep->flags & DWC3_EP_STALL)
299 usb_status = 1 << USB_ENDPOINT_HALT;
300 break;
301 default:
302 return -EINVAL;
303 };
304
305 response_pkt = (__le16 *) dwc->setup_buf;
306 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200307
308 dep = dwc->eps[0];
309 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100310 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200311 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100312 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200313
314 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300315}
316
317static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
318 struct usb_ctrlrequest *ctrl, int set)
319{
320 struct dwc3_ep *dep;
321 u32 recip;
322 u32 wValue;
323 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200324 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300325 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300326
327 wValue = le16_to_cpu(ctrl->wValue);
328 wIndex = le16_to_cpu(ctrl->wIndex);
329 recip = ctrl->bRequestType & USB_RECIP_MASK;
330 switch (recip) {
331 case USB_RECIP_DEVICE:
332
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200333 switch (wValue) {
334 case USB_DEVICE_REMOTE_WAKEUP:
335 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 /*
337 * 9.4.1 says only only for SS, in AddressState only for
338 * default control pipe
339 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300340 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300341 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
342 return -EINVAL;
343 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
344 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300345
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200346 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
347 if (set)
348 reg |= DWC3_DCTL_INITU1ENA;
349 else
350 reg &= ~DWC3_DCTL_INITU1ENA;
351 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300352 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200353
Felipe Balbi72246da2011-08-19 18:10:58 +0300354 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200355 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
356 return -EINVAL;
357 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
358 return -EINVAL;
359
360 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
361 if (set)
362 reg |= DWC3_DCTL_INITU2ENA;
363 else
364 reg &= ~DWC3_DCTL_INITU2ENA;
365 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200367
Felipe Balbi72246da2011-08-19 18:10:58 +0300368 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200369 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300370 break;
371
372 case USB_DEVICE_TEST_MODE:
373 if ((wIndex & 0xff) != 0)
374 return -EINVAL;
375 if (!set)
376 return -EINVAL;
377
Gerard Cauvy3b637362012-02-10 12:21:18 +0200378 dwc->test_mode_nr = wIndex >> 8;
379 dwc->test_mode = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300380 }
381 break;
382
383 case USB_RECIP_INTERFACE:
384 switch (wValue) {
385 case USB_INTRF_FUNC_SUSPEND:
386 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
387 /* XXX enable Low power suspend */
388 ;
389 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
390 /* XXX enable remote wakeup */
391 ;
392 break;
393 default:
394 return -EINVAL;
395 }
396 break;
397
398 case USB_RECIP_ENDPOINT:
399 switch (wValue) {
400 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800401 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300402 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 */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200475 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300476 dwc->dev_state = DWC3_CONFIGURED_STATE;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200477 dwc->resize_fifos = true;
478 dev_dbg(dwc->dev, "resize fifos flag SET\n");
479 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 break;
481
482 case DWC3_CONFIGURED_STATE:
483 ret = dwc3_ep0_delegate_req(dwc, ctrl);
484 if (!cfg)
485 dwc->dev_state = DWC3_ADDRESS_STATE;
486 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100487 default:
488 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100490 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300491}
492
Felipe Balbi865e09e2012-04-24 16:19:49 +0300493static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
494{
495 struct dwc3_ep *dep = to_dwc3_ep(ep);
496 struct dwc3 *dwc = dep->dwc;
497
498 u32 param = 0;
499 u32 reg;
500
501 struct timing {
502 u8 u1sel;
503 u8 u1pel;
504 u16 u2sel;
505 u16 u2pel;
506 } __packed timing;
507
508 int ret;
509
510 memcpy(&timing, req->buf, sizeof(timing));
511
512 dwc->u1sel = timing.u1sel;
513 dwc->u1pel = timing.u1pel;
514 dwc->u2sel = timing.u2sel;
515 dwc->u2pel = timing.u2pel;
516
517 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
518 if (reg & DWC3_DCTL_INITU2ENA)
519 param = dwc->u2pel;
520 if (reg & DWC3_DCTL_INITU1ENA)
521 param = dwc->u1pel;
522
523 /*
524 * According to Synopsys Databook, if parameter is
525 * greater than 125, a value of zero should be
526 * programmed in the register.
527 */
528 if (param > 125)
529 param = 0;
530
531 /* now that we have the time, issue DGCMD Set Sel */
532 ret = dwc3_send_gadget_generic_command(dwc,
533 DWC3_DGCMD_SET_PERIODIC_PAR, param);
534 WARN_ON(ret < 0);
535}
536
537static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
538{
539 struct dwc3_ep *dep;
540 u16 wLength;
541 u16 wValue;
542
543 if (dwc->dev_state == DWC3_DEFAULT_STATE)
544 return -EINVAL;
545
546 wValue = le16_to_cpu(ctrl->wValue);
547 wLength = le16_to_cpu(ctrl->wLength);
548
549 if (wLength != 6) {
550 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
551 wLength);
552 return -EINVAL;
553 }
554
555 /*
556 * To handle Set SEL we need to receive 6 bytes from Host. So let's
557 * queue a usb_request for 6 bytes.
558 *
559 * Remember, though, this controller can't handle non-wMaxPacketSize
560 * aligned transfers on the OUT direction, so we queue a request for
561 * wMaxPacketSize instead.
562 */
563 dep = dwc->eps[0];
564 dwc->ep0_usb_req.dep = dep;
565 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
566 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
567 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
568
569 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
570}
571
Felipe Balbic12a0d82012-04-25 10:45:05 +0300572static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
573{
574 u16 wLength;
575 u16 wValue;
576 u16 wIndex;
577
578 wValue = le16_to_cpu(ctrl->wValue);
579 wLength = le16_to_cpu(ctrl->wLength);
580 wIndex = le16_to_cpu(ctrl->wIndex);
581
582 if (wIndex || wLength)
583 return -EINVAL;
584
585 /*
586 * REVISIT It's unclear from Databook what to do with this
587 * value. For now, just cache it.
588 */
589 dwc->isoch_delay = wValue;
590
591 return 0;
592}
593
Felipe Balbi72246da2011-08-19 18:10:58 +0300594static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
595{
596 int ret;
597
598 switch (ctrl->bRequest) {
599 case USB_REQ_GET_STATUS:
600 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
601 ret = dwc3_ep0_handle_status(dwc, ctrl);
602 break;
603 case USB_REQ_CLEAR_FEATURE:
604 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
605 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
606 break;
607 case USB_REQ_SET_FEATURE:
608 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
609 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
610 break;
611 case USB_REQ_SET_ADDRESS:
612 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
613 ret = dwc3_ep0_set_address(dwc, ctrl);
614 break;
615 case USB_REQ_SET_CONFIGURATION:
616 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
617 ret = dwc3_ep0_set_config(dwc, ctrl);
618 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300619 case USB_REQ_SET_SEL:
620 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
621 ret = dwc3_ep0_set_sel(dwc, ctrl);
622 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300623 case USB_REQ_SET_ISOCH_DELAY:
624 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
625 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
626 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300627 default:
628 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
629 ret = dwc3_ep0_delegate_req(dwc, ctrl);
630 break;
631 };
632
633 return ret;
634}
635
636static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
637 const struct dwc3_event_depevt *event)
638{
639 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
640 int ret;
641 u32 len;
642
643 if (!dwc->gadget_driver)
644 goto err;
645
646 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300647 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300648 dwc->three_stage_setup = false;
649 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300650 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
651 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300652 dwc->three_stage_setup = true;
653 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300654 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
655 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300656
657 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
658 ret = dwc3_ep0_std_request(dwc, ctrl);
659 else
660 ret = dwc3_ep0_delegate_req(dwc, ctrl);
661
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100662 if (ret == USB_GADGET_DELAYED_STATUS)
663 dwc->delayed_status = true;
664
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 if (ret >= 0)
666 return;
667
668err:
669 dwc3_ep0_stall_and_restart(dwc);
670}
671
672static void dwc3_ep0_complete_data(struct dwc3 *dwc,
673 const struct dwc3_event_depevt *event)
674{
675 struct dwc3_request *r = NULL;
676 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200677 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200678 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300679 u32 transferred;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200680 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 u8 epnum;
682
683 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200684 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300685
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300686 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
687
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200688 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200689 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300690
Felipe Balbif6bafc62012-02-06 11:04:53 +0200691 trb = dwc->ep0_trb;
692 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300693
Felipe Balbia6829702011-08-27 22:18:09 +0300694 if (dwc->ep0_bounced) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300695 transferred = min_t(u32, ur->length,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200696 ep0->endpoint.maxpacket - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300697 memcpy(ur->buf, dwc->ep0_bounce, transferred);
698 dwc->ep0_bounced = false;
699 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200700 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300701 ur->actual += transferred;
702 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300703
704 if ((epnum & 1) && ur->actual < ur->length) {
705 /* for some reason we did not get everything out */
706
707 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 } else {
709 /*
710 * handle the case where we have to send a zero packet. This
711 * seems to be case when req.length > maxpacket. Could it be?
712 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300713 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200714 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300715 }
716}
717
718static void dwc3_ep0_complete_req(struct dwc3 *dwc,
719 const struct dwc3_event_depevt *event)
720{
721 struct dwc3_request *r;
722 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300723
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300724 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300725
726 if (!list_empty(&dep->request_list)) {
727 r = next_request(&dep->request_list);
728
729 dwc3_gadget_giveback(dep, r, 0);
730 }
731
Gerard Cauvy3b637362012-02-10 12:21:18 +0200732 if (dwc->test_mode) {
733 int ret;
734
735 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
736 if (ret < 0) {
737 dev_dbg(dwc->dev, "Invalid Test #%d\n",
738 dwc->test_mode_nr);
739 dwc3_ep0_stall_and_restart(dwc);
740 }
741 }
742
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300743 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300744 dwc3_ep0_out_start(dwc);
745}
746
747static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
748 const struct dwc3_event_depevt *event)
749{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300750 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
751
752 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -0800753 dep->res_trans_idx = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300754 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300755
Felipe Balbi72246da2011-08-19 18:10:58 +0300756 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300757 case EP0_SETUP_PHASE:
758 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300759 dwc3_ep0_inspect_setup(dwc, event);
760 break;
761
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300762 case EP0_DATA_PHASE:
763 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300764 dwc3_ep0_complete_data(dwc, event);
765 break;
766
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300767 case EP0_STATUS_PHASE:
768 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 dwc3_ep0_complete_req(dwc, event);
770 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300771 default:
772 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300773 }
774}
775
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300776static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
777 const struct dwc3_event_depevt *event)
778{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300779 dwc3_ep0_out_start(dwc);
780}
781
782static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
783 const struct dwc3_event_depevt *event)
784{
785 struct dwc3_ep *dep;
786 struct dwc3_request *req;
787 int ret;
788
789 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300790
791 if (list_empty(&dep->request_list)) {
792 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
793 dep->flags |= DWC3_EP_PENDING_REQUEST;
794
795 if (event->endpoint_number)
796 dep->flags |= DWC3_EP0_DIR_IN;
797 return;
798 }
799
800 req = next_request(&dep->request_list);
801 req->direction = !!event->endpoint_number;
802
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300803 if (req->request.length == 0) {
804 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
805 dwc->ctrl_req_addr, 0,
806 DWC3_TRBCTL_CONTROL_DATA);
807 } else if ((req->request.length % dep->endpoint.maxpacket)
808 && (event->endpoint_number == 0)) {
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200809 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
810 event->endpoint_number);
811 if (ret) {
812 dev_dbg(dwc->dev, "failed to map request\n");
813 return;
814 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300815
816 WARN_ON(req->request.length > dep->endpoint.maxpacket);
817
818 dwc->ep0_bounced = true;
819
820 /*
821 * REVISIT in case request length is bigger than EP0
822 * wMaxPacketSize, we will need two chained TRBs to handle
823 * the transfer.
824 */
825 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
826 dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
827 DWC3_TRBCTL_CONTROL_DATA);
828 } else {
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200829 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
830 event->endpoint_number);
831 if (ret) {
832 dev_dbg(dwc->dev, "failed to map request\n");
833 return;
834 }
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300835
836 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
837 req->request.dma, req->request.length,
838 DWC3_TRBCTL_CONTROL_DATA);
839 }
840
841 WARN_ON(ret < 0);
842}
843
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100844static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300845{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100846 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300847 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300848
849 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
850 : DWC3_TRBCTL_CONTROL_STATUS2;
851
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100852 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300853 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100854}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300855
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100856static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
857{
858 struct dwc3_ep *dep = dwc->eps[epnum];
859
Felipe Balbi457e84b2012-01-18 18:04:09 +0200860 if (dwc->resize_fifos) {
861 dev_dbg(dwc->dev, "starting to resize fifos\n");
862 dwc3_gadget_resize_tx_fifos(dwc);
863 dwc->resize_fifos = 0;
864 }
865
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100866 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300867}
868
Felipe Balbi72246da2011-08-19 18:10:58 +0300869static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
870 const struct dwc3_event_depevt *event)
871{
Felipe Balbidf62df52011-10-14 15:11:49 +0300872 dwc->setup_packet_pending = true;
873
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300874 /*
875 * This part is very tricky: If we has just handled
876 * XferNotReady(Setup) and we're now expecting a
877 * XferComplete but, instead, we receive another
878 * XferNotReady(Setup), we should STALL and restart
879 * the state machine.
880 *
881 * In all other cases, we just continue waiting
882 * for the XferComplete event.
883 *
884 * We are a little bit unsafe here because we're
885 * not trying to ensure that last event was, indeed,
886 * XferNotReady(Setup).
887 *
888 * Still, we don't expect any condition where that
889 * should happen and, even if it does, it would be
890 * another error condition.
891 */
892 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
893 switch (event->status) {
894 case DEPEVT_STATUS_CONTROL_SETUP:
895 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
896 dwc3_ep0_stall_and_restart(dwc);
897 break;
898 case DEPEVT_STATUS_CONTROL_DATA:
899 /* FALLTHROUGH */
900 case DEPEVT_STATUS_CONTROL_STATUS:
901 /* FALLTHROUGH */
902 default:
903 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
904 }
905
906 return;
907 }
908
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300909 switch (event->status) {
910 case DEPEVT_STATUS_CONTROL_SETUP:
911 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100912
913 dwc->ep0state = EP0_SETUP_PHASE;
914
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300915 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300916 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300917
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300918 case DEPEVT_STATUS_CONTROL_DATA:
919 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300920
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100921 dwc->ep0state = EP0_DATA_PHASE;
922
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300923 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
924 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300925 dwc->ep0_next_event,
926 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300927
928 dwc3_ep0_stall_and_restart(dwc);
929 return;
930 }
931
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300932 /*
933 * One of the possible error cases is when Host _does_
934 * request for Data Phase, but it does so on the wrong
935 * direction.
936 *
937 * Here, we already know ep0_next_event is DATA (see above),
938 * so we only need to check for direction.
939 */
940 if (dwc->ep0_expect_in != event->endpoint_number) {
941 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
942 dwc3_ep0_stall_and_restart(dwc);
943 return;
944 }
945
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300946 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300948
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300949 case DEPEVT_STATUS_CONTROL_STATUS:
950 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300951
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100952 dwc->ep0state = EP0_STATUS_PHASE;
953
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300954 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
955 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300956 dwc->ep0_next_event,
957 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300958
959 dwc3_ep0_stall_and_restart(dwc);
960 return;
961 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100962
963 if (dwc->delayed_status) {
964 WARN_ON_ONCE(event->endpoint_number != 1);
965 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
966 return;
967 }
968
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100969 dwc3_ep0_do_control_status(dwc, event->endpoint_number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 }
971}
972
973void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +0200974 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +0300975{
976 u8 epnum = event->endpoint_number;
977
978 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
979 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +0300980 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +0300981 dwc3_ep0_state_string(dwc->ep0state));
982
983 switch (event->endpoint_event) {
984 case DWC3_DEPEVT_XFERCOMPLETE:
985 dwc3_ep0_xfer_complete(dwc, event);
986 break;
987
988 case DWC3_DEPEVT_XFERNOTREADY:
989 dwc3_ep0_xfernotready(dwc, event);
990 break;
991
992 case DWC3_DEPEVT_XFERINPROGRESS:
993 case DWC3_DEPEVT_RXTXFIFOEVT:
994 case DWC3_DEPEVT_STREAMEVT:
995 case DWC3_DEPEVT_EPCMDCMPLT:
996 break;
997 }
998}