ep0.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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);
  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. void dwc3_ep0_out_start(struct dwc3 *dwc)
  232. {
  233. int ret;
  234. ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
  235. DWC3_TRBCTL_CONTROL_SETUP);
  236. WARN_ON(ret < 0);
  237. }
  238. static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
  239. {
  240. struct dwc3_ep *dep;
  241. u32 windex = le16_to_cpu(wIndex_le);
  242. u32 epnum;
  243. epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
  244. if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  245. epnum |= 1;
  246. dep = dwc->eps[epnum];
  247. if (dep->flags & DWC3_EP_ENABLED)
  248. return dep;
  249. return NULL;
  250. }
  251. static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
  252. {
  253. }
  254. /*
  255. * ch 9.4.5
  256. */
  257. static int dwc3_ep0_handle_status(struct dwc3 *dwc,
  258. struct usb_ctrlrequest *ctrl)
  259. {
  260. struct dwc3_ep *dep;
  261. u32 recip;
  262. u32 reg;
  263. u16 usb_status = 0;
  264. __le16 *response_pkt;
  265. recip = ctrl->bRequestType & USB_RECIP_MASK;
  266. switch (recip) {
  267. case USB_RECIP_DEVICE:
  268. /*
  269. * LTM will be set once we know how to set this in HW.
  270. */
  271. usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
  272. if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
  273. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  274. if (reg & DWC3_DCTL_INITU1ENA)
  275. usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
  276. if (reg & DWC3_DCTL_INITU2ENA)
  277. usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
  278. }
  279. break;
  280. case USB_RECIP_INTERFACE:
  281. /*
  282. * Function Remote Wake Capable D0
  283. * Function Remote Wakeup D1
  284. */
  285. break;
  286. case USB_RECIP_ENDPOINT:
  287. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  288. if (!dep)
  289. return -EINVAL;
  290. if (dep->flags & DWC3_EP_STALL)
  291. usb_status = 1 << USB_ENDPOINT_HALT;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. response_pkt = (__le16 *) dwc->setup_buf;
  297. *response_pkt = cpu_to_le16(usb_status);
  298. dep = dwc->eps[0];
  299. dwc->ep0_usb_req.dep = dep;
  300. dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
  301. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  302. dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
  303. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  304. }
  305. static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
  306. struct usb_ctrlrequest *ctrl, int set)
  307. {
  308. struct dwc3_ep *dep;
  309. u32 recip;
  310. u32 wValue;
  311. u32 wIndex;
  312. u32 reg;
  313. int ret;
  314. enum usb_device_state state;
  315. wValue = le16_to_cpu(ctrl->wValue);
  316. wIndex = le16_to_cpu(ctrl->wIndex);
  317. recip = ctrl->bRequestType & USB_RECIP_MASK;
  318. state = dwc->gadget.state;
  319. switch (recip) {
  320. case USB_RECIP_DEVICE:
  321. switch (wValue) {
  322. case USB_DEVICE_REMOTE_WAKEUP:
  323. break;
  324. /*
  325. * 9.4.1 says only only for SS, in AddressState only for
  326. * default control pipe
  327. */
  328. case USB_DEVICE_U1_ENABLE:
  329. if (state != USB_STATE_CONFIGURED)
  330. return -EINVAL;
  331. if (dwc->speed != DWC3_DSTS_SUPERSPEED)
  332. return -EINVAL;
  333. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  334. if (set)
  335. reg |= DWC3_DCTL_INITU1ENA;
  336. else
  337. reg &= ~DWC3_DCTL_INITU1ENA;
  338. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  339. break;
  340. case USB_DEVICE_U2_ENABLE:
  341. if (state != USB_STATE_CONFIGURED)
  342. return -EINVAL;
  343. if (dwc->speed != DWC3_DSTS_SUPERSPEED)
  344. return -EINVAL;
  345. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  346. if (set)
  347. reg |= DWC3_DCTL_INITU2ENA;
  348. else
  349. reg &= ~DWC3_DCTL_INITU2ENA;
  350. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  351. break;
  352. case USB_DEVICE_LTM_ENABLE:
  353. return -EINVAL;
  354. break;
  355. case USB_DEVICE_TEST_MODE:
  356. if ((wIndex & 0xff) != 0)
  357. return -EINVAL;
  358. if (!set)
  359. return -EINVAL;
  360. dwc->test_mode_nr = wIndex >> 8;
  361. dwc->test_mode = true;
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. break;
  367. case USB_RECIP_INTERFACE:
  368. switch (wValue) {
  369. case USB_INTRF_FUNC_SUSPEND:
  370. if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
  371. /* XXX enable Low power suspend */
  372. ;
  373. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
  374. /* XXX enable remote wakeup */
  375. ;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. break;
  381. case USB_RECIP_ENDPOINT:
  382. switch (wValue) {
  383. case USB_ENDPOINT_HALT:
  384. dep = dwc3_wIndex_to_dep(dwc, wIndex);
  385. if (!dep)
  386. return -EINVAL;
  387. if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
  388. break;
  389. ret = __dwc3_gadget_ep_set_halt(dep, set);
  390. if (ret)
  391. return -EINVAL;
  392. break;
  393. default:
  394. return -EINVAL;
  395. }
  396. break;
  397. default:
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  403. {
  404. enum usb_device_state state = dwc->gadget.state;
  405. u32 addr;
  406. u32 reg;
  407. addr = le16_to_cpu(ctrl->wValue);
  408. if (addr > 127) {
  409. dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
  410. return -EINVAL;
  411. }
  412. if (state == USB_STATE_CONFIGURED) {
  413. dwc3_trace(trace_dwc3_ep0,
  414. "trying to set address when configured");
  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. usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
  423. else
  424. usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
  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. enum usb_device_state state = dwc->gadget.state;
  438. u32 cfg;
  439. int ret;
  440. u32 reg;
  441. dwc->start_config_issued = false;
  442. cfg = le16_to_cpu(ctrl->wValue);
  443. switch (state) {
  444. case USB_STATE_DEFAULT:
  445. return -EINVAL;
  446. break;
  447. case USB_STATE_ADDRESS:
  448. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  449. /* if the cfg matches and the cfg is non zero */
  450. if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
  451. /*
  452. * only change state if set_config has already
  453. * been processed. If gadget driver returns
  454. * USB_GADGET_DELAYED_STATUS, we will wait
  455. * to change the state on the next usb_ep_queue()
  456. */
  457. if (ret == 0)
  458. usb_gadget_set_state(&dwc->gadget,
  459. USB_STATE_CONFIGURED);
  460. /*
  461. * Enable transition to U1/U2 state when
  462. * nothing is pending from application.
  463. */
  464. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  465. reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
  466. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  467. dwc->resize_fifos = true;
  468. dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET");
  469. }
  470. break;
  471. case USB_STATE_CONFIGURED:
  472. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  473. if (!cfg && !ret)
  474. usb_gadget_set_state(&dwc->gadget,
  475. USB_STATE_ADDRESS);
  476. break;
  477. default:
  478. ret = -EINVAL;
  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. enum usb_device_state state = dwc->gadget.state;
  521. u16 wLength;
  522. u16 wValue;
  523. if (state == USB_STATE_DEFAULT)
  524. return -EINVAL;
  525. wValue = le16_to_cpu(ctrl->wValue);
  526. wLength = le16_to_cpu(ctrl->wLength);
  527. if (wLength != 6) {
  528. dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
  529. wLength);
  530. return -EINVAL;
  531. }
  532. /*
  533. * To handle Set SEL we need to receive 6 bytes from Host. So let's
  534. * queue a usb_request for 6 bytes.
  535. *
  536. * Remember, though, this controller can't handle non-wMaxPacketSize
  537. * aligned transfers on the OUT direction, so we queue a request for
  538. * wMaxPacketSize instead.
  539. */
  540. dep = dwc->eps[0];
  541. dwc->ep0_usb_req.dep = dep;
  542. dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
  543. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  544. dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
  545. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  546. }
  547. static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  548. {
  549. u16 wLength;
  550. u16 wValue;
  551. u16 wIndex;
  552. wValue = le16_to_cpu(ctrl->wValue);
  553. wLength = le16_to_cpu(ctrl->wLength);
  554. wIndex = le16_to_cpu(ctrl->wIndex);
  555. if (wIndex || wLength)
  556. return -EINVAL;
  557. /*
  558. * REVISIT It's unclear from Databook what to do with this
  559. * value. For now, just cache it.
  560. */
  561. dwc->isoch_delay = wValue;
  562. return 0;
  563. }
  564. static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  565. {
  566. int ret;
  567. switch (ctrl->bRequest) {
  568. case USB_REQ_GET_STATUS:
  569. dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS\n");
  570. ret = dwc3_ep0_handle_status(dwc, ctrl);
  571. break;
  572. case USB_REQ_CLEAR_FEATURE:
  573. dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE\n");
  574. ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
  575. break;
  576. case USB_REQ_SET_FEATURE:
  577. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE\n");
  578. ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
  579. break;
  580. case USB_REQ_SET_ADDRESS:
  581. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS\n");
  582. ret = dwc3_ep0_set_address(dwc, ctrl);
  583. break;
  584. case USB_REQ_SET_CONFIGURATION:
  585. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION\n");
  586. ret = dwc3_ep0_set_config(dwc, ctrl);
  587. break;
  588. case USB_REQ_SET_SEL:
  589. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL\n");
  590. ret = dwc3_ep0_set_sel(dwc, ctrl);
  591. break;
  592. case USB_REQ_SET_ISOCH_DELAY:
  593. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY\n");
  594. ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
  595. break;
  596. default:
  597. dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver\n");
  598. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  599. break;
  600. }
  601. return ret;
  602. }
  603. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  604. const struct dwc3_event_depevt *event)
  605. {
  606. struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
  607. int ret = -EINVAL;
  608. u32 len;
  609. if (!dwc->gadget_driver)
  610. goto out;
  611. trace_dwc3_ctrl_req(ctrl);
  612. len = le16_to_cpu(ctrl->wLength);
  613. if (!len) {
  614. dwc->three_stage_setup = false;
  615. dwc->ep0_expect_in = false;
  616. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  617. } else {
  618. dwc->three_stage_setup = true;
  619. dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
  620. dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
  621. }
  622. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  623. ret = dwc3_ep0_std_request(dwc, ctrl);
  624. else
  625. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  626. if (ret == USB_GADGET_DELAYED_STATUS)
  627. dwc->delayed_status = true;
  628. out:
  629. if (ret < 0)
  630. dwc3_ep0_stall_and_restart(dwc);
  631. }
  632. static void dwc3_ep0_complete_data(struct dwc3 *dwc,
  633. const struct dwc3_event_depevt *event)
  634. {
  635. struct dwc3_request *r = NULL;
  636. struct usb_request *ur;
  637. struct dwc3_trb *trb;
  638. struct dwc3_ep *ep0;
  639. u32 transferred;
  640. u32 status;
  641. u32 length;
  642. u8 epnum;
  643. epnum = event->endpoint_number;
  644. ep0 = dwc->eps[0];
  645. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  646. r = next_request(&ep0->request_list);
  647. ur = &r->request;
  648. trb = dwc->ep0_trb;
  649. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  650. if (status == DWC3_TRBSTS_SETUP_PENDING) {
  651. dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
  652. if (r)
  653. dwc3_gadget_giveback(ep0, r, -ECONNRESET);
  654. return;
  655. }
  656. length = trb->size & DWC3_TRB_SIZE_MASK;
  657. if (dwc->ep0_bounced) {
  658. unsigned transfer_size = ur->length;
  659. unsigned maxp = ep0->endpoint.maxpacket;
  660. transfer_size += (maxp - (transfer_size % maxp));
  661. transferred = min_t(u32, ur->length,
  662. transfer_size - length);
  663. memcpy(ur->buf, dwc->ep0_bounce, transferred);
  664. } else {
  665. transferred = ur->length - length;
  666. }
  667. ur->actual += transferred;
  668. if ((epnum & 1) && ur->actual < ur->length) {
  669. /* for some reason we did not get everything out */
  670. dwc3_ep0_stall_and_restart(dwc);
  671. } else {
  672. /*
  673. * handle the case where we have to send a zero packet. This
  674. * seems to be case when req.length > maxpacket. Could it be?
  675. */
  676. if (r)
  677. dwc3_gadget_giveback(ep0, r, 0);
  678. }
  679. }
  680. static void dwc3_ep0_complete_status(struct dwc3 *dwc,
  681. const struct dwc3_event_depevt *event)
  682. {
  683. struct dwc3_request *r;
  684. struct dwc3_ep *dep;
  685. struct dwc3_trb *trb;
  686. u32 status;
  687. dep = dwc->eps[0];
  688. trb = dwc->ep0_trb;
  689. if (!list_empty(&dep->request_list)) {
  690. r = next_request(&dep->request_list);
  691. dwc3_gadget_giveback(dep, r, 0);
  692. }
  693. if (dwc->test_mode) {
  694. int ret;
  695. ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
  696. if (ret < 0) {
  697. dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
  698. dwc->test_mode_nr);
  699. dwc3_ep0_stall_and_restart(dwc);
  700. return;
  701. }
  702. }
  703. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  704. if (status == DWC3_TRBSTS_SETUP_PENDING)
  705. dwc3_trace(trace_dwc3_ep0, "Setup Pending received\n");
  706. dwc->ep0state = EP0_SETUP_PHASE;
  707. dwc3_ep0_out_start(dwc);
  708. }
  709. static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
  710. const struct dwc3_event_depevt *event)
  711. {
  712. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  713. dep->flags &= ~DWC3_EP_BUSY;
  714. dep->resource_index = 0;
  715. dwc->setup_packet_pending = false;
  716. switch (dwc->ep0state) {
  717. case EP0_SETUP_PHASE:
  718. dwc3_trace(trace_dwc3_ep0, "Setup Phase");
  719. dwc3_ep0_inspect_setup(dwc, event);
  720. break;
  721. case EP0_DATA_PHASE:
  722. dwc3_trace(trace_dwc3_ep0, "Data Phase");
  723. dwc3_ep0_complete_data(dwc, event);
  724. break;
  725. case EP0_STATUS_PHASE:
  726. dwc3_trace(trace_dwc3_ep0, "Status Phase");
  727. dwc3_ep0_complete_status(dwc, event);
  728. break;
  729. default:
  730. WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
  731. }
  732. }
  733. static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
  734. struct dwc3_ep *dep, struct dwc3_request *req)
  735. {
  736. int ret;
  737. req->direction = !!dep->number;
  738. if (req->request.length == 0) {
  739. ret = dwc3_ep0_start_trans(dwc, dep->number,
  740. dwc->ctrl_req_addr, 0,
  741. DWC3_TRBCTL_CONTROL_DATA);
  742. } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
  743. && (dep->number == 0)) {
  744. u32 transfer_size;
  745. u32 maxpacket;
  746. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  747. dep->number);
  748. if (ret) {
  749. dev_dbg(dwc->dev, "failed to map request\n");
  750. return;
  751. }
  752. WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
  753. maxpacket = dep->endpoint.maxpacket;
  754. transfer_size = roundup(req->request.length, maxpacket);
  755. dwc->ep0_bounced = true;
  756. /*
  757. * REVISIT in case request length is bigger than
  758. * DWC3_EP0_BOUNCE_SIZE we will need two chained
  759. * TRBs to handle the transfer.
  760. */
  761. ret = dwc3_ep0_start_trans(dwc, dep->number,
  762. dwc->ep0_bounce_addr, transfer_size,
  763. DWC3_TRBCTL_CONTROL_DATA);
  764. } else {
  765. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  766. dep->number);
  767. if (ret) {
  768. dev_dbg(dwc->dev, "failed to map request\n");
  769. return;
  770. }
  771. ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
  772. req->request.length, DWC3_TRBCTL_CONTROL_DATA);
  773. }
  774. WARN_ON(ret < 0);
  775. }
  776. static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
  777. {
  778. struct dwc3 *dwc = dep->dwc;
  779. u32 type;
  780. type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
  781. : DWC3_TRBCTL_CONTROL_STATUS2;
  782. return dwc3_ep0_start_trans(dwc, dep->number,
  783. dwc->ctrl_req_addr, 0, type);
  784. }
  785. static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
  786. {
  787. if (dwc->resize_fifos) {
  788. dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs");
  789. dwc3_gadget_resize_tx_fifos(dwc);
  790. dwc->resize_fifos = 0;
  791. }
  792. WARN_ON(dwc3_ep0_start_control_status(dep));
  793. }
  794. static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
  795. const struct dwc3_event_depevt *event)
  796. {
  797. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  798. __dwc3_ep0_do_control_status(dwc, dep);
  799. }
  800. static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
  801. {
  802. struct dwc3_gadget_ep_cmd_params params;
  803. u32 cmd;
  804. int ret;
  805. if (!dep->resource_index)
  806. return;
  807. cmd = DWC3_DEPCMD_ENDTRANSFER;
  808. cmd |= DWC3_DEPCMD_CMDIOC;
  809. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  810. memset(&params, 0, sizeof(params));
  811. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  812. WARN_ON_ONCE(ret);
  813. dep->resource_index = 0;
  814. }
  815. static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
  816. const struct dwc3_event_depevt *event)
  817. {
  818. dwc->setup_packet_pending = true;
  819. switch (event->status) {
  820. case DEPEVT_STATUS_CONTROL_DATA:
  821. dwc3_trace(trace_dwc3_ep0, "Control Data");
  822. /*
  823. * We already have a DATA transfer in the controller's cache,
  824. * if we receive a XferNotReady(DATA) we will ignore it, unless
  825. * it's for the wrong direction.
  826. *
  827. * In that case, we must issue END_TRANSFER command to the Data
  828. * Phase we already have started and issue SetStall on the
  829. * control endpoint.
  830. */
  831. if (dwc->ep0_expect_in != event->endpoint_number) {
  832. struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
  833. dwc3_trace(trace_dwc3_ep0,
  834. "Wrong direction for Data phase");
  835. dwc3_ep0_end_control_data(dwc, dep);
  836. dwc3_ep0_stall_and_restart(dwc);
  837. return;
  838. }
  839. break;
  840. case DEPEVT_STATUS_CONTROL_STATUS:
  841. if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
  842. return;
  843. dwc3_trace(trace_dwc3_ep0, "Control Status");
  844. dwc->ep0state = EP0_STATUS_PHASE;
  845. if (dwc->delayed_status) {
  846. WARN_ON_ONCE(event->endpoint_number != 1);
  847. dwc3_trace(trace_dwc3_ep0, "Delayed Status");
  848. return;
  849. }
  850. dwc3_ep0_do_control_status(dwc, event);
  851. }
  852. }
  853. void dwc3_ep0_interrupt(struct dwc3 *dwc,
  854. const struct dwc3_event_depevt *event)
  855. {
  856. u8 epnum = event->endpoint_number;
  857. dwc3_trace(trace_dwc3_ep0, "%s while ep%d%s in state '%s'",
  858. dwc3_ep_event_string(event->endpoint_event),
  859. epnum >> 1, (epnum & 1) ? "in" : "out",
  860. dwc3_ep0_state_string(dwc->ep0state));
  861. switch (event->endpoint_event) {
  862. case DWC3_DEPEVT_XFERCOMPLETE:
  863. dwc3_ep0_xfer_complete(dwc, event);
  864. break;
  865. case DWC3_DEPEVT_XFERNOTREADY:
  866. dwc3_ep0_xfernotready(dwc, event);
  867. break;
  868. case DWC3_DEPEVT_XFERINPROGRESS:
  869. case DWC3_DEPEVT_RXTXFIFOEVT:
  870. case DWC3_DEPEVT_STREAMEVT:
  871. case DWC3_DEPEVT_EPCMDCMPLT:
  872. break;
  873. }
  874. }