Commit | Line | Data |
---|---|---|
72246da4 FB |
1 | /** |
2 | * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling | |
3 | * | |
4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com | |
72246da4 FB |
5 | * |
6 | * Authors: Felipe Balbi <balbi@ti.com>, | |
7 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> | |
8 | * | |
5945f789 FB |
9 | * 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. | |
72246da4 | 12 | * |
5945f789 FB |
13 | * 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. | |
72246da4 FB |
17 | */ |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/spinlock.h> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/pm_runtime.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/io.h> | |
26 | #include <linux/list.h> | |
27 | #include <linux/dma-mapping.h> | |
28 | ||
29 | #include <linux/usb/ch9.h> | |
30 | #include <linux/usb/gadget.h> | |
5bdb1dcc | 31 | #include <linux/usb/composite.h> |
72246da4 FB |
32 | |
33 | #include "core.h" | |
80977dc9 | 34 | #include "debug.h" |
72246da4 FB |
35 | #include "gadget.h" |
36 | #include "io.h" | |
37 | ||
788a23f4 | 38 | static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep); |
a0807881 FB |
39 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
40 | struct dwc3_ep *dep, struct dwc3_request *req); | |
5bdb1dcc | 41 | |
72246da4 FB |
42 | static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state) |
43 | { | |
44 | switch (state) { | |
45 | case EP0_UNCONNECTED: | |
46 | return "Unconnected"; | |
c7fcdeb2 FB |
47 | case EP0_SETUP_PHASE: |
48 | return "Setup Phase"; | |
49 | case EP0_DATA_PHASE: | |
50 | return "Data Phase"; | |
51 | case EP0_STATUS_PHASE: | |
52 | return "Status Phase"; | |
72246da4 FB |
53 | default: |
54 | return "UNKNOWN"; | |
55 | } | |
56 | } | |
57 | ||
58 | static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma, | |
368ca113 | 59 | u32 len, u32 type, bool chain) |
72246da4 FB |
60 | { |
61 | struct dwc3_gadget_ep_cmd_params params; | |
f6bafc6a | 62 | struct dwc3_trb *trb; |
72246da4 FB |
63 | struct dwc3_ep *dep; |
64 | ||
65 | int ret; | |
66 | ||
67 | dep = dwc->eps[epnum]; | |
c7fcdeb2 | 68 | if (dep->flags & DWC3_EP_BUSY) { |
2c4cbe6e | 69 | dwc3_trace(trace_dwc3_ep0, "%s still busy", dep->name); |
c7fcdeb2 FB |
70 | return 0; |
71 | } | |
72246da4 | 72 | |
f6bafc6a | 73 | trb = dwc->ep0_trb; |
72246da4 | 74 | |
f6bafc6a FB |
75 | trb->bpl = lower_32_bits(buf_dma); |
76 | trb->bph = upper_32_bits(buf_dma); | |
77 | trb->size = len; | |
78 | trb->ctrl = type; | |
72246da4 | 79 | |
f6bafc6a FB |
80 | trb->ctrl |= (DWC3_TRB_CTRL_HWO |
81 | | DWC3_TRB_CTRL_LST | |
82 | | DWC3_TRB_CTRL_IOC | |
83 | | DWC3_TRB_CTRL_ISP_IMI); | |
72246da4 FB |
84 | |
85 | memset(¶ms, 0, sizeof(params)); | |
dc1c70a7 FB |
86 | params.param0 = upper_32_bits(dwc->ep0_trb_addr); |
87 | params.param1 = lower_32_bits(dwc->ep0_trb_addr); | |
72246da4 | 88 | |
ccb072de FB |
89 | trace_dwc3_prepare_trb(dep, trb); |
90 | ||
72246da4 FB |
91 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
92 | DWC3_DEPCMD_STARTTRANSFER, ¶ms); | |
93 | if (ret < 0) { | |
2c4cbe6e FB |
94 | dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed", |
95 | dep->name); | |
72246da4 FB |
96 | return ret; |
97 | } | |
98 | ||
c7fcdeb2 | 99 | dep->flags |= DWC3_EP_BUSY; |
b4996a86 | 100 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, |
72246da4 FB |
101 | dep->number); |
102 | ||
1ddcb218 FB |
103 | dwc->ep0_next_event = DWC3_EP0_COMPLETE; |
104 | ||
72246da4 FB |
105 | return 0; |
106 | } | |
107 | ||
108 | static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, | |
109 | struct dwc3_request *req) | |
110 | { | |
5bdb1dcc | 111 | struct dwc3 *dwc = dep->dwc; |
72246da4 FB |
112 | |
113 | req->request.actual = 0; | |
114 | req->request.status = -EINPROGRESS; | |
72246da4 FB |
115 | req->epnum = dep->number; |
116 | ||
117 | list_add_tail(&req->list, &dep->request_list); | |
a6829706 | 118 | |
c7fcdeb2 FB |
119 | /* |
120 | * Gadget driver might not be quick enough to queue a request | |
121 | * before we get a Transfer Not Ready event on this endpoint. | |
122 | * | |
123 | * In that case, we will set DWC3_EP_PENDING_REQUEST. When that | |
124 | * flag is set, it's telling us that as soon as Gadget queues the | |
125 | * required request, we should kick the transfer here because the | |
126 | * IRQ we were waiting for is long gone. | |
127 | */ | |
128 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { | |
c7fcdeb2 | 129 | unsigned direction; |
c7fcdeb2 FB |
130 | |
131 | direction = !!(dep->flags & DWC3_EP0_DIR_IN); | |
132 | ||
68d8a781 FB |
133 | if (dwc->ep0state != EP0_DATA_PHASE) { |
134 | dev_WARN(dwc->dev, "Unexpected pending request\n"); | |
c7fcdeb2 FB |
135 | return 0; |
136 | } | |
72246da4 | 137 | |
a0807881 FB |
138 | __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req); |
139 | ||
c7fcdeb2 FB |
140 | dep->flags &= ~(DWC3_EP_PENDING_REQUEST | |
141 | DWC3_EP0_DIR_IN); | |
d9b33c60 FB |
142 | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /* | |
147 | * In case gadget driver asked us to delay the STATUS phase, | |
148 | * handle it here. | |
149 | */ | |
150 | if (dwc->delayed_status) { | |
7125d584 FB |
151 | unsigned direction; |
152 | ||
153 | direction = !dwc->ep0_expect_in; | |
5bdb1dcc | 154 | dwc->delayed_status = false; |
7c81290e | 155 | usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED); |
68d3e668 FB |
156 | |
157 | if (dwc->ep0state == EP0_STATUS_PHASE) | |
7125d584 | 158 | __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]); |
68d3e668 | 159 | else |
2c4cbe6e FB |
160 | dwc3_trace(trace_dwc3_ep0, |
161 | "too early for delayed status"); | |
d9b33c60 FB |
162 | |
163 | return 0; | |
72246da4 FB |
164 | } |
165 | ||
fca8892a FB |
166 | /* |
167 | * Unfortunately we have uncovered a limitation wrt the Data Phase. | |
168 | * | |
169 | * Section 9.4 says we can wait for the XferNotReady(DATA) event to | |
170 | * come before issueing Start Transfer command, but if we do, we will | |
171 | * miss situations where the host starts another SETUP phase instead of | |
172 | * the DATA phase. Such cases happen at least on TD.7.6 of the Link | |
173 | * Layer Compliance Suite. | |
174 | * | |
175 | * The problem surfaces due to the fact that in case of back-to-back | |
176 | * SETUP packets there will be no XferNotReady(DATA) generated and we | |
177 | * will be stuck waiting for XferNotReady(DATA) forever. | |
178 | * | |
179 | * By looking at tables 9-13 and 9-14 of the Databook, we can see that | |
180 | * it tells us to start Data Phase right away. It also mentions that if | |
181 | * we receive a SETUP phase instead of the DATA phase, core will issue | |
182 | * XferComplete for the DATA phase, before actually initiating it in | |
183 | * the wire, with the TRB's status set to "SETUP_PENDING". Such status | |
184 | * can only be used to print some debugging logs, as the core expects | |
185 | * us to go through to the STATUS phase and start a CONTROL_STATUS TRB, | |
186 | * just so it completes right away, without transferring anything and, | |
187 | * only then, we can go back to the SETUP phase. | |
188 | * | |
189 | * Because of this scenario, SNPS decided to change the programming | |
190 | * model of control transfers and support on-demand transfers only for | |
191 | * the STATUS phase. To fix the issue we have now, we will always wait | |
192 | * for gadget driver to queue the DATA phase's struct usb_request, then | |
193 | * start it right away. | |
194 | * | |
195 | * If we're actually in a 2-stage transfer, we will wait for | |
196 | * XferNotReady(STATUS). | |
197 | */ | |
198 | if (dwc->three_stage_setup) { | |
199 | unsigned direction; | |
200 | ||
201 | direction = dwc->ep0_expect_in; | |
202 | dwc->ep0state = EP0_DATA_PHASE; | |
203 | ||
204 | __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req); | |
205 | ||
206 | dep->flags &= ~DWC3_EP0_DIR_IN; | |
207 | } | |
208 | ||
35f75696 | 209 | return 0; |
72246da4 FB |
210 | } |
211 | ||
212 | int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, | |
213 | gfp_t gfp_flags) | |
214 | { | |
215 | struct dwc3_request *req = to_dwc3_request(request); | |
216 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
217 | struct dwc3 *dwc = dep->dwc; | |
218 | ||
219 | unsigned long flags; | |
220 | ||
221 | int ret; | |
222 | ||
72246da4 | 223 | spin_lock_irqsave(&dwc->lock, flags); |
16e78db7 | 224 | if (!dep->endpoint.desc) { |
2c4cbe6e FB |
225 | dwc3_trace(trace_dwc3_ep0, |
226 | "trying to queue request %p to disabled %s", | |
72246da4 FB |
227 | request, dep->name); |
228 | ret = -ESHUTDOWN; | |
229 | goto out; | |
230 | } | |
231 | ||
232 | /* we share one TRB for ep0/1 */ | |
c2da2ff0 | 233 | if (!list_empty(&dep->request_list)) { |
72246da4 FB |
234 | ret = -EBUSY; |
235 | goto out; | |
236 | } | |
237 | ||
2c4cbe6e FB |
238 | dwc3_trace(trace_dwc3_ep0, |
239 | "queueing request %p to %s length %d state '%s'", | |
72246da4 FB |
240 | request, dep->name, request->length, |
241 | dwc3_ep0_state_string(dwc->ep0state)); | |
242 | ||
243 | ret = __dwc3_gadget_ep0_queue(dep, req); | |
244 | ||
245 | out: | |
246 | spin_unlock_irqrestore(&dwc->lock, flags); | |
247 | ||
248 | return ret; | |
249 | } | |
250 | ||
251 | static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc) | |
252 | { | |
2dfe37d4 FB |
253 | struct dwc3_ep *dep; |
254 | ||
255 | /* reinitialize physical ep1 */ | |
256 | dep = dwc->eps[1]; | |
257 | dep->flags = DWC3_EP_ENABLED; | |
d742220b | 258 | |
72246da4 | 259 | /* stall is always issued on EP0 */ |
2dfe37d4 | 260 | dep = dwc->eps[0]; |
7a608559 | 261 | __dwc3_gadget_ep_set_halt(dep, 1, false); |
c2da2ff0 | 262 | dep->flags = DWC3_EP_ENABLED; |
5bdb1dcc | 263 | dwc->delayed_status = false; |
d742220b FB |
264 | |
265 | if (!list_empty(&dep->request_list)) { | |
266 | struct dwc3_request *req; | |
267 | ||
268 | req = next_request(&dep->request_list); | |
269 | dwc3_gadget_giveback(dep, req, -ECONNRESET); | |
270 | } | |
271 | ||
c7fcdeb2 | 272 | dwc->ep0state = EP0_SETUP_PHASE; |
72246da4 FB |
273 | dwc3_ep0_out_start(dwc); |
274 | } | |
275 | ||
33fb691b | 276 | int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
08f0d966 PA |
277 | { |
278 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
279 | struct dwc3 *dwc = dep->dwc; | |
280 | ||
281 | dwc3_ep0_stall_and_restart(dwc); | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
33fb691b FB |
286 | int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value) |
287 | { | |
288 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
289 | struct dwc3 *dwc = dep->dwc; | |
290 | unsigned long flags; | |
291 | int ret; | |
292 | ||
293 | spin_lock_irqsave(&dwc->lock, flags); | |
294 | ret = __dwc3_gadget_ep0_set_halt(ep, value); | |
295 | spin_unlock_irqrestore(&dwc->lock, flags); | |
296 | ||
297 | return ret; | |
298 | } | |
299 | ||
72246da4 FB |
300 | void dwc3_ep0_out_start(struct dwc3 *dwc) |
301 | { | |
72246da4 FB |
302 | int ret; |
303 | ||
c7fcdeb2 | 304 | ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8, |
368ca113 | 305 | DWC3_TRBCTL_CONTROL_SETUP, false); |
72246da4 FB |
306 | WARN_ON(ret < 0); |
307 | } | |
308 | ||
72246da4 FB |
309 | static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le) |
310 | { | |
311 | struct dwc3_ep *dep; | |
312 | u32 windex = le16_to_cpu(wIndex_le); | |
313 | u32 epnum; | |
314 | ||
315 | epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1; | |
316 | if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) | |
317 | epnum |= 1; | |
318 | ||
319 | dep = dwc->eps[epnum]; | |
320 | if (dep->flags & DWC3_EP_ENABLED) | |
321 | return dep; | |
322 | ||
323 | return NULL; | |
324 | } | |
325 | ||
8ee6270c | 326 | static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req) |
72246da4 | 327 | { |
72246da4 | 328 | } |
72246da4 FB |
329 | /* |
330 | * ch 9.4.5 | |
331 | */ | |
25b8ff68 FB |
332 | static int dwc3_ep0_handle_status(struct dwc3 *dwc, |
333 | struct usb_ctrlrequest *ctrl) | |
72246da4 FB |
334 | { |
335 | struct dwc3_ep *dep; | |
336 | u32 recip; | |
e6a3b5e2 | 337 | u32 reg; |
72246da4 FB |
338 | u16 usb_status = 0; |
339 | __le16 *response_pkt; | |
340 | ||
341 | recip = ctrl->bRequestType & USB_RECIP_MASK; | |
342 | switch (recip) { | |
343 | case USB_RECIP_DEVICE: | |
344 | /* | |
e6a3b5e2 | 345 | * LTM will be set once we know how to set this in HW. |
72246da4 | 346 | */ |
bcdea503 | 347 | usb_status |= dwc->gadget.is_selfpowered; |
e6a3b5e2 SAS |
348 | |
349 | if (dwc->speed == DWC3_DSTS_SUPERSPEED) { | |
350 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
351 | if (reg & DWC3_DCTL_INITU1ENA) | |
352 | usb_status |= 1 << USB_DEV_STAT_U1_ENABLED; | |
353 | if (reg & DWC3_DCTL_INITU2ENA) | |
354 | usb_status |= 1 << USB_DEV_STAT_U2_ENABLED; | |
355 | } | |
356 | ||
72246da4 FB |
357 | break; |
358 | ||
359 | case USB_RECIP_INTERFACE: | |
360 | /* | |
361 | * Function Remote Wake Capable D0 | |
362 | * Function Remote Wakeup D1 | |
363 | */ | |
364 | break; | |
365 | ||
366 | case USB_RECIP_ENDPOINT: | |
367 | dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex); | |
368 | if (!dep) | |
25b8ff68 | 369 | return -EINVAL; |
72246da4 FB |
370 | |
371 | if (dep->flags & DWC3_EP_STALL) | |
372 | usb_status = 1 << USB_ENDPOINT_HALT; | |
373 | break; | |
374 | default: | |
375 | return -EINVAL; | |
2b84f92b | 376 | } |
72246da4 FB |
377 | |
378 | response_pkt = (__le16 *) dwc->setup_buf; | |
379 | *response_pkt = cpu_to_le16(usb_status); | |
e2617796 FB |
380 | |
381 | dep = dwc->eps[0]; | |
382 | dwc->ep0_usb_req.dep = dep; | |
e0ce0b0a | 383 | dwc->ep0_usb_req.request.length = sizeof(*response_pkt); |
0fc9a1be | 384 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; |
e0ce0b0a | 385 | dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl; |
e2617796 FB |
386 | |
387 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); | |
72246da4 FB |
388 | } |
389 | ||
390 | static int dwc3_ep0_handle_feature(struct dwc3 *dwc, | |
391 | struct usb_ctrlrequest *ctrl, int set) | |
392 | { | |
393 | struct dwc3_ep *dep; | |
394 | u32 recip; | |
395 | u32 wValue; | |
396 | u32 wIndex; | |
e6a3b5e2 | 397 | u32 reg; |
72246da4 | 398 | int ret; |
fdba5aa5 | 399 | enum usb_device_state state; |
72246da4 FB |
400 | |
401 | wValue = le16_to_cpu(ctrl->wValue); | |
402 | wIndex = le16_to_cpu(ctrl->wIndex); | |
403 | recip = ctrl->bRequestType & USB_RECIP_MASK; | |
fdba5aa5 FB |
404 | state = dwc->gadget.state; |
405 | ||
72246da4 FB |
406 | switch (recip) { |
407 | case USB_RECIP_DEVICE: | |
408 | ||
e6a3b5e2 SAS |
409 | switch (wValue) { |
410 | case USB_DEVICE_REMOTE_WAKEUP: | |
411 | break; | |
72246da4 FB |
412 | /* |
413 | * 9.4.1 says only only for SS, in AddressState only for | |
414 | * default control pipe | |
415 | */ | |
72246da4 | 416 | case USB_DEVICE_U1_ENABLE: |
fdba5aa5 | 417 | if (state != USB_STATE_CONFIGURED) |
72246da4 FB |
418 | return -EINVAL; |
419 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) | |
420 | return -EINVAL; | |
72246da4 | 421 | |
e6a3b5e2 SAS |
422 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
423 | if (set) | |
424 | reg |= DWC3_DCTL_INITU1ENA; | |
425 | else | |
426 | reg &= ~DWC3_DCTL_INITU1ENA; | |
427 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
72246da4 | 428 | break; |
e6a3b5e2 | 429 | |
72246da4 | 430 | case USB_DEVICE_U2_ENABLE: |
fdba5aa5 | 431 | if (state != USB_STATE_CONFIGURED) |
e6a3b5e2 SAS |
432 | return -EINVAL; |
433 | if (dwc->speed != DWC3_DSTS_SUPERSPEED) | |
434 | return -EINVAL; | |
435 | ||
436 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
437 | if (set) | |
438 | reg |= DWC3_DCTL_INITU2ENA; | |
439 | else | |
440 | reg &= ~DWC3_DCTL_INITU2ENA; | |
441 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
72246da4 | 442 | break; |
e6a3b5e2 | 443 | |
72246da4 | 444 | case USB_DEVICE_LTM_ENABLE: |
e6a3b5e2 | 445 | return -EINVAL; |
72246da4 FB |
446 | |
447 | case USB_DEVICE_TEST_MODE: | |
448 | if ((wIndex & 0xff) != 0) | |
449 | return -EINVAL; | |
450 | if (!set) | |
451 | return -EINVAL; | |
452 | ||
3b637367 GC |
453 | dwc->test_mode_nr = wIndex >> 8; |
454 | dwc->test_mode = true; | |
ecb07797 GC |
455 | break; |
456 | default: | |
457 | return -EINVAL; | |
72246da4 FB |
458 | } |
459 | break; | |
460 | ||
461 | case USB_RECIP_INTERFACE: | |
462 | switch (wValue) { | |
463 | case USB_INTRF_FUNC_SUSPEND: | |
464 | if (wIndex & USB_INTRF_FUNC_SUSPEND_LP) | |
465 | /* XXX enable Low power suspend */ | |
466 | ; | |
467 | if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) | |
468 | /* XXX enable remote wakeup */ | |
469 | ; | |
470 | break; | |
471 | default: | |
472 | return -EINVAL; | |
473 | } | |
474 | break; | |
475 | ||
476 | case USB_RECIP_ENDPOINT: | |
477 | switch (wValue) { | |
478 | case USB_ENDPOINT_HALT: | |
1d046793 | 479 | dep = dwc3_wIndex_to_dep(dwc, wIndex); |
72246da4 FB |
480 | if (!dep) |
481 | return -EINVAL; | |
a535d81c AS |
482 | if (set == 0 && (dep->flags & DWC3_EP_WEDGE)) |
483 | break; | |
7a608559 | 484 | ret = __dwc3_gadget_ep_set_halt(dep, set, true); |
72246da4 FB |
485 | if (ret) |
486 | return -EINVAL; | |
487 | break; | |
488 | default: | |
489 | return -EINVAL; | |
490 | } | |
491 | break; | |
492 | ||
493 | default: | |
494 | return -EINVAL; | |
2b84f92b | 495 | } |
72246da4 | 496 | |
72246da4 FB |
497 | return 0; |
498 | } | |
499 | ||
500 | static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) | |
501 | { | |
fdba5aa5 | 502 | enum usb_device_state state = dwc->gadget.state; |
72246da4 FB |
503 | u32 addr; |
504 | u32 reg; | |
505 | ||
506 | addr = le16_to_cpu(ctrl->wValue); | |
f96a6ec1 | 507 | if (addr > 127) { |
2c4cbe6e | 508 | dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr); |
72246da4 | 509 | return -EINVAL; |
f96a6ec1 FB |
510 | } |
511 | ||
fdba5aa5 | 512 | if (state == USB_STATE_CONFIGURED) { |
2c4cbe6e FB |
513 | dwc3_trace(trace_dwc3_ep0, |
514 | "trying to set address when configured"); | |
f96a6ec1 FB |
515 | return -EINVAL; |
516 | } | |
72246da4 | 517 | |
2646021e FB |
518 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
519 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); | |
520 | reg |= DWC3_DCFG_DEVADDR(addr); | |
521 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
72246da4 | 522 | |
fdba5aa5 | 523 | if (addr) |
14cd592f | 524 | usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS); |
fdba5aa5 | 525 | else |
14cd592f | 526 | usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT); |
c7fcdeb2 | 527 | |
2646021e | 528 | return 0; |
72246da4 FB |
529 | } |
530 | ||
531 | static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) | |
532 | { | |
533 | int ret; | |
534 | ||
535 | spin_unlock(&dwc->lock); | |
536 | ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); | |
537 | spin_lock(&dwc->lock); | |
538 | return ret; | |
539 | } | |
540 | ||
541 | static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) | |
542 | { | |
fdba5aa5 | 543 | enum usb_device_state state = dwc->gadget.state; |
72246da4 FB |
544 | u32 cfg; |
545 | int ret; | |
e274a31e | 546 | u32 reg; |
72246da4 | 547 | |
b23c8439 | 548 | dwc->start_config_issued = false; |
72246da4 FB |
549 | cfg = le16_to_cpu(ctrl->wValue); |
550 | ||
fdba5aa5 FB |
551 | switch (state) { |
552 | case USB_STATE_DEFAULT: | |
72246da4 | 553 | return -EINVAL; |
72246da4 | 554 | |
fdba5aa5 | 555 | case USB_STATE_ADDRESS: |
72246da4 FB |
556 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
557 | /* if the cfg matches and the cfg is non zero */ | |
457e84b6 | 558 | if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { |
7c81290e FB |
559 | |
560 | /* | |
561 | * only change state if set_config has already | |
562 | * been processed. If gadget driver returns | |
563 | * USB_GADGET_DELAYED_STATUS, we will wait | |
564 | * to change the state on the next usb_ep_queue() | |
565 | */ | |
566 | if (ret == 0) | |
567 | usb_gadget_set_state(&dwc->gadget, | |
568 | USB_STATE_CONFIGURED); | |
14cd592f | 569 | |
e274a31e PA |
570 | /* |
571 | * Enable transition to U1/U2 state when | |
572 | * nothing is pending from application. | |
573 | */ | |
574 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
575 | reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA); | |
576 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
577 | ||
457e84b6 | 578 | dwc->resize_fifos = true; |
2c4cbe6e | 579 | dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET"); |
457e84b6 | 580 | } |
72246da4 FB |
581 | break; |
582 | ||
fdba5aa5 | 583 | case USB_STATE_CONFIGURED: |
72246da4 | 584 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
7a42d835 | 585 | if (!cfg && !ret) |
14cd592f FB |
586 | usb_gadget_set_state(&dwc->gadget, |
587 | USB_STATE_ADDRESS); | |
72246da4 | 588 | break; |
5bdb1dcc SAS |
589 | default: |
590 | ret = -EINVAL; | |
72246da4 | 591 | } |
5bdb1dcc | 592 | return ret; |
72246da4 FB |
593 | } |
594 | ||
865e09e7 FB |
595 | static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req) |
596 | { | |
597 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
598 | struct dwc3 *dwc = dep->dwc; | |
599 | ||
600 | u32 param = 0; | |
601 | u32 reg; | |
602 | ||
603 | struct timing { | |
604 | u8 u1sel; | |
605 | u8 u1pel; | |
606 | u16 u2sel; | |
607 | u16 u2pel; | |
608 | } __packed timing; | |
609 | ||
610 | int ret; | |
611 | ||
612 | memcpy(&timing, req->buf, sizeof(timing)); | |
613 | ||
614 | dwc->u1sel = timing.u1sel; | |
615 | dwc->u1pel = timing.u1pel; | |
c8cf7af4 FB |
616 | dwc->u2sel = le16_to_cpu(timing.u2sel); |
617 | dwc->u2pel = le16_to_cpu(timing.u2pel); | |
865e09e7 FB |
618 | |
619 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
620 | if (reg & DWC3_DCTL_INITU2ENA) | |
621 | param = dwc->u2pel; | |
622 | if (reg & DWC3_DCTL_INITU1ENA) | |
623 | param = dwc->u1pel; | |
624 | ||
625 | /* | |
626 | * According to Synopsys Databook, if parameter is | |
627 | * greater than 125, a value of zero should be | |
628 | * programmed in the register. | |
629 | */ | |
630 | if (param > 125) | |
631 | param = 0; | |
632 | ||
633 | /* now that we have the time, issue DGCMD Set Sel */ | |
634 | ret = dwc3_send_gadget_generic_command(dwc, | |
635 | DWC3_DGCMD_SET_PERIODIC_PAR, param); | |
636 | WARN_ON(ret < 0); | |
637 | } | |
638 | ||
639 | static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) | |
640 | { | |
641 | struct dwc3_ep *dep; | |
fdba5aa5 | 642 | enum usb_device_state state = dwc->gadget.state; |
865e09e7 FB |
643 | u16 wLength; |
644 | u16 wValue; | |
645 | ||
fdba5aa5 | 646 | if (state == USB_STATE_DEFAULT) |
865e09e7 FB |
647 | return -EINVAL; |
648 | ||
649 | wValue = le16_to_cpu(ctrl->wValue); | |
650 | wLength = le16_to_cpu(ctrl->wLength); | |
651 | ||
652 | if (wLength != 6) { | |
653 | dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n", | |
654 | wLength); | |
655 | return -EINVAL; | |
656 | } | |
657 | ||
658 | /* | |
659 | * To handle Set SEL we need to receive 6 bytes from Host. So let's | |
660 | * queue a usb_request for 6 bytes. | |
661 | * | |
662 | * Remember, though, this controller can't handle non-wMaxPacketSize | |
663 | * aligned transfers on the OUT direction, so we queue a request for | |
664 | * wMaxPacketSize instead. | |
665 | */ | |
666 | dep = dwc->eps[0]; | |
667 | dwc->ep0_usb_req.dep = dep; | |
668 | dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket; | |
669 | dwc->ep0_usb_req.request.buf = dwc->setup_buf; | |
670 | dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl; | |
671 | ||
672 | return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req); | |
673 | } | |
674 | ||
c12a0d86 FB |
675 | static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
676 | { | |
677 | u16 wLength; | |
678 | u16 wValue; | |
679 | u16 wIndex; | |
680 | ||
681 | wValue = le16_to_cpu(ctrl->wValue); | |
682 | wLength = le16_to_cpu(ctrl->wLength); | |
683 | wIndex = le16_to_cpu(ctrl->wIndex); | |
684 | ||
685 | if (wIndex || wLength) | |
686 | return -EINVAL; | |
687 | ||
688 | /* | |
689 | * REVISIT It's unclear from Databook what to do with this | |
690 | * value. For now, just cache it. | |
691 | */ | |
692 | dwc->isoch_delay = wValue; | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
72246da4 FB |
697 | static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) |
698 | { | |
699 | int ret; | |
700 | ||
701 | switch (ctrl->bRequest) { | |
702 | case USB_REQ_GET_STATUS: | |
dab716cd | 703 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS"); |
72246da4 FB |
704 | ret = dwc3_ep0_handle_status(dwc, ctrl); |
705 | break; | |
706 | case USB_REQ_CLEAR_FEATURE: | |
dab716cd | 707 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE"); |
72246da4 FB |
708 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 0); |
709 | break; | |
710 | case USB_REQ_SET_FEATURE: | |
dab716cd | 711 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE"); |
72246da4 FB |
712 | ret = dwc3_ep0_handle_feature(dwc, ctrl, 1); |
713 | break; | |
714 | case USB_REQ_SET_ADDRESS: | |
dab716cd | 715 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS"); |
72246da4 FB |
716 | ret = dwc3_ep0_set_address(dwc, ctrl); |
717 | break; | |
718 | case USB_REQ_SET_CONFIGURATION: | |
dab716cd | 719 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION"); |
72246da4 FB |
720 | ret = dwc3_ep0_set_config(dwc, ctrl); |
721 | break; | |
865e09e7 | 722 | case USB_REQ_SET_SEL: |
dab716cd | 723 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL"); |
865e09e7 FB |
724 | ret = dwc3_ep0_set_sel(dwc, ctrl); |
725 | break; | |
c12a0d86 | 726 | case USB_REQ_SET_ISOCH_DELAY: |
dab716cd | 727 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY"); |
c12a0d86 FB |
728 | ret = dwc3_ep0_set_isoch_delay(dwc, ctrl); |
729 | break; | |
aebda618 JY |
730 | case USB_REQ_SET_INTERFACE: |
731 | dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_INTERFACE"); | |
732 | dwc->start_config_issued = false; | |
733 | /* Fall through */ | |
72246da4 | 734 | default: |
dab716cd | 735 | dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver"); |
72246da4 FB |
736 | ret = dwc3_ep0_delegate_req(dwc, ctrl); |
737 | break; | |
2b84f92b | 738 | } |
72246da4 FB |
739 | |
740 | return ret; | |
741 | } | |
742 | ||
743 | static void dwc3_ep0_inspect_setup(struct dwc3 *dwc, | |
744 | const struct dwc3_event_depevt *event) | |
745 | { | |
746 | struct usb_ctrlrequest *ctrl = dwc->ctrl_req; | |
ef21ede6 | 747 | int ret = -EINVAL; |
72246da4 FB |
748 | u32 len; |
749 | ||
750 | if (!dwc->gadget_driver) | |
ef21ede6 | 751 | goto out; |
72246da4 | 752 | |
2c4cbe6e FB |
753 | trace_dwc3_ctrl_req(ctrl); |
754 | ||
72246da4 | 755 | len = le16_to_cpu(ctrl->wLength); |
1ddcb218 | 756 | if (!len) { |
d95b09b9 FB |
757 | dwc->three_stage_setup = false; |
758 | dwc->ep0_expect_in = false; | |
1ddcb218 FB |
759 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
760 | } else { | |
d95b09b9 FB |
761 | dwc->three_stage_setup = true; |
762 | dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN); | |
1ddcb218 FB |
763 | dwc->ep0_next_event = DWC3_EP0_NRDY_DATA; |
764 | } | |
72246da4 FB |
765 | |
766 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) | |
767 | ret = dwc3_ep0_std_request(dwc, ctrl); | |
768 | else | |
769 | ret = dwc3_ep0_delegate_req(dwc, ctrl); | |
770 | ||
5bdb1dcc SAS |
771 | if (ret == USB_GADGET_DELAYED_STATUS) |
772 | dwc->delayed_status = true; | |
773 | ||
ef21ede6 FB |
774 | out: |
775 | if (ret < 0) | |
776 | dwc3_ep0_stall_and_restart(dwc); | |
72246da4 FB |
777 | } |
778 | ||
779 | static void dwc3_ep0_complete_data(struct dwc3 *dwc, | |
780 | const struct dwc3_event_depevt *event) | |
781 | { | |
782 | struct dwc3_request *r = NULL; | |
783 | struct usb_request *ur; | |
f6bafc6a | 784 | struct dwc3_trb *trb; |
c2da2ff0 | 785 | struct dwc3_ep *ep0; |
8a344220 KVA |
786 | unsigned transfer_size = 0; |
787 | unsigned maxp; | |
788 | unsigned remaining_ur_length; | |
789 | void *buf; | |
790 | u32 transferred = 0; | |
fca8892a | 791 | u32 status; |
f6bafc6a | 792 | u32 length; |
72246da4 FB |
793 | u8 epnum; |
794 | ||
795 | epnum = event->endpoint_number; | |
c2da2ff0 | 796 | ep0 = dwc->eps[0]; |
72246da4 | 797 | |
1ddcb218 FB |
798 | dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS; |
799 | ||
f6bafc6a | 800 | trb = dwc->ep0_trb; |
fca8892a | 801 | |
ccb072de FB |
802 | trace_dwc3_complete_trb(ep0, trb); |
803 | ||
520fe763 FB |
804 | r = next_request(&ep0->request_list); |
805 | if (!r) | |
806 | return; | |
807 | ||
fca8892a FB |
808 | status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
809 | if (status == DWC3_TRBSTS_SETUP_PENDING) { | |
2c4cbe6e | 810 | dwc3_trace(trace_dwc3_ep0, "Setup Pending received"); |
fca8892a FB |
811 | |
812 | if (r) | |
813 | dwc3_gadget_giveback(ep0, r, -ECONNRESET); | |
814 | ||
815 | return; | |
816 | } | |
817 | ||
6856d30c | 818 | ur = &r->request; |
8a344220 KVA |
819 | buf = ur->buf; |
820 | remaining_ur_length = ur->length; | |
6856d30c | 821 | |
f6bafc6a | 822 | length = trb->size & DWC3_TRB_SIZE_MASK; |
72246da4 | 823 | |
8a344220 KVA |
824 | maxp = ep0->endpoint.maxpacket; |
825 | ||
a6829706 | 826 | if (dwc->ep0_bounced) { |
8a344220 KVA |
827 | transfer_size = roundup((ur->length - transfer_size), |
828 | maxp); | |
b2fb5b1a KVA |
829 | |
830 | /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */ | |
831 | if (transfer_size > DWC3_EP0_BOUNCE_SIZE) | |
832 | transfer_size = DWC3_EP0_BOUNCE_SIZE; | |
833 | ||
8a344220 KVA |
834 | transferred = min_t(u32, remaining_ur_length, |
835 | transfer_size - length); | |
836 | memcpy(buf, dwc->ep0_bounce, transferred); | |
a6829706 | 837 | } else { |
f6bafc6a | 838 | transferred = ur->length - length; |
a6829706 | 839 | } |
72246da4 | 840 | |
cd423dd3 FB |
841 | ur->actual += transferred; |
842 | ||
72246da4 FB |
843 | if ((epnum & 1) && ur->actual < ur->length) { |
844 | /* for some reason we did not get everything out */ | |
845 | ||
846 | dwc3_ep0_stall_and_restart(dwc); | |
72246da4 | 847 | } else { |
36f84ffb FB |
848 | dwc3_gadget_giveback(ep0, r, 0); |
849 | ||
850 | if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) && | |
851 | ur->length && ur->zero) { | |
852 | int ret; | |
853 | ||
854 | dwc->ep0_next_event = DWC3_EP0_COMPLETE; | |
855 | ||
856 | ret = dwc3_ep0_start_trans(dwc, epnum, | |
857 | dwc->ctrl_req_addr, 0, | |
368ca113 | 858 | DWC3_TRBCTL_CONTROL_DATA, false); |
36f84ffb FB |
859 | WARN_ON(ret < 0); |
860 | } | |
72246da4 FB |
861 | } |
862 | } | |
863 | ||
85a78101 | 864 | static void dwc3_ep0_complete_status(struct dwc3 *dwc, |
72246da4 FB |
865 | const struct dwc3_event_depevt *event) |
866 | { | |
867 | struct dwc3_request *r; | |
868 | struct dwc3_ep *dep; | |
fca8892a FB |
869 | struct dwc3_trb *trb; |
870 | u32 status; | |
72246da4 | 871 | |
c7fcdeb2 | 872 | dep = dwc->eps[0]; |
fca8892a | 873 | trb = dwc->ep0_trb; |
72246da4 | 874 | |
ccb072de FB |
875 | trace_dwc3_complete_trb(dep, trb); |
876 | ||
72246da4 FB |
877 | if (!list_empty(&dep->request_list)) { |
878 | r = next_request(&dep->request_list); | |
879 | ||
880 | dwc3_gadget_giveback(dep, r, 0); | |
881 | } | |
882 | ||
3b637367 GC |
883 | if (dwc->test_mode) { |
884 | int ret; | |
885 | ||
886 | ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr); | |
887 | if (ret < 0) { | |
2c4cbe6e | 888 | dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d", |
3b637367 GC |
889 | dwc->test_mode_nr); |
890 | dwc3_ep0_stall_and_restart(dwc); | |
5c81abab | 891 | return; |
3b637367 GC |
892 | } |
893 | } | |
894 | ||
fca8892a FB |
895 | status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
896 | if (status == DWC3_TRBSTS_SETUP_PENDING) | |
dab716cd | 897 | dwc3_trace(trace_dwc3_ep0, "Setup Pending received"); |
fca8892a | 898 | |
c7fcdeb2 | 899 | dwc->ep0state = EP0_SETUP_PHASE; |
72246da4 FB |
900 | dwc3_ep0_out_start(dwc); |
901 | } | |
902 | ||
903 | static void dwc3_ep0_xfer_complete(struct dwc3 *dwc, | |
904 | const struct dwc3_event_depevt *event) | |
905 | { | |
c7fcdeb2 FB |
906 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; |
907 | ||
908 | dep->flags &= ~DWC3_EP_BUSY; | |
b4996a86 | 909 | dep->resource_index = 0; |
df62df56 | 910 | dwc->setup_packet_pending = false; |
c7fcdeb2 | 911 | |
72246da4 | 912 | switch (dwc->ep0state) { |
c7fcdeb2 | 913 | case EP0_SETUP_PHASE: |
2c4cbe6e | 914 | dwc3_trace(trace_dwc3_ep0, "Setup Phase"); |
72246da4 FB |
915 | dwc3_ep0_inspect_setup(dwc, event); |
916 | break; | |
917 | ||
c7fcdeb2 | 918 | case EP0_DATA_PHASE: |
2c4cbe6e | 919 | dwc3_trace(trace_dwc3_ep0, "Data Phase"); |
72246da4 FB |
920 | dwc3_ep0_complete_data(dwc, event); |
921 | break; | |
922 | ||
c7fcdeb2 | 923 | case EP0_STATUS_PHASE: |
2c4cbe6e | 924 | dwc3_trace(trace_dwc3_ep0, "Status Phase"); |
85a78101 | 925 | dwc3_ep0_complete_status(dwc, event); |
72246da4 | 926 | break; |
c7fcdeb2 FB |
927 | default: |
928 | WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); | |
929 | } | |
930 | } | |
72246da4 | 931 | |
a0807881 FB |
932 | static void __dwc3_ep0_do_control_data(struct dwc3 *dwc, |
933 | struct dwc3_ep *dep, struct dwc3_request *req) | |
c7fcdeb2 | 934 | { |
c7fcdeb2 FB |
935 | int ret; |
936 | ||
a0807881 | 937 | req->direction = !!dep->number; |
c7fcdeb2 | 938 | |
c7fcdeb2 | 939 | if (req->request.length == 0) { |
a0807881 | 940 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
c7fcdeb2 | 941 | dwc->ctrl_req_addr, 0, |
368ca113 | 942 | DWC3_TRBCTL_CONTROL_DATA, false); |
c74c6d4a | 943 | } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) |
a0807881 | 944 | && (dep->number == 0)) { |
8a344220 | 945 | u32 transfer_size = 0; |
c390b036 | 946 | u32 maxpacket; |
a0807881 | 947 | |
0fc9a1be | 948 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
a0807881 | 949 | dep->number); |
0fc9a1be FB |
950 | if (ret) { |
951 | dev_dbg(dwc->dev, "failed to map request\n"); | |
952 | return; | |
953 | } | |
c7fcdeb2 | 954 | |
c390b036 | 955 | maxpacket = dep->endpoint.maxpacket; |
8a344220 KVA |
956 | transfer_size = roundup((req->request.length - transfer_size), |
957 | maxpacket); | |
a0807881 | 958 | |
b2fb5b1a KVA |
959 | if (transfer_size > DWC3_EP0_BOUNCE_SIZE) { |
960 | dev_WARN(dwc->dev, "bounce buf can't handle req len\n"); | |
961 | transfer_size = DWC3_EP0_BOUNCE_SIZE; | |
962 | } | |
963 | ||
c7fcdeb2 FB |
964 | dwc->ep0_bounced = true; |
965 | ||
966 | /* | |
4552a0ca FB |
967 | * REVISIT in case request length is bigger than |
968 | * DWC3_EP0_BOUNCE_SIZE we will need two chained | |
969 | * TRBs to handle the transfer. | |
c7fcdeb2 | 970 | */ |
a0807881 FB |
971 | ret = dwc3_ep0_start_trans(dwc, dep->number, |
972 | dwc->ep0_bounce_addr, transfer_size, | |
368ca113 | 973 | DWC3_TRBCTL_CONTROL_DATA, false); |
c7fcdeb2 | 974 | } else { |
0fc9a1be | 975 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
a0807881 | 976 | dep->number); |
0fc9a1be FB |
977 | if (ret) { |
978 | dev_dbg(dwc->dev, "failed to map request\n"); | |
979 | return; | |
980 | } | |
c7fcdeb2 | 981 | |
a0807881 | 982 | ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma, |
368ca113 KVA |
983 | req->request.length, DWC3_TRBCTL_CONTROL_DATA, |
984 | false); | |
c7fcdeb2 FB |
985 | } |
986 | ||
987 | WARN_ON(ret < 0); | |
72246da4 FB |
988 | } |
989 | ||
f0f2b2a2 | 990 | static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) |
72246da4 | 991 | { |
f0f2b2a2 | 992 | struct dwc3 *dwc = dep->dwc; |
c7fcdeb2 | 993 | u32 type; |
72246da4 | 994 | |
c7fcdeb2 FB |
995 | type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3 |
996 | : DWC3_TRBCTL_CONTROL_STATUS2; | |
997 | ||
f0f2b2a2 | 998 | return dwc3_ep0_start_trans(dwc, dep->number, |
368ca113 | 999 | dwc->ctrl_req_addr, 0, type, false); |
f0f2b2a2 | 1000 | } |
c7fcdeb2 | 1001 | |
788a23f4 | 1002 | static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep) |
f0f2b2a2 | 1003 | { |
457e84b6 | 1004 | if (dwc->resize_fifos) { |
2c4cbe6e | 1005 | dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs"); |
457e84b6 FB |
1006 | dwc3_gadget_resize_tx_fifos(dwc); |
1007 | dwc->resize_fifos = 0; | |
1008 | } | |
1009 | ||
f0f2b2a2 | 1010 | WARN_ON(dwc3_ep0_start_control_status(dep)); |
c7fcdeb2 FB |
1011 | } |
1012 | ||
788a23f4 FB |
1013 | static void dwc3_ep0_do_control_status(struct dwc3 *dwc, |
1014 | const struct dwc3_event_depevt *event) | |
1015 | { | |
1016 | struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; | |
1017 | ||
1018 | __dwc3_ep0_do_control_status(dwc, dep); | |
1019 | } | |
1020 | ||
2e3db064 FB |
1021 | static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep) |
1022 | { | |
1023 | struct dwc3_gadget_ep_cmd_params params; | |
1024 | u32 cmd; | |
1025 | int ret; | |
1026 | ||
1027 | if (!dep->resource_index) | |
1028 | return; | |
1029 | ||
1030 | cmd = DWC3_DEPCMD_ENDTRANSFER; | |
1031 | cmd |= DWC3_DEPCMD_CMDIOC; | |
1032 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); | |
1033 | memset(¶ms, 0, sizeof(params)); | |
1034 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); | |
1035 | WARN_ON_ONCE(ret); | |
1036 | dep->resource_index = 0; | |
1037 | } | |
1038 | ||
c7fcdeb2 FB |
1039 | static void dwc3_ep0_xfernotready(struct dwc3 *dwc, |
1040 | const struct dwc3_event_depevt *event) | |
1041 | { | |
df62df56 FB |
1042 | dwc->setup_packet_pending = true; |
1043 | ||
c7fcdeb2 | 1044 | switch (event->status) { |
c7fcdeb2 | 1045 | case DEPEVT_STATUS_CONTROL_DATA: |
2c4cbe6e | 1046 | dwc3_trace(trace_dwc3_ep0, "Control Data"); |
1ddcb218 | 1047 | |
55f3fba6 | 1048 | /* |
2e3db064 FB |
1049 | * We already have a DATA transfer in the controller's cache, |
1050 | * if we receive a XferNotReady(DATA) we will ignore it, unless | |
1051 | * it's for the wrong direction. | |
55f3fba6 | 1052 | * |
2e3db064 FB |
1053 | * In that case, we must issue END_TRANSFER command to the Data |
1054 | * Phase we already have started and issue SetStall on the | |
1055 | * control endpoint. | |
55f3fba6 FB |
1056 | */ |
1057 | if (dwc->ep0_expect_in != event->endpoint_number) { | |
2e3db064 FB |
1058 | struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in]; |
1059 | ||
2c4cbe6e FB |
1060 | dwc3_trace(trace_dwc3_ep0, |
1061 | "Wrong direction for Data phase"); | |
2e3db064 | 1062 | dwc3_ep0_end_control_data(dwc, dep); |
55f3fba6 FB |
1063 | dwc3_ep0_stall_and_restart(dwc); |
1064 | return; | |
1065 | } | |
1066 | ||
c7fcdeb2 | 1067 | break; |
1ddcb218 | 1068 | |
c7fcdeb2 | 1069 | case DEPEVT_STATUS_CONTROL_STATUS: |
77fa6df8 FB |
1070 | if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) |
1071 | return; | |
1072 | ||
2c4cbe6e | 1073 | dwc3_trace(trace_dwc3_ep0, "Control Status"); |
1ddcb218 | 1074 | |
f0f2b2a2 SAS |
1075 | dwc->ep0state = EP0_STATUS_PHASE; |
1076 | ||
5bdb1dcc SAS |
1077 | if (dwc->delayed_status) { |
1078 | WARN_ON_ONCE(event->endpoint_number != 1); | |
2c4cbe6e | 1079 | dwc3_trace(trace_dwc3_ep0, "Delayed Status"); |
5bdb1dcc SAS |
1080 | return; |
1081 | } | |
1082 | ||
788a23f4 | 1083 | dwc3_ep0_do_control_status(dwc, event); |
72246da4 FB |
1084 | } |
1085 | } | |
1086 | ||
1087 | void dwc3_ep0_interrupt(struct dwc3 *dwc, | |
8becf270 | 1088 | const struct dwc3_event_depevt *event) |
72246da4 FB |
1089 | { |
1090 | u8 epnum = event->endpoint_number; | |
1091 | ||
2c4cbe6e | 1092 | dwc3_trace(trace_dwc3_ep0, "%s while ep%d%s in state '%s'", |
72246da4 | 1093 | dwc3_ep_event_string(event->endpoint_event), |
b147f357 | 1094 | epnum >> 1, (epnum & 1) ? "in" : "out", |
72246da4 FB |
1095 | dwc3_ep0_state_string(dwc->ep0state)); |
1096 | ||
1097 | switch (event->endpoint_event) { | |
1098 | case DWC3_DEPEVT_XFERCOMPLETE: | |
1099 | dwc3_ep0_xfer_complete(dwc, event); | |
1100 | break; | |
1101 | ||
1102 | case DWC3_DEPEVT_XFERNOTREADY: | |
1103 | dwc3_ep0_xfernotready(dwc, event); | |
1104 | break; | |
1105 | ||
1106 | case DWC3_DEPEVT_XFERINPROGRESS: | |
1107 | case DWC3_DEPEVT_RXTXFIFOEVT: | |
1108 | case DWC3_DEPEVT_STREAMEVT: | |
1109 | case DWC3_DEPEVT_EPCMDCMPLT: | |
1110 | break; | |
1111 | } | |
1112 | } |