ep0.c 27 KB

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