ep0.c 26 KB

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