blob: 69d5741a4db01c7c8ae9e70907e937513d06e4ac [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 Balbi9610cd22012-05-21 14:22:41 +030057static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbidb0af402012-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 Balbi4959cfc2012-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 Balbi72246da2011-08-19 18:10:58 +0300128
129 req->request.actual = 0;
130 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300131 req->epnum = dep->number;
132
133 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300134
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300135 /*
136 * Gadget driver might not be quick enough to queue a request
137 * before we get a Transfer Not Ready event on this endpoint.
138 *
139 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
140 * flag is set, it's telling us that as soon as Gadget queues the
141 * required request, we should kick the transfer here because the
142 * IRQ we were waiting for is long gone.
143 */
144 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300145 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300146
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300147 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300148
Felipe Balbi68d8a782011-12-29 06:32:29 +0200149 if (dwc->ep0state != EP0_DATA_PHASE) {
150 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300151 return 0;
152 }
Felipe Balbia6829702011-08-27 22:18:09 +0300153
Felipe Balbidb0af402012-05-04 13:03:54 +0300154 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
155
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300156 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
157 DWC3_EP0_DIR_IN);
Felipe Balbi929646c2012-07-19 08:51:13 +0300158
159 return 0;
160 }
161
162 /*
163 * In case gadget driver asked us to delay the STATUS phase,
164 * handle it here.
165 */
166 if (dwc->delayed_status) {
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100167 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200168
169 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi9610cd22012-05-21 14:22:41 +0300170 __dwc3_ep0_do_control_status(dwc, dwc->eps[1]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200171 else
172 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi929646c2012-07-19 08:51:13 +0300173
174 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300175 }
176
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300177 /*
178 * Unfortunately we have uncovered a limitation wrt the Data Phase.
179 *
180 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
181 * come before issueing Start Transfer command, but if we do, we will
182 * miss situations where the host starts another SETUP phase instead of
183 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
184 * Layer Compliance Suite.
185 *
186 * The problem surfaces due to the fact that in case of back-to-back
187 * SETUP packets there will be no XferNotReady(DATA) generated and we
188 * will be stuck waiting for XferNotReady(DATA) forever.
189 *
190 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
191 * it tells us to start Data Phase right away. It also mentions that if
192 * we receive a SETUP phase instead of the DATA phase, core will issue
193 * XferComplete for the DATA phase, before actually initiating it in
194 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
195 * can only be used to print some debugging logs, as the core expects
196 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
197 * just so it completes right away, without transferring anything and,
198 * only then, we can go back to the SETUP phase.
199 *
200 * Because of this scenario, SNPS decided to change the programming
201 * model of control transfers and support on-demand transfers only for
202 * the STATUS phase. To fix the issue we have now, we will always wait
203 * for gadget driver to queue the DATA phase's struct usb_request, then
204 * start it right away.
205 *
206 * If we're actually in a 2-stage transfer, we will wait for
207 * XferNotReady(STATUS).
208 */
209 if (dwc->three_stage_setup) {
210 unsigned direction;
211
212 direction = dwc->ep0_expect_in;
213 dwc->ep0state = EP0_DATA_PHASE;
214
215 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
216
217 dep->flags &= ~DWC3_EP0_DIR_IN;
218 }
219
Felipe Balbi2640f342012-07-19 08:49:01 +0300220 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300221}
222
223int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
224 gfp_t gfp_flags)
225{
226 struct dwc3_request *req = to_dwc3_request(request);
227 struct dwc3_ep *dep = to_dwc3_ep(ep);
228 struct dwc3 *dwc = dep->dwc;
229
230 unsigned long flags;
231
232 int ret;
233
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200235 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300236 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
237 request, dep->name);
238 ret = -ESHUTDOWN;
239 goto out;
240 }
241
242 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200243 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300244 ret = -EBUSY;
245 goto out;
246 }
247
248 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
249 request, dep->name, request->length,
250 dwc3_ep0_state_string(dwc->ep0state));
251
252 ret = __dwc3_gadget_ep0_queue(dep, req);
253
254out:
255 spin_unlock_irqrestore(&dwc->lock, flags);
256
257 return ret;
258}
259
260static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
261{
Felipe Balbid7422202011-09-08 18:17:12 +0300262 struct dwc3_ep *dep = dwc->eps[0];
263
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200265 __dwc3_gadget_ep_set_halt(dep, 1);
266 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100267 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300268
269 if (!list_empty(&dep->request_list)) {
270 struct dwc3_request *req;
271
272 req = next_request(&dep->request_list);
273 dwc3_gadget_giveback(dep, req, -ECONNRESET);
274 }
275
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300276 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300277 dwc3_ep0_out_start(dwc);
278}
279
Pratyush Anandeb840752012-06-25 22:40:43 +0530280int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
281{
282 struct dwc3_ep *dep = to_dwc3_ep(ep);
283 struct dwc3 *dwc = dep->dwc;
284
285 dwc3_ep0_stall_and_restart(dwc);
286
287 return 0;
288}
289
Felipe Balbi72246da2011-08-19 18:10:58 +0300290void dwc3_ep0_out_start(struct dwc3 *dwc)
291{
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 int ret;
293
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300294 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
295 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 WARN_ON(ret < 0);
297}
298
Felipe Balbi72246da2011-08-19 18:10:58 +0300299static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
300{
301 struct dwc3_ep *dep;
302 u32 windex = le16_to_cpu(wIndex_le);
303 u32 epnum;
304
305 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
306 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
307 epnum |= 1;
308
309 dep = dwc->eps[epnum];
310 if (dep->flags & DWC3_EP_ENABLED)
311 return dep;
312
313 return NULL;
314}
315
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200316static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300317{
Felipe Balbi72246da2011-08-19 18:10:58 +0300318}
Felipe Balbi72246da2011-08-19 18:10:58 +0300319/*
320 * ch 9.4.5
321 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200322static int dwc3_ep0_handle_status(struct dwc3 *dwc,
323 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300324{
325 struct dwc3_ep *dep;
326 u32 recip;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200327 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300328 u16 usb_status = 0;
329 __le16 *response_pkt;
330
331 recip = ctrl->bRequestType & USB_RECIP_MASK;
332 switch (recip) {
333 case USB_RECIP_DEVICE:
334 /*
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200335 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 */
337 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200338
339 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
340 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
341 if (reg & DWC3_DCTL_INITU1ENA)
342 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
343 if (reg & DWC3_DCTL_INITU2ENA)
344 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
345 }
346
Felipe Balbi72246da2011-08-19 18:10:58 +0300347 break;
348
349 case USB_RECIP_INTERFACE:
350 /*
351 * Function Remote Wake Capable D0
352 * Function Remote Wakeup D1
353 */
354 break;
355
356 case USB_RECIP_ENDPOINT:
357 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
358 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200359 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300360
361 if (dep->flags & DWC3_EP_STALL)
362 usb_status = 1 << USB_ENDPOINT_HALT;
363 break;
364 default:
365 return -EINVAL;
366 };
367
368 response_pkt = (__le16 *) dwc->setup_buf;
369 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200370
371 dep = dwc->eps[0];
372 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100373 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200374 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100375 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200376
377 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300378}
379
380static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
381 struct usb_ctrlrequest *ctrl, int set)
382{
383 struct dwc3_ep *dep;
384 u32 recip;
385 u32 wValue;
386 u32 wIndex;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200387 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300388 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300389
390 wValue = le16_to_cpu(ctrl->wValue);
391 wIndex = le16_to_cpu(ctrl->wIndex);
392 recip = ctrl->bRequestType & USB_RECIP_MASK;
393 switch (recip) {
394 case USB_RECIP_DEVICE:
395
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200396 switch (wValue) {
397 case USB_DEVICE_REMOTE_WAKEUP:
398 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300399 /*
400 * 9.4.1 says only only for SS, in AddressState only for
401 * default control pipe
402 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300403 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300404 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
405 return -EINVAL;
406 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
407 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200409 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
410 if (set)
411 reg |= DWC3_DCTL_INITU1ENA;
412 else
413 reg &= ~DWC3_DCTL_INITU1ENA;
414 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300415 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200416
Felipe Balbi72246da2011-08-19 18:10:58 +0300417 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200418 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
419 return -EINVAL;
420 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
421 return -EINVAL;
422
423 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
424 if (set)
425 reg |= DWC3_DCTL_INITU2ENA;
426 else
427 reg &= ~DWC3_DCTL_INITU2ENA;
428 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200430
Felipe Balbi72246da2011-08-19 18:10:58 +0300431 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200432 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300433 break;
434
435 case USB_DEVICE_TEST_MODE:
436 if ((wIndex & 0xff) != 0)
437 return -EINVAL;
438 if (!set)
439 return -EINVAL;
440
Gerard Cauvy3b637362012-02-10 12:21:18 +0200441 dwc->test_mode_nr = wIndex >> 8;
442 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200443 break;
444 default:
445 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300446 }
447 break;
448
449 case USB_RECIP_INTERFACE:
450 switch (wValue) {
451 case USB_INTRF_FUNC_SUSPEND:
452 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
453 /* XXX enable Low power suspend */
454 ;
455 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
456 /* XXX enable remote wakeup */
457 ;
458 break;
459 default:
460 return -EINVAL;
461 }
462 break;
463
464 case USB_RECIP_ENDPOINT:
465 switch (wValue) {
466 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800467 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 if (!dep)
469 return -EINVAL;
470 ret = __dwc3_gadget_ep_set_halt(dep, set);
471 if (ret)
472 return -EINVAL;
473 break;
474 default:
475 return -EINVAL;
476 }
477 break;
478
479 default:
480 return -EINVAL;
481 };
482
Felipe Balbi72246da2011-08-19 18:10:58 +0300483 return 0;
484}
485
486static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
487{
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 u32 addr;
489 u32 reg;
490
491 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300492 if (addr > 127) {
493 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300494 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300495 }
496
497 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
498 dev_dbg(dwc->dev, "trying to set address when configured\n");
499 return -EINVAL;
500 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300501
Felipe Balbi26460212011-09-30 10:58:36 +0300502 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
503 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
504 reg |= DWC3_DCFG_DEVADDR(addr);
505 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300506
Felipe Balbi26460212011-09-30 10:58:36 +0300507 if (addr)
508 dwc->dev_state = DWC3_ADDRESS_STATE;
509 else
510 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300511
Felipe Balbi26460212011-09-30 10:58:36 +0300512 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300513}
514
515static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
516{
517 int ret;
518
519 spin_unlock(&dwc->lock);
520 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
521 spin_lock(&dwc->lock);
522 return ret;
523}
524
525static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
526{
527 u32 cfg;
528 int ret;
Pratyush Anand49917a42012-07-02 10:21:54 +0530529 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300530
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300531 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300532 cfg = le16_to_cpu(ctrl->wValue);
533
534 switch (dwc->dev_state) {
535 case DWC3_DEFAULT_STATE:
536 return -EINVAL;
537 break;
538
539 case DWC3_ADDRESS_STATE:
540 ret = dwc3_ep0_delegate_req(dwc, ctrl);
541 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200542 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300543 dwc->dev_state = DWC3_CONFIGURED_STATE;
Pratyush Anand49917a42012-07-02 10:21:54 +0530544 /*
545 * Enable transition to U1/U2 state when
546 * nothing is pending from application.
547 */
548 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
549 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
550 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
551
Felipe Balbi457e84b2012-01-18 18:04:09 +0200552 dwc->resize_fifos = true;
553 dev_dbg(dwc->dev, "resize fifos flag SET\n");
554 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 break;
556
557 case DWC3_CONFIGURED_STATE:
558 ret = dwc3_ep0_delegate_req(dwc, ctrl);
559 if (!cfg)
560 dwc->dev_state = DWC3_ADDRESS_STATE;
561 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100562 default:
563 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100565 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566}
567
Felipe Balbi9e788d62012-04-24 16:19:49 +0300568static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
569{
570 struct dwc3_ep *dep = to_dwc3_ep(ep);
571 struct dwc3 *dwc = dep->dwc;
572
573 u32 param = 0;
574 u32 reg;
575
576 struct timing {
577 u8 u1sel;
578 u8 u1pel;
579 u16 u2sel;
580 u16 u2pel;
581 } __packed timing;
582
583 int ret;
584
585 memcpy(&timing, req->buf, sizeof(timing));
586
587 dwc->u1sel = timing.u1sel;
588 dwc->u1pel = timing.u1pel;
Felipe Balbi87619212012-05-31 11:00:28 +0300589 dwc->u2sel = le16_to_cpu(timing.u2sel);
590 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi9e788d62012-04-24 16:19:49 +0300591
592 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
593 if (reg & DWC3_DCTL_INITU2ENA)
594 param = dwc->u2pel;
595 if (reg & DWC3_DCTL_INITU1ENA)
596 param = dwc->u1pel;
597
598 /*
599 * According to Synopsys Databook, if parameter is
600 * greater than 125, a value of zero should be
601 * programmed in the register.
602 */
603 if (param > 125)
604 param = 0;
605
606 /* now that we have the time, issue DGCMD Set Sel */
607 ret = dwc3_send_gadget_generic_command(dwc,
608 DWC3_DGCMD_SET_PERIODIC_PAR, param);
609 WARN_ON(ret < 0);
610}
611
612static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
613{
614 struct dwc3_ep *dep;
615 u16 wLength;
616 u16 wValue;
617
618 if (dwc->dev_state == DWC3_DEFAULT_STATE)
619 return -EINVAL;
620
621 wValue = le16_to_cpu(ctrl->wValue);
622 wLength = le16_to_cpu(ctrl->wLength);
623
624 if (wLength != 6) {
625 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
626 wLength);
627 return -EINVAL;
628 }
629
630 /*
631 * To handle Set SEL we need to receive 6 bytes from Host. So let's
632 * queue a usb_request for 6 bytes.
633 *
634 * Remember, though, this controller can't handle non-wMaxPacketSize
635 * aligned transfers on the OUT direction, so we queue a request for
636 * wMaxPacketSize instead.
637 */
638 dep = dwc->eps[0];
639 dwc->ep0_usb_req.dep = dep;
640 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
641 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
642 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
643
644 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
645}
646
Felipe Balbi395c3492012-04-25 10:45:05 +0300647static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
648{
649 u16 wLength;
650 u16 wValue;
651 u16 wIndex;
652
653 wValue = le16_to_cpu(ctrl->wValue);
654 wLength = le16_to_cpu(ctrl->wLength);
655 wIndex = le16_to_cpu(ctrl->wIndex);
656
657 if (wIndex || wLength)
658 return -EINVAL;
659
660 /*
661 * REVISIT It's unclear from Databook what to do with this
662 * value. For now, just cache it.
663 */
664 dwc->isoch_delay = wValue;
665
666 return 0;
667}
668
Felipe Balbi72246da2011-08-19 18:10:58 +0300669static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
670{
671 int ret;
672
673 switch (ctrl->bRequest) {
674 case USB_REQ_GET_STATUS:
675 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
676 ret = dwc3_ep0_handle_status(dwc, ctrl);
677 break;
678 case USB_REQ_CLEAR_FEATURE:
679 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
680 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
681 break;
682 case USB_REQ_SET_FEATURE:
683 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
684 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
685 break;
686 case USB_REQ_SET_ADDRESS:
687 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
688 ret = dwc3_ep0_set_address(dwc, ctrl);
689 break;
690 case USB_REQ_SET_CONFIGURATION:
691 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
692 ret = dwc3_ep0_set_config(dwc, ctrl);
693 break;
Felipe Balbi9e788d62012-04-24 16:19:49 +0300694 case USB_REQ_SET_SEL:
695 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
696 ret = dwc3_ep0_set_sel(dwc, ctrl);
697 break;
Felipe Balbi395c3492012-04-25 10:45:05 +0300698 case USB_REQ_SET_ISOCH_DELAY:
699 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
700 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
701 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300702 default:
703 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
704 ret = dwc3_ep0_delegate_req(dwc, ctrl);
705 break;
706 };
707
708 return ret;
709}
710
711static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
712 const struct dwc3_event_depevt *event)
713{
714 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbi65811b62012-05-31 10:29:49 +0300715 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 u32 len;
717
718 if (!dwc->gadget_driver)
Felipe Balbi65811b62012-05-31 10:29:49 +0300719 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300720
721 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300722 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300723 dwc->three_stage_setup = false;
724 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300725 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
726 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300727 dwc->three_stage_setup = true;
728 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300729 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
730 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300731
732 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
733 ret = dwc3_ep0_std_request(dwc, ctrl);
734 else
735 ret = dwc3_ep0_delegate_req(dwc, ctrl);
736
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100737 if (ret == USB_GADGET_DELAYED_STATUS)
738 dwc->delayed_status = true;
739
Felipe Balbi65811b62012-05-31 10:29:49 +0300740out:
741 if (ret < 0)
742 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300743}
744
745static void dwc3_ep0_complete_data(struct dwc3 *dwc,
746 const struct dwc3_event_depevt *event)
747{
748 struct dwc3_request *r = NULL;
749 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200750 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200751 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300752 u32 transferred;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300753 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200754 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 u8 epnum;
756
757 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200758 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300759
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300760 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
761
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200762 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200763 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300764
Felipe Balbif6bafc62012-02-06 11:04:53 +0200765 trb = dwc->ep0_trb;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300766
767 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
768 if (status == DWC3_TRBSTS_SETUP_PENDING) {
769 dev_dbg(dwc->dev, "Setup Pending received\n");
770
771 if (r)
772 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
773
774 return;
775 }
776
Felipe Balbif6bafc62012-02-06 11:04:53 +0200777 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300778
Felipe Balbia6829702011-08-27 22:18:09 +0300779 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500780 unsigned transfer_size = ur->length;
781 unsigned maxp = ep0->endpoint.maxpacket;
782
783 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300784 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500785 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300786 memcpy(ur->buf, dwc->ep0_bounce, transferred);
787 dwc->ep0_bounced = false;
788 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200789 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300790 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300791
Felipe Balbicd423dd2012-03-21 11:44:00 +0200792 ur->actual += transferred;
793
Felipe Balbi72246da2011-08-19 18:10:58 +0300794 if ((epnum & 1) && ur->actual < ur->length) {
795 /* for some reason we did not get everything out */
796
797 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300798 } else {
799 /*
800 * handle the case where we have to send a zero packet. This
801 * seems to be case when req.length > maxpacket. Could it be?
802 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300803 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200804 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 }
806}
807
Felipe Balbied8c3982012-05-31 12:32:37 +0300808static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 const struct dwc3_event_depevt *event)
810{
811 struct dwc3_request *r;
812 struct dwc3_ep *dep;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300813 struct dwc3_trb *trb;
814 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300815
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300816 dep = dwc->eps[0];
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300817 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300818
819 if (!list_empty(&dep->request_list)) {
820 r = next_request(&dep->request_list);
821
822 dwc3_gadget_giveback(dep, r, 0);
823 }
824
Gerard Cauvy3b637362012-02-10 12:21:18 +0200825 if (dwc->test_mode) {
826 int ret;
827
828 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
829 if (ret < 0) {
830 dev_dbg(dwc->dev, "Invalid Test #%d\n",
831 dwc->test_mode_nr);
832 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi0ed27b12012-06-25 19:30:49 +0300833 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200834 }
835 }
836
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300837 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
838 if (status == DWC3_TRBSTS_SETUP_PENDING)
839 dev_dbg(dwc->dev, "Setup Pending received\n");
840
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300841 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300842 dwc3_ep0_out_start(dwc);
843}
844
845static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
846 const struct dwc3_event_depevt *event)
847{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300848 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
849
850 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi4959cfc2012-06-06 12:04:13 +0300851 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300852 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300853
Felipe Balbi72246da2011-08-19 18:10:58 +0300854 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300855 case EP0_SETUP_PHASE:
856 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300857 dwc3_ep0_inspect_setup(dwc, event);
858 break;
859
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300860 case EP0_DATA_PHASE:
861 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300862 dwc3_ep0_complete_data(dwc, event);
863 break;
864
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300865 case EP0_STATUS_PHASE:
866 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbied8c3982012-05-31 12:32:37 +0300867 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300868 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300869 default:
870 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 }
872}
873
Felipe Balbidb0af402012-05-04 13:03:54 +0300874static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
875 struct dwc3_ep *dep, struct dwc3_request *req)
876{
877 int ret;
878
879 req->direction = !!dep->number;
880
881 if (req->request.length == 0) {
882 ret = dwc3_ep0_start_trans(dwc, dep->number,
883 dwc->ctrl_req_addr, 0,
884 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbi0a1730a2012-05-04 13:08:22 +0300885 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbidb0af402012-05-04 13:03:54 +0300886 && (dep->number == 0)) {
887 u32 transfer_size;
888
889 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
890 dep->number);
891 if (ret) {
892 dev_dbg(dwc->dev, "failed to map request\n");
893 return;
894 }
895
896 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
897
898 transfer_size = roundup(req->request.length,
899 (u32) dep->endpoint.maxpacket);
900
901 dwc->ep0_bounced = true;
902
903 /*
904 * REVISIT in case request length is bigger than
905 * DWC3_EP0_BOUNCE_SIZE we will need two chained
906 * TRBs to handle the transfer.
907 */
908 ret = dwc3_ep0_start_trans(dwc, dep->number,
909 dwc->ep0_bounce_addr, transfer_size,
910 DWC3_TRBCTL_CONTROL_DATA);
911 } else {
912 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
913 dep->number);
914 if (ret) {
915 dev_dbg(dwc->dev, "failed to map request\n");
916 return;
917 }
918
919 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
920 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
921 }
922
923 WARN_ON(ret < 0);
924}
925
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300926static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
927 const struct dwc3_event_depevt *event)
928{
929 struct dwc3_ep *dep;
930 struct dwc3_request *req;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300931
932 dep = dwc->eps[0];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300933
934 if (list_empty(&dep->request_list)) {
935 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
936 dep->flags |= DWC3_EP_PENDING_REQUEST;
937
938 if (event->endpoint_number)
939 dep->flags |= DWC3_EP0_DIR_IN;
940 return;
941 }
942
943 req = next_request(&dep->request_list);
Felipe Balbidb0af402012-05-04 13:03:54 +0300944 dep = dwc->eps[event->endpoint_number];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300945
Felipe Balbidb0af402012-05-04 13:03:54 +0300946 __dwc3_ep0_do_control_data(dwc, dep, req);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300947}
948
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100949static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300950{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100951 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300952 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300953
954 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
955 : DWC3_TRBCTL_CONTROL_STATUS2;
956
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100957 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300958 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100959}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300960
Felipe Balbi9610cd22012-05-21 14:22:41 +0300961static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100962{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200963 if (dwc->resize_fifos) {
964 dev_dbg(dwc->dev, "starting to resize fifos\n");
965 dwc3_gadget_resize_tx_fifos(dwc);
966 dwc->resize_fifos = 0;
967 }
968
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100969 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300970}
971
Felipe Balbi9610cd22012-05-21 14:22:41 +0300972static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
973 const struct dwc3_event_depevt *event)
974{
975 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
976
977 __dwc3_ep0_do_control_status(dwc, dep);
978}
979
Felipe Balbi72246da2011-08-19 18:10:58 +0300980static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
981 const struct dwc3_event_depevt *event)
982{
Felipe Balbidf62df52011-10-14 15:11:49 +0300983 dwc->setup_packet_pending = true;
984
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300985 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300986 case DEPEVT_STATUS_CONTROL_DATA:
987 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300988
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100989 dwc->ep0state = EP0_DATA_PHASE;
990
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300991 if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
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_DATA);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300995
996 dwc3_ep0_stall_and_restart(dwc);
997 return;
998 }
999
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001000 /*
1001 * One of the possible error cases is when Host _does_
1002 * request for Data Phase, but it does so on the wrong
1003 * direction.
1004 *
1005 * Here, we already know ep0_next_event is DATA (see above),
1006 * so we only need to check for direction.
1007 */
1008 if (dwc->ep0_expect_in != event->endpoint_number) {
1009 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
1010 dwc3_ep0_stall_and_restart(dwc);
1011 return;
1012 }
1013
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001014 dwc3_ep0_do_control_data(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001015 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001016
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001017 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbicb84c5e2012-07-23 09:09:32 +03001018 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1019 return;
1020
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001021 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001022
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001023 dwc->ep0state = EP0_STATUS_PHASE;
1024
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001025 if (dwc->delayed_status) {
1026 WARN_ON_ONCE(event->endpoint_number != 1);
1027 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
1028 return;
1029 }
1030
Felipe Balbi9610cd22012-05-21 14:22:41 +03001031 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001032 }
1033}
1034
1035void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001036 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001037{
1038 u8 epnum = event->endpoint_number;
1039
1040 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1041 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001042 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001043 dwc3_ep0_state_string(dwc->ep0state));
1044
1045 switch (event->endpoint_event) {
1046 case DWC3_DEPEVT_XFERCOMPLETE:
1047 dwc3_ep0_xfer_complete(dwc, event);
1048 break;
1049
1050 case DWC3_DEPEVT_XFERNOTREADY:
1051 dwc3_ep0_xfernotready(dwc, event);
1052 break;
1053
1054 case DWC3_DEPEVT_XFERINPROGRESS:
1055 case DWC3_DEPEVT_RXTXFIFOEVT:
1056 case DWC3_DEPEVT_STREAMEVT:
1057 case DWC3_DEPEVT_EPCMDCMPLT:
1058 break;
1059 }
1060}