ep0.c 28 KB

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