blob: e9d099cee6361053fed1bcc1d0d046352afb9c95 [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 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/list.h>
27#include <linux/dma-mapping.h>
28
29#include <linux/usb/ch9.h>
30#include <linux/usb/gadget.h>
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010031#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030032
33#include "core.h"
34#include "gadget.h"
35#include "io.h"
36
Felipe Balbi788a23f2012-05-21 14:22:41 +030037static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbia0807882012-05-04 13:03:54 +030038static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
39 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010040
Felipe Balbi72246da2011-08-19 18:10:58 +030041static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
42{
43 switch (state) {
44 case EP0_UNCONNECTED:
45 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030046 case EP0_SETUP_PHASE:
47 return "Setup Phase";
48 case EP0_DATA_PHASE:
49 return "Data Phase";
50 case EP0_STATUS_PHASE:
51 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030052 default:
53 return "UNKNOWN";
54 }
55}
56
57static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030058 u32 len, u32 type)
Felipe Balbi72246da2011-08-19 18:10:58 +030059{
60 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020061 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030062 struct dwc3_ep *dep;
63
64 int ret;
65
66 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030067 if (dep->flags & DWC3_EP_BUSY) {
68 dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
69 return 0;
70 }
Felipe Balbi72246da2011-08-19 18:10:58 +030071
Felipe Balbif6bafc62012-02-06 11:04:53 +020072 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030073
Felipe Balbif6bafc62012-02-06 11:04:53 +020074 trb->bpl = lower_32_bits(buf_dma);
75 trb->bph = upper_32_bits(buf_dma);
76 trb->size = len;
77 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030078
Felipe Balbif6bafc62012-02-06 11:04:53 +020079 trb->ctrl |= (DWC3_TRB_CTRL_HWO
80 | DWC3_TRB_CTRL_LST
81 | DWC3_TRB_CTRL_IOC
82 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030083
84 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030085 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
86 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030087
88 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
89 DWC3_DEPCMD_STARTTRANSFER, &params);
90 if (ret < 0) {
91 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
92 return ret;
93 }
94
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030095 dep->flags |= DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +030096 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +030097 dep->number);
98
Felipe Balbi1ddcb212011-08-30 15:52:17 +030099 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
100
Felipe Balbi72246da2011-08-19 18:10:58 +0300101 return 0;
102}
103
104static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
105 struct dwc3_request *req)
106{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100107 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300108
109 req->request.actual = 0;
110 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300111 req->epnum = dep->number;
112
113 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300114
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300115 /*
116 * Gadget driver might not be quick enough to queue a request
117 * before we get a Transfer Not Ready event on this endpoint.
118 *
119 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
120 * flag is set, it's telling us that as soon as Gadget queues the
121 * required request, we should kick the transfer here because the
122 * IRQ we were waiting for is long gone.
123 */
124 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300125 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300126
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300127 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300128
Felipe Balbi68d8a782011-12-29 06:32:29 +0200129 if (dwc->ep0state != EP0_DATA_PHASE) {
130 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300131 return 0;
132 }
Felipe Balbia6829702011-08-27 22:18:09 +0300133
Felipe Balbia0807882012-05-04 13:03:54 +0300134 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
135
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300136 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
137 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300138
139 return 0;
140 }
141
142 /*
143 * In case gadget driver asked us to delay the STATUS phase,
144 * handle it here.
145 */
146 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300147 unsigned direction;
148
149 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100150 dwc->delayed_status = false;
Felipe Balbi68d3e662011-12-08 13:56:27 +0200151
152 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300153 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200154 else
155 dev_dbg(dwc->dev, "too early for delayed status\n");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300156
157 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300158 }
159
Felipe Balbifca8892a2012-07-19 09:05:35 +0300160 /*
161 * Unfortunately we have uncovered a limitation wrt the Data Phase.
162 *
163 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
164 * come before issueing Start Transfer command, but if we do, we will
165 * miss situations where the host starts another SETUP phase instead of
166 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
167 * Layer Compliance Suite.
168 *
169 * The problem surfaces due to the fact that in case of back-to-back
170 * SETUP packets there will be no XferNotReady(DATA) generated and we
171 * will be stuck waiting for XferNotReady(DATA) forever.
172 *
173 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
174 * it tells us to start Data Phase right away. It also mentions that if
175 * we receive a SETUP phase instead of the DATA phase, core will issue
176 * XferComplete for the DATA phase, before actually initiating it in
177 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
178 * can only be used to print some debugging logs, as the core expects
179 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
180 * just so it completes right away, without transferring anything and,
181 * only then, we can go back to the SETUP phase.
182 *
183 * Because of this scenario, SNPS decided to change the programming
184 * model of control transfers and support on-demand transfers only for
185 * the STATUS phase. To fix the issue we have now, we will always wait
186 * for gadget driver to queue the DATA phase's struct usb_request, then
187 * start it right away.
188 *
189 * If we're actually in a 2-stage transfer, we will wait for
190 * XferNotReady(STATUS).
191 */
192 if (dwc->three_stage_setup) {
193 unsigned direction;
194
195 direction = dwc->ep0_expect_in;
196 dwc->ep0state = EP0_DATA_PHASE;
197
198 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
199
200 dep->flags &= ~DWC3_EP0_DIR_IN;
201 }
202
Felipe Balbi35f75692012-07-19 08:49:01 +0300203 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300204}
205
206int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
207 gfp_t gfp_flags)
208{
209 struct dwc3_request *req = to_dwc3_request(request);
210 struct dwc3_ep *dep = to_dwc3_ep(ep);
211 struct dwc3 *dwc = dep->dwc;
212
213 unsigned long flags;
214
215 int ret;
216
Felipe Balbi72246da2011-08-19 18:10:58 +0300217 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200218 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300219 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
220 request, dep->name);
221 ret = -ESHUTDOWN;
222 goto out;
223 }
224
225 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200226 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 ret = -EBUSY;
228 goto out;
229 }
230
231 dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
232 request, dep->name, request->length,
233 dwc3_ep0_state_string(dwc->ep0state));
234
235 ret = __dwc3_gadget_ep0_queue(dep, req);
236
237out:
238 spin_unlock_irqrestore(&dwc->lock, flags);
239
240 return ret;
241}
242
243static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
244{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300245 struct dwc3_ep *dep;
246
247 /* reinitialize physical ep1 */
248 dep = dwc->eps[1];
249 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300250
Felipe Balbi72246da2011-08-19 18:10:58 +0300251 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300252 dep = dwc->eps[0];
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200253 __dwc3_gadget_ep_set_halt(dep, 1);
254 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100255 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300256
257 if (!list_empty(&dep->request_list)) {
258 struct dwc3_request *req;
259
260 req = next_request(&dep->request_list);
261 dwc3_gadget_giveback(dep, req, -ECONNRESET);
262 }
263
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300264 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300265 dwc3_ep0_out_start(dwc);
266}
267
Pratyush Anand08f0d962012-06-25 22:40:43 +0530268int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
269{
270 struct dwc3_ep *dep = to_dwc3_ep(ep);
271 struct dwc3 *dwc = dep->dwc;
272
273 dwc3_ep0_stall_and_restart(dwc);
274
275 return 0;
276}
277
Felipe Balbi72246da2011-08-19 18:10:58 +0300278void dwc3_ep0_out_start(struct dwc3 *dwc)
279{
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 int ret;
281
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300282 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
283 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300284 WARN_ON(ret < 0);
285}
286
Felipe Balbi72246da2011-08-19 18:10:58 +0300287static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
288{
289 struct dwc3_ep *dep;
290 u32 windex = le16_to_cpu(wIndex_le);
291 u32 epnum;
292
293 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
294 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
295 epnum |= 1;
296
297 dep = dwc->eps[epnum];
298 if (dep->flags & DWC3_EP_ENABLED)
299 return dep;
300
301 return NULL;
302}
303
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200304static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300305{
Felipe Balbi72246da2011-08-19 18:10:58 +0300306}
Felipe Balbi72246da2011-08-19 18:10:58 +0300307/*
308 * ch 9.4.5
309 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200310static int dwc3_ep0_handle_status(struct dwc3 *dwc,
311 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300312{
313 struct dwc3_ep *dep;
314 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200315 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300316 u16 usb_status = 0;
317 __le16 *response_pkt;
318
319 recip = ctrl->bRequestType & USB_RECIP_MASK;
320 switch (recip) {
321 case USB_RECIP_DEVICE:
322 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200323 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300324 */
325 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200326
327 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
328 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
329 if (reg & DWC3_DCTL_INITU1ENA)
330 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
331 if (reg & DWC3_DCTL_INITU2ENA)
332 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
333 }
334
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 break;
336
337 case USB_RECIP_INTERFACE:
338 /*
339 * Function Remote Wake Capable D0
340 * Function Remote Wakeup D1
341 */
342 break;
343
344 case USB_RECIP_ENDPOINT:
345 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
346 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200347 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300348
349 if (dep->flags & DWC3_EP_STALL)
350 usb_status = 1 << USB_ENDPOINT_HALT;
351 break;
352 default:
353 return -EINVAL;
354 };
355
356 response_pkt = (__le16 *) dwc->setup_buf;
357 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200358
359 dep = dwc->eps[0];
360 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100361 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200362 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100363 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200364
365 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366}
367
368static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
369 struct usb_ctrlrequest *ctrl, int set)
370{
371 struct dwc3_ep *dep;
372 u32 recip;
373 u32 wValue;
374 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200375 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376 int ret;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200377 enum usb_device_state state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300378
379 wValue = le16_to_cpu(ctrl->wValue);
380 wIndex = le16_to_cpu(ctrl->wIndex);
381 recip = ctrl->bRequestType & USB_RECIP_MASK;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200382 state = dwc->gadget.state;
383
Felipe Balbi72246da2011-08-19 18:10:58 +0300384 switch (recip) {
385 case USB_RECIP_DEVICE:
386
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200387 switch (wValue) {
388 case USB_DEVICE_REMOTE_WAKEUP:
389 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 /*
391 * 9.4.1 says only only for SS, in AddressState only for
392 * default control pipe
393 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300394 case USB_DEVICE_U1_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200395 if (state != USB_STATE_CONFIGURED)
Felipe Balbi72246da2011-08-19 18:10:58 +0300396 return -EINVAL;
397 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
398 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300399
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200400 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
401 if (set)
402 reg |= DWC3_DCTL_INITU1ENA;
403 else
404 reg &= ~DWC3_DCTL_INITU1ENA;
405 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200407
Felipe Balbi72246da2011-08-19 18:10:58 +0300408 case USB_DEVICE_U2_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200409 if (state != USB_STATE_CONFIGURED)
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200410 return -EINVAL;
411 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
412 return -EINVAL;
413
414 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
415 if (set)
416 reg |= DWC3_DCTL_INITU2ENA;
417 else
418 reg &= ~DWC3_DCTL_INITU2ENA;
419 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300420 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200421
Felipe Balbi72246da2011-08-19 18:10:58 +0300422 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200423 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 break;
425
426 case USB_DEVICE_TEST_MODE:
427 if ((wIndex & 0xff) != 0)
428 return -EINVAL;
429 if (!set)
430 return -EINVAL;
431
Gerard Cauvy3b637362012-02-10 12:21:18 +0200432 dwc->test_mode_nr = wIndex >> 8;
433 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200434 break;
435 default:
436 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 }
438 break;
439
440 case USB_RECIP_INTERFACE:
441 switch (wValue) {
442 case USB_INTRF_FUNC_SUSPEND:
443 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
444 /* XXX enable Low power suspend */
445 ;
446 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
447 /* XXX enable remote wakeup */
448 ;
449 break;
450 default:
451 return -EINVAL;
452 }
453 break;
454
455 case USB_RECIP_ENDPOINT:
456 switch (wValue) {
457 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800458 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300459 if (!dep)
460 return -EINVAL;
461 ret = __dwc3_gadget_ep_set_halt(dep, set);
462 if (ret)
463 return -EINVAL;
464 break;
465 default:
466 return -EINVAL;
467 }
468 break;
469
470 default:
471 return -EINVAL;
472 };
473
Felipe Balbi72246da2011-08-19 18:10:58 +0300474 return 0;
475}
476
477static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
478{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200479 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 u32 addr;
481 u32 reg;
482
483 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300484 if (addr > 127) {
485 dev_dbg(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300486 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300487 }
488
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200489 if (state == USB_STATE_CONFIGURED) {
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300490 dev_dbg(dwc->dev, "trying to set address when configured\n");
491 return -EINVAL;
492 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300493
Felipe Balbi26460212011-09-30 10:58:36 +0300494 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
495 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
496 reg |= DWC3_DCFG_DEVADDR(addr);
497 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300498
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200499 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200500 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200501 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200502 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
Felipe Balbi26460212011-09-30 10:58:36 +0300504 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300505}
506
507static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
508{
509 int ret;
510
511 spin_unlock(&dwc->lock);
512 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
513 spin_lock(&dwc->lock);
514 return ret;
515}
516
517static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
518{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200519 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 u32 cfg;
521 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530522 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300524 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525 cfg = le16_to_cpu(ctrl->wValue);
526
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200527 switch (state) {
528 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300529 return -EINVAL;
530 break;
531
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200532 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300533 ret = dwc3_ep0_delegate_req(dwc, ctrl);
534 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200535 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi14cd5922011-12-19 13:01:52 +0200536 usb_gadget_set_state(&dwc->gadget,
537 USB_STATE_CONFIGURED);
538
Pratyush Anande274a312012-07-02 10:21:54 +0530539 /*
540 * Enable transition to U1/U2 state when
541 * nothing is pending from application.
542 */
543 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
544 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
545 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
546
Felipe Balbi457e84b2012-01-18 18:04:09 +0200547 dwc->resize_fifos = true;
548 dev_dbg(dwc->dev, "resize fifos flag SET\n");
549 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 break;
551
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200552 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200554 if (!cfg)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200555 usb_gadget_set_state(&dwc->gadget,
556 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300557 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100558 default:
559 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300560 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100561 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562}
563
Felipe Balbi865e09e2012-04-24 16:19:49 +0300564static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
565{
566 struct dwc3_ep *dep = to_dwc3_ep(ep);
567 struct dwc3 *dwc = dep->dwc;
568
569 u32 param = 0;
570 u32 reg;
571
572 struct timing {
573 u8 u1sel;
574 u8 u1pel;
575 u16 u2sel;
576 u16 u2pel;
577 } __packed timing;
578
579 int ret;
580
581 memcpy(&timing, req->buf, sizeof(timing));
582
583 dwc->u1sel = timing.u1sel;
584 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300585 dwc->u2sel = le16_to_cpu(timing.u2sel);
586 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300587
588 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
589 if (reg & DWC3_DCTL_INITU2ENA)
590 param = dwc->u2pel;
591 if (reg & DWC3_DCTL_INITU1ENA)
592 param = dwc->u1pel;
593
594 /*
595 * According to Synopsys Databook, if parameter is
596 * greater than 125, a value of zero should be
597 * programmed in the register.
598 */
599 if (param > 125)
600 param = 0;
601
602 /* now that we have the time, issue DGCMD Set Sel */
603 ret = dwc3_send_gadget_generic_command(dwc,
604 DWC3_DGCMD_SET_PERIODIC_PAR, param);
605 WARN_ON(ret < 0);
606}
607
608static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
609{
610 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200611 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300612 u16 wLength;
613 u16 wValue;
614
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200615 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300616 return -EINVAL;
617
618 wValue = le16_to_cpu(ctrl->wValue);
619 wLength = le16_to_cpu(ctrl->wLength);
620
621 if (wLength != 6) {
622 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
623 wLength);
624 return -EINVAL;
625 }
626
627 /*
628 * To handle Set SEL we need to receive 6 bytes from Host. So let's
629 * queue a usb_request for 6 bytes.
630 *
631 * Remember, though, this controller can't handle non-wMaxPacketSize
632 * aligned transfers on the OUT direction, so we queue a request for
633 * wMaxPacketSize instead.
634 */
635 dep = dwc->eps[0];
636 dwc->ep0_usb_req.dep = dep;
637 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
638 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
639 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
640
641 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
642}
643
Felipe Balbic12a0d82012-04-25 10:45:05 +0300644static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
645{
646 u16 wLength;
647 u16 wValue;
648 u16 wIndex;
649
650 wValue = le16_to_cpu(ctrl->wValue);
651 wLength = le16_to_cpu(ctrl->wLength);
652 wIndex = le16_to_cpu(ctrl->wIndex);
653
654 if (wIndex || wLength)
655 return -EINVAL;
656
657 /*
658 * REVISIT It's unclear from Databook what to do with this
659 * value. For now, just cache it.
660 */
661 dwc->isoch_delay = wValue;
662
663 return 0;
664}
665
Felipe Balbi72246da2011-08-19 18:10:58 +0300666static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
667{
668 int ret;
669
670 switch (ctrl->bRequest) {
671 case USB_REQ_GET_STATUS:
672 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
673 ret = dwc3_ep0_handle_status(dwc, ctrl);
674 break;
675 case USB_REQ_CLEAR_FEATURE:
676 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
677 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
678 break;
679 case USB_REQ_SET_FEATURE:
680 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
681 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
682 break;
683 case USB_REQ_SET_ADDRESS:
684 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
685 ret = dwc3_ep0_set_address(dwc, ctrl);
686 break;
687 case USB_REQ_SET_CONFIGURATION:
688 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
689 ret = dwc3_ep0_set_config(dwc, ctrl);
690 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300691 case USB_REQ_SET_SEL:
692 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
693 ret = dwc3_ep0_set_sel(dwc, ctrl);
694 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300695 case USB_REQ_SET_ISOCH_DELAY:
696 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
697 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
698 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300699 default:
700 dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
701 ret = dwc3_ep0_delegate_req(dwc, ctrl);
702 break;
703 };
704
705 return ret;
706}
707
708static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
709 const struct dwc3_event_depevt *event)
710{
711 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300712 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300713 u32 len;
714
715 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300716 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300717
718 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300719 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300720 dwc->three_stage_setup = false;
721 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300722 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
723 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300724 dwc->three_stage_setup = true;
725 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300726 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
727 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300728
729 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
730 ret = dwc3_ep0_std_request(dwc, ctrl);
731 else
732 ret = dwc3_ep0_delegate_req(dwc, ctrl);
733
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100734 if (ret == USB_GADGET_DELAYED_STATUS)
735 dwc->delayed_status = true;
736
Felipe Balbief21ede2012-05-31 10:29:49 +0300737out:
738 if (ret < 0)
739 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300740}
741
742static void dwc3_ep0_complete_data(struct dwc3 *dwc,
743 const struct dwc3_event_depevt *event)
744{
745 struct dwc3_request *r = NULL;
746 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200747 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200748 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300749 u32 transferred;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300750 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200751 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 u8 epnum;
753
754 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200755 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300756
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300757 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
758
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200759 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200760 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300761
Felipe Balbif6bafc62012-02-06 11:04:53 +0200762 trb = dwc->ep0_trb;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300763
764 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
765 if (status == DWC3_TRBSTS_SETUP_PENDING) {
766 dev_dbg(dwc->dev, "Setup Pending received\n");
767
768 if (r)
769 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
770
771 return;
772 }
773
Felipe Balbif6bafc62012-02-06 11:04:53 +0200774 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300775
Felipe Balbia6829702011-08-27 22:18:09 +0300776 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500777 unsigned transfer_size = ur->length;
778 unsigned maxp = ep0->endpoint.maxpacket;
779
780 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300781 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500782 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300783 memcpy(ur->buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300784 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200785 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300786 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300787
Felipe Balbicd423dd2012-03-21 11:44:00 +0200788 ur->actual += transferred;
789
Felipe Balbi72246da2011-08-19 18:10:58 +0300790 if ((epnum & 1) && ur->actual < ur->length) {
791 /* for some reason we did not get everything out */
792
793 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300794 } else {
795 /*
796 * handle the case where we have to send a zero packet. This
797 * seems to be case when req.length > maxpacket. Could it be?
798 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300799 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200800 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300801 }
802}
803
Felipe Balbi85a78102012-05-31 12:32:37 +0300804static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 const struct dwc3_event_depevt *event)
806{
807 struct dwc3_request *r;
808 struct dwc3_ep *dep;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300809 struct dwc3_trb *trb;
810 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300811
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300812 dep = dwc->eps[0];
Felipe Balbifca8892a2012-07-19 09:05:35 +0300813 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300814
815 if (!list_empty(&dep->request_list)) {
816 r = next_request(&dep->request_list);
817
818 dwc3_gadget_giveback(dep, r, 0);
819 }
820
Gerard Cauvy3b637362012-02-10 12:21:18 +0200821 if (dwc->test_mode) {
822 int ret;
823
824 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
825 if (ret < 0) {
826 dev_dbg(dwc->dev, "Invalid Test #%d\n",
827 dwc->test_mode_nr);
828 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300829 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200830 }
831 }
832
Felipe Balbifca8892a2012-07-19 09:05:35 +0300833 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
834 if (status == DWC3_TRBSTS_SETUP_PENDING)
835 dev_dbg(dwc->dev, "Setup Pending received\n");
836
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300837 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300838 dwc3_ep0_out_start(dwc);
839}
840
841static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
842 const struct dwc3_event_depevt *event)
843{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300844 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
845
846 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300847 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300848 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300849
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300851 case EP0_SETUP_PHASE:
852 dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300853 dwc3_ep0_inspect_setup(dwc, event);
854 break;
855
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300856 case EP0_DATA_PHASE:
857 dev_vdbg(dwc->dev, "Data Phase\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300858 dwc3_ep0_complete_data(dwc, event);
859 break;
860
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300861 case EP0_STATUS_PHASE:
862 dev_vdbg(dwc->dev, "Status Phase\n");
Felipe Balbi85a78102012-05-31 12:32:37 +0300863 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300864 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300865 default:
866 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300867 }
868}
869
Felipe Balbia0807882012-05-04 13:03:54 +0300870static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
871 struct dwc3_ep *dep, struct dwc3_request *req)
872{
873 int ret;
874
875 req->direction = !!dep->number;
876
877 if (req->request.length == 0) {
878 ret = dwc3_ep0_start_trans(dwc, dep->number,
879 dwc->ctrl_req_addr, 0,
880 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300881 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300882 && (dep->number == 0)) {
Andrew Mortonc390b032013-03-08 09:42:50 +0200883 u32 transfer_size;
884 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +0300885
886 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
887 dep->number);
888 if (ret) {
889 dev_dbg(dwc->dev, "failed to map request\n");
890 return;
891 }
892
893 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
894
Andrew Mortonc390b032013-03-08 09:42:50 +0200895 maxpacket = dep->endpoint.maxpacket;
896 transfer_size = roundup(req->request.length, maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +0300897
898 dwc->ep0_bounced = true;
899
900 /*
901 * REVISIT in case request length is bigger than
902 * DWC3_EP0_BOUNCE_SIZE we will need two chained
903 * TRBs to handle the transfer.
904 */
905 ret = dwc3_ep0_start_trans(dwc, dep->number,
906 dwc->ep0_bounce_addr, transfer_size,
907 DWC3_TRBCTL_CONTROL_DATA);
908 } else {
909 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
910 dep->number);
911 if (ret) {
912 dev_dbg(dwc->dev, "failed to map request\n");
913 return;
914 }
915
916 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
917 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
918 }
919
920 WARN_ON(ret < 0);
921}
922
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100923static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300924{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100925 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300926 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300927
928 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
929 : DWC3_TRBCTL_CONTROL_STATUS2;
930
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100931 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300932 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100933}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300934
Felipe Balbi788a23f2012-05-21 14:22:41 +0300935static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100936{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200937 if (dwc->resize_fifos) {
938 dev_dbg(dwc->dev, "starting to resize fifos\n");
939 dwc3_gadget_resize_tx_fifos(dwc);
940 dwc->resize_fifos = 0;
941 }
942
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100943 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300944}
945
Felipe Balbi788a23f2012-05-21 14:22:41 +0300946static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
947 const struct dwc3_event_depevt *event)
948{
949 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
950
951 __dwc3_ep0_do_control_status(dwc, dep);
952}
953
Felipe Balbi2e3db062012-07-19 09:26:59 +0300954static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
955{
956 struct dwc3_gadget_ep_cmd_params params;
957 u32 cmd;
958 int ret;
959
960 if (!dep->resource_index)
961 return;
962
963 cmd = DWC3_DEPCMD_ENDTRANSFER;
964 cmd |= DWC3_DEPCMD_CMDIOC;
965 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
966 memset(&params, 0, sizeof(params));
967 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
968 WARN_ON_ONCE(ret);
969 dep->resource_index = 0;
970}
971
Felipe Balbi72246da2011-08-19 18:10:58 +0300972static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
973 const struct dwc3_event_depevt *event)
974{
Felipe Balbidf62df52011-10-14 15:11:49 +0300975 dwc->setup_packet_pending = true;
976
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300977 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300978 case DEPEVT_STATUS_CONTROL_DATA:
979 dev_vdbg(dwc->dev, "Control Data\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300980
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300981 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +0300982 * We already have a DATA transfer in the controller's cache,
983 * if we receive a XferNotReady(DATA) we will ignore it, unless
984 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300985 *
Felipe Balbi2e3db062012-07-19 09:26:59 +0300986 * In that case, we must issue END_TRANSFER command to the Data
987 * Phase we already have started and issue SetStall on the
988 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300989 */
990 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +0300991 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
992
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300993 dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
Felipe Balbi2e3db062012-07-19 09:26:59 +0300994 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +0300995 dwc3_ep0_stall_and_restart(dwc);
996 return;
997 }
998
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001000
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001001 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001002 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1003 return;
1004
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001005 dev_vdbg(dwc->dev, "Control Status\n");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001006
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001007 dwc->ep0state = EP0_STATUS_PHASE;
1008
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001009 if (dwc->delayed_status) {
1010 WARN_ON_ONCE(event->endpoint_number != 1);
1011 dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
1012 return;
1013 }
1014
Felipe Balbi788a23f2012-05-21 14:22:41 +03001015 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 }
1017}
1018
1019void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001020 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001021{
1022 u8 epnum = event->endpoint_number;
1023
1024 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
1025 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001026 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001027 dwc3_ep0_state_string(dwc->ep0state));
1028
1029 switch (event->endpoint_event) {
1030 case DWC3_DEPEVT_XFERCOMPLETE:
1031 dwc3_ep0_xfer_complete(dwc, event);
1032 break;
1033
1034 case DWC3_DEPEVT_XFERNOTREADY:
1035 dwc3_ep0_xfernotready(dwc, event);
1036 break;
1037
1038 case DWC3_DEPEVT_XFERINPROGRESS:
1039 case DWC3_DEPEVT_RXTXFIFOEVT:
1040 case DWC3_DEPEVT_STREAMEVT:
1041 case DWC3_DEPEVT_EPCMDCMPLT:
1042 break;
1043 }
1044}