ep0.c 27 KB

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