ep0.c 27 KB

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