blob: b35938777ddeaa03954bb567fb1fb0cf096b5982 [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"
Felipe Balbi80977dc2014-08-19 16:37:22 -050034#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030035#include "gadget.h"
36#include "io.h"
37
Felipe Balbi788a23f2012-05-21 14:22:41 +030038static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbia0807882012-05-04 13:03:54 +030039static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
40 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010041
Felipe Balbi72246da2011-08-19 18:10:58 +030042static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
43{
44 switch (state) {
45 case EP0_UNCONNECTED:
46 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030047 case EP0_SETUP_PHASE:
48 return "Setup Phase";
49 case EP0_DATA_PHASE:
50 return "Data Phase";
51 case EP0_STATUS_PHASE:
52 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030053 default:
54 return "UNKNOWN";
55 }
56}
57
58static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030059 u32 len, u32 type)
Felipe Balbi72246da2011-08-19 18:10:58 +030060{
61 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020062 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030063 struct dwc3_ep *dep;
64
65 int ret;
66
67 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030068 if (dep->flags & DWC3_EP_BUSY) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -050069 dwc3_trace(trace_dwc3_ep0, "%s still busy", dep->name);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030070 return 0;
71 }
Felipe Balbi72246da2011-08-19 18:10:58 +030072
Felipe Balbif6bafc62012-02-06 11:04:53 +020073 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030074
Felipe Balbif6bafc62012-02-06 11:04:53 +020075 trb->bpl = lower_32_bits(buf_dma);
76 trb->bph = upper_32_bits(buf_dma);
77 trb->size = len;
78 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030079
Felipe Balbif6bafc62012-02-06 11:04:53 +020080 trb->ctrl |= (DWC3_TRB_CTRL_HWO
81 | DWC3_TRB_CTRL_LST
82 | DWC3_TRB_CTRL_IOC
83 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030084
85 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030086 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
87 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030088
89 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
90 DWC3_DEPCMD_STARTTRANSFER, &params);
91 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -050092 dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed",
93 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +030094 return ret;
95 }
96
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030097 dep->flags |= DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +030098 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +030099 dep->number);
100
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300101 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
102
Felipe Balbi72246da2011-08-19 18:10:58 +0300103 return 0;
104}
105
106static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
107 struct dwc3_request *req)
108{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100109 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300110
111 req->request.actual = 0;
112 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300113 req->epnum = dep->number;
114
115 list_add_tail(&req->list, &dep->request_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300116
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300117 /*
118 * Gadget driver might not be quick enough to queue a request
119 * before we get a Transfer Not Ready event on this endpoint.
120 *
121 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
122 * flag is set, it's telling us that as soon as Gadget queues the
123 * required request, we should kick the transfer here because the
124 * IRQ we were waiting for is long gone.
125 */
126 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300127 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300128
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300129 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300130
Felipe Balbi68d8a782011-12-29 06:32:29 +0200131 if (dwc->ep0state != EP0_DATA_PHASE) {
132 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300133 return 0;
134 }
Felipe Balbia6829702011-08-27 22:18:09 +0300135
Felipe Balbia0807882012-05-04 13:03:54 +0300136 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
137
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300138 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
139 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300140
141 return 0;
142 }
143
144 /*
145 * In case gadget driver asked us to delay the STATUS phase,
146 * handle it here.
147 */
148 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300149 unsigned direction;
150
151 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100152 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300153 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200154
155 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300156 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200157 else
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500158 dwc3_trace(trace_dwc3_ep0,
159 "too early for delayed status");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300160
161 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300162 }
163
Felipe Balbifca8892a2012-07-19 09:05:35 +0300164 /*
165 * Unfortunately we have uncovered a limitation wrt the Data Phase.
166 *
167 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
168 * come before issueing Start Transfer command, but if we do, we will
169 * miss situations where the host starts another SETUP phase instead of
170 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
171 * Layer Compliance Suite.
172 *
173 * The problem surfaces due to the fact that in case of back-to-back
174 * SETUP packets there will be no XferNotReady(DATA) generated and we
175 * will be stuck waiting for XferNotReady(DATA) forever.
176 *
177 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
178 * it tells us to start Data Phase right away. It also mentions that if
179 * we receive a SETUP phase instead of the DATA phase, core will issue
180 * XferComplete for the DATA phase, before actually initiating it in
181 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
182 * can only be used to print some debugging logs, as the core expects
183 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
184 * just so it completes right away, without transferring anything and,
185 * only then, we can go back to the SETUP phase.
186 *
187 * Because of this scenario, SNPS decided to change the programming
188 * model of control transfers and support on-demand transfers only for
189 * the STATUS phase. To fix the issue we have now, we will always wait
190 * for gadget driver to queue the DATA phase's struct usb_request, then
191 * start it right away.
192 *
193 * If we're actually in a 2-stage transfer, we will wait for
194 * XferNotReady(STATUS).
195 */
196 if (dwc->three_stage_setup) {
197 unsigned direction;
198
199 direction = dwc->ep0_expect_in;
200 dwc->ep0state = EP0_DATA_PHASE;
201
202 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
203
204 dep->flags &= ~DWC3_EP0_DIR_IN;
205 }
206
Felipe Balbi35f75692012-07-19 08:49:01 +0300207 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300208}
209
210int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
211 gfp_t gfp_flags)
212{
213 struct dwc3_request *req = to_dwc3_request(request);
214 struct dwc3_ep *dep = to_dwc3_ep(ep);
215 struct dwc3 *dwc = dep->dwc;
216
217 unsigned long flags;
218
219 int ret;
220
Felipe Balbi72246da2011-08-19 18:10:58 +0300221 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200222 if (!dep->endpoint.desc) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500223 dwc3_trace(trace_dwc3_ep0,
224 "trying to queue request %p to disabled %s",
Felipe Balbi72246da2011-08-19 18:10:58 +0300225 request, dep->name);
226 ret = -ESHUTDOWN;
227 goto out;
228 }
229
230 /* we share one TRB for ep0/1 */
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200231 if (!list_empty(&dep->request_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300232 ret = -EBUSY;
233 goto out;
234 }
235
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500236 dwc3_trace(trace_dwc3_ep0,
237 "queueing request %p to %s length %d state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +0300238 request, dep->name, request->length,
239 dwc3_ep0_state_string(dwc->ep0state));
240
241 ret = __dwc3_gadget_ep0_queue(dep, req);
242
243out:
244 spin_unlock_irqrestore(&dwc->lock, flags);
245
246 return ret;
247}
248
249static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
250{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300251 struct dwc3_ep *dep;
252
253 /* reinitialize physical ep1 */
254 dep = dwc->eps[1];
255 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300256
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300258 dep = dwc->eps[0];
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200259 __dwc3_gadget_ep_set_halt(dep, 1);
260 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100261 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300262
263 if (!list_empty(&dep->request_list)) {
264 struct dwc3_request *req;
265
266 req = next_request(&dep->request_list);
267 dwc3_gadget_giveback(dep, req, -ECONNRESET);
268 }
269
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300270 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300271 dwc3_ep0_out_start(dwc);
272}
273
Pratyush Anand08f0d962012-06-25 22:40:43 +0530274int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
275{
276 struct dwc3_ep *dep = to_dwc3_ep(ep);
277 struct dwc3 *dwc = dep->dwc;
278
279 dwc3_ep0_stall_and_restart(dwc);
280
281 return 0;
282}
283
Felipe Balbi72246da2011-08-19 18:10:58 +0300284void dwc3_ep0_out_start(struct dwc3 *dwc)
285{
Felipe Balbi72246da2011-08-19 18:10:58 +0300286 int ret;
287
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300288 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
289 DWC3_TRBCTL_CONTROL_SETUP);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290 WARN_ON(ret < 0);
291}
292
Felipe Balbi72246da2011-08-19 18:10:58 +0300293static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
294{
295 struct dwc3_ep *dep;
296 u32 windex = le16_to_cpu(wIndex_le);
297 u32 epnum;
298
299 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
300 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
301 epnum |= 1;
302
303 dep = dwc->eps[epnum];
304 if (dep->flags & DWC3_EP_ENABLED)
305 return dep;
306
307 return NULL;
308}
309
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200310static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300311{
Felipe Balbi72246da2011-08-19 18:10:58 +0300312}
Felipe Balbi72246da2011-08-19 18:10:58 +0300313/*
314 * ch 9.4.5
315 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200316static int dwc3_ep0_handle_status(struct dwc3 *dwc,
317 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300318{
319 struct dwc3_ep *dep;
320 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200321 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300322 u16 usb_status = 0;
323 __le16 *response_pkt;
324
325 recip = ctrl->bRequestType & USB_RECIP_MASK;
326 switch (recip) {
327 case USB_RECIP_DEVICE:
328 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200329 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300330 */
331 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200332
333 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
334 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
335 if (reg & DWC3_DCTL_INITU1ENA)
336 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
337 if (reg & DWC3_DCTL_INITU2ENA)
338 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
339 }
340
Felipe Balbi72246da2011-08-19 18:10:58 +0300341 break;
342
343 case USB_RECIP_INTERFACE:
344 /*
345 * Function Remote Wake Capable D0
346 * Function Remote Wakeup D1
347 */
348 break;
349
350 case USB_RECIP_ENDPOINT:
351 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
352 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200353 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300354
355 if (dep->flags & DWC3_EP_STALL)
356 usb_status = 1 << USB_ENDPOINT_HALT;
357 break;
358 default:
359 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700360 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300361
362 response_pkt = (__le16 *) dwc->setup_buf;
363 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200364
365 dep = dwc->eps[0];
366 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100367 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200368 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100369 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200370
371 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300372}
373
374static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
375 struct usb_ctrlrequest *ctrl, int set)
376{
377 struct dwc3_ep *dep;
378 u32 recip;
379 u32 wValue;
380 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200381 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300382 int ret;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200383 enum usb_device_state state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300384
385 wValue = le16_to_cpu(ctrl->wValue);
386 wIndex = le16_to_cpu(ctrl->wIndex);
387 recip = ctrl->bRequestType & USB_RECIP_MASK;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200388 state = dwc->gadget.state;
389
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 switch (recip) {
391 case USB_RECIP_DEVICE:
392
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200393 switch (wValue) {
394 case USB_DEVICE_REMOTE_WAKEUP:
395 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300396 /*
397 * 9.4.1 says only only for SS, in AddressState only for
398 * default control pipe
399 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300400 case USB_DEVICE_U1_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200401 if (state != USB_STATE_CONFIGURED)
Felipe Balbi72246da2011-08-19 18:10:58 +0300402 return -EINVAL;
403 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
404 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300405
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200406 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
407 if (set)
408 reg |= DWC3_DCTL_INITU1ENA;
409 else
410 reg &= ~DWC3_DCTL_INITU1ENA;
411 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200413
Felipe Balbi72246da2011-08-19 18:10:58 +0300414 case USB_DEVICE_U2_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200415 if (state != USB_STATE_CONFIGURED)
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200416 return -EINVAL;
417 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
418 return -EINVAL;
419
420 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
421 if (set)
422 reg |= DWC3_DCTL_INITU2ENA;
423 else
424 reg &= ~DWC3_DCTL_INITU2ENA;
425 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200427
Felipe Balbi72246da2011-08-19 18:10:58 +0300428 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200429 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 break;
431
432 case USB_DEVICE_TEST_MODE:
433 if ((wIndex & 0xff) != 0)
434 return -EINVAL;
435 if (!set)
436 return -EINVAL;
437
Gerard Cauvy3b637362012-02-10 12:21:18 +0200438 dwc->test_mode_nr = wIndex >> 8;
439 dwc->test_mode = true;
Gerard Cauvyecb07792012-03-16 16:20:10 +0200440 break;
441 default:
442 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300443 }
444 break;
445
446 case USB_RECIP_INTERFACE:
447 switch (wValue) {
448 case USB_INTRF_FUNC_SUSPEND:
449 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
450 /* XXX enable Low power suspend */
451 ;
452 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
453 /* XXX enable remote wakeup */
454 ;
455 break;
456 default:
457 return -EINVAL;
458 }
459 break;
460
461 case USB_RECIP_ENDPOINT:
462 switch (wValue) {
463 case USB_ENDPOINT_HALT:
Paul Zimmerman1d046792012-02-15 18:56:56 -0800464 dep = dwc3_wIndex_to_dep(dwc, wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 if (!dep)
466 return -EINVAL;
Alan Sterna535d812013-11-01 12:05:12 -0400467 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
468 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300469 ret = __dwc3_gadget_ep_set_halt(dep, set);
470 if (ret)
471 return -EINVAL;
472 break;
473 default:
474 return -EINVAL;
475 }
476 break;
477
478 default:
479 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700480 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300481
Felipe Balbi72246da2011-08-19 18:10:58 +0300482 return 0;
483}
484
485static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
486{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200487 enum usb_device_state state = dwc->gadget.state;
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) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500493 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300494 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300495 }
496
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200497 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500498 dwc3_trace(trace_dwc3_ep0,
499 "trying to set address when configured");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300500 return -EINVAL;
501 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300502
Felipe Balbi26460212011-09-30 10:58:36 +0300503 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
504 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
505 reg |= DWC3_DCFG_DEVADDR(addr);
506 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300507
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200508 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200509 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200510 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200511 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300512
Felipe Balbi26460212011-09-30 10:58:36 +0300513 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514}
515
516static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
517{
518 int ret;
519
520 spin_unlock(&dwc->lock);
521 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
522 spin_lock(&dwc->lock);
523 return ret;
524}
525
526static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
527{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200528 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300529 u32 cfg;
530 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530531 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300532
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300533 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300534 cfg = le16_to_cpu(ctrl->wValue);
535
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200536 switch (state) {
537 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300538 return -EINVAL;
539 break;
540
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200541 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300542 ret = dwc3_ep0_delegate_req(dwc, ctrl);
543 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200544 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300545
546 /*
547 * only change state if set_config has already
548 * been processed. If gadget driver returns
549 * USB_GADGET_DELAYED_STATUS, we will wait
550 * to change the state on the next usb_ep_queue()
551 */
552 if (ret == 0)
553 usb_gadget_set_state(&dwc->gadget,
554 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200555
Pratyush Anande274a312012-07-02 10:21:54 +0530556 /*
557 * Enable transition to U1/U2 state when
558 * nothing is pending from application.
559 */
560 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
561 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
562 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
563
Felipe Balbi457e84b2012-01-18 18:04:09 +0200564 dwc->resize_fifos = true;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500565 dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200566 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 break;
568
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200569 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300571 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200572 usb_gadget_set_state(&dwc->gadget,
573 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300574 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100575 default:
576 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100578 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579}
580
Felipe Balbi865e09e2012-04-24 16:19:49 +0300581static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
582{
583 struct dwc3_ep *dep = to_dwc3_ep(ep);
584 struct dwc3 *dwc = dep->dwc;
585
586 u32 param = 0;
587 u32 reg;
588
589 struct timing {
590 u8 u1sel;
591 u8 u1pel;
592 u16 u2sel;
593 u16 u2pel;
594 } __packed timing;
595
596 int ret;
597
598 memcpy(&timing, req->buf, sizeof(timing));
599
600 dwc->u1sel = timing.u1sel;
601 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300602 dwc->u2sel = le16_to_cpu(timing.u2sel);
603 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300604
605 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
606 if (reg & DWC3_DCTL_INITU2ENA)
607 param = dwc->u2pel;
608 if (reg & DWC3_DCTL_INITU1ENA)
609 param = dwc->u1pel;
610
611 /*
612 * According to Synopsys Databook, if parameter is
613 * greater than 125, a value of zero should be
614 * programmed in the register.
615 */
616 if (param > 125)
617 param = 0;
618
619 /* now that we have the time, issue DGCMD Set Sel */
620 ret = dwc3_send_gadget_generic_command(dwc,
621 DWC3_DGCMD_SET_PERIODIC_PAR, param);
622 WARN_ON(ret < 0);
623}
624
625static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
626{
627 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200628 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300629 u16 wLength;
630 u16 wValue;
631
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200632 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300633 return -EINVAL;
634
635 wValue = le16_to_cpu(ctrl->wValue);
636 wLength = le16_to_cpu(ctrl->wLength);
637
638 if (wLength != 6) {
639 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
640 wLength);
641 return -EINVAL;
642 }
643
644 /*
645 * To handle Set SEL we need to receive 6 bytes from Host. So let's
646 * queue a usb_request for 6 bytes.
647 *
648 * Remember, though, this controller can't handle non-wMaxPacketSize
649 * aligned transfers on the OUT direction, so we queue a request for
650 * wMaxPacketSize instead.
651 */
652 dep = dwc->eps[0];
653 dwc->ep0_usb_req.dep = dep;
654 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
655 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
656 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
657
658 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
659}
660
Felipe Balbic12a0d82012-04-25 10:45:05 +0300661static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
662{
663 u16 wLength;
664 u16 wValue;
665 u16 wIndex;
666
667 wValue = le16_to_cpu(ctrl->wValue);
668 wLength = le16_to_cpu(ctrl->wLength);
669 wIndex = le16_to_cpu(ctrl->wIndex);
670
671 if (wIndex || wLength)
672 return -EINVAL;
673
674 /*
675 * REVISIT It's unclear from Databook what to do with this
676 * value. For now, just cache it.
677 */
678 dwc->isoch_delay = wValue;
679
680 return 0;
681}
682
Felipe Balbi72246da2011-08-19 18:10:58 +0300683static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
684{
685 int ret;
686
687 switch (ctrl->bRequest) {
688 case USB_REQ_GET_STATUS:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500689 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300690 ret = dwc3_ep0_handle_status(dwc, ctrl);
691 break;
692 case USB_REQ_CLEAR_FEATURE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500693 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300694 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
695 break;
696 case USB_REQ_SET_FEATURE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500697 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300698 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
699 break;
700 case USB_REQ_SET_ADDRESS:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500701 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300702 ret = dwc3_ep0_set_address(dwc, ctrl);
703 break;
704 case USB_REQ_SET_CONFIGURATION:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500705 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300706 ret = dwc3_ep0_set_config(dwc, ctrl);
707 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300708 case USB_REQ_SET_SEL:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500709 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL\n");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300710 ret = dwc3_ep0_set_sel(dwc, ctrl);
711 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300712 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500713 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY\n");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300714 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
715 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 default:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500717 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver\n");
Felipe Balbi72246da2011-08-19 18:10:58 +0300718 ret = dwc3_ep0_delegate_req(dwc, ctrl);
719 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700720 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300721
722 return ret;
723}
724
725static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
726 const struct dwc3_event_depevt *event)
727{
728 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300729 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 u32 len;
731
732 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300733 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300734
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500735 trace_dwc3_ctrl_req(ctrl);
736
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300738 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300739 dwc->three_stage_setup = false;
740 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300741 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
742 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300743 dwc->three_stage_setup = true;
744 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300745 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
746 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300747
748 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
749 ret = dwc3_ep0_std_request(dwc, ctrl);
750 else
751 ret = dwc3_ep0_delegate_req(dwc, ctrl);
752
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100753 if (ret == USB_GADGET_DELAYED_STATUS)
754 dwc->delayed_status = true;
755
Felipe Balbief21ede2012-05-31 10:29:49 +0300756out:
757 if (ret < 0)
758 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300759}
760
761static void dwc3_ep0_complete_data(struct dwc3 *dwc,
762 const struct dwc3_event_depevt *event)
763{
764 struct dwc3_request *r = NULL;
765 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200766 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200767 struct dwc3_ep *ep0;
Felipe Balbic611ccb2011-08-27 02:30:33 +0300768 u32 transferred;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300769 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200770 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300771 u8 epnum;
772
773 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200774 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300775
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300776 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
777
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200778 r = next_request(&ep0->request_list);
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200779 ur = &r->request;
Felipe Balbi72246da2011-08-19 18:10:58 +0300780
Felipe Balbif6bafc62012-02-06 11:04:53 +0200781 trb = dwc->ep0_trb;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300782
783 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
784 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500785 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca8892a2012-07-19 09:05:35 +0300786
787 if (r)
788 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
789
790 return;
791 }
792
Felipe Balbif6bafc62012-02-06 11:04:53 +0200793 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300794
Felipe Balbia6829702011-08-27 22:18:09 +0300795 if (dwc->ep0_bounced) {
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500796 unsigned transfer_size = ur->length;
797 unsigned maxp = ep0->endpoint.maxpacket;
798
799 transfer_size += (maxp - (transfer_size % maxp));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300800 transferred = min_t(u32, ur->length,
Moiz Sonasath566ccdd2012-03-14 00:44:56 -0500801 transfer_size - length);
Felipe Balbia6829702011-08-27 22:18:09 +0300802 memcpy(ur->buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300803 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200804 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300805 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300806
Felipe Balbicd423dd2012-03-21 11:44:00 +0200807 ur->actual += transferred;
808
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 if ((epnum & 1) && ur->actual < ur->length) {
810 /* for some reason we did not get everything out */
811
812 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300813 } else {
814 /*
815 * handle the case where we have to send a zero packet. This
816 * seems to be case when req.length > maxpacket. Could it be?
817 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300818 if (r)
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200819 dwc3_gadget_giveback(ep0, r, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300820 }
821}
822
Felipe Balbi85a78102012-05-31 12:32:37 +0300823static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300824 const struct dwc3_event_depevt *event)
825{
826 struct dwc3_request *r;
827 struct dwc3_ep *dep;
Felipe Balbifca8892a2012-07-19 09:05:35 +0300828 struct dwc3_trb *trb;
829 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300830
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300831 dep = dwc->eps[0];
Felipe Balbifca8892a2012-07-19 09:05:35 +0300832 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300833
834 if (!list_empty(&dep->request_list)) {
835 r = next_request(&dep->request_list);
836
837 dwc3_gadget_giveback(dep, r, 0);
838 }
839
Gerard Cauvy3b637362012-02-10 12:21:18 +0200840 if (dwc->test_mode) {
841 int ret;
842
843 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
844 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500845 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200846 dwc->test_mode_nr);
847 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300848 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200849 }
850 }
851
Felipe Balbifca8892a2012-07-19 09:05:35 +0300852 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
853 if (status == DWC3_TRBSTS_SETUP_PENDING)
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500854 dwc3_trace(trace_dwc3_ep0, "Setup Pending received\n");
Felipe Balbifca8892a2012-07-19 09:05:35 +0300855
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300856 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300857 dwc3_ep0_out_start(dwc);
858}
859
860static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
861 const struct dwc3_event_depevt *event)
862{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300863 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
864
865 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300866 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300867 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300868
Felipe Balbi72246da2011-08-19 18:10:58 +0300869 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300870 case EP0_SETUP_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500871 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300872 dwc3_ep0_inspect_setup(dwc, event);
873 break;
874
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300875 case EP0_DATA_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500876 dwc3_trace(trace_dwc3_ep0, "Data Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 dwc3_ep0_complete_data(dwc, event);
878 break;
879
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300880 case EP0_STATUS_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500881 dwc3_trace(trace_dwc3_ep0, "Status Phase");
Felipe Balbi85a78102012-05-31 12:32:37 +0300882 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300883 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300884 default:
885 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300886 }
887}
888
Felipe Balbia0807882012-05-04 13:03:54 +0300889static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
890 struct dwc3_ep *dep, struct dwc3_request *req)
891{
892 int ret;
893
894 req->direction = !!dep->number;
895
896 if (req->request.length == 0) {
897 ret = dwc3_ep0_start_trans(dwc, dep->number,
898 dwc->ctrl_req_addr, 0,
899 DWC3_TRBCTL_CONTROL_DATA);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300900 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300901 && (dep->number == 0)) {
Andrew Mortonc390b032013-03-08 09:42:50 +0200902 u32 transfer_size;
903 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +0300904
905 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
906 dep->number);
907 if (ret) {
908 dev_dbg(dwc->dev, "failed to map request\n");
909 return;
910 }
911
912 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
913
Andrew Mortonc390b032013-03-08 09:42:50 +0200914 maxpacket = dep->endpoint.maxpacket;
915 transfer_size = roundup(req->request.length, maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +0300916
917 dwc->ep0_bounced = true;
918
919 /*
920 * REVISIT in case request length is bigger than
921 * DWC3_EP0_BOUNCE_SIZE we will need two chained
922 * TRBs to handle the transfer.
923 */
924 ret = dwc3_ep0_start_trans(dwc, dep->number,
925 dwc->ep0_bounce_addr, transfer_size,
926 DWC3_TRBCTL_CONTROL_DATA);
927 } else {
928 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
929 dep->number);
930 if (ret) {
931 dev_dbg(dwc->dev, "failed to map request\n");
932 return;
933 }
934
935 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
936 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
937 }
938
939 WARN_ON(ret < 0);
940}
941
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100942static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300943{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100944 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300945 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300946
947 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
948 : DWC3_TRBCTL_CONTROL_STATUS2;
949
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100950 return dwc3_ep0_start_trans(dwc, dep->number,
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300951 dwc->ctrl_req_addr, 0, type);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100952}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300953
Felipe Balbi788a23f2012-05-21 14:22:41 +0300954static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100955{
Felipe Balbi457e84b2012-01-18 18:04:09 +0200956 if (dwc->resize_fifos) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500957 dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200958 dwc3_gadget_resize_tx_fifos(dwc);
959 dwc->resize_fifos = 0;
960 }
961
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +0100962 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300963}
964
Felipe Balbi788a23f2012-05-21 14:22:41 +0300965static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
966 const struct dwc3_event_depevt *event)
967{
968 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
969
970 __dwc3_ep0_do_control_status(dwc, dep);
971}
972
Felipe Balbi2e3db062012-07-19 09:26:59 +0300973static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
974{
975 struct dwc3_gadget_ep_cmd_params params;
976 u32 cmd;
977 int ret;
978
979 if (!dep->resource_index)
980 return;
981
982 cmd = DWC3_DEPCMD_ENDTRANSFER;
983 cmd |= DWC3_DEPCMD_CMDIOC;
984 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
985 memset(&params, 0, sizeof(params));
986 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
987 WARN_ON_ONCE(ret);
988 dep->resource_index = 0;
989}
990
Felipe Balbi72246da2011-08-19 18:10:58 +0300991static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
992 const struct dwc3_event_depevt *event)
993{
Felipe Balbidf62df52011-10-14 15:11:49 +0300994 dwc->setup_packet_pending = true;
995
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300996 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300997 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500998 dwc3_trace(trace_dwc3_ep0, "Control Data");
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300999
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001000 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001001 * We already have a DATA transfer in the controller's cache,
1002 * if we receive a XferNotReady(DATA) we will ignore it, unless
1003 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001004 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001005 * In that case, we must issue END_TRANSFER command to the Data
1006 * Phase we already have started and issue SetStall on the
1007 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001008 */
1009 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001010 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1011
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001012 dwc3_trace(trace_dwc3_ep0,
1013 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001014 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001015 dwc3_ep0_stall_and_restart(dwc);
1016 return;
1017 }
1018
Felipe Balbi72246da2011-08-19 18:10:58 +03001019 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001020
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001021 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001022 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1023 return;
1024
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001025 dwc3_trace(trace_dwc3_ep0, "Control Status");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001026
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001027 dwc->ep0state = EP0_STATUS_PHASE;
1028
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001029 if (dwc->delayed_status) {
1030 WARN_ON_ONCE(event->endpoint_number != 1);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001031 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001032 return;
1033 }
1034
Felipe Balbi788a23f2012-05-21 14:22:41 +03001035 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001036 }
1037}
1038
1039void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001040 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001041{
1042 u8 epnum = event->endpoint_number;
1043
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001044 dwc3_trace(trace_dwc3_ep0, "%s while ep%d%s in state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +03001045 dwc3_ep_event_string(event->endpoint_event),
Sebastian Andrzej Siewiorb147f352011-09-30 10:58:40 +03001046 epnum >> 1, (epnum & 1) ? "in" : "out",
Felipe Balbi72246da2011-08-19 18:10:58 +03001047 dwc3_ep0_state_string(dwc->ep0state));
1048
1049 switch (event->endpoint_event) {
1050 case DWC3_DEPEVT_XFERCOMPLETE:
1051 dwc3_ep0_xfer_complete(dwc, event);
1052 break;
1053
1054 case DWC3_DEPEVT_XFERNOTREADY:
1055 dwc3_ep0_xfernotready(dwc, event);
1056 break;
1057
1058 case DWC3_DEPEVT_XFERINPROGRESS:
1059 case DWC3_DEPEVT_RXTXFIFOEVT:
1060 case DWC3_DEPEVT_STREAMEVT:
1061 case DWC3_DEPEVT_EPCMDCMPLT:
1062 break;
1063 }
1064}