ep0.c 26 KB

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