blob: 79aad1061cdc095f4606bd3c771e903cd85deed4 [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 Balbi72246da2011-08-19 18:10:58 +0300148void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
149 int status)
150{
151 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530152 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300153
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200154 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530155 i = 0;
156 do {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200157 dep->busy_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530158 /*
159 * Skip LINK TRB. We can't use req->trb and check for
160 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
161 * just completed (not the LINK TRB).
162 */
163 if (((dep->busy_slot & DWC3_TRB_MASK) ==
164 DWC3_TRB_NUM- 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200165 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530166 dep->busy_slot++;
167 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200168 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300169 }
170 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200171 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300172
173 if (req->request.status == -EINPROGRESS)
174 req->request.status = status;
175
Pratyush Anand0416e492012-08-10 13:42:16 +0530176 if (dwc->ep0_bounced && dep->number == 0)
177 dwc->ep0_bounced = false;
178 else
179 usb_gadget_unmap_request(&dwc->gadget, &req->request,
180 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300181
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500182 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300183
184 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200185 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300186 spin_lock(&dwc->lock);
187}
188
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500189int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300190{
191 u32 timeout = 500;
192 u32 reg;
193
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500194 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500195
Felipe Balbib09bb642012-04-24 16:19:11 +0300196 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
197 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
198
199 do {
200 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
201 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600202 dwc3_trace(trace_dwc3_gadget,
203 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300204 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530205 if (DWC3_DGCMD_STATUS(reg))
206 return -EINVAL;
Felipe Balbib09bb642012-04-24 16:19:11 +0300207 return 0;
208 }
209
210 /*
211 * We can't sleep here, because it's also called from
212 * interrupt context.
213 */
214 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600215 if (!timeout) {
216 dwc3_trace(trace_dwc3_gadget,
217 "Command Timed Out");
Felipe Balbib09bb642012-04-24 16:19:11 +0300218 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600219 }
Felipe Balbib09bb642012-04-24 16:19:11 +0300220 udelay(1);
221 } while (1);
222}
223
Felipe Balbi72246da2011-08-19 18:10:58 +0300224int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
225 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
226{
227 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200228 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300229 u32 reg;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300230
231 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300232 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300233
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500234 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300235
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300236 /*
237 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
238 * we're issuing an endpoint command, we must check if
239 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
240 *
241 * We will also set SUSPHY bit to what it was before returning as stated
242 * by the same section on Synopsys databook.
243 */
244 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
245 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
246 susphy = true;
247 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
248 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
249 }
250
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300251 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
252 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
253 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300254
255 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
256 do {
257 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
258 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600259 dwc3_trace(trace_dwc3_gadget,
260 "Command Complete --> %d",
Felipe Balbi164f6e12011-08-27 20:29:58 +0300261 DWC3_DEPCMD_STATUS(reg));
Subbaraya Sundeep Bhatta76e838c2015-05-21 15:46:48 +0530262 if (DWC3_DEPCMD_STATUS(reg))
Felipe Balbic0ca3242016-04-04 09:11:51 +0300263 break;
264 ret = 0;
265 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300266 }
267
268 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300269 * We can't sleep here, because it is also called from
270 * interrupt context.
271 */
272 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600273 if (!timeout) {
274 dwc3_trace(trace_dwc3_gadget,
275 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300276 ret = -ETIMEDOUT;
277 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600278 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300279
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200280 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300281 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300282
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300283 if (unlikely(susphy)) {
284 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
285 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
286 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
287 }
288
Felipe Balbic0ca3242016-04-04 09:11:51 +0300289 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300290}
291
292static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200293 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300294{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300295 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300296
297 return dep->trb_pool_dma + offset;
298}
299
300static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
301{
302 struct dwc3 *dwc = dep->dwc;
303
304 if (dep->trb_pool)
305 return 0;
306
Felipe Balbi72246da2011-08-19 18:10:58 +0300307 dep->trb_pool = dma_alloc_coherent(dwc->dev,
308 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
309 &dep->trb_pool_dma, GFP_KERNEL);
310 if (!dep->trb_pool) {
311 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
312 dep->name);
313 return -ENOMEM;
314 }
315
316 return 0;
317}
318
319static void dwc3_free_trb_pool(struct dwc3_ep *dep)
320{
321 struct dwc3 *dwc = dep->dwc;
322
323 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
324 dep->trb_pool, dep->trb_pool_dma);
325
326 dep->trb_pool = NULL;
327 dep->trb_pool_dma = 0;
328}
329
John Younc4509602016-02-16 20:10:53 -0800330static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
331
332/**
333 * dwc3_gadget_start_config - Configure EP resources
334 * @dwc: pointer to our controller context structure
335 * @dep: endpoint that is being enabled
336 *
337 * The assignment of transfer resources cannot perfectly follow the
338 * data book due to the fact that the controller driver does not have
339 * all knowledge of the configuration in advance. It is given this
340 * information piecemeal by the composite gadget framework after every
341 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
342 * programming model in this scenario can cause errors. For two
343 * reasons:
344 *
345 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
346 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
347 * multiple interfaces.
348 *
349 * 2) The databook does not mention doing more DEPXFERCFG for new
350 * endpoint on alt setting (8.1.6).
351 *
352 * The following simplified method is used instead:
353 *
354 * All hardware endpoints can be assigned a transfer resource and this
355 * setting will stay persistent until either a core reset or
356 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
357 * do DEPXFERCFG for every hardware endpoint as well. We are
358 * guaranteed that there are as many transfer resources as endpoints.
359 *
360 * This function is called for each endpoint when it is being enabled
361 * but is triggered only when called for EP0-out, which always happens
362 * first, and which should only happen in one of the above conditions.
363 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300364static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
365{
366 struct dwc3_gadget_ep_cmd_params params;
367 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800368 int i;
369 int ret;
370
371 if (dep->number)
372 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300373
374 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800375 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376
John Younc4509602016-02-16 20:10:53 -0800377 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
378 if (ret)
379 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300380
John Younc4509602016-02-16 20:10:53 -0800381 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
382 struct dwc3_ep *dep = dwc->eps[i];
383
384 if (!dep)
385 continue;
386
387 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
388 if (ret)
389 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 }
391
392 return 0;
393}
394
395static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200396 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300397 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600398 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300399{
400 struct dwc3_gadget_ep_cmd_params params;
401
402 memset(&params, 0x00, sizeof(params));
403
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300404 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900405 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
406
407 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800408 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Chanho Parkd2e9a132012-08-31 16:54:07 +0900409 u32 burst = dep->endpoint.maxburst - 1;
410
411 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
412 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300413
Felipe Balbi4b345c92012-07-16 14:08:16 +0300414 if (ignore)
415 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
416
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600417 if (restore) {
418 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
419 params.param2 |= dep->saved_state;
420 }
421
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300422 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
423 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200425 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300426 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
427 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300428 dep->stream_capable = true;
429 }
430
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500431 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300432 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300433
434 /*
435 * We are doing 1:1 mapping for endpoints, meaning
436 * Physical Endpoints 2 maps to Logical Endpoint 2 and
437 * so on. We consider the direction bit as part of the physical
438 * endpoint number. So USB endpoint 0x81 is 0x03.
439 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300440 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300441
442 /*
443 * We must use the lower 16 TX FIFOs even though
444 * HW might have more
445 */
446 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300447 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300448
449 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300450 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300451 dep->interval = 1 << (desc->bInterval - 1);
452 }
453
454 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
455 DWC3_DEPCMD_SETEPCONFIG, &params);
456}
457
458static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
459{
460 struct dwc3_gadget_ep_cmd_params params;
461
462 memset(&params, 0x00, sizeof(params));
463
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300464 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300465
466 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
467 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
468}
469
470/**
471 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
472 * @dep: endpoint to be initialized
473 * @desc: USB Endpoint Descriptor
474 *
475 * Caller should take care of locking
476 */
477static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200478 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300479 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600480 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300481{
482 struct dwc3 *dwc = dep->dwc;
483 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300484 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300485
Felipe Balbi73815282015-01-27 13:48:14 -0600486 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300487
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 if (!(dep->flags & DWC3_EP_ENABLED)) {
489 ret = dwc3_gadget_start_config(dwc, dep);
490 if (ret)
491 return ret;
492 }
493
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600494 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
495 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300496 if (ret)
497 return ret;
498
499 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200500 struct dwc3_trb *trb_st_hw;
501 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300502
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200503 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200504 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300505 dep->type = usb_endpoint_type(desc);
506 dep->flags |= DWC3_EP_ENABLED;
507
508 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
509 reg |= DWC3_DALEPENA_EP(dep->number);
510 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
511
512 if (!usb_endpoint_xfer_isoc(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200513 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
Paul Zimmerman1d046792012-02-15 18:56:56 -0800515 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300516 trb_st_hw = &dep->trb_pool[0];
517
Felipe Balbif6bafc62012-02-06 11:04:53 +0200518 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700519 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300520
Felipe Balbif6bafc62012-02-06 11:04:53 +0200521 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
522 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
523 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
524 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525 }
526
Felipe Balbie901aa12016-03-16 14:01:37 +0200527out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500528 switch (usb_endpoint_type(desc)) {
529 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200530 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500531 break;
532 case USB_ENDPOINT_XFER_ISOC:
533 strlcat(dep->name, "-isoc", sizeof(dep->name));
534 break;
535 case USB_ENDPOINT_XFER_BULK:
536 strlcat(dep->name, "-bulk", sizeof(dep->name));
537 break;
538 case USB_ENDPOINT_XFER_INT:
539 strlcat(dep->name, "-int", sizeof(dep->name));
540 break;
541 default:
542 dev_err(dwc->dev, "invalid endpoint transfer type\n");
543 }
544
Felipe Balbi72246da2011-08-19 18:10:58 +0300545 return 0;
546}
547
Paul Zimmermanb992e682012-04-27 14:17:35 +0300548static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200549static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300550{
551 struct dwc3_request *req;
552
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200553 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300554 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200555
Pratyush Anand57911502012-07-06 15:19:10 +0530556 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200557 while (!list_empty(&dep->started_list)) {
558 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530559
560 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
561 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200562 }
563
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200564 while (!list_empty(&dep->pending_list)) {
565 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200567 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300568 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300569}
570
571/**
572 * __dwc3_gadget_ep_disable - Disables a HW endpoint
573 * @dep: the endpoint to disable
574 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200575 * This function also removes requests which are currently processed ny the
576 * hardware and those which are not yet scheduled.
577 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300579static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
580{
581 struct dwc3 *dwc = dep->dwc;
582 u32 reg;
583
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500584 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
585
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200586 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300587
Felipe Balbi687ef982014-04-16 10:30:33 -0500588 /* make sure HW endpoint isn't stalled */
589 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500590 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500591
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
593 reg &= ~DWC3_DALEPENA_EP(dep->number);
594 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
595
Felipe Balbi879631a2011-09-30 10:58:47 +0300596 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200597 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200598 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300600 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300601
Felipe Balbiaa739972015-07-20 14:48:13 -0500602 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
603 dep->number >> 1,
604 (dep->number & 1) ? "in" : "out");
605
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 return 0;
607}
608
609/* -------------------------------------------------------------------------- */
610
611static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
612 const struct usb_endpoint_descriptor *desc)
613{
614 return -EINVAL;
615}
616
617static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
618{
619 return -EINVAL;
620}
621
622/* -------------------------------------------------------------------------- */
623
624static int dwc3_gadget_ep_enable(struct usb_ep *ep,
625 const struct usb_endpoint_descriptor *desc)
626{
627 struct dwc3_ep *dep;
628 struct dwc3 *dwc;
629 unsigned long flags;
630 int ret;
631
632 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
633 pr_debug("dwc3: invalid parameters\n");
634 return -EINVAL;
635 }
636
637 if (!desc->wMaxPacketSize) {
638 pr_debug("dwc3: missing wMaxPacketSize\n");
639 return -EINVAL;
640 }
641
642 dep = to_dwc3_ep(ep);
643 dwc = dep->dwc;
644
Felipe Balbi95ca9612015-12-10 13:08:20 -0600645 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
646 "%s is already enabled\n",
647 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300648 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300649
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600651 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300652 spin_unlock_irqrestore(&dwc->lock, flags);
653
654 return ret;
655}
656
657static int dwc3_gadget_ep_disable(struct usb_ep *ep)
658{
659 struct dwc3_ep *dep;
660 struct dwc3 *dwc;
661 unsigned long flags;
662 int ret;
663
664 if (!ep) {
665 pr_debug("dwc3: invalid parameters\n");
666 return -EINVAL;
667 }
668
669 dep = to_dwc3_ep(ep);
670 dwc = dep->dwc;
671
Felipe Balbi95ca9612015-12-10 13:08:20 -0600672 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
673 "%s is already disabled\n",
674 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300676
Felipe Balbi72246da2011-08-19 18:10:58 +0300677 spin_lock_irqsave(&dwc->lock, flags);
678 ret = __dwc3_gadget_ep_disable(dep);
679 spin_unlock_irqrestore(&dwc->lock, flags);
680
681 return ret;
682}
683
684static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
685 gfp_t gfp_flags)
686{
687 struct dwc3_request *req;
688 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300689
690 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900691 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300692 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300693
694 req->epnum = dep->number;
695 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300696
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500697 trace_dwc3_alloc_request(req);
698
Felipe Balbi72246da2011-08-19 18:10:58 +0300699 return &req->request;
700}
701
702static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
703 struct usb_request *request)
704{
705 struct dwc3_request *req = to_dwc3_request(request);
706
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500707 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 kfree(req);
709}
710
Felipe Balbic71fc372011-11-22 11:37:34 +0200711/**
712 * dwc3_prepare_one_trb - setup one TRB from one request
713 * @dep: endpoint for which this request is prepared
714 * @req: dwc3_request pointer
715 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200716static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200717 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530718 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200719{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200720 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200721
Felipe Balbi73815282015-01-27 13:48:14 -0600722 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200723 dep->name, req, (unsigned long long) dma,
724 length, last ? " last" : "",
725 chain ? " chain" : "");
726
Pratyush Anand915e2022013-01-14 15:59:35 +0530727
728 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200729
Felipe Balbieeb720f2011-11-28 12:46:59 +0200730 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200731 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200732 req->trb = trb;
733 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530734 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200735 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200736
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530737 dep->free_slot++;
Zhuang Jin Can5cd8c482014-05-16 05:57:57 +0800738 /* Skip the LINK-TRB on ISOC */
739 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
740 usb_endpoint_xfer_isoc(dep->endpoint.desc))
741 dep->free_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530742
Felipe Balbif6bafc62012-02-06 11:04:53 +0200743 trb->size = DWC3_TRB_SIZE_LENGTH(length);
744 trb->bpl = lower_32_bits(dma);
745 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200746
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200747 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200748 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200749 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200750 break;
751
752 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530753 if (!node)
754 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
755 else
756 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200757
758 /* always enable Interrupt on Missed ISOC */
759 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200760 break;
761
762 case USB_ENDPOINT_XFER_BULK:
763 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200764 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200765 break;
766 default:
767 /*
768 * This is only possible with faulty memory because we
769 * checked it already :)
770 */
771 BUG();
772 }
773
Felipe Balbica4d44e2016-03-10 13:53:27 +0200774 /* always enable Continue on Short Packet */
775 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600776
Felipe Balbica4d44e2016-03-10 13:53:27 +0200777 if (!req->request.no_interrupt)
778 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
779
780 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530781 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200782
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530783 if (chain)
784 trb->ctrl |= DWC3_TRB_CTRL_CHN;
785
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200786 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200787 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
788
789 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500790
791 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200792}
793
Felipe Balbi72246da2011-08-19 18:10:58 +0300794/*
795 * dwc3_prepare_trbs - setup TRBs from requests
796 * @dep: endpoint for which requests are being prepared
797 * @starting: true if the endpoint is idle and no requests are queued.
798 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800799 * The function goes through the requests list and sets up TRBs for the
800 * transfers. The function returns once there are no more TRBs available or
801 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300802 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200803static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300804{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200805 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200807 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200808 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300809
810 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
811
812 /* the first request must not be queued */
813 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200814
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200815 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200816 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200817 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
818 if (trbs_left > max)
819 trbs_left = max;
820 }
821
Felipe Balbi72246da2011-08-19 18:10:58 +0300822 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800823 * If busy & slot are equal than it is either full or empty. If we are
824 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300825 * full and don't do anything
826 */
827 if (!trbs_left) {
828 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200829 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300830 trbs_left = DWC3_TRB_NUM;
831 /*
832 * In case we start from scratch, we queue the ISOC requests
833 * starting from slot 1. This is done because we use ring
834 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800835 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300836 * after the first request so we start at slot 1 and have
837 * 7 requests proceed before we hit the first IOC.
838 * Other transfer types don't use the ring buffer and are
839 * processed from the first TRB until the last one. Since we
840 * don't wrap around we have to start at the beginning.
841 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200842 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 dep->busy_slot = 1;
844 dep->free_slot = 1;
845 } else {
846 dep->busy_slot = 0;
847 dep->free_slot = 0;
848 }
849 }
850
851 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200852 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200853 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300854
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200855 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200856 unsigned length;
857 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530858 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300859
Felipe Balbieeb720f2011-11-28 12:46:59 +0200860 if (req->request.num_mapped_sgs > 0) {
861 struct usb_request *request = &req->request;
862 struct scatterlist *sg = request->sg;
863 struct scatterlist *s;
864 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300865
Felipe Balbieeb720f2011-11-28 12:46:59 +0200866 for_each_sg(sg, s, request->num_mapped_sgs, i) {
867 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300868
Felipe Balbieeb720f2011-11-28 12:46:59 +0200869 length = sg_dma_len(s);
870 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300871
Paul Zimmerman1d046792012-02-15 18:56:56 -0800872 if (i == (request->num_mapped_sgs - 1) ||
873 sg_is_last(s)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200874 if (list_empty(&dep->pending_list))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530875 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200876 chain = false;
877 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300878
Felipe Balbieeb720f2011-11-28 12:46:59 +0200879 trbs_left--;
880 if (!trbs_left)
881 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300882
Felipe Balbieeb720f2011-11-28 12:46:59 +0200883 if (last_one)
884 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300885
Felipe Balbieeb720f2011-11-28 12:46:59 +0200886 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530887 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300888
Felipe Balbieeb720f2011-11-28 12:46:59 +0200889 if (last_one)
890 break;
891 }
Amit Virdi39e60632015-01-13 14:27:21 +0530892
893 if (last_one)
894 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300895 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200896 dma = req->request.dma;
897 length = req->request.length;
898 trbs_left--;
899
900 if (!trbs_left)
901 last_one = 1;
902
903 /* Is this the last request? */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200904 if (list_is_last(&req->list, &dep->pending_list))
Felipe Balbieeb720f2011-11-28 12:46:59 +0200905 last_one = 1;
906
907 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530908 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200909
910 if (last_one)
911 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300912 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300913 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300914}
915
916static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
917 int start_new)
918{
919 struct dwc3_gadget_ep_cmd_params params;
920 struct dwc3_request *req;
921 struct dwc3 *dwc = dep->dwc;
922 int ret;
923 u32 cmd;
924
925 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600926 dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300927 return -EBUSY;
928 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300929
930 /*
931 * If we are getting here after a short-out-packet we don't enqueue any
932 * new requests as we try to set the IOC bit only on the last request.
933 */
934 if (start_new) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200935 if (list_empty(&dep->started_list))
Felipe Balbi72246da2011-08-19 18:10:58 +0300936 dwc3_prepare_trbs(dep, start_new);
937
938 /* req points to the first request which will be sent */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200939 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300940 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200941 dwc3_prepare_trbs(dep, start_new);
942
Felipe Balbi72246da2011-08-19 18:10:58 +0300943 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800944 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200946 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 }
948 if (!req) {
949 dep->flags |= DWC3_EP_PENDING_REQUEST;
950 return 0;
951 }
952
953 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300954
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530955 if (start_new) {
956 params.param0 = upper_32_bits(req->trb_dma);
957 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530959 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300960 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530961 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300962
963 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
964 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
965 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300966 /*
967 * FIXME we need to iterate over the list of requests
968 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800969 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200971 usb_gadget_unmap_request(&dwc->gadget, &req->request,
972 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300973 list_del(&req->list);
974 return ret;
975 }
976
977 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200978
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000979 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +0300980 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000981 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +0300982 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000983 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200984
Felipe Balbi72246da2011-08-19 18:10:58 +0300985 return 0;
986}
987
Pratyush Anandd6d6ec72012-05-25 18:54:56 +0530988static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
989 struct dwc3_ep *dep, u32 cur_uf)
990{
991 u32 uf;
992
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200993 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600994 dwc3_trace(trace_dwc3_gadget,
995 "ISOC ep %s run out for requests",
996 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +0530997 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +0530998 return;
999 }
1000
1001 /* 4 micro frames in the future */
1002 uf = cur_uf + dep->interval * 4;
1003
1004 __dwc3_gadget_kick_transfer(dep, uf, 1);
1005}
1006
1007static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1008 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1009{
1010 u32 cur_uf, mask;
1011
1012 mask = ~(dep->interval - 1);
1013 cur_uf = event->parameters & mask;
1014
1015 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1016}
1017
Felipe Balbi72246da2011-08-19 18:10:58 +03001018static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1019{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001020 struct dwc3 *dwc = dep->dwc;
1021 int ret;
1022
Felipe Balbibb423982015-11-16 15:31:21 -06001023 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001024 dwc3_trace(trace_dwc3_gadget,
1025 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001026 &req->request, dep->endpoint.name);
1027 return -ESHUTDOWN;
1028 }
1029
1030 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1031 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001032 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1033 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001034 return -EINVAL;
1035 }
1036
Felipe Balbi72246da2011-08-19 18:10:58 +03001037 req->request.actual = 0;
1038 req->request.status = -EINPROGRESS;
1039 req->direction = dep->direction;
1040 req->epnum = dep->number;
1041
Felipe Balbife84f522015-09-01 09:01:38 -05001042 trace_dwc3_ep_queue(req);
1043
Felipe Balbi72246da2011-08-19 18:10:58 +03001044 /*
1045 * We only add to our list of requests now and
1046 * start consuming the list once we get XferNotReady
1047 * IRQ.
1048 *
1049 * That way, we avoid doing anything that we don't need
1050 * to do now and defer it until the point we receive a
1051 * particular token from the Host side.
1052 *
1053 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001054 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001055 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001056 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1057 dep->direction);
1058 if (ret)
1059 return ret;
1060
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001061 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001062
1063 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001064 * If there are no pending requests and the endpoint isn't already
1065 * busy, we will just start the request straight away.
1066 *
1067 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1068 * little bit faster.
1069 */
1070 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001071 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001072 !(dep->flags & DWC3_EP_BUSY)) {
1073 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbia8f32812015-09-16 10:40:07 -05001074 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001075 }
1076
1077 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001078 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001079 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001080 * 1. XferNotReady with empty list of requests. We need to kick the
1081 * transfer here in that situation, otherwise we will be NAKing
1082 * forever. If we get XferNotReady before gadget driver has a
1083 * chance to queue a request, we will ACK the IRQ but won't be
1084 * able to receive the data until the next request is queued.
1085 * The following code is handling exactly that.
1086 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001087 */
1088 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301089 /*
1090 * If xfernotready is already elapsed and it is a case
1091 * of isoc transfer, then issue END TRANSFER, so that
1092 * you can receive xfernotready again and can have
1093 * notion of current microframe.
1094 */
1095 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001096 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001097 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301098 dep->flags = DWC3_EP_ENABLED;
1099 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301100 return 0;
1101 }
1102
Felipe Balbib511e5e2012-06-06 12:00:50 +03001103 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbi89185912015-09-15 09:49:14 -05001104 if (!ret)
1105 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1106
Felipe Balbia8f32812015-09-16 10:40:07 -05001107 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001108 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001109
Felipe Balbib511e5e2012-06-06 12:00:50 +03001110 /*
1111 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1112 * kick the transfer here after queuing a request, otherwise the
1113 * core may not see the modified TRB(s).
1114 */
1115 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301116 (dep->flags & DWC3_EP_BUSY) &&
1117 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001118 WARN_ON_ONCE(!dep->resource_index);
1119 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001120 false);
Felipe Balbia8f32812015-09-16 10:40:07 -05001121 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001122 }
1123
Felipe Balbib997ada2012-07-26 13:26:50 +03001124 /*
1125 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1126 * right away, otherwise host will not know we have streams to be
1127 * handled.
1128 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001129 if (dep->stream_capable)
Felipe Balbib997ada2012-07-26 13:26:50 +03001130 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbib997ada2012-07-26 13:26:50 +03001131
Felipe Balbia8f32812015-09-16 10:40:07 -05001132out:
1133 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001134 dwc3_trace(trace_dwc3_gadget,
1135 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001136 dep->name);
1137 if (ret == -EBUSY)
1138 ret = 0;
1139
1140 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001141}
1142
Felipe Balbi04c03d12015-12-02 10:06:45 -06001143static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1144 struct usb_request *request)
1145{
1146 dwc3_gadget_ep_free_request(ep, request);
1147}
1148
1149static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1150{
1151 struct dwc3_request *req;
1152 struct usb_request *request;
1153 struct usb_ep *ep = &dep->endpoint;
1154
1155 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1156 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1157 if (!request)
1158 return -ENOMEM;
1159
1160 request->length = 0;
1161 request->buf = dwc->zlp_buf;
1162 request->complete = __dwc3_gadget_ep_zlp_complete;
1163
1164 req = to_dwc3_request(request);
1165
1166 return __dwc3_gadget_ep_queue(dep, req);
1167}
1168
Felipe Balbi72246da2011-08-19 18:10:58 +03001169static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1170 gfp_t gfp_flags)
1171{
1172 struct dwc3_request *req = to_dwc3_request(request);
1173 struct dwc3_ep *dep = to_dwc3_ep(ep);
1174 struct dwc3 *dwc = dep->dwc;
1175
1176 unsigned long flags;
1177
1178 int ret;
1179
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001180 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001181 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001182
1183 /*
1184 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1185 * setting request->zero, instead of doing magic, we will just queue an
1186 * extra usb_request ourselves so that it gets handled the same way as
1187 * any other request.
1188 */
John Yound92618982015-12-22 12:23:20 -08001189 if (ret == 0 && request->zero && request->length &&
1190 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001191 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1192
Felipe Balbi72246da2011-08-19 18:10:58 +03001193 spin_unlock_irqrestore(&dwc->lock, flags);
1194
1195 return ret;
1196}
1197
1198static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1199 struct usb_request *request)
1200{
1201 struct dwc3_request *req = to_dwc3_request(request);
1202 struct dwc3_request *r = NULL;
1203
1204 struct dwc3_ep *dep = to_dwc3_ep(ep);
1205 struct dwc3 *dwc = dep->dwc;
1206
1207 unsigned long flags;
1208 int ret = 0;
1209
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001210 trace_dwc3_ep_dequeue(req);
1211
Felipe Balbi72246da2011-08-19 18:10:58 +03001212 spin_lock_irqsave(&dwc->lock, flags);
1213
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001214 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001215 if (r == req)
1216 break;
1217 }
1218
1219 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001220 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001221 if (r == req)
1222 break;
1223 }
1224 if (r == req) {
1225 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001226 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301227 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 }
1229 dev_err(dwc->dev, "request %p was not queued to %s\n",
1230 request, ep->name);
1231 ret = -EINVAL;
1232 goto out0;
1233 }
1234
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301235out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001236 /* giveback the request */
1237 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1238
1239out0:
1240 spin_unlock_irqrestore(&dwc->lock, flags);
1241
1242 return ret;
1243}
1244
Felipe Balbi7a608552014-09-24 14:19:52 -05001245int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001246{
1247 struct dwc3_gadget_ep_cmd_params params;
1248 struct dwc3 *dwc = dep->dwc;
1249 int ret;
1250
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001251 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1252 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1253 return -EINVAL;
1254 }
1255
Felipe Balbi72246da2011-08-19 18:10:58 +03001256 memset(&params, 0x00, sizeof(params));
1257
1258 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001259 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001260 (!list_empty(&dep->started_list) ||
1261 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001262 dwc3_trace(trace_dwc3_gadget,
1263 "%s: pending request, cannot halt\n",
Felipe Balbi7a608552014-09-24 14:19:52 -05001264 dep->name);
1265 return -EAGAIN;
1266 }
1267
Felipe Balbi72246da2011-08-19 18:10:58 +03001268 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1269 DWC3_DEPCMD_SETSTALL, &params);
1270 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001271 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 dep->name);
1273 else
1274 dep->flags |= DWC3_EP_STALL;
1275 } else {
1276 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1277 DWC3_DEPCMD_CLEARSTALL, &params);
1278 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001279 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001280 dep->name);
1281 else
Alan Sterna535d812013-11-01 12:05:12 -04001282 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001283 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001284
Felipe Balbi72246da2011-08-19 18:10:58 +03001285 return ret;
1286}
1287
1288static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1289{
1290 struct dwc3_ep *dep = to_dwc3_ep(ep);
1291 struct dwc3 *dwc = dep->dwc;
1292
1293 unsigned long flags;
1294
1295 int ret;
1296
1297 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001298 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001299 spin_unlock_irqrestore(&dwc->lock, flags);
1300
1301 return ret;
1302}
1303
1304static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1305{
1306 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001307 struct dwc3 *dwc = dep->dwc;
1308 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001309 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001310
Paul Zimmerman249a4562012-02-24 17:32:16 -08001311 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001312 dep->flags |= DWC3_EP_WEDGE;
1313
Pratyush Anand08f0d962012-06-25 22:40:43 +05301314 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001315 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301316 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001317 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001318 spin_unlock_irqrestore(&dwc->lock, flags);
1319
1320 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001321}
1322
1323/* -------------------------------------------------------------------------- */
1324
1325static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1326 .bLength = USB_DT_ENDPOINT_SIZE,
1327 .bDescriptorType = USB_DT_ENDPOINT,
1328 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1329};
1330
1331static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1332 .enable = dwc3_gadget_ep0_enable,
1333 .disable = dwc3_gadget_ep0_disable,
1334 .alloc_request = dwc3_gadget_ep_alloc_request,
1335 .free_request = dwc3_gadget_ep_free_request,
1336 .queue = dwc3_gadget_ep0_queue,
1337 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301338 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 .set_wedge = dwc3_gadget_ep_set_wedge,
1340};
1341
1342static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1343 .enable = dwc3_gadget_ep_enable,
1344 .disable = dwc3_gadget_ep_disable,
1345 .alloc_request = dwc3_gadget_ep_alloc_request,
1346 .free_request = dwc3_gadget_ep_free_request,
1347 .queue = dwc3_gadget_ep_queue,
1348 .dequeue = dwc3_gadget_ep_dequeue,
1349 .set_halt = dwc3_gadget_ep_set_halt,
1350 .set_wedge = dwc3_gadget_ep_set_wedge,
1351};
1352
1353/* -------------------------------------------------------------------------- */
1354
1355static int dwc3_gadget_get_frame(struct usb_gadget *g)
1356{
1357 struct dwc3 *dwc = gadget_to_dwc(g);
1358 u32 reg;
1359
1360 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1361 return DWC3_DSTS_SOFFN(reg);
1362}
1363
1364static int dwc3_gadget_wakeup(struct usb_gadget *g)
1365{
1366 struct dwc3 *dwc = gadget_to_dwc(g);
1367
1368 unsigned long timeout;
1369 unsigned long flags;
1370
1371 u32 reg;
1372
1373 int ret = 0;
1374
1375 u8 link_state;
1376 u8 speed;
1377
1378 spin_lock_irqsave(&dwc->lock, flags);
1379
1380 /*
1381 * According to the Databook Remote wakeup request should
1382 * be issued only when the device is in early suspend state.
1383 *
1384 * We can check that via USB Link State bits in DSTS register.
1385 */
1386 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1387
1388 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001389 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1390 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001391 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001392 ret = -EINVAL;
1393 goto out;
1394 }
1395
1396 link_state = DWC3_DSTS_USBLNKST(reg);
1397
1398 switch (link_state) {
1399 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1400 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1401 break;
1402 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001403 dwc3_trace(trace_dwc3_gadget,
1404 "can't wakeup from '%s'\n",
1405 dwc3_gadget_link_string(link_state));
Felipe Balbi72246da2011-08-19 18:10:58 +03001406 ret = -EINVAL;
1407 goto out;
1408 }
1409
Felipe Balbi8598bde2012-01-02 18:55:57 +02001410 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1411 if (ret < 0) {
1412 dev_err(dwc->dev, "failed to put link in Recovery\n");
1413 goto out;
1414 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001415
Paul Zimmerman802fde92012-04-27 13:10:52 +03001416 /* Recent versions do this automatically */
1417 if (dwc->revision < DWC3_REVISION_194A) {
1418 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001419 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001420 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1421 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1422 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001423
Paul Zimmerman1d046792012-02-15 18:56:56 -08001424 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001425 timeout = jiffies + msecs_to_jiffies(100);
1426
Paul Zimmerman1d046792012-02-15 18:56:56 -08001427 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001428 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1429
1430 /* in HS, means ON */
1431 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1432 break;
1433 }
1434
1435 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1436 dev_err(dwc->dev, "failed to send remote wakeup\n");
1437 ret = -EINVAL;
1438 }
1439
1440out:
1441 spin_unlock_irqrestore(&dwc->lock, flags);
1442
1443 return ret;
1444}
1445
1446static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1447 int is_selfpowered)
1448{
1449 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001450 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001451
Paul Zimmerman249a4562012-02-24 17:32:16 -08001452 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001453 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001454 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001455
1456 return 0;
1457}
1458
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001459static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001460{
1461 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001462 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001463
1464 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001465 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001466 if (dwc->revision <= DWC3_REVISION_187A) {
1467 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1468 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1469 }
1470
1471 if (dwc->revision >= DWC3_REVISION_194A)
1472 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1473 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001474
1475 if (dwc->has_hibernation)
1476 reg |= DWC3_DCTL_KEEP_CONNECT;
1477
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001478 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001479 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001480 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001481
1482 if (dwc->has_hibernation && !suspend)
1483 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1484
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001485 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001486 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001487
1488 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1489
1490 do {
1491 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1492 if (is_on) {
1493 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1494 break;
1495 } else {
1496 if (reg & DWC3_DSTS_DEVCTRLHLT)
1497 break;
1498 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001499 timeout--;
1500 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301501 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001502 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001503 } while (1);
1504
Felipe Balbi73815282015-01-27 13:48:14 -06001505 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001506 dwc->gadget_driver
1507 ? dwc->gadget_driver->function : "no-function",
1508 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301509
1510 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001511}
1512
1513static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1514{
1515 struct dwc3 *dwc = gadget_to_dwc(g);
1516 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301517 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001518
1519 is_on = !!is_on;
1520
1521 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001522 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001523 spin_unlock_irqrestore(&dwc->lock, flags);
1524
Pratyush Anand6f17f742012-07-02 10:21:55 +05301525 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001526}
1527
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001528static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1529{
1530 u32 reg;
1531
1532 /* Enable all but Start and End of Frame IRQs */
1533 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1534 DWC3_DEVTEN_EVNTOVERFLOWEN |
1535 DWC3_DEVTEN_CMDCMPLTEN |
1536 DWC3_DEVTEN_ERRTICERREN |
1537 DWC3_DEVTEN_WKUPEVTEN |
1538 DWC3_DEVTEN_ULSTCNGEN |
1539 DWC3_DEVTEN_CONNECTDONEEN |
1540 DWC3_DEVTEN_USBRSTEN |
1541 DWC3_DEVTEN_DISCONNEVTEN);
1542
1543 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1544}
1545
1546static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1547{
1548 /* mask all interrupts */
1549 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1550}
1551
1552static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001553static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001554
Felipe Balbi72246da2011-08-19 18:10:58 +03001555static int dwc3_gadget_start(struct usb_gadget *g,
1556 struct usb_gadget_driver *driver)
1557{
1558 struct dwc3 *dwc = gadget_to_dwc(g);
1559 struct dwc3_ep *dep;
1560 unsigned long flags;
1561 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001562 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001563 u32 reg;
1564
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001565 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1566 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbidea520a2016-03-30 09:39:34 +03001567 IRQF_SHARED, "dwc3", dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001568 if (ret) {
1569 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1570 irq, ret);
1571 goto err0;
1572 }
1573
Felipe Balbi72246da2011-08-19 18:10:58 +03001574 spin_lock_irqsave(&dwc->lock, flags);
1575
1576 if (dwc->gadget_driver) {
1577 dev_err(dwc->dev, "%s is already bound to %s\n",
1578 dwc->gadget.name,
1579 dwc->gadget_driver->driver.name);
1580 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001581 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 }
1583
1584 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001585
Felipe Balbi72246da2011-08-19 18:10:58 +03001586 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1587 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001588
1589 /**
1590 * WORKAROUND: DWC3 revision < 2.20a have an issue
1591 * which would cause metastability state on Run/Stop
1592 * bit if we try to force the IP to USB2-only mode.
1593 *
1594 * Because of that, we cannot configure the IP to any
1595 * speed other than the SuperSpeed
1596 *
1597 * Refers to:
1598 *
1599 * STAR#9000525659: Clock Domain Crossing on DCTL in
1600 * USB 2.0 Mode
1601 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001602 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001603 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001604 } else {
1605 switch (dwc->maximum_speed) {
1606 case USB_SPEED_LOW:
1607 reg |= DWC3_DSTS_LOWSPEED;
1608 break;
1609 case USB_SPEED_FULL:
1610 reg |= DWC3_DSTS_FULLSPEED1;
1611 break;
1612 case USB_SPEED_HIGH:
1613 reg |= DWC3_DSTS_HIGHSPEED;
1614 break;
John Youn75808622016-02-05 17:09:13 -08001615 case USB_SPEED_SUPER_PLUS:
1616 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1617 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001618 default:
John Youn77966eb2016-02-19 17:31:01 -08001619 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1620 dwc->maximum_speed);
1621 /* fall through */
1622 case USB_SPEED_SUPER:
1623 reg |= DWC3_DCFG_SUPERSPEED;
1624 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001625 }
1626 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001627 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1628
1629 /* Start with SuperSpeed Default */
1630 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1631
1632 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001633 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1634 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001635 if (ret) {
1636 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001637 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001638 }
1639
1640 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001641 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1642 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001643 if (ret) {
1644 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001645 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001646 }
1647
1648 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001649 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 dwc3_ep0_out_start(dwc);
1651
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001652 dwc3_gadget_enable_irq(dwc);
1653
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 spin_unlock_irqrestore(&dwc->lock, flags);
1655
1656 return 0;
1657
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001658err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001659 __dwc3_gadget_ep_disable(dwc->eps[0]);
1660
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001661err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001662 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001663
1664err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001665 spin_unlock_irqrestore(&dwc->lock, flags);
1666
Felipe Balbidea520a2016-03-30 09:39:34 +03001667 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001668
1669err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001670 return ret;
1671}
1672
Felipe Balbi22835b82014-10-17 12:05:12 -05001673static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001674{
1675 struct dwc3 *dwc = gadget_to_dwc(g);
1676 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001677 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001678
1679 spin_lock_irqsave(&dwc->lock, flags);
1680
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001681 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001682 __dwc3_gadget_ep_disable(dwc->eps[0]);
1683 __dwc3_gadget_ep_disable(dwc->eps[1]);
1684
1685 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001686
1687 spin_unlock_irqrestore(&dwc->lock, flags);
1688
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001689 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
Felipe Balbidea520a2016-03-30 09:39:34 +03001690 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001691
Felipe Balbi72246da2011-08-19 18:10:58 +03001692 return 0;
1693}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001694
Felipe Balbi72246da2011-08-19 18:10:58 +03001695static const struct usb_gadget_ops dwc3_gadget_ops = {
1696 .get_frame = dwc3_gadget_get_frame,
1697 .wakeup = dwc3_gadget_wakeup,
1698 .set_selfpowered = dwc3_gadget_set_selfpowered,
1699 .pullup = dwc3_gadget_pullup,
1700 .udc_start = dwc3_gadget_start,
1701 .udc_stop = dwc3_gadget_stop,
1702};
1703
1704/* -------------------------------------------------------------------------- */
1705
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001706static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1707 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001708{
1709 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001710 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001711
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001712 for (i = 0; i < num; i++) {
1713 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001714
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001716 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001717 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001718
1719 dep->dwc = dwc;
1720 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001721 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001722 dwc->eps[epnum] = dep;
1723
1724 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1725 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001726
Felipe Balbi72246da2011-08-19 18:10:58 +03001727 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001728
Felipe Balbi73815282015-01-27 13:48:14 -06001729 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001730
Felipe Balbi72246da2011-08-19 18:10:58 +03001731 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001732 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301733 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001734 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1735 if (!epnum)
1736 dwc->gadget.ep0 = &dep->endpoint;
1737 } else {
1738 int ret;
1739
Robert Baldygae117e742013-12-13 12:23:38 +01001740 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001741 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001742 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1743 list_add_tail(&dep->endpoint.ep_list,
1744 &dwc->gadget.ep_list);
1745
1746 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001747 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001748 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001749 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001750
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001751 if (epnum == 0 || epnum == 1) {
1752 dep->endpoint.caps.type_control = true;
1753 } else {
1754 dep->endpoint.caps.type_iso = true;
1755 dep->endpoint.caps.type_bulk = true;
1756 dep->endpoint.caps.type_int = true;
1757 }
1758
1759 dep->endpoint.caps.dir_in = !!direction;
1760 dep->endpoint.caps.dir_out = !direction;
1761
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001762 INIT_LIST_HEAD(&dep->pending_list);
1763 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 }
1765
1766 return 0;
1767}
1768
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001769static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1770{
1771 int ret;
1772
1773 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1774
1775 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1776 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001777 dwc3_trace(trace_dwc3_gadget,
1778 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001779 return ret;
1780 }
1781
1782 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1783 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001784 dwc3_trace(trace_dwc3_gadget,
1785 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001786 return ret;
1787 }
1788
1789 return 0;
1790}
1791
Felipe Balbi72246da2011-08-19 18:10:58 +03001792static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1793{
1794 struct dwc3_ep *dep;
1795 u8 epnum;
1796
1797 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1798 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001799 if (!dep)
1800 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301801 /*
1802 * Physical endpoints 0 and 1 are special; they form the
1803 * bi-directional USB endpoint 0.
1804 *
1805 * For those two physical endpoints, we don't allocate a TRB
1806 * pool nor do we add them the endpoints list. Due to that, we
1807 * shouldn't do these two operations otherwise we would end up
1808 * with all sorts of bugs when removing dwc3.ko.
1809 */
1810 if (epnum != 0 && epnum != 1) {
1811 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001812 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301813 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001814
1815 kfree(dep);
1816 }
1817}
1818
Felipe Balbi72246da2011-08-19 18:10:58 +03001819/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001820
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301821static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1822 struct dwc3_request *req, struct dwc3_trb *trb,
1823 const struct dwc3_event_depevt *event, int status)
1824{
1825 unsigned int count;
1826 unsigned int s_pkt = 0;
1827 unsigned int trb_status;
1828
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001829 trace_dwc3_complete_trb(dep, trb);
1830
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301831 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1832 /*
1833 * We continue despite the error. There is not much we
1834 * can do. If we don't clean it up we loop forever. If
1835 * we skip the TRB then it gets overwritten after a
1836 * while since we use them in a ring buffer. A BUG()
1837 * would help. Lets hope that if this occurs, someone
1838 * fixes the root cause instead of looking away :)
1839 */
1840 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1841 dep->name, trb);
1842 count = trb->size & DWC3_TRB_SIZE_MASK;
1843
1844 if (dep->direction) {
1845 if (count) {
1846 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1847 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001848 dwc3_trace(trace_dwc3_gadget,
1849 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301850 dep->name);
1851 /*
1852 * If missed isoc occurred and there is
1853 * no request queued then issue END
1854 * TRANSFER, so that core generates
1855 * next xfernotready and we will issue
1856 * a fresh START TRANSFER.
1857 * If there are still queued request
1858 * then wait, do not issue either END
1859 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001860 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301861 * giveback.If any future queued request
1862 * is successfully transferred then we
1863 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001864 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301865 */
1866 dep->flags |= DWC3_EP_MISSED_ISOC;
1867 } else {
1868 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1869 dep->name);
1870 status = -ECONNRESET;
1871 }
1872 } else {
1873 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1874 }
1875 } else {
1876 if (count && (event->status & DEPEVT_STATUS_SHORT))
1877 s_pkt = 1;
1878 }
1879
1880 /*
1881 * We assume here we will always receive the entire data block
1882 * which we should receive. Meaning, if we program RX to
1883 * receive 4K but we receive only 2K, we assume that's all we
1884 * should receive and we simply bounce the request back to the
1885 * gadget driver for further processing.
1886 */
1887 req->request.actual += req->request.length - count;
1888 if (s_pkt)
1889 return 1;
1890 if ((event->status & DEPEVT_STATUS_LST) &&
1891 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1892 DWC3_TRB_CTRL_HWO)))
1893 return 1;
1894 if ((event->status & DEPEVT_STATUS_IOC) &&
1895 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1896 return 1;
1897 return 0;
1898}
1899
Felipe Balbi72246da2011-08-19 18:10:58 +03001900static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1901 const struct dwc3_event_depevt *event, int status)
1902{
1903 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001904 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301905 unsigned int slot;
1906 unsigned int i;
1907 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001908
1909 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001910 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001911 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001912 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001913
Ville Syrjäläd115d702015-08-31 19:48:28 +03001914 i = 0;
1915 do {
1916 slot = req->start_slot + i;
1917 if ((slot == DWC3_TRB_NUM - 1) &&
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301918 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001919 slot++;
1920 slot %= DWC3_TRB_NUM;
1921 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001922
Ville Syrjäläd115d702015-08-31 19:48:28 +03001923 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1924 event, status);
1925 if (ret)
1926 break;
1927 } while (++i < req->request.num_mapped_sgs);
1928
1929 dwc3_gadget_giveback(dep, req, status);
1930
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301931 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001932 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03001933 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001934
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301935 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001936 list_empty(&dep->started_list)) {
1937 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301938 /*
1939 * If there is no entry in request list then do
1940 * not issue END TRANSFER now. Just set PENDING
1941 * flag, so that END TRANSFER is issued when an
1942 * entry is added into request list.
1943 */
1944 dep->flags = DWC3_EP_PENDING_REQUEST;
1945 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001946 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301947 dep->flags = DWC3_EP_ENABLED;
1948 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301949 return 1;
1950 }
1951
Felipe Balbi72246da2011-08-19 18:10:58 +03001952 return 1;
1953}
1954
1955static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09001956 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001957{
1958 unsigned status = 0;
1959 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05001960 u32 is_xfer_complete;
1961
1962 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001963
1964 if (event->status & DEPEVT_STATUS_BUSERR)
1965 status = -ECONNRESET;
1966
Paul Zimmerman1d046792012-02-15 18:56:56 -08001967 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05001968 if (clean_busy && (is_xfer_complete ||
1969 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001970 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001971
1972 /*
1973 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1974 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1975 */
1976 if (dwc->revision < DWC3_REVISION_183A) {
1977 u32 reg;
1978 int i;
1979
1980 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001981 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001982
1983 if (!(dep->flags & DWC3_EP_ENABLED))
1984 continue;
1985
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001986 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03001987 return;
1988 }
1989
1990 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1991 reg |= dwc->u1u2;
1992 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1993
1994 dwc->u1u2 = 0;
1995 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05001996
Felipe Balbie6e709b2015-09-28 15:16:56 -05001997 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05001998 int ret;
1999
Felipe Balbie6e709b2015-09-28 15:16:56 -05002000 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002001 if (!ret || ret == -EBUSY)
2002 return;
2003 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002004}
2005
Felipe Balbi72246da2011-08-19 18:10:58 +03002006static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2007 const struct dwc3_event_depevt *event)
2008{
2009 struct dwc3_ep *dep;
2010 u8 epnum = event->endpoint_number;
2011
2012 dep = dwc->eps[epnum];
2013
Felipe Balbi3336abb2012-06-06 09:19:35 +03002014 if (!(dep->flags & DWC3_EP_ENABLED))
2015 return;
2016
Felipe Balbi72246da2011-08-19 18:10:58 +03002017 if (epnum == 0 || epnum == 1) {
2018 dwc3_ep0_interrupt(dwc, event);
2019 return;
2020 }
2021
2022 switch (event->endpoint_event) {
2023 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002024 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002025
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002026 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002027 dwc3_trace(trace_dwc3_gadget,
2028 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002029 dep->name);
2030 return;
2031 }
2032
Jingoo Han029d97f2014-07-04 15:00:51 +09002033 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002034 break;
2035 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002036 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002037 break;
2038 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002039 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 dwc3_gadget_start_isoc(dwc, dep, event);
2041 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002042 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002043 int ret;
2044
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002045 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2046
Felipe Balbi73815282015-01-27 13:48:14 -06002047 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002048 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002049 : "Transfer Not Active");
2050
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002051 ret = __dwc3_gadget_kick_transfer(dep, 0, !active);
Felipe Balbi72246da2011-08-19 18:10:58 +03002052 if (!ret || ret == -EBUSY)
2053 return;
2054
Felipe Balbiec5e7952015-11-16 16:04:13 -06002055 dwc3_trace(trace_dwc3_gadget,
2056 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002057 dep->name);
2058 }
2059
2060 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002061 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002062 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002063 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2064 dep->name);
2065 return;
2066 }
2067
2068 switch (event->status) {
2069 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002070 dwc3_trace(trace_dwc3_gadget,
2071 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002072 event->parameters);
2073
2074 break;
2075 case DEPEVT_STREAMEVT_NOTFOUND:
2076 /* FALLTHROUGH */
2077 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002078 dwc3_trace(trace_dwc3_gadget,
2079 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002080 }
2081 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002083 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002084 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002085 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002086 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002087 break;
2088 }
2089}
2090
2091static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2092{
2093 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2094 spin_unlock(&dwc->lock);
2095 dwc->gadget_driver->disconnect(&dwc->gadget);
2096 spin_lock(&dwc->lock);
2097 }
2098}
2099
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002100static void dwc3_suspend_gadget(struct dwc3 *dwc)
2101{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002102 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002103 spin_unlock(&dwc->lock);
2104 dwc->gadget_driver->suspend(&dwc->gadget);
2105 spin_lock(&dwc->lock);
2106 }
2107}
2108
2109static void dwc3_resume_gadget(struct dwc3 *dwc)
2110{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002111 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002112 spin_unlock(&dwc->lock);
2113 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002114 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002115 }
2116}
2117
2118static void dwc3_reset_gadget(struct dwc3 *dwc)
2119{
2120 if (!dwc->gadget_driver)
2121 return;
2122
2123 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2124 spin_unlock(&dwc->lock);
2125 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002126 spin_lock(&dwc->lock);
2127 }
2128}
2129
Paul Zimmermanb992e682012-04-27 14:17:35 +03002130static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002131{
2132 struct dwc3_ep *dep;
2133 struct dwc3_gadget_ep_cmd_params params;
2134 u32 cmd;
2135 int ret;
2136
2137 dep = dwc->eps[epnum];
2138
Felipe Balbib4996a82012-06-06 12:04:13 +03002139 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302140 return;
2141
Pratyush Anand57911502012-07-06 15:19:10 +05302142 /*
2143 * NOTICE: We are violating what the Databook says about the
2144 * EndTransfer command. Ideally we would _always_ wait for the
2145 * EndTransfer Command Completion IRQ, but that's causing too
2146 * much trouble synchronizing between us and gadget driver.
2147 *
2148 * We have discussed this with the IP Provider and it was
2149 * suggested to giveback all requests here, but give HW some
2150 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002151 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302152 *
2153 * Note also that a similar handling was tested by Synopsys
2154 * (thanks a lot Paul) and nothing bad has come out of it.
2155 * In short, what we're doing is:
2156 *
2157 * - Issue EndTransfer WITH CMDIOC bit set
2158 * - Wait 100us
2159 */
2160
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302161 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002162 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2163 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002164 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302165 memset(&params, 0, sizeof(params));
2166 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2167 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002168 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002169 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302170 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002171}
2172
2173static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2174{
2175 u32 epnum;
2176
2177 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2178 struct dwc3_ep *dep;
2179
2180 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002181 if (!dep)
2182 continue;
2183
Felipe Balbi72246da2011-08-19 18:10:58 +03002184 if (!(dep->flags & DWC3_EP_ENABLED))
2185 continue;
2186
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002187 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002188 }
2189}
2190
2191static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2192{
2193 u32 epnum;
2194
2195 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2196 struct dwc3_ep *dep;
2197 struct dwc3_gadget_ep_cmd_params params;
2198 int ret;
2199
2200 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002201 if (!dep)
2202 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002203
2204 if (!(dep->flags & DWC3_EP_STALL))
2205 continue;
2206
2207 dep->flags &= ~DWC3_EP_STALL;
2208
2209 memset(&params, 0, sizeof(params));
2210 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2211 DWC3_DEPCMD_CLEARSTALL, &params);
2212 WARN_ON_ONCE(ret);
2213 }
2214}
2215
2216static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2217{
Felipe Balbic4430a22012-05-24 10:30:01 +03002218 int reg;
2219
Felipe Balbi72246da2011-08-19 18:10:58 +03002220 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2221 reg &= ~DWC3_DCTL_INITU1ENA;
2222 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2223
2224 reg &= ~DWC3_DCTL_INITU2ENA;
2225 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002226
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 dwc3_disconnect_gadget(dwc);
2228
2229 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002230 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002231 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002232}
2233
Felipe Balbi72246da2011-08-19 18:10:58 +03002234static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2235{
2236 u32 reg;
2237
Felipe Balbidf62df52011-10-14 15:11:49 +03002238 /*
2239 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2240 * would cause a missing Disconnect Event if there's a
2241 * pending Setup Packet in the FIFO.
2242 *
2243 * There's no suggested workaround on the official Bug
2244 * report, which states that "unless the driver/application
2245 * is doing any special handling of a disconnect event,
2246 * there is no functional issue".
2247 *
2248 * Unfortunately, it turns out that we _do_ some special
2249 * handling of a disconnect event, namely complete all
2250 * pending transfers, notify gadget driver of the
2251 * disconnection, and so on.
2252 *
2253 * Our suggested workaround is to follow the Disconnect
2254 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002255 * flag. Such flag gets set whenever we have a SETUP_PENDING
2256 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002257 * same endpoint.
2258 *
2259 * Refers to:
2260 *
2261 * STAR#9000466709: RTL: Device : Disconnect event not
2262 * generated if setup packet pending in FIFO
2263 */
2264 if (dwc->revision < DWC3_REVISION_188A) {
2265 if (dwc->setup_packet_pending)
2266 dwc3_gadget_disconnect_interrupt(dwc);
2267 }
2268
Felipe Balbi8e744752014-11-06 14:27:53 +08002269 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002270
2271 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2272 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2273 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002274 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002275
2276 dwc3_stop_active_transfers(dwc);
2277 dwc3_clear_stall_all_ep(dwc);
2278
2279 /* Reset device address to zero */
2280 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2281 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2282 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002283}
2284
2285static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2286{
2287 u32 reg;
2288 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2289
2290 /*
2291 * We change the clock only at SS but I dunno why I would want to do
2292 * this. Maybe it becomes part of the power saving plan.
2293 */
2294
John Younee5cd412016-02-05 17:08:45 -08002295 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2296 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002297 return;
2298
2299 /*
2300 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2301 * each time on Connect Done.
2302 */
2303 if (!usb30_clock)
2304 return;
2305
2306 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2307 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2308 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2309}
2310
Felipe Balbi72246da2011-08-19 18:10:58 +03002311static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2312{
Felipe Balbi72246da2011-08-19 18:10:58 +03002313 struct dwc3_ep *dep;
2314 int ret;
2315 u32 reg;
2316 u8 speed;
2317
Felipe Balbi72246da2011-08-19 18:10:58 +03002318 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2319 speed = reg & DWC3_DSTS_CONNECTSPD;
2320 dwc->speed = speed;
2321
2322 dwc3_update_ram_clk_sel(dwc, speed);
2323
2324 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002325 case DWC3_DCFG_SUPERSPEED_PLUS:
2326 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2327 dwc->gadget.ep0->maxpacket = 512;
2328 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2329 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002330 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002331 /*
2332 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2333 * would cause a missing USB3 Reset event.
2334 *
2335 * In such situations, we should force a USB3 Reset
2336 * event by calling our dwc3_gadget_reset_interrupt()
2337 * routine.
2338 *
2339 * Refers to:
2340 *
2341 * STAR#9000483510: RTL: SS : USB3 reset event may
2342 * not be generated always when the link enters poll
2343 */
2344 if (dwc->revision < DWC3_REVISION_190A)
2345 dwc3_gadget_reset_interrupt(dwc);
2346
Felipe Balbi72246da2011-08-19 18:10:58 +03002347 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2348 dwc->gadget.ep0->maxpacket = 512;
2349 dwc->gadget.speed = USB_SPEED_SUPER;
2350 break;
2351 case DWC3_DCFG_HIGHSPEED:
2352 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2353 dwc->gadget.ep0->maxpacket = 64;
2354 dwc->gadget.speed = USB_SPEED_HIGH;
2355 break;
2356 case DWC3_DCFG_FULLSPEED2:
2357 case DWC3_DCFG_FULLSPEED1:
2358 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2359 dwc->gadget.ep0->maxpacket = 64;
2360 dwc->gadget.speed = USB_SPEED_FULL;
2361 break;
2362 case DWC3_DCFG_LOWSPEED:
2363 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2364 dwc->gadget.ep0->maxpacket = 8;
2365 dwc->gadget.speed = USB_SPEED_LOW;
2366 break;
2367 }
2368
Pratyush Anand2b758352013-01-14 15:59:31 +05302369 /* Enable USB2 LPM Capability */
2370
John Younee5cd412016-02-05 17:08:45 -08002371 if ((dwc->revision > DWC3_REVISION_194A) &&
2372 (speed != DWC3_DCFG_SUPERSPEED) &&
2373 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302374 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2375 reg |= DWC3_DCFG_LPM_CAP;
2376 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2377
2378 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2379 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2380
Huang Rui460d0982014-10-31 11:11:18 +08002381 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302382
Huang Rui80caf7d2014-10-28 19:54:26 +08002383 /*
2384 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2385 * DCFG.LPMCap is set, core responses with an ACK and the
2386 * BESL value in the LPM token is less than or equal to LPM
2387 * NYET threshold.
2388 */
2389 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2390 && dwc->has_lpm_erratum,
2391 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2392
2393 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2394 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2395
Pratyush Anand2b758352013-01-14 15:59:31 +05302396 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002397 } else {
2398 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2399 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2400 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302401 }
2402
Felipe Balbi72246da2011-08-19 18:10:58 +03002403 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002404 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2405 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002406 if (ret) {
2407 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2408 return;
2409 }
2410
2411 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002412 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2413 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002414 if (ret) {
2415 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2416 return;
2417 }
2418
2419 /*
2420 * Configure PHY via GUSB3PIPECTLn if required.
2421 *
2422 * Update GTXFIFOSIZn
2423 *
2424 * In both cases reset values should be sufficient.
2425 */
2426}
2427
2428static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2429{
Felipe Balbi72246da2011-08-19 18:10:58 +03002430 /*
2431 * TODO take core out of low power mode when that's
2432 * implemented.
2433 */
2434
Jiebing Liad14d4e2014-12-11 13:26:29 +08002435 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2436 spin_unlock(&dwc->lock);
2437 dwc->gadget_driver->resume(&dwc->gadget);
2438 spin_lock(&dwc->lock);
2439 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002440}
2441
2442static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2443 unsigned int evtinfo)
2444{
Felipe Balbifae2b902011-10-14 13:00:30 +03002445 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002446 unsigned int pwropt;
2447
2448 /*
2449 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2450 * Hibernation mode enabled which would show up when device detects
2451 * host-initiated U3 exit.
2452 *
2453 * In that case, device will generate a Link State Change Interrupt
2454 * from U3 to RESUME which is only necessary if Hibernation is
2455 * configured in.
2456 *
2457 * There are no functional changes due to such spurious event and we
2458 * just need to ignore it.
2459 *
2460 * Refers to:
2461 *
2462 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2463 * operational mode
2464 */
2465 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2466 if ((dwc->revision < DWC3_REVISION_250A) &&
2467 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2468 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2469 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002470 dwc3_trace(trace_dwc3_gadget,
2471 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002472 return;
2473 }
2474 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002475
2476 /*
2477 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2478 * on the link partner, the USB session might do multiple entry/exit
2479 * of low power states before a transfer takes place.
2480 *
2481 * Due to this problem, we might experience lower throughput. The
2482 * suggested workaround is to disable DCTL[12:9] bits if we're
2483 * transitioning from U1/U2 to U0 and enable those bits again
2484 * after a transfer completes and there are no pending transfers
2485 * on any of the enabled endpoints.
2486 *
2487 * This is the first half of that workaround.
2488 *
2489 * Refers to:
2490 *
2491 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2492 * core send LGO_Ux entering U0
2493 */
2494 if (dwc->revision < DWC3_REVISION_183A) {
2495 if (next == DWC3_LINK_STATE_U0) {
2496 u32 u1u2;
2497 u32 reg;
2498
2499 switch (dwc->link_state) {
2500 case DWC3_LINK_STATE_U1:
2501 case DWC3_LINK_STATE_U2:
2502 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2503 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2504 | DWC3_DCTL_ACCEPTU2ENA
2505 | DWC3_DCTL_INITU1ENA
2506 | DWC3_DCTL_ACCEPTU1ENA);
2507
2508 if (!dwc->u1u2)
2509 dwc->u1u2 = reg & u1u2;
2510
2511 reg &= ~u1u2;
2512
2513 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2514 break;
2515 default:
2516 /* do nothing */
2517 break;
2518 }
2519 }
2520 }
2521
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002522 switch (next) {
2523 case DWC3_LINK_STATE_U1:
2524 if (dwc->speed == USB_SPEED_SUPER)
2525 dwc3_suspend_gadget(dwc);
2526 break;
2527 case DWC3_LINK_STATE_U2:
2528 case DWC3_LINK_STATE_U3:
2529 dwc3_suspend_gadget(dwc);
2530 break;
2531 case DWC3_LINK_STATE_RESUME:
2532 dwc3_resume_gadget(dwc);
2533 break;
2534 default:
2535 /* do nothing */
2536 break;
2537 }
2538
Felipe Balbie57ebc12014-04-22 13:20:12 -05002539 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002540}
2541
Felipe Balbie1dadd32014-02-25 14:47:54 -06002542static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2543 unsigned int evtinfo)
2544{
2545 unsigned int is_ss = evtinfo & BIT(4);
2546
2547 /**
2548 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2549 * have a known issue which can cause USB CV TD.9.23 to fail
2550 * randomly.
2551 *
2552 * Because of this issue, core could generate bogus hibernation
2553 * events which SW needs to ignore.
2554 *
2555 * Refers to:
2556 *
2557 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2558 * Device Fallback from SuperSpeed
2559 */
2560 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2561 return;
2562
2563 /* enter hibernation here */
2564}
2565
Felipe Balbi72246da2011-08-19 18:10:58 +03002566static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2567 const struct dwc3_event_devt *event)
2568{
2569 switch (event->type) {
2570 case DWC3_DEVICE_EVENT_DISCONNECT:
2571 dwc3_gadget_disconnect_interrupt(dwc);
2572 break;
2573 case DWC3_DEVICE_EVENT_RESET:
2574 dwc3_gadget_reset_interrupt(dwc);
2575 break;
2576 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2577 dwc3_gadget_conndone_interrupt(dwc);
2578 break;
2579 case DWC3_DEVICE_EVENT_WAKEUP:
2580 dwc3_gadget_wakeup_interrupt(dwc);
2581 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002582 case DWC3_DEVICE_EVENT_HIBER_REQ:
2583 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2584 "unexpected hibernation event\n"))
2585 break;
2586
2587 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2588 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002589 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2590 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2591 break;
2592 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002593 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002594 break;
2595 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002596 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002597 break;
2598 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002599 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002600 break;
2601 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002602 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002603 break;
2604 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002605 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002606 break;
2607 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06002608 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002609 }
2610}
2611
2612static void dwc3_process_event_entry(struct dwc3 *dwc,
2613 const union dwc3_event *event)
2614{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002615 trace_dwc3_event(event->raw);
2616
Felipe Balbi72246da2011-08-19 18:10:58 +03002617 /* Endpoint IRQ, handle it and return early */
2618 if (event->type.is_devspec == 0) {
2619 /* depevt */
2620 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2621 }
2622
2623 switch (event->type.type) {
2624 case DWC3_EVENT_TYPE_DEV:
2625 dwc3_gadget_interrupt(dwc, &event->devt);
2626 break;
2627 /* REVISIT what to do with Carkit and I2C events ? */
2628 default:
2629 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2630 }
2631}
2632
Felipe Balbidea520a2016-03-30 09:39:34 +03002633static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002634{
Felipe Balbidea520a2016-03-30 09:39:34 +03002635 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002636 irqreturn_t ret = IRQ_NONE;
2637 int left;
2638 u32 reg;
2639
Felipe Balbif42f2442013-06-12 21:25:08 +03002640 left = evt->count;
2641
2642 if (!(evt->flags & DWC3_EVENT_PENDING))
2643 return IRQ_NONE;
2644
2645 while (left > 0) {
2646 union dwc3_event event;
2647
2648 event.raw = *(u32 *) (evt->buf + evt->lpos);
2649
2650 dwc3_process_event_entry(dwc, &event);
2651
2652 /*
2653 * FIXME we wrap around correctly to the next entry as
2654 * almost all entries are 4 bytes in size. There is one
2655 * entry which has 12 bytes which is a regular entry
2656 * followed by 8 bytes data. ATM I don't know how
2657 * things are organized if we get next to the a
2658 * boundary so I worry about that once we try to handle
2659 * that.
2660 */
2661 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2662 left -= 4;
2663
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002664 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002665 }
2666
2667 evt->count = 0;
2668 evt->flags &= ~DWC3_EVENT_PENDING;
2669 ret = IRQ_HANDLED;
2670
2671 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002672 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002673 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002674 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002675
2676 return ret;
2677}
2678
Felipe Balbidea520a2016-03-30 09:39:34 +03002679static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002680{
Felipe Balbidea520a2016-03-30 09:39:34 +03002681 struct dwc3_event_buffer *evt = _evt;
2682 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002683 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002684 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002685
Felipe Balbie5f68b42015-10-12 13:25:44 -05002686 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002687 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002688 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002689
2690 return ret;
2691}
2692
Felipe Balbidea520a2016-03-30 09:39:34 +03002693static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002694{
Felipe Balbidea520a2016-03-30 09:39:34 +03002695 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002696 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002697 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002698
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002699 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002700 count &= DWC3_GEVNTCOUNT_MASK;
2701 if (!count)
2702 return IRQ_NONE;
2703
Felipe Balbib15a7622011-06-30 16:57:15 +03002704 evt->count = count;
2705 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002706
Felipe Balbie8adfc32013-06-12 21:11:14 +03002707 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002708 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002709 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002710 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002711
Felipe Balbib15a7622011-06-30 16:57:15 +03002712 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002713}
2714
Felipe Balbidea520a2016-03-30 09:39:34 +03002715static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002716{
Felipe Balbidea520a2016-03-30 09:39:34 +03002717 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002718
Felipe Balbidea520a2016-03-30 09:39:34 +03002719 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002720}
2721
2722/**
2723 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002724 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002725 *
2726 * Returns 0 on success otherwise negative errno.
2727 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002728int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002729{
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002731
2732 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2733 &dwc->ctrl_req_addr, GFP_KERNEL);
2734 if (!dwc->ctrl_req) {
2735 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2736 ret = -ENOMEM;
2737 goto err0;
2738 }
2739
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302740 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002741 &dwc->ep0_trb_addr, GFP_KERNEL);
2742 if (!dwc->ep0_trb) {
2743 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2744 ret = -ENOMEM;
2745 goto err1;
2746 }
2747
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002748 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002749 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002750 ret = -ENOMEM;
2751 goto err2;
2752 }
2753
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002754 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002755 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2756 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002757 if (!dwc->ep0_bounce) {
2758 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2759 ret = -ENOMEM;
2760 goto err3;
2761 }
2762
Felipe Balbi04c03d12015-12-02 10:06:45 -06002763 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2764 if (!dwc->zlp_buf) {
2765 ret = -ENOMEM;
2766 goto err4;
2767 }
2768
Felipe Balbi72246da2011-08-19 18:10:58 +03002769 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002770 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002771 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002772 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002773 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002774
2775 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002776 * FIXME We might be setting max_speed to <SUPER, however versions
2777 * <2.20a of dwc3 have an issue with metastability (documented
2778 * elsewhere in this driver) which tells us we can't set max speed to
2779 * anything lower than SUPER.
2780 *
2781 * Because gadget.max_speed is only used by composite.c and function
2782 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2783 * to happen so we avoid sending SuperSpeed Capability descriptor
2784 * together with our BOS descriptor as that could confuse host into
2785 * thinking we can handle super speed.
2786 *
2787 * Note that, in fact, we won't even support GetBOS requests when speed
2788 * is less than super speed because we don't have means, yet, to tell
2789 * composite.c that we are USB 2.0 + LPM ECN.
2790 */
2791 if (dwc->revision < DWC3_REVISION_220A)
2792 dwc3_trace(trace_dwc3_gadget,
2793 "Changing max_speed on rev %08x\n",
2794 dwc->revision);
2795
2796 dwc->gadget.max_speed = dwc->maximum_speed;
2797
2798 /*
David Cohena4b9d942013-12-09 15:55:38 -08002799 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2800 * on ep out.
2801 */
2802 dwc->gadget.quirk_ep_out_aligned_size = true;
2803
2804 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002805 * REVISIT: Here we should clear all pending IRQs to be
2806 * sure we're starting from a well known location.
2807 */
2808
2809 ret = dwc3_gadget_init_endpoints(dwc);
2810 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002811 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002812
Felipe Balbi72246da2011-08-19 18:10:58 +03002813 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2814 if (ret) {
2815 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002816 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002817 }
2818
2819 return 0;
2820
Felipe Balbi04c03d12015-12-02 10:06:45 -06002821err5:
2822 kfree(dwc->zlp_buf);
2823
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002824err4:
David Cohene1f80462013-09-11 17:42:47 -07002825 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002826 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2827 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002828
Felipe Balbi72246da2011-08-19 18:10:58 +03002829err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002830 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002831
2832err2:
2833 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2834 dwc->ep0_trb, dwc->ep0_trb_addr);
2835
2836err1:
2837 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2838 dwc->ctrl_req, dwc->ctrl_req_addr);
2839
2840err0:
2841 return ret;
2842}
2843
Felipe Balbi7415f172012-04-30 14:56:33 +03002844/* -------------------------------------------------------------------------- */
2845
Felipe Balbi72246da2011-08-19 18:10:58 +03002846void dwc3_gadget_exit(struct dwc3 *dwc)
2847{
Felipe Balbi72246da2011-08-19 18:10:58 +03002848 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002849
Felipe Balbi72246da2011-08-19 18:10:58 +03002850 dwc3_gadget_free_endpoints(dwc);
2851
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002852 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2853 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002854
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002855 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002856 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002857
2858 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2859 dwc->ep0_trb, dwc->ep0_trb_addr);
2860
2861 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2862 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002863}
Felipe Balbi7415f172012-04-30 14:56:33 +03002864
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002865int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002866{
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002867 if (dwc->pullups_connected) {
Felipe Balbi7415f172012-04-30 14:56:33 +03002868 dwc3_gadget_disable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002869 dwc3_gadget_run_stop(dwc, true, true);
2870 }
Felipe Balbi7415f172012-04-30 14:56:33 +03002871
Felipe Balbi7415f172012-04-30 14:56:33 +03002872 __dwc3_gadget_ep_disable(dwc->eps[0]);
2873 __dwc3_gadget_ep_disable(dwc->eps[1]);
2874
2875 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2876
2877 return 0;
2878}
2879
2880int dwc3_gadget_resume(struct dwc3 *dwc)
2881{
2882 struct dwc3_ep *dep;
2883 int ret;
2884
2885 /* Start with SuperSpeed Default */
2886 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2887
2888 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002889 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2890 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002891 if (ret)
2892 goto err0;
2893
2894 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002895 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2896 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002897 if (ret)
2898 goto err1;
2899
2900 /* begin to receive SETUP packets */
2901 dwc->ep0state = EP0_SETUP_PHASE;
2902 dwc3_ep0_out_start(dwc);
2903
2904 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2905
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002906 if (dwc->pullups_connected) {
2907 dwc3_gadget_enable_irq(dwc);
2908 dwc3_gadget_run_stop(dwc, true, false);
2909 }
2910
Felipe Balbi7415f172012-04-30 14:56:33 +03002911 return 0;
2912
2913err1:
2914 __dwc3_gadget_ep_disable(dwc->eps[0]);
2915
2916err0:
2917 return ret;
2918}