ep0.c 27 KB

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