ep0.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. WARN_ON(ret < 0);
  233. for (i = 2; i < DWC3_ENDPOINTS_NUM; i++) {
  234. struct dwc3_ep *dwc3_ep;
  235. dwc3_ep = dwc->eps[i];
  236. if (!dwc3_ep)
  237. continue;
  238. if (!(dwc3_ep->flags & DWC3_EP_DELAY_STOP))
  239. continue;
  240. dwc3_ep->flags &= ~DWC3_EP_DELAY_STOP;
  241. if (dwc->connected)
  242. dwc3_stop_active_transfer(dwc3_ep, true, true);
  243. else
  244. dwc3_remove_requests(dwc, dwc3_ep, -ESHUTDOWN);
  245. }
  246. }
  247. static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
  248. {
  249. struct dwc3_ep *dep;
  250. u32 windex = le16_to_cpu(wIndex_le);
  251. u32 epnum;
  252. epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
  253. if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  254. epnum |= 1;
  255. dep = dwc->eps[epnum];
  256. if (dep == NULL)
  257. return NULL;
  258. if (dep->flags & DWC3_EP_ENABLED)
  259. return dep;
  260. return NULL;
  261. }
  262. static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
  263. {
  264. }
  265. /*
  266. * ch 9.4.5
  267. */
  268. static int dwc3_ep0_handle_status(struct dwc3 *dwc,
  269. struct usb_ctrlrequest *ctrl)
  270. {
  271. struct dwc3_ep *dep;
  272. u32 recip;
  273. u32 value;
  274. u32 reg;
  275. u16 usb_status = 0;
  276. __le16 *response_pkt;
  277. /* We don't support PTM_STATUS */
  278. value = le16_to_cpu(ctrl->wValue);
  279. if (value != 0)
  280. return -EINVAL;
  281. recip = ctrl->bRequestType & USB_RECIP_MASK;
  282. switch (recip) {
  283. case USB_RECIP_DEVICE:
  284. /*
  285. * LTM will be set once we know how to set this in HW.
  286. */
  287. usb_status |= dwc->gadget->is_selfpowered;
  288. if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
  289. (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
  290. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  291. if (reg & DWC3_DCTL_INITU1ENA)
  292. usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
  293. if (reg & DWC3_DCTL_INITU2ENA)
  294. usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
  295. } else {
  296. usb_status |= dwc->gadget->wakeup_armed <<
  297. USB_DEVICE_REMOTE_WAKEUP;
  298. }
  299. break;
  300. case USB_RECIP_INTERFACE:
  301. /*
  302. * Function Remote Wake Capable D0
  303. * Function Remote Wakeup D1
  304. */
  305. return dwc3_ep0_delegate_req(dwc, ctrl);
  306. case USB_RECIP_ENDPOINT:
  307. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  308. if (!dep)
  309. return -EINVAL;
  310. if (dep->flags & DWC3_EP_STALL)
  311. usb_status = 1 << USB_ENDPOINT_HALT;
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. response_pkt = (__le16 *) dwc->setup_buf;
  317. *response_pkt = cpu_to_le16(usb_status);
  318. dep = dwc->eps[0];
  319. dwc->ep0_usb_req.dep = dep;
  320. dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
  321. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  322. dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
  323. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  324. }
  325. static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
  326. int set)
  327. {
  328. u32 reg;
  329. if (state != USB_STATE_CONFIGURED)
  330. return -EINVAL;
  331. if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
  332. (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
  333. return -EINVAL;
  334. if (set && dwc->dis_u1_entry_quirk)
  335. return -EINVAL;
  336. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  337. if (set)
  338. reg |= DWC3_DCTL_INITU1ENA;
  339. else
  340. reg &= ~DWC3_DCTL_INITU1ENA;
  341. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  342. return 0;
  343. }
  344. static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
  345. int set)
  346. {
  347. u32 reg;
  348. if (state != USB_STATE_CONFIGURED)
  349. return -EINVAL;
  350. if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
  351. (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
  352. return -EINVAL;
  353. if (set && dwc->dis_u2_entry_quirk)
  354. return -EINVAL;
  355. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  356. if (set)
  357. reg |= DWC3_DCTL_INITU2ENA;
  358. else
  359. reg &= ~DWC3_DCTL_INITU2ENA;
  360. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  361. return 0;
  362. }
  363. static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
  364. u32 wIndex, int set)
  365. {
  366. if ((wIndex & 0xff) != 0)
  367. return -EINVAL;
  368. if (!set)
  369. return -EINVAL;
  370. switch (wIndex >> 8) {
  371. case USB_TEST_J:
  372. case USB_TEST_K:
  373. case USB_TEST_SE0_NAK:
  374. case USB_TEST_PACKET:
  375. case USB_TEST_FORCE_ENABLE:
  376. dwc->test_mode_nr = wIndex >> 8;
  377. dwc->test_mode = true;
  378. break;
  379. default:
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. static int dwc3_ep0_handle_device(struct dwc3 *dwc,
  385. struct usb_ctrlrequest *ctrl, int set)
  386. {
  387. enum usb_device_state state;
  388. u32 wValue;
  389. u32 wIndex;
  390. int ret = 0;
  391. wValue = le16_to_cpu(ctrl->wValue);
  392. wIndex = le16_to_cpu(ctrl->wIndex);
  393. state = dwc->gadget->state;
  394. switch (wValue) {
  395. case USB_DEVICE_REMOTE_WAKEUP:
  396. if (dwc->wakeup_configured)
  397. dwc->gadget->wakeup_armed = set;
  398. else
  399. ret = -EINVAL;
  400. break;
  401. /*
  402. * 9.4.1 says only for SS, in AddressState only for
  403. * default control pipe
  404. */
  405. case USB_DEVICE_U1_ENABLE:
  406. ret = dwc3_ep0_handle_u1(dwc, state, set);
  407. break;
  408. case USB_DEVICE_U2_ENABLE:
  409. ret = dwc3_ep0_handle_u2(dwc, state, set);
  410. break;
  411. case USB_DEVICE_LTM_ENABLE:
  412. ret = -EINVAL;
  413. break;
  414. case USB_DEVICE_TEST_MODE:
  415. ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
  416. break;
  417. default:
  418. ret = -EINVAL;
  419. }
  420. return ret;
  421. }
  422. static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
  423. struct usb_ctrlrequest *ctrl, int set)
  424. {
  425. u32 wValue;
  426. int ret = 0;
  427. wValue = le16_to_cpu(ctrl->wValue);
  428. switch (wValue) {
  429. case USB_INTRF_FUNC_SUSPEND:
  430. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  431. break;
  432. default:
  433. ret = -EINVAL;
  434. }
  435. return ret;
  436. }
  437. static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
  438. struct usb_ctrlrequest *ctrl, int set)
  439. {
  440. struct dwc3_ep *dep;
  441. u32 wValue;
  442. int ret;
  443. wValue = le16_to_cpu(ctrl->wValue);
  444. switch (wValue) {
  445. case USB_ENDPOINT_HALT:
  446. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  447. if (!dep)
  448. return -EINVAL;
  449. if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
  450. break;
  451. ret = __dwc3_gadget_ep_set_halt(dep, set, true);
  452. if (ret)
  453. return -EINVAL;
  454. /* ClearFeature(Halt) may need delayed status */
  455. if (!set && (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  456. return USB_GADGET_DELAYED_STATUS;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. return 0;
  462. }
  463. static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
  464. struct usb_ctrlrequest *ctrl, int set)
  465. {
  466. u32 recip;
  467. int ret;
  468. recip = ctrl->bRequestType & USB_RECIP_MASK;
  469. switch (recip) {
  470. case USB_RECIP_DEVICE:
  471. ret = dwc3_ep0_handle_device(dwc, ctrl, set);
  472. break;
  473. case USB_RECIP_INTERFACE:
  474. ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
  475. break;
  476. case USB_RECIP_ENDPOINT:
  477. ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
  478. break;
  479. default:
  480. ret = -EINVAL;
  481. }
  482. return ret;
  483. }
  484. static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  485. {
  486. enum usb_device_state state = dwc->gadget->state;
  487. u32 addr;
  488. u32 reg;
  489. addr = le16_to_cpu(ctrl->wValue);
  490. if (addr > 127) {
  491. dev_err(dwc->dev, "invalid device address %d\n", addr);
  492. return -EINVAL;
  493. }
  494. if (state == USB_STATE_CONFIGURED) {
  495. dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
  496. return -EINVAL;
  497. }
  498. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  499. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  500. reg |= DWC3_DCFG_DEVADDR(addr);
  501. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  502. if (addr)
  503. usb_gadget_set_state(dwc->gadget, USB_STATE_ADDRESS);
  504. else
  505. usb_gadget_set_state(dwc->gadget, USB_STATE_DEFAULT);
  506. return 0;
  507. }
  508. static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  509. {
  510. int ret = -EINVAL;
  511. if (dwc->async_callbacks) {
  512. spin_unlock(&dwc->lock);
  513. ret = dwc->gadget_driver->setup(dwc->gadget, ctrl);
  514. spin_lock(&dwc->lock);
  515. }
  516. return ret;
  517. }
  518. static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  519. {
  520. enum usb_device_state state = dwc->gadget->state;
  521. u32 cfg;
  522. int ret;
  523. u32 reg;
  524. cfg = le16_to_cpu(ctrl->wValue);
  525. switch (state) {
  526. case USB_STATE_DEFAULT:
  527. return -EINVAL;
  528. case USB_STATE_ADDRESS:
  529. dwc3_gadget_start_config(dwc, 2);
  530. dwc3_gadget_clear_tx_fifos(dwc);
  531. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  532. /* if the cfg matches and the cfg is non zero */
  533. if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
  534. /*
  535. * only change state if set_config has already
  536. * been processed. If gadget driver returns
  537. * USB_GADGET_DELAYED_STATUS, we will wait
  538. * to change the state on the next usb_ep_queue()
  539. */
  540. if (ret == 0)
  541. usb_gadget_set_state(dwc->gadget,
  542. USB_STATE_CONFIGURED);
  543. /*
  544. * Enable transition to U1/U2 state when
  545. * nothing is pending from application.
  546. */
  547. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  548. if (!dwc->dis_u1_entry_quirk)
  549. reg |= DWC3_DCTL_ACCEPTU1ENA;
  550. if (!dwc->dis_u2_entry_quirk)
  551. reg |= DWC3_DCTL_ACCEPTU2ENA;
  552. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  553. }
  554. break;
  555. case USB_STATE_CONFIGURED:
  556. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  557. if (!cfg && !ret)
  558. usb_gadget_set_state(dwc->gadget,
  559. USB_STATE_ADDRESS);
  560. break;
  561. default:
  562. ret = -EINVAL;
  563. }
  564. return ret;
  565. }
  566. static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
  567. {
  568. struct dwc3_ep *dep = to_dwc3_ep(ep);
  569. struct dwc3 *dwc = dep->dwc;
  570. u32 param = 0;
  571. u32 reg;
  572. struct timing {
  573. u8 u1sel;
  574. u8 u1pel;
  575. __le16 u2sel;
  576. __le16 u2pel;
  577. } __packed timing;
  578. int ret;
  579. memcpy(&timing, req->buf, sizeof(timing));
  580. dwc->u1sel = timing.u1sel;
  581. dwc->u1pel = timing.u1pel;
  582. dwc->u2sel = le16_to_cpu(timing.u2sel);
  583. dwc->u2pel = le16_to_cpu(timing.u2pel);
  584. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  585. if (reg & DWC3_DCTL_INITU2ENA)
  586. param = dwc->u2pel;
  587. if (reg & DWC3_DCTL_INITU1ENA)
  588. param = dwc->u1pel;
  589. /*
  590. * According to Synopsys Databook, if parameter is
  591. * greater than 125, a value of zero should be
  592. * programmed in the register.
  593. */
  594. if (param > 125)
  595. param = 0;
  596. /* now that we have the time, issue DGCMD Set Sel */
  597. ret = dwc3_send_gadget_generic_command(dwc,
  598. DWC3_DGCMD_SET_PERIODIC_PAR, param);
  599. WARN_ON(ret < 0);
  600. }
  601. static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  602. {
  603. struct dwc3_ep *dep;
  604. enum usb_device_state state = dwc->gadget->state;
  605. u16 wLength;
  606. if (state == USB_STATE_DEFAULT)
  607. return -EINVAL;
  608. wLength = le16_to_cpu(ctrl->wLength);
  609. if (wLength != 6) {
  610. dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
  611. wLength);
  612. return -EINVAL;
  613. }
  614. /*
  615. * To handle Set SEL we need to receive 6 bytes from Host. So let's
  616. * queue a usb_request for 6 bytes.
  617. *
  618. * Remember, though, this controller can't handle non-wMaxPacketSize
  619. * aligned transfers on the OUT direction, so we queue a request for
  620. * wMaxPacketSize instead.
  621. */
  622. dep = dwc->eps[0];
  623. dwc->ep0_usb_req.dep = dep;
  624. dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
  625. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  626. dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
  627. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  628. }
  629. static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  630. {
  631. u16 wLength;
  632. u16 wValue;
  633. u16 wIndex;
  634. wValue = le16_to_cpu(ctrl->wValue);
  635. wLength = le16_to_cpu(ctrl->wLength);
  636. wIndex = le16_to_cpu(ctrl->wIndex);
  637. if (wIndex || wLength)
  638. return -EINVAL;
  639. dwc->gadget->isoch_delay = wValue;
  640. return 0;
  641. }
  642. static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  643. {
  644. int ret;
  645. switch (ctrl->bRequest) {
  646. case USB_REQ_GET_STATUS:
  647. ret = dwc3_ep0_handle_status(dwc, ctrl);
  648. break;
  649. case USB_REQ_CLEAR_FEATURE:
  650. ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
  651. break;
  652. case USB_REQ_SET_FEATURE:
  653. ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
  654. break;
  655. case USB_REQ_SET_ADDRESS:
  656. ret = dwc3_ep0_set_address(dwc, ctrl);
  657. break;
  658. case USB_REQ_SET_CONFIGURATION:
  659. ret = dwc3_ep0_set_config(dwc, ctrl);
  660. break;
  661. case USB_REQ_SET_SEL:
  662. ret = dwc3_ep0_set_sel(dwc, ctrl);
  663. break;
  664. case USB_REQ_SET_ISOCH_DELAY:
  665. ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
  666. break;
  667. default:
  668. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  669. break;
  670. }
  671. return ret;
  672. }
  673. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  674. const struct dwc3_event_depevt *event)
  675. {
  676. struct usb_ctrlrequest *ctrl = (void *) dwc->ep0_trb;
  677. int ret = -EINVAL;
  678. u32 len;
  679. if (!dwc->gadget_driver || !dwc->softconnect || !dwc->connected)
  680. goto out;
  681. trace_dwc3_ctrl_req(ctrl);
  682. len = le16_to_cpu(ctrl->wLength);
  683. if (!len) {
  684. dwc->three_stage_setup = false;
  685. dwc->ep0_expect_in = false;
  686. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  687. } else {
  688. dwc->three_stage_setup = true;
  689. dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
  690. dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
  691. }
  692. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  693. ret = dwc3_ep0_std_request(dwc, ctrl);
  694. else
  695. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  696. if (ret == USB_GADGET_DELAYED_STATUS)
  697. dwc->delayed_status = true;
  698. out:
  699. if (ret < 0)
  700. dwc3_ep0_stall_and_restart(dwc);
  701. }
  702. static void dwc3_ep0_complete_data(struct dwc3 *dwc,
  703. const struct dwc3_event_depevt *event)
  704. {
  705. struct dwc3_request *r;
  706. struct usb_request *ur;
  707. struct dwc3_trb *trb;
  708. struct dwc3_ep *ep0;
  709. u32 transferred = 0;
  710. u32 status;
  711. u32 length;
  712. u8 epnum;
  713. epnum = event->endpoint_number;
  714. ep0 = dwc->eps[0];
  715. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  716. trb = dwc->ep0_trb;
  717. trace_dwc3_complete_trb(ep0, trb);
  718. r = next_request(&ep0->pending_list);
  719. if (!r)
  720. return;
  721. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  722. if (status == DWC3_TRBSTS_SETUP_PENDING) {
  723. dwc->setup_packet_pending = true;
  724. if (r)
  725. dwc3_gadget_giveback(ep0, r, -ECONNRESET);
  726. return;
  727. }
  728. ur = &r->request;
  729. length = trb->size & DWC3_TRB_SIZE_MASK;
  730. transferred = ur->length - length;
  731. ur->actual += transferred;
  732. if ((IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
  733. ur->length && ur->zero) || dwc->ep0_bounced) {
  734. trb++;
  735. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  736. trace_dwc3_complete_trb(ep0, trb);
  737. if (r->direction)
  738. dwc->eps[1]->trb_enqueue = 0;
  739. else
  740. dwc->eps[0]->trb_enqueue = 0;
  741. dwc->ep0_bounced = false;
  742. }
  743. if ((epnum & 1) && ur->actual < ur->length)
  744. dwc3_ep0_stall_and_restart(dwc);
  745. else
  746. dwc3_gadget_giveback(ep0, r, 0);
  747. }
  748. static void dwc3_ep0_complete_status(struct dwc3 *dwc,
  749. const struct dwc3_event_depevt *event)
  750. {
  751. struct dwc3_request *r;
  752. struct dwc3_ep *dep;
  753. struct dwc3_trb *trb;
  754. u32 status;
  755. dep = dwc->eps[0];
  756. trb = dwc->ep0_trb;
  757. trace_dwc3_complete_trb(dep, trb);
  758. if (!list_empty(&dep->pending_list)) {
  759. r = next_request(&dep->pending_list);
  760. dwc3_gadget_giveback(dep, r, 0);
  761. }
  762. if (dwc->test_mode) {
  763. int ret;
  764. ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
  765. if (ret < 0) {
  766. dev_err(dwc->dev, "invalid test #%d\n",
  767. dwc->test_mode_nr);
  768. dwc3_ep0_stall_and_restart(dwc);
  769. return;
  770. }
  771. }
  772. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  773. if (status == DWC3_TRBSTS_SETUP_PENDING)
  774. dwc->setup_packet_pending = true;
  775. dwc->ep0state = EP0_SETUP_PHASE;
  776. dwc3_ep0_out_start(dwc);
  777. }
  778. static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
  779. const struct dwc3_event_depevt *event)
  780. {
  781. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  782. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  783. dep->resource_index = 0;
  784. dwc->setup_packet_pending = false;
  785. switch (dwc->ep0state) {
  786. case EP0_SETUP_PHASE:
  787. dwc3_ep0_inspect_setup(dwc, event);
  788. break;
  789. case EP0_DATA_PHASE:
  790. dwc3_ep0_complete_data(dwc, event);
  791. break;
  792. case EP0_STATUS_PHASE:
  793. dwc3_ep0_complete_status(dwc, event);
  794. break;
  795. default:
  796. WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
  797. }
  798. }
  799. static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
  800. struct dwc3_ep *dep, struct dwc3_request *req)
  801. {
  802. unsigned int trb_length = 0;
  803. int ret;
  804. req->direction = !!dep->number;
  805. if (req->request.length == 0) {
  806. if (!req->direction)
  807. trb_length = dep->endpoint.maxpacket;
  808. dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr, trb_length,
  809. DWC3_TRBCTL_CONTROL_DATA, false);
  810. ret = dwc3_ep0_start_trans(dep);
  811. } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
  812. && (dep->number == 0)) {
  813. u32 maxpacket;
  814. u32 rem;
  815. ret = usb_gadget_map_request_by_dev(dwc->sysdev,
  816. &req->request, dep->number);
  817. if (ret)
  818. return;
  819. maxpacket = dep->endpoint.maxpacket;
  820. rem = req->request.length % maxpacket;
  821. dwc->ep0_bounced = true;
  822. /* prepare normal TRB */
  823. dwc3_ep0_prepare_one_trb(dep, req->request.dma,
  824. req->request.length,
  825. DWC3_TRBCTL_CONTROL_DATA,
  826. true);
  827. req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
  828. /* Now prepare one extra TRB to align transfer size */
  829. dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
  830. maxpacket - rem,
  831. DWC3_TRBCTL_CONTROL_DATA,
  832. false);
  833. ret = dwc3_ep0_start_trans(dep);
  834. } else if (IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
  835. req->request.length && req->request.zero) {
  836. ret = usb_gadget_map_request_by_dev(dwc->sysdev,
  837. &req->request, dep->number);
  838. if (ret)
  839. return;
  840. /* prepare normal TRB */
  841. dwc3_ep0_prepare_one_trb(dep, req->request.dma,
  842. req->request.length,
  843. DWC3_TRBCTL_CONTROL_DATA,
  844. true);
  845. req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
  846. if (!req->direction)
  847. trb_length = dep->endpoint.maxpacket;
  848. /* Now prepare one extra TRB to align transfer size */
  849. dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
  850. trb_length, DWC3_TRBCTL_CONTROL_DATA,
  851. false);
  852. ret = dwc3_ep0_start_trans(dep);
  853. } else {
  854. ret = usb_gadget_map_request_by_dev(dwc->sysdev,
  855. &req->request, dep->number);
  856. if (ret)
  857. return;
  858. dwc3_ep0_prepare_one_trb(dep, req->request.dma,
  859. req->request.length, DWC3_TRBCTL_CONTROL_DATA,
  860. false);
  861. req->trb = &dwc->ep0_trb[dep->trb_enqueue];
  862. ret = dwc3_ep0_start_trans(dep);
  863. }
  864. WARN_ON(ret < 0);
  865. }
  866. static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
  867. {
  868. struct dwc3 *dwc = dep->dwc;
  869. u32 type;
  870. type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
  871. : DWC3_TRBCTL_CONTROL_STATUS2;
  872. dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 0, type, false);
  873. return dwc3_ep0_start_trans(dep);
  874. }
  875. static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
  876. {
  877. WARN_ON(dwc3_ep0_start_control_status(dep));
  878. }
  879. static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
  880. const struct dwc3_event_depevt *event)
  881. {
  882. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  883. __dwc3_ep0_do_control_status(dwc, dep);
  884. }
  885. void dwc3_ep0_send_delayed_status(struct dwc3 *dwc)
  886. {
  887. unsigned int direction = !dwc->ep0_expect_in;
  888. dwc->delayed_status = false;
  889. dwc->clear_stall_protocol = 0;
  890. if (dwc->ep0state != EP0_STATUS_PHASE)
  891. return;
  892. __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
  893. }
  894. void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
  895. {
  896. struct dwc3_gadget_ep_cmd_params params;
  897. u32 cmd;
  898. int ret;
  899. /*
  900. * For status/DATA OUT stage, TRB will be queued on ep0 out
  901. * endpoint for which resource index is zero. Hence allow
  902. * queuing ENDXFER command for ep0 out endpoint.
  903. */
  904. if (!dep->resource_index && dep->number)
  905. return;
  906. cmd = DWC3_DEPCMD_ENDTRANSFER;
  907. cmd |= DWC3_DEPCMD_CMDIOC;
  908. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  909. memset(&params, 0, sizeof(params));
  910. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  911. WARN_ON_ONCE(ret);
  912. dep->resource_index = 0;
  913. }
  914. static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
  915. const struct dwc3_event_depevt *event)
  916. {
  917. switch (event->status) {
  918. case DEPEVT_STATUS_CONTROL_DATA:
  919. if (!dwc->softconnect || !dwc->connected)
  920. return;
  921. /*
  922. * We already have a DATA transfer in the controller's cache,
  923. * if we receive a XferNotReady(DATA) we will ignore it, unless
  924. * it's for the wrong direction.
  925. *
  926. * In that case, we must issue END_TRANSFER command to the Data
  927. * Phase we already have started and issue SetStall on the
  928. * control endpoint.
  929. */
  930. if (dwc->ep0_expect_in != event->endpoint_number) {
  931. struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
  932. dev_err(dwc->dev, "unexpected direction for Data Phase\n");
  933. dwc3_ep0_end_control_data(dwc, dep);
  934. dwc3_ep0_stall_and_restart(dwc);
  935. return;
  936. }
  937. break;
  938. case DEPEVT_STATUS_CONTROL_STATUS:
  939. if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
  940. return;
  941. if (dwc->setup_packet_pending) {
  942. dwc3_ep0_stall_and_restart(dwc);
  943. return;
  944. }
  945. dwc->ep0state = EP0_STATUS_PHASE;
  946. if (dwc->delayed_status) {
  947. struct dwc3_ep *dep = dwc->eps[0];
  948. WARN_ON_ONCE(event->endpoint_number != 1);
  949. /*
  950. * We should handle the delay STATUS phase here if the
  951. * request for handling delay STATUS has been queued
  952. * into the list.
  953. */
  954. if (!list_empty(&dep->pending_list)) {
  955. dwc->delayed_status = false;
  956. usb_gadget_set_state(dwc->gadget,
  957. USB_STATE_CONFIGURED);
  958. dwc3_ep0_do_control_status(dwc, event);
  959. }
  960. return;
  961. }
  962. dwc3_ep0_do_control_status(dwc, event);
  963. }
  964. }
  965. void dwc3_ep0_interrupt(struct dwc3 *dwc,
  966. const struct dwc3_event_depevt *event)
  967. {
  968. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  969. u8 cmd;
  970. switch (event->endpoint_event) {
  971. case DWC3_DEPEVT_XFERCOMPLETE:
  972. dwc3_ep0_xfer_complete(dwc, event);
  973. break;
  974. case DWC3_DEPEVT_XFERNOTREADY:
  975. dwc3_ep0_xfernotready(dwc, event);
  976. break;
  977. case DWC3_DEPEVT_XFERINPROGRESS:
  978. case DWC3_DEPEVT_RXTXFIFOEVT:
  979. case DWC3_DEPEVT_STREAMEVT:
  980. break;
  981. case DWC3_DEPEVT_EPCMDCMPLT:
  982. cmd = DEPEVT_PARAMETER_CMD(event->parameters);
  983. if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
  984. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  985. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  986. }
  987. break;
  988. default:
  989. dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event);
  990. break;
  991. }
  992. }