ep0.c 27 KB

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