blob: 15b62a5aaff8f6f28e279088b10bc79e5d6689df [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 int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +053043 u32 len, u32 type, bool chain)
Felipe Balbi72246da2011-08-19 18:10:58 +030044{
45 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbif6bafc62012-02-06 11:04:53 +020046 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030047 struct dwc3_ep *dep;
48
49 int ret;
50
51 dep = dwc->eps[epnum];
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030052 if (dep->flags & DWC3_EP_BUSY) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -050053 dwc3_trace(trace_dwc3_ep0, "%s still busy", dep->name);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030054 return 0;
55 }
Felipe Balbi72246da2011-08-19 18:10:58 +030056
Felipe Balbi53fd8812016-04-04 15:33:41 +030057 trb = &dwc->ep0_trb[dep->trb_enqueue];
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053058
59 if (chain)
Felipe Balbi53fd8812016-04-04 15:33:41 +030060 dep->trb_enqueue++;
Felipe Balbi72246da2011-08-19 18:10:58 +030061
Felipe Balbif6bafc62012-02-06 11:04:53 +020062 trb->bpl = lower_32_bits(buf_dma);
63 trb->bph = upper_32_bits(buf_dma);
64 trb->size = len;
65 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030066
Felipe Balbif6bafc62012-02-06 11:04:53 +020067 trb->ctrl |= (DWC3_TRB_CTRL_HWO
Felipe Balbif6bafc62012-02-06 11:04:53 +020068 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030069
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053070 if (chain)
71 trb->ctrl |= DWC3_TRB_CTRL_CHN;
72 else
73 trb->ctrl |= (DWC3_TRB_CTRL_IOC
74 | DWC3_TRB_CTRL_LST);
75
76 if (chain)
77 return 0;
78
Felipe Balbi72246da2011-08-19 18:10:58 +030079 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030080 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
81 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030082
Felipe Balbiccb072d2014-10-01 12:20:29 -050083 trace_dwc3_prepare_trb(dep, trb);
84
Felipe Balbi2cd47182016-04-12 16:42:43 +030085 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, &params);
Felipe Balbi8566cd12016-09-26 11:16:39 +030086 if (ret < 0)
Felipe Balbi72246da2011-08-19 18:10:58 +030087 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +030088
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030089 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi2eb88012016-04-12 16:53:39 +030090 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi1ddcb212011-08-30 15:52:17 +030091 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
92
Felipe Balbi72246da2011-08-19 18:10:58 +030093 return 0;
94}
95
96static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
97 struct dwc3_request *req)
98{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010099 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300100
101 req->request.actual = 0;
102 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300103 req->epnum = dep->number;
104
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200105 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300106
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300107 /*
108 * Gadget driver might not be quick enough to queue a request
109 * before we get a Transfer Not Ready event on this endpoint.
110 *
111 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
112 * flag is set, it's telling us that as soon as Gadget queues the
113 * required request, we should kick the transfer here because the
114 * IRQ we were waiting for is long gone.
115 */
116 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300117 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300118
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300119 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300120
Felipe Balbi68d8a782011-12-29 06:32:29 +0200121 if (dwc->ep0state != EP0_DATA_PHASE) {
122 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300123 return 0;
124 }
Felipe Balbia6829702011-08-27 22:18:09 +0300125
Felipe Balbia0807882012-05-04 13:03:54 +0300126 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
127
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300128 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
129 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300130
131 return 0;
132 }
133
134 /*
135 * In case gadget driver asked us to delay the STATUS phase,
136 * handle it here.
137 */
138 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300139 unsigned direction;
140
141 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100142 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300143 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200144
145 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300146 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200147 else
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500148 dwc3_trace(trace_dwc3_ep0,
149 "too early for delayed status");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300150
151 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300152 }
153
Felipe Balbifca88922012-07-19 09:05:35 +0300154 /*
155 * Unfortunately we have uncovered a limitation wrt the Data Phase.
156 *
157 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
158 * come before issueing Start Transfer command, but if we do, we will
159 * miss situations where the host starts another SETUP phase instead of
160 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
161 * Layer Compliance Suite.
162 *
163 * The problem surfaces due to the fact that in case of back-to-back
164 * SETUP packets there will be no XferNotReady(DATA) generated and we
165 * will be stuck waiting for XferNotReady(DATA) forever.
166 *
167 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
168 * it tells us to start Data Phase right away. It also mentions that if
169 * we receive a SETUP phase instead of the DATA phase, core will issue
170 * XferComplete for the DATA phase, before actually initiating it in
171 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
172 * can only be used to print some debugging logs, as the core expects
173 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
174 * just so it completes right away, without transferring anything and,
175 * only then, we can go back to the SETUP phase.
176 *
177 * Because of this scenario, SNPS decided to change the programming
178 * model of control transfers and support on-demand transfers only for
179 * the STATUS phase. To fix the issue we have now, we will always wait
180 * for gadget driver to queue the DATA phase's struct usb_request, then
181 * start it right away.
182 *
183 * If we're actually in a 2-stage transfer, we will wait for
184 * XferNotReady(STATUS).
185 */
186 if (dwc->three_stage_setup) {
187 unsigned direction;
188
189 direction = dwc->ep0_expect_in;
190 dwc->ep0state = EP0_DATA_PHASE;
191
192 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
193
194 dep->flags &= ~DWC3_EP0_DIR_IN;
195 }
196
Felipe Balbi35f75692012-07-19 08:49:01 +0300197 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300198}
199
200int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
201 gfp_t gfp_flags)
202{
203 struct dwc3_request *req = to_dwc3_request(request);
204 struct dwc3_ep *dep = to_dwc3_ep(ep);
205 struct dwc3 *dwc = dep->dwc;
206
207 unsigned long flags;
208
209 int ret;
210
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200212 if (!dep->endpoint.desc) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500213 dwc3_trace(trace_dwc3_ep0,
214 "trying to queue request %p to disabled %s",
Felipe Balbi72246da2011-08-19 18:10:58 +0300215 request, dep->name);
216 ret = -ESHUTDOWN;
217 goto out;
218 }
219
220 /* we share one TRB for ep0/1 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200221 if (!list_empty(&dep->pending_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300222 ret = -EBUSY;
223 goto out;
224 }
225
Felipe Balbi72246da2011-08-19 18:10:58 +0300226 ret = __dwc3_gadget_ep0_queue(dep, req);
227
228out:
229 spin_unlock_irqrestore(&dwc->lock, flags);
230
231 return ret;
232}
233
234static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
235{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300236 struct dwc3_ep *dep;
237
238 /* reinitialize physical ep1 */
239 dep = dwc->eps[1];
240 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300241
Felipe Balbi72246da2011-08-19 18:10:58 +0300242 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300243 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500244 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200245 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100246 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300247
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200248 if (!list_empty(&dep->pending_list)) {
Felipe Balbid7422202011-09-08 18:17:12 +0300249 struct dwc3_request *req;
250
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200251 req = next_request(&dep->pending_list);
Felipe Balbid7422202011-09-08 18:17:12 +0300252 dwc3_gadget_giveback(dep, req, -ECONNRESET);
253 }
254
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300255 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300256 dwc3_ep0_out_start(dwc);
257}
258
Felipe Balbi33fb6912014-09-24 10:46:46 -0500259int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530260{
261 struct dwc3_ep *dep = to_dwc3_ep(ep);
262 struct dwc3 *dwc = dep->dwc;
263
264 dwc3_ep0_stall_and_restart(dwc);
265
266 return 0;
267}
268
Felipe Balbi33fb6912014-09-24 10:46:46 -0500269int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
270{
271 struct dwc3_ep *dep = to_dwc3_ep(ep);
272 struct dwc3 *dwc = dep->dwc;
273 unsigned long flags;
274 int ret;
275
276 spin_lock_irqsave(&dwc->lock, flags);
277 ret = __dwc3_gadget_ep0_set_halt(ep, value);
278 spin_unlock_irqrestore(&dwc->lock, flags);
279
280 return ret;
281}
282
Felipe Balbi72246da2011-08-19 18:10:58 +0300283void dwc3_ep0_out_start(struct dwc3 *dwc)
284{
Felipe Balbi72246da2011-08-19 18:10:58 +0300285 int ret;
286
Baolin Wangbb014732016-10-14 17:11:33 +0800287 complete(&dwc->ep0_in_setup);
288
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300289 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530290 DWC3_TRBCTL_CONTROL_SETUP, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300291 WARN_ON(ret < 0);
292}
293
Felipe Balbi72246da2011-08-19 18:10:58 +0300294static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
295{
296 struct dwc3_ep *dep;
297 u32 windex = le16_to_cpu(wIndex_le);
298 u32 epnum;
299
300 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
301 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
302 epnum |= 1;
303
304 dep = dwc->eps[epnum];
305 if (dep->flags & DWC3_EP_ENABLED)
306 return dep;
307
308 return NULL;
309}
310
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200311static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300312{
Felipe Balbi72246da2011-08-19 18:10:58 +0300313}
Felipe Balbi72246da2011-08-19 18:10:58 +0300314/*
315 * ch 9.4.5
316 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200317static int dwc3_ep0_handle_status(struct dwc3 *dwc,
318 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300319{
320 struct dwc3_ep *dep;
321 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200322 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300323 u16 usb_status = 0;
324 __le16 *response_pkt;
325
326 recip = ctrl->bRequestType & USB_RECIP_MASK;
327 switch (recip) {
328 case USB_RECIP_DEVICE:
329 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200330 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300331 */
Peter Chenbcdea502015-01-28 16:32:40 +0800332 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200333
John Younee5cd412016-02-05 17:08:45 -0800334 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
335 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200336 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
337 if (reg & DWC3_DCTL_INITU1ENA)
338 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
339 if (reg & DWC3_DCTL_INITU2ENA)
340 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
341 }
342
Felipe Balbi72246da2011-08-19 18:10:58 +0300343 break;
344
345 case USB_RECIP_INTERFACE:
346 /*
347 * Function Remote Wake Capable D0
348 * Function Remote Wakeup D1
349 */
350 break;
351
352 case USB_RECIP_ENDPOINT:
353 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
354 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200355 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300356
357 if (dep->flags & DWC3_EP_STALL)
358 usb_status = 1 << USB_ENDPOINT_HALT;
359 break;
360 default:
361 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700362 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300363
364 response_pkt = (__le16 *) dwc->setup_buf;
365 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200366
367 dep = dwc->eps[0];
368 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100369 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200370 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100371 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200372
373 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300374}
375
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300376static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
377 int set)
Felipe Balbi72246da2011-08-19 18:10:58 +0300378{
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300379 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300380
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300381 if (state != USB_STATE_CONFIGURED)
382 return -EINVAL;
383 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
384 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
385 return -EINVAL;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200386
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300387 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
388 if (set)
389 reg |= DWC3_DCTL_INITU1ENA;
390 else
391 reg &= ~DWC3_DCTL_INITU1ENA;
392 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300393
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300394 return 0;
395}
Felipe Balbi72246da2011-08-19 18:10:58 +0300396
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300397static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
398 int set)
399{
400 u32 reg;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200401
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200402
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300403 if (state != USB_STATE_CONFIGURED)
404 return -EINVAL;
405 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
406 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
407 return -EINVAL;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200408
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300409 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
410 if (set)
411 reg |= DWC3_DCTL_INITU2ENA;
412 else
413 reg &= ~DWC3_DCTL_INITU2ENA;
414 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300415
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300416 return 0;
417}
Felipe Balbi72246da2011-08-19 18:10:58 +0300418
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300419static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
420 u32 wIndex, int set)
421{
422 if ((wIndex & 0xff) != 0)
423 return -EINVAL;
424 if (!set)
425 return -EINVAL;
426
427 switch (wIndex >> 8) {
428 case TEST_J:
429 case TEST_K:
430 case TEST_SE0_NAK:
431 case TEST_PACKET:
432 case TEST_FORCE_EN:
433 dwc->test_mode_nr = wIndex >> 8;
434 dwc->test_mode = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300435 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 default:
437 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700438 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300439
Felipe Balbi72246da2011-08-19 18:10:58 +0300440 return 0;
441}
442
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300443static int dwc3_ep0_handle_device(struct dwc3 *dwc,
444 struct usb_ctrlrequest *ctrl, int set)
445{
446 enum usb_device_state state;
447 u32 wValue;
448 u32 wIndex;
449 int ret = 0;
450
451 wValue = le16_to_cpu(ctrl->wValue);
452 wIndex = le16_to_cpu(ctrl->wIndex);
453 state = dwc->gadget.state;
454
455 switch (wValue) {
456 case USB_DEVICE_REMOTE_WAKEUP:
457 break;
458 /*
459 * 9.4.1 says only only for SS, in AddressState only for
460 * default control pipe
461 */
462 case USB_DEVICE_U1_ENABLE:
463 ret = dwc3_ep0_handle_u1(dwc, state, set);
464 break;
465 case USB_DEVICE_U2_ENABLE:
466 ret = dwc3_ep0_handle_u2(dwc, state, set);
467 break;
468 case USB_DEVICE_LTM_ENABLE:
469 ret = -EINVAL;
470 break;
471 case USB_DEVICE_TEST_MODE:
472 ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
473 break;
474 default:
475 ret = -EINVAL;
476 }
477
478 return ret;
479}
480
481static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
482 struct usb_ctrlrequest *ctrl, int set)
483{
484 enum usb_device_state state;
485 u32 wValue;
486 u32 wIndex;
487 int ret = 0;
488
489 wValue = le16_to_cpu(ctrl->wValue);
490 wIndex = le16_to_cpu(ctrl->wIndex);
491 state = dwc->gadget.state;
492
493 switch (wValue) {
494 case USB_INTRF_FUNC_SUSPEND:
495 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
496 /* XXX enable Low power suspend */
497 ;
498 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
499 /* XXX enable remote wakeup */
500 ;
501 break;
502 default:
503 ret = -EINVAL;
504 }
505
506 return ret;
507}
508
509static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
510 struct usb_ctrlrequest *ctrl, int set)
511{
512 struct dwc3_ep *dep;
513 enum usb_device_state state;
514 u32 wValue;
515 u32 wIndex;
516 int ret;
517
518 wValue = le16_to_cpu(ctrl->wValue);
519 wIndex = le16_to_cpu(ctrl->wIndex);
520 state = dwc->gadget.state;
521
522 switch (wValue) {
523 case USB_ENDPOINT_HALT:
524 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
525 if (!dep)
526 return -EINVAL;
527
528 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
529 break;
530
531 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
532 if (ret)
533 return -EINVAL;
534 break;
535 default:
536 return -EINVAL;
537 }
538
539 return 0;
540}
541
542static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
543 struct usb_ctrlrequest *ctrl, int set)
544{
545 u32 recip;
546 int ret;
547 enum usb_device_state state;
548
549 recip = ctrl->bRequestType & USB_RECIP_MASK;
550 state = dwc->gadget.state;
551
552 switch (recip) {
553 case USB_RECIP_DEVICE:
554 ret = dwc3_ep0_handle_device(dwc, ctrl, set);
555 break;
556 case USB_RECIP_INTERFACE:
557 ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
558 break;
559 case USB_RECIP_ENDPOINT:
560 ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
561 break;
562 default:
563 ret = -EINVAL;
564 }
565
566 return ret;
567}
568
Felipe Balbi72246da2011-08-19 18:10:58 +0300569static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
570{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200571 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 u32 addr;
573 u32 reg;
574
575 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300576 if (addr > 127) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500577 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300579 }
580
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200581 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500582 dwc3_trace(trace_dwc3_ep0,
583 "trying to set address when configured");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300584 return -EINVAL;
585 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300586
Felipe Balbi26460212011-09-30 10:58:36 +0300587 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
588 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
589 reg |= DWC3_DCFG_DEVADDR(addr);
590 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200592 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200593 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200594 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200595 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
Felipe Balbi26460212011-09-30 10:58:36 +0300597 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300598}
599
600static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
601{
602 int ret;
603
604 spin_unlock(&dwc->lock);
605 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
606 spin_lock(&dwc->lock);
607 return ret;
608}
609
610static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
611{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200612 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 u32 cfg;
614 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530615 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 cfg = le16_to_cpu(ctrl->wValue);
618
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200619 switch (state) {
620 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200623 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 ret = dwc3_ep0_delegate_req(dwc, ctrl);
625 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200626 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300627
628 /*
629 * only change state if set_config has already
630 * been processed. If gadget driver returns
631 * USB_GADGET_DELAYED_STATUS, we will wait
632 * to change the state on the next usb_ep_queue()
633 */
634 if (ret == 0)
635 usb_gadget_set_state(&dwc->gadget,
636 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200637
Pratyush Anande274a312012-07-02 10:21:54 +0530638 /*
639 * Enable transition to U1/U2 state when
640 * nothing is pending from application.
641 */
642 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
643 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
644 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200645 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 break;
647
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200648 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300650 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200651 usb_gadget_set_state(&dwc->gadget,
652 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300653 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100654 default:
655 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100657 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300658}
659
Felipe Balbi865e09e2012-04-24 16:19:49 +0300660static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
661{
662 struct dwc3_ep *dep = to_dwc3_ep(ep);
663 struct dwc3 *dwc = dep->dwc;
664
665 u32 param = 0;
666 u32 reg;
667
668 struct timing {
669 u8 u1sel;
670 u8 u1pel;
John Youn501058e2016-05-23 11:32:40 -0700671 __le16 u2sel;
672 __le16 u2pel;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300673 } __packed timing;
674
675 int ret;
676
677 memcpy(&timing, req->buf, sizeof(timing));
678
679 dwc->u1sel = timing.u1sel;
680 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300681 dwc->u2sel = le16_to_cpu(timing.u2sel);
682 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300683
684 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
685 if (reg & DWC3_DCTL_INITU2ENA)
686 param = dwc->u2pel;
687 if (reg & DWC3_DCTL_INITU1ENA)
688 param = dwc->u1pel;
689
690 /*
691 * According to Synopsys Databook, if parameter is
692 * greater than 125, a value of zero should be
693 * programmed in the register.
694 */
695 if (param > 125)
696 param = 0;
697
698 /* now that we have the time, issue DGCMD Set Sel */
699 ret = dwc3_send_gadget_generic_command(dwc,
700 DWC3_DGCMD_SET_PERIODIC_PAR, param);
701 WARN_ON(ret < 0);
702}
703
704static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
705{
706 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200707 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300708 u16 wLength;
709 u16 wValue;
710
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200711 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300712 return -EINVAL;
713
714 wValue = le16_to_cpu(ctrl->wValue);
715 wLength = le16_to_cpu(ctrl->wLength);
716
717 if (wLength != 6) {
718 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
719 wLength);
720 return -EINVAL;
721 }
722
723 /*
724 * To handle Set SEL we need to receive 6 bytes from Host. So let's
725 * queue a usb_request for 6 bytes.
726 *
727 * Remember, though, this controller can't handle non-wMaxPacketSize
728 * aligned transfers on the OUT direction, so we queue a request for
729 * wMaxPacketSize instead.
730 */
731 dep = dwc->eps[0];
732 dwc->ep0_usb_req.dep = dep;
733 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
734 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
735 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
736
737 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
738}
739
Felipe Balbic12a0d82012-04-25 10:45:05 +0300740static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
741{
742 u16 wLength;
743 u16 wValue;
744 u16 wIndex;
745
746 wValue = le16_to_cpu(ctrl->wValue);
747 wLength = le16_to_cpu(ctrl->wLength);
748 wIndex = le16_to_cpu(ctrl->wIndex);
749
750 if (wIndex || wLength)
751 return -EINVAL;
752
753 /*
754 * REVISIT It's unclear from Databook what to do with this
755 * value. For now, just cache it.
756 */
757 dwc->isoch_delay = wValue;
758
759 return 0;
760}
761
Felipe Balbi72246da2011-08-19 18:10:58 +0300762static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
763{
764 int ret;
765
766 switch (ctrl->bRequest) {
767 case USB_REQ_GET_STATUS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500768 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 ret = dwc3_ep0_handle_status(dwc, ctrl);
770 break;
771 case USB_REQ_CLEAR_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500772 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300773 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
774 break;
775 case USB_REQ_SET_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500776 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
778 break;
779 case USB_REQ_SET_ADDRESS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500780 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300781 ret = dwc3_ep0_set_address(dwc, ctrl);
782 break;
783 case USB_REQ_SET_CONFIGURATION:
Felipe Balbidab716c2014-09-24 11:22:23 -0500784 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
Felipe Balbi72246da2011-08-19 18:10:58 +0300785 ret = dwc3_ep0_set_config(dwc, ctrl);
786 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300787 case USB_REQ_SET_SEL:
Felipe Balbidab716c2014-09-24 11:22:23 -0500788 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300789 ret = dwc3_ep0_set_sel(dwc, ctrl);
790 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300791 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbidab716c2014-09-24 11:22:23 -0500792 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300793 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
794 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 default:
Felipe Balbidab716c2014-09-24 11:22:23 -0500796 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 ret = dwc3_ep0_delegate_req(dwc, ctrl);
798 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700799 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300800
801 return ret;
802}
803
804static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
805 const struct dwc3_event_depevt *event)
806{
807 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300808 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 u32 len;
810
811 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300812 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300813
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500814 trace_dwc3_ctrl_req(ctrl);
815
Felipe Balbi72246da2011-08-19 18:10:58 +0300816 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300817 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300818 dwc->three_stage_setup = false;
819 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300820 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
821 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300822 dwc->three_stage_setup = true;
823 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300824 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
825 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300826
827 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
828 ret = dwc3_ep0_std_request(dwc, ctrl);
829 else
830 ret = dwc3_ep0_delegate_req(dwc, ctrl);
831
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100832 if (ret == USB_GADGET_DELAYED_STATUS)
833 dwc->delayed_status = true;
834
Felipe Balbief21ede2012-05-31 10:29:49 +0300835out:
836 if (ret < 0)
837 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300838}
839
840static void dwc3_ep0_complete_data(struct dwc3 *dwc,
841 const struct dwc3_event_depevt *event)
842{
843 struct dwc3_request *r = NULL;
844 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200845 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200846 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530847 unsigned transfer_size = 0;
848 unsigned maxp;
849 unsigned remaining_ur_length;
850 void *buf;
851 u32 transferred = 0;
Felipe Balbifca88922012-07-19 09:05:35 +0300852 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200853 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300854 u8 epnum;
855
856 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200857 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300858
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300859 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
860
Felipe Balbif6bafc62012-02-06 11:04:53 +0200861 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300862
Felipe Balbiccb072d2014-10-01 12:20:29 -0500863 trace_dwc3_complete_trb(ep0, trb);
864
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200865 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600866 if (!r)
867 return;
868
Felipe Balbifca88922012-07-19 09:05:35 +0300869 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
870 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600871 dwc->setup_packet_pending = true;
872
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500873 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300874
875 if (r)
876 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
877
878 return;
879 }
880
Felipe Balbi6856d302014-09-30 11:43:20 -0500881 ur = &r->request;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530882 buf = ur->buf;
883 remaining_ur_length = ur->length;
Felipe Balbi6856d302014-09-30 11:43:20 -0500884
Felipe Balbif6bafc62012-02-06 11:04:53 +0200885 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300886
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530887 maxp = ep0->endpoint.maxpacket;
888
Felipe Balbia6829702011-08-27 22:18:09 +0300889 if (dwc->ep0_bounced) {
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530890 /*
891 * Handle the first TRB before handling the bounce buffer if
892 * the request length is greater than the bounce buffer size
893 */
894 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
895 transfer_size = ALIGN(ur->length - maxp, maxp);
896 transferred = transfer_size - length;
897 buf = (u8 *)buf + transferred;
898 ur->actual += transferred;
899 remaining_ur_length -= transferred;
900
901 trb++;
902 length = trb->size & DWC3_TRB_SIZE_MASK;
903
Felipe Balbi53fd8812016-04-04 15:33:41 +0300904 ep0->trb_enqueue = 0;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530905 }
906
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530907 transfer_size = roundup((ur->length - transfer_size),
908 maxp);
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530909
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530910 transferred = min_t(u32, remaining_ur_length,
911 transfer_size - length);
912 memcpy(buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300913 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200914 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300915 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300916
Felipe Balbicd423dd2012-03-21 11:44:00 +0200917 ur->actual += transferred;
918
Felipe Balbi72246da2011-08-19 18:10:58 +0300919 if ((epnum & 1) && ur->actual < ur->length) {
920 /* for some reason we did not get everything out */
921
922 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300923 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500924 dwc3_gadget_giveback(ep0, r, 0);
925
926 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
927 ur->length && ur->zero) {
928 int ret;
929
930 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
931
932 ret = dwc3_ep0_start_trans(dwc, epnum,
933 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530934 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500935 WARN_ON(ret < 0);
936 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300937 }
938}
939
Felipe Balbi85a78102012-05-31 12:32:37 +0300940static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300941 const struct dwc3_event_depevt *event)
942{
943 struct dwc3_request *r;
944 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300945 struct dwc3_trb *trb;
946 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300947
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300948 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300949 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300950
Felipe Balbiccb072d2014-10-01 12:20:29 -0500951 trace_dwc3_complete_trb(dep, trb);
952
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200953 if (!list_empty(&dep->pending_list)) {
954 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300955
956 dwc3_gadget_giveback(dep, r, 0);
957 }
958
Gerard Cauvy3b637362012-02-10 12:21:18 +0200959 if (dwc->test_mode) {
960 int ret;
961
962 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
963 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500964 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200965 dwc->test_mode_nr);
966 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300967 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200968 }
969 }
970
Felipe Balbifca88922012-07-19 09:05:35 +0300971 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbib5d335e2015-11-16 16:20:34 -0600972 if (status == DWC3_TRBSTS_SETUP_PENDING) {
973 dwc->setup_packet_pending = true;
Felipe Balbidab716c2014-09-24 11:22:23 -0500974 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbib5d335e2015-11-16 16:20:34 -0600975 }
Felipe Balbifca88922012-07-19 09:05:35 +0300976
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300977 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300978 dwc3_ep0_out_start(dwc);
979}
980
981static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
982 const struct dwc3_event_depevt *event)
983{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300984 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
985
986 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300987 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300988 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300989
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300991 case EP0_SETUP_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 dwc3_ep0_inspect_setup(dwc, event);
993 break;
994
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300995 case EP0_DATA_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 dwc3_ep0_complete_data(dwc, event);
997 break;
998
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300999 case EP0_STATUS_PHASE:
Felipe Balbi85a78102012-05-31 12:32:37 +03001000 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001001 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001002 default:
1003 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 }
1005}
1006
Felipe Balbia0807882012-05-04 13:03:54 +03001007static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
1008 struct dwc3_ep *dep, struct dwc3_request *req)
1009{
1010 int ret;
1011
1012 req->direction = !!dep->number;
1013
1014 if (req->request.length == 0) {
1015 ret = dwc3_ep0_start_trans(dwc, dep->number,
1016 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301017 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic74c6d42012-05-04 13:08:22 +03001018 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +03001019 && (dep->number == 0)) {
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301020 u32 transfer_size = 0;
Andrew Mortonc390b032013-03-08 09:42:50 +02001021 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +03001022
1023 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1024 dep->number);
1025 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001026 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +03001027 return;
1028 }
1029
Andrew Mortonc390b032013-03-08 09:42:50 +02001030 maxpacket = dep->endpoint.maxpacket;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +05301031
1032 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
1033 transfer_size = ALIGN(req->request.length - maxpacket,
1034 maxpacket);
1035 ret = dwc3_ep0_start_trans(dwc, dep->number,
1036 req->request.dma,
1037 transfer_size,
1038 DWC3_TRBCTL_CONTROL_DATA,
1039 true);
1040 }
1041
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301042 transfer_size = roundup((req->request.length - transfer_size),
1043 maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +03001044
1045 dwc->ep0_bounced = true;
1046
Felipe Balbia0807882012-05-04 13:03:54 +03001047 ret = dwc3_ep0_start_trans(dwc, dep->number,
1048 dwc->ep0_bounce_addr, transfer_size,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301049 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbia0807882012-05-04 13:03:54 +03001050 } else {
1051 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1052 dep->number);
1053 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001054 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +03001055 return;
1056 }
1057
1058 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301059 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1060 false);
Felipe Balbia0807882012-05-04 13:03:54 +03001061 }
1062
1063 WARN_ON(ret < 0);
1064}
1065
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001066static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001067{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001068 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001069 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001070
1071 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1072 : DWC3_TRBCTL_CONTROL_STATUS2;
1073
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001074 return dwc3_ep0_start_trans(dwc, dep->number,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301075 dwc->ctrl_req_addr, 0, type, false);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001076}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001077
Felipe Balbi788a23f2012-05-21 14:22:41 +03001078static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001079{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001080 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001081}
1082
Felipe Balbi788a23f2012-05-21 14:22:41 +03001083static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1084 const struct dwc3_event_depevt *event)
1085{
1086 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1087
1088 __dwc3_ep0_do_control_status(dwc, dep);
1089}
1090
Felipe Balbi2e3db062012-07-19 09:26:59 +03001091static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1092{
1093 struct dwc3_gadget_ep_cmd_params params;
1094 u32 cmd;
1095 int ret;
1096
1097 if (!dep->resource_index)
1098 return;
1099
1100 cmd = DWC3_DEPCMD_ENDTRANSFER;
1101 cmd |= DWC3_DEPCMD_CMDIOC;
1102 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1103 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001104 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi2e3db062012-07-19 09:26:59 +03001105 WARN_ON_ONCE(ret);
1106 dep->resource_index = 0;
1107}
1108
Felipe Balbi72246da2011-08-19 18:10:58 +03001109static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1110 const struct dwc3_event_depevt *event)
1111{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001112 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001113 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001114 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001115 * We already have a DATA transfer in the controller's cache,
1116 * if we receive a XferNotReady(DATA) we will ignore it, unless
1117 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001118 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001119 * In that case, we must issue END_TRANSFER command to the Data
1120 * Phase we already have started and issue SetStall on the
1121 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001122 */
1123 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001124 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1125
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001126 dwc3_trace(trace_dwc3_ep0,
1127 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001128 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001129 dwc3_ep0_stall_and_restart(dwc);
1130 return;
1131 }
1132
Felipe Balbi72246da2011-08-19 18:10:58 +03001133 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001134
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001135 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001136 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1137 return;
1138
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001139 dwc->ep0state = EP0_STATUS_PHASE;
1140
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001141 if (dwc->delayed_status) {
1142 WARN_ON_ONCE(event->endpoint_number != 1);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001143 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001144 return;
1145 }
1146
Felipe Balbi788a23f2012-05-21 14:22:41 +03001147 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001148 }
1149}
1150
1151void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001152 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001153{
Felipe Balbi72246da2011-08-19 18:10:58 +03001154 switch (event->endpoint_event) {
1155 case DWC3_DEPEVT_XFERCOMPLETE:
1156 dwc3_ep0_xfer_complete(dwc, event);
1157 break;
1158
1159 case DWC3_DEPEVT_XFERNOTREADY:
1160 dwc3_ep0_xfernotready(dwc, event);
1161 break;
1162
1163 case DWC3_DEPEVT_XFERINPROGRESS:
1164 case DWC3_DEPEVT_RXTXFIFOEVT:
1165 case DWC3_DEPEVT_STREAMEVT:
1166 case DWC3_DEPEVT_EPCMDCMPLT:
1167 break;
1168 }
1169}