blob: 2b22ea7263d860cc5ba1ff8af03da47024a26c8d [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 Balbi5eb30ce2016-11-03 14:07:51 +020052 if (dep->flags & DWC3_EP_BUSY)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030053 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +030054
Felipe Balbi53fd8812016-04-04 15:33:41 +030055 trb = &dwc->ep0_trb[dep->trb_enqueue];
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053056
57 if (chain)
Felipe Balbi53fd8812016-04-04 15:33:41 +030058 dep->trb_enqueue++;
Felipe Balbi72246da2011-08-19 18:10:58 +030059
Felipe Balbif6bafc62012-02-06 11:04:53 +020060 trb->bpl = lower_32_bits(buf_dma);
61 trb->bph = upper_32_bits(buf_dma);
62 trb->size = len;
63 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030064
Felipe Balbif6bafc62012-02-06 11:04:53 +020065 trb->ctrl |= (DWC3_TRB_CTRL_HWO
Felipe Balbif6bafc62012-02-06 11:04:53 +020066 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030067
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053068 if (chain)
69 trb->ctrl |= DWC3_TRB_CTRL_CHN;
70 else
71 trb->ctrl |= (DWC3_TRB_CTRL_IOC
72 | DWC3_TRB_CTRL_LST);
73
74 if (chain)
75 return 0;
76
Felipe Balbi72246da2011-08-19 18:10:58 +030077 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +030078 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
79 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +030080
Felipe Balbiccb072d2014-10-01 12:20:29 -050081 trace_dwc3_prepare_trb(dep, trb);
82
Felipe Balbi2cd47182016-04-12 16:42:43 +030083 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, &params);
Felipe Balbi8566cd12016-09-26 11:16:39 +030084 if (ret < 0)
Felipe Balbi72246da2011-08-19 18:10:58 +030085 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +030086
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030087 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi2eb88012016-04-12 16:53:39 +030088 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi1ddcb212011-08-30 15:52:17 +030089 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
90
Felipe Balbi72246da2011-08-19 18:10:58 +030091 return 0;
92}
93
94static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
95 struct dwc3_request *req)
96{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010097 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +030098
99 req->request.actual = 0;
100 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300101 req->epnum = dep->number;
102
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200103 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300104
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300105 /*
106 * Gadget driver might not be quick enough to queue a request
107 * before we get a Transfer Not Ready event on this endpoint.
108 *
109 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
110 * flag is set, it's telling us that as soon as Gadget queues the
111 * required request, we should kick the transfer here because the
112 * IRQ we were waiting for is long gone.
113 */
114 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300115 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300116
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300117 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300118
Felipe Balbi68d8a782011-12-29 06:32:29 +0200119 if (dwc->ep0state != EP0_DATA_PHASE) {
120 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300121 return 0;
122 }
Felipe Balbia6829702011-08-27 22:18:09 +0300123
Felipe Balbia0807882012-05-04 13:03:54 +0300124 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
125
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300126 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
127 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300128
129 return 0;
130 }
131
132 /*
133 * In case gadget driver asked us to delay the STATUS phase,
134 * handle it here.
135 */
136 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300137 unsigned direction;
138
139 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100140 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300141 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200142
143 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300144 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300145
146 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300147 }
148
Felipe Balbifca88922012-07-19 09:05:35 +0300149 /*
150 * Unfortunately we have uncovered a limitation wrt the Data Phase.
151 *
152 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
153 * come before issueing Start Transfer command, but if we do, we will
154 * miss situations where the host starts another SETUP phase instead of
155 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
156 * Layer Compliance Suite.
157 *
158 * The problem surfaces due to the fact that in case of back-to-back
159 * SETUP packets there will be no XferNotReady(DATA) generated and we
160 * will be stuck waiting for XferNotReady(DATA) forever.
161 *
162 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
163 * it tells us to start Data Phase right away. It also mentions that if
164 * we receive a SETUP phase instead of the DATA phase, core will issue
165 * XferComplete for the DATA phase, before actually initiating it in
166 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
167 * can only be used to print some debugging logs, as the core expects
168 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
169 * just so it completes right away, without transferring anything and,
170 * only then, we can go back to the SETUP phase.
171 *
172 * Because of this scenario, SNPS decided to change the programming
173 * model of control transfers and support on-demand transfers only for
174 * the STATUS phase. To fix the issue we have now, we will always wait
175 * for gadget driver to queue the DATA phase's struct usb_request, then
176 * start it right away.
177 *
178 * If we're actually in a 2-stage transfer, we will wait for
179 * XferNotReady(STATUS).
180 */
181 if (dwc->three_stage_setup) {
182 unsigned direction;
183
184 direction = dwc->ep0_expect_in;
185 dwc->ep0state = EP0_DATA_PHASE;
186
187 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
188
189 dep->flags &= ~DWC3_EP0_DIR_IN;
190 }
191
Felipe Balbi35f75692012-07-19 08:49:01 +0300192 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300193}
194
195int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
196 gfp_t gfp_flags)
197{
198 struct dwc3_request *req = to_dwc3_request(request);
199 struct dwc3_ep *dep = to_dwc3_ep(ep);
200 struct dwc3 *dwc = dep->dwc;
201
202 unsigned long flags;
203
204 int ret;
205
Felipe Balbi72246da2011-08-19 18:10:58 +0300206 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200207 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200208 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
209 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300210 ret = -ESHUTDOWN;
211 goto out;
212 }
213
214 /* we share one TRB for ep0/1 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200215 if (!list_empty(&dep->pending_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300216 ret = -EBUSY;
217 goto out;
218 }
219
Felipe Balbi72246da2011-08-19 18:10:58 +0300220 ret = __dwc3_gadget_ep0_queue(dep, req);
221
222out:
223 spin_unlock_irqrestore(&dwc->lock, flags);
224
225 return ret;
226}
227
228static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
229{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300230 struct dwc3_ep *dep;
231
232 /* reinitialize physical ep1 */
233 dep = dwc->eps[1];
234 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300235
Felipe Balbi72246da2011-08-19 18:10:58 +0300236 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300237 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500238 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200239 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100240 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300241
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200242 if (!list_empty(&dep->pending_list)) {
Felipe Balbid7422202011-09-08 18:17:12 +0300243 struct dwc3_request *req;
244
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200245 req = next_request(&dep->pending_list);
Felipe Balbid7422202011-09-08 18:17:12 +0300246 dwc3_gadget_giveback(dep, req, -ECONNRESET);
247 }
248
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300249 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300250 dwc3_ep0_out_start(dwc);
251}
252
Felipe Balbi33fb6912014-09-24 10:46:46 -0500253int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530254{
255 struct dwc3_ep *dep = to_dwc3_ep(ep);
256 struct dwc3 *dwc = dep->dwc;
257
258 dwc3_ep0_stall_and_restart(dwc);
259
260 return 0;
261}
262
Felipe Balbi33fb6912014-09-24 10:46:46 -0500263int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
264{
265 struct dwc3_ep *dep = to_dwc3_ep(ep);
266 struct dwc3 *dwc = dep->dwc;
267 unsigned long flags;
268 int ret;
269
270 spin_lock_irqsave(&dwc->lock, flags);
271 ret = __dwc3_gadget_ep0_set_halt(ep, value);
272 spin_unlock_irqrestore(&dwc->lock, flags);
273
274 return ret;
275}
276
Felipe Balbi72246da2011-08-19 18:10:58 +0300277void dwc3_ep0_out_start(struct dwc3 *dwc)
278{
Felipe Balbi72246da2011-08-19 18:10:58 +0300279 int ret;
280
Baolin Wangbb014732016-10-14 17:11:33 +0800281 complete(&dwc->ep0_in_setup);
282
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300283 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530284 DWC3_TRBCTL_CONTROL_SETUP, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285 WARN_ON(ret < 0);
286}
287
Felipe Balbi72246da2011-08-19 18:10:58 +0300288static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
289{
290 struct dwc3_ep *dep;
291 u32 windex = le16_to_cpu(wIndex_le);
292 u32 epnum;
293
294 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
295 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
296 epnum |= 1;
297
298 dep = dwc->eps[epnum];
299 if (dep->flags & DWC3_EP_ENABLED)
300 return dep;
301
302 return NULL;
303}
304
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200305static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300306{
Felipe Balbi72246da2011-08-19 18:10:58 +0300307}
Felipe Balbi72246da2011-08-19 18:10:58 +0300308/*
309 * ch 9.4.5
310 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200311static int dwc3_ep0_handle_status(struct dwc3 *dwc,
312 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300313{
314 struct dwc3_ep *dep;
315 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200316 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300317 u16 usb_status = 0;
318 __le16 *response_pkt;
319
320 recip = ctrl->bRequestType & USB_RECIP_MASK;
321 switch (recip) {
322 case USB_RECIP_DEVICE:
323 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200324 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300325 */
Peter Chenbcdea502015-01-28 16:32:40 +0800326 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200327
John Younee5cd412016-02-05 17:08:45 -0800328 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
329 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200330 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
331 if (reg & DWC3_DCTL_INITU1ENA)
332 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
333 if (reg & DWC3_DCTL_INITU2ENA)
334 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
335 }
336
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 break;
338
339 case USB_RECIP_INTERFACE:
340 /*
341 * Function Remote Wake Capable D0
342 * Function Remote Wakeup D1
343 */
344 break;
345
346 case USB_RECIP_ENDPOINT:
347 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
348 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200349 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300350
351 if (dep->flags & DWC3_EP_STALL)
352 usb_status = 1 << USB_ENDPOINT_HALT;
353 break;
354 default:
355 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700356 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300357
358 response_pkt = (__le16 *) dwc->setup_buf;
359 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200360
361 dep = dwc->eps[0];
362 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100363 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200364 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100365 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200366
367 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300368}
369
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300370static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
371 int set)
Felipe Balbi72246da2011-08-19 18:10:58 +0300372{
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300373 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300374
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300375 if (state != USB_STATE_CONFIGURED)
376 return -EINVAL;
377 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
378 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
379 return -EINVAL;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200380
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300381 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
382 if (set)
383 reg |= DWC3_DCTL_INITU1ENA;
384 else
385 reg &= ~DWC3_DCTL_INITU1ENA;
386 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300387
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300388 return 0;
389}
Felipe Balbi72246da2011-08-19 18:10:58 +0300390
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300391static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
392 int set)
393{
394 u32 reg;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200395
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200396
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300397 if (state != USB_STATE_CONFIGURED)
398 return -EINVAL;
399 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
400 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
401 return -EINVAL;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200402
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300403 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
404 if (set)
405 reg |= DWC3_DCTL_INITU2ENA;
406 else
407 reg &= ~DWC3_DCTL_INITU2ENA;
408 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300409
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300410 return 0;
411}
Felipe Balbi72246da2011-08-19 18:10:58 +0300412
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300413static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
414 u32 wIndex, int set)
415{
416 if ((wIndex & 0xff) != 0)
417 return -EINVAL;
418 if (!set)
419 return -EINVAL;
420
421 switch (wIndex >> 8) {
422 case TEST_J:
423 case TEST_K:
424 case TEST_SE0_NAK:
425 case TEST_PACKET:
426 case TEST_FORCE_EN:
427 dwc->test_mode_nr = wIndex >> 8;
428 dwc->test_mode = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 default:
431 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700432 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300433
Felipe Balbi72246da2011-08-19 18:10:58 +0300434 return 0;
435}
436
Felipe Balbi39e07ff2016-10-03 12:55:29 +0300437static int dwc3_ep0_handle_device(struct dwc3 *dwc,
438 struct usb_ctrlrequest *ctrl, int set)
439{
440 enum usb_device_state state;
441 u32 wValue;
442 u32 wIndex;
443 int ret = 0;
444
445 wValue = le16_to_cpu(ctrl->wValue);
446 wIndex = le16_to_cpu(ctrl->wIndex);
447 state = dwc->gadget.state;
448
449 switch (wValue) {
450 case USB_DEVICE_REMOTE_WAKEUP:
451 break;
452 /*
453 * 9.4.1 says only only for SS, in AddressState only for
454 * default control pipe
455 */
456 case USB_DEVICE_U1_ENABLE:
457 ret = dwc3_ep0_handle_u1(dwc, state, set);
458 break;
459 case USB_DEVICE_U2_ENABLE:
460 ret = dwc3_ep0_handle_u2(dwc, state, set);
461 break;
462 case USB_DEVICE_LTM_ENABLE:
463 ret = -EINVAL;
464 break;
465 case USB_DEVICE_TEST_MODE:
466 ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
467 break;
468 default:
469 ret = -EINVAL;
470 }
471
472 return ret;
473}
474
475static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
476 struct usb_ctrlrequest *ctrl, int set)
477{
478 enum usb_device_state state;
479 u32 wValue;
480 u32 wIndex;
481 int ret = 0;
482
483 wValue = le16_to_cpu(ctrl->wValue);
484 wIndex = le16_to_cpu(ctrl->wIndex);
485 state = dwc->gadget.state;
486
487 switch (wValue) {
488 case USB_INTRF_FUNC_SUSPEND:
489 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
490 /* XXX enable Low power suspend */
491 ;
492 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
493 /* XXX enable remote wakeup */
494 ;
495 break;
496 default:
497 ret = -EINVAL;
498 }
499
500 return ret;
501}
502
503static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
504 struct usb_ctrlrequest *ctrl, int set)
505{
506 struct dwc3_ep *dep;
507 enum usb_device_state state;
508 u32 wValue;
509 u32 wIndex;
510 int ret;
511
512 wValue = le16_to_cpu(ctrl->wValue);
513 wIndex = le16_to_cpu(ctrl->wIndex);
514 state = dwc->gadget.state;
515
516 switch (wValue) {
517 case USB_ENDPOINT_HALT:
518 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
519 if (!dep)
520 return -EINVAL;
521
522 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
523 break;
524
525 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
526 if (ret)
527 return -EINVAL;
528 break;
529 default:
530 return -EINVAL;
531 }
532
533 return 0;
534}
535
536static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
537 struct usb_ctrlrequest *ctrl, int set)
538{
539 u32 recip;
540 int ret;
541 enum usb_device_state state;
542
543 recip = ctrl->bRequestType & USB_RECIP_MASK;
544 state = dwc->gadget.state;
545
546 switch (recip) {
547 case USB_RECIP_DEVICE:
548 ret = dwc3_ep0_handle_device(dwc, ctrl, set);
549 break;
550 case USB_RECIP_INTERFACE:
551 ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
552 break;
553 case USB_RECIP_ENDPOINT:
554 ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
555 break;
556 default:
557 ret = -EINVAL;
558 }
559
560 return ret;
561}
562
Felipe Balbi72246da2011-08-19 18:10:58 +0300563static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
564{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200565 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566 u32 addr;
567 u32 reg;
568
569 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300570 if (addr > 127) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200571 dev_err(dwc->dev, "invalid device address %d\n", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300573 }
574
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200575 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200576 dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300577 return -EINVAL;
578 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
Felipe Balbi26460212011-09-30 10:58:36 +0300580 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
581 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
582 reg |= DWC3_DCFG_DEVADDR(addr);
583 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200585 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200586 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200587 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200588 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300589
Felipe Balbi26460212011-09-30 10:58:36 +0300590 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591}
592
593static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
594{
595 int ret;
596
597 spin_unlock(&dwc->lock);
598 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
599 spin_lock(&dwc->lock);
600 return ret;
601}
602
603static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
604{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200605 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 u32 cfg;
607 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530608 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300609
610 cfg = le16_to_cpu(ctrl->wValue);
611
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200612 switch (state) {
613 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200616 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300617 ret = dwc3_ep0_delegate_req(dwc, ctrl);
618 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200619 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300620
621 /*
622 * only change state if set_config has already
623 * been processed. If gadget driver returns
624 * USB_GADGET_DELAYED_STATUS, we will wait
625 * to change the state on the next usb_ep_queue()
626 */
627 if (ret == 0)
628 usb_gadget_set_state(&dwc->gadget,
629 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200630
Pratyush Anande274a312012-07-02 10:21:54 +0530631 /*
632 * Enable transition to U1/U2 state when
633 * nothing is pending from application.
634 */
635 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
636 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
637 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200638 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300639 break;
640
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200641 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300642 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300643 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200644 usb_gadget_set_state(&dwc->gadget,
645 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100647 default:
648 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100650 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300651}
652
Felipe Balbi865e09e2012-04-24 16:19:49 +0300653static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
654{
655 struct dwc3_ep *dep = to_dwc3_ep(ep);
656 struct dwc3 *dwc = dep->dwc;
657
658 u32 param = 0;
659 u32 reg;
660
661 struct timing {
662 u8 u1sel;
663 u8 u1pel;
John Youn501058e2016-05-23 11:32:40 -0700664 __le16 u2sel;
665 __le16 u2pel;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300666 } __packed timing;
667
668 int ret;
669
670 memcpy(&timing, req->buf, sizeof(timing));
671
672 dwc->u1sel = timing.u1sel;
673 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300674 dwc->u2sel = le16_to_cpu(timing.u2sel);
675 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300676
677 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
678 if (reg & DWC3_DCTL_INITU2ENA)
679 param = dwc->u2pel;
680 if (reg & DWC3_DCTL_INITU1ENA)
681 param = dwc->u1pel;
682
683 /*
684 * According to Synopsys Databook, if parameter is
685 * greater than 125, a value of zero should be
686 * programmed in the register.
687 */
688 if (param > 125)
689 param = 0;
690
691 /* now that we have the time, issue DGCMD Set Sel */
692 ret = dwc3_send_gadget_generic_command(dwc,
693 DWC3_DGCMD_SET_PERIODIC_PAR, param);
694 WARN_ON(ret < 0);
695}
696
697static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
698{
699 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200700 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300701 u16 wLength;
702 u16 wValue;
703
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200704 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300705 return -EINVAL;
706
707 wValue = le16_to_cpu(ctrl->wValue);
708 wLength = le16_to_cpu(ctrl->wLength);
709
710 if (wLength != 6) {
711 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
712 wLength);
713 return -EINVAL;
714 }
715
716 /*
717 * To handle Set SEL we need to receive 6 bytes from Host. So let's
718 * queue a usb_request for 6 bytes.
719 *
720 * Remember, though, this controller can't handle non-wMaxPacketSize
721 * aligned transfers on the OUT direction, so we queue a request for
722 * wMaxPacketSize instead.
723 */
724 dep = dwc->eps[0];
725 dwc->ep0_usb_req.dep = dep;
726 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
727 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
728 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
729
730 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
731}
732
Felipe Balbic12a0d82012-04-25 10:45:05 +0300733static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
734{
735 u16 wLength;
736 u16 wValue;
737 u16 wIndex;
738
739 wValue = le16_to_cpu(ctrl->wValue);
740 wLength = le16_to_cpu(ctrl->wLength);
741 wIndex = le16_to_cpu(ctrl->wIndex);
742
743 if (wIndex || wLength)
744 return -EINVAL;
745
746 /*
747 * REVISIT It's unclear from Databook what to do with this
748 * value. For now, just cache it.
749 */
750 dwc->isoch_delay = wValue;
751
752 return 0;
753}
754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
756{
757 int ret;
758
759 switch (ctrl->bRequest) {
760 case USB_REQ_GET_STATUS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 ret = dwc3_ep0_handle_status(dwc, ctrl);
762 break;
763 case USB_REQ_CLEAR_FEATURE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300764 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
765 break;
766 case USB_REQ_SET_FEATURE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300767 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
768 break;
769 case USB_REQ_SET_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300770 ret = dwc3_ep0_set_address(dwc, ctrl);
771 break;
772 case USB_REQ_SET_CONFIGURATION:
Felipe Balbi72246da2011-08-19 18:10:58 +0300773 ret = dwc3_ep0_set_config(dwc, ctrl);
774 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300775 case USB_REQ_SET_SEL:
Felipe Balbi865e09e2012-04-24 16:19:49 +0300776 ret = dwc3_ep0_set_sel(dwc, ctrl);
777 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300778 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbic12a0d82012-04-25 10:45:05 +0300779 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
780 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300781 default:
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 ret = dwc3_ep0_delegate_req(dwc, ctrl);
783 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700784 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300785
786 return ret;
787}
788
789static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
790 const struct dwc3_event_depevt *event)
791{
792 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300793 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300794 u32 len;
795
796 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300797 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300798
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500799 trace_dwc3_ctrl_req(ctrl);
800
Felipe Balbi72246da2011-08-19 18:10:58 +0300801 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300802 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300803 dwc->three_stage_setup = false;
804 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300805 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
806 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300807 dwc->three_stage_setup = true;
808 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300809 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
810 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300811
812 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
813 ret = dwc3_ep0_std_request(dwc, ctrl);
814 else
815 ret = dwc3_ep0_delegate_req(dwc, ctrl);
816
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100817 if (ret == USB_GADGET_DELAYED_STATUS)
818 dwc->delayed_status = true;
819
Felipe Balbief21ede2012-05-31 10:29:49 +0300820out:
821 if (ret < 0)
822 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300823}
824
825static void dwc3_ep0_complete_data(struct dwc3 *dwc,
826 const struct dwc3_event_depevt *event)
827{
828 struct dwc3_request *r = NULL;
829 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200830 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200831 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530832 unsigned transfer_size = 0;
833 unsigned maxp;
834 unsigned remaining_ur_length;
835 void *buf;
836 u32 transferred = 0;
Felipe Balbifca88922012-07-19 09:05:35 +0300837 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200838 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300839 u8 epnum;
840
841 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200842 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300843
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300844 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
845
Felipe Balbif6bafc62012-02-06 11:04:53 +0200846 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300847
Felipe Balbiccb072d2014-10-01 12:20:29 -0500848 trace_dwc3_complete_trb(ep0, trb);
849
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200850 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600851 if (!r)
852 return;
853
Felipe Balbifca88922012-07-19 09:05:35 +0300854 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
855 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600856 dwc->setup_packet_pending = true;
Felipe Balbifca88922012-07-19 09:05:35 +0300857 if (r)
858 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
859
860 return;
861 }
862
Felipe Balbi6856d302014-09-30 11:43:20 -0500863 ur = &r->request;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530864 buf = ur->buf;
865 remaining_ur_length = ur->length;
Felipe Balbi6856d302014-09-30 11:43:20 -0500866
Felipe Balbif6bafc62012-02-06 11:04:53 +0200867 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300868
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530869 maxp = ep0->endpoint.maxpacket;
870
Felipe Balbia6829702011-08-27 22:18:09 +0300871 if (dwc->ep0_bounced) {
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530872 /*
873 * Handle the first TRB before handling the bounce buffer if
874 * the request length is greater than the bounce buffer size
875 */
876 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
877 transfer_size = ALIGN(ur->length - maxp, maxp);
878 transferred = transfer_size - length;
879 buf = (u8 *)buf + transferred;
880 ur->actual += transferred;
881 remaining_ur_length -= transferred;
882
883 trb++;
884 length = trb->size & DWC3_TRB_SIZE_MASK;
885
Felipe Balbi53fd8812016-04-04 15:33:41 +0300886 ep0->trb_enqueue = 0;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530887 }
888
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530889 transfer_size = roundup((ur->length - transfer_size),
890 maxp);
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530891
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530892 transferred = min_t(u32, remaining_ur_length,
893 transfer_size - length);
894 memcpy(buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300895 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200896 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300897 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
Felipe Balbicd423dd2012-03-21 11:44:00 +0200899 ur->actual += transferred;
900
Felipe Balbi72246da2011-08-19 18:10:58 +0300901 if ((epnum & 1) && ur->actual < ur->length) {
902 /* for some reason we did not get everything out */
903
904 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300905 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500906 dwc3_gadget_giveback(ep0, r, 0);
907
908 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
909 ur->length && ur->zero) {
910 int ret;
911
912 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
913
914 ret = dwc3_ep0_start_trans(dwc, epnum,
915 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530916 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500917 WARN_ON(ret < 0);
918 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300919 }
920}
921
Felipe Balbi85a78102012-05-31 12:32:37 +0300922static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300923 const struct dwc3_event_depevt *event)
924{
925 struct dwc3_request *r;
926 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300927 struct dwc3_trb *trb;
928 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300929
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300930 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300931 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300932
Felipe Balbiccb072d2014-10-01 12:20:29 -0500933 trace_dwc3_complete_trb(dep, trb);
934
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200935 if (!list_empty(&dep->pending_list)) {
936 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300937
938 dwc3_gadget_giveback(dep, r, 0);
939 }
940
Gerard Cauvy3b637362012-02-10 12:21:18 +0200941 if (dwc->test_mode) {
942 int ret;
943
944 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
945 if (ret < 0) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200946 dev_err(dwc->dev, "invalid test #%d\n",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200947 dwc->test_mode_nr);
948 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300949 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200950 }
951 }
952
Felipe Balbifca88922012-07-19 09:05:35 +0300953 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200954 if (status == DWC3_TRBSTS_SETUP_PENDING)
Felipe Balbib5d335e2015-11-16 16:20:34 -0600955 dwc->setup_packet_pending = true;
Felipe Balbifca88922012-07-19 09:05:35 +0300956
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300957 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 dwc3_ep0_out_start(dwc);
959}
960
961static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
962 const struct dwc3_event_depevt *event)
963{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300964 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
965
966 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300967 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300968 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300969
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300971 case EP0_SETUP_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 dwc3_ep0_inspect_setup(dwc, event);
973 break;
974
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300975 case EP0_DATA_PHASE:
Felipe Balbi72246da2011-08-19 18:10:58 +0300976 dwc3_ep0_complete_data(dwc, event);
977 break;
978
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300979 case EP0_STATUS_PHASE:
Felipe Balbi85a78102012-05-31 12:32:37 +0300980 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300981 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300982 default:
983 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300984 }
985}
986
Felipe Balbia0807882012-05-04 13:03:54 +0300987static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
988 struct dwc3_ep *dep, struct dwc3_request *req)
989{
990 int ret;
991
992 req->direction = !!dep->number;
993
994 if (req->request.length == 0) {
995 ret = dwc3_ep0_start_trans(dwc, dep->number,
996 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530997 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic74c6d42012-05-04 13:08:22 +0300998 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +0300999 && (dep->number == 0)) {
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301000 u32 transfer_size = 0;
Andrew Mortonc390b032013-03-08 09:42:50 +02001001 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +03001002
1003 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1004 dep->number);
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001005 if (ret)
Felipe Balbia0807882012-05-04 13:03:54 +03001006 return;
Felipe Balbia0807882012-05-04 13:03:54 +03001007
Andrew Mortonc390b032013-03-08 09:42:50 +02001008 maxpacket = dep->endpoint.maxpacket;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +05301009
1010 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
1011 transfer_size = ALIGN(req->request.length - maxpacket,
1012 maxpacket);
1013 ret = dwc3_ep0_start_trans(dwc, dep->number,
1014 req->request.dma,
1015 transfer_size,
1016 DWC3_TRBCTL_CONTROL_DATA,
1017 true);
1018 }
1019
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301020 transfer_size = roundup((req->request.length - transfer_size),
1021 maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +03001022
1023 dwc->ep0_bounced = true;
1024
Felipe Balbia0807882012-05-04 13:03:54 +03001025 ret = dwc3_ep0_start_trans(dwc, dep->number,
1026 dwc->ep0_bounce_addr, transfer_size,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301027 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbia0807882012-05-04 13:03:54 +03001028 } else {
1029 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1030 dep->number);
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001031 if (ret)
Felipe Balbia0807882012-05-04 13:03:54 +03001032 return;
Felipe Balbia0807882012-05-04 13:03:54 +03001033
1034 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301035 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1036 false);
Felipe Balbia0807882012-05-04 13:03:54 +03001037 }
1038
1039 WARN_ON(ret < 0);
1040}
1041
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001042static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001043{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001044 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001045 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001046
1047 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1048 : DWC3_TRBCTL_CONTROL_STATUS2;
1049
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001050 return dwc3_ep0_start_trans(dwc, dep->number,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301051 dwc->ctrl_req_addr, 0, type, false);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001052}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001053
Felipe Balbi788a23f2012-05-21 14:22:41 +03001054static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001055{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001056 WARN_ON(dwc3_ep0_start_control_status(dep));
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001057}
1058
Felipe Balbi788a23f2012-05-21 14:22:41 +03001059static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1060 const struct dwc3_event_depevt *event)
1061{
1062 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1063
1064 __dwc3_ep0_do_control_status(dwc, dep);
1065}
1066
Felipe Balbi2e3db062012-07-19 09:26:59 +03001067static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1068{
1069 struct dwc3_gadget_ep_cmd_params params;
1070 u32 cmd;
1071 int ret;
1072
1073 if (!dep->resource_index)
1074 return;
1075
1076 cmd = DWC3_DEPCMD_ENDTRANSFER;
1077 cmd |= DWC3_DEPCMD_CMDIOC;
1078 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1079 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001080 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi2e3db062012-07-19 09:26:59 +03001081 WARN_ON_ONCE(ret);
1082 dep->resource_index = 0;
1083}
1084
Felipe Balbi72246da2011-08-19 18:10:58 +03001085static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1086 const struct dwc3_event_depevt *event)
1087{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001088 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001089 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001090 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001091 * We already have a DATA transfer in the controller's cache,
1092 * if we receive a XferNotReady(DATA) we will ignore it, unless
1093 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001094 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001095 * In that case, we must issue END_TRANSFER command to the Data
1096 * Phase we already have started and issue SetStall on the
1097 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001098 */
1099 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001100 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1101
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001102 dev_err(dwc->dev, "unexpected direction for Data Phase\n");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001103 dwc3_ep0_end_control_data(dwc, dep);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001104 dwc3_ep0_stall_and_restart(dwc);
1105 return;
1106 }
1107
Felipe Balbi72246da2011-08-19 18:10:58 +03001108 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001109
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001110 case DEPEVT_STATUS_CONTROL_STATUS:
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001111 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1112 return;
1113
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001114 dwc->ep0state = EP0_STATUS_PHASE;
1115
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001116 if (dwc->delayed_status) {
1117 WARN_ON_ONCE(event->endpoint_number != 1);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001118 return;
1119 }
1120
Felipe Balbi788a23f2012-05-21 14:22:41 +03001121 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001122 }
1123}
1124
1125void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001126 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001127{
Felipe Balbi72246da2011-08-19 18:10:58 +03001128 switch (event->endpoint_event) {
1129 case DWC3_DEPEVT_XFERCOMPLETE:
1130 dwc3_ep0_xfer_complete(dwc, event);
1131 break;
1132
1133 case DWC3_DEPEVT_XFERNOTREADY:
1134 dwc3_ep0_xfernotready(dwc, event);
1135 break;
1136
1137 case DWC3_DEPEVT_XFERINPROGRESS:
1138 case DWC3_DEPEVT_RXTXFIFOEVT:
1139 case DWC3_DEPEVT_STREAMEVT:
1140 case DWC3_DEPEVT_EPCMDCMPLT:
1141 break;
1142 }
1143}