ep0.c 26 KB

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