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. (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
  293. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  294. if (reg & DWC3_DCTL_INITU1ENA)
  295. usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
  296. if (reg & DWC3_DCTL_INITU2ENA)
  297. usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
  298. }
  299. break;
  300. case USB_RECIP_INTERFACE:
  301. /*
  302. * Function Remote Wake Capable D0
  303. * Function Remote Wakeup D1
  304. */
  305. break;
  306. case USB_RECIP_ENDPOINT:
  307. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  308. if (!dep)
  309. return -EINVAL;
  310. if (dep->flags & DWC3_EP_STALL)
  311. usb_status = 1 << USB_ENDPOINT_HALT;
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. response_pkt = (__le16 *) dwc->setup_buf;
  317. *response_pkt = cpu_to_le16(usb_status);
  318. dep = dwc->eps[0];
  319. dwc->ep0_usb_req.dep = dep;
  320. dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
  321. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  322. dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
  323. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  324. }
  325. static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
  326. struct usb_ctrlrequest *ctrl, int set)
  327. {
  328. struct dwc3_ep *dep;
  329. u32 recip;
  330. u32 wValue;
  331. u32 wIndex;
  332. u32 reg;
  333. int ret;
  334. enum usb_device_state state;
  335. wValue = le16_to_cpu(ctrl->wValue);
  336. wIndex = le16_to_cpu(ctrl->wIndex);
  337. recip = ctrl->bRequestType & USB_RECIP_MASK;
  338. state = dwc->gadget.state;
  339. switch (recip) {
  340. case USB_RECIP_DEVICE:
  341. switch (wValue) {
  342. case USB_DEVICE_REMOTE_WAKEUP:
  343. break;
  344. /*
  345. * 9.4.1 says only only for SS, in AddressState only for
  346. * default control pipe
  347. */
  348. case USB_DEVICE_U1_ENABLE:
  349. if (state != USB_STATE_CONFIGURED)
  350. return -EINVAL;
  351. if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
  352. (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
  353. return -EINVAL;
  354. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  355. if (set)
  356. reg |= DWC3_DCTL_INITU1ENA;
  357. else
  358. reg &= ~DWC3_DCTL_INITU1ENA;
  359. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  360. break;
  361. case USB_DEVICE_U2_ENABLE:
  362. if (state != USB_STATE_CONFIGURED)
  363. return -EINVAL;
  364. if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
  365. (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
  366. return -EINVAL;
  367. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  368. if (set)
  369. reg |= DWC3_DCTL_INITU2ENA;
  370. else
  371. reg &= ~DWC3_DCTL_INITU2ENA;
  372. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  373. break;
  374. case USB_DEVICE_LTM_ENABLE:
  375. return -EINVAL;
  376. case USB_DEVICE_TEST_MODE:
  377. if ((wIndex & 0xff) != 0)
  378. return -EINVAL;
  379. if (!set)
  380. return -EINVAL;
  381. dwc->test_mode_nr = wIndex >> 8;
  382. dwc->test_mode = true;
  383. break;
  384. default:
  385. return -EINVAL;
  386. }
  387. break;
  388. case USB_RECIP_INTERFACE:
  389. switch (wValue) {
  390. case USB_INTRF_FUNC_SUSPEND:
  391. if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
  392. /* XXX enable Low power suspend */
  393. ;
  394. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
  395. /* XXX enable remote wakeup */
  396. ;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. break;
  402. case USB_RECIP_ENDPOINT:
  403. switch (wValue) {
  404. case USB_ENDPOINT_HALT:
  405. dep = dwc3_wIndex_to_dep(dwc, wIndex);
  406. if (!dep)
  407. return -EINVAL;
  408. if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
  409. break;
  410. ret = __dwc3_gadget_ep_set_halt(dep, set, true);
  411. if (ret)
  412. return -EINVAL;
  413. break;
  414. default:
  415. return -EINVAL;
  416. }
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. return 0;
  422. }
  423. static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  424. {
  425. enum usb_device_state state = dwc->gadget.state;
  426. u32 addr;
  427. u32 reg;
  428. addr = le16_to_cpu(ctrl->wValue);
  429. if (addr > 127) {
  430. dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
  431. return -EINVAL;
  432. }
  433. if (state == USB_STATE_CONFIGURED) {
  434. dwc3_trace(trace_dwc3_ep0,
  435. "trying to set address when configured");
  436. return -EINVAL;
  437. }
  438. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  439. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  440. reg |= DWC3_DCFG_DEVADDR(addr);
  441. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  442. if (addr)
  443. usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
  444. else
  445. usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
  446. return 0;
  447. }
  448. static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  449. {
  450. int ret;
  451. spin_unlock(&dwc->lock);
  452. ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
  453. spin_lock(&dwc->lock);
  454. return ret;
  455. }
  456. static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  457. {
  458. enum usb_device_state state = dwc->gadget.state;
  459. u32 cfg;
  460. int ret;
  461. u32 reg;
  462. cfg = le16_to_cpu(ctrl->wValue);
  463. switch (state) {
  464. case USB_STATE_DEFAULT:
  465. return -EINVAL;
  466. case USB_STATE_ADDRESS:
  467. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  468. /* if the cfg matches and the cfg is non zero */
  469. if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
  470. /*
  471. * only change state if set_config has already
  472. * been processed. If gadget driver returns
  473. * USB_GADGET_DELAYED_STATUS, we will wait
  474. * to change the state on the next usb_ep_queue()
  475. */
  476. if (ret == 0)
  477. usb_gadget_set_state(&dwc->gadget,
  478. USB_STATE_CONFIGURED);
  479. /*
  480. * Enable transition to U1/U2 state when
  481. * nothing is pending from application.
  482. */
  483. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  484. reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
  485. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  486. dwc->resize_fifos = true;
  487. dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET");
  488. }
  489. break;
  490. case USB_STATE_CONFIGURED:
  491. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  492. if (!cfg && !ret)
  493. usb_gadget_set_state(&dwc->gadget,
  494. USB_STATE_ADDRESS);
  495. break;
  496. default:
  497. ret = -EINVAL;
  498. }
  499. return ret;
  500. }
  501. static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
  502. {
  503. struct dwc3_ep *dep = to_dwc3_ep(ep);
  504. struct dwc3 *dwc = dep->dwc;
  505. u32 param = 0;
  506. u32 reg;
  507. struct timing {
  508. u8 u1sel;
  509. u8 u1pel;
  510. u16 u2sel;
  511. u16 u2pel;
  512. } __packed timing;
  513. int ret;
  514. memcpy(&timing, req->buf, sizeof(timing));
  515. dwc->u1sel = timing.u1sel;
  516. dwc->u1pel = timing.u1pel;
  517. dwc->u2sel = le16_to_cpu(timing.u2sel);
  518. dwc->u2pel = le16_to_cpu(timing.u2pel);
  519. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  520. if (reg & DWC3_DCTL_INITU2ENA)
  521. param = dwc->u2pel;
  522. if (reg & DWC3_DCTL_INITU1ENA)
  523. param = dwc->u1pel;
  524. /*
  525. * According to Synopsys Databook, if parameter is
  526. * greater than 125, a value of zero should be
  527. * programmed in the register.
  528. */
  529. if (param > 125)
  530. param = 0;
  531. /* now that we have the time, issue DGCMD Set Sel */
  532. ret = dwc3_send_gadget_generic_command(dwc,
  533. DWC3_DGCMD_SET_PERIODIC_PAR, param);
  534. WARN_ON(ret < 0);
  535. }
  536. static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  537. {
  538. struct dwc3_ep *dep;
  539. enum usb_device_state state = dwc->gadget.state;
  540. u16 wLength;
  541. u16 wValue;
  542. if (state == USB_STATE_DEFAULT)
  543. return -EINVAL;
  544. wValue = le16_to_cpu(ctrl->wValue);
  545. wLength = le16_to_cpu(ctrl->wLength);
  546. if (wLength != 6) {
  547. dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
  548. wLength);
  549. return -EINVAL;
  550. }
  551. /*
  552. * To handle Set SEL we need to receive 6 bytes from Host. So let's
  553. * queue a usb_request for 6 bytes.
  554. *
  555. * Remember, though, this controller can't handle non-wMaxPacketSize
  556. * aligned transfers on the OUT direction, so we queue a request for
  557. * wMaxPacketSize instead.
  558. */
  559. dep = dwc->eps[0];
  560. dwc->ep0_usb_req.dep = dep;
  561. dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
  562. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  563. dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
  564. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  565. }
  566. static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  567. {
  568. u16 wLength;
  569. u16 wValue;
  570. u16 wIndex;
  571. wValue = le16_to_cpu(ctrl->wValue);
  572. wLength = le16_to_cpu(ctrl->wLength);
  573. wIndex = le16_to_cpu(ctrl->wIndex);
  574. if (wIndex || wLength)
  575. return -EINVAL;
  576. /*
  577. * REVISIT It's unclear from Databook what to do with this
  578. * value. For now, just cache it.
  579. */
  580. dwc->isoch_delay = wValue;
  581. return 0;
  582. }
  583. static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  584. {
  585. int ret;
  586. switch (ctrl->bRequest) {
  587. case USB_REQ_GET_STATUS:
  588. dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
  589. ret = dwc3_ep0_handle_status(dwc, ctrl);
  590. break;
  591. case USB_REQ_CLEAR_FEATURE:
  592. dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
  593. ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
  594. break;
  595. case USB_REQ_SET_FEATURE:
  596. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
  597. ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
  598. break;
  599. case USB_REQ_SET_ADDRESS:
  600. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
  601. ret = dwc3_ep0_set_address(dwc, ctrl);
  602. break;
  603. case USB_REQ_SET_CONFIGURATION:
  604. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
  605. ret = dwc3_ep0_set_config(dwc, ctrl);
  606. break;
  607. case USB_REQ_SET_SEL:
  608. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
  609. ret = dwc3_ep0_set_sel(dwc, ctrl);
  610. break;
  611. case USB_REQ_SET_ISOCH_DELAY:
  612. dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
  613. ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
  614. break;
  615. default:
  616. dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
  617. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  618. break;
  619. }
  620. return ret;
  621. }
  622. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  623. const struct dwc3_event_depevt *event)
  624. {
  625. struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
  626. int ret = -EINVAL;
  627. u32 len;
  628. if (!dwc->gadget_driver)
  629. goto out;
  630. trace_dwc3_ctrl_req(ctrl);
  631. len = le16_to_cpu(ctrl->wLength);
  632. if (!len) {
  633. dwc->three_stage_setup = false;
  634. dwc->ep0_expect_in = false;
  635. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  636. } else {
  637. dwc->three_stage_setup = true;
  638. dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
  639. dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
  640. }
  641. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  642. ret = dwc3_ep0_std_request(dwc, ctrl);
  643. else
  644. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  645. if (ret == USB_GADGET_DELAYED_STATUS)
  646. dwc->delayed_status = true;
  647. out:
  648. if (ret < 0)
  649. dwc3_ep0_stall_and_restart(dwc);
  650. }
  651. static void dwc3_ep0_complete_data(struct dwc3 *dwc,
  652. const struct dwc3_event_depevt *event)
  653. {
  654. struct dwc3_request *r = NULL;
  655. struct usb_request *ur;
  656. struct dwc3_trb *trb;
  657. struct dwc3_ep *ep0;
  658. unsigned transfer_size = 0;
  659. unsigned maxp;
  660. unsigned remaining_ur_length;
  661. void *buf;
  662. u32 transferred = 0;
  663. u32 status;
  664. u32 length;
  665. u8 epnum;
  666. epnum = event->endpoint_number;
  667. ep0 = dwc->eps[0];
  668. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  669. trb = dwc->ep0_trb;
  670. trace_dwc3_complete_trb(ep0, trb);
  671. r = next_request(&ep0->request_list);
  672. if (!r)
  673. return;
  674. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  675. if (status == DWC3_TRBSTS_SETUP_PENDING) {
  676. dwc->setup_packet_pending = true;
  677. dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
  678. if (r)
  679. dwc3_gadget_giveback(ep0, r, -ECONNRESET);
  680. return;
  681. }
  682. ur = &r->request;
  683. buf = ur->buf;
  684. remaining_ur_length = ur->length;
  685. length = trb->size & DWC3_TRB_SIZE_MASK;
  686. maxp = ep0->endpoint.maxpacket;
  687. if (dwc->ep0_bounced) {
  688. /*
  689. * Handle the first TRB before handling the bounce buffer if
  690. * the request length is greater than the bounce buffer size
  691. */
  692. if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
  693. transfer_size = ALIGN(ur->length - maxp, maxp);
  694. transferred = transfer_size - length;
  695. buf = (u8 *)buf + transferred;
  696. ur->actual += transferred;
  697. remaining_ur_length -= transferred;
  698. trb++;
  699. length = trb->size & DWC3_TRB_SIZE_MASK;
  700. ep0->free_slot = 0;
  701. }
  702. transfer_size = roundup((ur->length - transfer_size),
  703. maxp);
  704. transferred = min_t(u32, remaining_ur_length,
  705. transfer_size - length);
  706. memcpy(buf, dwc->ep0_bounce, transferred);
  707. } else {
  708. transferred = ur->length - length;
  709. }
  710. ur->actual += transferred;
  711. if ((epnum & 1) && ur->actual < ur->length) {
  712. /* for some reason we did not get everything out */
  713. dwc3_ep0_stall_and_restart(dwc);
  714. } else {
  715. dwc3_gadget_giveback(ep0, r, 0);
  716. if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
  717. ur->length && ur->zero) {
  718. int ret;
  719. dwc->ep0_next_event = DWC3_EP0_COMPLETE;
  720. ret = dwc3_ep0_start_trans(dwc, epnum,
  721. dwc->ctrl_req_addr, 0,
  722. DWC3_TRBCTL_CONTROL_DATA, false);
  723. WARN_ON(ret < 0);
  724. }
  725. }
  726. }
  727. static void dwc3_ep0_complete_status(struct dwc3 *dwc,
  728. const struct dwc3_event_depevt *event)
  729. {
  730. struct dwc3_request *r;
  731. struct dwc3_ep *dep;
  732. struct dwc3_trb *trb;
  733. u32 status;
  734. dep = dwc->eps[0];
  735. trb = dwc->ep0_trb;
  736. trace_dwc3_complete_trb(dep, trb);
  737. if (!list_empty(&dep->request_list)) {
  738. r = next_request(&dep->request_list);
  739. dwc3_gadget_giveback(dep, r, 0);
  740. }
  741. if (dwc->test_mode) {
  742. int ret;
  743. ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
  744. if (ret < 0) {
  745. dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
  746. dwc->test_mode_nr);
  747. dwc3_ep0_stall_and_restart(dwc);
  748. return;
  749. }
  750. }
  751. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  752. if (status == DWC3_TRBSTS_SETUP_PENDING) {
  753. dwc->setup_packet_pending = true;
  754. dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
  755. }
  756. dwc->ep0state = EP0_SETUP_PHASE;
  757. dwc3_ep0_out_start(dwc);
  758. }
  759. static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
  760. const struct dwc3_event_depevt *event)
  761. {
  762. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  763. dep->flags &= ~DWC3_EP_BUSY;
  764. dep->resource_index = 0;
  765. dwc->setup_packet_pending = false;
  766. switch (dwc->ep0state) {
  767. case EP0_SETUP_PHASE:
  768. dwc3_trace(trace_dwc3_ep0, "Setup Phase");
  769. dwc3_ep0_inspect_setup(dwc, event);
  770. break;
  771. case EP0_DATA_PHASE:
  772. dwc3_trace(trace_dwc3_ep0, "Data Phase");
  773. dwc3_ep0_complete_data(dwc, event);
  774. break;
  775. case EP0_STATUS_PHASE:
  776. dwc3_trace(trace_dwc3_ep0, "Status Phase");
  777. dwc3_ep0_complete_status(dwc, event);
  778. break;
  779. default:
  780. WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
  781. }
  782. }
  783. static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
  784. struct dwc3_ep *dep, struct dwc3_request *req)
  785. {
  786. int ret;
  787. req->direction = !!dep->number;
  788. if (req->request.length == 0) {
  789. ret = dwc3_ep0_start_trans(dwc, dep->number,
  790. dwc->ctrl_req_addr, 0,
  791. DWC3_TRBCTL_CONTROL_DATA, false);
  792. } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
  793. && (dep->number == 0)) {
  794. u32 transfer_size = 0;
  795. u32 maxpacket;
  796. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  797. dep->number);
  798. if (ret) {
  799. dwc3_trace(trace_dwc3_ep0, "failed to map request\n");
  800. return;
  801. }
  802. maxpacket = dep->endpoint.maxpacket;
  803. if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
  804. transfer_size = ALIGN(req->request.length - maxpacket,
  805. maxpacket);
  806. ret = dwc3_ep0_start_trans(dwc, dep->number,
  807. req->request.dma,
  808. transfer_size,
  809. DWC3_TRBCTL_CONTROL_DATA,
  810. true);
  811. }
  812. transfer_size = roundup((req->request.length - transfer_size),
  813. maxpacket);
  814. dwc->ep0_bounced = true;
  815. ret = dwc3_ep0_start_trans(dwc, dep->number,
  816. dwc->ep0_bounce_addr, transfer_size,
  817. DWC3_TRBCTL_CONTROL_DATA, false);
  818. } else {
  819. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  820. dep->number);
  821. if (ret) {
  822. dwc3_trace(trace_dwc3_ep0, "failed to map request\n");
  823. return;
  824. }
  825. ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
  826. req->request.length, DWC3_TRBCTL_CONTROL_DATA,
  827. false);
  828. }
  829. WARN_ON(ret < 0);
  830. }
  831. static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
  832. {
  833. struct dwc3 *dwc = dep->dwc;
  834. u32 type;
  835. type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
  836. : DWC3_TRBCTL_CONTROL_STATUS2;
  837. return dwc3_ep0_start_trans(dwc, dep->number,
  838. dwc->ctrl_req_addr, 0, type, false);
  839. }
  840. static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
  841. {
  842. if (dwc->resize_fifos) {
  843. dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs");
  844. dwc3_gadget_resize_tx_fifos(dwc);
  845. dwc->resize_fifos = 0;
  846. }
  847. WARN_ON(dwc3_ep0_start_control_status(dep));
  848. }
  849. static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
  850. const struct dwc3_event_depevt *event)
  851. {
  852. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  853. __dwc3_ep0_do_control_status(dwc, dep);
  854. }
  855. static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
  856. {
  857. struct dwc3_gadget_ep_cmd_params params;
  858. u32 cmd;
  859. int ret;
  860. if (!dep->resource_index)
  861. return;
  862. cmd = DWC3_DEPCMD_ENDTRANSFER;
  863. cmd |= DWC3_DEPCMD_CMDIOC;
  864. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  865. memset(&params, 0, sizeof(params));
  866. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  867. WARN_ON_ONCE(ret);
  868. dep->resource_index = 0;
  869. }
  870. static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
  871. const struct dwc3_event_depevt *event)
  872. {
  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. }