ep0.c 28 KB

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