ep0.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. * All rights reserved.
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. The names of the above-listed copyright holders may not be used
  20. * to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * ALTERNATIVELY, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2, as published by the Free
  25. * Software Foundation.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/io.h>
  46. #include <linux/list.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/usb/ch9.h>
  49. #include <linux/usb/gadget.h>
  50. #include "core.h"
  51. #include "gadget.h"
  52. #include "io.h"
  53. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  54. const struct dwc3_event_depevt *event);
  55. static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
  56. {
  57. switch (state) {
  58. case EP0_UNCONNECTED:
  59. return "Unconnected";
  60. case EP0_SETUP_PHASE:
  61. return "Setup Phase";
  62. case EP0_DATA_PHASE:
  63. return "Data Phase";
  64. case EP0_STATUS_PHASE:
  65. return "Status Phase";
  66. default:
  67. return "UNKNOWN";
  68. }
  69. }
  70. static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
  71. u32 len, u32 type)
  72. {
  73. struct dwc3_gadget_ep_cmd_params params;
  74. struct dwc3_trb_hw *trb_hw;
  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_hw = dwc->ep0_trb;
  84. memset(&trb, 0, sizeof(trb));
  85. trb.trbctl = type;
  86. trb.bplh = buf_dma;
  87. trb.length = len;
  88. trb.hwo = 1;
  89. trb.lst = 1;
  90. trb.ioc = 1;
  91. trb.isp_imi = 1;
  92. dwc3_trb_to_hw(&trb, trb_hw);
  93. memset(&params, 0, sizeof(params));
  94. params.param0.depstrtxfer.transfer_desc_addr_high =
  95. upper_32_bits(dwc->ep0_trb_addr);
  96. params.param1.depstrtxfer.transfer_desc_addr_low =
  97. lower_32_bits(dwc->ep0_trb_addr);
  98. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  99. DWC3_DEPCMD_STARTTRANSFER, &params);
  100. if (ret < 0) {
  101. dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
  102. return ret;
  103. }
  104. dep->flags |= DWC3_EP_BUSY;
  105. dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
  106. dep->number);
  107. dwc->ep0_next_event = DWC3_EP0_COMPLETE;
  108. return 0;
  109. }
  110. static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
  111. struct dwc3_request *req)
  112. {
  113. int ret = 0;
  114. req->request.actual = 0;
  115. req->request.status = -EINPROGRESS;
  116. req->epnum = dep->number;
  117. list_add_tail(&req->list, &dep->request_list);
  118. /*
  119. * Gadget driver might not be quick enough to queue a request
  120. * before we get a Transfer Not Ready event on this endpoint.
  121. *
  122. * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
  123. * flag is set, it's telling us that as soon as Gadget queues the
  124. * required request, we should kick the transfer here because the
  125. * IRQ we were waiting for is long gone.
  126. */
  127. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  128. struct dwc3 *dwc = dep->dwc;
  129. unsigned direction;
  130. u32 type;
  131. direction = !!(dep->flags & DWC3_EP0_DIR_IN);
  132. if (dwc->ep0state == EP0_STATUS_PHASE) {
  133. type = dwc->three_stage_setup
  134. ? DWC3_TRBCTL_CONTROL_STATUS3
  135. : DWC3_TRBCTL_CONTROL_STATUS2;
  136. } else if (dwc->ep0state == EP0_DATA_PHASE) {
  137. type = DWC3_TRBCTL_CONTROL_DATA;
  138. } else {
  139. /* should never happen */
  140. WARN_ON(1);
  141. return 0;
  142. }
  143. ret = dwc3_ep0_start_trans(dwc, direction,
  144. req->request.dma, req->request.length, type);
  145. dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
  146. DWC3_EP0_DIR_IN);
  147. }
  148. return ret;
  149. }
  150. int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
  151. gfp_t gfp_flags)
  152. {
  153. struct dwc3_request *req = to_dwc3_request(request);
  154. struct dwc3_ep *dep = to_dwc3_ep(ep);
  155. struct dwc3 *dwc = dep->dwc;
  156. unsigned long flags;
  157. int ret;
  158. spin_lock_irqsave(&dwc->lock, flags);
  159. if (!dep->desc) {
  160. dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
  161. request, dep->name);
  162. ret = -ESHUTDOWN;
  163. goto out;
  164. }
  165. /* we share one TRB for ep0/1 */
  166. if (!list_empty(&dwc->eps[0]->request_list) ||
  167. !list_empty(&dwc->eps[1]->request_list) ||
  168. dwc->ep0_status_pending) {
  169. ret = -EBUSY;
  170. goto out;
  171. }
  172. dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
  173. request, dep->name, request->length,
  174. dwc3_ep0_state_string(dwc->ep0state));
  175. ret = __dwc3_gadget_ep0_queue(dep, req);
  176. out:
  177. spin_unlock_irqrestore(&dwc->lock, flags);
  178. return ret;
  179. }
  180. static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
  181. {
  182. /* stall is always issued on EP0 */
  183. __dwc3_gadget_ep_set_halt(dwc->eps[0], 1);
  184. dwc->eps[0]->flags = DWC3_EP_ENABLED;
  185. dwc->ep0state = EP0_SETUP_PHASE;
  186. dwc3_ep0_out_start(dwc);
  187. }
  188. void dwc3_ep0_out_start(struct dwc3 *dwc)
  189. {
  190. int ret;
  191. ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
  192. DWC3_TRBCTL_CONTROL_SETUP);
  193. WARN_ON(ret < 0);
  194. }
  195. static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
  196. {
  197. struct dwc3_ep *dep;
  198. u32 windex = le16_to_cpu(wIndex_le);
  199. u32 epnum;
  200. epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
  201. if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  202. epnum |= 1;
  203. dep = dwc->eps[epnum];
  204. if (dep->flags & DWC3_EP_ENABLED)
  205. return dep;
  206. return NULL;
  207. }
  208. static void dwc3_ep0_send_status_response(struct dwc3 *dwc)
  209. {
  210. dwc3_ep0_start_trans(dwc, 1, dwc->setup_buf_addr,
  211. dwc->ep0_usb_req.length,
  212. DWC3_TRBCTL_CONTROL_DATA);
  213. }
  214. /*
  215. * ch 9.4.5
  216. */
  217. static int dwc3_ep0_handle_status(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  218. {
  219. struct dwc3_ep *dep;
  220. u32 recip;
  221. u16 usb_status = 0;
  222. __le16 *response_pkt;
  223. recip = ctrl->bRequestType & USB_RECIP_MASK;
  224. switch (recip) {
  225. case USB_RECIP_DEVICE:
  226. /*
  227. * We are self-powered. U1/U2/LTM will be set later
  228. * once we handle this states. RemoteWakeup is 0 on SS
  229. */
  230. usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
  231. break;
  232. case USB_RECIP_INTERFACE:
  233. /*
  234. * Function Remote Wake Capable D0
  235. * Function Remote Wakeup D1
  236. */
  237. break;
  238. case USB_RECIP_ENDPOINT:
  239. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  240. if (!dep)
  241. return -EINVAL;
  242. if (dep->flags & DWC3_EP_STALL)
  243. usb_status = 1 << USB_ENDPOINT_HALT;
  244. break;
  245. default:
  246. return -EINVAL;
  247. };
  248. response_pkt = (__le16 *) dwc->setup_buf;
  249. *response_pkt = cpu_to_le16(usb_status);
  250. dwc->ep0_usb_req.length = sizeof(*response_pkt);
  251. dwc->ep0_status_pending = 1;
  252. return 0;
  253. }
  254. static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
  255. struct usb_ctrlrequest *ctrl, int set)
  256. {
  257. struct dwc3_ep *dep;
  258. u32 recip;
  259. u32 wValue;
  260. u32 wIndex;
  261. u32 reg;
  262. int ret;
  263. u32 mode;
  264. wValue = le16_to_cpu(ctrl->wValue);
  265. wIndex = le16_to_cpu(ctrl->wIndex);
  266. recip = ctrl->bRequestType & USB_RECIP_MASK;
  267. switch (recip) {
  268. case USB_RECIP_DEVICE:
  269. /*
  270. * 9.4.1 says only only for SS, in AddressState only for
  271. * default control pipe
  272. */
  273. switch (wValue) {
  274. case USB_DEVICE_U1_ENABLE:
  275. case USB_DEVICE_U2_ENABLE:
  276. case USB_DEVICE_LTM_ENABLE:
  277. if (dwc->dev_state != DWC3_CONFIGURED_STATE)
  278. return -EINVAL;
  279. if (dwc->speed != DWC3_DSTS_SUPERSPEED)
  280. return -EINVAL;
  281. }
  282. /* XXX add U[12] & LTM */
  283. switch (wValue) {
  284. case USB_DEVICE_REMOTE_WAKEUP:
  285. break;
  286. case USB_DEVICE_U1_ENABLE:
  287. break;
  288. case USB_DEVICE_U2_ENABLE:
  289. break;
  290. case USB_DEVICE_LTM_ENABLE:
  291. break;
  292. case USB_DEVICE_TEST_MODE:
  293. if ((wIndex & 0xff) != 0)
  294. return -EINVAL;
  295. if (!set)
  296. return -EINVAL;
  297. mode = wIndex >> 8;
  298. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  299. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  300. switch (mode) {
  301. case TEST_J:
  302. case TEST_K:
  303. case TEST_SE0_NAK:
  304. case TEST_PACKET:
  305. case TEST_FORCE_EN:
  306. reg |= mode << 1;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. break;
  317. case USB_RECIP_INTERFACE:
  318. switch (wValue) {
  319. case USB_INTRF_FUNC_SUSPEND:
  320. if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
  321. /* XXX enable Low power suspend */
  322. ;
  323. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
  324. /* XXX enable remote wakeup */
  325. ;
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. break;
  331. case USB_RECIP_ENDPOINT:
  332. switch (wValue) {
  333. case USB_ENDPOINT_HALT:
  334. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  335. if (!dep)
  336. return -EINVAL;
  337. ret = __dwc3_gadget_ep_set_halt(dep, set);
  338. if (ret)
  339. return -EINVAL;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. break;
  345. default:
  346. return -EINVAL;
  347. };
  348. return 0;
  349. }
  350. static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  351. {
  352. int ret = 0;
  353. u32 addr;
  354. u32 reg;
  355. addr = le16_to_cpu(ctrl->wValue);
  356. if (addr > 127)
  357. return -EINVAL;
  358. switch (dwc->dev_state) {
  359. case DWC3_DEFAULT_STATE:
  360. case DWC3_ADDRESS_STATE:
  361. /*
  362. * Not sure if we should program DevAddr now or later
  363. */
  364. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  365. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  366. reg |= DWC3_DCFG_DEVADDR(addr);
  367. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  368. if (addr)
  369. dwc->dev_state = DWC3_ADDRESS_STATE;
  370. else
  371. dwc->dev_state = DWC3_DEFAULT_STATE;
  372. break;
  373. case DWC3_CONFIGURED_STATE:
  374. ret = -EINVAL;
  375. break;
  376. }
  377. return ret;
  378. }
  379. static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  380. {
  381. int ret;
  382. spin_unlock(&dwc->lock);
  383. ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
  384. spin_lock(&dwc->lock);
  385. return ret;
  386. }
  387. static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  388. {
  389. u32 cfg;
  390. int ret;
  391. cfg = le16_to_cpu(ctrl->wValue);
  392. switch (dwc->dev_state) {
  393. case DWC3_DEFAULT_STATE:
  394. return -EINVAL;
  395. break;
  396. case DWC3_ADDRESS_STATE:
  397. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  398. /* if the cfg matches and the cfg is non zero */
  399. if (!ret && cfg)
  400. dwc->dev_state = DWC3_CONFIGURED_STATE;
  401. break;
  402. case DWC3_CONFIGURED_STATE:
  403. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  404. if (!cfg)
  405. dwc->dev_state = DWC3_ADDRESS_STATE;
  406. break;
  407. }
  408. return 0;
  409. }
  410. static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  411. {
  412. int ret;
  413. switch (ctrl->bRequest) {
  414. case USB_REQ_GET_STATUS:
  415. dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
  416. ret = dwc3_ep0_handle_status(dwc, ctrl);
  417. break;
  418. case USB_REQ_CLEAR_FEATURE:
  419. dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
  420. ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
  421. break;
  422. case USB_REQ_SET_FEATURE:
  423. dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
  424. ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
  425. break;
  426. case USB_REQ_SET_ADDRESS:
  427. dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
  428. ret = dwc3_ep0_set_address(dwc, ctrl);
  429. break;
  430. case USB_REQ_SET_CONFIGURATION:
  431. dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
  432. ret = dwc3_ep0_set_config(dwc, ctrl);
  433. break;
  434. default:
  435. dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
  436. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  437. break;
  438. };
  439. return ret;
  440. }
  441. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  442. const struct dwc3_event_depevt *event)
  443. {
  444. struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
  445. int ret;
  446. u32 len;
  447. if (!dwc->gadget_driver)
  448. goto err;
  449. len = le16_to_cpu(ctrl->wLength);
  450. if (!len) {
  451. dwc->three_stage_setup = 0;
  452. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  453. } else {
  454. dwc->three_stage_setup = 1;
  455. dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
  456. }
  457. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  458. ret = dwc3_ep0_std_request(dwc, ctrl);
  459. else
  460. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  461. if (ret >= 0)
  462. return;
  463. err:
  464. dwc3_ep0_stall_and_restart(dwc);
  465. }
  466. static void dwc3_ep0_complete_data(struct dwc3 *dwc,
  467. const struct dwc3_event_depevt *event)
  468. {
  469. struct dwc3_request *r = NULL;
  470. struct usb_request *ur;
  471. struct dwc3_trb trb;
  472. struct dwc3_ep *dep;
  473. u32 transferred;
  474. u8 epnum;
  475. epnum = event->endpoint_number;
  476. dep = dwc->eps[epnum];
  477. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  478. if (!dwc->ep0_status_pending) {
  479. r = next_request(&dwc->eps[0]->request_list);
  480. ur = &r->request;
  481. } else {
  482. ur = &dwc->ep0_usb_req;
  483. dwc->ep0_status_pending = 0;
  484. }
  485. dwc3_trb_to_nat(dwc->ep0_trb, &trb);
  486. if (dwc->ep0_bounced) {
  487. struct dwc3_ep *ep0 = dwc->eps[0];
  488. transferred = min_t(u32, ur->length,
  489. ep0->endpoint.maxpacket - trb.length);
  490. memcpy(ur->buf, dwc->ep0_bounce, transferred);
  491. dwc->ep0_bounced = false;
  492. } else {
  493. transferred = ur->length - trb.length;
  494. ur->actual += transferred;
  495. }
  496. if ((epnum & 1) && ur->actual < ur->length) {
  497. /* for some reason we did not get everything out */
  498. dwc3_ep0_stall_and_restart(dwc);
  499. dwc3_gadget_giveback(dep, r, -ECONNRESET);
  500. } else {
  501. /*
  502. * handle the case where we have to send a zero packet. This
  503. * seems to be case when req.length > maxpacket. Could it be?
  504. */
  505. if (r)
  506. dwc3_gadget_giveback(dep, r, 0);
  507. }
  508. }
  509. static void dwc3_ep0_complete_req(struct dwc3 *dwc,
  510. const struct dwc3_event_depevt *event)
  511. {
  512. struct dwc3_request *r;
  513. struct dwc3_ep *dep;
  514. dep = dwc->eps[0];
  515. if (!list_empty(&dep->request_list)) {
  516. r = next_request(&dep->request_list);
  517. dwc3_gadget_giveback(dep, r, 0);
  518. }
  519. dwc->ep0state = EP0_SETUP_PHASE;
  520. dwc3_ep0_out_start(dwc);
  521. }
  522. static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
  523. const struct dwc3_event_depevt *event)
  524. {
  525. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  526. dep->flags &= ~DWC3_EP_BUSY;
  527. switch (dwc->ep0state) {
  528. case EP0_SETUP_PHASE:
  529. dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
  530. dwc3_ep0_inspect_setup(dwc, event);
  531. break;
  532. case EP0_DATA_PHASE:
  533. dev_vdbg(dwc->dev, "Data Phase\n");
  534. dwc3_ep0_complete_data(dwc, event);
  535. break;
  536. case EP0_STATUS_PHASE:
  537. dev_vdbg(dwc->dev, "Status Phase\n");
  538. dwc3_ep0_complete_req(dwc, event);
  539. break;
  540. default:
  541. WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
  542. }
  543. }
  544. static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
  545. const struct dwc3_event_depevt *event)
  546. {
  547. dwc->ep0state = EP0_SETUP_PHASE;
  548. dwc3_ep0_out_start(dwc);
  549. }
  550. static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
  551. const struct dwc3_event_depevt *event)
  552. {
  553. struct dwc3_ep *dep;
  554. struct dwc3_request *req;
  555. int ret;
  556. dep = dwc->eps[0];
  557. dwc->ep0state = EP0_DATA_PHASE;
  558. if (dwc->ep0_status_pending) {
  559. dwc3_ep0_send_status_response(dwc);
  560. return;
  561. }
  562. if (list_empty(&dep->request_list)) {
  563. dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
  564. dep->flags |= DWC3_EP_PENDING_REQUEST;
  565. if (event->endpoint_number)
  566. dep->flags |= DWC3_EP0_DIR_IN;
  567. return;
  568. }
  569. req = next_request(&dep->request_list);
  570. req->direction = !!event->endpoint_number;
  571. dwc->ep0state = EP0_DATA_PHASE;
  572. if (req->request.length == 0) {
  573. ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
  574. dwc->ctrl_req_addr, 0,
  575. DWC3_TRBCTL_CONTROL_DATA);
  576. } else if ((req->request.length % dep->endpoint.maxpacket)
  577. && (event->endpoint_number == 0)) {
  578. dwc3_map_buffer_to_dma(req);
  579. WARN_ON(req->request.length > dep->endpoint.maxpacket);
  580. dwc->ep0_bounced = true;
  581. /*
  582. * REVISIT in case request length is bigger than EP0
  583. * wMaxPacketSize, we will need two chained TRBs to handle
  584. * the transfer.
  585. */
  586. ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
  587. dwc->ep0_bounce_addr, dep->endpoint.maxpacket,
  588. DWC3_TRBCTL_CONTROL_DATA);
  589. } else {
  590. dwc3_map_buffer_to_dma(req);
  591. ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
  592. req->request.dma, req->request.length,
  593. DWC3_TRBCTL_CONTROL_DATA);
  594. }
  595. WARN_ON(ret < 0);
  596. }
  597. static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
  598. const struct dwc3_event_depevt *event)
  599. {
  600. u32 type;
  601. int ret;
  602. dwc->ep0state = EP0_STATUS_PHASE;
  603. type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
  604. : DWC3_TRBCTL_CONTROL_STATUS2;
  605. ret = dwc3_ep0_start_trans(dwc, event->endpoint_number,
  606. dwc->ctrl_req_addr, 0, type);
  607. WARN_ON(ret < 0);
  608. }
  609. static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
  610. const struct dwc3_event_depevt *event)
  611. {
  612. switch (event->status) {
  613. case DEPEVT_STATUS_CONTROL_SETUP:
  614. dev_vdbg(dwc->dev, "Control Setup\n");
  615. dwc3_ep0_do_control_setup(dwc, event);
  616. break;
  617. case DEPEVT_STATUS_CONTROL_DATA:
  618. dev_vdbg(dwc->dev, "Control Data\n");
  619. if (dwc->ep0_next_event != DWC3_EP0_NRDY_DATA) {
  620. dev_vdbg(dwc->dev, "Expected %d got %d\n",
  621. DEPEVT_STATUS_CONTROL_DATA,
  622. event->status);
  623. dwc3_ep0_stall_and_restart(dwc);
  624. return;
  625. }
  626. dwc3_ep0_do_control_data(dwc, event);
  627. break;
  628. case DEPEVT_STATUS_CONTROL_STATUS:
  629. dev_vdbg(dwc->dev, "Control Status\n");
  630. if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS) {
  631. dev_vdbg(dwc->dev, "Expected %d got %d\n",
  632. DEPEVT_STATUS_CONTROL_STATUS,
  633. event->status);
  634. dwc3_ep0_stall_and_restart(dwc);
  635. return;
  636. }
  637. dwc3_ep0_do_control_status(dwc, event);
  638. }
  639. }
  640. void dwc3_ep0_interrupt(struct dwc3 *dwc,
  641. const const struct dwc3_event_depevt *event)
  642. {
  643. u8 epnum = event->endpoint_number;
  644. dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
  645. dwc3_ep_event_string(event->endpoint_event),
  646. epnum, (epnum & 1) ? "in" : "out",
  647. dwc3_ep0_state_string(dwc->ep0state));
  648. switch (event->endpoint_event) {
  649. case DWC3_DEPEVT_XFERCOMPLETE:
  650. dwc3_ep0_xfer_complete(dwc, event);
  651. break;
  652. case DWC3_DEPEVT_XFERNOTREADY:
  653. dwc3_ep0_xfernotready(dwc, event);
  654. break;
  655. case DWC3_DEPEVT_XFERINPROGRESS:
  656. case DWC3_DEPEVT_RXTXFIFOEVT:
  657. case DWC3_DEPEVT_STREAMEVT:
  658. case DWC3_DEPEVT_EPCMDCMPLT:
  659. break;
  660. }
  661. }