ep0.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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 *dwc, u8 epnum,
  38. dma_addr_t buf_dma, u32 len, u32 type, bool chain)
  39. {
  40. struct dwc3_trb *trb;
  41. struct dwc3_ep *dep;
  42. dep = dwc->eps[epnum];
  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 *dwc, u8 epnum)
  60. {
  61. struct dwc3_gadget_ep_cmd_params params;
  62. struct dwc3_ep *dep;
  63. int ret;
  64. dep = dwc->eps[epnum];
  65. if (dep->flags & DWC3_EP_BUSY)
  66. return 0;
  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. int ret;
  226. complete(&dwc->ep0_in_setup);
  227. dwc3_ep0_prepare_one_trb(dwc, 0, dwc->ctrl_req_addr, 8,
  228. DWC3_TRBCTL_CONTROL_SETUP, false);
  229. ret = dwc3_ep0_start_trans(dwc, 0);
  230. WARN_ON(ret < 0);
  231. }
  232. static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
  233. {
  234. struct dwc3_ep *dep;
  235. u32 windex = le16_to_cpu(wIndex_le);
  236. u32 epnum;
  237. epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
  238. if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  239. epnum |= 1;
  240. dep = dwc->eps[epnum];
  241. if (dep->flags & DWC3_EP_ENABLED)
  242. return dep;
  243. return NULL;
  244. }
  245. static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
  246. {
  247. }
  248. /*
  249. * ch 9.4.5
  250. */
  251. static int dwc3_ep0_handle_status(struct dwc3 *dwc,
  252. struct usb_ctrlrequest *ctrl)
  253. {
  254. struct dwc3_ep *dep;
  255. u32 recip;
  256. u32 reg;
  257. u16 usb_status = 0;
  258. __le16 *response_pkt;
  259. recip = ctrl->bRequestType & USB_RECIP_MASK;
  260. switch (recip) {
  261. case USB_RECIP_DEVICE:
  262. /*
  263. * LTM will be set once we know how to set this in HW.
  264. */
  265. usb_status |= dwc->gadget.is_selfpowered;
  266. if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
  267. (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
  268. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  269. if (reg & DWC3_DCTL_INITU1ENA)
  270. usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
  271. if (reg & DWC3_DCTL_INITU2ENA)
  272. usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
  273. }
  274. break;
  275. case USB_RECIP_INTERFACE:
  276. /*
  277. * Function Remote Wake Capable D0
  278. * Function Remote Wakeup D1
  279. */
  280. break;
  281. case USB_RECIP_ENDPOINT:
  282. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  283. if (!dep)
  284. return -EINVAL;
  285. if (dep->flags & DWC3_EP_STALL)
  286. usb_status = 1 << USB_ENDPOINT_HALT;
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. response_pkt = (__le16 *) dwc->setup_buf;
  292. *response_pkt = cpu_to_le16(usb_status);
  293. dep = dwc->eps[0];
  294. dwc->ep0_usb_req.dep = dep;
  295. dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
  296. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  297. dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
  298. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  299. }
  300. static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
  301. int set)
  302. {
  303. u32 reg;
  304. if (state != USB_STATE_CONFIGURED)
  305. return -EINVAL;
  306. if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
  307. (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
  308. return -EINVAL;
  309. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  310. if (set)
  311. reg |= DWC3_DCTL_INITU1ENA;
  312. else
  313. reg &= ~DWC3_DCTL_INITU1ENA;
  314. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  315. return 0;
  316. }
  317. static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
  318. int set)
  319. {
  320. u32 reg;
  321. if (state != USB_STATE_CONFIGURED)
  322. return -EINVAL;
  323. if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
  324. (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
  325. return -EINVAL;
  326. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  327. if (set)
  328. reg |= DWC3_DCTL_INITU2ENA;
  329. else
  330. reg &= ~DWC3_DCTL_INITU2ENA;
  331. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  332. return 0;
  333. }
  334. static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
  335. u32 wIndex, int set)
  336. {
  337. if ((wIndex & 0xff) != 0)
  338. return -EINVAL;
  339. if (!set)
  340. return -EINVAL;
  341. switch (wIndex >> 8) {
  342. case TEST_J:
  343. case TEST_K:
  344. case TEST_SE0_NAK:
  345. case TEST_PACKET:
  346. case TEST_FORCE_EN:
  347. dwc->test_mode_nr = wIndex >> 8;
  348. dwc->test_mode = true;
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. return 0;
  354. }
  355. static int dwc3_ep0_handle_device(struct dwc3 *dwc,
  356. struct usb_ctrlrequest *ctrl, int set)
  357. {
  358. enum usb_device_state state;
  359. u32 wValue;
  360. u32 wIndex;
  361. int ret = 0;
  362. wValue = le16_to_cpu(ctrl->wValue);
  363. wIndex = le16_to_cpu(ctrl->wIndex);
  364. state = dwc->gadget.state;
  365. switch (wValue) {
  366. case USB_DEVICE_REMOTE_WAKEUP:
  367. break;
  368. /*
  369. * 9.4.1 says only only for SS, in AddressState only for
  370. * default control pipe
  371. */
  372. case USB_DEVICE_U1_ENABLE:
  373. ret = dwc3_ep0_handle_u1(dwc, state, set);
  374. break;
  375. case USB_DEVICE_U2_ENABLE:
  376. ret = dwc3_ep0_handle_u2(dwc, state, set);
  377. break;
  378. case USB_DEVICE_LTM_ENABLE:
  379. ret = -EINVAL;
  380. break;
  381. case USB_DEVICE_TEST_MODE:
  382. ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
  383. break;
  384. default:
  385. ret = -EINVAL;
  386. }
  387. return ret;
  388. }
  389. static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
  390. struct usb_ctrlrequest *ctrl, int set)
  391. {
  392. enum usb_device_state state;
  393. u32 wValue;
  394. u32 wIndex;
  395. int ret = 0;
  396. wValue = le16_to_cpu(ctrl->wValue);
  397. wIndex = le16_to_cpu(ctrl->wIndex);
  398. state = dwc->gadget.state;
  399. switch (wValue) {
  400. case USB_INTRF_FUNC_SUSPEND:
  401. /*
  402. * REVISIT: Ideally we would enable some low power mode here,
  403. * however it's unclear what we should be doing here.
  404. *
  405. * For now, we're not doing anything, just making sure we return
  406. * 0 so USB Command Verifier tests pass without any errors.
  407. */
  408. break;
  409. default:
  410. ret = -EINVAL;
  411. }
  412. return ret;
  413. }
  414. static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
  415. struct usb_ctrlrequest *ctrl, int set)
  416. {
  417. struct dwc3_ep *dep;
  418. enum usb_device_state state;
  419. u32 wValue;
  420. u32 wIndex;
  421. int ret;
  422. wValue = le16_to_cpu(ctrl->wValue);
  423. wIndex = le16_to_cpu(ctrl->wIndex);
  424. state = dwc->gadget.state;
  425. switch (wValue) {
  426. case USB_ENDPOINT_HALT:
  427. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  428. if (!dep)
  429. return -EINVAL;
  430. if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
  431. break;
  432. ret = __dwc3_gadget_ep_set_halt(dep, set, true);
  433. if (ret)
  434. return -EINVAL;
  435. break;
  436. default:
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
  442. struct usb_ctrlrequest *ctrl, int set)
  443. {
  444. u32 recip;
  445. int ret;
  446. enum usb_device_state state;
  447. recip = ctrl->bRequestType & USB_RECIP_MASK;
  448. state = dwc->gadget.state;
  449. switch (recip) {
  450. case USB_RECIP_DEVICE:
  451. ret = dwc3_ep0_handle_device(dwc, ctrl, set);
  452. break;
  453. case USB_RECIP_INTERFACE:
  454. ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
  455. break;
  456. case USB_RECIP_ENDPOINT:
  457. ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
  458. break;
  459. default:
  460. ret = -EINVAL;
  461. }
  462. return ret;
  463. }
  464. static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  465. {
  466. enum usb_device_state state = dwc->gadget.state;
  467. u32 addr;
  468. u32 reg;
  469. addr = le16_to_cpu(ctrl->wValue);
  470. if (addr > 127) {
  471. dev_err(dwc->dev, "invalid device address %d\n", addr);
  472. return -EINVAL;
  473. }
  474. if (state == USB_STATE_CONFIGURED) {
  475. dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
  476. return -EINVAL;
  477. }
  478. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  479. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  480. reg |= DWC3_DCFG_DEVADDR(addr);
  481. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  482. if (addr)
  483. usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
  484. else
  485. usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
  486. return 0;
  487. }
  488. static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  489. {
  490. int ret;
  491. spin_unlock(&dwc->lock);
  492. ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
  493. spin_lock(&dwc->lock);
  494. return ret;
  495. }
  496. static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  497. {
  498. enum usb_device_state state = dwc->gadget.state;
  499. u32 cfg;
  500. int ret;
  501. u32 reg;
  502. cfg = le16_to_cpu(ctrl->wValue);
  503. switch (state) {
  504. case USB_STATE_DEFAULT:
  505. return -EINVAL;
  506. case USB_STATE_ADDRESS:
  507. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  508. /* if the cfg matches and the cfg is non zero */
  509. if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
  510. /*
  511. * only change state if set_config has already
  512. * been processed. If gadget driver returns
  513. * USB_GADGET_DELAYED_STATUS, we will wait
  514. * to change the state on the next usb_ep_queue()
  515. */
  516. if (ret == 0)
  517. usb_gadget_set_state(&dwc->gadget,
  518. USB_STATE_CONFIGURED);
  519. /*
  520. * Enable transition to U1/U2 state when
  521. * nothing is pending from application.
  522. */
  523. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  524. reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
  525. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  526. }
  527. break;
  528. case USB_STATE_CONFIGURED:
  529. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  530. if (!cfg && !ret)
  531. usb_gadget_set_state(&dwc->gadget,
  532. USB_STATE_ADDRESS);
  533. break;
  534. default:
  535. ret = -EINVAL;
  536. }
  537. return ret;
  538. }
  539. static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
  540. {
  541. struct dwc3_ep *dep = to_dwc3_ep(ep);
  542. struct dwc3 *dwc = dep->dwc;
  543. u32 param = 0;
  544. u32 reg;
  545. struct timing {
  546. u8 u1sel;
  547. u8 u1pel;
  548. __le16 u2sel;
  549. __le16 u2pel;
  550. } __packed timing;
  551. int ret;
  552. memcpy(&timing, req->buf, sizeof(timing));
  553. dwc->u1sel = timing.u1sel;
  554. dwc->u1pel = timing.u1pel;
  555. dwc->u2sel = le16_to_cpu(timing.u2sel);
  556. dwc->u2pel = le16_to_cpu(timing.u2pel);
  557. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  558. if (reg & DWC3_DCTL_INITU2ENA)
  559. param = dwc->u2pel;
  560. if (reg & DWC3_DCTL_INITU1ENA)
  561. param = dwc->u1pel;
  562. /*
  563. * According to Synopsys Databook, if parameter is
  564. * greater than 125, a value of zero should be
  565. * programmed in the register.
  566. */
  567. if (param > 125)
  568. param = 0;
  569. /* now that we have the time, issue DGCMD Set Sel */
  570. ret = dwc3_send_gadget_generic_command(dwc,
  571. DWC3_DGCMD_SET_PERIODIC_PAR, param);
  572. WARN_ON(ret < 0);
  573. }
  574. static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  575. {
  576. struct dwc3_ep *dep;
  577. enum usb_device_state state = dwc->gadget.state;
  578. u16 wLength;
  579. u16 wValue;
  580. if (state == USB_STATE_DEFAULT)
  581. return -EINVAL;
  582. wValue = le16_to_cpu(ctrl->wValue);
  583. wLength = le16_to_cpu(ctrl->wLength);
  584. if (wLength != 6) {
  585. dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
  586. wLength);
  587. return -EINVAL;
  588. }
  589. /*
  590. * To handle Set SEL we need to receive 6 bytes from Host. So let's
  591. * queue a usb_request for 6 bytes.
  592. *
  593. * Remember, though, this controller can't handle non-wMaxPacketSize
  594. * aligned transfers on the OUT direction, so we queue a request for
  595. * wMaxPacketSize instead.
  596. */
  597. dep = dwc->eps[0];
  598. dwc->ep0_usb_req.dep = dep;
  599. dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
  600. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  601. dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
  602. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  603. }
  604. static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  605. {
  606. u16 wLength;
  607. u16 wValue;
  608. u16 wIndex;
  609. wValue = le16_to_cpu(ctrl->wValue);
  610. wLength = le16_to_cpu(ctrl->wLength);
  611. wIndex = le16_to_cpu(ctrl->wIndex);
  612. if (wIndex || wLength)
  613. return -EINVAL;
  614. /*
  615. * REVISIT It's unclear from Databook what to do with this
  616. * value. For now, just cache it.
  617. */
  618. dwc->isoch_delay = wValue;
  619. return 0;
  620. }
  621. static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  622. {
  623. int ret;
  624. switch (ctrl->bRequest) {
  625. case USB_REQ_GET_STATUS:
  626. ret = dwc3_ep0_handle_status(dwc, ctrl);
  627. break;
  628. case USB_REQ_CLEAR_FEATURE:
  629. ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
  630. break;
  631. case USB_REQ_SET_FEATURE:
  632. ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
  633. break;
  634. case USB_REQ_SET_ADDRESS:
  635. ret = dwc3_ep0_set_address(dwc, ctrl);
  636. break;
  637. case USB_REQ_SET_CONFIGURATION:
  638. ret = dwc3_ep0_set_config(dwc, ctrl);
  639. break;
  640. case USB_REQ_SET_SEL:
  641. ret = dwc3_ep0_set_sel(dwc, ctrl);
  642. break;
  643. case USB_REQ_SET_ISOCH_DELAY:
  644. ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
  645. break;
  646. default:
  647. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  648. break;
  649. }
  650. return ret;
  651. }
  652. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  653. const struct dwc3_event_depevt *event)
  654. {
  655. struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
  656. int ret = -EINVAL;
  657. u32 len;
  658. if (!dwc->gadget_driver)
  659. goto out;
  660. trace_dwc3_ctrl_req(ctrl);
  661. len = le16_to_cpu(ctrl->wLength);
  662. if (!len) {
  663. dwc->three_stage_setup = false;
  664. dwc->ep0_expect_in = false;
  665. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  666. } else {
  667. dwc->three_stage_setup = true;
  668. dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
  669. dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
  670. }
  671. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  672. ret = dwc3_ep0_std_request(dwc, ctrl);
  673. else
  674. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  675. if (ret == USB_GADGET_DELAYED_STATUS)
  676. dwc->delayed_status = true;
  677. out:
  678. if (ret < 0)
  679. dwc3_ep0_stall_and_restart(dwc);
  680. }
  681. static void dwc3_ep0_complete_data(struct dwc3 *dwc,
  682. const struct dwc3_event_depevt *event)
  683. {
  684. struct dwc3_request *r = NULL;
  685. struct usb_request *ur;
  686. struct dwc3_trb *trb;
  687. struct dwc3_ep *ep0;
  688. unsigned transfer_size = 0;
  689. unsigned maxp;
  690. unsigned remaining_ur_length;
  691. void *buf;
  692. u32 transferred = 0;
  693. u32 status;
  694. u32 length;
  695. u8 epnum;
  696. epnum = event->endpoint_number;
  697. ep0 = dwc->eps[0];
  698. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  699. trb = dwc->ep0_trb;
  700. trace_dwc3_complete_trb(ep0, trb);
  701. r = next_request(&ep0->pending_list);
  702. if (!r)
  703. return;
  704. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  705. if (status == DWC3_TRBSTS_SETUP_PENDING) {
  706. dwc->setup_packet_pending = true;
  707. if (r)
  708. dwc3_gadget_giveback(ep0, r, -ECONNRESET);
  709. return;
  710. }
  711. ur = &r->request;
  712. buf = ur->buf;
  713. remaining_ur_length = ur->length;
  714. length = trb->size & DWC3_TRB_SIZE_MASK;
  715. maxp = ep0->endpoint.maxpacket;
  716. if (dwc->ep0_bounced) {
  717. /*
  718. * Handle the first TRB before handling the bounce buffer if
  719. * the request length is greater than the bounce buffer size
  720. */
  721. if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
  722. transfer_size = ALIGN(ur->length - maxp, maxp);
  723. transferred = transfer_size - length;
  724. buf = (u8 *)buf + transferred;
  725. ur->actual += transferred;
  726. remaining_ur_length -= transferred;
  727. trb++;
  728. length = trb->size & DWC3_TRB_SIZE_MASK;
  729. ep0->trb_enqueue = 0;
  730. }
  731. transfer_size = roundup((ur->length - transfer_size),
  732. maxp);
  733. transferred = min_t(u32, remaining_ur_length,
  734. transfer_size - length);
  735. memcpy(buf, dwc->ep0_bounce, transferred);
  736. } else {
  737. transferred = ur->length - length;
  738. }
  739. ur->actual += transferred;
  740. if ((epnum & 1) && ur->actual < ur->length) {
  741. /* for some reason we did not get everything out */
  742. dwc3_ep0_stall_and_restart(dwc);
  743. } else {
  744. dwc3_gadget_giveback(ep0, r, 0);
  745. if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
  746. ur->length && ur->zero) {
  747. int ret;
  748. dwc->ep0_next_event = DWC3_EP0_COMPLETE;
  749. dwc3_ep0_prepare_one_trb(dwc, epnum, dwc->ctrl_req_addr,
  750. 0, DWC3_TRBCTL_CONTROL_DATA, false);
  751. ret = dwc3_ep0_start_trans(dwc, epnum);
  752. WARN_ON(ret < 0);
  753. }
  754. }
  755. }
  756. static void dwc3_ep0_complete_status(struct dwc3 *dwc,
  757. const struct dwc3_event_depevt *event)
  758. {
  759. struct dwc3_request *r;
  760. struct dwc3_ep *dep;
  761. struct dwc3_trb *trb;
  762. u32 status;
  763. dep = dwc->eps[0];
  764. trb = dwc->ep0_trb;
  765. trace_dwc3_complete_trb(dep, trb);
  766. if (!list_empty(&dep->pending_list)) {
  767. r = next_request(&dep->pending_list);
  768. dwc3_gadget_giveback(dep, r, 0);
  769. }
  770. if (dwc->test_mode) {
  771. int ret;
  772. ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
  773. if (ret < 0) {
  774. dev_err(dwc->dev, "invalid test #%d\n",
  775. dwc->test_mode_nr);
  776. dwc3_ep0_stall_and_restart(dwc);
  777. return;
  778. }
  779. }
  780. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  781. if (status == DWC3_TRBSTS_SETUP_PENDING)
  782. dwc->setup_packet_pending = true;
  783. dwc->ep0state = EP0_SETUP_PHASE;
  784. dwc3_ep0_out_start(dwc);
  785. }
  786. static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
  787. const struct dwc3_event_depevt *event)
  788. {
  789. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  790. dep->flags &= ~DWC3_EP_BUSY;
  791. dep->resource_index = 0;
  792. dwc->setup_packet_pending = false;
  793. switch (dwc->ep0state) {
  794. case EP0_SETUP_PHASE:
  795. dwc3_ep0_inspect_setup(dwc, event);
  796. break;
  797. case EP0_DATA_PHASE:
  798. dwc3_ep0_complete_data(dwc, event);
  799. break;
  800. case EP0_STATUS_PHASE:
  801. dwc3_ep0_complete_status(dwc, event);
  802. break;
  803. default:
  804. WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
  805. }
  806. }
  807. static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
  808. struct dwc3_ep *dep, struct dwc3_request *req)
  809. {
  810. int ret;
  811. req->direction = !!dep->number;
  812. if (req->request.length == 0) {
  813. dwc3_ep0_prepare_one_trb(dwc, dep->number,
  814. dwc->ctrl_req_addr, 0,
  815. DWC3_TRBCTL_CONTROL_DATA, false);
  816. ret = dwc3_ep0_start_trans(dwc, dep->number);
  817. } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
  818. && (dep->number == 0)) {
  819. u32 transfer_size = 0;
  820. u32 maxpacket;
  821. ret = usb_gadget_map_request_by_dev(dwc->sysdev,
  822. &req->request, dep->number);
  823. if (ret)
  824. return;
  825. maxpacket = dep->endpoint.maxpacket;
  826. if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
  827. transfer_size = ALIGN(req->request.length - maxpacket,
  828. maxpacket);
  829. dwc3_ep0_prepare_one_trb(dwc, dep->number,
  830. req->request.dma,
  831. transfer_size,
  832. DWC3_TRBCTL_CONTROL_DATA,
  833. true);
  834. }
  835. transfer_size = roundup((req->request.length - transfer_size),
  836. maxpacket);
  837. dwc->ep0_bounced = true;
  838. dwc3_ep0_prepare_one_trb(dwc, dep->number,
  839. dwc->ep0_bounce_addr, transfer_size,
  840. DWC3_TRBCTL_CONTROL_DATA, false);
  841. ret = dwc3_ep0_start_trans(dwc, dep->number);
  842. } else {
  843. ret = usb_gadget_map_request_by_dev(dwc->sysdev,
  844. &req->request, dep->number);
  845. if (ret)
  846. return;
  847. dwc3_ep0_prepare_one_trb(dwc, dep->number, req->request.dma,
  848. req->request.length, DWC3_TRBCTL_CONTROL_DATA,
  849. false);
  850. ret = dwc3_ep0_start_trans(dwc, dep->number);
  851. }
  852. WARN_ON(ret < 0);
  853. }
  854. static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
  855. {
  856. struct dwc3 *dwc = dep->dwc;
  857. u32 type;
  858. type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
  859. : DWC3_TRBCTL_CONTROL_STATUS2;
  860. dwc3_ep0_prepare_one_trb(dwc, dep->number,
  861. dwc->ctrl_req_addr, 0, type, false);
  862. return dwc3_ep0_start_trans(dwc, dep->number);
  863. }
  864. static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
  865. {
  866. WARN_ON(dwc3_ep0_start_control_status(dep));
  867. }
  868. static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
  869. const struct dwc3_event_depevt *event)
  870. {
  871. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  872. __dwc3_ep0_do_control_status(dwc, dep);
  873. }
  874. static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
  875. {
  876. struct dwc3_gadget_ep_cmd_params params;
  877. u32 cmd;
  878. int ret;
  879. if (!dep->resource_index)
  880. return;
  881. cmd = DWC3_DEPCMD_ENDTRANSFER;
  882. cmd |= DWC3_DEPCMD_CMDIOC;
  883. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  884. memset(&params, 0, sizeof(params));
  885. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  886. WARN_ON_ONCE(ret);
  887. dep->resource_index = 0;
  888. }
  889. static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
  890. const struct dwc3_event_depevt *event)
  891. {
  892. switch (event->status) {
  893. case DEPEVT_STATUS_CONTROL_DATA:
  894. /*
  895. * We already have a DATA transfer in the controller's cache,
  896. * if we receive a XferNotReady(DATA) we will ignore it, unless
  897. * it's for the wrong direction.
  898. *
  899. * In that case, we must issue END_TRANSFER command to the Data
  900. * Phase we already have started and issue SetStall on the
  901. * control endpoint.
  902. */
  903. if (dwc->ep0_expect_in != event->endpoint_number) {
  904. struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
  905. dev_err(dwc->dev, "unexpected direction for Data Phase\n");
  906. dwc3_ep0_end_control_data(dwc, dep);
  907. dwc3_ep0_stall_and_restart(dwc);
  908. return;
  909. }
  910. break;
  911. case DEPEVT_STATUS_CONTROL_STATUS:
  912. if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
  913. return;
  914. dwc->ep0state = EP0_STATUS_PHASE;
  915. if (dwc->delayed_status) {
  916. WARN_ON_ONCE(event->endpoint_number != 1);
  917. return;
  918. }
  919. dwc3_ep0_do_control_status(dwc, event);
  920. }
  921. }
  922. void dwc3_ep0_interrupt(struct dwc3 *dwc,
  923. const struct dwc3_event_depevt *event)
  924. {
  925. switch (event->endpoint_event) {
  926. case DWC3_DEPEVT_XFERCOMPLETE:
  927. dwc3_ep0_xfer_complete(dwc, event);
  928. break;
  929. case DWC3_DEPEVT_XFERNOTREADY:
  930. dwc3_ep0_xfernotready(dwc, event);
  931. break;
  932. case DWC3_DEPEVT_XFERINPROGRESS:
  933. case DWC3_DEPEVT_RXTXFIFOEVT:
  934. case DWC3_DEPEVT_STREAMEVT:
  935. case DWC3_DEPEVT_EPCMDCMPLT:
  936. break;
  937. }
  938. }