ep0.c 28 KB

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