ep0.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /**
  2. * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/slab.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/pm_runtime.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/io.h>
  45. #include <linux/list.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/usb/ch9.h>
  48. #include <linux/usb/gadget.h>
  49. #include <linux/usb/composite.h>
  50. #include "core.h"
  51. #include "gadget.h"
  52. #include "io.h"
  53. static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
  54. static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
  55. struct dwc3_ep *dep, struct dwc3_request *req);
  56. static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
  57. {
  58. switch (state) {
  59. case EP0_UNCONNECTED:
  60. return "Unconnected";
  61. case EP0_SETUP_PHASE:
  62. return "Setup Phase";
  63. case EP0_DATA_PHASE:
  64. return "Data Phase";
  65. case EP0_STATUS_PHASE:
  66. return "Status Phase";
  67. default:
  68. return "UNKNOWN";
  69. }
  70. }
  71. static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
  72. u32 len, u32 type)
  73. {
  74. struct dwc3_gadget_ep_cmd_params params;
  75. struct dwc3_trb *trb;
  76. struct dwc3_ep *dep;
  77. int ret;
  78. dep = dwc->eps[epnum];
  79. if (dep->flags & DWC3_EP_BUSY) {
  80. dev_vdbg(dwc->dev, "%s: still busy\n", dep->name);
  81. return 0;
  82. }
  83. trb = dwc->ep0_trb;
  84. trb->bpl = lower_32_bits(buf_dma);
  85. trb->bph = upper_32_bits(buf_dma);
  86. trb->size = len;
  87. trb->ctrl = type;
  88. trb->ctrl |= (DWC3_TRB_CTRL_HWO
  89. | DWC3_TRB_CTRL_LST
  90. | DWC3_TRB_CTRL_IOC
  91. | DWC3_TRB_CTRL_ISP_IMI);
  92. memset(&params, 0, sizeof(params));
  93. params.param0 = upper_32_bits(dwc->ep0_trb_addr);
  94. params.param1 = lower_32_bits(dwc->ep0_trb_addr);
  95. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  96. DWC3_DEPCMD_STARTTRANSFER, &params);
  97. if (ret < 0) {
  98. dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
  99. return ret;
  100. }
  101. dep->flags |= DWC3_EP_BUSY;
  102. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
  103. dep->number);
  104. dwc->ep0_next_event = DWC3_EP0_COMPLETE;
  105. return 0;
  106. }
  107. static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
  108. struct dwc3_request *req)
  109. {
  110. struct dwc3 *dwc = dep->dwc;
  111. req->request.actual = 0;
  112. req->request.status = -EINPROGRESS;
  113. req->epnum = dep->number;
  114. list_add_tail(&req->list, &dep->request_list);
  115. /*
  116. * Gadget driver might not be quick enough to queue a request
  117. * before we get a Transfer Not Ready event on this endpoint.
  118. *
  119. * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
  120. * flag is set, it's telling us that as soon as Gadget queues the
  121. * required request, we should kick the transfer here because the
  122. * IRQ we were waiting for is long gone.
  123. */
  124. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  125. unsigned direction;
  126. direction = !!(dep->flags & DWC3_EP0_DIR_IN);
  127. if (dwc->ep0state != EP0_DATA_PHASE) {
  128. dev_WARN(dwc->dev, "Unexpected pending request\n");
  129. return 0;
  130. }
  131. __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
  132. dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
  133. DWC3_EP0_DIR_IN);
  134. return 0;
  135. }
  136. /*
  137. * In case gadget driver asked us to delay the STATUS phase,
  138. * handle it here.
  139. */
  140. if (dwc->delayed_status) {
  141. unsigned direction;
  142. direction = !dwc->ep0_expect_in;
  143. dwc->delayed_status = false;
  144. if (dwc->ep0state == EP0_STATUS_PHASE)
  145. __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
  146. else
  147. dev_dbg(dwc->dev, "too early for delayed status\n");
  148. return 0;
  149. }
  150. /*
  151. * Unfortunately we have uncovered a limitation wrt the Data Phase.
  152. *
  153. * Section 9.4 says we can wait for the XferNotReady(DATA) event to
  154. * come before issueing Start Transfer command, but if we do, we will
  155. * miss situations where the host starts another SETUP phase instead of
  156. * the DATA phase. Such cases happen at least on TD.7.6 of the Link
  157. * Layer Compliance Suite.
  158. *
  159. * The problem surfaces due to the fact that in case of back-to-back
  160. * SETUP packets there will be no XferNotReady(DATA) generated and we
  161. * will be stuck waiting for XferNotReady(DATA) forever.
  162. *
  163. * By looking at tables 9-13 and 9-14 of the Databook, we can see that
  164. * it tells us to start Data Phase right away. It also mentions that if
  165. * we receive a SETUP phase instead of the DATA phase, core will issue
  166. * XferComplete for the DATA phase, before actually initiating it in
  167. * the wire, with the TRB's status set to "SETUP_PENDING". Such status
  168. * can only be used to print some debugging logs, as the core expects
  169. * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
  170. * just so it completes right away, without transferring anything and,
  171. * only then, we can go back to the SETUP phase.
  172. *
  173. * Because of this scenario, SNPS decided to change the programming
  174. * model of control transfers and support on-demand transfers only for
  175. * the STATUS phase. To fix the issue we have now, we will always wait
  176. * for gadget driver to queue the DATA phase's struct usb_request, then
  177. * start it right away.
  178. *
  179. * If we're actually in a 2-stage transfer, we will wait for
  180. * XferNotReady(STATUS).
  181. */
  182. if (dwc->three_stage_setup) {
  183. unsigned direction;
  184. direction = dwc->ep0_expect_in;
  185. dwc->ep0state = EP0_DATA_PHASE;
  186. __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
  187. dep->flags &= ~DWC3_EP0_DIR_IN;
  188. }
  189. return 0;
  190. }
  191. int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
  192. gfp_t gfp_flags)
  193. {
  194. struct dwc3_request *req = to_dwc3_request(request);
  195. struct dwc3_ep *dep = to_dwc3_ep(ep);
  196. struct dwc3 *dwc = dep->dwc;
  197. unsigned long flags;
  198. int ret;
  199. spin_lock_irqsave(&dwc->lock, flags);
  200. if (!dep->endpoint.desc) {
  201. dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
  202. request, dep->name);
  203. ret = -ESHUTDOWN;
  204. goto out;
  205. }
  206. /* we share one TRB for ep0/1 */
  207. if (!list_empty(&dep->request_list)) {
  208. ret = -EBUSY;
  209. goto out;
  210. }
  211. dev_vdbg(dwc->dev, "queueing request %p to %s length %d, state '%s'\n",
  212. request, dep->name, request->length,
  213. dwc3_ep0_state_string(dwc->ep0state));
  214. ret = __dwc3_gadget_ep0_queue(dep, req);
  215. out:
  216. spin_unlock_irqrestore(&dwc->lock, flags);
  217. return ret;
  218. }
  219. static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
  220. {
  221. struct dwc3_ep *dep;
  222. /* reinitialize physical ep1 */
  223. dep = dwc->eps[1];
  224. dep->flags = DWC3_EP_ENABLED;
  225. /* stall is always issued on EP0 */
  226. dep = dwc->eps[0];
  227. __dwc3_gadget_ep_set_halt(dep, 1);
  228. dep->flags = DWC3_EP_ENABLED;
  229. dwc->delayed_status = false;
  230. if (!list_empty(&dep->request_list)) {
  231. struct dwc3_request *req;
  232. req = next_request(&dep->request_list);
  233. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  234. }
  235. dwc->ep0state = EP0_SETUP_PHASE;
  236. dwc3_ep0_out_start(dwc);
  237. }
  238. int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
  239. {
  240. struct dwc3_ep *dep = to_dwc3_ep(ep);
  241. struct dwc3 *dwc = dep->dwc;
  242. dwc3_ep0_stall_and_restart(dwc);
  243. return 0;
  244. }
  245. void dwc3_ep0_out_start(struct dwc3 *dwc)
  246. {
  247. int ret;
  248. ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
  249. DWC3_TRBCTL_CONTROL_SETUP);
  250. WARN_ON(ret < 0);
  251. }
  252. static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
  253. {
  254. struct dwc3_ep *dep;
  255. u32 windex = le16_to_cpu(wIndex_le);
  256. u32 epnum;
  257. epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
  258. if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  259. epnum |= 1;
  260. dep = dwc->eps[epnum];
  261. if (dep->flags & DWC3_EP_ENABLED)
  262. return dep;
  263. return NULL;
  264. }
  265. static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
  266. {
  267. }
  268. /*
  269. * ch 9.4.5
  270. */
  271. static int dwc3_ep0_handle_status(struct dwc3 *dwc,
  272. struct usb_ctrlrequest *ctrl)
  273. {
  274. struct dwc3_ep *dep;
  275. u32 recip;
  276. u32 reg;
  277. u16 usb_status = 0;
  278. __le16 *response_pkt;
  279. recip = ctrl->bRequestType & USB_RECIP_MASK;
  280. switch (recip) {
  281. case USB_RECIP_DEVICE:
  282. /*
  283. * LTM will be set once we know how to set this in HW.
  284. */
  285. usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
  286. if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
  287. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  288. if (reg & DWC3_DCTL_INITU1ENA)
  289. usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
  290. if (reg & DWC3_DCTL_INITU2ENA)
  291. usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
  292. }
  293. break;
  294. case USB_RECIP_INTERFACE:
  295. /*
  296. * Function Remote Wake Capable D0
  297. * Function Remote Wakeup D1
  298. */
  299. break;
  300. case USB_RECIP_ENDPOINT:
  301. dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
  302. if (!dep)
  303. return -EINVAL;
  304. if (dep->flags & DWC3_EP_STALL)
  305. usb_status = 1 << USB_ENDPOINT_HALT;
  306. break;
  307. default:
  308. return -EINVAL;
  309. };
  310. response_pkt = (__le16 *) dwc->setup_buf;
  311. *response_pkt = cpu_to_le16(usb_status);
  312. dep = dwc->eps[0];
  313. dwc->ep0_usb_req.dep = dep;
  314. dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
  315. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  316. dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
  317. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  318. }
  319. static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
  320. struct usb_ctrlrequest *ctrl, int set)
  321. {
  322. struct dwc3_ep *dep;
  323. u32 recip;
  324. u32 wValue;
  325. u32 wIndex;
  326. u32 reg;
  327. int ret;
  328. wValue = le16_to_cpu(ctrl->wValue);
  329. wIndex = le16_to_cpu(ctrl->wIndex);
  330. recip = ctrl->bRequestType & USB_RECIP_MASK;
  331. switch (recip) {
  332. case USB_RECIP_DEVICE:
  333. switch (wValue) {
  334. case USB_DEVICE_REMOTE_WAKEUP:
  335. break;
  336. /*
  337. * 9.4.1 says only only for SS, in AddressState only for
  338. * default control pipe
  339. */
  340. case USB_DEVICE_U1_ENABLE:
  341. if (dwc->dev_state != DWC3_CONFIGURED_STATE)
  342. return -EINVAL;
  343. if (dwc->speed != DWC3_DSTS_SUPERSPEED)
  344. return -EINVAL;
  345. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  346. if (set)
  347. reg |= DWC3_DCTL_INITU1ENA;
  348. else
  349. reg &= ~DWC3_DCTL_INITU1ENA;
  350. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  351. break;
  352. case USB_DEVICE_U2_ENABLE:
  353. if (dwc->dev_state != DWC3_CONFIGURED_STATE)
  354. return -EINVAL;
  355. if (dwc->speed != DWC3_DSTS_SUPERSPEED)
  356. return -EINVAL;
  357. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  358. if (set)
  359. reg |= DWC3_DCTL_INITU2ENA;
  360. else
  361. reg &= ~DWC3_DCTL_INITU2ENA;
  362. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  363. break;
  364. case USB_DEVICE_LTM_ENABLE:
  365. return -EINVAL;
  366. break;
  367. case USB_DEVICE_TEST_MODE:
  368. if ((wIndex & 0xff) != 0)
  369. return -EINVAL;
  370. if (!set)
  371. return -EINVAL;
  372. dwc->test_mode_nr = wIndex >> 8;
  373. dwc->test_mode = true;
  374. break;
  375. default:
  376. return -EINVAL;
  377. }
  378. break;
  379. case USB_RECIP_INTERFACE:
  380. switch (wValue) {
  381. case USB_INTRF_FUNC_SUSPEND:
  382. if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
  383. /* XXX enable Low power suspend */
  384. ;
  385. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
  386. /* XXX enable remote wakeup */
  387. ;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. break;
  393. case USB_RECIP_ENDPOINT:
  394. switch (wValue) {
  395. case USB_ENDPOINT_HALT:
  396. dep = dwc3_wIndex_to_dep(dwc, wIndex);
  397. if (!dep)
  398. return -EINVAL;
  399. ret = __dwc3_gadget_ep_set_halt(dep, set);
  400. if (ret)
  401. return -EINVAL;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. break;
  407. default:
  408. return -EINVAL;
  409. };
  410. return 0;
  411. }
  412. static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  413. {
  414. u32 addr;
  415. u32 reg;
  416. addr = le16_to_cpu(ctrl->wValue);
  417. if (addr > 127) {
  418. dev_dbg(dwc->dev, "invalid device address %d\n", addr);
  419. return -EINVAL;
  420. }
  421. if (dwc->dev_state == DWC3_CONFIGURED_STATE) {
  422. dev_dbg(dwc->dev, "trying to set address when configured\n");
  423. return -EINVAL;
  424. }
  425. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  426. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  427. reg |= DWC3_DCFG_DEVADDR(addr);
  428. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  429. if (addr)
  430. dwc->dev_state = DWC3_ADDRESS_STATE;
  431. else
  432. dwc->dev_state = DWC3_DEFAULT_STATE;
  433. return 0;
  434. }
  435. static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  436. {
  437. int ret;
  438. spin_unlock(&dwc->lock);
  439. ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
  440. spin_lock(&dwc->lock);
  441. return ret;
  442. }
  443. static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  444. {
  445. u32 cfg;
  446. int ret;
  447. u32 reg;
  448. dwc->start_config_issued = false;
  449. cfg = le16_to_cpu(ctrl->wValue);
  450. switch (dwc->dev_state) {
  451. case DWC3_DEFAULT_STATE:
  452. return -EINVAL;
  453. break;
  454. case DWC3_ADDRESS_STATE:
  455. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  456. /* if the cfg matches and the cfg is non zero */
  457. if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
  458. dwc->dev_state = DWC3_CONFIGURED_STATE;
  459. /*
  460. * Enable transition to U1/U2 state when
  461. * nothing is pending from application.
  462. */
  463. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  464. reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
  465. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  466. dwc->resize_fifos = true;
  467. dev_dbg(dwc->dev, "resize fifos flag SET\n");
  468. }
  469. break;
  470. case DWC3_CONFIGURED_STATE:
  471. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  472. if (!cfg)
  473. dwc->dev_state = DWC3_ADDRESS_STATE;
  474. break;
  475. default:
  476. ret = -EINVAL;
  477. }
  478. return ret;
  479. }
  480. static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
  481. {
  482. struct dwc3_ep *dep = to_dwc3_ep(ep);
  483. struct dwc3 *dwc = dep->dwc;
  484. u32 param = 0;
  485. u32 reg;
  486. struct timing {
  487. u8 u1sel;
  488. u8 u1pel;
  489. u16 u2sel;
  490. u16 u2pel;
  491. } __packed timing;
  492. int ret;
  493. memcpy(&timing, req->buf, sizeof(timing));
  494. dwc->u1sel = timing.u1sel;
  495. dwc->u1pel = timing.u1pel;
  496. dwc->u2sel = le16_to_cpu(timing.u2sel);
  497. dwc->u2pel = le16_to_cpu(timing.u2pel);
  498. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  499. if (reg & DWC3_DCTL_INITU2ENA)
  500. param = dwc->u2pel;
  501. if (reg & DWC3_DCTL_INITU1ENA)
  502. param = dwc->u1pel;
  503. /*
  504. * According to Synopsys Databook, if parameter is
  505. * greater than 125, a value of zero should be
  506. * programmed in the register.
  507. */
  508. if (param > 125)
  509. param = 0;
  510. /* now that we have the time, issue DGCMD Set Sel */
  511. ret = dwc3_send_gadget_generic_command(dwc,
  512. DWC3_DGCMD_SET_PERIODIC_PAR, param);
  513. WARN_ON(ret < 0);
  514. }
  515. static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  516. {
  517. struct dwc3_ep *dep;
  518. u16 wLength;
  519. u16 wValue;
  520. if (dwc->dev_state == DWC3_DEFAULT_STATE)
  521. return -EINVAL;
  522. wValue = le16_to_cpu(ctrl->wValue);
  523. wLength = le16_to_cpu(ctrl->wLength);
  524. if (wLength != 6) {
  525. dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
  526. wLength);
  527. return -EINVAL;
  528. }
  529. /*
  530. * To handle Set SEL we need to receive 6 bytes from Host. So let's
  531. * queue a usb_request for 6 bytes.
  532. *
  533. * Remember, though, this controller can't handle non-wMaxPacketSize
  534. * aligned transfers on the OUT direction, so we queue a request for
  535. * wMaxPacketSize instead.
  536. */
  537. dep = dwc->eps[0];
  538. dwc->ep0_usb_req.dep = dep;
  539. dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
  540. dwc->ep0_usb_req.request.buf = dwc->setup_buf;
  541. dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
  542. return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
  543. }
  544. static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  545. {
  546. u16 wLength;
  547. u16 wValue;
  548. u16 wIndex;
  549. wValue = le16_to_cpu(ctrl->wValue);
  550. wLength = le16_to_cpu(ctrl->wLength);
  551. wIndex = le16_to_cpu(ctrl->wIndex);
  552. if (wIndex || wLength)
  553. return -EINVAL;
  554. /*
  555. * REVISIT It's unclear from Databook what to do with this
  556. * value. For now, just cache it.
  557. */
  558. dwc->isoch_delay = wValue;
  559. return 0;
  560. }
  561. static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
  562. {
  563. int ret;
  564. switch (ctrl->bRequest) {
  565. case USB_REQ_GET_STATUS:
  566. dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS\n");
  567. ret = dwc3_ep0_handle_status(dwc, ctrl);
  568. break;
  569. case USB_REQ_CLEAR_FEATURE:
  570. dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE\n");
  571. ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
  572. break;
  573. case USB_REQ_SET_FEATURE:
  574. dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE\n");
  575. ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
  576. break;
  577. case USB_REQ_SET_ADDRESS:
  578. dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS\n");
  579. ret = dwc3_ep0_set_address(dwc, ctrl);
  580. break;
  581. case USB_REQ_SET_CONFIGURATION:
  582. dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION\n");
  583. ret = dwc3_ep0_set_config(dwc, ctrl);
  584. break;
  585. case USB_REQ_SET_SEL:
  586. dev_vdbg(dwc->dev, "USB_REQ_SET_SEL\n");
  587. ret = dwc3_ep0_set_sel(dwc, ctrl);
  588. break;
  589. case USB_REQ_SET_ISOCH_DELAY:
  590. dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
  591. ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
  592. break;
  593. default:
  594. dev_vdbg(dwc->dev, "Forwarding to gadget driver\n");
  595. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  596. break;
  597. };
  598. return ret;
  599. }
  600. static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
  601. const struct dwc3_event_depevt *event)
  602. {
  603. struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
  604. int ret = -EINVAL;
  605. u32 len;
  606. if (!dwc->gadget_driver)
  607. goto out;
  608. len = le16_to_cpu(ctrl->wLength);
  609. if (!len) {
  610. dwc->three_stage_setup = false;
  611. dwc->ep0_expect_in = false;
  612. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  613. } else {
  614. dwc->three_stage_setup = true;
  615. dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
  616. dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
  617. }
  618. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  619. ret = dwc3_ep0_std_request(dwc, ctrl);
  620. else
  621. ret = dwc3_ep0_delegate_req(dwc, ctrl);
  622. if (ret == USB_GADGET_DELAYED_STATUS)
  623. dwc->delayed_status = true;
  624. out:
  625. if (ret < 0)
  626. dwc3_ep0_stall_and_restart(dwc);
  627. }
  628. static void dwc3_ep0_complete_data(struct dwc3 *dwc,
  629. const struct dwc3_event_depevt *event)
  630. {
  631. struct dwc3_request *r = NULL;
  632. struct usb_request *ur;
  633. struct dwc3_trb *trb;
  634. struct dwc3_ep *ep0;
  635. u32 transferred;
  636. u32 status;
  637. u32 length;
  638. u8 epnum;
  639. epnum = event->endpoint_number;
  640. ep0 = dwc->eps[0];
  641. dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
  642. r = next_request(&ep0->request_list);
  643. ur = &r->request;
  644. trb = dwc->ep0_trb;
  645. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  646. if (status == DWC3_TRBSTS_SETUP_PENDING) {
  647. dev_dbg(dwc->dev, "Setup Pending received\n");
  648. if (r)
  649. dwc3_gadget_giveback(ep0, r, -ECONNRESET);
  650. return;
  651. }
  652. length = trb->size & DWC3_TRB_SIZE_MASK;
  653. if (dwc->ep0_bounced) {
  654. unsigned transfer_size = ur->length;
  655. unsigned maxp = ep0->endpoint.maxpacket;
  656. transfer_size += (maxp - (transfer_size % maxp));
  657. transferred = min_t(u32, ur->length,
  658. transfer_size - length);
  659. memcpy(ur->buf, dwc->ep0_bounce, transferred);
  660. } else {
  661. transferred = ur->length - length;
  662. }
  663. ur->actual += transferred;
  664. if ((epnum & 1) && ur->actual < ur->length) {
  665. /* for some reason we did not get everything out */
  666. dwc3_ep0_stall_and_restart(dwc);
  667. } else {
  668. /*
  669. * handle the case where we have to send a zero packet. This
  670. * seems to be case when req.length > maxpacket. Could it be?
  671. */
  672. if (r)
  673. dwc3_gadget_giveback(ep0, r, 0);
  674. }
  675. }
  676. static void dwc3_ep0_complete_status(struct dwc3 *dwc,
  677. const struct dwc3_event_depevt *event)
  678. {
  679. struct dwc3_request *r;
  680. struct dwc3_ep *dep;
  681. struct dwc3_trb *trb;
  682. u32 status;
  683. dep = dwc->eps[0];
  684. trb = dwc->ep0_trb;
  685. if (!list_empty(&dep->request_list)) {
  686. r = next_request(&dep->request_list);
  687. dwc3_gadget_giveback(dep, r, 0);
  688. }
  689. if (dwc->test_mode) {
  690. int ret;
  691. ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
  692. if (ret < 0) {
  693. dev_dbg(dwc->dev, "Invalid Test #%d\n",
  694. dwc->test_mode_nr);
  695. dwc3_ep0_stall_and_restart(dwc);
  696. return;
  697. }
  698. }
  699. status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  700. if (status == DWC3_TRBSTS_SETUP_PENDING)
  701. dev_dbg(dwc->dev, "Setup Pending received\n");
  702. dwc->ep0state = EP0_SETUP_PHASE;
  703. dwc3_ep0_out_start(dwc);
  704. }
  705. static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
  706. const struct dwc3_event_depevt *event)
  707. {
  708. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  709. dep->flags &= ~DWC3_EP_BUSY;
  710. dep->resource_index = 0;
  711. dwc->setup_packet_pending = false;
  712. switch (dwc->ep0state) {
  713. case EP0_SETUP_PHASE:
  714. dev_vdbg(dwc->dev, "Inspecting Setup Bytes\n");
  715. dwc3_ep0_inspect_setup(dwc, event);
  716. break;
  717. case EP0_DATA_PHASE:
  718. dev_vdbg(dwc->dev, "Data Phase\n");
  719. dwc3_ep0_complete_data(dwc, event);
  720. break;
  721. case EP0_STATUS_PHASE:
  722. dev_vdbg(dwc->dev, "Status Phase\n");
  723. dwc3_ep0_complete_status(dwc, event);
  724. break;
  725. default:
  726. WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
  727. }
  728. }
  729. static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
  730. struct dwc3_ep *dep, struct dwc3_request *req)
  731. {
  732. int ret;
  733. req->direction = !!dep->number;
  734. if (req->request.length == 0) {
  735. ret = dwc3_ep0_start_trans(dwc, dep->number,
  736. dwc->ctrl_req_addr, 0,
  737. DWC3_TRBCTL_CONTROL_DATA);
  738. } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
  739. && (dep->number == 0)) {
  740. u32 transfer_size;
  741. u32 maxpacket;
  742. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  743. dep->number);
  744. if (ret) {
  745. dev_dbg(dwc->dev, "failed to map request\n");
  746. return;
  747. }
  748. WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
  749. maxpacket = dep->endpoint.maxpacket;
  750. transfer_size = roundup(req->request.length, maxpacket);
  751. dwc->ep0_bounced = true;
  752. /*
  753. * REVISIT in case request length is bigger than
  754. * DWC3_EP0_BOUNCE_SIZE we will need two chained
  755. * TRBs to handle the transfer.
  756. */
  757. ret = dwc3_ep0_start_trans(dwc, dep->number,
  758. dwc->ep0_bounce_addr, transfer_size,
  759. DWC3_TRBCTL_CONTROL_DATA);
  760. } else {
  761. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  762. dep->number);
  763. if (ret) {
  764. dev_dbg(dwc->dev, "failed to map request\n");
  765. return;
  766. }
  767. ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
  768. req->request.length, DWC3_TRBCTL_CONTROL_DATA);
  769. }
  770. WARN_ON(ret < 0);
  771. }
  772. static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
  773. {
  774. struct dwc3 *dwc = dep->dwc;
  775. u32 type;
  776. type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
  777. : DWC3_TRBCTL_CONTROL_STATUS2;
  778. return dwc3_ep0_start_trans(dwc, dep->number,
  779. dwc->ctrl_req_addr, 0, type);
  780. }
  781. static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
  782. {
  783. if (dwc->resize_fifos) {
  784. dev_dbg(dwc->dev, "starting to resize fifos\n");
  785. dwc3_gadget_resize_tx_fifos(dwc);
  786. dwc->resize_fifos = 0;
  787. }
  788. WARN_ON(dwc3_ep0_start_control_status(dep));
  789. }
  790. static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
  791. const struct dwc3_event_depevt *event)
  792. {
  793. struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
  794. __dwc3_ep0_do_control_status(dwc, dep);
  795. }
  796. static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
  797. {
  798. struct dwc3_gadget_ep_cmd_params params;
  799. u32 cmd;
  800. int ret;
  801. if (!dep->resource_index)
  802. return;
  803. cmd = DWC3_DEPCMD_ENDTRANSFER;
  804. cmd |= DWC3_DEPCMD_CMDIOC;
  805. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  806. memset(&params, 0, sizeof(params));
  807. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  808. WARN_ON_ONCE(ret);
  809. dep->resource_index = 0;
  810. }
  811. static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
  812. const struct dwc3_event_depevt *event)
  813. {
  814. dwc->setup_packet_pending = true;
  815. switch (event->status) {
  816. case DEPEVT_STATUS_CONTROL_DATA:
  817. dev_vdbg(dwc->dev, "Control Data\n");
  818. /*
  819. * We already have a DATA transfer in the controller's cache,
  820. * if we receive a XferNotReady(DATA) we will ignore it, unless
  821. * it's for the wrong direction.
  822. *
  823. * In that case, we must issue END_TRANSFER command to the Data
  824. * Phase we already have started and issue SetStall on the
  825. * control endpoint.
  826. */
  827. if (dwc->ep0_expect_in != event->endpoint_number) {
  828. struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
  829. dev_vdbg(dwc->dev, "Wrong direction for Data phase\n");
  830. dwc3_ep0_end_control_data(dwc, dep);
  831. dwc3_ep0_stall_and_restart(dwc);
  832. return;
  833. }
  834. break;
  835. case DEPEVT_STATUS_CONTROL_STATUS:
  836. if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
  837. return;
  838. dev_vdbg(dwc->dev, "Control Status\n");
  839. dwc->ep0state = EP0_STATUS_PHASE;
  840. if (dwc->delayed_status) {
  841. WARN_ON_ONCE(event->endpoint_number != 1);
  842. dev_vdbg(dwc->dev, "Mass Storage delayed status\n");
  843. return;
  844. }
  845. dwc3_ep0_do_control_status(dwc, event);
  846. }
  847. }
  848. void dwc3_ep0_interrupt(struct dwc3 *dwc,
  849. const struct dwc3_event_depevt *event)
  850. {
  851. u8 epnum = event->endpoint_number;
  852. dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'\n",
  853. dwc3_ep_event_string(event->endpoint_event),
  854. epnum >> 1, (epnum & 1) ? "in" : "out",
  855. dwc3_ep0_state_string(dwc->ep0state));
  856. switch (event->endpoint_event) {
  857. case DWC3_DEPEVT_XFERCOMPLETE:
  858. dwc3_ep0_xfer_complete(dwc, event);
  859. break;
  860. case DWC3_DEPEVT_XFERNOTREADY:
  861. dwc3_ep0_xfernotready(dwc, event);
  862. break;
  863. case DWC3_DEPEVT_XFERINPROGRESS:
  864. case DWC3_DEPEVT_RXTXFIFOEVT:
  865. case DWC3_DEPEVT_STREAMEVT:
  866. case DWC3_DEPEVT_EPCMDCMPLT:
  867. break;
  868. }
  869. }