ep0.c 26 KB

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