ep0.c 27 KB

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