ep0.c 26 KB

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