blob: 3f2c698a2bd16a8e35c13c6f6bd9d7be0ee24a53 [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) {
Felipe Balbia866ee02012-07-19 21:05:08 +0300167 unsigned direction;
168
169 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100170 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200171
172 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbia866ee02012-07-19 21:05:08 +0300173 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200174 else
175 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbi929646c2012-07-19 08:51:13 +0300176
177 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300178 }
179
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300180 /*
181 * Unfortunately we have uncovered a limitation wrt the Data Phase.
182 *
183 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
184 * come before issueing Start Transfer command, but if we do, we will
185 * miss situations where the host starts another SETUP phase instead of
186 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
187 * Layer Compliance Suite.
188 *
189 * The problem surfaces due to the fact that in case of back-to-back
190 * SETUP packets there will be no XferNotReady(DATA) generated and we
191 * will be stuck waiting for XferNotReady(DATA) forever.
192 *
193 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
194 * it tells us to start Data Phase right away. It also mentions that if
195 * we receive a SETUP phase instead of the DATA phase, core will issue
196 * XferComplete for the DATA phase, before actually initiating it in
197 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
198 * can only be used to print some debugging logs, as the core expects
199 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
200 * just so it completes right away, without transferring anything and,
201 * only then, we can go back to the SETUP phase.
202 *
203 * Because of this scenario, SNPS decided to change the programming
204 * model of control transfers and support on-demand transfers only for
205 * the STATUS phase. To fix the issue we have now, we will always wait
206 * for gadget driver to queue the DATA phase's struct usb_request, then
207 * start it right away.
208 *
209 * If we're actually in a 2-stage transfer, we will wait for
210 * XferNotReady(STATUS).
211 */
212 if (dwc->three_stage_setup) {
213 unsigned direction;
214
215 direction = dwc->ep0_expect_in;
216 dwc->ep0state = EP0_DATA_PHASE;
217
218 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
219
220 dep->flags &= ~DWC3_EP0_DIR_IN;
221 }
222
Felipe Balbi2640f342012-07-19 08:49:01 +0300223 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300224}
225
226int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
227 gfp_t gfp_flags)
228{
229 struct dwc3_request *req = to_dwc3_request(request);
230 struct dwc3_ep *dep = to_dwc3_ep(ep);
231 struct dwc3 *dwc = dep->dwc;
232
233 unsigned long flags;
234
235 int ret;
236
Felipe Balbi72246da2011-08-19 18:10:58 +0300237 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200238 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300239 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
240 request, dep->name);
241 ret = -ESHUTDOWN;
242 goto out;
243 }
244
245 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200246 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300247 ret = -EBUSY;
248 goto out;
249 }
250
251 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
252 request, dep->name, request->length,
253 dwc3_ep0_state_string(dwc->ep0state));
254
255 ret = __dwc3_gadget_ep0_queue(dep, req);
256
257out:
258 spin_unlock_irqrestore(&dwc->lock, flags);
259
260 return ret;
261}
262
263static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
264{
Felipe Balbid7422202011-09-08 18:17:12 +0300265 struct dwc3_ep *dep = dwc->eps[0];
266
Felipe Balbi72246da2011-08-19 18:10:58 +0300267 /* stall is always issued on EP0 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200268 __dwc3_gadget_ep_set_halt(dep, 1);
269 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100270 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300271
272 if (!list_empty(&dep->request_list)) {
273 struct dwc3_request *req;
274
275 req = next_request(&dep->request_list);
276 dwc3_gadget_giveback(dep, req, -ECONNRESET);
277 }
278
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300279 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 dwc3_ep0_out_start(dwc);
281}
282
Pratyush Anandeb840752012-06-25 22:40:43 +0530283int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
284{
285 struct dwc3_ep *dep = to_dwc3_ep(ep);
286 struct dwc3 *dwc = dep->dwc;
287
288 dwc3_ep0_stall_and_restart(dwc);
289
290 return 0;
291}
292
Felipe Balbi72246da2011-08-19 18:10:58 +0300293void dwc3_ep0_out_start(struct dwc3 *dwc)
294{
Felipe Balbi72246da2011-08-19 18:10:58 +0300295 int ret;
296
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300297 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
298 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300299 WARN_ON(ret < 0);
300}
301
Felipe Balbi72246da2011-08-19 18:10:58 +0300302static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
303{
304 struct dwc3_ep *dep;
305 u32 windex = le16_to_cpu(wIndex_le);
306 u32 epnum;
307
308 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
309 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
310 epnum |= 1;
311
312 dep = dwc->eps[epnum];
313 if (dep->flags & DWC3_EP_ENABLED)
314 return dep;
315
316 return NULL;
317}
318
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200319static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300320{
Felipe Balbi72246da2011-08-19 18:10:58 +0300321}
Felipe Balbi72246da2011-08-19 18:10:58 +0300322/*
323 * ch 9.4.5
324 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200325static int dwc3_ep0_handle_status(struct dwc3 *dwc,
326 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300327{
328 struct dwc3_ep *dep;
329 u32 recip;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200330 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300331 u16 usb_status = 0;
332 __le16 *response_pkt;
333
334 recip = ctrl->bRequestType & USB_RECIP_MASK;
335 switch (recip) {
336 case USB_RECIP_DEVICE:
337 /*
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200338 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300339 */
340 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200341
342 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
343 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
344 if (reg & DWC3_DCTL_INITU1ENA)
345 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
346 if (reg & DWC3_DCTL_INITU2ENA)
347 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
348 }
349
Felipe Balbi72246da2011-08-19 18:10:58 +0300350 break;
351
352 case USB_RECIP_INTERFACE:
353 /*
354 * Function Remote Wake Capable D0
355 * Function Remote Wakeup D1
356 */
357 break;
358
359 case USB_RECIP_ENDPOINT:
360 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
361 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200362 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300363
364 if (dep->flags & DWC3_EP_STALL)
365 usb_status = 1 << USB_ENDPOINT_HALT;
366 break;
367 default:
368 return -EINVAL;
369 };
370
371 response_pkt = (__le16 *) dwc->setup_buf;
372 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200373
374 dep = dwc->eps[0];
375 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100376 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200377 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100378 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200379
380 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300381}
382
383static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
384 struct usb_ctrlrequest *ctrl, int set)
385{
386 struct dwc3_ep *dep;
387 u32 recip;
388 u32 wValue;
389 u32 wIndex;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200390 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300391 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300392
393 wValue = le16_to_cpu(ctrl->wValue);
394 wIndex = le16_to_cpu(ctrl->wIndex);
395 recip = ctrl->bRequestType & USB_RECIP_MASK;
396 switch (recip) {
397 case USB_RECIP_DEVICE:
398
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200399 switch (wValue) {
400 case USB_DEVICE_REMOTE_WAKEUP:
401 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300402 /*
403 * 9.4.1 says only only for SS, in AddressState only for
404 * default control pipe
405 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 case USB_DEVICE_U1_ENABLE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300407 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
408 return -EINVAL;
409 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
410 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200412 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
413 if (set)
414 reg |= DWC3_DCTL_INITU1ENA;
415 else
416 reg &= ~DWC3_DCTL_INITU1ENA;
417 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300418 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200419
Felipe Balbi72246da2011-08-19 18:10:58 +0300420 case USB_DEVICE_U2_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200421 if (dwc->dev_state != DWC3_CONFIGURED_STATE)
422 return -EINVAL;
423 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
424 return -EINVAL;
425
426 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
427 if (set)
428 reg |= DWC3_DCTL_INITU2ENA;
429 else
430 reg &= ~DWC3_DCTL_INITU2ENA;
431 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300432 break;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200433
Felipe Balbi72246da2011-08-19 18:10:58 +0300434 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +0200435 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 break;
437
438 case USB_DEVICE_TEST_MODE:
439 if ((wIndex & 0xff) != 0)
440 return -EINVAL;
441 if (!set)
442 return -EINVAL;
443
Gerard Cauvy3b637362012-02-10 12:21:18 +0200444 dwc->test_mode_nr = wIndex >> 8;
445 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200446 break;
447 default:
448 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449 }
450 break;
451
452 case USB_RECIP_INTERFACE:
453 switch (wValue) {
454 case USB_INTRF_FUNC_SUSPEND:
455 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
456 /* XXX enable Low power suspend */
457 ;
458 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
459 /* XXX enable remote wakeup */
460 ;
461 break;
462 default:
463 return -EINVAL;
464 }
465 break;
466
467 case USB_RECIP_ENDPOINT:
468 switch (wValue) {
469 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800470 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300471 if (!dep)
472 return -EINVAL;
473 ret = __dwc3_gadget_ep_set_halt(dep, set);
474 if (ret)
475 return -EINVAL;
476 break;
477 default:
478 return -EINVAL;
479 }
480 break;
481
482 default:
483 return -EINVAL;
484 };
485
Felipe Balbi72246da2011-08-19 18:10:58 +0300486 return 0;
487}
488
489static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
490{
Felipe Balbi72246da2011-08-19 18:10:58 +0300491 u32 addr;
492 u32 reg;
493
494 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300495 if (addr > 127) {
496 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300497 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300498 }
499
500 if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
501 dev_dbg(dwc->dev, "trying to set address when configured\n");
502 return -EINVAL;
503 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300504
Felipe Balbi26460212011-09-30 10:58:36 +0300505 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
506 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
507 reg |= DWC3_DCFG_DEVADDR(addr);
508 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300509
Felipe Balbi26460212011-09-30 10:58:36 +0300510 if (addr)
511 dwc->dev_state = DWC3_ADDRESS_STATE;
512 else
513 dwc->dev_state = DWC3_DEFAULT_STATE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
Felipe Balbi26460212011-09-30 10:58:36 +0300515 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300516}
517
518static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
519{
520 int ret;
521
522 spin_unlock(&dwc->lock);
523 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
524 spin_lock(&dwc->lock);
525 return ret;
526}
527
528static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
529{
530 u32 cfg;
531 int ret;
Pratyush Anand49917a42012-07-02 10:21:54 +0530532 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300533
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300534 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 cfg = le16_to_cpu(ctrl->wValue);
536
537 switch (dwc->dev_state) {
538 case DWC3_DEFAULT_STATE:
539 return -EINVAL;
540 break;
541
542 case DWC3_ADDRESS_STATE:
543 ret = dwc3_ep0_delegate_req(dwc, ctrl);
544 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200545 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300546 dwc->dev_state = DWC3_CONFIGURED_STATE;
Pratyush Anand49917a42012-07-02 10:21:54 +0530547 /*
548 * Enable transition to U1/U2 state when
549 * nothing is pending from application.
550 */
551 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
552 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
553 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
554
Felipe Balbi457e84b2012-01-18 18:04:09 +0200555 dwc->resize_fifos = true;
556 dev_dbg(dwc->dev, "resize fifos flag SET\n");
557 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300558 break;
559
560 case DWC3_CONFIGURED_STATE:
561 ret = dwc3_ep0_delegate_req(dwc, ctrl);
562 if (!cfg)
563 dwc->dev_state = DWC3_ADDRESS_STATE;
564 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100565 default:
566 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100568 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300569}
570
Felipe Balbi9e788d62012-04-24 16:19:49 +0300571static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
572{
573 struct dwc3_ep *dep = to_dwc3_ep(ep);
574 struct dwc3 *dwc = dep->dwc;
575
576 u32 param = 0;
577 u32 reg;
578
579 struct timing {
580 u8 u1sel;
581 u8 u1pel;
582 u16 u2sel;
583 u16 u2pel;
584 } __packed timing;
585
586 int ret;
587
588 memcpy(&timing, req->buf, sizeof(timing));
589
590 dwc->u1sel = timing.u1sel;
591 dwc->u1pel = timing.u1pel;
Felipe Balbi87619212012-05-31 11:00:28 +0300592 dwc->u2sel = le16_to_cpu(timing.u2sel);
593 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi9e788d62012-04-24 16:19:49 +0300594
595 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
596 if (reg & DWC3_DCTL_INITU2ENA)
597 param = dwc->u2pel;
598 if (reg & DWC3_DCTL_INITU1ENA)
599 param = dwc->u1pel;
600
601 /*
602 * According to Synopsys Databook, if parameter is
603 * greater than 125, a value of zero should be
604 * programmed in the register.
605 */
606 if (param > 125)
607 param = 0;
608
609 /* now that we have the time, issue DGCMD Set Sel */
610 ret = dwc3_send_gadget_generic_command(dwc,
611 DWC3_DGCMD_SET_PERIODIC_PAR, param);
612 WARN_ON(ret < 0);
613}
614
615static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
616{
617 struct dwc3_ep *dep;
618 u16 wLength;
619 u16 wValue;
620
621 if (dwc->dev_state == DWC3_DEFAULT_STATE)
622 return -EINVAL;
623
624 wValue = le16_to_cpu(ctrl->wValue);
625 wLength = le16_to_cpu(ctrl->wLength);
626
627 if (wLength != 6) {
628 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
629 wLength);
630 return -EINVAL;
631 }
632
633 /*
634 * To handle Set SEL we need to receive 6 bytes from Host. So let's
635 * queue a usb_request for 6 bytes.
636 *
637 * Remember, though, this controller can't handle non-wMaxPacketSize
638 * aligned transfers on the OUT direction, so we queue a request for
639 * wMaxPacketSize instead.
640 */
641 dep = dwc->eps[0];
642 dwc->ep0_usb_req.dep = dep;
643 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
644 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
645 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
646
647 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
648}
649
Felipe Balbi395c3492012-04-25 10:45:05 +0300650static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
651{
652 u16 wLength;
653 u16 wValue;
654 u16 wIndex;
655
656 wValue = le16_to_cpu(ctrl->wValue);
657 wLength = le16_to_cpu(ctrl->wLength);
658 wIndex = le16_to_cpu(ctrl->wIndex);
659
660 if (wIndex || wLength)
661 return -EINVAL;
662
663 /*
664 * REVISIT It's unclear from Databook what to do with this
665 * value. For now, just cache it.
666 */
667 dwc->isoch_delay = wValue;
668
669 return 0;
670}
671
Felipe Balbi72246da2011-08-19 18:10:58 +0300672static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
673{
674 int ret;
675
676 switch (ctrl->bRequest) {
677 case USB_REQ_GET_STATUS:
678 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
679 ret = dwc3_ep0_handle_status(dwc, ctrl);
680 break;
681 case USB_REQ_CLEAR_FEATURE:
682 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
683 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
684 break;
685 case USB_REQ_SET_FEATURE:
686 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
687 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
688 break;
689 case USB_REQ_SET_ADDRESS:
690 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
691 ret = dwc3_ep0_set_address(dwc, ctrl);
692 break;
693 case USB_REQ_SET_CONFIGURATION:
694 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
695 ret = dwc3_ep0_set_config(dwc, ctrl);
696 break;
Felipe Balbi9e788d62012-04-24 16:19:49 +0300697 case USB_REQ_SET_SEL:
698 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
699 ret = dwc3_ep0_set_sel(dwc, ctrl);
700 break;
Felipe Balbi395c3492012-04-25 10:45:05 +0300701 case USB_REQ_SET_ISOCH_DELAY:
702 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
703 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
704 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 default:
706 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
707 ret = dwc3_ep0_delegate_req(dwc, ctrl);
708 break;
709 };
710
711 return ret;
712}
713
714static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
715 const struct dwc3_event_depevt *event)
716{
717 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbi65811b62012-05-31 10:29:49 +0300718 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300719 u32 len;
720
721 if (!dwc->gadget_driver)
Felipe Balbi65811b62012-05-31 10:29:49 +0300722 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300723
724 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300725 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300726 dwc->three_stage_setup = false;
727 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300728 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
729 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300730 dwc->three_stage_setup = true;
731 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300732 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
733 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300734
735 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
736 ret = dwc3_ep0_std_request(dwc, ctrl);
737 else
738 ret = dwc3_ep0_delegate_req(dwc, ctrl);
739
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100740 if (ret == USB_GADGET_DELAYED_STATUS)
741 dwc->delayed_status = true;
742
Felipe Balbi65811b62012-05-31 10:29:49 +0300743out:
744 if (ret < 0)
745 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300746}
747
748static void dwc3_ep0_complete_data(struct dwc3 *dwc,
749 const struct dwc3_event_depevt *event)
750{
751 struct dwc3_request *r = NULL;
752 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200753 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200754 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300755 u32 transferred;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300756 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200757 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 u8 epnum;
759
760 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200761 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300762
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300763 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
764
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200765 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200766 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300767
Felipe Balbif6bafc62012-02-06 11:04:53 +0200768 trb = dwc->ep0_trb;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300769
770 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
771 if (status == DWC3_TRBSTS_SETUP_PENDING) {
772 dev_dbg(dwc->dev, "Setup Pending received\n");
773
774 if (r)
775 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
776
777 return;
778 }
779
Felipe Balbif6bafc62012-02-06 11:04:53 +0200780 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300781
Felipe Balbia6829702011-08-27 22:18:09 +0300782 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500783 unsigned transfer_size = ur->length;
784 unsigned maxp = ep0->endpoint.maxpacket;
785
786 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300787 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500788 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300789 memcpy(ur->buf, dwc->ep0_bounce, transferred);
790 dwc->ep0_bounced = false;
791 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200792 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300793 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300794
Felipe Balbicd423dd2012-03-21 11:44:00 +0200795 ur->actual += transferred;
796
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 if ((epnum & 1) && ur->actual < ur->length) {
798 /* for some reason we did not get everything out */
799
800 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300801 } else {
802 /*
803 * handle the case where we have to send a zero packet. This
804 * seems to be case when req.length > maxpacket. Could it be?
805 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200807 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 }
809}
810
Felipe Balbied8c3982012-05-31 12:32:37 +0300811static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300812 const struct dwc3_event_depevt *event)
813{
814 struct dwc3_request *r;
815 struct dwc3_ep *dep;
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300816 struct dwc3_trb *trb;
817 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300818
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300819 dep = dwc->eps[0];
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300820 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300821
822 if (!list_empty(&dep->request_list)) {
823 r = next_request(&dep->request_list);
824
825 dwc3_gadget_giveback(dep, r, 0);
826 }
827
Gerard Cauvy3b637362012-02-10 12:21:18 +0200828 if (dwc->test_mode) {
829 int ret;
830
831 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
832 if (ret < 0) {
833 dev_dbg(dwc->dev, "Invalid Test #%d\n",
834 dwc->test_mode_nr);
835 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi0ed27b12012-06-25 19:30:49 +0300836 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200837 }
838 }
839
Felipe Balbib6c4acd2012-07-19 09:05:35 +0300840 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
841 if (status == DWC3_TRBSTS_SETUP_PENDING)
842 dev_dbg(dwc->dev, "Setup Pending received\n");
843
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300844 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300845 dwc3_ep0_out_start(dwc);
846}
847
848static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
849 const struct dwc3_event_depevt *event)
850{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300851 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
852
853 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi4959cfc2012-06-06 12:04:13 +0300854 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300855 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300856
Felipe Balbi72246da2011-08-19 18:10:58 +0300857 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300858 case EP0_SETUP_PHASE:
859 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 dwc3_ep0_inspect_setup(dwc, event);
861 break;
862
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300863 case EP0_DATA_PHASE:
864 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300865 dwc3_ep0_complete_data(dwc, event);
866 break;
867
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300868 case EP0_STATUS_PHASE:
869 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbied8c3982012-05-31 12:32:37 +0300870 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300872 default:
873 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 }
875}
876
Felipe Balbidb0af402012-05-04 13:03:54 +0300877static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
878 struct dwc3_ep *dep, struct dwc3_request *req)
879{
880 int ret;
881
882 req->direction = !!dep->number;
883
884 if (req->request.length == 0) {
885 ret = dwc3_ep0_start_trans(dwc, dep->number,
886 dwc->ctrl_req_addr, 0,
887 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbi0a1730a2012-05-04 13:08:22 +0300888 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbidb0af402012-05-04 13:03:54 +0300889 && (dep->number == 0)) {
890 u32 transfer_size;
891
892 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
893 dep->number);
894 if (ret) {
895 dev_dbg(dwc->dev, "failed to map request\n");
896 return;
897 }
898
899 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
900
901 transfer_size = roundup(req->request.length,
902 (u32) dep->endpoint.maxpacket);
903
904 dwc->ep0_bounced = true;
905
906 /*
907 * REVISIT in case request length is bigger than
908 * DWC3_EP0_BOUNCE_SIZE we will need two chained
909 * TRBs to handle the transfer.
910 */
911 ret = dwc3_ep0_start_trans(dwc, dep->number,
912 dwc->ep0_bounce_addr, transfer_size,
913 DWC3_TRBCTL_CONTROL_DATA);
914 } else {
915 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
916 dep->number);
917 if (ret) {
918 dev_dbg(dwc->dev, "failed to map request\n");
919 return;
920 }
921
922 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
923 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
924 }
925
926 WARN_ON(ret < 0);
927}
928
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100929static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300930{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100931 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300932 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300933
934 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
935 : DWC3_TRBCTL_CONTROL_STATUS2;
936
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100937 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300938 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100939}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300940
Felipe Balbi9610cd22012-05-21 14:22:41 +0300941static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100942{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200943 if (dwc->resize_fifos) {
944 dev_dbg(dwc->dev, "starting to resize fifos\n");
945 dwc3_gadget_resize_tx_fifos(dwc);
946 dwc->resize_fifos = 0;
947 }
948
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100949 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300950}
951
Felipe Balbi9610cd22012-05-21 14:22:41 +0300952static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
953 const struct dwc3_event_depevt *event)
954{
955 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
956
957 __dwc3_ep0_do_control_status(dwc, dep);
958}
959
Felipe Balbia7e8d652012-07-19 09:26:59 +0300960static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
961{
962 struct dwc3_gadget_ep_cmd_params params;
963 u32 cmd;
964 int ret;
965
966 if (!dep->resource_index)
967 return;
968
969 cmd = DWC3_DEPCMD_ENDTRANSFER;
970 cmd |= DWC3_DEPCMD_CMDIOC;
971 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
972 memset(&params, 0, sizeof(params));
973 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
974 WARN_ON_ONCE(ret);
975 dep->resource_index = 0;
976}
977
Felipe Balbi72246da2011-08-19 18:10:58 +0300978static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
979 const struct dwc3_event_depevt *event)
980{
Felipe Balbidf62df52011-10-14 15:11:49 +0300981 dwc->setup_packet_pending = true;
982
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300983 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300984 case DEPEVT_STATUS_CONTROL_DATA:
985 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300986
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300987 /*
Felipe Balbia7e8d652012-07-19 09:26:59 +0300988 * We already have a DATA transfer in the controller's cache,
989 * if we receive a XferNotReady(DATA) we will ignore it, unless
990 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300991 *
Felipe Balbia7e8d652012-07-19 09:26:59 +0300992 * In that case, we must issue END_TRANSFER command to the Data
993 * Phase we already have started and issue SetStall on the
994 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300995 */
996 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbia7e8d652012-07-19 09:26:59 +0300997 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
998
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300999 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
Felipe Balbia7e8d652012-07-19 09:26:59 +03001000 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001001 dwc3_ep0_stall_and_restart(dwc);
1002 return;
1003 }
1004
Felipe Balbi72246da2011-08-19 18:10:58 +03001005 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001006
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001007 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbicb84c5e2012-07-23 09:09:32 +03001008 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1009 return;
1010
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001011 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001012
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001013 dwc->ep0state = EP0_STATUS_PHASE;
1014
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001015 if (dwc->delayed_status) {
1016 WARN_ON_ONCE(event->endpoint_number != 1);
1017 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
1018 return;
1019 }
1020
Felipe Balbi9610cd22012-05-21 14:22:41 +03001021 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001022 }
1023}
1024
1025void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001026 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001027{
1028 u8 epnum = event->endpoint_number;
1029
1030 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1031 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001032 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001033 dwc3_ep0_state_string(dwc->ep0state));
1034
1035 switch (event->endpoint_event) {
1036 case DWC3_DEPEVT_XFERCOMPLETE:
1037 dwc3_ep0_xfer_complete(dwc, event);
1038 break;
1039
1040 case DWC3_DEPEVT_XFERNOTREADY:
1041 dwc3_ep0_xfernotready(dwc, event);
1042 break;
1043
1044 case DWC3_DEPEVT_XFERINPROGRESS:
1045 case DWC3_DEPEVT_RXTXFIFOEVT:
1046 case DWC3_DEPEVT_STREAMEVT:
1047 case DWC3_DEPEVT_EPCMDCMPLT:
1048 break;
1049 }
1050}