ep0.c 26 KB

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