mtu3_gadget.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mtu3_gadget.c - MediaTek usb3 DRD peripheral support
  4. *
  5. * Copyright (C) 2016 MediaTek Inc.
  6. *
  7. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  8. */
  9. #include "mtu3.h"
  10. void mtu3_req_complete(struct mtu3_ep *mep,
  11. struct usb_request *req, int status)
  12. __releases(mep->mtu->lock)
  13. __acquires(mep->mtu->lock)
  14. {
  15. struct mtu3_request *mreq;
  16. struct mtu3 *mtu;
  17. int busy = mep->busy;
  18. mreq = to_mtu3_request(req);
  19. list_del(&mreq->list);
  20. if (mreq->request.status == -EINPROGRESS)
  21. mreq->request.status = status;
  22. mtu = mreq->mtu;
  23. mep->busy = 1;
  24. spin_unlock(&mtu->lock);
  25. /* ep0 makes use of PIO, needn't unmap it */
  26. if (mep->epnum)
  27. usb_gadget_unmap_request(&mtu->g, req, mep->is_in);
  28. dev_dbg(mtu->dev, "%s complete req: %p, sts %d, %d/%d\n", mep->name,
  29. req, req->status, mreq->request.actual, mreq->request.length);
  30. usb_gadget_giveback_request(&mep->ep, &mreq->request);
  31. spin_lock(&mtu->lock);
  32. mep->busy = busy;
  33. }
  34. static void nuke(struct mtu3_ep *mep, const int status)
  35. {
  36. struct mtu3_request *mreq = NULL;
  37. mep->busy = 1;
  38. if (list_empty(&mep->req_list))
  39. return;
  40. dev_dbg(mep->mtu->dev, "abort %s's req: sts %d\n", mep->name, status);
  41. /* exclude EP0 */
  42. if (mep->epnum)
  43. mtu3_qmu_flush(mep);
  44. while (!list_empty(&mep->req_list)) {
  45. mreq = list_first_entry(&mep->req_list,
  46. struct mtu3_request, list);
  47. mtu3_req_complete(mep, &mreq->request, status);
  48. }
  49. }
  50. static int mtu3_ep_enable(struct mtu3_ep *mep)
  51. {
  52. const struct usb_endpoint_descriptor *desc;
  53. const struct usb_ss_ep_comp_descriptor *comp_desc;
  54. struct mtu3 *mtu = mep->mtu;
  55. u32 interval = 0;
  56. u32 mult = 0;
  57. u32 burst = 0;
  58. int max_packet;
  59. int ret;
  60. desc = mep->desc;
  61. comp_desc = mep->comp_desc;
  62. mep->type = usb_endpoint_type(desc);
  63. max_packet = usb_endpoint_maxp(desc);
  64. mep->maxp = max_packet & GENMASK(10, 0);
  65. switch (mtu->g.speed) {
  66. case USB_SPEED_SUPER:
  67. case USB_SPEED_SUPER_PLUS:
  68. if (usb_endpoint_xfer_int(desc) ||
  69. usb_endpoint_xfer_isoc(desc)) {
  70. interval = desc->bInterval;
  71. interval = clamp_val(interval, 1, 16) - 1;
  72. if (usb_endpoint_xfer_isoc(desc) && comp_desc)
  73. mult = comp_desc->bmAttributes;
  74. }
  75. if (comp_desc)
  76. burst = comp_desc->bMaxBurst;
  77. break;
  78. case USB_SPEED_HIGH:
  79. if (usb_endpoint_xfer_isoc(desc) ||
  80. usb_endpoint_xfer_int(desc)) {
  81. interval = desc->bInterval;
  82. interval = clamp_val(interval, 1, 16) - 1;
  83. burst = (max_packet & GENMASK(12, 11)) >> 11;
  84. }
  85. break;
  86. default:
  87. break; /*others are ignored */
  88. }
  89. dev_dbg(mtu->dev, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n",
  90. __func__, mep->maxp, interval, burst, mult);
  91. mep->ep.maxpacket = mep->maxp;
  92. mep->ep.desc = desc;
  93. mep->ep.comp_desc = comp_desc;
  94. /* slot mainly affects bulk/isoc transfer, so ignore int */
  95. mep->slot = usb_endpoint_xfer_int(desc) ? 0 : mtu->slot;
  96. ret = mtu3_config_ep(mtu, mep, interval, burst, mult);
  97. if (ret < 0)
  98. return ret;
  99. ret = mtu3_gpd_ring_alloc(mep);
  100. if (ret < 0) {
  101. mtu3_deconfig_ep(mtu, mep);
  102. return ret;
  103. }
  104. mtu3_qmu_start(mep);
  105. return 0;
  106. }
  107. static int mtu3_ep_disable(struct mtu3_ep *mep)
  108. {
  109. struct mtu3 *mtu = mep->mtu;
  110. mtu3_qmu_stop(mep);
  111. /* abort all pending requests */
  112. nuke(mep, -ESHUTDOWN);
  113. mtu3_deconfig_ep(mtu, mep);
  114. mtu3_gpd_ring_free(mep);
  115. mep->desc = NULL;
  116. mep->ep.desc = NULL;
  117. mep->comp_desc = NULL;
  118. mep->type = 0;
  119. mep->flags = 0;
  120. return 0;
  121. }
  122. static int mtu3_gadget_ep_enable(struct usb_ep *ep,
  123. const struct usb_endpoint_descriptor *desc)
  124. {
  125. struct mtu3_ep *mep;
  126. struct mtu3 *mtu;
  127. unsigned long flags;
  128. int ret = -EINVAL;
  129. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  130. pr_debug("%s invalid parameters\n", __func__);
  131. return -EINVAL;
  132. }
  133. if (!desc->wMaxPacketSize) {
  134. pr_debug("%s missing wMaxPacketSize\n", __func__);
  135. return -EINVAL;
  136. }
  137. mep = to_mtu3_ep(ep);
  138. mtu = mep->mtu;
  139. /* check ep number and direction against endpoint */
  140. if (usb_endpoint_num(desc) != mep->epnum)
  141. return -EINVAL;
  142. if (!!usb_endpoint_dir_in(desc) ^ !!mep->is_in)
  143. return -EINVAL;
  144. dev_dbg(mtu->dev, "%s %s\n", __func__, ep->name);
  145. if (mep->flags & MTU3_EP_ENABLED) {
  146. dev_WARN_ONCE(mtu->dev, true, "%s is already enabled\n",
  147. mep->name);
  148. return 0;
  149. }
  150. spin_lock_irqsave(&mtu->lock, flags);
  151. mep->desc = desc;
  152. mep->comp_desc = ep->comp_desc;
  153. ret = mtu3_ep_enable(mep);
  154. if (ret)
  155. goto error;
  156. mep->busy = 0;
  157. mep->wedged = 0;
  158. mep->flags |= MTU3_EP_ENABLED;
  159. mtu->active_ep++;
  160. error:
  161. spin_unlock_irqrestore(&mtu->lock, flags);
  162. dev_dbg(mtu->dev, "%s active_ep=%d\n", __func__, mtu->active_ep);
  163. return ret;
  164. }
  165. static int mtu3_gadget_ep_disable(struct usb_ep *ep)
  166. {
  167. struct mtu3_ep *mep = to_mtu3_ep(ep);
  168. struct mtu3 *mtu = mep->mtu;
  169. unsigned long flags;
  170. dev_dbg(mtu->dev, "%s %s\n", __func__, mep->name);
  171. if (!(mep->flags & MTU3_EP_ENABLED)) {
  172. dev_warn(mtu->dev, "%s is already disabled\n", mep->name);
  173. return 0;
  174. }
  175. spin_lock_irqsave(&mtu->lock, flags);
  176. mtu3_ep_disable(mep);
  177. mep->flags &= ~MTU3_EP_ENABLED;
  178. mtu->active_ep--;
  179. spin_unlock_irqrestore(&(mtu->lock), flags);
  180. dev_dbg(mtu->dev, "%s active_ep=%d, mtu3 is_active=%d\n",
  181. __func__, mtu->active_ep, mtu->is_active);
  182. return 0;
  183. }
  184. struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  185. {
  186. struct mtu3_ep *mep = to_mtu3_ep(ep);
  187. struct mtu3_request *mreq;
  188. mreq = kzalloc(sizeof(*mreq), gfp_flags);
  189. if (!mreq)
  190. return NULL;
  191. mreq->request.dma = DMA_ADDR_INVALID;
  192. mreq->epnum = mep->epnum;
  193. mreq->mep = mep;
  194. return &mreq->request;
  195. }
  196. void mtu3_free_request(struct usb_ep *ep, struct usb_request *req)
  197. {
  198. kfree(to_mtu3_request(req));
  199. }
  200. static int mtu3_gadget_queue(struct usb_ep *ep,
  201. struct usb_request *req, gfp_t gfp_flags)
  202. {
  203. struct mtu3_ep *mep;
  204. struct mtu3_request *mreq;
  205. struct mtu3 *mtu;
  206. unsigned long flags;
  207. int ret = 0;
  208. if (!ep || !req)
  209. return -EINVAL;
  210. if (!req->buf)
  211. return -ENODATA;
  212. mep = to_mtu3_ep(ep);
  213. mtu = mep->mtu;
  214. mreq = to_mtu3_request(req);
  215. mreq->mtu = mtu;
  216. if (mreq->mep != mep)
  217. return -EINVAL;
  218. dev_dbg(mtu->dev, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n",
  219. __func__, mep->is_in ? "TX" : "RX", mreq->epnum, ep->name,
  220. mreq, ep->maxpacket, mreq->request.length);
  221. if (req->length > GPD_BUF_SIZE) {
  222. dev_warn(mtu->dev,
  223. "req length > supported MAX:%d requested:%d\n",
  224. GPD_BUF_SIZE, req->length);
  225. return -EOPNOTSUPP;
  226. }
  227. /* don't queue if the ep is down */
  228. if (!mep->desc) {
  229. dev_dbg(mtu->dev, "req=%p queued to %s while it's disabled\n",
  230. req, ep->name);
  231. return -ESHUTDOWN;
  232. }
  233. mreq->request.actual = 0;
  234. mreq->request.status = -EINPROGRESS;
  235. ret = usb_gadget_map_request(&mtu->g, req, mep->is_in);
  236. if (ret) {
  237. dev_err(mtu->dev, "dma mapping failed\n");
  238. return ret;
  239. }
  240. spin_lock_irqsave(&mtu->lock, flags);
  241. if (mtu3_prepare_transfer(mep)) {
  242. ret = -EAGAIN;
  243. goto error;
  244. }
  245. list_add_tail(&mreq->list, &mep->req_list);
  246. mtu3_insert_gpd(mep, mreq);
  247. mtu3_qmu_resume(mep);
  248. error:
  249. spin_unlock_irqrestore(&mtu->lock, flags);
  250. return ret;
  251. }
  252. static int mtu3_gadget_dequeue(struct usb_ep *ep, struct usb_request *req)
  253. {
  254. struct mtu3_ep *mep = to_mtu3_ep(ep);
  255. struct mtu3_request *mreq = to_mtu3_request(req);
  256. struct mtu3_request *r;
  257. unsigned long flags;
  258. int ret = 0;
  259. struct mtu3 *mtu = mep->mtu;
  260. if (!ep || !req || mreq->mep != mep)
  261. return -EINVAL;
  262. dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req);
  263. spin_lock_irqsave(&mtu->lock, flags);
  264. list_for_each_entry(r, &mep->req_list, list) {
  265. if (r == mreq)
  266. break;
  267. }
  268. if (r != mreq) {
  269. dev_dbg(mtu->dev, "req=%p not queued to %s\n", req, ep->name);
  270. ret = -EINVAL;
  271. goto done;
  272. }
  273. mtu3_qmu_flush(mep); /* REVISIT: set BPS ?? */
  274. mtu3_req_complete(mep, req, -ECONNRESET);
  275. mtu3_qmu_start(mep);
  276. done:
  277. spin_unlock_irqrestore(&mtu->lock, flags);
  278. return ret;
  279. }
  280. /*
  281. * Set or clear the halt bit of an EP.
  282. * A halted EP won't TX/RX any data but will queue requests.
  283. */
  284. static int mtu3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  285. {
  286. struct mtu3_ep *mep = to_mtu3_ep(ep);
  287. struct mtu3 *mtu = mep->mtu;
  288. struct mtu3_request *mreq;
  289. unsigned long flags;
  290. int ret = 0;
  291. if (!ep)
  292. return -EINVAL;
  293. dev_dbg(mtu->dev, "%s : %s...", __func__, ep->name);
  294. spin_lock_irqsave(&mtu->lock, flags);
  295. if (mep->type == USB_ENDPOINT_XFER_ISOC) {
  296. ret = -EINVAL;
  297. goto done;
  298. }
  299. mreq = next_request(mep);
  300. if (value) {
  301. /*
  302. * If there is not request for TX-EP, QMU will not transfer
  303. * data to TX-FIFO, so no need check whether TX-FIFO
  304. * holds bytes or not here
  305. */
  306. if (mreq) {
  307. dev_dbg(mtu->dev, "req in progress, cannot halt %s\n",
  308. ep->name);
  309. ret = -EAGAIN;
  310. goto done;
  311. }
  312. } else {
  313. mep->wedged = 0;
  314. }
  315. dev_dbg(mtu->dev, "%s %s stall\n", ep->name, value ? "set" : "clear");
  316. mtu3_ep_stall_set(mep, value);
  317. done:
  318. spin_unlock_irqrestore(&mtu->lock, flags);
  319. return ret;
  320. }
  321. /* Sets the halt feature with the clear requests ignored */
  322. static int mtu3_gadget_ep_set_wedge(struct usb_ep *ep)
  323. {
  324. struct mtu3_ep *mep = to_mtu3_ep(ep);
  325. if (!ep)
  326. return -EINVAL;
  327. mep->wedged = 1;
  328. return usb_ep_set_halt(ep);
  329. }
  330. static const struct usb_ep_ops mtu3_ep_ops = {
  331. .enable = mtu3_gadget_ep_enable,
  332. .disable = mtu3_gadget_ep_disable,
  333. .alloc_request = mtu3_alloc_request,
  334. .free_request = mtu3_free_request,
  335. .queue = mtu3_gadget_queue,
  336. .dequeue = mtu3_gadget_dequeue,
  337. .set_halt = mtu3_gadget_ep_set_halt,
  338. .set_wedge = mtu3_gadget_ep_set_wedge,
  339. };
  340. static int mtu3_gadget_get_frame(struct usb_gadget *gadget)
  341. {
  342. struct mtu3 *mtu = gadget_to_mtu3(gadget);
  343. return (int)mtu3_readl(mtu->mac_base, U3D_USB20_FRAME_NUM);
  344. }
  345. static int mtu3_gadget_wakeup(struct usb_gadget *gadget)
  346. {
  347. struct mtu3 *mtu = gadget_to_mtu3(gadget);
  348. unsigned long flags;
  349. dev_dbg(mtu->dev, "%s\n", __func__);
  350. /* remote wakeup feature is not enabled by host */
  351. if (!mtu->may_wakeup)
  352. return -EOPNOTSUPP;
  353. spin_lock_irqsave(&mtu->lock, flags);
  354. if (mtu->g.speed >= USB_SPEED_SUPER) {
  355. mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT);
  356. } else {
  357. mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
  358. spin_unlock_irqrestore(&mtu->lock, flags);
  359. usleep_range(10000, 11000);
  360. spin_lock_irqsave(&mtu->lock, flags);
  361. mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME);
  362. }
  363. spin_unlock_irqrestore(&mtu->lock, flags);
  364. return 0;
  365. }
  366. static int mtu3_gadget_set_self_powered(struct usb_gadget *gadget,
  367. int is_selfpowered)
  368. {
  369. struct mtu3 *mtu = gadget_to_mtu3(gadget);
  370. mtu->is_self_powered = !!is_selfpowered;
  371. return 0;
  372. }
  373. static int mtu3_gadget_pullup(struct usb_gadget *gadget, int is_on)
  374. {
  375. struct mtu3 *mtu = gadget_to_mtu3(gadget);
  376. unsigned long flags;
  377. dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__,
  378. is_on ? "on" : "off", mtu->is_active ? "" : "in");
  379. /* we'd rather not pullup unless the device is active. */
  380. spin_lock_irqsave(&mtu->lock, flags);
  381. is_on = !!is_on;
  382. if (!mtu->is_active) {
  383. /* save it for mtu3_start() to process the request */
  384. mtu->softconnect = is_on;
  385. } else if (is_on != mtu->softconnect) {
  386. mtu->softconnect = is_on;
  387. mtu3_dev_on_off(mtu, is_on);
  388. }
  389. spin_unlock_irqrestore(&mtu->lock, flags);
  390. return 0;
  391. }
  392. static int mtu3_gadget_start(struct usb_gadget *gadget,
  393. struct usb_gadget_driver *driver)
  394. {
  395. struct mtu3 *mtu = gadget_to_mtu3(gadget);
  396. unsigned long flags;
  397. if (mtu->gadget_driver) {
  398. dev_err(mtu->dev, "%s is already bound to %s\n",
  399. mtu->g.name, mtu->gadget_driver->driver.name);
  400. return -EBUSY;
  401. }
  402. dev_dbg(mtu->dev, "bind driver %s\n", driver->function);
  403. spin_lock_irqsave(&mtu->lock, flags);
  404. mtu->softconnect = 0;
  405. mtu->gadget_driver = driver;
  406. if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
  407. mtu3_start(mtu);
  408. spin_unlock_irqrestore(&mtu->lock, flags);
  409. return 0;
  410. }
  411. static void stop_activity(struct mtu3 *mtu)
  412. {
  413. struct usb_gadget_driver *driver = mtu->gadget_driver;
  414. int i;
  415. /* don't disconnect if it's not connected */
  416. if (mtu->g.speed == USB_SPEED_UNKNOWN)
  417. driver = NULL;
  418. else
  419. mtu->g.speed = USB_SPEED_UNKNOWN;
  420. /* deactivate the hardware */
  421. if (mtu->softconnect) {
  422. mtu->softconnect = 0;
  423. mtu3_dev_on_off(mtu, 0);
  424. }
  425. /*
  426. * killing any outstanding requests will quiesce the driver;
  427. * then report disconnect
  428. */
  429. nuke(mtu->ep0, -ESHUTDOWN);
  430. for (i = 1; i < mtu->num_eps; i++) {
  431. nuke(mtu->in_eps + i, -ESHUTDOWN);
  432. nuke(mtu->out_eps + i, -ESHUTDOWN);
  433. }
  434. if (driver) {
  435. spin_unlock(&mtu->lock);
  436. driver->disconnect(&mtu->g);
  437. spin_lock(&mtu->lock);
  438. }
  439. }
  440. static int mtu3_gadget_stop(struct usb_gadget *g)
  441. {
  442. struct mtu3 *mtu = gadget_to_mtu3(g);
  443. unsigned long flags;
  444. dev_dbg(mtu->dev, "%s\n", __func__);
  445. spin_lock_irqsave(&mtu->lock, flags);
  446. stop_activity(mtu);
  447. mtu->gadget_driver = NULL;
  448. if (mtu->ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
  449. mtu3_stop(mtu);
  450. spin_unlock_irqrestore(&mtu->lock, flags);
  451. return 0;
  452. }
  453. static const struct usb_gadget_ops mtu3_gadget_ops = {
  454. .get_frame = mtu3_gadget_get_frame,
  455. .wakeup = mtu3_gadget_wakeup,
  456. .set_selfpowered = mtu3_gadget_set_self_powered,
  457. .pullup = mtu3_gadget_pullup,
  458. .udc_start = mtu3_gadget_start,
  459. .udc_stop = mtu3_gadget_stop,
  460. };
  461. static void mtu3_state_reset(struct mtu3 *mtu)
  462. {
  463. mtu->address = 0;
  464. mtu->ep0_state = MU3D_EP0_STATE_SETUP;
  465. mtu->may_wakeup = 0;
  466. mtu->u1_enable = 0;
  467. mtu->u2_enable = 0;
  468. mtu->delayed_status = false;
  469. mtu->test_mode = false;
  470. }
  471. static void init_hw_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
  472. u32 epnum, u32 is_in)
  473. {
  474. mep->epnum = epnum;
  475. mep->mtu = mtu;
  476. mep->is_in = is_in;
  477. INIT_LIST_HEAD(&mep->req_list);
  478. sprintf(mep->name, "ep%d%s", epnum,
  479. !epnum ? "" : (is_in ? "in" : "out"));
  480. mep->ep.name = mep->name;
  481. INIT_LIST_HEAD(&mep->ep.ep_list);
  482. /* initialize maxpacket as SS */
  483. if (!epnum) {
  484. usb_ep_set_maxpacket_limit(&mep->ep, 512);
  485. mep->ep.caps.type_control = true;
  486. mep->ep.ops = &mtu3_ep0_ops;
  487. mtu->g.ep0 = &mep->ep;
  488. } else {
  489. usb_ep_set_maxpacket_limit(&mep->ep, 1024);
  490. mep->ep.caps.type_iso = true;
  491. mep->ep.caps.type_bulk = true;
  492. mep->ep.caps.type_int = true;
  493. mep->ep.ops = &mtu3_ep_ops;
  494. list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list);
  495. }
  496. dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name,
  497. mep->ep.maxpacket);
  498. if (!epnum) {
  499. mep->ep.caps.dir_in = true;
  500. mep->ep.caps.dir_out = true;
  501. } else if (is_in) {
  502. mep->ep.caps.dir_in = true;
  503. } else {
  504. mep->ep.caps.dir_out = true;
  505. }
  506. }
  507. static void mtu3_gadget_init_eps(struct mtu3 *mtu)
  508. {
  509. u8 epnum;
  510. /* initialize endpoint list just once */
  511. INIT_LIST_HEAD(&(mtu->g.ep_list));
  512. dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n",
  513. __func__, mtu->num_eps);
  514. init_hw_ep(mtu, mtu->ep0, 0, 0);
  515. for (epnum = 1; epnum < mtu->num_eps; epnum++) {
  516. init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1);
  517. init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0);
  518. }
  519. }
  520. int mtu3_gadget_setup(struct mtu3 *mtu)
  521. {
  522. int ret;
  523. mtu->g.ops = &mtu3_gadget_ops;
  524. mtu->g.max_speed = mtu->max_speed;
  525. mtu->g.speed = USB_SPEED_UNKNOWN;
  526. mtu->g.sg_supported = 0;
  527. mtu->g.name = MTU3_DRIVER_NAME;
  528. mtu->is_active = 0;
  529. mtu->delayed_status = false;
  530. mtu3_gadget_init_eps(mtu);
  531. ret = usb_add_gadget_udc(mtu->dev, &mtu->g);
  532. if (ret)
  533. dev_err(mtu->dev, "failed to register udc\n");
  534. return ret;
  535. }
  536. void mtu3_gadget_cleanup(struct mtu3 *mtu)
  537. {
  538. usb_del_gadget_udc(&mtu->g);
  539. }
  540. void mtu3_gadget_resume(struct mtu3 *mtu)
  541. {
  542. dev_dbg(mtu->dev, "gadget RESUME\n");
  543. if (mtu->gadget_driver && mtu->gadget_driver->resume) {
  544. spin_unlock(&mtu->lock);
  545. mtu->gadget_driver->resume(&mtu->g);
  546. spin_lock(&mtu->lock);
  547. }
  548. }
  549. /* called when SOF packets stop for 3+ msec or enters U3 */
  550. void mtu3_gadget_suspend(struct mtu3 *mtu)
  551. {
  552. dev_dbg(mtu->dev, "gadget SUSPEND\n");
  553. if (mtu->gadget_driver && mtu->gadget_driver->suspend) {
  554. spin_unlock(&mtu->lock);
  555. mtu->gadget_driver->suspend(&mtu->g);
  556. spin_lock(&mtu->lock);
  557. }
  558. }
  559. /* called when VBUS drops below session threshold, and in other cases */
  560. void mtu3_gadget_disconnect(struct mtu3 *mtu)
  561. {
  562. dev_dbg(mtu->dev, "gadget DISCONNECT\n");
  563. if (mtu->gadget_driver && mtu->gadget_driver->disconnect) {
  564. spin_unlock(&mtu->lock);
  565. mtu->gadget_driver->disconnect(&mtu->g);
  566. spin_lock(&mtu->lock);
  567. }
  568. mtu3_state_reset(mtu);
  569. usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED);
  570. }
  571. void mtu3_gadget_reset(struct mtu3 *mtu)
  572. {
  573. dev_dbg(mtu->dev, "gadget RESET\n");
  574. /* report disconnect, if we didn't flush EP state */
  575. if (mtu->g.speed != USB_SPEED_UNKNOWN)
  576. mtu3_gadget_disconnect(mtu);
  577. else
  578. mtu3_state_reset(mtu);
  579. }