blob: 28d512a65ada54f2571a660d03a8e551c9f6344e [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
154 if (req->queued) {
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);
Pratyush Anandc9fda7d2013-01-14 15:59:38 +0530168 req->queued = 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;
230
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500231 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300232
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300233 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
234 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
235 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300236
237 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
238 do {
239 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
240 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600241 dwc3_trace(trace_dwc3_gadget,
242 "Command Complete --> %d",
Felipe Balbi164f6e12011-08-27 20:29:58 +0300243 DWC3_DEPCMD_STATUS(reg));
Subbaraya Sundeep Bhatta76e838c2015-05-21 15:46:48 +0530244 if (DWC3_DEPCMD_STATUS(reg))
245 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 return 0;
247 }
248
249 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300250 * We can't sleep here, because it is also called from
251 * interrupt context.
252 */
253 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600254 if (!timeout) {
255 dwc3_trace(trace_dwc3_gadget,
256 "Command Timed Out");
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600258 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300259
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200260 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300261 } while (1);
262}
263
264static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200265 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300266{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300267 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300268
269 return dep->trb_pool_dma + offset;
270}
271
272static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
273{
274 struct dwc3 *dwc = dep->dwc;
275
276 if (dep->trb_pool)
277 return 0;
278
Felipe Balbi72246da2011-08-19 18:10:58 +0300279 dep->trb_pool = dma_alloc_coherent(dwc->dev,
280 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
281 &dep->trb_pool_dma, GFP_KERNEL);
282 if (!dep->trb_pool) {
283 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
284 dep->name);
285 return -ENOMEM;
286 }
287
288 return 0;
289}
290
291static void dwc3_free_trb_pool(struct dwc3_ep *dep)
292{
293 struct dwc3 *dwc = dep->dwc;
294
295 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
296 dep->trb_pool, dep->trb_pool_dma);
297
298 dep->trb_pool = NULL;
299 dep->trb_pool_dma = 0;
300}
301
John Younc4509602016-02-16 20:10:53 -0800302static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
303
304/**
305 * dwc3_gadget_start_config - Configure EP resources
306 * @dwc: pointer to our controller context structure
307 * @dep: endpoint that is being enabled
308 *
309 * The assignment of transfer resources cannot perfectly follow the
310 * data book due to the fact that the controller driver does not have
311 * all knowledge of the configuration in advance. It is given this
312 * information piecemeal by the composite gadget framework after every
313 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
314 * programming model in this scenario can cause errors. For two
315 * reasons:
316 *
317 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
318 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
319 * multiple interfaces.
320 *
321 * 2) The databook does not mention doing more DEPXFERCFG for new
322 * endpoint on alt setting (8.1.6).
323 *
324 * The following simplified method is used instead:
325 *
326 * All hardware endpoints can be assigned a transfer resource and this
327 * setting will stay persistent until either a core reset or
328 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
329 * do DEPXFERCFG for every hardware endpoint as well. We are
330 * guaranteed that there are as many transfer resources as endpoints.
331 *
332 * This function is called for each endpoint when it is being enabled
333 * but is triggered only when called for EP0-out, which always happens
334 * first, and which should only happen in one of the above conditions.
335 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300336static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
337{
338 struct dwc3_gadget_ep_cmd_params params;
339 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800340 int i;
341 int ret;
342
343 if (dep->number)
344 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300345
346 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800347 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300348
John Younc4509602016-02-16 20:10:53 -0800349 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
350 if (ret)
351 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300352
John Younc4509602016-02-16 20:10:53 -0800353 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
354 struct dwc3_ep *dep = dwc->eps[i];
355
356 if (!dep)
357 continue;
358
359 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
360 if (ret)
361 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300362 }
363
364 return 0;
365}
366
367static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200368 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300369 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600370 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300371{
372 struct dwc3_gadget_ep_cmd_params params;
373
374 memset(&params, 0x00, sizeof(params));
375
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300376 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900377 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
378
379 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800380 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Chanho Parkd2e9a132012-08-31 16:54:07 +0900381 u32 burst = dep->endpoint.maxburst - 1;
382
383 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
384 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300385
Felipe Balbi4b345c92012-07-16 14:08:16 +0300386 if (ignore)
387 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
388
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600389 if (restore) {
390 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
391 params.param2 |= dep->saved_state;
392 }
393
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300394 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
395 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300396
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200397 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300398 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
399 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300400 dep->stream_capable = true;
401 }
402
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500403 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300404 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300405
406 /*
407 * We are doing 1:1 mapping for endpoints, meaning
408 * Physical Endpoints 2 maps to Logical Endpoint 2 and
409 * so on. We consider the direction bit as part of the physical
410 * endpoint number. So USB endpoint 0x81 is 0x03.
411 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300412 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300413
414 /*
415 * We must use the lower 16 TX FIFOs even though
416 * HW might have more
417 */
418 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300419 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300420
421 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300422 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300423 dep->interval = 1 << (desc->bInterval - 1);
424 }
425
426 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
427 DWC3_DEPCMD_SETEPCONFIG, &params);
428}
429
430static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
431{
432 struct dwc3_gadget_ep_cmd_params params;
433
434 memset(&params, 0x00, sizeof(params));
435
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300436 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300437
438 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
439 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
440}
441
442/**
443 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
444 * @dep: endpoint to be initialized
445 * @desc: USB Endpoint Descriptor
446 *
447 * Caller should take care of locking
448 */
449static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200450 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300451 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600452 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300453{
454 struct dwc3 *dwc = dep->dwc;
455 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300456 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300457
Felipe Balbi73815282015-01-27 13:48:14 -0600458 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300459
Felipe Balbi72246da2011-08-19 18:10:58 +0300460 if (!(dep->flags & DWC3_EP_ENABLED)) {
461 ret = dwc3_gadget_start_config(dwc, dep);
462 if (ret)
463 return ret;
464 }
465
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600466 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
467 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 if (ret)
469 return ret;
470
471 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200472 struct dwc3_trb *trb_st_hw;
473 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300474
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200475 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200476 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300477 dep->type = usb_endpoint_type(desc);
478 dep->flags |= DWC3_EP_ENABLED;
479
480 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
481 reg |= DWC3_DALEPENA_EP(dep->number);
482 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
483
484 if (!usb_endpoint_xfer_isoc(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200485 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300486
Paul Zimmerman1d046792012-02-15 18:56:56 -0800487 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 trb_st_hw = &dep->trb_pool[0];
489
Felipe Balbif6bafc62012-02-06 11:04:53 +0200490 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700491 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300492
Felipe Balbif6bafc62012-02-06 11:04:53 +0200493 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
494 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
495 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
496 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300497 }
498
Felipe Balbie901aa12016-03-16 14:01:37 +0200499out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500500 switch (usb_endpoint_type(desc)) {
501 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200502 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500503 break;
504 case USB_ENDPOINT_XFER_ISOC:
505 strlcat(dep->name, "-isoc", sizeof(dep->name));
506 break;
507 case USB_ENDPOINT_XFER_BULK:
508 strlcat(dep->name, "-bulk", sizeof(dep->name));
509 break;
510 case USB_ENDPOINT_XFER_INT:
511 strlcat(dep->name, "-int", sizeof(dep->name));
512 break;
513 default:
514 dev_err(dwc->dev, "invalid endpoint transfer type\n");
515 }
516
Felipe Balbi72246da2011-08-19 18:10:58 +0300517 return 0;
518}
519
Paul Zimmermanb992e682012-04-27 14:17:35 +0300520static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200521static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300522{
523 struct dwc3_request *req;
524
Felipe Balbiea53b882012-02-17 12:10:04 +0200525 if (!list_empty(&dep->req_queued)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300526 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200527
Pratyush Anand57911502012-07-06 15:19:10 +0530528 /* - giveback all requests to gadget driver */
Pratyush Anand15916332012-06-15 11:54:36 +0530529 while (!list_empty(&dep->req_queued)) {
530 req = next_request(&dep->req_queued);
531
532 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
533 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200534 }
535
Felipe Balbi72246da2011-08-19 18:10:58 +0300536 while (!list_empty(&dep->request_list)) {
537 req = next_request(&dep->request_list);
538
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200539 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300541}
542
543/**
544 * __dwc3_gadget_ep_disable - Disables a HW endpoint
545 * @dep: the endpoint to disable
546 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200547 * This function also removes requests which are currently processed ny the
548 * hardware and those which are not yet scheduled.
549 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300551static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
552{
553 struct dwc3 *dwc = dep->dwc;
554 u32 reg;
555
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500556 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
557
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200558 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300559
Felipe Balbi687ef982014-04-16 10:30:33 -0500560 /* make sure HW endpoint isn't stalled */
561 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500562 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500563
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
565 reg &= ~DWC3_DALEPENA_EP(dep->number);
566 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
567
Felipe Balbi879631a2011-09-30 10:58:47 +0300568 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200569 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200570 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300571 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300572 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
Felipe Balbiaa739972015-07-20 14:48:13 -0500574 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
575 dep->number >> 1,
576 (dep->number & 1) ? "in" : "out");
577
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 return 0;
579}
580
581/* -------------------------------------------------------------------------- */
582
583static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
584 const struct usb_endpoint_descriptor *desc)
585{
586 return -EINVAL;
587}
588
589static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
590{
591 return -EINVAL;
592}
593
594/* -------------------------------------------------------------------------- */
595
596static int dwc3_gadget_ep_enable(struct usb_ep *ep,
597 const struct usb_endpoint_descriptor *desc)
598{
599 struct dwc3_ep *dep;
600 struct dwc3 *dwc;
601 unsigned long flags;
602 int ret;
603
604 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
605 pr_debug("dwc3: invalid parameters\n");
606 return -EINVAL;
607 }
608
609 if (!desc->wMaxPacketSize) {
610 pr_debug("dwc3: missing wMaxPacketSize\n");
611 return -EINVAL;
612 }
613
614 dep = to_dwc3_ep(ep);
615 dwc = dep->dwc;
616
Felipe Balbi95ca9612015-12-10 13:08:20 -0600617 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
618 "%s is already enabled\n",
619 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300620 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300621
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600623 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 spin_unlock_irqrestore(&dwc->lock, flags);
625
626 return ret;
627}
628
629static int dwc3_gadget_ep_disable(struct usb_ep *ep)
630{
631 struct dwc3_ep *dep;
632 struct dwc3 *dwc;
633 unsigned long flags;
634 int ret;
635
636 if (!ep) {
637 pr_debug("dwc3: invalid parameters\n");
638 return -EINVAL;
639 }
640
641 dep = to_dwc3_ep(ep);
642 dwc = dep->dwc;
643
Felipe Balbi95ca9612015-12-10 13:08:20 -0600644 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
645 "%s is already disabled\n",
646 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300648
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 spin_lock_irqsave(&dwc->lock, flags);
650 ret = __dwc3_gadget_ep_disable(dep);
651 spin_unlock_irqrestore(&dwc->lock, flags);
652
653 return ret;
654}
655
656static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
657 gfp_t gfp_flags)
658{
659 struct dwc3_request *req;
660 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300661
662 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900663 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300664 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300665
666 req->epnum = dep->number;
667 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300668
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500669 trace_dwc3_alloc_request(req);
670
Felipe Balbi72246da2011-08-19 18:10:58 +0300671 return &req->request;
672}
673
674static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
675 struct usb_request *request)
676{
677 struct dwc3_request *req = to_dwc3_request(request);
678
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500679 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 kfree(req);
681}
682
Felipe Balbic71fc372011-11-22 11:37:34 +0200683/**
684 * dwc3_prepare_one_trb - setup one TRB from one request
685 * @dep: endpoint for which this request is prepared
686 * @req: dwc3_request pointer
687 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200688static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200689 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530690 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200691{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200692 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200693
Felipe Balbi73815282015-01-27 13:48:14 -0600694 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200695 dep->name, req, (unsigned long long) dma,
696 length, last ? " last" : "",
697 chain ? " chain" : "");
698
Pratyush Anand915e2022013-01-14 15:59:35 +0530699
700 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200701
Felipe Balbieeb720f2011-11-28 12:46:59 +0200702 if (!req->trb) {
703 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200704 req->trb = trb;
705 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530706 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200707 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200708
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530709 dep->free_slot++;
Zhuang Jin Can5cd8c482014-05-16 05:57:57 +0800710 /* Skip the LINK-TRB on ISOC */
711 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
712 usb_endpoint_xfer_isoc(dep->endpoint.desc))
713 dep->free_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530714
Felipe Balbif6bafc62012-02-06 11:04:53 +0200715 trb->size = DWC3_TRB_SIZE_LENGTH(length);
716 trb->bpl = lower_32_bits(dma);
717 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200718
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200719 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200720 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200721 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200722 break;
723
724 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530725 if (!node)
726 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
727 else
728 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200729
730 /* always enable Interrupt on Missed ISOC */
731 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200732 break;
733
734 case USB_ENDPOINT_XFER_BULK:
735 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200736 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200737 break;
738 default:
739 /*
740 * This is only possible with faulty memory because we
741 * checked it already :)
742 */
743 BUG();
744 }
745
Felipe Balbica4d44e2016-03-10 13:53:27 +0200746 /* always enable Continue on Short Packet */
747 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600748
Felipe Balbica4d44e2016-03-10 13:53:27 +0200749 if (!req->request.no_interrupt)
750 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
751
752 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530753 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200754
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530755 if (chain)
756 trb->ctrl |= DWC3_TRB_CTRL_CHN;
757
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200758 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200759 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
760
761 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500762
763 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200764}
765
Felipe Balbi72246da2011-08-19 18:10:58 +0300766/*
767 * dwc3_prepare_trbs - setup TRBs from requests
768 * @dep: endpoint for which requests are being prepared
769 * @starting: true if the endpoint is idle and no requests are queued.
770 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800771 * The function goes through the requests list and sets up TRBs for the
772 * transfers. The function returns once there are no more TRBs available or
773 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300774 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200775static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300776{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200777 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200779 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200780 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300781
782 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
783
784 /* the first request must not be queued */
785 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200786
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200787 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200788 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200789 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
790 if (trbs_left > max)
791 trbs_left = max;
792 }
793
Felipe Balbi72246da2011-08-19 18:10:58 +0300794 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800795 * If busy & slot are equal than it is either full or empty. If we are
796 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 * full and don't do anything
798 */
799 if (!trbs_left) {
800 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200801 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300802 trbs_left = DWC3_TRB_NUM;
803 /*
804 * In case we start from scratch, we queue the ISOC requests
805 * starting from slot 1. This is done because we use ring
806 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800807 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 * after the first request so we start at slot 1 and have
809 * 7 requests proceed before we hit the first IOC.
810 * Other transfer types don't use the ring buffer and are
811 * processed from the first TRB until the last one. Since we
812 * don't wrap around we have to start at the beginning.
813 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200814 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300815 dep->busy_slot = 1;
816 dep->free_slot = 1;
817 } else {
818 dep->busy_slot = 0;
819 dep->free_slot = 0;
820 }
821 }
822
823 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200824 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200825 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300826
827 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200828 unsigned length;
829 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530830 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300831
Felipe Balbieeb720f2011-11-28 12:46:59 +0200832 if (req->request.num_mapped_sgs > 0) {
833 struct usb_request *request = &req->request;
834 struct scatterlist *sg = request->sg;
835 struct scatterlist *s;
836 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300837
Felipe Balbieeb720f2011-11-28 12:46:59 +0200838 for_each_sg(sg, s, request->num_mapped_sgs, i) {
839 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300840
Felipe Balbieeb720f2011-11-28 12:46:59 +0200841 length = sg_dma_len(s);
842 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300843
Paul Zimmerman1d046792012-02-15 18:56:56 -0800844 if (i == (request->num_mapped_sgs - 1) ||
845 sg_is_last(s)) {
Amit Virdiec512fb2015-01-13 14:27:20 +0530846 if (list_empty(&dep->request_list))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530847 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200848 chain = false;
849 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300850
Felipe Balbieeb720f2011-11-28 12:46:59 +0200851 trbs_left--;
852 if (!trbs_left)
853 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300854
Felipe Balbieeb720f2011-11-28 12:46:59 +0200855 if (last_one)
856 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300857
Felipe Balbieeb720f2011-11-28 12:46:59 +0200858 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530859 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300860
Felipe Balbieeb720f2011-11-28 12:46:59 +0200861 if (last_one)
862 break;
863 }
Amit Virdi39e60632015-01-13 14:27:21 +0530864
865 if (last_one)
866 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300867 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200868 dma = req->request.dma;
869 length = req->request.length;
870 trbs_left--;
871
872 if (!trbs_left)
873 last_one = 1;
874
875 /* Is this the last request? */
876 if (list_is_last(&req->list, &dep->request_list))
877 last_one = 1;
878
879 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530880 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200881
882 if (last_one)
883 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300885 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300886}
887
888static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
889 int start_new)
890{
891 struct dwc3_gadget_ep_cmd_params params;
892 struct dwc3_request *req;
893 struct dwc3 *dwc = dep->dwc;
894 int ret;
895 u32 cmd;
896
897 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600898 dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300899 return -EBUSY;
900 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
902 /*
903 * If we are getting here after a short-out-packet we don't enqueue any
904 * new requests as we try to set the IOC bit only on the last request.
905 */
906 if (start_new) {
907 if (list_empty(&dep->req_queued))
908 dwc3_prepare_trbs(dep, start_new);
909
910 /* req points to the first request which will be sent */
911 req = next_request(&dep->req_queued);
912 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200913 dwc3_prepare_trbs(dep, start_new);
914
Felipe Balbi72246da2011-08-19 18:10:58 +0300915 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800916 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300917 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200918 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300919 }
920 if (!req) {
921 dep->flags |= DWC3_EP_PENDING_REQUEST;
922 return 0;
923 }
924
925 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300926
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530927 if (start_new) {
928 params.param0 = upper_32_bits(req->trb_dma);
929 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300930 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530931 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300932 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530933 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300934
935 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
936 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
937 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300938 /*
939 * FIXME we need to iterate over the list of requests
940 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800941 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300942 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200943 usb_gadget_unmap_request(&dwc->gadget, &req->request,
944 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 list_del(&req->list);
946 return ret;
947 }
948
949 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200950
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000951 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +0300952 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000953 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +0300954 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000955 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200956
Felipe Balbi72246da2011-08-19 18:10:58 +0300957 return 0;
958}
959
Pratyush Anandd6d6ec72012-05-25 18:54:56 +0530960static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
961 struct dwc3_ep *dep, u32 cur_uf)
962{
963 u32 uf;
964
965 if (list_empty(&dep->request_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600966 dwc3_trace(trace_dwc3_gadget,
967 "ISOC ep %s run out for requests",
968 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +0530969 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +0530970 return;
971 }
972
973 /* 4 micro frames in the future */
974 uf = cur_uf + dep->interval * 4;
975
976 __dwc3_gadget_kick_transfer(dep, uf, 1);
977}
978
979static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
980 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
981{
982 u32 cur_uf, mask;
983
984 mask = ~(dep->interval - 1);
985 cur_uf = event->parameters & mask;
986
987 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
988}
989
Felipe Balbi72246da2011-08-19 18:10:58 +0300990static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
991{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200992 struct dwc3 *dwc = dep->dwc;
993 int ret;
994
Felipe Balbibb423982015-11-16 15:31:21 -0600995 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -0600996 dwc3_trace(trace_dwc3_gadget,
997 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -0600998 &req->request, dep->endpoint.name);
999 return -ESHUTDOWN;
1000 }
1001
1002 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1003 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001004 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1005 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001006 return -EINVAL;
1007 }
1008
Felipe Balbi72246da2011-08-19 18:10:58 +03001009 req->request.actual = 0;
1010 req->request.status = -EINPROGRESS;
1011 req->direction = dep->direction;
1012 req->epnum = dep->number;
1013
Felipe Balbife84f522015-09-01 09:01:38 -05001014 trace_dwc3_ep_queue(req);
1015
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 /*
1017 * We only add to our list of requests now and
1018 * start consuming the list once we get XferNotReady
1019 * IRQ.
1020 *
1021 * That way, we avoid doing anything that we don't need
1022 * to do now and defer it until the point we receive a
1023 * particular token from the Host side.
1024 *
1025 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001026 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001027 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001028 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1029 dep->direction);
1030 if (ret)
1031 return ret;
1032
Felipe Balbi72246da2011-08-19 18:10:58 +03001033 list_add_tail(&req->list, &dep->request_list);
1034
1035 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001036 * If there are no pending requests and the endpoint isn't already
1037 * busy, we will just start the request straight away.
1038 *
1039 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1040 * little bit faster.
1041 */
1042 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001043 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001044 !(dep->flags & DWC3_EP_BUSY)) {
1045 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbia8f32812015-09-16 10:40:07 -05001046 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001047 }
1048
1049 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001050 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001051 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001052 * 1. XferNotReady with empty list of requests. We need to kick the
1053 * transfer here in that situation, otherwise we will be NAKing
1054 * forever. If we get XferNotReady before gadget driver has a
1055 * chance to queue a request, we will ACK the IRQ but won't be
1056 * able to receive the data until the next request is queued.
1057 * The following code is handling exactly that.
1058 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001059 */
1060 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301061 /*
1062 * If xfernotready is already elapsed and it is a case
1063 * of isoc transfer, then issue END TRANSFER, so that
1064 * you can receive xfernotready again and can have
1065 * notion of current microframe.
1066 */
1067 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301068 if (list_empty(&dep->req_queued)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001069 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301070 dep->flags = DWC3_EP_ENABLED;
1071 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301072 return 0;
1073 }
1074
Felipe Balbib511e5e2012-06-06 12:00:50 +03001075 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbi89185912015-09-15 09:49:14 -05001076 if (!ret)
1077 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1078
Felipe Balbia8f32812015-09-16 10:40:07 -05001079 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001080 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001081
Felipe Balbib511e5e2012-06-06 12:00:50 +03001082 /*
1083 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1084 * kick the transfer here after queuing a request, otherwise the
1085 * core may not see the modified TRB(s).
1086 */
1087 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301088 (dep->flags & DWC3_EP_BUSY) &&
1089 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001090 WARN_ON_ONCE(!dep->resource_index);
1091 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001092 false);
Felipe Balbia8f32812015-09-16 10:40:07 -05001093 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001094 }
1095
Felipe Balbib997ada2012-07-26 13:26:50 +03001096 /*
1097 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1098 * right away, otherwise host will not know we have streams to be
1099 * handled.
1100 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001101 if (dep->stream_capable)
Felipe Balbib997ada2012-07-26 13:26:50 +03001102 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbib997ada2012-07-26 13:26:50 +03001103
Felipe Balbia8f32812015-09-16 10:40:07 -05001104out:
1105 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001106 dwc3_trace(trace_dwc3_gadget,
1107 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001108 dep->name);
1109 if (ret == -EBUSY)
1110 ret = 0;
1111
1112 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001113}
1114
Felipe Balbi04c03d12015-12-02 10:06:45 -06001115static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1116 struct usb_request *request)
1117{
1118 dwc3_gadget_ep_free_request(ep, request);
1119}
1120
1121static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1122{
1123 struct dwc3_request *req;
1124 struct usb_request *request;
1125 struct usb_ep *ep = &dep->endpoint;
1126
1127 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1128 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1129 if (!request)
1130 return -ENOMEM;
1131
1132 request->length = 0;
1133 request->buf = dwc->zlp_buf;
1134 request->complete = __dwc3_gadget_ep_zlp_complete;
1135
1136 req = to_dwc3_request(request);
1137
1138 return __dwc3_gadget_ep_queue(dep, req);
1139}
1140
Felipe Balbi72246da2011-08-19 18:10:58 +03001141static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1142 gfp_t gfp_flags)
1143{
1144 struct dwc3_request *req = to_dwc3_request(request);
1145 struct dwc3_ep *dep = to_dwc3_ep(ep);
1146 struct dwc3 *dwc = dep->dwc;
1147
1148 unsigned long flags;
1149
1150 int ret;
1151
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001152 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001153 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001154
1155 /*
1156 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1157 * setting request->zero, instead of doing magic, we will just queue an
1158 * extra usb_request ourselves so that it gets handled the same way as
1159 * any other request.
1160 */
John Yound92618982015-12-22 12:23:20 -08001161 if (ret == 0 && request->zero && request->length &&
1162 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001163 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1164
Felipe Balbi72246da2011-08-19 18:10:58 +03001165 spin_unlock_irqrestore(&dwc->lock, flags);
1166
1167 return ret;
1168}
1169
1170static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1171 struct usb_request *request)
1172{
1173 struct dwc3_request *req = to_dwc3_request(request);
1174 struct dwc3_request *r = NULL;
1175
1176 struct dwc3_ep *dep = to_dwc3_ep(ep);
1177 struct dwc3 *dwc = dep->dwc;
1178
1179 unsigned long flags;
1180 int ret = 0;
1181
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001182 trace_dwc3_ep_dequeue(req);
1183
Felipe Balbi72246da2011-08-19 18:10:58 +03001184 spin_lock_irqsave(&dwc->lock, flags);
1185
1186 list_for_each_entry(r, &dep->request_list, list) {
1187 if (r == req)
1188 break;
1189 }
1190
1191 if (r != req) {
1192 list_for_each_entry(r, &dep->req_queued, list) {
1193 if (r == req)
1194 break;
1195 }
1196 if (r == req) {
1197 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001198 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301199 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001200 }
1201 dev_err(dwc->dev, "request %p was not queued to %s\n",
1202 request, ep->name);
1203 ret = -EINVAL;
1204 goto out0;
1205 }
1206
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301207out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001208 /* giveback the request */
1209 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1210
1211out0:
1212 spin_unlock_irqrestore(&dwc->lock, flags);
1213
1214 return ret;
1215}
1216
Felipe Balbi7a608552014-09-24 14:19:52 -05001217int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001218{
1219 struct dwc3_gadget_ep_cmd_params params;
1220 struct dwc3 *dwc = dep->dwc;
1221 int ret;
1222
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001223 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1224 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1225 return -EINVAL;
1226 }
1227
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 memset(&params, 0x00, sizeof(params));
1229
1230 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001231 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
1232 (!list_empty(&dep->req_queued) ||
1233 !list_empty(&dep->request_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001234 dwc3_trace(trace_dwc3_gadget,
1235 "%s: pending request, cannot halt\n",
Felipe Balbi7a608552014-09-24 14:19:52 -05001236 dep->name);
1237 return -EAGAIN;
1238 }
1239
Felipe Balbi72246da2011-08-19 18:10:58 +03001240 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1241 DWC3_DEPCMD_SETSTALL, &params);
1242 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001243 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001244 dep->name);
1245 else
1246 dep->flags |= DWC3_EP_STALL;
1247 } else {
1248 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1249 DWC3_DEPCMD_CLEARSTALL, &params);
1250 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001251 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001252 dep->name);
1253 else
Alan Sterna535d812013-11-01 12:05:12 -04001254 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001255 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001256
Felipe Balbi72246da2011-08-19 18:10:58 +03001257 return ret;
1258}
1259
1260static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1261{
1262 struct dwc3_ep *dep = to_dwc3_ep(ep);
1263 struct dwc3 *dwc = dep->dwc;
1264
1265 unsigned long flags;
1266
1267 int ret;
1268
1269 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001270 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001271 spin_unlock_irqrestore(&dwc->lock, flags);
1272
1273 return ret;
1274}
1275
1276static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1277{
1278 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001279 struct dwc3 *dwc = dep->dwc;
1280 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001281 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001282
Paul Zimmerman249a4562012-02-24 17:32:16 -08001283 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001284 dep->flags |= DWC3_EP_WEDGE;
1285
Pratyush Anand08f0d962012-06-25 22:40:43 +05301286 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001287 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301288 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001289 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001290 spin_unlock_irqrestore(&dwc->lock, flags);
1291
1292 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001293}
1294
1295/* -------------------------------------------------------------------------- */
1296
1297static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1298 .bLength = USB_DT_ENDPOINT_SIZE,
1299 .bDescriptorType = USB_DT_ENDPOINT,
1300 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1301};
1302
1303static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1304 .enable = dwc3_gadget_ep0_enable,
1305 .disable = dwc3_gadget_ep0_disable,
1306 .alloc_request = dwc3_gadget_ep_alloc_request,
1307 .free_request = dwc3_gadget_ep_free_request,
1308 .queue = dwc3_gadget_ep0_queue,
1309 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301310 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001311 .set_wedge = dwc3_gadget_ep_set_wedge,
1312};
1313
1314static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1315 .enable = dwc3_gadget_ep_enable,
1316 .disable = dwc3_gadget_ep_disable,
1317 .alloc_request = dwc3_gadget_ep_alloc_request,
1318 .free_request = dwc3_gadget_ep_free_request,
1319 .queue = dwc3_gadget_ep_queue,
1320 .dequeue = dwc3_gadget_ep_dequeue,
1321 .set_halt = dwc3_gadget_ep_set_halt,
1322 .set_wedge = dwc3_gadget_ep_set_wedge,
1323};
1324
1325/* -------------------------------------------------------------------------- */
1326
1327static int dwc3_gadget_get_frame(struct usb_gadget *g)
1328{
1329 struct dwc3 *dwc = gadget_to_dwc(g);
1330 u32 reg;
1331
1332 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1333 return DWC3_DSTS_SOFFN(reg);
1334}
1335
1336static int dwc3_gadget_wakeup(struct usb_gadget *g)
1337{
1338 struct dwc3 *dwc = gadget_to_dwc(g);
1339
1340 unsigned long timeout;
1341 unsigned long flags;
1342
1343 u32 reg;
1344
1345 int ret = 0;
1346
1347 u8 link_state;
1348 u8 speed;
1349
1350 spin_lock_irqsave(&dwc->lock, flags);
1351
1352 /*
1353 * According to the Databook Remote wakeup request should
1354 * be issued only when the device is in early suspend state.
1355 *
1356 * We can check that via USB Link State bits in DSTS register.
1357 */
1358 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1359
1360 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001361 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1362 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001363 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001364 ret = -EINVAL;
1365 goto out;
1366 }
1367
1368 link_state = DWC3_DSTS_USBLNKST(reg);
1369
1370 switch (link_state) {
1371 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1372 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1373 break;
1374 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001375 dwc3_trace(trace_dwc3_gadget,
1376 "can't wakeup from '%s'\n",
1377 dwc3_gadget_link_string(link_state));
Felipe Balbi72246da2011-08-19 18:10:58 +03001378 ret = -EINVAL;
1379 goto out;
1380 }
1381
Felipe Balbi8598bde2012-01-02 18:55:57 +02001382 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1383 if (ret < 0) {
1384 dev_err(dwc->dev, "failed to put link in Recovery\n");
1385 goto out;
1386 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001387
Paul Zimmerman802fde92012-04-27 13:10:52 +03001388 /* Recent versions do this automatically */
1389 if (dwc->revision < DWC3_REVISION_194A) {
1390 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001391 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001392 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1393 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1394 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001395
Paul Zimmerman1d046792012-02-15 18:56:56 -08001396 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001397 timeout = jiffies + msecs_to_jiffies(100);
1398
Paul Zimmerman1d046792012-02-15 18:56:56 -08001399 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001400 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1401
1402 /* in HS, means ON */
1403 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1404 break;
1405 }
1406
1407 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1408 dev_err(dwc->dev, "failed to send remote wakeup\n");
1409 ret = -EINVAL;
1410 }
1411
1412out:
1413 spin_unlock_irqrestore(&dwc->lock, flags);
1414
1415 return ret;
1416}
1417
1418static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1419 int is_selfpowered)
1420{
1421 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001422 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001423
Paul Zimmerman249a4562012-02-24 17:32:16 -08001424 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001425 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001426 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001427
1428 return 0;
1429}
1430
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001431static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001432{
1433 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001434 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001435
1436 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001437 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001438 if (dwc->revision <= DWC3_REVISION_187A) {
1439 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1440 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1441 }
1442
1443 if (dwc->revision >= DWC3_REVISION_194A)
1444 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1445 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001446
1447 if (dwc->has_hibernation)
1448 reg |= DWC3_DCTL_KEEP_CONNECT;
1449
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001450 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001451 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001452 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001453
1454 if (dwc->has_hibernation && !suspend)
1455 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1456
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001457 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001458 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001459
1460 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1461
1462 do {
1463 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1464 if (is_on) {
1465 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1466 break;
1467 } else {
1468 if (reg & DWC3_DSTS_DEVCTRLHLT)
1469 break;
1470 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001471 timeout--;
1472 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301473 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001474 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 } while (1);
1476
Felipe Balbi73815282015-01-27 13:48:14 -06001477 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001478 dwc->gadget_driver
1479 ? dwc->gadget_driver->function : "no-function",
1480 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301481
1482 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001483}
1484
1485static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1486{
1487 struct dwc3 *dwc = gadget_to_dwc(g);
1488 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301489 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001490
1491 is_on = !!is_on;
1492
1493 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001494 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001495 spin_unlock_irqrestore(&dwc->lock, flags);
1496
Pratyush Anand6f17f742012-07-02 10:21:55 +05301497 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001498}
1499
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001500static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1501{
1502 u32 reg;
1503
1504 /* Enable all but Start and End of Frame IRQs */
1505 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1506 DWC3_DEVTEN_EVNTOVERFLOWEN |
1507 DWC3_DEVTEN_CMDCMPLTEN |
1508 DWC3_DEVTEN_ERRTICERREN |
1509 DWC3_DEVTEN_WKUPEVTEN |
1510 DWC3_DEVTEN_ULSTCNGEN |
1511 DWC3_DEVTEN_CONNECTDONEEN |
1512 DWC3_DEVTEN_USBRSTEN |
1513 DWC3_DEVTEN_DISCONNEVTEN);
1514
1515 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1516}
1517
1518static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1519{
1520 /* mask all interrupts */
1521 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1522}
1523
1524static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001525static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001526
Felipe Balbi72246da2011-08-19 18:10:58 +03001527static int dwc3_gadget_start(struct usb_gadget *g,
1528 struct usb_gadget_driver *driver)
1529{
1530 struct dwc3 *dwc = gadget_to_dwc(g);
1531 struct dwc3_ep *dep;
1532 unsigned long flags;
1533 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001534 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001535 u32 reg;
1536
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001537 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1538 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbie8adfc32013-06-12 21:11:14 +03001539 IRQF_SHARED, "dwc3", dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001540 if (ret) {
1541 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1542 irq, ret);
1543 goto err0;
1544 }
1545
Felipe Balbi72246da2011-08-19 18:10:58 +03001546 spin_lock_irqsave(&dwc->lock, flags);
1547
1548 if (dwc->gadget_driver) {
1549 dev_err(dwc->dev, "%s is already bound to %s\n",
1550 dwc->gadget.name,
1551 dwc->gadget_driver->driver.name);
1552 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001553 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001554 }
1555
1556 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001557
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1559 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001560
1561 /**
1562 * WORKAROUND: DWC3 revision < 2.20a have an issue
1563 * which would cause metastability state on Run/Stop
1564 * bit if we try to force the IP to USB2-only mode.
1565 *
1566 * Because of that, we cannot configure the IP to any
1567 * speed other than the SuperSpeed
1568 *
1569 * Refers to:
1570 *
1571 * STAR#9000525659: Clock Domain Crossing on DCTL in
1572 * USB 2.0 Mode
1573 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001574 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001575 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001576 } else {
1577 switch (dwc->maximum_speed) {
1578 case USB_SPEED_LOW:
1579 reg |= DWC3_DSTS_LOWSPEED;
1580 break;
1581 case USB_SPEED_FULL:
1582 reg |= DWC3_DSTS_FULLSPEED1;
1583 break;
1584 case USB_SPEED_HIGH:
1585 reg |= DWC3_DSTS_HIGHSPEED;
1586 break;
John Youn75808622016-02-05 17:09:13 -08001587 case USB_SPEED_SUPER_PLUS:
1588 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1589 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001590 default:
John Youn77966eb2016-02-19 17:31:01 -08001591 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1592 dwc->maximum_speed);
1593 /* fall through */
1594 case USB_SPEED_SUPER:
1595 reg |= DWC3_DCFG_SUPERSPEED;
1596 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001597 }
1598 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001599 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1600
1601 /* Start with SuperSpeed Default */
1602 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1603
1604 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001605 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1606 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 if (ret) {
1608 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001609 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001610 }
1611
1612 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001613 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1614 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 if (ret) {
1616 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001617 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001618 }
1619
1620 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001621 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001622 dwc3_ep0_out_start(dwc);
1623
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001624 dwc3_gadget_enable_irq(dwc);
1625
Felipe Balbi72246da2011-08-19 18:10:58 +03001626 spin_unlock_irqrestore(&dwc->lock, flags);
1627
1628 return 0;
1629
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001630err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001631 __dwc3_gadget_ep_disable(dwc->eps[0]);
1632
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001633err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001634 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001635
1636err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 spin_unlock_irqrestore(&dwc->lock, flags);
1638
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001639 free_irq(irq, dwc);
1640
1641err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001642 return ret;
1643}
1644
Felipe Balbi22835b82014-10-17 12:05:12 -05001645static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001646{
1647 struct dwc3 *dwc = gadget_to_dwc(g);
1648 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001649 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001650
1651 spin_lock_irqsave(&dwc->lock, flags);
1652
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001653 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 __dwc3_gadget_ep_disable(dwc->eps[0]);
1655 __dwc3_gadget_ep_disable(dwc->eps[1]);
1656
1657 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001658
1659 spin_unlock_irqrestore(&dwc->lock, flags);
1660
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001661 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1662 free_irq(irq, dwc);
1663
Felipe Balbi72246da2011-08-19 18:10:58 +03001664 return 0;
1665}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001666
Felipe Balbi72246da2011-08-19 18:10:58 +03001667static const struct usb_gadget_ops dwc3_gadget_ops = {
1668 .get_frame = dwc3_gadget_get_frame,
1669 .wakeup = dwc3_gadget_wakeup,
1670 .set_selfpowered = dwc3_gadget_set_selfpowered,
1671 .pullup = dwc3_gadget_pullup,
1672 .udc_start = dwc3_gadget_start,
1673 .udc_stop = dwc3_gadget_stop,
1674};
1675
1676/* -------------------------------------------------------------------------- */
1677
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001678static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1679 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001680{
1681 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001682 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001683
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001684 for (i = 0; i < num; i++) {
1685 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001686
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001688 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001689 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001690
1691 dep->dwc = dwc;
1692 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001693 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001694 dwc->eps[epnum] = dep;
1695
1696 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1697 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001698
Felipe Balbi72246da2011-08-19 18:10:58 +03001699 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001700
Felipe Balbi73815282015-01-27 13:48:14 -06001701 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001702
Felipe Balbi72246da2011-08-19 18:10:58 +03001703 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001704 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301705 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001706 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1707 if (!epnum)
1708 dwc->gadget.ep0 = &dep->endpoint;
1709 } else {
1710 int ret;
1711
Robert Baldygae117e742013-12-13 12:23:38 +01001712 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001713 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1715 list_add_tail(&dep->endpoint.ep_list,
1716 &dwc->gadget.ep_list);
1717
1718 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001719 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001720 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001722
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001723 if (epnum == 0 || epnum == 1) {
1724 dep->endpoint.caps.type_control = true;
1725 } else {
1726 dep->endpoint.caps.type_iso = true;
1727 dep->endpoint.caps.type_bulk = true;
1728 dep->endpoint.caps.type_int = true;
1729 }
1730
1731 dep->endpoint.caps.dir_in = !!direction;
1732 dep->endpoint.caps.dir_out = !direction;
1733
Felipe Balbi72246da2011-08-19 18:10:58 +03001734 INIT_LIST_HEAD(&dep->request_list);
1735 INIT_LIST_HEAD(&dep->req_queued);
1736 }
1737
1738 return 0;
1739}
1740
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001741static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1742{
1743 int ret;
1744
1745 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1746
1747 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1748 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001749 dwc3_trace(trace_dwc3_gadget,
1750 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001751 return ret;
1752 }
1753
1754 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1755 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001756 dwc3_trace(trace_dwc3_gadget,
1757 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001758 return ret;
1759 }
1760
1761 return 0;
1762}
1763
Felipe Balbi72246da2011-08-19 18:10:58 +03001764static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1765{
1766 struct dwc3_ep *dep;
1767 u8 epnum;
1768
1769 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1770 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001771 if (!dep)
1772 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301773 /*
1774 * Physical endpoints 0 and 1 are special; they form the
1775 * bi-directional USB endpoint 0.
1776 *
1777 * For those two physical endpoints, we don't allocate a TRB
1778 * pool nor do we add them the endpoints list. Due to that, we
1779 * shouldn't do these two operations otherwise we would end up
1780 * with all sorts of bugs when removing dwc3.ko.
1781 */
1782 if (epnum != 0 && epnum != 1) {
1783 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301785 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001786
1787 kfree(dep);
1788 }
1789}
1790
Felipe Balbi72246da2011-08-19 18:10:58 +03001791/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001792
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301793static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1794 struct dwc3_request *req, struct dwc3_trb *trb,
1795 const struct dwc3_event_depevt *event, int status)
1796{
1797 unsigned int count;
1798 unsigned int s_pkt = 0;
1799 unsigned int trb_status;
1800
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001801 trace_dwc3_complete_trb(dep, trb);
1802
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301803 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1804 /*
1805 * We continue despite the error. There is not much we
1806 * can do. If we don't clean it up we loop forever. If
1807 * we skip the TRB then it gets overwritten after a
1808 * while since we use them in a ring buffer. A BUG()
1809 * would help. Lets hope that if this occurs, someone
1810 * fixes the root cause instead of looking away :)
1811 */
1812 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1813 dep->name, trb);
1814 count = trb->size & DWC3_TRB_SIZE_MASK;
1815
1816 if (dep->direction) {
1817 if (count) {
1818 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1819 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001820 dwc3_trace(trace_dwc3_gadget,
1821 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301822 dep->name);
1823 /*
1824 * If missed isoc occurred and there is
1825 * no request queued then issue END
1826 * TRANSFER, so that core generates
1827 * next xfernotready and we will issue
1828 * a fresh START TRANSFER.
1829 * If there are still queued request
1830 * then wait, do not issue either END
1831 * or UPDATE TRANSFER, just attach next
1832 * request in request_list during
1833 * giveback.If any future queued request
1834 * is successfully transferred then we
1835 * will issue UPDATE TRANSFER for all
1836 * request in the request_list.
1837 */
1838 dep->flags |= DWC3_EP_MISSED_ISOC;
1839 } else {
1840 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1841 dep->name);
1842 status = -ECONNRESET;
1843 }
1844 } else {
1845 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1846 }
1847 } else {
1848 if (count && (event->status & DEPEVT_STATUS_SHORT))
1849 s_pkt = 1;
1850 }
1851
1852 /*
1853 * We assume here we will always receive the entire data block
1854 * which we should receive. Meaning, if we program RX to
1855 * receive 4K but we receive only 2K, we assume that's all we
1856 * should receive and we simply bounce the request back to the
1857 * gadget driver for further processing.
1858 */
1859 req->request.actual += req->request.length - count;
1860 if (s_pkt)
1861 return 1;
1862 if ((event->status & DEPEVT_STATUS_LST) &&
1863 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1864 DWC3_TRB_CTRL_HWO)))
1865 return 1;
1866 if ((event->status & DEPEVT_STATUS_IOC) &&
1867 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1868 return 1;
1869 return 0;
1870}
1871
Felipe Balbi72246da2011-08-19 18:10:58 +03001872static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1873 const struct dwc3_event_depevt *event, int status)
1874{
1875 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001876 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301877 unsigned int slot;
1878 unsigned int i;
1879 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001880
1881 do {
Ville Syrjäläd115d702015-08-31 19:48:28 +03001882 req = next_request(&dep->req_queued);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001883 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001884 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001885
Ville Syrjäläd115d702015-08-31 19:48:28 +03001886 i = 0;
1887 do {
1888 slot = req->start_slot + i;
1889 if ((slot == DWC3_TRB_NUM - 1) &&
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301890 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001891 slot++;
1892 slot %= DWC3_TRB_NUM;
1893 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001894
Ville Syrjäläd115d702015-08-31 19:48:28 +03001895 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1896 event, status);
1897 if (ret)
1898 break;
1899 } while (++i < req->request.num_mapped_sgs);
1900
1901 dwc3_gadget_giveback(dep, req, status);
1902
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301903 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001904 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03001905 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001906
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301907 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1908 list_empty(&dep->req_queued)) {
1909 if (list_empty(&dep->request_list)) {
1910 /*
1911 * If there is no entry in request list then do
1912 * not issue END TRANSFER now. Just set PENDING
1913 * flag, so that END TRANSFER is issued when an
1914 * entry is added into request list.
1915 */
1916 dep->flags = DWC3_EP_PENDING_REQUEST;
1917 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001918 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301919 dep->flags = DWC3_EP_ENABLED;
1920 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301921 return 1;
1922 }
1923
Felipe Balbi72246da2011-08-19 18:10:58 +03001924 return 1;
1925}
1926
1927static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09001928 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001929{
1930 unsigned status = 0;
1931 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05001932 u32 is_xfer_complete;
1933
1934 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001935
1936 if (event->status & DEPEVT_STATUS_BUSERR)
1937 status = -ECONNRESET;
1938
Paul Zimmerman1d046792012-02-15 18:56:56 -08001939 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05001940 if (clean_busy && (is_xfer_complete ||
1941 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001942 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001943
1944 /*
1945 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1946 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1947 */
1948 if (dwc->revision < DWC3_REVISION_183A) {
1949 u32 reg;
1950 int i;
1951
1952 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001953 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001954
1955 if (!(dep->flags & DWC3_EP_ENABLED))
1956 continue;
1957
1958 if (!list_empty(&dep->req_queued))
1959 return;
1960 }
1961
1962 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1963 reg |= dwc->u1u2;
1964 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1965
1966 dwc->u1u2 = 0;
1967 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05001968
Felipe Balbie6e709b2015-09-28 15:16:56 -05001969 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05001970 int ret;
1971
Felipe Balbie6e709b2015-09-28 15:16:56 -05001972 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05001973 if (!ret || ret == -EBUSY)
1974 return;
1975 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001976}
1977
Felipe Balbi72246da2011-08-19 18:10:58 +03001978static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1979 const struct dwc3_event_depevt *event)
1980{
1981 struct dwc3_ep *dep;
1982 u8 epnum = event->endpoint_number;
1983
1984 dep = dwc->eps[epnum];
1985
Felipe Balbi3336abb2012-06-06 09:19:35 +03001986 if (!(dep->flags & DWC3_EP_ENABLED))
1987 return;
1988
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 if (epnum == 0 || epnum == 1) {
1990 dwc3_ep0_interrupt(dwc, event);
1991 return;
1992 }
1993
1994 switch (event->endpoint_event) {
1995 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03001996 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001997
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001998 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001999 dwc3_trace(trace_dwc3_gadget,
2000 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002001 dep->name);
2002 return;
2003 }
2004
Jingoo Han029d97f2014-07-04 15:00:51 +09002005 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002006 break;
2007 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002008 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002009 break;
2010 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002011 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002012 dwc3_gadget_start_isoc(dwc, dep, event);
2013 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002014 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002015 int ret;
2016
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002017 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2018
Felipe Balbi73815282015-01-27 13:48:14 -06002019 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002020 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002021 : "Transfer Not Active");
2022
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002023 ret = __dwc3_gadget_kick_transfer(dep, 0, !active);
Felipe Balbi72246da2011-08-19 18:10:58 +03002024 if (!ret || ret == -EBUSY)
2025 return;
2026
Felipe Balbiec5e7952015-11-16 16:04:13 -06002027 dwc3_trace(trace_dwc3_gadget,
2028 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002029 dep->name);
2030 }
2031
2032 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002033 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002034 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002035 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2036 dep->name);
2037 return;
2038 }
2039
2040 switch (event->status) {
2041 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002042 dwc3_trace(trace_dwc3_gadget,
2043 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002044 event->parameters);
2045
2046 break;
2047 case DEPEVT_STREAMEVT_NOTFOUND:
2048 /* FALLTHROUGH */
2049 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002050 dwc3_trace(trace_dwc3_gadget,
2051 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002052 }
2053 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002054 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002055 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002056 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002057 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002058 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002059 break;
2060 }
2061}
2062
2063static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2064{
2065 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2066 spin_unlock(&dwc->lock);
2067 dwc->gadget_driver->disconnect(&dwc->gadget);
2068 spin_lock(&dwc->lock);
2069 }
2070}
2071
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002072static void dwc3_suspend_gadget(struct dwc3 *dwc)
2073{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002074 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002075 spin_unlock(&dwc->lock);
2076 dwc->gadget_driver->suspend(&dwc->gadget);
2077 spin_lock(&dwc->lock);
2078 }
2079}
2080
2081static void dwc3_resume_gadget(struct dwc3 *dwc)
2082{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002083 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002084 spin_unlock(&dwc->lock);
2085 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002086 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002087 }
2088}
2089
2090static void dwc3_reset_gadget(struct dwc3 *dwc)
2091{
2092 if (!dwc->gadget_driver)
2093 return;
2094
2095 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2096 spin_unlock(&dwc->lock);
2097 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002098 spin_lock(&dwc->lock);
2099 }
2100}
2101
Paul Zimmermanb992e682012-04-27 14:17:35 +03002102static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002103{
2104 struct dwc3_ep *dep;
2105 struct dwc3_gadget_ep_cmd_params params;
2106 u32 cmd;
2107 int ret;
2108
2109 dep = dwc->eps[epnum];
2110
Felipe Balbib4996a82012-06-06 12:04:13 +03002111 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302112 return;
2113
Pratyush Anand57911502012-07-06 15:19:10 +05302114 /*
2115 * NOTICE: We are violating what the Databook says about the
2116 * EndTransfer command. Ideally we would _always_ wait for the
2117 * EndTransfer Command Completion IRQ, but that's causing too
2118 * much trouble synchronizing between us and gadget driver.
2119 *
2120 * We have discussed this with the IP Provider and it was
2121 * suggested to giveback all requests here, but give HW some
2122 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002123 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302124 *
2125 * Note also that a similar handling was tested by Synopsys
2126 * (thanks a lot Paul) and nothing bad has come out of it.
2127 * In short, what we're doing is:
2128 *
2129 * - Issue EndTransfer WITH CMDIOC bit set
2130 * - Wait 100us
2131 */
2132
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302133 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002134 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2135 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002136 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302137 memset(&params, 0, sizeof(params));
2138 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2139 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002140 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002141 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302142 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002143}
2144
2145static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2146{
2147 u32 epnum;
2148
2149 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2150 struct dwc3_ep *dep;
2151
2152 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002153 if (!dep)
2154 continue;
2155
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 if (!(dep->flags & DWC3_EP_ENABLED))
2157 continue;
2158
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002159 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 }
2161}
2162
2163static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2164{
2165 u32 epnum;
2166
2167 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2168 struct dwc3_ep *dep;
2169 struct dwc3_gadget_ep_cmd_params params;
2170 int ret;
2171
2172 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002173 if (!dep)
2174 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002175
2176 if (!(dep->flags & DWC3_EP_STALL))
2177 continue;
2178
2179 dep->flags &= ~DWC3_EP_STALL;
2180
2181 memset(&params, 0, sizeof(params));
2182 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2183 DWC3_DEPCMD_CLEARSTALL, &params);
2184 WARN_ON_ONCE(ret);
2185 }
2186}
2187
2188static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2189{
Felipe Balbic4430a22012-05-24 10:30:01 +03002190 int reg;
2191
Felipe Balbi72246da2011-08-19 18:10:58 +03002192 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2193 reg &= ~DWC3_DCTL_INITU1ENA;
2194 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2195
2196 reg &= ~DWC3_DCTL_INITU2ENA;
2197 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002198
Felipe Balbi72246da2011-08-19 18:10:58 +03002199 dwc3_disconnect_gadget(dwc);
2200
2201 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002202 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002203 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002204}
2205
Felipe Balbi72246da2011-08-19 18:10:58 +03002206static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2207{
2208 u32 reg;
2209
Felipe Balbidf62df52011-10-14 15:11:49 +03002210 /*
2211 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2212 * would cause a missing Disconnect Event if there's a
2213 * pending Setup Packet in the FIFO.
2214 *
2215 * There's no suggested workaround on the official Bug
2216 * report, which states that "unless the driver/application
2217 * is doing any special handling of a disconnect event,
2218 * there is no functional issue".
2219 *
2220 * Unfortunately, it turns out that we _do_ some special
2221 * handling of a disconnect event, namely complete all
2222 * pending transfers, notify gadget driver of the
2223 * disconnection, and so on.
2224 *
2225 * Our suggested workaround is to follow the Disconnect
2226 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002227 * flag. Such flag gets set whenever we have a SETUP_PENDING
2228 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002229 * same endpoint.
2230 *
2231 * Refers to:
2232 *
2233 * STAR#9000466709: RTL: Device : Disconnect event not
2234 * generated if setup packet pending in FIFO
2235 */
2236 if (dwc->revision < DWC3_REVISION_188A) {
2237 if (dwc->setup_packet_pending)
2238 dwc3_gadget_disconnect_interrupt(dwc);
2239 }
2240
Felipe Balbi8e744752014-11-06 14:27:53 +08002241 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002242
2243 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2244 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2245 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002246 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002247
2248 dwc3_stop_active_transfers(dwc);
2249 dwc3_clear_stall_all_ep(dwc);
2250
2251 /* Reset device address to zero */
2252 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2253 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2254 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002255}
2256
2257static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2258{
2259 u32 reg;
2260 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2261
2262 /*
2263 * We change the clock only at SS but I dunno why I would want to do
2264 * this. Maybe it becomes part of the power saving plan.
2265 */
2266
John Younee5cd412016-02-05 17:08:45 -08002267 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2268 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002269 return;
2270
2271 /*
2272 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2273 * each time on Connect Done.
2274 */
2275 if (!usb30_clock)
2276 return;
2277
2278 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2279 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2280 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2281}
2282
Felipe Balbi72246da2011-08-19 18:10:58 +03002283static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2284{
Felipe Balbi72246da2011-08-19 18:10:58 +03002285 struct dwc3_ep *dep;
2286 int ret;
2287 u32 reg;
2288 u8 speed;
2289
Felipe Balbi72246da2011-08-19 18:10:58 +03002290 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2291 speed = reg & DWC3_DSTS_CONNECTSPD;
2292 dwc->speed = speed;
2293
2294 dwc3_update_ram_clk_sel(dwc, speed);
2295
2296 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002297 case DWC3_DCFG_SUPERSPEED_PLUS:
2298 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2299 dwc->gadget.ep0->maxpacket = 512;
2300 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2301 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002302 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002303 /*
2304 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2305 * would cause a missing USB3 Reset event.
2306 *
2307 * In such situations, we should force a USB3 Reset
2308 * event by calling our dwc3_gadget_reset_interrupt()
2309 * routine.
2310 *
2311 * Refers to:
2312 *
2313 * STAR#9000483510: RTL: SS : USB3 reset event may
2314 * not be generated always when the link enters poll
2315 */
2316 if (dwc->revision < DWC3_REVISION_190A)
2317 dwc3_gadget_reset_interrupt(dwc);
2318
Felipe Balbi72246da2011-08-19 18:10:58 +03002319 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2320 dwc->gadget.ep0->maxpacket = 512;
2321 dwc->gadget.speed = USB_SPEED_SUPER;
2322 break;
2323 case DWC3_DCFG_HIGHSPEED:
2324 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2325 dwc->gadget.ep0->maxpacket = 64;
2326 dwc->gadget.speed = USB_SPEED_HIGH;
2327 break;
2328 case DWC3_DCFG_FULLSPEED2:
2329 case DWC3_DCFG_FULLSPEED1:
2330 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2331 dwc->gadget.ep0->maxpacket = 64;
2332 dwc->gadget.speed = USB_SPEED_FULL;
2333 break;
2334 case DWC3_DCFG_LOWSPEED:
2335 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2336 dwc->gadget.ep0->maxpacket = 8;
2337 dwc->gadget.speed = USB_SPEED_LOW;
2338 break;
2339 }
2340
Pratyush Anand2b758352013-01-14 15:59:31 +05302341 /* Enable USB2 LPM Capability */
2342
John Younee5cd412016-02-05 17:08:45 -08002343 if ((dwc->revision > DWC3_REVISION_194A) &&
2344 (speed != DWC3_DCFG_SUPERSPEED) &&
2345 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302346 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2347 reg |= DWC3_DCFG_LPM_CAP;
2348 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2349
2350 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2351 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2352
Huang Rui460d0982014-10-31 11:11:18 +08002353 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302354
Huang Rui80caf7d2014-10-28 19:54:26 +08002355 /*
2356 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2357 * DCFG.LPMCap is set, core responses with an ACK and the
2358 * BESL value in the LPM token is less than or equal to LPM
2359 * NYET threshold.
2360 */
2361 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2362 && dwc->has_lpm_erratum,
2363 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2364
2365 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2366 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2367
Pratyush Anand2b758352013-01-14 15:59:31 +05302368 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002369 } else {
2370 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2371 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2372 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302373 }
2374
Felipe Balbi72246da2011-08-19 18:10:58 +03002375 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002376 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2377 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002378 if (ret) {
2379 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2380 return;
2381 }
2382
2383 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002384 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2385 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002386 if (ret) {
2387 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2388 return;
2389 }
2390
2391 /*
2392 * Configure PHY via GUSB3PIPECTLn if required.
2393 *
2394 * Update GTXFIFOSIZn
2395 *
2396 * In both cases reset values should be sufficient.
2397 */
2398}
2399
2400static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2401{
Felipe Balbi72246da2011-08-19 18:10:58 +03002402 /*
2403 * TODO take core out of low power mode when that's
2404 * implemented.
2405 */
2406
Jiebing Liad14d4e2014-12-11 13:26:29 +08002407 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2408 spin_unlock(&dwc->lock);
2409 dwc->gadget_driver->resume(&dwc->gadget);
2410 spin_lock(&dwc->lock);
2411 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002412}
2413
2414static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2415 unsigned int evtinfo)
2416{
Felipe Balbifae2b902011-10-14 13:00:30 +03002417 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002418 unsigned int pwropt;
2419
2420 /*
2421 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2422 * Hibernation mode enabled which would show up when device detects
2423 * host-initiated U3 exit.
2424 *
2425 * In that case, device will generate a Link State Change Interrupt
2426 * from U3 to RESUME which is only necessary if Hibernation is
2427 * configured in.
2428 *
2429 * There are no functional changes due to such spurious event and we
2430 * just need to ignore it.
2431 *
2432 * Refers to:
2433 *
2434 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2435 * operational mode
2436 */
2437 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2438 if ((dwc->revision < DWC3_REVISION_250A) &&
2439 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2440 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2441 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002442 dwc3_trace(trace_dwc3_gadget,
2443 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002444 return;
2445 }
2446 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002447
2448 /*
2449 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2450 * on the link partner, the USB session might do multiple entry/exit
2451 * of low power states before a transfer takes place.
2452 *
2453 * Due to this problem, we might experience lower throughput. The
2454 * suggested workaround is to disable DCTL[12:9] bits if we're
2455 * transitioning from U1/U2 to U0 and enable those bits again
2456 * after a transfer completes and there are no pending transfers
2457 * on any of the enabled endpoints.
2458 *
2459 * This is the first half of that workaround.
2460 *
2461 * Refers to:
2462 *
2463 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2464 * core send LGO_Ux entering U0
2465 */
2466 if (dwc->revision < DWC3_REVISION_183A) {
2467 if (next == DWC3_LINK_STATE_U0) {
2468 u32 u1u2;
2469 u32 reg;
2470
2471 switch (dwc->link_state) {
2472 case DWC3_LINK_STATE_U1:
2473 case DWC3_LINK_STATE_U2:
2474 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2475 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2476 | DWC3_DCTL_ACCEPTU2ENA
2477 | DWC3_DCTL_INITU1ENA
2478 | DWC3_DCTL_ACCEPTU1ENA);
2479
2480 if (!dwc->u1u2)
2481 dwc->u1u2 = reg & u1u2;
2482
2483 reg &= ~u1u2;
2484
2485 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2486 break;
2487 default:
2488 /* do nothing */
2489 break;
2490 }
2491 }
2492 }
2493
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002494 switch (next) {
2495 case DWC3_LINK_STATE_U1:
2496 if (dwc->speed == USB_SPEED_SUPER)
2497 dwc3_suspend_gadget(dwc);
2498 break;
2499 case DWC3_LINK_STATE_U2:
2500 case DWC3_LINK_STATE_U3:
2501 dwc3_suspend_gadget(dwc);
2502 break;
2503 case DWC3_LINK_STATE_RESUME:
2504 dwc3_resume_gadget(dwc);
2505 break;
2506 default:
2507 /* do nothing */
2508 break;
2509 }
2510
Felipe Balbie57ebc12014-04-22 13:20:12 -05002511 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002512}
2513
Felipe Balbie1dadd32014-02-25 14:47:54 -06002514static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2515 unsigned int evtinfo)
2516{
2517 unsigned int is_ss = evtinfo & BIT(4);
2518
2519 /**
2520 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2521 * have a known issue which can cause USB CV TD.9.23 to fail
2522 * randomly.
2523 *
2524 * Because of this issue, core could generate bogus hibernation
2525 * events which SW needs to ignore.
2526 *
2527 * Refers to:
2528 *
2529 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2530 * Device Fallback from SuperSpeed
2531 */
2532 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2533 return;
2534
2535 /* enter hibernation here */
2536}
2537
Felipe Balbi72246da2011-08-19 18:10:58 +03002538static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2539 const struct dwc3_event_devt *event)
2540{
2541 switch (event->type) {
2542 case DWC3_DEVICE_EVENT_DISCONNECT:
2543 dwc3_gadget_disconnect_interrupt(dwc);
2544 break;
2545 case DWC3_DEVICE_EVENT_RESET:
2546 dwc3_gadget_reset_interrupt(dwc);
2547 break;
2548 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2549 dwc3_gadget_conndone_interrupt(dwc);
2550 break;
2551 case DWC3_DEVICE_EVENT_WAKEUP:
2552 dwc3_gadget_wakeup_interrupt(dwc);
2553 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002554 case DWC3_DEVICE_EVENT_HIBER_REQ:
2555 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2556 "unexpected hibernation event\n"))
2557 break;
2558
2559 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2560 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002561 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2562 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2563 break;
2564 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002565 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002566 break;
2567 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002568 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002569 break;
2570 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002571 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002572 break;
2573 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002574 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002575 break;
2576 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002577 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002578 break;
2579 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06002580 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002581 }
2582}
2583
2584static void dwc3_process_event_entry(struct dwc3 *dwc,
2585 const union dwc3_event *event)
2586{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002587 trace_dwc3_event(event->raw);
2588
Felipe Balbi72246da2011-08-19 18:10:58 +03002589 /* Endpoint IRQ, handle it and return early */
2590 if (event->type.is_devspec == 0) {
2591 /* depevt */
2592 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2593 }
2594
2595 switch (event->type.type) {
2596 case DWC3_EVENT_TYPE_DEV:
2597 dwc3_gadget_interrupt(dwc, &event->devt);
2598 break;
2599 /* REVISIT what to do with Carkit and I2C events ? */
2600 default:
2601 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2602 }
2603}
2604
Felipe Balbif42f2442013-06-12 21:25:08 +03002605static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2606{
2607 struct dwc3_event_buffer *evt;
2608 irqreturn_t ret = IRQ_NONE;
2609 int left;
2610 u32 reg;
2611
2612 evt = dwc->ev_buffs[buf];
2613 left = evt->count;
2614
2615 if (!(evt->flags & DWC3_EVENT_PENDING))
2616 return IRQ_NONE;
2617
2618 while (left > 0) {
2619 union dwc3_event event;
2620
2621 event.raw = *(u32 *) (evt->buf + evt->lpos);
2622
2623 dwc3_process_event_entry(dwc, &event);
2624
2625 /*
2626 * FIXME we wrap around correctly to the next entry as
2627 * almost all entries are 4 bytes in size. There is one
2628 * entry which has 12 bytes which is a regular entry
2629 * followed by 8 bytes data. ATM I don't know how
2630 * things are organized if we get next to the a
2631 * boundary so I worry about that once we try to handle
2632 * that.
2633 */
2634 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2635 left -= 4;
2636
2637 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2638 }
2639
2640 evt->count = 0;
2641 evt->flags &= ~DWC3_EVENT_PENDING;
2642 ret = IRQ_HANDLED;
2643
2644 /* Unmask interrupt */
2645 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2646 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2647 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2648
2649 return ret;
2650}
2651
Felipe Balbib15a7622011-06-30 16:57:15 +03002652static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2653{
2654 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002655 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002656 irqreturn_t ret = IRQ_NONE;
2657 int i;
2658
Felipe Balbie5f68b42015-10-12 13:25:44 -05002659 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002660
Felipe Balbif42f2442013-06-12 21:25:08 +03002661 for (i = 0; i < dwc->num_event_buffers; i++)
2662 ret |= dwc3_process_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002663
Felipe Balbie5f68b42015-10-12 13:25:44 -05002664 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002665
2666 return ret;
2667}
2668
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002669static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
Felipe Balbi72246da2011-08-19 18:10:58 +03002670{
2671 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002672 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002673 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002674
Felipe Balbib15a7622011-06-30 16:57:15 +03002675 evt = dwc->ev_buffs[buf];
2676
Felipe Balbi72246da2011-08-19 18:10:58 +03002677 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2678 count &= DWC3_GEVNTCOUNT_MASK;
2679 if (!count)
2680 return IRQ_NONE;
2681
Felipe Balbib15a7622011-06-30 16:57:15 +03002682 evt->count = count;
2683 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002684
Felipe Balbie8adfc32013-06-12 21:11:14 +03002685 /* Mask interrupt */
2686 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2687 reg |= DWC3_GEVNTSIZ_INTMASK;
2688 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2689
Felipe Balbib15a7622011-06-30 16:57:15 +03002690 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002691}
2692
2693static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2694{
2695 struct dwc3 *dwc = _dwc;
2696 int i;
2697 irqreturn_t ret = IRQ_NONE;
2698
Felipe Balbi9f622b22011-10-12 10:31:04 +03002699 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002700 irqreturn_t status;
2701
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002702 status = dwc3_check_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002703 if (status == IRQ_WAKE_THREAD)
Felipe Balbi72246da2011-08-19 18:10:58 +03002704 ret = status;
2705 }
2706
Felipe Balbi72246da2011-08-19 18:10:58 +03002707 return ret;
2708}
2709
2710/**
2711 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002712 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002713 *
2714 * Returns 0 on success otherwise negative errno.
2715 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002716int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002717{
Felipe Balbi72246da2011-08-19 18:10:58 +03002718 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002719
2720 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2721 &dwc->ctrl_req_addr, GFP_KERNEL);
2722 if (!dwc->ctrl_req) {
2723 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2724 ret = -ENOMEM;
2725 goto err0;
2726 }
2727
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302728 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002729 &dwc->ep0_trb_addr, GFP_KERNEL);
2730 if (!dwc->ep0_trb) {
2731 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2732 ret = -ENOMEM;
2733 goto err1;
2734 }
2735
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002736 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002737 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002738 ret = -ENOMEM;
2739 goto err2;
2740 }
2741
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002742 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002743 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2744 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002745 if (!dwc->ep0_bounce) {
2746 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2747 ret = -ENOMEM;
2748 goto err3;
2749 }
2750
Felipe Balbi04c03d12015-12-02 10:06:45 -06002751 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2752 if (!dwc->zlp_buf) {
2753 ret = -ENOMEM;
2754 goto err4;
2755 }
2756
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002758 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002759 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002760 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002761 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002762
2763 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002764 * FIXME We might be setting max_speed to <SUPER, however versions
2765 * <2.20a of dwc3 have an issue with metastability (documented
2766 * elsewhere in this driver) which tells us we can't set max speed to
2767 * anything lower than SUPER.
2768 *
2769 * Because gadget.max_speed is only used by composite.c and function
2770 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2771 * to happen so we avoid sending SuperSpeed Capability descriptor
2772 * together with our BOS descriptor as that could confuse host into
2773 * thinking we can handle super speed.
2774 *
2775 * Note that, in fact, we won't even support GetBOS requests when speed
2776 * is less than super speed because we don't have means, yet, to tell
2777 * composite.c that we are USB 2.0 + LPM ECN.
2778 */
2779 if (dwc->revision < DWC3_REVISION_220A)
2780 dwc3_trace(trace_dwc3_gadget,
2781 "Changing max_speed on rev %08x\n",
2782 dwc->revision);
2783
2784 dwc->gadget.max_speed = dwc->maximum_speed;
2785
2786 /*
David Cohena4b9d942013-12-09 15:55:38 -08002787 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2788 * on ep out.
2789 */
2790 dwc->gadget.quirk_ep_out_aligned_size = true;
2791
2792 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002793 * REVISIT: Here we should clear all pending IRQs to be
2794 * sure we're starting from a well known location.
2795 */
2796
2797 ret = dwc3_gadget_init_endpoints(dwc);
2798 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002799 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002800
Felipe Balbi72246da2011-08-19 18:10:58 +03002801 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2802 if (ret) {
2803 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002804 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002805 }
2806
2807 return 0;
2808
Felipe Balbi04c03d12015-12-02 10:06:45 -06002809err5:
2810 kfree(dwc->zlp_buf);
2811
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002812err4:
David Cohene1f80462013-09-11 17:42:47 -07002813 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002814 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2815 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002816
Felipe Balbi72246da2011-08-19 18:10:58 +03002817err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002818 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002819
2820err2:
2821 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2822 dwc->ep0_trb, dwc->ep0_trb_addr);
2823
2824err1:
2825 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2826 dwc->ctrl_req, dwc->ctrl_req_addr);
2827
2828err0:
2829 return ret;
2830}
2831
Felipe Balbi7415f172012-04-30 14:56:33 +03002832/* -------------------------------------------------------------------------- */
2833
Felipe Balbi72246da2011-08-19 18:10:58 +03002834void dwc3_gadget_exit(struct dwc3 *dwc)
2835{
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002837
Felipe Balbi72246da2011-08-19 18:10:58 +03002838 dwc3_gadget_free_endpoints(dwc);
2839
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002840 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2841 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002842
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002843 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002844 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002845
2846 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2847 dwc->ep0_trb, dwc->ep0_trb_addr);
2848
2849 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2850 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002851}
Felipe Balbi7415f172012-04-30 14:56:33 +03002852
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002853int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002854{
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002855 if (dwc->pullups_connected) {
Felipe Balbi7415f172012-04-30 14:56:33 +03002856 dwc3_gadget_disable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002857 dwc3_gadget_run_stop(dwc, true, true);
2858 }
Felipe Balbi7415f172012-04-30 14:56:33 +03002859
Felipe Balbi7415f172012-04-30 14:56:33 +03002860 __dwc3_gadget_ep_disable(dwc->eps[0]);
2861 __dwc3_gadget_ep_disable(dwc->eps[1]);
2862
2863 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2864
2865 return 0;
2866}
2867
2868int dwc3_gadget_resume(struct dwc3 *dwc)
2869{
2870 struct dwc3_ep *dep;
2871 int ret;
2872
2873 /* Start with SuperSpeed Default */
2874 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2875
2876 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002877 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2878 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002879 if (ret)
2880 goto err0;
2881
2882 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002883 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2884 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002885 if (ret)
2886 goto err1;
2887
2888 /* begin to receive SETUP packets */
2889 dwc->ep0state = EP0_SETUP_PHASE;
2890 dwc3_ep0_out_start(dwc);
2891
2892 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2893
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002894 if (dwc->pullups_connected) {
2895 dwc3_gadget_enable_irq(dwc);
2896 dwc3_gadget_run_stop(dwc, true, false);
2897 }
2898
Felipe Balbi7415f172012-04-30 14:56:33 +03002899 return 0;
2900
2901err1:
2902 __dwc3_gadget_ep_disable(dwc->eps[0]);
2903
2904err0:
2905 return ret;
2906}