blob: 1315e787a31696733cc9376e346da9ac00933e38 [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
Pratyush Anand08f0d962012-06-25 22:40:43 +0530228int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
229{
230 struct dwc3_ep *dep = to_dwc3_ep(ep);
231 struct dwc3 *dwc = dep->dwc;
232
233 dwc3_ep0_stall_and_restart(dwc);
234
235 return 0;
236}
237
Felipe Balbi72246da2011-08-19 18:10:58 +0300238void dwc3_ep0_out_start(struct dwc3 *dwc)
239{
Felipe Balbi72246da2011-08-19 18:10:58 +0300240 int ret;
241
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300242 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
243 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300244 WARN_ON(ret < 0);
245}
246
Felipe Balbi72246da2011-08-19 18:10:58 +0300247static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
248{
249 struct dwc3_ep *dep;
250 u32 windex = le16_to_cpu(wIndex_le);
251 u32 epnum;
252
253 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
254 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
255 epnum |= 1;
256
257 dep = dwc->eps[epnum];
258 if (dep->flags & DWC3_EP_ENABLED)
259 return dep;
260
261 return NULL;
262}
263
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200264static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300265{
Felipe Balbi72246da2011-08-19 18:10:58 +0300266}
Felipe Balbi72246da2011-08-19 18:10:58 +0300267/*
268 * ch 9.4.5
269 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200270static int dwc3_ep0_handle_status(struct dwc3 *dwc,
271 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300272{
273 struct dwc3_ep *dep;
274 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200275 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300276 u16 usb_status = 0;
277 __le16 *response_pkt;
278
279 recip = ctrl->bRequestType & USB_RECIP_MASK;
280 switch (recip) {
281 case USB_RECIP_DEVICE:
282 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200283 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300284 */
285 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200286
287 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
288 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
289 if (reg & DWC3_DCTL_INITU1ENA)
290 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
291 if (reg & DWC3_DCTL_INITU2ENA)
292 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
293 }
294
Felipe Balbi72246da2011-08-19 18:10:58 +0300295 break;
296
297 case USB_RECIP_INTERFACE:
298 /*
299 * Function Remote Wake Capable D0
300 * Function Remote Wakeup D1
301 */
302 break;
303
304 case USB_RECIP_ENDPOINT:
305 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
306 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200307 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300308
309 if (dep->flags & DWC3_EP_STALL)
310 usb_status = 1 << USB_ENDPOINT_HALT;
311 break;
312 default:
313 return -EINVAL;
314 };
315
316 response_pkt = (__le16 *) dwc->setup_buf;
317 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200318
319 dep = dwc->eps[0];
320 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100321 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200322 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100323 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200324
325 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300326}
327
328static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
329 struct usb_ctrlrequest *ctrl, int set)
330{
331 struct dwc3_ep *dep;
332 u32 recip;
333 u32 wValue;
334 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200335 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300337
338 wValue = le16_to_cpu(ctrl->wValue);
339 wIndex = le16_to_cpu(ctrl->wIndex);
340 recip = ctrl->bRequestType & USB_RECIP_MASK;
341 switch (recip) {
342 case USB_RECIP_DEVICE:
343
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200344 switch (wValue) {
345 case USB_DEVICE_REMOTE_WAKEUP:
346 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300347 /*
348 * 9.4.1 says only only for SS, in AddressState only for
349 * default control pipe
350 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300351 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300352 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
353 return -EINVAL;
354 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
355 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300356
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200357 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
358 if (set)
359 reg |= DWC3_DCTL_INITU1ENA;
360 else
361 reg &= ~DWC3_DCTL_INITU1ENA;
362 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300363 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200364
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200366 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
367 return -EINVAL;
368 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
369 return -EINVAL;
370
371 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
372 if (set)
373 reg |= DWC3_DCTL_INITU2ENA;
374 else
375 reg &= ~DWC3_DCTL_INITU2ENA;
376 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300377 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200378
Felipe Balbi72246da2011-08-19 18:10:58 +0300379 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200380 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300381 break;
382
383 case USB_DEVICE_TEST_MODE:
384 if ((wIndex & 0xff) != 0)
385 return -EINVAL;
386 if (!set)
387 return -EINVAL;
388
Gerard Cauvy3b637362012-02-10 12:21:18 +0200389 dwc->test_mode_nr = wIndex >> 8;
390 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200391 break;
392 default:
393 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300394 }
395 break;
396
397 case USB_RECIP_INTERFACE:
398 switch (wValue) {
399 case USB_INTRF_FUNC_SUSPEND:
400 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
401 /* XXX enable Low power suspend */
402 ;
403 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
404 /* XXX enable remote wakeup */
405 ;
406 break;
407 default:
408 return -EINVAL;
409 }
410 break;
411
412 case USB_RECIP_ENDPOINT:
413 switch (wValue) {
414 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800415 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 if (!dep)
417 return -EINVAL;
418 ret = __dwc3_gadget_ep_set_halt(dep, set);
419 if (ret)
420 return -EINVAL;
421 break;
422 default:
423 return -EINVAL;
424 }
425 break;
426
427 default:
428 return -EINVAL;
429 };
430
Felipe Balbi72246da2011-08-19 18:10:58 +0300431 return 0;
432}
433
434static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
435{
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 u32 addr;
437 u32 reg;
438
439 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300440 if (addr > 127) {
441 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300442 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300443 }
444
445 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
446 dev_dbg(dwc->dev, "trying to set address when configured\n");
447 return -EINVAL;
448 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300449
Felipe Balbi26460212011-09-30 10:58:36 +0300450 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
451 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
452 reg |= DWC3_DCFG_DEVADDR(addr);
453 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300454
Felipe Balbi26460212011-09-30 10:58:36 +0300455 if (addr)
456 dwc->dev_state = DWC3_ADDRESS_STATE;
457 else
458 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300459
Felipe Balbi26460212011-09-30 10:58:36 +0300460 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300461}
462
463static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
464{
465 int ret;
466
467 spin_unlock(&dwc->lock);
468 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
469 spin_lock(&dwc->lock);
470 return ret;
471}
472
473static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
474{
475 u32 cfg;
476 int ret;
477
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300478 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300479 cfg = le16_to_cpu(ctrl->wValue);
480
481 switch (dwc->dev_state) {
482 case DWC3_DEFAULT_STATE:
483 return -EINVAL;
484 break;
485
486 case DWC3_ADDRESS_STATE:
487 ret = dwc3_ep0_delegate_req(dwc, ctrl);
488 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200489 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300490 dwc->dev_state = DWC3_CONFIGURED_STATE;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200491 dwc->resize_fifos = true;
492 dev_dbg(dwc->dev, "resize fifos flag SET\n");
493 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300494 break;
495
496 case DWC3_CONFIGURED_STATE:
497 ret = dwc3_ep0_delegate_req(dwc, ctrl);
498 if (!cfg)
499 dwc->dev_state = DWC3_ADDRESS_STATE;
500 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100501 default:
502 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100504 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300505}
506
Felipe Balbi865e09e2012-04-24 16:19:49 +0300507static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
508{
509 struct dwc3_ep *dep = to_dwc3_ep(ep);
510 struct dwc3 *dwc = dep->dwc;
511
512 u32 param = 0;
513 u32 reg;
514
515 struct timing {
516 u8 u1sel;
517 u8 u1pel;
518 u16 u2sel;
519 u16 u2pel;
520 } __packed timing;
521
522 int ret;
523
524 memcpy(&timing, req->buf, sizeof(timing));
525
526 dwc->u1sel = timing.u1sel;
527 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300528 dwc->u2sel = le16_to_cpu(timing.u2sel);
529 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300530
531 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
532 if (reg & DWC3_DCTL_INITU2ENA)
533 param = dwc->u2pel;
534 if (reg & DWC3_DCTL_INITU1ENA)
535 param = dwc->u1pel;
536
537 /*
538 * According to Synopsys Databook, if parameter is
539 * greater than 125, a value of zero should be
540 * programmed in the register.
541 */
542 if (param > 125)
543 param = 0;
544
545 /* now that we have the time, issue DGCMD Set Sel */
546 ret = dwc3_send_gadget_generic_command(dwc,
547 DWC3_DGCMD_SET_PERIODIC_PAR, param);
548 WARN_ON(ret < 0);
549}
550
551static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
552{
553 struct dwc3_ep *dep;
554 u16 wLength;
555 u16 wValue;
556
557 if (dwc->dev_state == DWC3_DEFAULT_STATE)
558 return -EINVAL;
559
560 wValue = le16_to_cpu(ctrl->wValue);
561 wLength = le16_to_cpu(ctrl->wLength);
562
563 if (wLength != 6) {
564 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
565 wLength);
566 return -EINVAL;
567 }
568
569 /*
570 * To handle Set SEL we need to receive 6 bytes from Host. So let's
571 * queue a usb_request for 6 bytes.
572 *
573 * Remember, though, this controller can't handle non-wMaxPacketSize
574 * aligned transfers on the OUT direction, so we queue a request for
575 * wMaxPacketSize instead.
576 */
577 dep = dwc->eps[0];
578 dwc->ep0_usb_req.dep = dep;
579 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
580 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
581 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
582
583 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
584}
585
Felipe Balbic12a0d82012-04-25 10:45:05 +0300586static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
587{
588 u16 wLength;
589 u16 wValue;
590 u16 wIndex;
591
592 wValue = le16_to_cpu(ctrl->wValue);
593 wLength = le16_to_cpu(ctrl->wLength);
594 wIndex = le16_to_cpu(ctrl->wIndex);
595
596 if (wIndex || wLength)
597 return -EINVAL;
598
599 /*
600 * REVISIT It's unclear from Databook what to do with this
601 * value. For now, just cache it.
602 */
603 dwc->isoch_delay = wValue;
604
605 return 0;
606}
607
Felipe Balbi72246da2011-08-19 18:10:58 +0300608static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
609{
610 int ret;
611
612 switch (ctrl->bRequest) {
613 case USB_REQ_GET_STATUS:
614 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
615 ret = dwc3_ep0_handle_status(dwc, ctrl);
616 break;
617 case USB_REQ_CLEAR_FEATURE:
618 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
619 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
620 break;
621 case USB_REQ_SET_FEATURE:
622 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
623 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
624 break;
625 case USB_REQ_SET_ADDRESS:
626 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
627 ret = dwc3_ep0_set_address(dwc, ctrl);
628 break;
629 case USB_REQ_SET_CONFIGURATION:
630 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
631 ret = dwc3_ep0_set_config(dwc, ctrl);
632 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300633 case USB_REQ_SET_SEL:
634 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
635 ret = dwc3_ep0_set_sel(dwc, ctrl);
636 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300637 case USB_REQ_SET_ISOCH_DELAY:
638 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
639 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
640 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 default:
642 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
643 ret = dwc3_ep0_delegate_req(dwc, ctrl);
644 break;
645 };
646
647 return ret;
648}
649
650static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
651 const struct dwc3_event_depevt *event)
652{
653 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300654 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300655 u32 len;
656
657 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300658 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300659
660 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300661 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300662 dwc->three_stage_setup = false;
663 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300664 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
665 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300666 dwc->three_stage_setup = true;
667 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300668 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
669 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300670
671 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
672 ret = dwc3_ep0_std_request(dwc, ctrl);
673 else
674 ret = dwc3_ep0_delegate_req(dwc, ctrl);
675
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100676 if (ret == USB_GADGET_DELAYED_STATUS)
677 dwc->delayed_status = true;
678
Felipe Balbief21ede2012-05-31 10:29:49 +0300679out:
680 if (ret < 0)
681 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300682}
683
684static void dwc3_ep0_complete_data(struct dwc3 *dwc,
685 const struct dwc3_event_depevt *event)
686{
687 struct dwc3_request *r = NULL;
688 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200689 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200690 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300691 u32 transferred;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200692 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300693 u8 epnum;
694
695 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200696 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300697
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300698 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
699
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200700 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200701 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300702
Felipe Balbif6bafc62012-02-06 11:04:53 +0200703 trb = dwc->ep0_trb;
704 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300705
Felipe Balbia6829702011-08-27 22:18:09 +0300706 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500707 unsigned transfer_size = ur->length;
708 unsigned maxp = ep0->endpoint.maxpacket;
709
710 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300711 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500712 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300713 memcpy(ur->buf, dwc->ep0_bounce, transferred);
714 dwc->ep0_bounced = false;
715 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200716 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300717 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300718
Felipe Balbicd423dd2012-03-21 11:44:00 +0200719 ur->actual += transferred;
720
Felipe Balbi72246da2011-08-19 18:10:58 +0300721 if ((epnum & 1) && ur->actual < ur->length) {
722 /* for some reason we did not get everything out */
723
724 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 } else {
726 /*
727 * handle the case where we have to send a zero packet. This
728 * seems to be case when req.length > maxpacket. Could it be?
729 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200731 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300732 }
733}
734
Felipe Balbi85a78102012-05-31 12:32:37 +0300735static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300736 const struct dwc3_event_depevt *event)
737{
738 struct dwc3_request *r;
739 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300740
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300741 dep = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300742
743 if (!list_empty(&dep->request_list)) {
744 r = next_request(&dep->request_list);
745
746 dwc3_gadget_giveback(dep, r, 0);
747 }
748
Gerard Cauvy3b637362012-02-10 12:21:18 +0200749 if (dwc->test_mode) {
750 int ret;
751
752 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
753 if (ret < 0) {
754 dev_dbg(dwc->dev, "Invalid Test #%d\n",
755 dwc->test_mode_nr);
756 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300757 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200758 }
759 }
760
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300761 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300762 dwc3_ep0_out_start(dwc);
763}
764
765static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
766 const struct dwc3_event_depevt *event)
767{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300768 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
769
770 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300771 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300772 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300773
Felipe Balbi72246da2011-08-19 18:10:58 +0300774 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300775 case EP0_SETUP_PHASE:
776 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 dwc3_ep0_inspect_setup(dwc, event);
778 break;
779
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300780 case EP0_DATA_PHASE:
781 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 dwc3_ep0_complete_data(dwc, event);
783 break;
784
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300785 case EP0_STATUS_PHASE:
786 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi85a78102012-05-31 12:32:37 +0300787 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300788 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300789 default:
790 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 }
792}
793
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300794static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
795 const struct dwc3_event_depevt *event)
796{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300797 dwc3_ep0_out_start(dwc);
798}
799
Felipe Balbia0807882012-05-04 13:03:54 +0300800static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
801 struct dwc3_ep *dep, struct dwc3_request *req)
802{
803 int ret;
804
805 req->direction = !!dep->number;
806
807 if (req->request.length == 0) {
808 ret = dwc3_ep0_start_trans(dwc, dep->number,
809 dwc->ctrl_req_addr, 0,
810 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300811 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300812 && (dep->number == 0)) {
813 u32 transfer_size;
814
815 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
816 dep->number);
817 if (ret) {
818 dev_dbg(dwc->dev, "failed to map request\n");
819 return;
820 }
821
822 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
823
824 transfer_size = roundup(req->request.length,
825 (u32) dep->endpoint.maxpacket);
826
827 dwc->ep0_bounced = true;
828
829 /*
830 * REVISIT in case request length is bigger than
831 * DWC3_EP0_BOUNCE_SIZE we will need two chained
832 * TRBs to handle the transfer.
833 */
834 ret = dwc3_ep0_start_trans(dwc, dep->number,
835 dwc->ep0_bounce_addr, transfer_size,
836 DWC3_TRBCTL_CONTROL_DATA);
837 } else {
838 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
839 dep->number);
840 if (ret) {
841 dev_dbg(dwc->dev, "failed to map request\n");
842 return;
843 }
844
845 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
846 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
847 }
848
849 WARN_ON(ret < 0);
850}
851
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300852static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
853 const struct dwc3_event_depevt *event)
854{
855 struct dwc3_ep *dep;
856 struct dwc3_request *req;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300857
858 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300859
860 if (list_empty(&dep->request_list)) {
861 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
862 dep->flags |= DWC3_EP_PENDING_REQUEST;
863
864 if (event->endpoint_number)
865 dep->flags |= DWC3_EP0_DIR_IN;
866 return;
867 }
868
869 req = next_request(&dep->request_list);
Felipe Balbia0807882012-05-04 13:03:54 +0300870 dep = dwc->eps[event->endpoint_number];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300871
Felipe Balbia0807882012-05-04 13:03:54 +0300872 __dwc3_ep0_do_control_data(dwc, dep, req);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300873}
874
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100875static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300876{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100877 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300878 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300879
880 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
881 : DWC3_TRBCTL_CONTROL_STATUS2;
882
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100883 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300884 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100885}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300886
Felipe Balbi788a23f2012-05-21 14:22:41 +0300887static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100888{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200889 if (dwc->resize_fifos) {
890 dev_dbg(dwc->dev, "starting to resize fifos\n");
891 dwc3_gadget_resize_tx_fifos(dwc);
892 dwc->resize_fifos = 0;
893 }
894
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100895 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300896}
897
Felipe Balbi788a23f2012-05-21 14:22:41 +0300898static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
899 const struct dwc3_event_depevt *event)
900{
901 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
902
903 __dwc3_ep0_do_control_status(dwc, dep);
904}
905
Felipe Balbi72246da2011-08-19 18:10:58 +0300906static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
907 const struct dwc3_event_depevt *event)
908{
Felipe Balbidf62df52011-10-14 15:11:49 +0300909 dwc->setup_packet_pending = true;
910
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300911 /*
Felipe Balbi33b84c22012-05-21 14:35:17 +0300912 * This part is very tricky: If we have just handled
Felipe Balbi9cc9bcd2011-10-18 18:00:26 +0300913 * XferNotReady(Setup) and we're now expecting a
914 * XferComplete but, instead, we receive another
915 * XferNotReady(Setup), we should STALL and restart
916 * the state machine.
917 *
918 * In all other cases, we just continue waiting
919 * for the XferComplete event.
920 *
921 * We are a little bit unsafe here because we're
922 * not trying to ensure that last event was, indeed,
923 * XferNotReady(Setup).
924 *
925 * Still, we don't expect any condition where that
926 * should happen and, even if it does, it would be
927 * another error condition.
928 */
929 if (dwc->ep0_next_event == DWC3_EP0_COMPLETE) {
930 switch (event->status) {
931 case DEPEVT_STATUS_CONTROL_SETUP:
932 dev_vdbg(dwc->dev, "Unexpected XferNotReady(Setup)\n");
933 dwc3_ep0_stall_and_restart(dwc);
934 break;
935 case DEPEVT_STATUS_CONTROL_DATA:
936 /* FALLTHROUGH */
937 case DEPEVT_STATUS_CONTROL_STATUS:
938 /* FALLTHROUGH */
939 default:
940 dev_vdbg(dwc->dev, "waiting for XferComplete\n");
941 }
942
943 return;
944 }
945
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300946 switch (event->status) {
947 case DEPEVT_STATUS_CONTROL_SETUP:
948 dev_vdbg(dwc->dev, "Control Setup\n");
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100949
950 dwc->ep0state = EP0_SETUP_PHASE;
951
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300952 dwc3_ep0_do_control_setup(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300953 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300954
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300955 case DEPEVT_STATUS_CONTROL_DATA:
956 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300957
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100958 dwc->ep0state = EP0_DATA_PHASE;
959
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300960 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
961 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300962 dwc->ep0_next_event,
963 DWC3_EP0_NRDY_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300964
965 dwc3_ep0_stall_and_restart(dwc);
966 return;
967 }
968
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300969 /*
970 * One of the possible error cases is when Host _does_
971 * request for Data Phase, but it does so on the wrong
972 * direction.
973 *
974 * Here, we already know ep0_next_event is DATA (see above),
975 * so we only need to check for direction.
976 */
977 if (dwc->ep0_expect_in != event->endpoint_number) {
978 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
979 dwc3_ep0_stall_and_restart(dwc);
980 return;
981 }
982
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300983 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300984 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300985
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300986 case DEPEVT_STATUS_CONTROL_STATUS:
987 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300988
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100989 dwc->ep0state = EP0_STATUS_PHASE;
990
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300991 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
992 dev_vdbg(dwc->dev, "Expected %d got %d\n",
Felipe Balbi25355be2011-09-30 10:58:38 +0300993 dwc->ep0_next_event,
994 DWC3_EP0_NRDY_STATUS);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300995
996 dwc3_ep0_stall_and_restart(dwc);
997 return;
998 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100999
1000 if (dwc->delayed_status) {
1001 WARN_ON_ONCE(event->endpoint_number != 1);
1002 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
1003 return;
1004 }
1005
Felipe Balbi788a23f2012-05-21 14:22:41 +03001006 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001007 }
1008}
1009
1010void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001011 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001012{
1013 u8 epnum = event->endpoint_number;
1014
1015 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1016 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001017 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001018 dwc3_ep0_state_string(dwc->ep0state));
1019
1020 switch (event->endpoint_event) {
1021 case DWC3_DEPEVT_XFERCOMPLETE:
1022 dwc3_ep0_xfer_complete(dwc, event);
1023 break;
1024
1025 case DWC3_DEPEVT_XFERNOTREADY:
1026 dwc3_ep0_xfernotready(dwc, event);
1027 break;
1028
1029 case DWC3_DEPEVT_XFERINPROGRESS:
1030 case DWC3_DEPEVT_RXTXFIFOEVT:
1031 case DWC3_DEPEVT_STREAMEVT:
1032 case DWC3_DEPEVT_EPCMDCMPLT:
1033 break;
1034 }
1035}