bdc_udc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * bdc_udc.c - BRCM BDC USB3.0 device controller gagdet ops
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  5. *
  6. * Author: Ashwini Pahuja
  7. *
  8. * Based on drivers under drivers/usb/gadget/udc/
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/ioport.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/timer.h>
  27. #include <linux/list.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/device.h>
  31. #include <linux/usb/ch9.h>
  32. #include <linux/usb/gadget.h>
  33. #include <linux/usb/otg.h>
  34. #include <linux/pm.h>
  35. #include <linux/io.h>
  36. #include <linux/irq.h>
  37. #include <asm/unaligned.h>
  38. #include <linux/platform_device.h>
  39. #include "bdc.h"
  40. #include "bdc_ep.h"
  41. #include "bdc_cmd.h"
  42. #include "bdc_dbg.h"
  43. static const struct usb_gadget_ops bdc_gadget_ops;
  44. static const char * const conn_speed_str[] = {
  45. "Not connected",
  46. "Full Speed",
  47. "Low Speed",
  48. "High Speed",
  49. "Super Speed",
  50. };
  51. /* EP0 initial descripror */
  52. static struct usb_endpoint_descriptor bdc_gadget_ep0_desc = {
  53. .bLength = USB_DT_ENDPOINT_SIZE,
  54. .bDescriptorType = USB_DT_ENDPOINT,
  55. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  56. .bEndpointAddress = 0,
  57. .wMaxPacketSize = cpu_to_le16(EP0_MAX_PKT_SIZE),
  58. };
  59. /* Advance the srr dqp maintained by SW */
  60. static void srr_dqp_index_advc(struct bdc *bdc, u32 srr_num)
  61. {
  62. struct srr *srr;
  63. srr = &bdc->srr;
  64. dev_dbg_ratelimited(bdc->dev, "srr->dqp_index:%d\n", srr->dqp_index);
  65. srr->dqp_index++;
  66. /* rollback to 0 if we are past the last */
  67. if (srr->dqp_index == NUM_SR_ENTRIES)
  68. srr->dqp_index = 0;
  69. }
  70. /* connect sr */
  71. static void bdc_uspc_connected(struct bdc *bdc)
  72. {
  73. u32 speed, temp;
  74. u32 usppms;
  75. int ret;
  76. temp = bdc_readl(bdc->regs, BDC_USPC);
  77. speed = BDC_PSP(temp);
  78. dev_dbg(bdc->dev, "%s speed=%x\n", __func__, speed);
  79. switch (speed) {
  80. case BDC_SPEED_SS:
  81. bdc_gadget_ep0_desc.wMaxPacketSize =
  82. cpu_to_le16(EP0_MAX_PKT_SIZE);
  83. bdc->gadget.ep0->maxpacket = EP0_MAX_PKT_SIZE;
  84. bdc->gadget.speed = USB_SPEED_SUPER;
  85. /* Enable U1T in SS mode */
  86. usppms = bdc_readl(bdc->regs, BDC_USPPMS);
  87. usppms &= ~BDC_U1T(0xff);
  88. usppms |= BDC_U1T(U1_TIMEOUT);
  89. usppms |= BDC_PORT_W1S;
  90. bdc_writel(bdc->regs, BDC_USPPMS, usppms);
  91. break;
  92. case BDC_SPEED_HS:
  93. bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  94. bdc->gadget.ep0->maxpacket = 64;
  95. bdc->gadget.speed = USB_SPEED_HIGH;
  96. break;
  97. case BDC_SPEED_FS:
  98. bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  99. bdc->gadget.ep0->maxpacket = 64;
  100. bdc->gadget.speed = USB_SPEED_FULL;
  101. break;
  102. case BDC_SPEED_LS:
  103. bdc_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  104. bdc->gadget.ep0->maxpacket = 8;
  105. bdc->gadget.speed = USB_SPEED_LOW;
  106. break;
  107. default:
  108. dev_err(bdc->dev, "UNDEFINED SPEED\n");
  109. return;
  110. }
  111. dev_dbg(bdc->dev, "connected at %s\n", conn_speed_str[speed]);
  112. /* Now we know the speed, configure ep0 */
  113. bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc;
  114. ret = bdc_config_ep(bdc, bdc->bdc_ep_array[1]);
  115. if (ret)
  116. dev_err(bdc->dev, "EP0 config failed\n");
  117. bdc->bdc_ep_array[1]->usb_ep.desc = &bdc_gadget_ep0_desc;
  118. bdc->bdc_ep_array[1]->flags |= BDC_EP_ENABLED;
  119. usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT);
  120. }
  121. /* device got disconnected */
  122. static void bdc_uspc_disconnected(struct bdc *bdc, bool reinit)
  123. {
  124. struct bdc_ep *ep;
  125. dev_dbg(bdc->dev, "%s\n", __func__);
  126. /*
  127. * Only stop ep0 from here, rest of the endpoints will be disabled
  128. * from gadget_disconnect
  129. */
  130. ep = bdc->bdc_ep_array[1];
  131. if (ep && (ep->flags & BDC_EP_ENABLED))
  132. /* if enabled then stop and remove requests */
  133. bdc_ep_disable(ep);
  134. if (bdc->gadget_driver && bdc->gadget_driver->disconnect) {
  135. spin_unlock(&bdc->lock);
  136. bdc->gadget_driver->disconnect(&bdc->gadget);
  137. spin_lock(&bdc->lock);
  138. }
  139. /* Set Unknown speed */
  140. bdc->gadget.speed = USB_SPEED_UNKNOWN;
  141. bdc->devstatus &= DEVSTATUS_CLEAR;
  142. bdc->delayed_status = false;
  143. bdc->reinit = reinit;
  144. bdc->test_mode = false;
  145. }
  146. /* TNotify wkaeup timer */
  147. static void bdc_func_wake_timer(struct work_struct *work)
  148. {
  149. struct bdc *bdc = container_of(work, struct bdc, func_wake_notify.work);
  150. unsigned long flags;
  151. dev_dbg(bdc->dev, "%s\n", __func__);
  152. spin_lock_irqsave(&bdc->lock, flags);
  153. /*
  154. * Check if host has started transferring on endpoints
  155. * FUNC_WAKE_ISSUED is cleared when transfer has started after resume
  156. */
  157. if (bdc->devstatus & FUNC_WAKE_ISSUED) {
  158. dev_dbg(bdc->dev, "FUNC_WAKE_ISSUED FLAG IS STILL SET\n");
  159. /* flag is still set, so again send func wake */
  160. bdc_function_wake_fh(bdc, 0);
  161. schedule_delayed_work(&bdc->func_wake_notify,
  162. msecs_to_jiffies(BDC_TNOTIFY));
  163. }
  164. spin_unlock_irqrestore(&bdc->lock, flags);
  165. }
  166. /* handler for Link state change condition */
  167. static void handle_link_state_change(struct bdc *bdc, u32 uspc)
  168. {
  169. u32 link_state;
  170. dev_dbg(bdc->dev, "Link state change");
  171. link_state = BDC_PST(uspc);
  172. switch (link_state) {
  173. case BDC_LINK_STATE_U3:
  174. if ((bdc->gadget.speed != USB_SPEED_UNKNOWN) &&
  175. bdc->gadget_driver->suspend) {
  176. dev_dbg(bdc->dev, "Entered Suspend mode\n");
  177. spin_unlock(&bdc->lock);
  178. bdc->devstatus |= DEVICE_SUSPENDED;
  179. bdc->gadget_driver->suspend(&bdc->gadget);
  180. spin_lock(&bdc->lock);
  181. }
  182. break;
  183. case BDC_LINK_STATE_U0:
  184. if (bdc->devstatus & REMOTE_WAKEUP_ISSUED) {
  185. bdc->devstatus &= ~REMOTE_WAKEUP_ISSUED;
  186. if (bdc->gadget.speed == USB_SPEED_SUPER) {
  187. bdc_function_wake_fh(bdc, 0);
  188. bdc->devstatus |= FUNC_WAKE_ISSUED;
  189. /*
  190. * Start a Notification timer and check if the
  191. * Host transferred anything on any of the EPs,
  192. * if not then send function wake again every
  193. * TNotification secs until host initiates
  194. * transfer to BDC, USB3 spec Table 8.13
  195. */
  196. schedule_delayed_work(
  197. &bdc->func_wake_notify,
  198. msecs_to_jiffies(BDC_TNOTIFY));
  199. dev_dbg(bdc->dev, "sched func_wake_notify\n");
  200. }
  201. }
  202. break;
  203. case BDC_LINK_STATE_RESUME:
  204. dev_dbg(bdc->dev, "Resumed from Suspend\n");
  205. if (bdc->devstatus & DEVICE_SUSPENDED) {
  206. bdc->gadget_driver->resume(&bdc->gadget);
  207. bdc->devstatus &= ~DEVICE_SUSPENDED;
  208. }
  209. break;
  210. default:
  211. dev_dbg(bdc->dev, "link state:%d\n", link_state);
  212. }
  213. }
  214. /* something changes on upstream port, handle it here */
  215. void bdc_sr_uspc(struct bdc *bdc, struct bdc_sr *sreport)
  216. {
  217. u32 clear_flags = 0;
  218. u32 uspc;
  219. bool connected = false;
  220. bool disconn = false;
  221. uspc = bdc_readl(bdc->regs, BDC_USPC);
  222. dev_dbg(bdc->dev, "%s uspc=0x%08x\n", __func__, uspc);
  223. /* Port connect changed */
  224. if (uspc & BDC_PCC) {
  225. /* Vbus not present, and not connected to Downstream port */
  226. if ((uspc & BDC_VBC) && !(uspc & BDC_VBS) && !(uspc & BDC_PCS))
  227. disconn = true;
  228. else if ((uspc & BDC_PCS) && !BDC_PST(uspc))
  229. connected = true;
  230. }
  231. /* Change in VBus and VBus is present */
  232. if ((uspc & BDC_VBC) && (uspc & BDC_VBS)) {
  233. if (bdc->pullup) {
  234. dev_dbg(bdc->dev, "Do a softconnect\n");
  235. /* Attached state, do a softconnect */
  236. bdc_softconn(bdc);
  237. usb_gadget_set_state(&bdc->gadget, USB_STATE_POWERED);
  238. }
  239. clear_flags = BDC_VBC;
  240. } else if ((uspc & BDC_PRS) || (uspc & BDC_PRC) || disconn) {
  241. /* Hot reset, warm reset, 2.0 bus reset or disconn */
  242. dev_dbg(bdc->dev, "Port reset or disconn\n");
  243. bdc_uspc_disconnected(bdc, disconn);
  244. clear_flags = BDC_PCC|BDC_PCS|BDC_PRS|BDC_PRC;
  245. } else if ((uspc & BDC_PSC) && (uspc & BDC_PCS)) {
  246. /* Change in Link state */
  247. handle_link_state_change(bdc, uspc);
  248. clear_flags = BDC_PSC|BDC_PCS;
  249. }
  250. /*
  251. * In SS we might not have PRC bit set before connection, but in 2.0
  252. * the PRC bit is set before connection, so moving this condition out
  253. * of bus reset to handle both SS/2.0 speeds.
  254. */
  255. if (connected) {
  256. /* This is the connect event for U0/L0 */
  257. dev_dbg(bdc->dev, "Connected\n");
  258. bdc_uspc_connected(bdc);
  259. bdc->devstatus &= ~(DEVICE_SUSPENDED);
  260. }
  261. uspc = bdc_readl(bdc->regs, BDC_USPC);
  262. uspc &= (~BDC_USPSC_RW);
  263. dev_dbg(bdc->dev, "uspc=%x\n", uspc);
  264. bdc_writel(bdc->regs, BDC_USPC, clear_flags);
  265. }
  266. /* Main interrupt handler for bdc */
  267. static irqreturn_t bdc_udc_interrupt(int irq, void *_bdc)
  268. {
  269. u32 eqp_index, dqp_index, sr_type, srr_int;
  270. struct bdc_sr *sreport;
  271. struct bdc *bdc = _bdc;
  272. u32 status;
  273. int ret;
  274. spin_lock(&bdc->lock);
  275. status = bdc_readl(bdc->regs, BDC_BDCSC);
  276. if (!(status & BDC_GIP)) {
  277. spin_unlock(&bdc->lock);
  278. return IRQ_NONE;
  279. }
  280. srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0));
  281. /* Check if the SRR IP bit it set? */
  282. if (!(srr_int & BDC_SRR_IP)) {
  283. dev_warn(bdc->dev, "Global irq pending but SRR IP is 0\n");
  284. spin_unlock(&bdc->lock);
  285. return IRQ_NONE;
  286. }
  287. eqp_index = BDC_SRR_EPI(srr_int);
  288. dqp_index = BDC_SRR_DPI(srr_int);
  289. dev_dbg(bdc->dev,
  290. "%s eqp_index=%d dqp_index=%d srr.dqp_index=%d\n\n",
  291. __func__, eqp_index, dqp_index, bdc->srr.dqp_index);
  292. /* check for ring empty condition */
  293. if (eqp_index == dqp_index) {
  294. dev_dbg(bdc->dev, "SRR empty?\n");
  295. spin_unlock(&bdc->lock);
  296. return IRQ_HANDLED;
  297. }
  298. while (bdc->srr.dqp_index != eqp_index) {
  299. sreport = &bdc->srr.sr_bds[bdc->srr.dqp_index];
  300. /* sreport is read before using it */
  301. rmb();
  302. sr_type = le32_to_cpu(sreport->offset[3]) & BD_TYPE_BITMASK;
  303. dev_dbg_ratelimited(bdc->dev, "sr_type=%d\n", sr_type);
  304. switch (sr_type) {
  305. case SR_XSF:
  306. bdc->sr_handler[0](bdc, sreport);
  307. break;
  308. case SR_USPC:
  309. bdc->sr_handler[1](bdc, sreport);
  310. break;
  311. default:
  312. dev_warn(bdc->dev, "SR:%d not handled\n", sr_type);
  313. }
  314. /* Advance the srr dqp index */
  315. srr_dqp_index_advc(bdc, 0);
  316. }
  317. /* update the hw dequeue pointer */
  318. srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0));
  319. srr_int &= ~BDC_SRR_DPI_MASK;
  320. srr_int &= ~(BDC_SRR_RWS|BDC_SRR_RST|BDC_SRR_ISR);
  321. srr_int |= ((bdc->srr.dqp_index) << 16);
  322. srr_int |= BDC_SRR_IP;
  323. bdc_writel(bdc->regs, BDC_SRRINT(0), srr_int);
  324. srr_int = bdc_readl(bdc->regs, BDC_SRRINT(0));
  325. if (bdc->reinit) {
  326. ret = bdc_reinit(bdc);
  327. if (ret)
  328. dev_err(bdc->dev, "err in bdc reinit\n");
  329. }
  330. spin_unlock(&bdc->lock);
  331. return IRQ_HANDLED;
  332. }
  333. /* Gadget ops */
  334. static int bdc_udc_start(struct usb_gadget *gadget,
  335. struct usb_gadget_driver *driver)
  336. {
  337. struct bdc *bdc = gadget_to_bdc(gadget);
  338. unsigned long flags;
  339. int ret = 0;
  340. dev_dbg(bdc->dev, "%s()\n", __func__);
  341. spin_lock_irqsave(&bdc->lock, flags);
  342. if (bdc->gadget_driver) {
  343. dev_err(bdc->dev, "%s is already bound to %s\n",
  344. bdc->gadget.name,
  345. bdc->gadget_driver->driver.name);
  346. ret = -EBUSY;
  347. goto err;
  348. }
  349. /*
  350. * Run the controller from here and when BDC is connected to
  351. * Host then driver will receive a USPC SR with VBUS present
  352. * and then driver will do a softconnect.
  353. */
  354. ret = bdc_run(bdc);
  355. if (ret) {
  356. dev_err(bdc->dev, "%s bdc run fail\n", __func__);
  357. goto err;
  358. }
  359. bdc->gadget_driver = driver;
  360. bdc->gadget.dev.driver = &driver->driver;
  361. err:
  362. spin_unlock_irqrestore(&bdc->lock, flags);
  363. return ret;
  364. }
  365. static int bdc_udc_stop(struct usb_gadget *gadget)
  366. {
  367. struct bdc *bdc = gadget_to_bdc(gadget);
  368. unsigned long flags;
  369. dev_dbg(bdc->dev, "%s()\n", __func__);
  370. spin_lock_irqsave(&bdc->lock, flags);
  371. bdc_stop(bdc);
  372. bdc->gadget_driver = NULL;
  373. bdc->gadget.dev.driver = NULL;
  374. spin_unlock_irqrestore(&bdc->lock, flags);
  375. return 0;
  376. }
  377. static int bdc_udc_pullup(struct usb_gadget *gadget, int is_on)
  378. {
  379. struct bdc *bdc = gadget_to_bdc(gadget);
  380. unsigned long flags;
  381. u32 uspc;
  382. dev_dbg(bdc->dev, "%s() is_on:%d\n", __func__, is_on);
  383. if (!gadget)
  384. return -EINVAL;
  385. spin_lock_irqsave(&bdc->lock, flags);
  386. if (!is_on) {
  387. bdc_softdisconn(bdc);
  388. bdc->pullup = false;
  389. } else {
  390. /*
  391. * For a self powered device, we need to wait till we receive
  392. * a VBUS change and Vbus present event, then if pullup flag
  393. * is set, then only we present the Termintation.
  394. */
  395. bdc->pullup = true;
  396. /*
  397. * Check if BDC is already connected to Host i.e Vbus=1,
  398. * if yes, then present TERM now, this is typical for bus
  399. * powered devices.
  400. */
  401. uspc = bdc_readl(bdc->regs, BDC_USPC);
  402. if (uspc & BDC_VBS)
  403. bdc_softconn(bdc);
  404. }
  405. spin_unlock_irqrestore(&bdc->lock, flags);
  406. return 0;
  407. }
  408. static int bdc_udc_set_selfpowered(struct usb_gadget *gadget,
  409. int is_self)
  410. {
  411. struct bdc *bdc = gadget_to_bdc(gadget);
  412. unsigned long flags;
  413. dev_dbg(bdc->dev, "%s()\n", __func__);
  414. gadget->is_selfpowered = (is_self != 0);
  415. spin_lock_irqsave(&bdc->lock, flags);
  416. if (!is_self)
  417. bdc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
  418. else
  419. bdc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  420. spin_unlock_irqrestore(&bdc->lock, flags);
  421. return 0;
  422. }
  423. static int bdc_udc_wakeup(struct usb_gadget *gadget)
  424. {
  425. struct bdc *bdc = gadget_to_bdc(gadget);
  426. unsigned long flags;
  427. u8 link_state;
  428. u32 uspc;
  429. int ret = 0;
  430. dev_dbg(bdc->dev,
  431. "%s() bdc->devstatus=%08x\n",
  432. __func__, bdc->devstatus);
  433. if (!(bdc->devstatus & REMOTE_WAKE_ENABLE))
  434. return -EOPNOTSUPP;
  435. spin_lock_irqsave(&bdc->lock, flags);
  436. uspc = bdc_readl(bdc->regs, BDC_USPC);
  437. link_state = BDC_PST(uspc);
  438. dev_dbg(bdc->dev, "link_state =%d portsc=%x", link_state, uspc);
  439. if (link_state != BDC_LINK_STATE_U3) {
  440. dev_warn(bdc->dev,
  441. "can't wakeup from link state %d\n",
  442. link_state);
  443. ret = -EINVAL;
  444. goto out;
  445. }
  446. if (bdc->gadget.speed == USB_SPEED_SUPER)
  447. bdc->devstatus |= REMOTE_WAKEUP_ISSUED;
  448. uspc &= ~BDC_PST_MASK;
  449. uspc &= (~BDC_USPSC_RW);
  450. uspc |= BDC_PST(BDC_LINK_STATE_U0);
  451. uspc |= BDC_SWS;
  452. bdc_writel(bdc->regs, BDC_USPC, uspc);
  453. uspc = bdc_readl(bdc->regs, BDC_USPC);
  454. link_state = BDC_PST(uspc);
  455. dev_dbg(bdc->dev, "link_state =%d portsc=%x", link_state, uspc);
  456. out:
  457. spin_unlock_irqrestore(&bdc->lock, flags);
  458. return ret;
  459. }
  460. static const struct usb_gadget_ops bdc_gadget_ops = {
  461. .wakeup = bdc_udc_wakeup,
  462. .set_selfpowered = bdc_udc_set_selfpowered,
  463. .pullup = bdc_udc_pullup,
  464. .udc_start = bdc_udc_start,
  465. .udc_stop = bdc_udc_stop,
  466. };
  467. /* Init the gadget interface and register the udc */
  468. int bdc_udc_init(struct bdc *bdc)
  469. {
  470. u32 temp;
  471. int ret;
  472. dev_dbg(bdc->dev, "%s()\n", __func__);
  473. bdc->gadget.ops = &bdc_gadget_ops;
  474. bdc->gadget.max_speed = USB_SPEED_SUPER;
  475. bdc->gadget.speed = USB_SPEED_UNKNOWN;
  476. bdc->gadget.dev.parent = bdc->dev;
  477. bdc->gadget.sg_supported = false;
  478. bdc->gadget.name = BRCM_BDC_NAME;
  479. ret = devm_request_irq(bdc->dev, bdc->irq, bdc_udc_interrupt,
  480. IRQF_SHARED , BRCM_BDC_NAME, bdc);
  481. if (ret) {
  482. dev_err(bdc->dev,
  483. "failed to request irq #%d %d\n",
  484. bdc->irq, ret);
  485. return ret;
  486. }
  487. ret = bdc_init_ep(bdc);
  488. if (ret) {
  489. dev_err(bdc->dev, "bdc init ep fail: %d\n", ret);
  490. return ret;
  491. }
  492. ret = usb_add_gadget_udc(bdc->dev, &bdc->gadget);
  493. if (ret) {
  494. dev_err(bdc->dev, "failed to register udc\n");
  495. goto err0;
  496. }
  497. usb_gadget_set_state(&bdc->gadget, USB_STATE_NOTATTACHED);
  498. bdc->bdc_ep_array[1]->desc = &bdc_gadget_ep0_desc;
  499. /*
  500. * Allocate bd list for ep0 only, ep0 will be enabled on connect
  501. * status report when the speed is known
  502. */
  503. ret = bdc_ep_enable(bdc->bdc_ep_array[1]);
  504. if (ret) {
  505. dev_err(bdc->dev, "fail to enable %s\n",
  506. bdc->bdc_ep_array[1]->name);
  507. goto err1;
  508. }
  509. INIT_DELAYED_WORK(&bdc->func_wake_notify, bdc_func_wake_timer);
  510. /* Enable Interrupts */
  511. temp = bdc_readl(bdc->regs, BDC_BDCSC);
  512. temp |= BDC_GIE;
  513. bdc_writel(bdc->regs, BDC_BDCSC, temp);
  514. return 0;
  515. err1:
  516. usb_del_gadget_udc(&bdc->gadget);
  517. err0:
  518. bdc_free_ep(bdc);
  519. return ret;
  520. }
  521. void bdc_udc_exit(struct bdc *bdc)
  522. {
  523. dev_dbg(bdc->dev, "%s()\n", __func__);
  524. bdc_ep_disable(bdc->bdc_ep_array[1]);
  525. usb_del_gadget_udc(&bdc->gadget);
  526. bdc_free_ep(bdc);
  527. }