blob: bff2c35f37a86bcc6aca5d2b690e2aec04f528b0 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
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/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi73815282015-01-27 13:48:14 -0600142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144
145 return -ETIMEDOUT;
146}
147
Felipe Balbief966b92016-04-05 13:09:51 +0300148static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200149{
Felipe Balbief966b92016-04-05 13:09:51 +0300150 dep->trb_enqueue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300151 dep->trb_enqueue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300152}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200153
Felipe Balbief966b92016-04-05 13:09:51 +0300154static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
155{
156 dep->trb_dequeue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300157 dep->trb_dequeue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbief966b92016-04-05 13:09:51 +0300160static int dwc3_ep_is_last_trb(unsigned int index)
161{
Felipe Balbi4faf7552016-04-05 13:14:31 +0300162 return index == DWC3_TRB_NUM - 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200163}
164
Felipe Balbi72246da2011-08-19 18:10:58 +0300165void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
166 int status)
167{
168 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530169 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300170
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200171 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530172 i = 0;
173 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300174 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530175 /*
176 * Skip LINK TRB. We can't use req->trb and check for
177 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
178 * just completed (not the LINK TRB).
179 */
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300180 if (dwc3_ep_is_last_trb(dep->trb_dequeue))
Felipe Balbief966b92016-04-05 13:09:51 +0300181 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530182 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200183 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 }
185 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200186 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 if (req->request.status == -EINPROGRESS)
189 req->request.status = status;
190
Pratyush Anand0416e492012-08-10 13:42:16 +0530191 if (dwc->ep0_bounced && dep->number == 0)
192 dwc->ep0_bounced = false;
193 else
194 usb_gadget_unmap_request(&dwc->gadget, &req->request,
195 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500197 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198
199 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200200 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 spin_lock(&dwc->lock);
202}
203
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500204int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300205{
206 u32 timeout = 500;
207 u32 reg;
208
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500209 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500210
Felipe Balbib09bb642012-04-24 16:19:11 +0300211 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
212 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
213
214 do {
215 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
216 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600217 dwc3_trace(trace_dwc3_gadget,
218 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300219 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530220 if (DWC3_DGCMD_STATUS(reg))
221 return -EINVAL;
Felipe Balbib09bb642012-04-24 16:19:11 +0300222 return 0;
223 }
224
225 /*
226 * We can't sleep here, because it's also called from
227 * interrupt context.
228 */
229 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600230 if (!timeout) {
231 dwc3_trace(trace_dwc3_gadget,
232 "Command Timed Out");
Felipe Balbib09bb642012-04-24 16:19:11 +0300233 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600234 }
Felipe Balbib09bb642012-04-24 16:19:11 +0300235 udelay(1);
236 } while (1);
237}
238
Felipe Balbic36d8e92016-04-04 12:46:33 +0300239static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
240
Felipe Balbi72246da2011-08-19 18:10:58 +0300241int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
242 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
243{
244 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200245 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 u32 reg;
247
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300248 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300249 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300250
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500251 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300252
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300253 /*
254 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
255 * we're issuing an endpoint command, we must check if
256 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
257 *
258 * We will also set SUSPHY bit to what it was before returning as stated
259 * by the same section on Synopsys databook.
260 */
261 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
262 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
263 susphy = true;
264 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
265 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
266 }
267
Felipe Balbic36d8e92016-04-04 12:46:33 +0300268 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
269 int needs_wakeup;
270
271 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
272 dwc->link_state == DWC3_LINK_STATE_U2 ||
273 dwc->link_state == DWC3_LINK_STATE_U3);
274
275 if (unlikely(needs_wakeup)) {
276 ret = __dwc3_gadget_wakeup(dwc);
277 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
278 ret);
279 }
280 }
281
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300282 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
283 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
284 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
286 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
287 do {
288 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
289 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000290 int cmd_status = DWC3_DEPCMD_STATUS(reg);
291
Felipe Balbi73815282015-01-27 13:48:14 -0600292 dwc3_trace(trace_dwc3_gadget,
293 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000294 cmd_status);
295
296 switch (cmd_status) {
297 case 0:
298 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300299 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000300 case DEPEVT_TRANSFER_NO_RESOURCE:
301 dwc3_trace(trace_dwc3_gadget, "%s: no resource available");
302 ret = -EINVAL;
303 break;
304 case DEPEVT_TRANSFER_BUS_EXPIRY:
305 /*
306 * SW issues START TRANSFER command to
307 * isochronous ep with future frame interval. If
308 * future interval time has already passed when
309 * core receives the command, it will respond
310 * with an error status of 'Bus Expiry'.
311 *
312 * Instead of always returning -EINVAL, let's
313 * give a hint to the gadget driver that this is
314 * the case by returning -EAGAIN.
315 */
316 dwc3_trace(trace_dwc3_gadget, "%s: bus expiry");
317 ret = -EAGAIN;
318 break;
319 default:
320 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
321 }
322
Felipe Balbic0ca3242016-04-04 09:11:51 +0300323 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300324 }
325
326 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 * We can't sleep here, because it is also called from
328 * interrupt context.
329 */
330 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600331 if (!timeout) {
332 dwc3_trace(trace_dwc3_gadget,
333 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334 ret = -ETIMEDOUT;
335 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600336 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300338
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300339 if (unlikely(susphy)) {
340 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
341 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
342 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
343 }
344
Felipe Balbic0ca3242016-04-04 09:11:51 +0300345 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300346}
347
John Youn50c763f2016-05-31 17:49:56 -0700348static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
349{
350 struct dwc3 *dwc = dep->dwc;
351 struct dwc3_gadget_ep_cmd_params params;
352 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
353
354 /*
355 * As of core revision 2.60a the recommended programming model
356 * is to set the ClearPendIN bit when issuing a Clear Stall EP
357 * command for IN endpoints. This is to prevent an issue where
358 * some (non-compliant) hosts may not send ACK TPs for pending
359 * IN transfers due to a mishandled error condition. Synopsys
360 * STAR 9000614252.
361 */
362 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
363 cmd |= DWC3_DEPCMD_CLEARPENDIN;
364
365 memset(&params, 0, sizeof(params));
366
367 return dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
368}
369
Felipe Balbi72246da2011-08-19 18:10:58 +0300370static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200371 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300372{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300373 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300374
375 return dep->trb_pool_dma + offset;
376}
377
378static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
379{
380 struct dwc3 *dwc = dep->dwc;
381
382 if (dep->trb_pool)
383 return 0;
384
Felipe Balbi72246da2011-08-19 18:10:58 +0300385 dep->trb_pool = dma_alloc_coherent(dwc->dev,
386 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
387 &dep->trb_pool_dma, GFP_KERNEL);
388 if (!dep->trb_pool) {
389 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
390 dep->name);
391 return -ENOMEM;
392 }
393
394 return 0;
395}
396
397static void dwc3_free_trb_pool(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400
401 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
402 dep->trb_pool, dep->trb_pool_dma);
403
404 dep->trb_pool = NULL;
405 dep->trb_pool_dma = 0;
406}
407
John Younc4509602016-02-16 20:10:53 -0800408static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
409
410/**
411 * dwc3_gadget_start_config - Configure EP resources
412 * @dwc: pointer to our controller context structure
413 * @dep: endpoint that is being enabled
414 *
415 * The assignment of transfer resources cannot perfectly follow the
416 * data book due to the fact that the controller driver does not have
417 * all knowledge of the configuration in advance. It is given this
418 * information piecemeal by the composite gadget framework after every
419 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
420 * programming model in this scenario can cause errors. For two
421 * reasons:
422 *
423 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
424 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
425 * multiple interfaces.
426 *
427 * 2) The databook does not mention doing more DEPXFERCFG for new
428 * endpoint on alt setting (8.1.6).
429 *
430 * The following simplified method is used instead:
431 *
432 * All hardware endpoints can be assigned a transfer resource and this
433 * setting will stay persistent until either a core reset or
434 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
435 * do DEPXFERCFG for every hardware endpoint as well. We are
436 * guaranteed that there are as many transfer resources as endpoints.
437 *
438 * This function is called for each endpoint when it is being enabled
439 * but is triggered only when called for EP0-out, which always happens
440 * first, and which should only happen in one of the above conditions.
441 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300442static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
443{
444 struct dwc3_gadget_ep_cmd_params params;
445 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800446 int i;
447 int ret;
448
449 if (dep->number)
450 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
452 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800453 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300454
John Younc4509602016-02-16 20:10:53 -0800455 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
456 if (ret)
457 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300458
John Younc4509602016-02-16 20:10:53 -0800459 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
460 struct dwc3_ep *dep = dwc->eps[i];
461
462 if (!dep)
463 continue;
464
465 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
466 if (ret)
467 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 }
469
470 return 0;
471}
472
473static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200474 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300475 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600476 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300477{
478 struct dwc3_gadget_ep_cmd_params params;
479
480 memset(&params, 0x00, sizeof(params));
481
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300482 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900483 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
484
485 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800486 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300487 u32 burst = dep->endpoint.maxburst;
488 u32 nump;
489 u32 reg;
Chanho Parkd2e9a132012-08-31 16:54:07 +0900490
Felipe Balbi676e3492016-04-26 10:49:07 +0300491 /* update NumP */
492 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
493 nump = DWC3_DCFG_NUMP(reg);
494 nump = max(nump, burst);
495 reg &= ~DWC3_DCFG_NUMP_MASK;
496 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
497 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
498
499 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900500 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300501
Felipe Balbi4b345c92012-07-16 14:08:16 +0300502 if (ignore)
503 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
504
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600505 if (restore) {
506 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
507 params.param2 |= dep->saved_state;
508 }
509
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300510 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
511 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300512
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200513 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300514 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
515 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300516 dep->stream_capable = true;
517 }
518
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500519 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300520 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521
522 /*
523 * We are doing 1:1 mapping for endpoints, meaning
524 * Physical Endpoints 2 maps to Logical Endpoint 2 and
525 * so on. We consider the direction bit as part of the physical
526 * endpoint number. So USB endpoint 0x81 is 0x03.
527 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300528 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300529
530 /*
531 * We must use the lower 16 TX FIFOs even though
532 * HW might have more
533 */
534 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300535 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300536
537 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300538 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 dep->interval = 1 << (desc->bInterval - 1);
540 }
541
542 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
543 DWC3_DEPCMD_SETEPCONFIG, &params);
544}
545
546static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
547{
548 struct dwc3_gadget_ep_cmd_params params;
549
550 memset(&params, 0x00, sizeof(params));
551
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300552 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300553
554 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
555 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
556}
557
558/**
559 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
560 * @dep: endpoint to be initialized
561 * @desc: USB Endpoint Descriptor
562 *
563 * Caller should take care of locking
564 */
565static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200566 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300567 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600568 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300569{
570 struct dwc3 *dwc = dep->dwc;
571 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300572 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
Felipe Balbi73815282015-01-27 13:48:14 -0600574 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300575
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 if (!(dep->flags & DWC3_EP_ENABLED)) {
577 ret = dwc3_gadget_start_config(dwc, dep);
578 if (ret)
579 return ret;
580 }
581
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600582 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
583 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 if (ret)
585 return ret;
586
587 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200588 struct dwc3_trb *trb_st_hw;
589 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200591 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200592 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 dep->type = usb_endpoint_type(desc);
594 dep->flags |= DWC3_EP_ENABLED;
595
596 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
597 reg |= DWC3_DALEPENA_EP(dep->number);
598 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
599
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300600 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200601 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300602
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300603 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 trb_st_hw = &dep->trb_pool[0];
605
Felipe Balbif6bafc62012-02-06 11:04:53 +0200606 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700607 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300608
Felipe Balbif6bafc62012-02-06 11:04:53 +0200609 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
610 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
611 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
612 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 }
614
Felipe Balbie901aa12016-03-16 14:01:37 +0200615out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500616 switch (usb_endpoint_type(desc)) {
617 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200618 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500619 break;
620 case USB_ENDPOINT_XFER_ISOC:
621 strlcat(dep->name, "-isoc", sizeof(dep->name));
622 break;
623 case USB_ENDPOINT_XFER_BULK:
624 strlcat(dep->name, "-bulk", sizeof(dep->name));
625 break;
626 case USB_ENDPOINT_XFER_INT:
627 strlcat(dep->name, "-int", sizeof(dep->name));
628 break;
629 default:
630 dev_err(dwc->dev, "invalid endpoint transfer type\n");
631 }
632
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 return 0;
634}
635
Paul Zimmermanb992e682012-04-27 14:17:35 +0300636static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200637static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300638{
639 struct dwc3_request *req;
640
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200641 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300642 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200643
Pratyush Anand57911502012-07-06 15:19:10 +0530644 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200645 while (!list_empty(&dep->started_list)) {
646 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530647
648 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
649 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200650 }
651
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200652 while (!list_empty(&dep->pending_list)) {
653 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300654
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200655 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300657}
658
659/**
660 * __dwc3_gadget_ep_disable - Disables a HW endpoint
661 * @dep: the endpoint to disable
662 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200663 * This function also removes requests which are currently processed ny the
664 * hardware and those which are not yet scheduled.
665 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300666 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300667static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
668{
669 struct dwc3 *dwc = dep->dwc;
670 u32 reg;
671
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500672 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
673
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200674 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300675
Felipe Balbi687ef982014-04-16 10:30:33 -0500676 /* make sure HW endpoint isn't stalled */
677 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500678 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500679
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
681 reg &= ~DWC3_DALEPENA_EP(dep->number);
682 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
683
Felipe Balbi879631a2011-09-30 10:58:47 +0300684 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200685 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200686 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300688 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300689
Felipe Balbiaa739972015-07-20 14:48:13 -0500690 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
691 dep->number >> 1,
692 (dep->number & 1) ? "in" : "out");
693
Felipe Balbi72246da2011-08-19 18:10:58 +0300694 return 0;
695}
696
697/* -------------------------------------------------------------------------- */
698
699static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
700 const struct usb_endpoint_descriptor *desc)
701{
702 return -EINVAL;
703}
704
705static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
706{
707 return -EINVAL;
708}
709
710/* -------------------------------------------------------------------------- */
711
712static int dwc3_gadget_ep_enable(struct usb_ep *ep,
713 const struct usb_endpoint_descriptor *desc)
714{
715 struct dwc3_ep *dep;
716 struct dwc3 *dwc;
717 unsigned long flags;
718 int ret;
719
720 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
721 pr_debug("dwc3: invalid parameters\n");
722 return -EINVAL;
723 }
724
725 if (!desc->wMaxPacketSize) {
726 pr_debug("dwc3: missing wMaxPacketSize\n");
727 return -EINVAL;
728 }
729
730 dep = to_dwc3_ep(ep);
731 dwc = dep->dwc;
732
Felipe Balbi95ca9612015-12-10 13:08:20 -0600733 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
734 "%s is already enabled\n",
735 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300736 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300737
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600739 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 spin_unlock_irqrestore(&dwc->lock, flags);
741
742 return ret;
743}
744
745static int dwc3_gadget_ep_disable(struct usb_ep *ep)
746{
747 struct dwc3_ep *dep;
748 struct dwc3 *dwc;
749 unsigned long flags;
750 int ret;
751
752 if (!ep) {
753 pr_debug("dwc3: invalid parameters\n");
754 return -EINVAL;
755 }
756
757 dep = to_dwc3_ep(ep);
758 dwc = dep->dwc;
759
Felipe Balbi95ca9612015-12-10 13:08:20 -0600760 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
761 "%s is already disabled\n",
762 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300764
Felipe Balbi72246da2011-08-19 18:10:58 +0300765 spin_lock_irqsave(&dwc->lock, flags);
766 ret = __dwc3_gadget_ep_disable(dep);
767 spin_unlock_irqrestore(&dwc->lock, flags);
768
769 return ret;
770}
771
772static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
773 gfp_t gfp_flags)
774{
775 struct dwc3_request *req;
776 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300777
778 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900779 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300780 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300781
782 req->epnum = dep->number;
783 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300784
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500785 trace_dwc3_alloc_request(req);
786
Felipe Balbi72246da2011-08-19 18:10:58 +0300787 return &req->request;
788}
789
790static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
791 struct usb_request *request)
792{
793 struct dwc3_request *req = to_dwc3_request(request);
794
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500795 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300796 kfree(req);
797}
798
Felipe Balbic71fc372011-11-22 11:37:34 +0200799/**
800 * dwc3_prepare_one_trb - setup one TRB from one request
801 * @dep: endpoint for which this request is prepared
802 * @req: dwc3_request pointer
803 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200804static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200805 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530806 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200807{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200808 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200809
Felipe Balbi73815282015-01-27 13:48:14 -0600810 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200811 dep->name, req, (unsigned long long) dma,
812 length, last ? " last" : "",
813 chain ? " chain" : "");
814
Pratyush Anand915e2022013-01-14 15:59:35 +0530815
Felipe Balbi4faf7552016-04-05 13:14:31 +0300816 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200817
Felipe Balbieeb720f2011-11-28 12:46:59 +0200818 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200819 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200820 req->trb = trb;
821 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300822 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200823 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200824
Felipe Balbief966b92016-04-05 13:09:51 +0300825 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300826 /* Skip the LINK-TRB */
827 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300828 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530829
Felipe Balbif6bafc62012-02-06 11:04:53 +0200830 trb->size = DWC3_TRB_SIZE_LENGTH(length);
831 trb->bpl = lower_32_bits(dma);
832 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200833
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200834 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200835 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200836 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200837 break;
838
839 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530840 if (!node)
841 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
842 else
843 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200844
845 /* always enable Interrupt on Missed ISOC */
846 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200847 break;
848
849 case USB_ENDPOINT_XFER_BULK:
850 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200851 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200852 break;
853 default:
854 /*
855 * This is only possible with faulty memory because we
856 * checked it already :)
857 */
858 BUG();
859 }
860
Felipe Balbica4d44e2016-03-10 13:53:27 +0200861 /* always enable Continue on Short Packet */
862 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600863
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300864 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200865 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
866
867 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530868 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200869
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530870 if (chain)
871 trb->ctrl |= DWC3_TRB_CTRL_CHN;
872
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200873 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200874 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
875
876 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500877
878 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200879}
880
Felipe Balbic4233572016-05-12 14:08:34 +0300881static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
882{
883 struct dwc3_trb *tmp;
884
885 /*
886 * If enqueue & dequeue are equal than it is either full or empty.
887 *
888 * One way to know for sure is if the TRB right before us has HWO bit
889 * set or not. If it has, then we're definitely full and can't fit any
890 * more transfers in our ring.
891 */
892 if (dep->trb_enqueue == dep->trb_dequeue) {
893 /* If we're full, enqueue/dequeue are > 0 */
894 if (dep->trb_enqueue) {
895 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
896 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
897 return 0;
898 }
899
900 return DWC3_TRB_NUM - 1;
901 }
902
903 return dep->trb_dequeue - dep->trb_enqueue;
904}
905
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300906static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
907 struct dwc3_request *req, unsigned int trbs_left)
908{
909 struct usb_request *request = &req->request;
910 struct scatterlist *sg = request->sg;
911 struct scatterlist *s;
912 unsigned int last = false;
913 unsigned int length;
914 dma_addr_t dma;
915 int i;
916
917 for_each_sg(sg, s, request->num_mapped_sgs, i) {
918 unsigned chain = true;
919
920 length = sg_dma_len(s);
921 dma = sg_dma_address(s);
922
923 if (sg_is_last(s)) {
924 if (list_is_last(&req->list, &dep->pending_list))
925 last = true;
926
927 chain = false;
928 }
929
930 if (!trbs_left)
931 last = true;
932
933 if (last)
934 chain = false;
935
936 dwc3_prepare_one_trb(dep, req, dma, length,
937 last, chain, i);
938
939 if (last)
940 break;
941 }
942}
943
944static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
945 struct dwc3_request *req, unsigned int trbs_left)
946{
947 unsigned int last = false;
948 unsigned int length;
949 dma_addr_t dma;
950
951 dma = req->request.dma;
952 length = req->request.length;
953
954 if (!trbs_left)
955 last = true;
956
957 /* Is this the last request? */
958 if (list_is_last(&req->list, &dep->pending_list))
959 last = true;
960
961 dwc3_prepare_one_trb(dep, req, dma, length,
962 last, false, 0);
963}
964
Felipe Balbi72246da2011-08-19 18:10:58 +0300965/*
966 * dwc3_prepare_trbs - setup TRBs from requests
967 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300968 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800969 * The function goes through the requests list and sets up TRBs for the
970 * transfers. The function returns once there are no more TRBs available or
971 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 */
Felipe Balbic4233572016-05-12 14:08:34 +0300973static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300974{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200975 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300976 u32 trbs_left;
977
978 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
979
Felipe Balbic4233572016-05-12 14:08:34 +0300980 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300981
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200982 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300983 if (req->request.num_mapped_sgs > 0)
984 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
985 else
986 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300987
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300988 if (!trbs_left)
989 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300991}
992
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300993static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300994{
995 struct dwc3_gadget_ep_cmd_params params;
996 struct dwc3_request *req;
997 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300998 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 int ret;
1000 u32 cmd;
1001
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001002 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001003
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001004 dwc3_prepare_trbs(dep);
1005 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001006 if (!req) {
1007 dep->flags |= DWC3_EP_PENDING_REQUEST;
1008 return 0;
1009 }
1010
1011 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001012
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001013 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301014 params.param0 = upper_32_bits(req->trb_dma);
1015 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301017 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001018 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301019 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001020
1021 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1022 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1023 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001024 /*
1025 * FIXME we need to iterate over the list of requests
1026 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001027 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001028 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001029 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1030 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001031 list_del(&req->list);
1032 return ret;
1033 }
1034
1035 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001036
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001037 if (starting) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001038 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001039 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001040 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001041 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001042
Felipe Balbi72246da2011-08-19 18:10:58 +03001043 return 0;
1044}
1045
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301046static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1047 struct dwc3_ep *dep, u32 cur_uf)
1048{
1049 u32 uf;
1050
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001051 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001052 dwc3_trace(trace_dwc3_gadget,
1053 "ISOC ep %s run out for requests",
1054 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301055 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301056 return;
1057 }
1058
1059 /* 4 micro frames in the future */
1060 uf = cur_uf + dep->interval * 4;
1061
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001062 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301063}
1064
1065static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1066 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1067{
1068 u32 cur_uf, mask;
1069
1070 mask = ~(dep->interval - 1);
1071 cur_uf = event->parameters & mask;
1072
1073 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1074}
1075
Felipe Balbi72246da2011-08-19 18:10:58 +03001076static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1077{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001078 struct dwc3 *dwc = dep->dwc;
1079 int ret;
1080
Felipe Balbibb423982015-11-16 15:31:21 -06001081 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001082 dwc3_trace(trace_dwc3_gadget,
1083 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001084 &req->request, dep->endpoint.name);
1085 return -ESHUTDOWN;
1086 }
1087
1088 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1089 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001090 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1091 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001092 return -EINVAL;
1093 }
1094
Felipe Balbi72246da2011-08-19 18:10:58 +03001095 req->request.actual = 0;
1096 req->request.status = -EINPROGRESS;
1097 req->direction = dep->direction;
1098 req->epnum = dep->number;
1099
Felipe Balbife84f522015-09-01 09:01:38 -05001100 trace_dwc3_ep_queue(req);
1101
Felipe Balbi72246da2011-08-19 18:10:58 +03001102 /*
1103 * We only add to our list of requests now and
1104 * start consuming the list once we get XferNotReady
1105 * IRQ.
1106 *
1107 * That way, we avoid doing anything that we don't need
1108 * to do now and defer it until the point we receive a
1109 * particular token from the Host side.
1110 *
1111 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001112 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001113 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001114 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1115 dep->direction);
1116 if (ret)
1117 return ret;
1118
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001119 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001120
1121 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001122 * If there are no pending requests and the endpoint isn't already
1123 * busy, we will just start the request straight away.
1124 *
1125 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1126 * little bit faster.
1127 */
1128 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001129 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001130 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001131 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001132 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001133 }
1134
1135 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001136 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001137 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001138 * 1. XferNotReady with empty list of requests. We need to kick the
1139 * transfer here in that situation, otherwise we will be NAKing
1140 * forever. If we get XferNotReady before gadget driver has a
1141 * chance to queue a request, we will ACK the IRQ but won't be
1142 * able to receive the data until the next request is queued.
1143 * The following code is handling exactly that.
1144 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 */
1146 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301147 /*
1148 * If xfernotready is already elapsed and it is a case
1149 * of isoc transfer, then issue END TRANSFER, so that
1150 * you can receive xfernotready again and can have
1151 * notion of current microframe.
1152 */
1153 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001154 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001155 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301156 dep->flags = DWC3_EP_ENABLED;
1157 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301158 return 0;
1159 }
1160
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001161 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001162 if (!ret)
1163 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1164
Felipe Balbia8f32812015-09-16 10:40:07 -05001165 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001166 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001167
Felipe Balbib511e5e2012-06-06 12:00:50 +03001168 /*
1169 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1170 * kick the transfer here after queuing a request, otherwise the
1171 * core may not see the modified TRB(s).
1172 */
1173 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301174 (dep->flags & DWC3_EP_BUSY) &&
1175 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001176 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001177 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001178 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001179 }
1180
Felipe Balbib997ada2012-07-26 13:26:50 +03001181 /*
1182 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1183 * right away, otherwise host will not know we have streams to be
1184 * handled.
1185 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001186 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001187 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001188
Felipe Balbia8f32812015-09-16 10:40:07 -05001189out:
1190 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001191 dwc3_trace(trace_dwc3_gadget,
1192 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001193 dep->name);
1194 if (ret == -EBUSY)
1195 ret = 0;
1196
1197 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001198}
1199
Felipe Balbi04c03d12015-12-02 10:06:45 -06001200static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1201 struct usb_request *request)
1202{
1203 dwc3_gadget_ep_free_request(ep, request);
1204}
1205
1206static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1207{
1208 struct dwc3_request *req;
1209 struct usb_request *request;
1210 struct usb_ep *ep = &dep->endpoint;
1211
1212 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1213 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1214 if (!request)
1215 return -ENOMEM;
1216
1217 request->length = 0;
1218 request->buf = dwc->zlp_buf;
1219 request->complete = __dwc3_gadget_ep_zlp_complete;
1220
1221 req = to_dwc3_request(request);
1222
1223 return __dwc3_gadget_ep_queue(dep, req);
1224}
1225
Felipe Balbi72246da2011-08-19 18:10:58 +03001226static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1227 gfp_t gfp_flags)
1228{
1229 struct dwc3_request *req = to_dwc3_request(request);
1230 struct dwc3_ep *dep = to_dwc3_ep(ep);
1231 struct dwc3 *dwc = dep->dwc;
1232
1233 unsigned long flags;
1234
1235 int ret;
1236
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001237 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001238 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001239
1240 /*
1241 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1242 * setting request->zero, instead of doing magic, we will just queue an
1243 * extra usb_request ourselves so that it gets handled the same way as
1244 * any other request.
1245 */
John Yound92618982015-12-22 12:23:20 -08001246 if (ret == 0 && request->zero && request->length &&
1247 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001248 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1249
Felipe Balbi72246da2011-08-19 18:10:58 +03001250 spin_unlock_irqrestore(&dwc->lock, flags);
1251
1252 return ret;
1253}
1254
1255static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1256 struct usb_request *request)
1257{
1258 struct dwc3_request *req = to_dwc3_request(request);
1259 struct dwc3_request *r = NULL;
1260
1261 struct dwc3_ep *dep = to_dwc3_ep(ep);
1262 struct dwc3 *dwc = dep->dwc;
1263
1264 unsigned long flags;
1265 int ret = 0;
1266
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001267 trace_dwc3_ep_dequeue(req);
1268
Felipe Balbi72246da2011-08-19 18:10:58 +03001269 spin_lock_irqsave(&dwc->lock, flags);
1270
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001271 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 if (r == req)
1273 break;
1274 }
1275
1276 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001277 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001278 if (r == req)
1279 break;
1280 }
1281 if (r == req) {
1282 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001283 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301284 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001285 }
1286 dev_err(dwc->dev, "request %p was not queued to %s\n",
1287 request, ep->name);
1288 ret = -EINVAL;
1289 goto out0;
1290 }
1291
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301292out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001293 /* giveback the request */
1294 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1295
1296out0:
1297 spin_unlock_irqrestore(&dwc->lock, flags);
1298
1299 return ret;
1300}
1301
Felipe Balbi7a608552014-09-24 14:19:52 -05001302int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001303{
1304 struct dwc3_gadget_ep_cmd_params params;
1305 struct dwc3 *dwc = dep->dwc;
1306 int ret;
1307
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001308 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1309 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1310 return -EINVAL;
1311 }
1312
Felipe Balbi72246da2011-08-19 18:10:58 +03001313 memset(&params, 0x00, sizeof(params));
1314
1315 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001316 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001317 (!list_empty(&dep->started_list) ||
1318 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001319 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001320 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001321 dep->name);
1322 return -EAGAIN;
1323 }
1324
Felipe Balbi72246da2011-08-19 18:10:58 +03001325 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1326 DWC3_DEPCMD_SETSTALL, &params);
1327 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001328 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 dep->name);
1330 else
1331 dep->flags |= DWC3_EP_STALL;
1332 } else {
John Youn50c763f2016-05-31 17:49:56 -07001333 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001335 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 dep->name);
1337 else
Alan Sterna535d812013-11-01 12:05:12 -04001338 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001340
Felipe Balbi72246da2011-08-19 18:10:58 +03001341 return ret;
1342}
1343
1344static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1345{
1346 struct dwc3_ep *dep = to_dwc3_ep(ep);
1347 struct dwc3 *dwc = dep->dwc;
1348
1349 unsigned long flags;
1350
1351 int ret;
1352
1353 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001354 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001355 spin_unlock_irqrestore(&dwc->lock, flags);
1356
1357 return ret;
1358}
1359
1360static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1361{
1362 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001363 struct dwc3 *dwc = dep->dwc;
1364 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001365 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001366
Paul Zimmerman249a4562012-02-24 17:32:16 -08001367 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 dep->flags |= DWC3_EP_WEDGE;
1369
Pratyush Anand08f0d962012-06-25 22:40:43 +05301370 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001371 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301372 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001373 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001374 spin_unlock_irqrestore(&dwc->lock, flags);
1375
1376 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001377}
1378
1379/* -------------------------------------------------------------------------- */
1380
1381static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1382 .bLength = USB_DT_ENDPOINT_SIZE,
1383 .bDescriptorType = USB_DT_ENDPOINT,
1384 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1385};
1386
1387static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1388 .enable = dwc3_gadget_ep0_enable,
1389 .disable = dwc3_gadget_ep0_disable,
1390 .alloc_request = dwc3_gadget_ep_alloc_request,
1391 .free_request = dwc3_gadget_ep_free_request,
1392 .queue = dwc3_gadget_ep0_queue,
1393 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301394 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001395 .set_wedge = dwc3_gadget_ep_set_wedge,
1396};
1397
1398static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1399 .enable = dwc3_gadget_ep_enable,
1400 .disable = dwc3_gadget_ep_disable,
1401 .alloc_request = dwc3_gadget_ep_alloc_request,
1402 .free_request = dwc3_gadget_ep_free_request,
1403 .queue = dwc3_gadget_ep_queue,
1404 .dequeue = dwc3_gadget_ep_dequeue,
1405 .set_halt = dwc3_gadget_ep_set_halt,
1406 .set_wedge = dwc3_gadget_ep_set_wedge,
1407};
1408
1409/* -------------------------------------------------------------------------- */
1410
1411static int dwc3_gadget_get_frame(struct usb_gadget *g)
1412{
1413 struct dwc3 *dwc = gadget_to_dwc(g);
1414 u32 reg;
1415
1416 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1417 return DWC3_DSTS_SOFFN(reg);
1418}
1419
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001420static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001421{
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001423
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001424 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001425 u32 reg;
1426
Felipe Balbi72246da2011-08-19 18:10:58 +03001427 u8 link_state;
1428 u8 speed;
1429
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 /*
1431 * According to the Databook Remote wakeup request should
1432 * be issued only when the device is in early suspend state.
1433 *
1434 * We can check that via USB Link State bits in DSTS register.
1435 */
1436 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1437
1438 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001439 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1440 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001441 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001442 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001443 }
1444
1445 link_state = DWC3_DSTS_USBLNKST(reg);
1446
1447 switch (link_state) {
1448 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1449 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1450 break;
1451 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001452 dwc3_trace(trace_dwc3_gadget,
1453 "can't wakeup from '%s'\n",
1454 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001455 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 }
1457
Felipe Balbi8598bde2012-01-02 18:55:57 +02001458 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1459 if (ret < 0) {
1460 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001461 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001462 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001463
Paul Zimmerman802fde92012-04-27 13:10:52 +03001464 /* Recent versions do this automatically */
1465 if (dwc->revision < DWC3_REVISION_194A) {
1466 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001467 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001468 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1469 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1470 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001471
Paul Zimmerman1d046792012-02-15 18:56:56 -08001472 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001473 timeout = jiffies + msecs_to_jiffies(100);
1474
Paul Zimmerman1d046792012-02-15 18:56:56 -08001475 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001476 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1477
1478 /* in HS, means ON */
1479 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1480 break;
1481 }
1482
1483 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1484 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001485 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001486 }
1487
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001488 return 0;
1489}
1490
1491static int dwc3_gadget_wakeup(struct usb_gadget *g)
1492{
1493 struct dwc3 *dwc = gadget_to_dwc(g);
1494 unsigned long flags;
1495 int ret;
1496
1497 spin_lock_irqsave(&dwc->lock, flags);
1498 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001499 spin_unlock_irqrestore(&dwc->lock, flags);
1500
1501 return ret;
1502}
1503
1504static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1505 int is_selfpowered)
1506{
1507 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001508 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001509
Paul Zimmerman249a4562012-02-24 17:32:16 -08001510 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001511 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001512 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001513
1514 return 0;
1515}
1516
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001517static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001518{
1519 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001520 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001521
1522 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001523 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001524 if (dwc->revision <= DWC3_REVISION_187A) {
1525 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1526 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1527 }
1528
1529 if (dwc->revision >= DWC3_REVISION_194A)
1530 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1531 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001532
1533 if (dwc->has_hibernation)
1534 reg |= DWC3_DCTL_KEEP_CONNECT;
1535
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001536 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001537 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001538 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001539
1540 if (dwc->has_hibernation && !suspend)
1541 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1542
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001543 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001544 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001545
1546 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1547
1548 do {
1549 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1550 if (is_on) {
1551 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1552 break;
1553 } else {
1554 if (reg & DWC3_DSTS_DEVCTRLHLT)
1555 break;
1556 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001557 timeout--;
1558 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301559 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001560 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001561 } while (1);
1562
Felipe Balbi73815282015-01-27 13:48:14 -06001563 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001564 dwc->gadget_driver
1565 ? dwc->gadget_driver->function : "no-function",
1566 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301567
1568 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001569}
1570
1571static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1572{
1573 struct dwc3 *dwc = gadget_to_dwc(g);
1574 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301575 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001576
1577 is_on = !!is_on;
1578
1579 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001580 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001581 spin_unlock_irqrestore(&dwc->lock, flags);
1582
Pratyush Anand6f17f742012-07-02 10:21:55 +05301583 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001584}
1585
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001586static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1587{
1588 u32 reg;
1589
1590 /* Enable all but Start and End of Frame IRQs */
1591 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1592 DWC3_DEVTEN_EVNTOVERFLOWEN |
1593 DWC3_DEVTEN_CMDCMPLTEN |
1594 DWC3_DEVTEN_ERRTICERREN |
1595 DWC3_DEVTEN_WKUPEVTEN |
1596 DWC3_DEVTEN_ULSTCNGEN |
1597 DWC3_DEVTEN_CONNECTDONEEN |
1598 DWC3_DEVTEN_USBRSTEN |
1599 DWC3_DEVTEN_DISCONNEVTEN);
1600
1601 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1602}
1603
1604static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1605{
1606 /* mask all interrupts */
1607 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1608}
1609
1610static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001611static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001612
Felipe Balbid7be2952016-05-04 15:49:37 +03001613static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001614{
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001616 int ret = 0;
1617 u32 reg;
1618
Felipe Balbi72246da2011-08-19 18:10:58 +03001619 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1620 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001621
1622 /**
1623 * WORKAROUND: DWC3 revision < 2.20a have an issue
1624 * which would cause metastability state on Run/Stop
1625 * bit if we try to force the IP to USB2-only mode.
1626 *
1627 * Because of that, we cannot configure the IP to any
1628 * speed other than the SuperSpeed
1629 *
1630 * Refers to:
1631 *
1632 * STAR#9000525659: Clock Domain Crossing on DCTL in
1633 * USB 2.0 Mode
1634 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001635 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001636 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001637 } else {
1638 switch (dwc->maximum_speed) {
1639 case USB_SPEED_LOW:
1640 reg |= DWC3_DSTS_LOWSPEED;
1641 break;
1642 case USB_SPEED_FULL:
1643 reg |= DWC3_DSTS_FULLSPEED1;
1644 break;
1645 case USB_SPEED_HIGH:
1646 reg |= DWC3_DSTS_HIGHSPEED;
1647 break;
John Youn75808622016-02-05 17:09:13 -08001648 case USB_SPEED_SUPER_PLUS:
1649 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1650 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001651 default:
John Youn77966eb2016-02-19 17:31:01 -08001652 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1653 dwc->maximum_speed);
1654 /* fall through */
1655 case USB_SPEED_SUPER:
1656 reg |= DWC3_DCFG_SUPERSPEED;
1657 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001658 }
1659 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1661
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001662 /*
1663 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1664 * field instead of letting dwc3 itself calculate that automatically.
1665 *
1666 * This way, we maximize the chances that we'll be able to get several
1667 * bursts of data without going through any sort of endpoint throttling.
1668 */
1669 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1670 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1671 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1672
Felipe Balbi72246da2011-08-19 18:10:58 +03001673 /* Start with SuperSpeed Default */
1674 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1675
1676 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001677 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1678 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001679 if (ret) {
1680 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001681 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001682 }
1683
1684 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001685 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1686 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 if (ret) {
1688 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001689 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001690 }
1691
1692 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001693 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001694 dwc3_ep0_out_start(dwc);
1695
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001696 dwc3_gadget_enable_irq(dwc);
1697
Felipe Balbid7be2952016-05-04 15:49:37 +03001698 return 0;
1699
1700err1:
1701 __dwc3_gadget_ep_disable(dwc->eps[0]);
1702
1703err0:
1704 return ret;
1705}
1706
1707static int dwc3_gadget_start(struct usb_gadget *g,
1708 struct usb_gadget_driver *driver)
1709{
1710 struct dwc3 *dwc = gadget_to_dwc(g);
1711 unsigned long flags;
1712 int ret = 0;
1713 int irq;
1714
1715 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1716 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1717 IRQF_SHARED, "dwc3", dwc->ev_buf);
1718 if (ret) {
1719 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1720 irq, ret);
1721 goto err0;
1722 }
1723
1724 spin_lock_irqsave(&dwc->lock, flags);
1725 if (dwc->gadget_driver) {
1726 dev_err(dwc->dev, "%s is already bound to %s\n",
1727 dwc->gadget.name,
1728 dwc->gadget_driver->driver.name);
1729 ret = -EBUSY;
1730 goto err1;
1731 }
1732
1733 dwc->gadget_driver = driver;
1734
1735 __dwc3_gadget_start(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001736 spin_unlock_irqrestore(&dwc->lock, flags);
1737
1738 return 0;
1739
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001740err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001741 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001742 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001743
1744err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001745 return ret;
1746}
1747
Felipe Balbid7be2952016-05-04 15:49:37 +03001748static void __dwc3_gadget_stop(struct dwc3 *dwc)
1749{
1750 dwc3_gadget_disable_irq(dwc);
1751 __dwc3_gadget_ep_disable(dwc->eps[0]);
1752 __dwc3_gadget_ep_disable(dwc->eps[1]);
1753}
1754
Felipe Balbi22835b82014-10-17 12:05:12 -05001755static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001756{
1757 struct dwc3 *dwc = gadget_to_dwc(g);
1758 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001759 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001760
1761 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001762 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001763 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 spin_unlock_irqrestore(&dwc->lock, flags);
1765
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001766 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
Felipe Balbidea520a2016-03-30 09:39:34 +03001767 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001768
Felipe Balbi72246da2011-08-19 18:10:58 +03001769 return 0;
1770}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001771
Felipe Balbi72246da2011-08-19 18:10:58 +03001772static const struct usb_gadget_ops dwc3_gadget_ops = {
1773 .get_frame = dwc3_gadget_get_frame,
1774 .wakeup = dwc3_gadget_wakeup,
1775 .set_selfpowered = dwc3_gadget_set_selfpowered,
1776 .pullup = dwc3_gadget_pullup,
1777 .udc_start = dwc3_gadget_start,
1778 .udc_stop = dwc3_gadget_stop,
1779};
1780
1781/* -------------------------------------------------------------------------- */
1782
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001783static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1784 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001785{
1786 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001787 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001788
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001789 for (i = 0; i < num; i++) {
1790 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001791
Felipe Balbi72246da2011-08-19 18:10:58 +03001792 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001793 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001794 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001795
1796 dep->dwc = dwc;
1797 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001798 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001799 dwc->eps[epnum] = dep;
1800
1801 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1802 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001803
Felipe Balbi72246da2011-08-19 18:10:58 +03001804 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001805
Felipe Balbi73815282015-01-27 13:48:14 -06001806 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001807
Felipe Balbi72246da2011-08-19 18:10:58 +03001808 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001809 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301810 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001811 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1812 if (!epnum)
1813 dwc->gadget.ep0 = &dep->endpoint;
1814 } else {
1815 int ret;
1816
Robert Baldygae117e742013-12-13 12:23:38 +01001817 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001818 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001819 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1820 list_add_tail(&dep->endpoint.ep_list,
1821 &dwc->gadget.ep_list);
1822
1823 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001824 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001825 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001826 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001827
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001828 if (epnum == 0 || epnum == 1) {
1829 dep->endpoint.caps.type_control = true;
1830 } else {
1831 dep->endpoint.caps.type_iso = true;
1832 dep->endpoint.caps.type_bulk = true;
1833 dep->endpoint.caps.type_int = true;
1834 }
1835
1836 dep->endpoint.caps.dir_in = !!direction;
1837 dep->endpoint.caps.dir_out = !direction;
1838
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001839 INIT_LIST_HEAD(&dep->pending_list);
1840 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001841 }
1842
1843 return 0;
1844}
1845
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001846static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1847{
1848 int ret;
1849
1850 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1851
1852 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1853 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001854 dwc3_trace(trace_dwc3_gadget,
1855 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001856 return ret;
1857 }
1858
1859 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1860 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001861 dwc3_trace(trace_dwc3_gadget,
1862 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001863 return ret;
1864 }
1865
1866 return 0;
1867}
1868
Felipe Balbi72246da2011-08-19 18:10:58 +03001869static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1870{
1871 struct dwc3_ep *dep;
1872 u8 epnum;
1873
1874 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1875 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001876 if (!dep)
1877 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301878 /*
1879 * Physical endpoints 0 and 1 are special; they form the
1880 * bi-directional USB endpoint 0.
1881 *
1882 * For those two physical endpoints, we don't allocate a TRB
1883 * pool nor do we add them the endpoints list. Due to that, we
1884 * shouldn't do these two operations otherwise we would end up
1885 * with all sorts of bugs when removing dwc3.ko.
1886 */
1887 if (epnum != 0 && epnum != 1) {
1888 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301890 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001891
1892 kfree(dep);
1893 }
1894}
1895
Felipe Balbi72246da2011-08-19 18:10:58 +03001896/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001897
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301898static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1899 struct dwc3_request *req, struct dwc3_trb *trb,
1900 const struct dwc3_event_depevt *event, int status)
1901{
1902 unsigned int count;
1903 unsigned int s_pkt = 0;
1904 unsigned int trb_status;
1905
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001906 trace_dwc3_complete_trb(dep, trb);
1907
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301908 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1909 /*
1910 * We continue despite the error. There is not much we
1911 * can do. If we don't clean it up we loop forever. If
1912 * we skip the TRB then it gets overwritten after a
1913 * while since we use them in a ring buffer. A BUG()
1914 * would help. Lets hope that if this occurs, someone
1915 * fixes the root cause instead of looking away :)
1916 */
1917 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1918 dep->name, trb);
1919 count = trb->size & DWC3_TRB_SIZE_MASK;
1920
1921 if (dep->direction) {
1922 if (count) {
1923 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1924 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001925 dwc3_trace(trace_dwc3_gadget,
1926 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301927 dep->name);
1928 /*
1929 * If missed isoc occurred and there is
1930 * no request queued then issue END
1931 * TRANSFER, so that core generates
1932 * next xfernotready and we will issue
1933 * a fresh START TRANSFER.
1934 * If there are still queued request
1935 * then wait, do not issue either END
1936 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001937 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301938 * giveback.If any future queued request
1939 * is successfully transferred then we
1940 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001941 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301942 */
1943 dep->flags |= DWC3_EP_MISSED_ISOC;
1944 } else {
1945 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1946 dep->name);
1947 status = -ECONNRESET;
1948 }
1949 } else {
1950 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1951 }
1952 } else {
1953 if (count && (event->status & DEPEVT_STATUS_SHORT))
1954 s_pkt = 1;
1955 }
1956
1957 /*
1958 * We assume here we will always receive the entire data block
1959 * which we should receive. Meaning, if we program RX to
1960 * receive 4K but we receive only 2K, we assume that's all we
1961 * should receive and we simply bounce the request back to the
1962 * gadget driver for further processing.
1963 */
1964 req->request.actual += req->request.length - count;
1965 if (s_pkt)
1966 return 1;
1967 if ((event->status & DEPEVT_STATUS_LST) &&
1968 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1969 DWC3_TRB_CTRL_HWO)))
1970 return 1;
1971 if ((event->status & DEPEVT_STATUS_IOC) &&
1972 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1973 return 1;
1974 return 0;
1975}
1976
Felipe Balbi72246da2011-08-19 18:10:58 +03001977static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1978 const struct dwc3_event_depevt *event, int status)
1979{
1980 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001981 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301982 unsigned int slot;
1983 unsigned int i;
1984 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001985
1986 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001987 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001988 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001989 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001990
Ville Syrjäläd115d702015-08-31 19:48:28 +03001991 i = 0;
1992 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03001993 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03001994 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03001995 slot++;
1996 slot %= DWC3_TRB_NUM;
1997 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001998
Ville Syrjäläd115d702015-08-31 19:48:28 +03001999 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2000 event, status);
2001 if (ret)
2002 break;
2003 } while (++i < req->request.num_mapped_sgs);
2004
2005 dwc3_gadget_giveback(dep, req, status);
2006
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302007 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002008 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002009 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002010
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302011 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002012 list_empty(&dep->started_list)) {
2013 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302014 /*
2015 * If there is no entry in request list then do
2016 * not issue END TRANSFER now. Just set PENDING
2017 * flag, so that END TRANSFER is issued when an
2018 * entry is added into request list.
2019 */
2020 dep->flags = DWC3_EP_PENDING_REQUEST;
2021 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002022 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302023 dep->flags = DWC3_EP_ENABLED;
2024 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302025 return 1;
2026 }
2027
Felipe Balbi72246da2011-08-19 18:10:58 +03002028 return 1;
2029}
2030
2031static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002032 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002033{
2034 unsigned status = 0;
2035 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002036 u32 is_xfer_complete;
2037
2038 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002039
2040 if (event->status & DEPEVT_STATUS_BUSERR)
2041 status = -ECONNRESET;
2042
Paul Zimmerman1d046792012-02-15 18:56:56 -08002043 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05002044 if (clean_busy && (is_xfer_complete ||
2045 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002046 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002047
2048 /*
2049 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2050 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2051 */
2052 if (dwc->revision < DWC3_REVISION_183A) {
2053 u32 reg;
2054 int i;
2055
2056 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002057 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002058
2059 if (!(dep->flags & DWC3_EP_ENABLED))
2060 continue;
2061
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002062 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002063 return;
2064 }
2065
2066 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2067 reg |= dwc->u1u2;
2068 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2069
2070 dwc->u1u2 = 0;
2071 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002072
Felipe Balbie6e709b2015-09-28 15:16:56 -05002073 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002074 int ret;
2075
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002076 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002077 if (!ret || ret == -EBUSY)
2078 return;
2079 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002080}
2081
Felipe Balbi72246da2011-08-19 18:10:58 +03002082static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2083 const struct dwc3_event_depevt *event)
2084{
2085 struct dwc3_ep *dep;
2086 u8 epnum = event->endpoint_number;
2087
2088 dep = dwc->eps[epnum];
2089
Felipe Balbi3336abb2012-06-06 09:19:35 +03002090 if (!(dep->flags & DWC3_EP_ENABLED))
2091 return;
2092
Felipe Balbi72246da2011-08-19 18:10:58 +03002093 if (epnum == 0 || epnum == 1) {
2094 dwc3_ep0_interrupt(dwc, event);
2095 return;
2096 }
2097
2098 switch (event->endpoint_event) {
2099 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002100 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002101
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002102 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002103 dwc3_trace(trace_dwc3_gadget,
2104 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002105 dep->name);
2106 return;
2107 }
2108
Jingoo Han029d97f2014-07-04 15:00:51 +09002109 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002110 break;
2111 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002112 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002113 break;
2114 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002115 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002116 dwc3_gadget_start_isoc(dwc, dep, event);
2117 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002118 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 int ret;
2120
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002121 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2122
Felipe Balbi73815282015-01-27 13:48:14 -06002123 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002124 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002125 : "Transfer Not Active");
2126
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002127 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002128 if (!ret || ret == -EBUSY)
2129 return;
2130
Felipe Balbiec5e7952015-11-16 16:04:13 -06002131 dwc3_trace(trace_dwc3_gadget,
2132 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002133 dep->name);
2134 }
2135
2136 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002137 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002138 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002139 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2140 dep->name);
2141 return;
2142 }
2143
2144 switch (event->status) {
2145 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002146 dwc3_trace(trace_dwc3_gadget,
2147 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002148 event->parameters);
2149
2150 break;
2151 case DEPEVT_STREAMEVT_NOTFOUND:
2152 /* FALLTHROUGH */
2153 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002154 dwc3_trace(trace_dwc3_gadget,
2155 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002156 }
2157 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002158 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002159 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002161 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002162 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002163 break;
2164 }
2165}
2166
2167static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2168{
2169 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2170 spin_unlock(&dwc->lock);
2171 dwc->gadget_driver->disconnect(&dwc->gadget);
2172 spin_lock(&dwc->lock);
2173 }
2174}
2175
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002176static void dwc3_suspend_gadget(struct dwc3 *dwc)
2177{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002178 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002179 spin_unlock(&dwc->lock);
2180 dwc->gadget_driver->suspend(&dwc->gadget);
2181 spin_lock(&dwc->lock);
2182 }
2183}
2184
2185static void dwc3_resume_gadget(struct dwc3 *dwc)
2186{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002187 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002188 spin_unlock(&dwc->lock);
2189 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002190 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002191 }
2192}
2193
2194static void dwc3_reset_gadget(struct dwc3 *dwc)
2195{
2196 if (!dwc->gadget_driver)
2197 return;
2198
2199 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2200 spin_unlock(&dwc->lock);
2201 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002202 spin_lock(&dwc->lock);
2203 }
2204}
2205
Paul Zimmermanb992e682012-04-27 14:17:35 +03002206static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002207{
2208 struct dwc3_ep *dep;
2209 struct dwc3_gadget_ep_cmd_params params;
2210 u32 cmd;
2211 int ret;
2212
2213 dep = dwc->eps[epnum];
2214
Felipe Balbib4996a82012-06-06 12:04:13 +03002215 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302216 return;
2217
Pratyush Anand57911502012-07-06 15:19:10 +05302218 /*
2219 * NOTICE: We are violating what the Databook says about the
2220 * EndTransfer command. Ideally we would _always_ wait for the
2221 * EndTransfer Command Completion IRQ, but that's causing too
2222 * much trouble synchronizing between us and gadget driver.
2223 *
2224 * We have discussed this with the IP Provider and it was
2225 * suggested to giveback all requests here, but give HW some
2226 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002227 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302228 *
2229 * Note also that a similar handling was tested by Synopsys
2230 * (thanks a lot Paul) and nothing bad has come out of it.
2231 * In short, what we're doing is:
2232 *
2233 * - Issue EndTransfer WITH CMDIOC bit set
2234 * - Wait 100us
2235 */
2236
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302237 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002238 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2239 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002240 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302241 memset(&params, 0, sizeof(params));
2242 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2243 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002244 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002245 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302246 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002247}
2248
2249static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2250{
2251 u32 epnum;
2252
2253 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2254 struct dwc3_ep *dep;
2255
2256 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002257 if (!dep)
2258 continue;
2259
Felipe Balbi72246da2011-08-19 18:10:58 +03002260 if (!(dep->flags & DWC3_EP_ENABLED))
2261 continue;
2262
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002263 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002264 }
2265}
2266
2267static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2268{
2269 u32 epnum;
2270
2271 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2272 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002273 int ret;
2274
2275 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002276 if (!dep)
2277 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002278
2279 if (!(dep->flags & DWC3_EP_STALL))
2280 continue;
2281
2282 dep->flags &= ~DWC3_EP_STALL;
2283
John Youn50c763f2016-05-31 17:49:56 -07002284 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002285 WARN_ON_ONCE(ret);
2286 }
2287}
2288
2289static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2290{
Felipe Balbic4430a22012-05-24 10:30:01 +03002291 int reg;
2292
Felipe Balbi72246da2011-08-19 18:10:58 +03002293 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2294 reg &= ~DWC3_DCTL_INITU1ENA;
2295 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2296
2297 reg &= ~DWC3_DCTL_INITU2ENA;
2298 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002299
Felipe Balbi72246da2011-08-19 18:10:58 +03002300 dwc3_disconnect_gadget(dwc);
2301
2302 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002303 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002304 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002305}
2306
Felipe Balbi72246da2011-08-19 18:10:58 +03002307static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2308{
2309 u32 reg;
2310
Felipe Balbidf62df52011-10-14 15:11:49 +03002311 /*
2312 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2313 * would cause a missing Disconnect Event if there's a
2314 * pending Setup Packet in the FIFO.
2315 *
2316 * There's no suggested workaround on the official Bug
2317 * report, which states that "unless the driver/application
2318 * is doing any special handling of a disconnect event,
2319 * there is no functional issue".
2320 *
2321 * Unfortunately, it turns out that we _do_ some special
2322 * handling of a disconnect event, namely complete all
2323 * pending transfers, notify gadget driver of the
2324 * disconnection, and so on.
2325 *
2326 * Our suggested workaround is to follow the Disconnect
2327 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002328 * flag. Such flag gets set whenever we have a SETUP_PENDING
2329 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002330 * same endpoint.
2331 *
2332 * Refers to:
2333 *
2334 * STAR#9000466709: RTL: Device : Disconnect event not
2335 * generated if setup packet pending in FIFO
2336 */
2337 if (dwc->revision < DWC3_REVISION_188A) {
2338 if (dwc->setup_packet_pending)
2339 dwc3_gadget_disconnect_interrupt(dwc);
2340 }
2341
Felipe Balbi8e744752014-11-06 14:27:53 +08002342 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002343
2344 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2345 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2346 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002347 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002348
2349 dwc3_stop_active_transfers(dwc);
2350 dwc3_clear_stall_all_ep(dwc);
2351
2352 /* Reset device address to zero */
2353 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2354 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2355 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002356}
2357
2358static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2359{
2360 u32 reg;
2361 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2362
2363 /*
2364 * We change the clock only at SS but I dunno why I would want to do
2365 * this. Maybe it becomes part of the power saving plan.
2366 */
2367
John Younee5cd412016-02-05 17:08:45 -08002368 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2369 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002370 return;
2371
2372 /*
2373 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2374 * each time on Connect Done.
2375 */
2376 if (!usb30_clock)
2377 return;
2378
2379 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2380 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2381 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2382}
2383
Felipe Balbi72246da2011-08-19 18:10:58 +03002384static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2385{
Felipe Balbi72246da2011-08-19 18:10:58 +03002386 struct dwc3_ep *dep;
2387 int ret;
2388 u32 reg;
2389 u8 speed;
2390
Felipe Balbi72246da2011-08-19 18:10:58 +03002391 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2392 speed = reg & DWC3_DSTS_CONNECTSPD;
2393 dwc->speed = speed;
2394
2395 dwc3_update_ram_clk_sel(dwc, speed);
2396
2397 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002398 case DWC3_DCFG_SUPERSPEED_PLUS:
2399 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2400 dwc->gadget.ep0->maxpacket = 512;
2401 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2402 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002403 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002404 /*
2405 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2406 * would cause a missing USB3 Reset event.
2407 *
2408 * In such situations, we should force a USB3 Reset
2409 * event by calling our dwc3_gadget_reset_interrupt()
2410 * routine.
2411 *
2412 * Refers to:
2413 *
2414 * STAR#9000483510: RTL: SS : USB3 reset event may
2415 * not be generated always when the link enters poll
2416 */
2417 if (dwc->revision < DWC3_REVISION_190A)
2418 dwc3_gadget_reset_interrupt(dwc);
2419
Felipe Balbi72246da2011-08-19 18:10:58 +03002420 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2421 dwc->gadget.ep0->maxpacket = 512;
2422 dwc->gadget.speed = USB_SPEED_SUPER;
2423 break;
2424 case DWC3_DCFG_HIGHSPEED:
2425 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2426 dwc->gadget.ep0->maxpacket = 64;
2427 dwc->gadget.speed = USB_SPEED_HIGH;
2428 break;
2429 case DWC3_DCFG_FULLSPEED2:
2430 case DWC3_DCFG_FULLSPEED1:
2431 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2432 dwc->gadget.ep0->maxpacket = 64;
2433 dwc->gadget.speed = USB_SPEED_FULL;
2434 break;
2435 case DWC3_DCFG_LOWSPEED:
2436 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2437 dwc->gadget.ep0->maxpacket = 8;
2438 dwc->gadget.speed = USB_SPEED_LOW;
2439 break;
2440 }
2441
Pratyush Anand2b758352013-01-14 15:59:31 +05302442 /* Enable USB2 LPM Capability */
2443
John Younee5cd412016-02-05 17:08:45 -08002444 if ((dwc->revision > DWC3_REVISION_194A) &&
2445 (speed != DWC3_DCFG_SUPERSPEED) &&
2446 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302447 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2448 reg |= DWC3_DCFG_LPM_CAP;
2449 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2450
2451 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2452 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2453
Huang Rui460d0982014-10-31 11:11:18 +08002454 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302455
Huang Rui80caf7d2014-10-28 19:54:26 +08002456 /*
2457 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2458 * DCFG.LPMCap is set, core responses with an ACK and the
2459 * BESL value in the LPM token is less than or equal to LPM
2460 * NYET threshold.
2461 */
2462 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2463 && dwc->has_lpm_erratum,
2464 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2465
2466 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2467 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2468
Pratyush Anand2b758352013-01-14 15:59:31 +05302469 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002470 } else {
2471 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2472 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2473 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302474 }
2475
Felipe Balbi72246da2011-08-19 18:10:58 +03002476 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002477 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2478 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002479 if (ret) {
2480 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2481 return;
2482 }
2483
2484 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002485 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2486 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 if (ret) {
2488 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2489 return;
2490 }
2491
2492 /*
2493 * Configure PHY via GUSB3PIPECTLn if required.
2494 *
2495 * Update GTXFIFOSIZn
2496 *
2497 * In both cases reset values should be sufficient.
2498 */
2499}
2500
2501static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2502{
Felipe Balbi72246da2011-08-19 18:10:58 +03002503 /*
2504 * TODO take core out of low power mode when that's
2505 * implemented.
2506 */
2507
Jiebing Liad14d4e2014-12-11 13:26:29 +08002508 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2509 spin_unlock(&dwc->lock);
2510 dwc->gadget_driver->resume(&dwc->gadget);
2511 spin_lock(&dwc->lock);
2512 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002513}
2514
2515static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2516 unsigned int evtinfo)
2517{
Felipe Balbifae2b902011-10-14 13:00:30 +03002518 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002519 unsigned int pwropt;
2520
2521 /*
2522 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2523 * Hibernation mode enabled which would show up when device detects
2524 * host-initiated U3 exit.
2525 *
2526 * In that case, device will generate a Link State Change Interrupt
2527 * from U3 to RESUME which is only necessary if Hibernation is
2528 * configured in.
2529 *
2530 * There are no functional changes due to such spurious event and we
2531 * just need to ignore it.
2532 *
2533 * Refers to:
2534 *
2535 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2536 * operational mode
2537 */
2538 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2539 if ((dwc->revision < DWC3_REVISION_250A) &&
2540 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2541 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2542 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002543 dwc3_trace(trace_dwc3_gadget,
2544 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002545 return;
2546 }
2547 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002548
2549 /*
2550 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2551 * on the link partner, the USB session might do multiple entry/exit
2552 * of low power states before a transfer takes place.
2553 *
2554 * Due to this problem, we might experience lower throughput. The
2555 * suggested workaround is to disable DCTL[12:9] bits if we're
2556 * transitioning from U1/U2 to U0 and enable those bits again
2557 * after a transfer completes and there are no pending transfers
2558 * on any of the enabled endpoints.
2559 *
2560 * This is the first half of that workaround.
2561 *
2562 * Refers to:
2563 *
2564 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2565 * core send LGO_Ux entering U0
2566 */
2567 if (dwc->revision < DWC3_REVISION_183A) {
2568 if (next == DWC3_LINK_STATE_U0) {
2569 u32 u1u2;
2570 u32 reg;
2571
2572 switch (dwc->link_state) {
2573 case DWC3_LINK_STATE_U1:
2574 case DWC3_LINK_STATE_U2:
2575 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2576 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2577 | DWC3_DCTL_ACCEPTU2ENA
2578 | DWC3_DCTL_INITU1ENA
2579 | DWC3_DCTL_ACCEPTU1ENA);
2580
2581 if (!dwc->u1u2)
2582 dwc->u1u2 = reg & u1u2;
2583
2584 reg &= ~u1u2;
2585
2586 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2587 break;
2588 default:
2589 /* do nothing */
2590 break;
2591 }
2592 }
2593 }
2594
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002595 switch (next) {
2596 case DWC3_LINK_STATE_U1:
2597 if (dwc->speed == USB_SPEED_SUPER)
2598 dwc3_suspend_gadget(dwc);
2599 break;
2600 case DWC3_LINK_STATE_U2:
2601 case DWC3_LINK_STATE_U3:
2602 dwc3_suspend_gadget(dwc);
2603 break;
2604 case DWC3_LINK_STATE_RESUME:
2605 dwc3_resume_gadget(dwc);
2606 break;
2607 default:
2608 /* do nothing */
2609 break;
2610 }
2611
Felipe Balbie57ebc12014-04-22 13:20:12 -05002612 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002613}
2614
Felipe Balbie1dadd32014-02-25 14:47:54 -06002615static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2616 unsigned int evtinfo)
2617{
2618 unsigned int is_ss = evtinfo & BIT(4);
2619
2620 /**
2621 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2622 * have a known issue which can cause USB CV TD.9.23 to fail
2623 * randomly.
2624 *
2625 * Because of this issue, core could generate bogus hibernation
2626 * events which SW needs to ignore.
2627 *
2628 * Refers to:
2629 *
2630 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2631 * Device Fallback from SuperSpeed
2632 */
2633 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2634 return;
2635
2636 /* enter hibernation here */
2637}
2638
Felipe Balbi72246da2011-08-19 18:10:58 +03002639static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2640 const struct dwc3_event_devt *event)
2641{
2642 switch (event->type) {
2643 case DWC3_DEVICE_EVENT_DISCONNECT:
2644 dwc3_gadget_disconnect_interrupt(dwc);
2645 break;
2646 case DWC3_DEVICE_EVENT_RESET:
2647 dwc3_gadget_reset_interrupt(dwc);
2648 break;
2649 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2650 dwc3_gadget_conndone_interrupt(dwc);
2651 break;
2652 case DWC3_DEVICE_EVENT_WAKEUP:
2653 dwc3_gadget_wakeup_interrupt(dwc);
2654 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002655 case DWC3_DEVICE_EVENT_HIBER_REQ:
2656 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2657 "unexpected hibernation event\n"))
2658 break;
2659
2660 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2661 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002662 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2663 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2664 break;
2665 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002666 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002667 break;
2668 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002669 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002670 break;
2671 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002672 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002673 break;
2674 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002675 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002676 break;
2677 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002678 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002679 break;
2680 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002681 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 }
2683}
2684
2685static void dwc3_process_event_entry(struct dwc3 *dwc,
2686 const union dwc3_event *event)
2687{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002688 trace_dwc3_event(event->raw);
2689
Felipe Balbi72246da2011-08-19 18:10:58 +03002690 /* Endpoint IRQ, handle it and return early */
2691 if (event->type.is_devspec == 0) {
2692 /* depevt */
2693 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2694 }
2695
2696 switch (event->type.type) {
2697 case DWC3_EVENT_TYPE_DEV:
2698 dwc3_gadget_interrupt(dwc, &event->devt);
2699 break;
2700 /* REVISIT what to do with Carkit and I2C events ? */
2701 default:
2702 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2703 }
2704}
2705
Felipe Balbidea520a2016-03-30 09:39:34 +03002706static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002707{
Felipe Balbidea520a2016-03-30 09:39:34 +03002708 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002709 irqreturn_t ret = IRQ_NONE;
2710 int left;
2711 u32 reg;
2712
Felipe Balbif42f2442013-06-12 21:25:08 +03002713 left = evt->count;
2714
2715 if (!(evt->flags & DWC3_EVENT_PENDING))
2716 return IRQ_NONE;
2717
2718 while (left > 0) {
2719 union dwc3_event event;
2720
2721 event.raw = *(u32 *) (evt->buf + evt->lpos);
2722
2723 dwc3_process_event_entry(dwc, &event);
2724
2725 /*
2726 * FIXME we wrap around correctly to the next entry as
2727 * almost all entries are 4 bytes in size. There is one
2728 * entry which has 12 bytes which is a regular entry
2729 * followed by 8 bytes data. ATM I don't know how
2730 * things are organized if we get next to the a
2731 * boundary so I worry about that once we try to handle
2732 * that.
2733 */
2734 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2735 left -= 4;
2736
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002737 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002738 }
2739
2740 evt->count = 0;
2741 evt->flags &= ~DWC3_EVENT_PENDING;
2742 ret = IRQ_HANDLED;
2743
2744 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002745 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002746 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002747 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002748
2749 return ret;
2750}
2751
Felipe Balbidea520a2016-03-30 09:39:34 +03002752static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002753{
Felipe Balbidea520a2016-03-30 09:39:34 +03002754 struct dwc3_event_buffer *evt = _evt;
2755 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002756 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002757 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002758
Felipe Balbie5f68b42015-10-12 13:25:44 -05002759 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002760 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002761 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002762
2763 return ret;
2764}
2765
Felipe Balbidea520a2016-03-30 09:39:34 +03002766static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002767{
Felipe Balbidea520a2016-03-30 09:39:34 +03002768 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002769 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002770 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002771
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002772 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002773 count &= DWC3_GEVNTCOUNT_MASK;
2774 if (!count)
2775 return IRQ_NONE;
2776
Felipe Balbib15a7622011-06-30 16:57:15 +03002777 evt->count = count;
2778 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002779
Felipe Balbie8adfc32013-06-12 21:11:14 +03002780 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002781 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002782 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002783 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002784
Felipe Balbib15a7622011-06-30 16:57:15 +03002785 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002786}
2787
Felipe Balbidea520a2016-03-30 09:39:34 +03002788static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002789{
Felipe Balbidea520a2016-03-30 09:39:34 +03002790 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002791
Felipe Balbidea520a2016-03-30 09:39:34 +03002792 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002793}
2794
2795/**
2796 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002797 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002798 *
2799 * Returns 0 on success otherwise negative errno.
2800 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002801int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002802{
Felipe Balbi72246da2011-08-19 18:10:58 +03002803 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002804
2805 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2806 &dwc->ctrl_req_addr, GFP_KERNEL);
2807 if (!dwc->ctrl_req) {
2808 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2809 ret = -ENOMEM;
2810 goto err0;
2811 }
2812
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302813 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002814 &dwc->ep0_trb_addr, GFP_KERNEL);
2815 if (!dwc->ep0_trb) {
2816 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2817 ret = -ENOMEM;
2818 goto err1;
2819 }
2820
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002821 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002822 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002823 ret = -ENOMEM;
2824 goto err2;
2825 }
2826
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002827 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002828 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2829 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002830 if (!dwc->ep0_bounce) {
2831 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2832 ret = -ENOMEM;
2833 goto err3;
2834 }
2835
Felipe Balbi04c03d12015-12-02 10:06:45 -06002836 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2837 if (!dwc->zlp_buf) {
2838 ret = -ENOMEM;
2839 goto err4;
2840 }
2841
Felipe Balbi72246da2011-08-19 18:10:58 +03002842 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002843 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002844 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002845 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002846 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002847
2848 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002849 * FIXME We might be setting max_speed to <SUPER, however versions
2850 * <2.20a of dwc3 have an issue with metastability (documented
2851 * elsewhere in this driver) which tells us we can't set max speed to
2852 * anything lower than SUPER.
2853 *
2854 * Because gadget.max_speed is only used by composite.c and function
2855 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2856 * to happen so we avoid sending SuperSpeed Capability descriptor
2857 * together with our BOS descriptor as that could confuse host into
2858 * thinking we can handle super speed.
2859 *
2860 * Note that, in fact, we won't even support GetBOS requests when speed
2861 * is less than super speed because we don't have means, yet, to tell
2862 * composite.c that we are USB 2.0 + LPM ECN.
2863 */
2864 if (dwc->revision < DWC3_REVISION_220A)
2865 dwc3_trace(trace_dwc3_gadget,
2866 "Changing max_speed on rev %08x\n",
2867 dwc->revision);
2868
2869 dwc->gadget.max_speed = dwc->maximum_speed;
2870
2871 /*
David Cohena4b9d942013-12-09 15:55:38 -08002872 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2873 * on ep out.
2874 */
2875 dwc->gadget.quirk_ep_out_aligned_size = true;
2876
2877 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 * REVISIT: Here we should clear all pending IRQs to be
2879 * sure we're starting from a well known location.
2880 */
2881
2882 ret = dwc3_gadget_init_endpoints(dwc);
2883 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002884 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002885
Felipe Balbi72246da2011-08-19 18:10:58 +03002886 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2887 if (ret) {
2888 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002889 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002890 }
2891
2892 return 0;
2893
Felipe Balbi04c03d12015-12-02 10:06:45 -06002894err5:
2895 kfree(dwc->zlp_buf);
2896
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002897err4:
David Cohene1f80462013-09-11 17:42:47 -07002898 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002899 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2900 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002901
Felipe Balbi72246da2011-08-19 18:10:58 +03002902err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002903 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002904
2905err2:
2906 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2907 dwc->ep0_trb, dwc->ep0_trb_addr);
2908
2909err1:
2910 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2911 dwc->ctrl_req, dwc->ctrl_req_addr);
2912
2913err0:
2914 return ret;
2915}
2916
Felipe Balbi7415f172012-04-30 14:56:33 +03002917/* -------------------------------------------------------------------------- */
2918
Felipe Balbi72246da2011-08-19 18:10:58 +03002919void dwc3_gadget_exit(struct dwc3 *dwc)
2920{
Felipe Balbi72246da2011-08-19 18:10:58 +03002921 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002922
Felipe Balbi72246da2011-08-19 18:10:58 +03002923 dwc3_gadget_free_endpoints(dwc);
2924
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002925 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2926 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002927
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002928 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002929 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002930
2931 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2932 dwc->ep0_trb, dwc->ep0_trb_addr);
2933
2934 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2935 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002936}
Felipe Balbi7415f172012-04-30 14:56:33 +03002937
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002938int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002939{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002940 int ret;
2941
Roger Quadros9772b472016-04-12 11:33:29 +03002942 if (!dwc->gadget_driver)
2943 return 0;
2944
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002945 ret = dwc3_gadget_run_stop(dwc, false, false);
2946 if (ret < 0)
2947 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002948
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002949 dwc3_disconnect_gadget(dwc);
2950 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002951
2952 return 0;
2953}
2954
2955int dwc3_gadget_resume(struct dwc3 *dwc)
2956{
Felipe Balbi7415f172012-04-30 14:56:33 +03002957 int ret;
2958
Roger Quadros9772b472016-04-12 11:33:29 +03002959 if (!dwc->gadget_driver)
2960 return 0;
2961
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002962 ret = __dwc3_gadget_start(dwc);
2963 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002964 goto err0;
2965
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002966 ret = dwc3_gadget_run_stop(dwc, true, false);
2967 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002968 goto err1;
2969
Felipe Balbi7415f172012-04-30 14:56:33 +03002970 return 0;
2971
2972err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002973 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002974
2975err0:
2976 return ret;
2977}