ep0.c 27 KB

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