blob: 8244eb53082eaebe9d36c135cde0766621063abc [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
Felipe Balbi788a23f2012-05-21 14:22:41 +030057static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbia0807882012-05-04 13:03:54 +030058static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
59 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010060
Felipe Balbi72246da2011-08-19 18:10:58 +030061static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
62{
63 switch (state) {
64 case EP0_UNCONNECTED:
65 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030066 case EP0_SETUP_PHASE:
67 return "Setup Phase";
68 case EP0_DATA_PHASE:
69 return "Data Phase";
70 case EP0_STATUS_PHASE:
71 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030072 default:
73 return "UNKNOWN";
74 }
75}
76
77static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030078 u32 len, u32 type)
Felipe Balbi72246da2011-08-19 18:10:58 +030079{
80 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020081 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030082 struct dwc3_ep *dep;
83
84 int ret;
85
86 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030087 if (dep->flags & DWC3_EP_BUSY) {
88 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
89 return 0;
90 }
Felipe Balbi72246da2011-08-19 18:10:58 +030091
Felipe Balbif6bafc62012-02-06 11:04:53 +020092 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030093
Felipe Balbif6bafc62012-02-06 11:04:53 +020094 trb->bpl = lower_32_bits(buf_dma);
95 trb->bph = upper_32_bits(buf_dma);
96 trb->size = len;
97 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030098
Felipe Balbif6bafc62012-02-06 11:04:53 +020099 trb->ctrl |= (DWC3_TRB_CTRL_HWO
100 | DWC3_TRB_CTRL_LST
101 | DWC3_TRB_CTRL_IOC
102 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +0300103
104 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300105 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
106 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300107
108 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
109 DWC3_DEPCMD_STARTTRANSFER, &params);
110 if (ret < 0) {
111 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
112 return ret;
113 }
114
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300115 dep->flags |= DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300116 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300117 dep->number);
118
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300119 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
120
Felipe Balbi72246da2011-08-19 18:10:58 +0300121 return 0;
122}
123
124static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
125 struct dwc3_request *req)
126{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100127 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300128 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300129
130 req->request.actual = 0;
131 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300132 req->epnum = dep->number;
133
134 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300135
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300136 /*
137 * Gadget driver might not be quick enough to queue a request
138 * before we get a Transfer Not Ready event on this endpoint.
139 *
140 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
141 * flag is set, it's telling us that as soon as Gadget queues the
142 * required request, we should kick the transfer here because the
143 * IRQ we were waiting for is long gone.
144 */
145 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300146 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300147
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300148 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300149
Felipe Balbi68d8a782011-12-29 06:32:29 +0200150 if (dwc->ep0state != EP0_DATA_PHASE) {
151 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300152 return 0;
153 }
Felipe Balbia6829702011-08-27 22:18:09 +0300154
Felipe Balbia0807882012-05-04 13:03:54 +0300155 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
156
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300157 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
158 DWC3_EP0_DIR_IN);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200159 } else if (dwc->delayed_status) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100160 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200161
162 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi788a23f2012-05-21 14:22:41 +0300163 __dwc3_ep0_do_control_status(dwc, dwc->eps[1]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200164 else
165 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300166 }
167
168 return ret;
169}
170
171int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
172 gfp_t gfp_flags)
173{
174 struct dwc3_request *req = to_dwc3_request(request);
175 struct dwc3_ep *dep = to_dwc3_ep(ep);
176 struct dwc3 *dwc = dep->dwc;
177
178 unsigned long flags;
179
180 int ret;
181
Felipe Balbi72246da2011-08-19 18:10:58 +0300182 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200183 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
185 request, dep->name);
186 ret = -ESHUTDOWN;
187 goto out;
188 }
189
190 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200191 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300192 ret = -EBUSY;
193 goto out;
194 }
195
196 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
197 request, dep->name, request->length,
198 dwc3_ep0_state_string(dwc->ep0state));
199
200 ret = __dwc3_gadget_ep0_queue(dep, req);
201
202out:
203 spin_unlock_irqrestore(&dwc->lock, flags);
204
205 return ret;
206}
207
208static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
209{
Felipe Balbid7422202011-09-08 18:17:12 +0300210 struct dwc3_ep *dep = dwc->eps[0];
211
Felipe Balbi72246da2011-08-19 18:10:58 +0300212 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200213 __dwc3_gadget_ep_set_halt(dep, 1);
214 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100215 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300216
217 if (!list_empty(&dep->request_list)) {
218 struct dwc3_request *req;
219
220 req = next_request(&dep->request_list);
221 dwc3_gadget_giveback(dep, req, -ECONNRESET);
222 }
223
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300224 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300225 dwc3_ep0_out_start(dwc);
226}
227
228void dwc3_ep0_out_start(struct dwc3 *dwc)
229{
Felipe Balbi72246da2011-08-19 18:10:58 +0300230 int ret;
231
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300232 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
233 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 WARN_ON(ret < 0);
235}
236
Felipe Balbi72246da2011-08-19 18:10:58 +0300237static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
238{
239 struct dwc3_ep *dep;
240 u32 windex = le16_to_cpu(wIndex_le);
241 u32 epnum;
242
243 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
244 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
245 epnum |= 1;
246
247 dep = dwc->eps[epnum];
248 if (dep->flags & DWC3_EP_ENABLED)
249 return dep;
250
251 return NULL;
252}
253
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200254static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300255{
Felipe Balbi72246da2011-08-19 18:10:58 +0300256}
Felipe Balbi72246da2011-08-19 18:10:58 +0300257/*
258 * ch 9.4.5
259 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200260static int dwc3_ep0_handle_status(struct dwc3 *dwc,
261 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300262{
263 struct dwc3_ep *dep;
264 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200265 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300266 u16 usb_status = 0;
267 __le16 *response_pkt;
268
269 recip = ctrl->bRequestType & USB_RECIP_MASK;
270 switch (recip) {
271 case USB_RECIP_DEVICE:
272 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200273 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300274 */
275 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200276
277 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
278 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
279 if (reg & DWC3_DCTL_INITU1ENA)
280 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
281 if (reg & DWC3_DCTL_INITU2ENA)
282 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
283 }
284
Felipe Balbi72246da2011-08-19 18:10:58 +0300285 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;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200325 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300326 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
328 wValue = le16_to_cpu(ctrl->wValue);
329 wIndex = le16_to_cpu(ctrl->wIndex);
330 recip = ctrl->bRequestType & USB_RECIP_MASK;
331 switch (recip) {
332 case USB_RECIP_DEVICE:
333
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200334 switch (wValue) {
335 case USB_DEVICE_REMOTE_WAKEUP:
336 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 /*
338 * 9.4.1 says only only for SS, in AddressState only for
339 * default control pipe
340 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300341 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300342 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
343 return -EINVAL;
344 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
345 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300346
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200347 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
348 if (set)
349 reg |= DWC3_DCTL_INITU1ENA;
350 else
351 reg &= ~DWC3_DCTL_INITU1ENA;
352 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300353 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200354
Felipe Balbi72246da2011-08-19 18:10:58 +0300355 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200356 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
357 return -EINVAL;
358 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
359 return -EINVAL;
360
361 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
362 if (set)
363 reg |= DWC3_DCTL_INITU2ENA;
364 else
365 reg &= ~DWC3_DCTL_INITU2ENA;
366 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300367 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200368
Felipe Balbi72246da2011-08-19 18:10:58 +0300369 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200370 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300371 break;
372
373 case USB_DEVICE_TEST_MODE:
374 if ((wIndex & 0xff) != 0)
375 return -EINVAL;
376 if (!set)
377 return -EINVAL;
378
Gerard Cauvy3b637362012-02-10 12:21:18 +0200379 dwc->test_mode_nr = wIndex >> 8;
380 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200381 break;
382 default:
383 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300384 }
385 break;
386
387 case USB_RECIP_INTERFACE:
388 switch (wValue) {
389 case USB_INTRF_FUNC_SUSPEND:
390 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
391 /* XXX enable Low power suspend */
392 ;
393 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
394 /* XXX enable remote wakeup */
395 ;
396 break;
397 default:
398 return -EINVAL;
399 }
400 break;
401
402 case USB_RECIP_ENDPOINT:
403 switch (wValue) {
404 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800405 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 if (!dep)
407 return -EINVAL;
408 ret = __dwc3_gadget_ep_set_halt(dep, set);
409 if (ret)
410 return -EINVAL;
411 break;
412 default:
413 return -EINVAL;
414 }
415 break;
416
417 default:
418 return -EINVAL;
419 };
420
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 return 0;
422}
423
424static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
425{
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 u32 addr;
427 u32 reg;
428
429 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300430 if (addr > 127) {
431 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300432 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300433 }
434
435 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
436 dev_dbg(dwc->dev, "trying to set address when configured\n");
437 return -EINVAL;
438 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300439
Felipe Balbi26460212011-09-30 10:58:36 +0300440 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
441 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
442 reg |= DWC3_DCFG_DEVADDR(addr);
443 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300444
Felipe Balbi26460212011-09-30 10:58:36 +0300445 if (addr)
446 dwc->dev_state = DWC3_ADDRESS_STATE;
447 else
448 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449
Felipe Balbi26460212011-09-30 10:58:36 +0300450 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300451}
452
453static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
454{
455 int ret;
456
457 spin_unlock(&dwc->lock);
458 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
459 spin_lock(&dwc->lock);
460 return ret;
461}
462
463static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
464{
465 u32 cfg;
466 int ret;
467
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300468 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300469 cfg = le16_to_cpu(ctrl->wValue);
470
471 switch (dwc->dev_state) {
472 case DWC3_DEFAULT_STATE:
473 return -EINVAL;
474 break;
475
476 case DWC3_ADDRESS_STATE:
477 ret = dwc3_ep0_delegate_req(dwc, ctrl);
478 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200479 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 dwc->dev_state = DWC3_CONFIGURED_STATE;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200481 dwc->resize_fifos = true;
482 dev_dbg(dwc->dev, "resize fifos flag SET\n");
483 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300484 break;
485
486 case DWC3_CONFIGURED_STATE:
487 ret = dwc3_ep0_delegate_req(dwc, ctrl);
488 if (!cfg)
489 dwc->dev_state = DWC3_ADDRESS_STATE;
490 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100491 default:
492 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300493 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100494 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300495}
496
Felipe Balbi865e09e2012-04-24 16:19:49 +0300497static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
498{
499 struct dwc3_ep *dep = to_dwc3_ep(ep);
500 struct dwc3 *dwc = dep->dwc;
501
502 u32 param = 0;
503 u32 reg;
504
505 struct timing {
506 u8 u1sel;
507 u8 u1pel;
508 u16 u2sel;
509 u16 u2pel;
510 } __packed timing;
511
512 int ret;
513
514 memcpy(&timing, req->buf, sizeof(timing));
515
516 dwc->u1sel = timing.u1sel;
517 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300518 dwc->u2sel = le16_to_cpu(timing.u2sel);
519 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300520
521 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
522 if (reg & DWC3_DCTL_INITU2ENA)
523 param = dwc->u2pel;
524 if (reg & DWC3_DCTL_INITU1ENA)
525 param = dwc->u1pel;
526
527 /*
528 * According to Synopsys Databook, if parameter is
529 * greater than 125, a value of zero should be
530 * programmed in the register.
531 */
532 if (param > 125)
533 param = 0;
534
535 /* now that we have the time, issue DGCMD Set Sel */
536 ret = dwc3_send_gadget_generic_command(dwc,
537 DWC3_DGCMD_SET_PERIODIC_PAR, param);
538 WARN_ON(ret < 0);
539}
540
541static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
542{
543 struct dwc3_ep *dep;
544 u16 wLength;
545 u16 wValue;
546
547 if (dwc->dev_state == DWC3_DEFAULT_STATE)
548 return -EINVAL;
549
550 wValue = le16_to_cpu(ctrl->wValue);
551 wLength = le16_to_cpu(ctrl->wLength);
552
553 if (wLength != 6) {
554 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
555 wLength);
556 return -EINVAL;
557 }
558
559 /*
560 * To handle Set SEL we need to receive 6 bytes from Host. So let's
561 * queue a usb_request for 6 bytes.
562 *
563 * Remember, though, this controller can't handle non-wMaxPacketSize
564 * aligned transfers on the OUT direction, so we queue a request for
565 * wMaxPacketSize instead.
566 */
567 dep = dwc->eps[0];
568 dwc->ep0_usb_req.dep = dep;
569 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
570 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
571 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
572
573 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
574}
575
Felipe Balbic12a0d82012-04-25 10:45:05 +0300576static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
577{
578 u16 wLength;
579 u16 wValue;
580 u16 wIndex;
581
582 wValue = le16_to_cpu(ctrl->wValue);
583 wLength = le16_to_cpu(ctrl->wLength);
584 wIndex = le16_to_cpu(ctrl->wIndex);
585
586 if (wIndex || wLength)
587 return -EINVAL;
588
589 /*
590 * REVISIT It's unclear from Databook what to do with this
591 * value. For now, just cache it.
592 */
593 dwc->isoch_delay = wValue;
594
595 return 0;
596}
597
Felipe Balbi72246da2011-08-19 18:10:58 +0300598static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
599{
600 int ret;
601
602 switch (ctrl->bRequest) {
603 case USB_REQ_GET_STATUS:
604 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
605 ret = dwc3_ep0_handle_status(dwc, ctrl);
606 break;
607 case USB_REQ_CLEAR_FEATURE:
608 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
609 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
610 break;
611 case USB_REQ_SET_FEATURE:
612 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
613 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
614 break;
615 case USB_REQ_SET_ADDRESS:
616 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
617 ret = dwc3_ep0_set_address(dwc, ctrl);
618 break;
619 case USB_REQ_SET_CONFIGURATION:
620 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
621 ret = dwc3_ep0_set_config(dwc, ctrl);
622 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300623 case USB_REQ_SET_SEL:
624 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
625 ret = dwc3_ep0_set_sel(dwc, ctrl);
626 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300627 case USB_REQ_SET_ISOCH_DELAY:
628 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
629 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
630 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300631 default:
632 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
633 ret = dwc3_ep0_delegate_req(dwc, ctrl);
634 break;
635 };
636
637 return ret;
638}
639
640static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
641 const struct dwc3_event_depevt *event)
642{
643 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300644 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300645 u32 len;
646
647 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300648 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300649
650 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300651 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300652 dwc->three_stage_setup = false;
653 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300654 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
655 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300656 dwc->three_stage_setup = true;
657 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300658 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
659 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300660
661 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
662 ret = dwc3_ep0_std_request(dwc, ctrl);
663 else
664 ret = dwc3_ep0_delegate_req(dwc, ctrl);
665
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100666 if (ret == USB_GADGET_DELAYED_STATUS)
667 dwc->delayed_status = true;
668
Felipe Balbief21ede2012-05-31 10:29:49 +0300669out:
670 if (ret < 0)
671 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300672}
673
674static void dwc3_ep0_complete_data(struct dwc3 *dwc,
675 const struct dwc3_event_depevt *event)
676{
677 struct dwc3_request *r = NULL;
678 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200679 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200680 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300681 u32 transferred;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200682 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 u8 epnum;
684
685 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200686 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300687
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300688 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
689
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200690 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200691 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300692
Felipe Balbif6bafc62012-02-06 11:04:53 +0200693 trb = dwc->ep0_trb;
694 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300695
Felipe Balbia6829702011-08-27 22:18:09 +0300696 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500697 unsigned transfer_size = ur->length;
698 unsigned maxp = ep0->endpoint.maxpacket;
699
700 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300701 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500702 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300703 memcpy(ur->buf, dwc->ep0_bounce, transferred);
704 dwc->ep0_bounced = false;
705 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200706 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300707 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300708
Felipe Balbicd423dd2012-03-21 11:44:00 +0200709 ur->actual += transferred;
710
Felipe Balbi72246da2011-08-19 18:10:58 +0300711 if ((epnum & 1) && ur->actual < ur->length) {
712 /* for some reason we did not get everything out */
713
714 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300715 } else {
716 /*
717 * handle the case where we have to send a zero packet. This
718 * seems to be case when req.length > maxpacket. Could it be?
719 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200721 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 }
723}
724
Felipe Balbi85a78102012-05-31 12:32:37 +0300725static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300726 const struct dwc3_event_depevt *event)
727{
728 struct dwc3_request *r;
729 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300730
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300731 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300732
733 if (!list_empty(&dep->request_list)) {
734 r = next_request(&dep->request_list);
735
736 dwc3_gadget_giveback(dep, r, 0);
737 }
738
Gerard Cauvy3b637362012-02-10 12:21:18 +0200739 if (dwc->test_mode) {
740 int ret;
741
742 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
743 if (ret < 0) {
744 dev_dbg(dwc->dev, "Invalid Test #%d\n",
745 dwc->test_mode_nr);
746 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300747 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200748 }
749 }
750
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300751 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 dwc3_ep0_out_start(dwc);
753}
754
755static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
756 const struct dwc3_event_depevt *event)
757{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300758 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
759
760 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300761 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300762 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300763
Felipe Balbi72246da2011-08-19 18:10:58 +0300764 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300765 case EP0_SETUP_PHASE:
766 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300767 dwc3_ep0_inspect_setup(dwc, event);
768 break;
769
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300770 case EP0_DATA_PHASE:
771 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 dwc3_ep0_complete_data(dwc, event);
773 break;
774
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300775 case EP0_STATUS_PHASE:
776 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi85a78102012-05-31 12:32:37 +0300777 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300779 default:
780 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300781 }
782}
783
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300784static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
785 const struct dwc3_event_depevt *event)
786{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300787 dwc3_ep0_out_start(dwc);
788}
789
Felipe Balbia0807882012-05-04 13:03:54 +0300790static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
791 struct dwc3_ep *dep, struct dwc3_request *req)
792{
793 int ret;
794
795 req->direction = !!dep->number;
796
797 if (req->request.length == 0) {
798 ret = dwc3_ep0_start_trans(dwc, dep->number,
799 dwc->ctrl_req_addr, 0,
800 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300801 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300802 && (dep->number == 0)) {
803 u32 transfer_size;
804
805 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
806 dep->number);
807 if (ret) {
808 dev_dbg(dwc->dev, "failed to map request\n");
809 return;
810 }
811
812 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
813
814 transfer_size = roundup(req->request.length,
815 (u32) dep->endpoint.maxpacket);
816
817 dwc->ep0_bounced = true;
818
819 /*
820 * REVISIT in case request length is bigger than
821 * DWC3_EP0_BOUNCE_SIZE we will need two chained
822 * TRBs to handle the transfer.
823 */
824 ret = dwc3_ep0_start_trans(dwc, dep->number,
825 dwc->ep0_bounce_addr, transfer_size,
826 DWC3_TRBCTL_CONTROL_DATA);
827 } else {
828 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
829 dep->number);
830 if (ret) {
831 dev_dbg(dwc->dev, "failed to map request\n");
832 return;
833 }
834
835 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
836 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
837 }
838
839 WARN_ON(ret < 0);
840}
841
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300842static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
843 const struct dwc3_event_depevt *event)
844{
845 struct dwc3_ep *dep;
846 struct dwc3_request *req;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300847
848 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300849
850 if (list_empty(&dep->request_list)) {
851 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
852 dep->flags |= DWC3_EP_PENDING_REQUEST;
853
854 if (event->endpoint_number)
855 dep->flags |= DWC3_EP0_DIR_IN;
856 return;
857 }
858
859 req = next_request(&dep->request_list);
Felipe Balbia0807882012-05-04 13:03:54 +0300860 dep = dwc->eps[event->endpoint_number];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300861
Felipe Balbia0807882012-05-04 13:03:54 +0300862 __dwc3_ep0_do_control_data(dwc, dep, req);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300863}
864
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100865static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300866{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100867 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300868 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300869
870 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
871 : DWC3_TRBCTL_CONTROL_STATUS2;
872
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100873 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300874 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100875}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300876
Felipe Balbi788a23f2012-05-21 14:22:41 +0300877static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100878{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200879 if (dwc->resize_fifos) {
880 dev_dbg(dwc->dev, "starting to resize fifos\n");
881 dwc3_gadget_resize_tx_fifos(dwc);
882 dwc->resize_fifos = 0;
883 }
884
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100885 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300886}
887
Felipe Balbi788a23f2012-05-21 14:22:41 +0300888static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
889 const struct dwc3_event_depevt *event)
890{
891 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
892
893 __dwc3_ep0_do_control_status(dwc, dep);
894}
895
Felipe Balbi72246da2011-08-19 18:10:58 +0300896static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
897 const struct dwc3_event_depevt *event)
898{
Felipe Balbidf62df52011-10-14 15:11:49 +0300899 dwc->setup_packet_pending = true;
900
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300901 /*
Felipe Balbi33b84c22012-05-21 14:35:17 +0300902 * This part is very tricky: If we have just handled
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300903 * XferNotReady(Setup) and we're now expecting a
904 * XferComplete but, instead, we receive another
905 * XferNotReady(Setup), we should STALL and restart
906 * the state machine.
907 *
908 * In all other cases, we just continue waiting
909 * for the XferComplete event.
910 *
911 * We are a little bit unsafe here because we're
912 * not trying to ensure that last event was, indeed,
913 * XferNotReady(Setup).
914 *
915 * Still, we don't expect any condition where that
916 * should happen and, even if it does, it would be
917 * another error condition.
918 */
919 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
920 switch (event->status) {
921 case DEPEVT_STATUS_CONTROL_SETUP:
922 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
923 dwc3_ep0_stall_and_restart(dwc);
924 break;
925 case DEPEVT_STATUS_CONTROL_DATA:
926 /* FALLTHROUGH */
927 case DEPEVT_STATUS_CONTROL_STATUS:
928 /* FALLTHROUGH */
929 default:
930 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
931 }
932
933 return;
934 }
935
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300936 switch (event->status) {
937 case DEPEVT_STATUS_CONTROL_SETUP:
938 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100939
940 dwc->ep0state = EP0_SETUP_PHASE;
941
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300942 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300943 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300944
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300945 case DEPEVT_STATUS_CONTROL_DATA:
946 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300947
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100948 dwc->ep0state = EP0_DATA_PHASE;
949
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300950 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
951 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300952 dwc->ep0_next_event,
953 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300954
955 dwc3_ep0_stall_and_restart(dwc);
956 return;
957 }
958
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300959 /*
960 * One of the possible error cases is when Host _does_
961 * request for Data Phase, but it does so on the wrong
962 * direction.
963 *
964 * Here, we already know ep0_next_event is DATA (see above),
965 * so we only need to check for direction.
966 */
967 if (dwc->ep0_expect_in != event->endpoint_number) {
968 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
969 dwc3_ep0_stall_and_restart(dwc);
970 return;
971 }
972
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300973 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300974 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300975
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300976 case DEPEVT_STATUS_CONTROL_STATUS:
977 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300978
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100979 dwc->ep0state = EP0_STATUS_PHASE;
980
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300981 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
982 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300983 dwc->ep0_next_event,
984 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300985
986 dwc3_ep0_stall_and_restart(dwc);
987 return;
988 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100989
990 if (dwc->delayed_status) {
991 WARN_ON_ONCE(event->endpoint_number != 1);
992 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
993 return;
994 }
995
Felipe Balbi788a23f2012-05-21 14:22:41 +0300996 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 }
998}
999
1000void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001001 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001002{
1003 u8 epnum = event->endpoint_number;
1004
1005 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1006 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001007 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001008 dwc3_ep0_state_string(dwc->ep0state));
1009
1010 switch (event->endpoint_event) {
1011 case DWC3_DEPEVT_XFERCOMPLETE:
1012 dwc3_ep0_xfer_complete(dwc, event);
1013 break;
1014
1015 case DWC3_DEPEVT_XFERNOTREADY:
1016 dwc3_ep0_xfernotready(dwc, event);
1017 break;
1018
1019 case DWC3_DEPEVT_XFERINPROGRESS:
1020 case DWC3_DEPEVT_RXTXFIFOEVT:
1021 case DWC3_DEPEVT_STREAMEVT:
1022 case DWC3_DEPEVT_EPCMDCMPLT:
1023 break;
1024 }
1025}