ep0.c 26 KB

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