udc.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /*
  2. * udc.c - ChipIdea UDC driver
  3. *
  4. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Author: David Lopo
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/dmapool.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/usb/ch9.h>
  27. #include <linux/usb/gadget.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/chipidea.h>
  30. #include "ci.h"
  31. #include "udc.h"
  32. #include "bits.h"
  33. #include "debug.h"
  34. /* control endpoint description */
  35. static const struct usb_endpoint_descriptor
  36. ctrl_endpt_out_desc = {
  37. .bLength = USB_DT_ENDPOINT_SIZE,
  38. .bDescriptorType = USB_DT_ENDPOINT,
  39. .bEndpointAddress = USB_DIR_OUT,
  40. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  41. .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
  42. };
  43. static const struct usb_endpoint_descriptor
  44. ctrl_endpt_in_desc = {
  45. .bLength = USB_DT_ENDPOINT_SIZE,
  46. .bDescriptorType = USB_DT_ENDPOINT,
  47. .bEndpointAddress = USB_DIR_IN,
  48. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  49. .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
  50. };
  51. /**
  52. * hw_ep_bit: calculates the bit number
  53. * @num: endpoint number
  54. * @dir: endpoint direction
  55. *
  56. * This function returns bit number
  57. */
  58. static inline int hw_ep_bit(int num, int dir)
  59. {
  60. return num + (dir ? 16 : 0);
  61. }
  62. static inline int ep_to_bit(struct ci13xxx *ci, int n)
  63. {
  64. int fill = 16 - ci->hw_ep_max / 2;
  65. if (n >= ci->hw_ep_max / 2)
  66. n += fill;
  67. return n;
  68. }
  69. /**
  70. * hw_device_state: enables/disables interrupts (execute without interruption)
  71. * @dma: 0 => disable, !0 => enable and set dma engine
  72. *
  73. * This function returns an error code
  74. */
  75. static int hw_device_state(struct ci13xxx *ci, u32 dma)
  76. {
  77. if (dma) {
  78. hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
  79. /* interrupt, error, port change, reset, sleep/suspend */
  80. hw_write(ci, OP_USBINTR, ~0,
  81. USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
  82. } else {
  83. hw_write(ci, OP_USBINTR, ~0, 0);
  84. }
  85. return 0;
  86. }
  87. /**
  88. * hw_ep_flush: flush endpoint fifo (execute without interruption)
  89. * @num: endpoint number
  90. * @dir: endpoint direction
  91. *
  92. * This function returns an error code
  93. */
  94. static int hw_ep_flush(struct ci13xxx *ci, int num, int dir)
  95. {
  96. int n = hw_ep_bit(num, dir);
  97. do {
  98. /* flush any pending transfer */
  99. hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
  100. while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
  101. cpu_relax();
  102. } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
  103. return 0;
  104. }
  105. /**
  106. * hw_ep_disable: disables endpoint (execute without interruption)
  107. * @num: endpoint number
  108. * @dir: endpoint direction
  109. *
  110. * This function returns an error code
  111. */
  112. static int hw_ep_disable(struct ci13xxx *ci, int num, int dir)
  113. {
  114. hw_ep_flush(ci, num, dir);
  115. hw_write(ci, OP_ENDPTCTRL + num,
  116. dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
  117. return 0;
  118. }
  119. /**
  120. * hw_ep_enable: enables endpoint (execute without interruption)
  121. * @num: endpoint number
  122. * @dir: endpoint direction
  123. * @type: endpoint type
  124. *
  125. * This function returns an error code
  126. */
  127. static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type)
  128. {
  129. u32 mask, data;
  130. if (dir) {
  131. mask = ENDPTCTRL_TXT; /* type */
  132. data = type << ffs_nr(mask);
  133. mask |= ENDPTCTRL_TXS; /* unstall */
  134. mask |= ENDPTCTRL_TXR; /* reset data toggle */
  135. data |= ENDPTCTRL_TXR;
  136. mask |= ENDPTCTRL_TXE; /* enable */
  137. data |= ENDPTCTRL_TXE;
  138. } else {
  139. mask = ENDPTCTRL_RXT; /* type */
  140. data = type << ffs_nr(mask);
  141. mask |= ENDPTCTRL_RXS; /* unstall */
  142. mask |= ENDPTCTRL_RXR; /* reset data toggle */
  143. data |= ENDPTCTRL_RXR;
  144. mask |= ENDPTCTRL_RXE; /* enable */
  145. data |= ENDPTCTRL_RXE;
  146. }
  147. hw_write(ci, OP_ENDPTCTRL + num, mask, data);
  148. return 0;
  149. }
  150. /**
  151. * hw_ep_get_halt: return endpoint halt status
  152. * @num: endpoint number
  153. * @dir: endpoint direction
  154. *
  155. * This function returns 1 if endpoint halted
  156. */
  157. static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir)
  158. {
  159. u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
  160. return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
  161. }
  162. /**
  163. * hw_test_and_clear_setup_status: test & clear setup status (execute without
  164. * interruption)
  165. * @n: endpoint number
  166. *
  167. * This function returns setup status
  168. */
  169. static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n)
  170. {
  171. n = ep_to_bit(ci, n);
  172. return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
  173. }
  174. /**
  175. * hw_ep_prime: primes endpoint (execute without interruption)
  176. * @num: endpoint number
  177. * @dir: endpoint direction
  178. * @is_ctrl: true if control endpoint
  179. *
  180. * This function returns an error code
  181. */
  182. static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl)
  183. {
  184. int n = hw_ep_bit(num, dir);
  185. if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
  186. return -EAGAIN;
  187. hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
  188. while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
  189. cpu_relax();
  190. if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
  191. return -EAGAIN;
  192. /* status shoult be tested according with manual but it doesn't work */
  193. return 0;
  194. }
  195. /**
  196. * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
  197. * without interruption)
  198. * @num: endpoint number
  199. * @dir: endpoint direction
  200. * @value: true => stall, false => unstall
  201. *
  202. * This function returns an error code
  203. */
  204. static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value)
  205. {
  206. if (value != 0 && value != 1)
  207. return -EINVAL;
  208. do {
  209. enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
  210. u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
  211. u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
  212. /* data toggle - reserved for EP0 but it's in ESS */
  213. hw_write(ci, reg, mask_xs|mask_xr,
  214. value ? mask_xs : mask_xr);
  215. } while (value != hw_ep_get_halt(ci, num, dir));
  216. return 0;
  217. }
  218. /**
  219. * hw_is_port_high_speed: test if port is high speed
  220. *
  221. * This function returns true if high speed port
  222. */
  223. static int hw_port_is_high_speed(struct ci13xxx *ci)
  224. {
  225. return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
  226. hw_read(ci, OP_PORTSC, PORTSC_HSP);
  227. }
  228. /**
  229. * hw_read_intr_enable: returns interrupt enable register
  230. *
  231. * This function returns register data
  232. */
  233. static u32 hw_read_intr_enable(struct ci13xxx *ci)
  234. {
  235. return hw_read(ci, OP_USBINTR, ~0);
  236. }
  237. /**
  238. * hw_read_intr_status: returns interrupt status register
  239. *
  240. * This function returns register data
  241. */
  242. static u32 hw_read_intr_status(struct ci13xxx *ci)
  243. {
  244. return hw_read(ci, OP_USBSTS, ~0);
  245. }
  246. /**
  247. * hw_test_and_clear_complete: test & clear complete status (execute without
  248. * interruption)
  249. * @n: endpoint number
  250. *
  251. * This function returns complete status
  252. */
  253. static int hw_test_and_clear_complete(struct ci13xxx *ci, int n)
  254. {
  255. n = ep_to_bit(ci, n);
  256. return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
  257. }
  258. /**
  259. * hw_test_and_clear_intr_active: test & clear active interrupts (execute
  260. * without interruption)
  261. *
  262. * This function returns active interrutps
  263. */
  264. static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci)
  265. {
  266. u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
  267. hw_write(ci, OP_USBSTS, ~0, reg);
  268. return reg;
  269. }
  270. static void hw_enable_vbus_intr(struct ci13xxx *ci)
  271. {
  272. hw_write(ci, OP_OTGSC, OTGSC_AVVIS, OTGSC_AVVIS);
  273. hw_write(ci, OP_OTGSC, OTGSC_AVVIE, OTGSC_AVVIE);
  274. queue_work(ci->wq, &ci->vbus_work);
  275. }
  276. static void hw_disable_vbus_intr(struct ci13xxx *ci)
  277. {
  278. hw_write(ci, OP_OTGSC, OTGSC_AVVIE, 0);
  279. }
  280. /**
  281. * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
  282. * interruption)
  283. *
  284. * This function returns guard value
  285. */
  286. static int hw_test_and_clear_setup_guard(struct ci13xxx *ci)
  287. {
  288. return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
  289. }
  290. /**
  291. * hw_test_and_set_setup_guard: test & set setup guard (execute without
  292. * interruption)
  293. *
  294. * This function returns guard value
  295. */
  296. static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
  297. {
  298. return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
  299. }
  300. /**
  301. * hw_usb_set_address: configures USB address (execute without interruption)
  302. * @value: new USB address
  303. *
  304. * This function explicitly sets the address, without the "USBADRA" (advance)
  305. * feature, which is not supported by older versions of the controller.
  306. */
  307. static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
  308. {
  309. hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
  310. value << ffs_nr(DEVICEADDR_USBADR));
  311. }
  312. /**
  313. * hw_usb_reset: restart device after a bus reset (execute without
  314. * interruption)
  315. *
  316. * This function returns an error code
  317. */
  318. static int hw_usb_reset(struct ci13xxx *ci)
  319. {
  320. hw_usb_set_address(ci, 0);
  321. /* ESS flushes only at end?!? */
  322. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  323. /* clear setup token semaphores */
  324. hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
  325. /* clear complete status */
  326. hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
  327. /* wait until all bits cleared */
  328. while (hw_read(ci, OP_ENDPTPRIME, ~0))
  329. udelay(10); /* not RTOS friendly */
  330. /* reset all endpoints ? */
  331. /* reset internal status and wait for further instructions
  332. no need to verify the port reset status (ESS does it) */
  333. return 0;
  334. }
  335. static void vbus_work(struct work_struct *work)
  336. {
  337. struct ci13xxx *ci = container_of(work, struct ci13xxx, vbus_work);
  338. if (hw_read(ci, OP_OTGSC, OTGSC_AVV))
  339. usb_gadget_vbus_connect(&ci->gadget);
  340. else
  341. usb_gadget_vbus_disconnect(&ci->gadget);
  342. }
  343. /******************************************************************************
  344. * UTIL block
  345. *****************************************************************************/
  346. /**
  347. * _usb_addr: calculates endpoint address from direction & number
  348. * @ep: endpoint
  349. */
  350. static inline u8 _usb_addr(struct ci13xxx_ep *ep)
  351. {
  352. return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
  353. }
  354. /**
  355. * _hardware_queue: configures a request at hardware level
  356. * @gadget: gadget
  357. * @mEp: endpoint
  358. *
  359. * This function returns an error code
  360. */
  361. static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  362. {
  363. struct ci13xxx *ci = mEp->ci;
  364. unsigned i;
  365. int ret = 0;
  366. unsigned length = mReq->req.length;
  367. /* don't queue twice */
  368. if (mReq->req.status == -EALREADY)
  369. return -EALREADY;
  370. mReq->req.status = -EALREADY;
  371. if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
  372. mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
  373. &mReq->zdma);
  374. if (mReq->zptr == NULL)
  375. return -ENOMEM;
  376. memset(mReq->zptr, 0, sizeof(*mReq->zptr));
  377. mReq->zptr->next = TD_TERMINATE;
  378. mReq->zptr->token = TD_STATUS_ACTIVE;
  379. if (!mReq->req.no_interrupt)
  380. mReq->zptr->token |= TD_IOC;
  381. }
  382. ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
  383. if (ret)
  384. return ret;
  385. /*
  386. * TD configuration
  387. * TODO - handle requests which spawns into several TDs
  388. */
  389. memset(mReq->ptr, 0, sizeof(*mReq->ptr));
  390. mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
  391. mReq->ptr->token &= TD_TOTAL_BYTES;
  392. mReq->ptr->token |= TD_STATUS_ACTIVE;
  393. if (mReq->zptr) {
  394. mReq->ptr->next = mReq->zdma;
  395. } else {
  396. mReq->ptr->next = TD_TERMINATE;
  397. if (!mReq->req.no_interrupt)
  398. mReq->ptr->token |= TD_IOC;
  399. }
  400. mReq->ptr->page[0] = mReq->req.dma;
  401. for (i = 1; i < 5; i++)
  402. mReq->ptr->page[i] =
  403. (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
  404. if (!list_empty(&mEp->qh.queue)) {
  405. struct ci13xxx_req *mReqPrev;
  406. int n = hw_ep_bit(mEp->num, mEp->dir);
  407. int tmp_stat;
  408. mReqPrev = list_entry(mEp->qh.queue.prev,
  409. struct ci13xxx_req, queue);
  410. if (mReqPrev->zptr)
  411. mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
  412. else
  413. mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
  414. wmb();
  415. if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
  416. goto done;
  417. do {
  418. hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
  419. tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
  420. } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
  421. hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
  422. if (tmp_stat)
  423. goto done;
  424. }
  425. /* QH configuration */
  426. mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
  427. mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
  428. mEp->qh.ptr->cap |= QH_ZLT;
  429. wmb(); /* synchronize before ep prime */
  430. ret = hw_ep_prime(ci, mEp->num, mEp->dir,
  431. mEp->type == USB_ENDPOINT_XFER_CONTROL);
  432. done:
  433. return ret;
  434. }
  435. /**
  436. * _hardware_dequeue: handles a request at hardware level
  437. * @gadget: gadget
  438. * @mEp: endpoint
  439. *
  440. * This function returns an error code
  441. */
  442. static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  443. {
  444. if (mReq->req.status != -EALREADY)
  445. return -EINVAL;
  446. if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
  447. return -EBUSY;
  448. if (mReq->zptr) {
  449. if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
  450. return -EBUSY;
  451. dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
  452. mReq->zptr = NULL;
  453. }
  454. mReq->req.status = 0;
  455. usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
  456. mReq->req.status = mReq->ptr->token & TD_STATUS;
  457. if ((TD_STATUS_HALTED & mReq->req.status) != 0)
  458. mReq->req.status = -1;
  459. else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
  460. mReq->req.status = -1;
  461. else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
  462. mReq->req.status = -1;
  463. mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
  464. mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
  465. mReq->req.actual = mReq->req.length - mReq->req.actual;
  466. mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
  467. return mReq->req.actual;
  468. }
  469. /**
  470. * _ep_nuke: dequeues all endpoint requests
  471. * @mEp: endpoint
  472. *
  473. * This function returns an error code
  474. * Caller must hold lock
  475. */
  476. static int _ep_nuke(struct ci13xxx_ep *mEp)
  477. __releases(mEp->lock)
  478. __acquires(mEp->lock)
  479. {
  480. if (mEp == NULL)
  481. return -EINVAL;
  482. hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
  483. while (!list_empty(&mEp->qh.queue)) {
  484. /* pop oldest request */
  485. struct ci13xxx_req *mReq = \
  486. list_entry(mEp->qh.queue.next,
  487. struct ci13xxx_req, queue);
  488. list_del_init(&mReq->queue);
  489. mReq->req.status = -ESHUTDOWN;
  490. if (mReq->req.complete != NULL) {
  491. spin_unlock(mEp->lock);
  492. mReq->req.complete(&mEp->ep, &mReq->req);
  493. spin_lock(mEp->lock);
  494. }
  495. }
  496. return 0;
  497. }
  498. /**
  499. * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
  500. * @gadget: gadget
  501. *
  502. * This function returns an error code
  503. */
  504. static int _gadget_stop_activity(struct usb_gadget *gadget)
  505. {
  506. struct usb_ep *ep;
  507. struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
  508. unsigned long flags;
  509. spin_lock_irqsave(&ci->lock, flags);
  510. ci->gadget.speed = USB_SPEED_UNKNOWN;
  511. ci->remote_wakeup = 0;
  512. ci->suspended = 0;
  513. spin_unlock_irqrestore(&ci->lock, flags);
  514. /* flush all endpoints */
  515. gadget_for_each_ep(ep, gadget) {
  516. usb_ep_fifo_flush(ep);
  517. }
  518. usb_ep_fifo_flush(&ci->ep0out->ep);
  519. usb_ep_fifo_flush(&ci->ep0in->ep);
  520. if (ci->driver)
  521. ci->driver->disconnect(gadget);
  522. /* make sure to disable all endpoints */
  523. gadget_for_each_ep(ep, gadget) {
  524. usb_ep_disable(ep);
  525. }
  526. if (ci->status != NULL) {
  527. usb_ep_free_request(&ci->ep0in->ep, ci->status);
  528. ci->status = NULL;
  529. }
  530. return 0;
  531. }
  532. /******************************************************************************
  533. * ISR block
  534. *****************************************************************************/
  535. /**
  536. * isr_reset_handler: USB reset interrupt handler
  537. * @ci: UDC device
  538. *
  539. * This function resets USB engine after a bus reset occurred
  540. */
  541. static void isr_reset_handler(struct ci13xxx *ci)
  542. __releases(ci->lock)
  543. __acquires(ci->lock)
  544. {
  545. int retval;
  546. dbg_event(0xFF, "BUS RST", 0);
  547. spin_unlock(&ci->lock);
  548. retval = _gadget_stop_activity(&ci->gadget);
  549. if (retval)
  550. goto done;
  551. retval = hw_usb_reset(ci);
  552. if (retval)
  553. goto done;
  554. ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
  555. if (ci->status == NULL)
  556. retval = -ENOMEM;
  557. done:
  558. spin_lock(&ci->lock);
  559. if (retval)
  560. dev_err(ci->dev, "error: %i\n", retval);
  561. }
  562. /**
  563. * isr_get_status_complete: get_status request complete function
  564. * @ep: endpoint
  565. * @req: request handled
  566. *
  567. * Caller must release lock
  568. */
  569. static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
  570. {
  571. if (ep == NULL || req == NULL)
  572. return;
  573. kfree(req->buf);
  574. usb_ep_free_request(ep, req);
  575. }
  576. /**
  577. * isr_get_status_response: get_status request response
  578. * @ci: ci struct
  579. * @setup: setup request packet
  580. *
  581. * This function returns an error code
  582. */
  583. static int isr_get_status_response(struct ci13xxx *ci,
  584. struct usb_ctrlrequest *setup)
  585. __releases(mEp->lock)
  586. __acquires(mEp->lock)
  587. {
  588. struct ci13xxx_ep *mEp = ci->ep0in;
  589. struct usb_request *req = NULL;
  590. gfp_t gfp_flags = GFP_ATOMIC;
  591. int dir, num, retval;
  592. if (mEp == NULL || setup == NULL)
  593. return -EINVAL;
  594. spin_unlock(mEp->lock);
  595. req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
  596. spin_lock(mEp->lock);
  597. if (req == NULL)
  598. return -ENOMEM;
  599. req->complete = isr_get_status_complete;
  600. req->length = 2;
  601. req->buf = kzalloc(req->length, gfp_flags);
  602. if (req->buf == NULL) {
  603. retval = -ENOMEM;
  604. goto err_free_req;
  605. }
  606. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  607. /* Assume that device is bus powered for now. */
  608. *(u16 *)req->buf = ci->remote_wakeup << 1;
  609. retval = 0;
  610. } else if ((setup->bRequestType & USB_RECIP_MASK) \
  611. == USB_RECIP_ENDPOINT) {
  612. dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
  613. TX : RX;
  614. num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  615. *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
  616. }
  617. /* else do nothing; reserved for future use */
  618. spin_unlock(mEp->lock);
  619. retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
  620. spin_lock(mEp->lock);
  621. if (retval)
  622. goto err_free_buf;
  623. return 0;
  624. err_free_buf:
  625. kfree(req->buf);
  626. err_free_req:
  627. spin_unlock(mEp->lock);
  628. usb_ep_free_request(&mEp->ep, req);
  629. spin_lock(mEp->lock);
  630. return retval;
  631. }
  632. /**
  633. * isr_setup_status_complete: setup_status request complete function
  634. * @ep: endpoint
  635. * @req: request handled
  636. *
  637. * Caller must release lock. Put the port in test mode if test mode
  638. * feature is selected.
  639. */
  640. static void
  641. isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
  642. {
  643. struct ci13xxx *ci = req->context;
  644. unsigned long flags;
  645. if (ci->setaddr) {
  646. hw_usb_set_address(ci, ci->address);
  647. ci->setaddr = false;
  648. }
  649. spin_lock_irqsave(&ci->lock, flags);
  650. if (ci->test_mode)
  651. hw_port_test_set(ci, ci->test_mode);
  652. spin_unlock_irqrestore(&ci->lock, flags);
  653. }
  654. /**
  655. * isr_setup_status_phase: queues the status phase of a setup transation
  656. * @ci: ci struct
  657. *
  658. * This function returns an error code
  659. */
  660. static int isr_setup_status_phase(struct ci13xxx *ci)
  661. __releases(mEp->lock)
  662. __acquires(mEp->lock)
  663. {
  664. int retval;
  665. struct ci13xxx_ep *mEp;
  666. mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
  667. ci->status->context = ci;
  668. ci->status->complete = isr_setup_status_complete;
  669. spin_unlock(mEp->lock);
  670. retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
  671. spin_lock(mEp->lock);
  672. return retval;
  673. }
  674. /**
  675. * isr_tr_complete_low: transaction complete low level handler
  676. * @mEp: endpoint
  677. *
  678. * This function returns an error code
  679. * Caller must hold lock
  680. */
  681. static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
  682. __releases(mEp->lock)
  683. __acquires(mEp->lock)
  684. {
  685. struct ci13xxx_req *mReq, *mReqTemp;
  686. struct ci13xxx_ep *mEpTemp = mEp;
  687. int retval = 0;
  688. list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
  689. queue) {
  690. retval = _hardware_dequeue(mEp, mReq);
  691. if (retval < 0)
  692. break;
  693. list_del_init(&mReq->queue);
  694. dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
  695. if (mReq->req.complete != NULL) {
  696. spin_unlock(mEp->lock);
  697. if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
  698. mReq->req.length)
  699. mEpTemp = mEp->ci->ep0in;
  700. mReq->req.complete(&mEpTemp->ep, &mReq->req);
  701. spin_lock(mEp->lock);
  702. }
  703. }
  704. if (retval == -EBUSY)
  705. retval = 0;
  706. if (retval < 0)
  707. dbg_event(_usb_addr(mEp), "DONE", retval);
  708. return retval;
  709. }
  710. /**
  711. * isr_tr_complete_handler: transaction complete interrupt handler
  712. * @ci: UDC descriptor
  713. *
  714. * This function handles traffic events
  715. */
  716. static void isr_tr_complete_handler(struct ci13xxx *ci)
  717. __releases(ci->lock)
  718. __acquires(ci->lock)
  719. {
  720. unsigned i;
  721. u8 tmode = 0;
  722. for (i = 0; i < ci->hw_ep_max; i++) {
  723. struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
  724. int type, num, dir, err = -EINVAL;
  725. struct usb_ctrlrequest req;
  726. if (mEp->ep.desc == NULL)
  727. continue; /* not configured */
  728. if (hw_test_and_clear_complete(ci, i)) {
  729. err = isr_tr_complete_low(mEp);
  730. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  731. if (err > 0) /* needs status phase */
  732. err = isr_setup_status_phase(ci);
  733. if (err < 0) {
  734. dbg_event(_usb_addr(mEp),
  735. "ERROR", err);
  736. spin_unlock(&ci->lock);
  737. if (usb_ep_set_halt(&mEp->ep))
  738. dev_err(ci->dev,
  739. "error: ep_set_halt\n");
  740. spin_lock(&ci->lock);
  741. }
  742. }
  743. }
  744. if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
  745. !hw_test_and_clear_setup_status(ci, i))
  746. continue;
  747. if (i != 0) {
  748. dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
  749. continue;
  750. }
  751. /*
  752. * Flush data and handshake transactions of previous
  753. * setup packet.
  754. */
  755. _ep_nuke(ci->ep0out);
  756. _ep_nuke(ci->ep0in);
  757. /* read_setup_packet */
  758. do {
  759. hw_test_and_set_setup_guard(ci);
  760. memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
  761. } while (!hw_test_and_clear_setup_guard(ci));
  762. type = req.bRequestType;
  763. ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
  764. dbg_setup(_usb_addr(mEp), &req);
  765. switch (req.bRequest) {
  766. case USB_REQ_CLEAR_FEATURE:
  767. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  768. le16_to_cpu(req.wValue) ==
  769. USB_ENDPOINT_HALT) {
  770. if (req.wLength != 0)
  771. break;
  772. num = le16_to_cpu(req.wIndex);
  773. dir = num & USB_ENDPOINT_DIR_MASK;
  774. num &= USB_ENDPOINT_NUMBER_MASK;
  775. if (dir) /* TX */
  776. num += ci->hw_ep_max/2;
  777. if (!ci->ci13xxx_ep[num].wedge) {
  778. spin_unlock(&ci->lock);
  779. err = usb_ep_clear_halt(
  780. &ci->ci13xxx_ep[num].ep);
  781. spin_lock(&ci->lock);
  782. if (err)
  783. break;
  784. }
  785. err = isr_setup_status_phase(ci);
  786. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
  787. le16_to_cpu(req.wValue) ==
  788. USB_DEVICE_REMOTE_WAKEUP) {
  789. if (req.wLength != 0)
  790. break;
  791. ci->remote_wakeup = 0;
  792. err = isr_setup_status_phase(ci);
  793. } else {
  794. goto delegate;
  795. }
  796. break;
  797. case USB_REQ_GET_STATUS:
  798. if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
  799. type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
  800. type != (USB_DIR_IN|USB_RECIP_INTERFACE))
  801. goto delegate;
  802. if (le16_to_cpu(req.wLength) != 2 ||
  803. le16_to_cpu(req.wValue) != 0)
  804. break;
  805. err = isr_get_status_response(ci, &req);
  806. break;
  807. case USB_REQ_SET_ADDRESS:
  808. if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
  809. goto delegate;
  810. if (le16_to_cpu(req.wLength) != 0 ||
  811. le16_to_cpu(req.wIndex) != 0)
  812. break;
  813. ci->address = (u8)le16_to_cpu(req.wValue);
  814. ci->setaddr = true;
  815. err = isr_setup_status_phase(ci);
  816. break;
  817. case USB_REQ_SET_FEATURE:
  818. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  819. le16_to_cpu(req.wValue) ==
  820. USB_ENDPOINT_HALT) {
  821. if (req.wLength != 0)
  822. break;
  823. num = le16_to_cpu(req.wIndex);
  824. dir = num & USB_ENDPOINT_DIR_MASK;
  825. num &= USB_ENDPOINT_NUMBER_MASK;
  826. if (dir) /* TX */
  827. num += ci->hw_ep_max/2;
  828. spin_unlock(&ci->lock);
  829. err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
  830. spin_lock(&ci->lock);
  831. if (!err)
  832. isr_setup_status_phase(ci);
  833. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
  834. if (req.wLength != 0)
  835. break;
  836. switch (le16_to_cpu(req.wValue)) {
  837. case USB_DEVICE_REMOTE_WAKEUP:
  838. ci->remote_wakeup = 1;
  839. err = isr_setup_status_phase(ci);
  840. break;
  841. case USB_DEVICE_TEST_MODE:
  842. tmode = le16_to_cpu(req.wIndex) >> 8;
  843. switch (tmode) {
  844. case TEST_J:
  845. case TEST_K:
  846. case TEST_SE0_NAK:
  847. case TEST_PACKET:
  848. case TEST_FORCE_EN:
  849. ci->test_mode = tmode;
  850. err = isr_setup_status_phase(
  851. ci);
  852. break;
  853. default:
  854. break;
  855. }
  856. default:
  857. goto delegate;
  858. }
  859. } else {
  860. goto delegate;
  861. }
  862. break;
  863. default:
  864. delegate:
  865. if (req.wLength == 0) /* no data phase */
  866. ci->ep0_dir = TX;
  867. spin_unlock(&ci->lock);
  868. err = ci->driver->setup(&ci->gadget, &req);
  869. spin_lock(&ci->lock);
  870. break;
  871. }
  872. if (err < 0) {
  873. dbg_event(_usb_addr(mEp), "ERROR", err);
  874. spin_unlock(&ci->lock);
  875. if (usb_ep_set_halt(&mEp->ep))
  876. dev_err(ci->dev, "error: ep_set_halt\n");
  877. spin_lock(&ci->lock);
  878. }
  879. }
  880. }
  881. /******************************************************************************
  882. * ENDPT block
  883. *****************************************************************************/
  884. /**
  885. * ep_enable: configure endpoint, making it usable
  886. *
  887. * Check usb_ep_enable() at "usb_gadget.h" for details
  888. */
  889. static int ep_enable(struct usb_ep *ep,
  890. const struct usb_endpoint_descriptor *desc)
  891. {
  892. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  893. int retval = 0;
  894. unsigned long flags;
  895. if (ep == NULL || desc == NULL)
  896. return -EINVAL;
  897. spin_lock_irqsave(mEp->lock, flags);
  898. /* only internal SW should enable ctrl endpts */
  899. mEp->ep.desc = desc;
  900. if (!list_empty(&mEp->qh.queue))
  901. dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
  902. mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
  903. mEp->num = usb_endpoint_num(desc);
  904. mEp->type = usb_endpoint_type(desc);
  905. mEp->ep.maxpacket = usb_endpoint_maxp(desc);
  906. dbg_event(_usb_addr(mEp), "ENABLE", 0);
  907. mEp->qh.ptr->cap = 0;
  908. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  909. mEp->qh.ptr->cap |= QH_IOS;
  910. else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
  911. mEp->qh.ptr->cap &= ~QH_MULT;
  912. else
  913. mEp->qh.ptr->cap &= ~QH_ZLT;
  914. mEp->qh.ptr->cap |=
  915. (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
  916. mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
  917. /*
  918. * Enable endpoints in the HW other than ep0 as ep0
  919. * is always enabled
  920. */
  921. if (mEp->num)
  922. retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
  923. spin_unlock_irqrestore(mEp->lock, flags);
  924. return retval;
  925. }
  926. /**
  927. * ep_disable: endpoint is no longer usable
  928. *
  929. * Check usb_ep_disable() at "usb_gadget.h" for details
  930. */
  931. static int ep_disable(struct usb_ep *ep)
  932. {
  933. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  934. int direction, retval = 0;
  935. unsigned long flags;
  936. if (ep == NULL)
  937. return -EINVAL;
  938. else if (mEp->ep.desc == NULL)
  939. return -EBUSY;
  940. spin_lock_irqsave(mEp->lock, flags);
  941. /* only internal SW should disable ctrl endpts */
  942. direction = mEp->dir;
  943. do {
  944. dbg_event(_usb_addr(mEp), "DISABLE", 0);
  945. retval |= _ep_nuke(mEp);
  946. retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
  947. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  948. mEp->dir = (mEp->dir == TX) ? RX : TX;
  949. } while (mEp->dir != direction);
  950. mEp->ep.desc = NULL;
  951. spin_unlock_irqrestore(mEp->lock, flags);
  952. return retval;
  953. }
  954. /**
  955. * ep_alloc_request: allocate a request object to use with this endpoint
  956. *
  957. * Check usb_ep_alloc_request() at "usb_gadget.h" for details
  958. */
  959. static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  960. {
  961. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  962. struct ci13xxx_req *mReq = NULL;
  963. if (ep == NULL)
  964. return NULL;
  965. mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
  966. if (mReq != NULL) {
  967. INIT_LIST_HEAD(&mReq->queue);
  968. mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
  969. &mReq->dma);
  970. if (mReq->ptr == NULL) {
  971. kfree(mReq);
  972. mReq = NULL;
  973. }
  974. }
  975. dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
  976. return (mReq == NULL) ? NULL : &mReq->req;
  977. }
  978. /**
  979. * ep_free_request: frees a request object
  980. *
  981. * Check usb_ep_free_request() at "usb_gadget.h" for details
  982. */
  983. static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
  984. {
  985. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  986. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  987. unsigned long flags;
  988. if (ep == NULL || req == NULL) {
  989. return;
  990. } else if (!list_empty(&mReq->queue)) {
  991. dev_err(mEp->ci->dev, "freeing queued request\n");
  992. return;
  993. }
  994. spin_lock_irqsave(mEp->lock, flags);
  995. if (mReq->ptr)
  996. dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
  997. kfree(mReq);
  998. dbg_event(_usb_addr(mEp), "FREE", 0);
  999. spin_unlock_irqrestore(mEp->lock, flags);
  1000. }
  1001. /**
  1002. * ep_queue: queues (submits) an I/O request to an endpoint
  1003. *
  1004. * Check usb_ep_queue()* at usb_gadget.h" for details
  1005. */
  1006. static int ep_queue(struct usb_ep *ep, struct usb_request *req,
  1007. gfp_t __maybe_unused gfp_flags)
  1008. {
  1009. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1010. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1011. struct ci13xxx *ci = mEp->ci;
  1012. int retval = 0;
  1013. unsigned long flags;
  1014. if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
  1015. return -EINVAL;
  1016. spin_lock_irqsave(mEp->lock, flags);
  1017. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  1018. if (req->length)
  1019. mEp = (ci->ep0_dir == RX) ?
  1020. ci->ep0out : ci->ep0in;
  1021. if (!list_empty(&mEp->qh.queue)) {
  1022. _ep_nuke(mEp);
  1023. retval = -EOVERFLOW;
  1024. dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
  1025. _usb_addr(mEp));
  1026. }
  1027. }
  1028. /* first nuke then test link, e.g. previous status has not sent */
  1029. if (!list_empty(&mReq->queue)) {
  1030. retval = -EBUSY;
  1031. dev_err(mEp->ci->dev, "request already in queue\n");
  1032. goto done;
  1033. }
  1034. if (req->length > 4 * CI13XXX_PAGE_SIZE) {
  1035. req->length = 4 * CI13XXX_PAGE_SIZE;
  1036. retval = -EMSGSIZE;
  1037. dev_warn(mEp->ci->dev, "request length truncated\n");
  1038. }
  1039. dbg_queue(_usb_addr(mEp), req, retval);
  1040. /* push request */
  1041. mReq->req.status = -EINPROGRESS;
  1042. mReq->req.actual = 0;
  1043. retval = _hardware_enqueue(mEp, mReq);
  1044. if (retval == -EALREADY) {
  1045. dbg_event(_usb_addr(mEp), "QUEUE", retval);
  1046. retval = 0;
  1047. }
  1048. if (!retval)
  1049. list_add_tail(&mReq->queue, &mEp->qh.queue);
  1050. done:
  1051. spin_unlock_irqrestore(mEp->lock, flags);
  1052. return retval;
  1053. }
  1054. /**
  1055. * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
  1056. *
  1057. * Check usb_ep_dequeue() at "usb_gadget.h" for details
  1058. */
  1059. static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  1060. {
  1061. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1062. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1063. unsigned long flags;
  1064. if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
  1065. mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
  1066. list_empty(&mEp->qh.queue))
  1067. return -EINVAL;
  1068. spin_lock_irqsave(mEp->lock, flags);
  1069. dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
  1070. hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
  1071. /* pop request */
  1072. list_del_init(&mReq->queue);
  1073. usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
  1074. req->status = -ECONNRESET;
  1075. if (mReq->req.complete != NULL) {
  1076. spin_unlock(mEp->lock);
  1077. mReq->req.complete(&mEp->ep, &mReq->req);
  1078. spin_lock(mEp->lock);
  1079. }
  1080. spin_unlock_irqrestore(mEp->lock, flags);
  1081. return 0;
  1082. }
  1083. /**
  1084. * ep_set_halt: sets the endpoint halt feature
  1085. *
  1086. * Check usb_ep_set_halt() at "usb_gadget.h" for details
  1087. */
  1088. static int ep_set_halt(struct usb_ep *ep, int value)
  1089. {
  1090. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1091. int direction, retval = 0;
  1092. unsigned long flags;
  1093. if (ep == NULL || mEp->ep.desc == NULL)
  1094. return -EINVAL;
  1095. spin_lock_irqsave(mEp->lock, flags);
  1096. #ifndef STALL_IN
  1097. /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
  1098. if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
  1099. !list_empty(&mEp->qh.queue)) {
  1100. spin_unlock_irqrestore(mEp->lock, flags);
  1101. return -EAGAIN;
  1102. }
  1103. #endif
  1104. direction = mEp->dir;
  1105. do {
  1106. dbg_event(_usb_addr(mEp), "HALT", value);
  1107. retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
  1108. if (!value)
  1109. mEp->wedge = 0;
  1110. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  1111. mEp->dir = (mEp->dir == TX) ? RX : TX;
  1112. } while (mEp->dir != direction);
  1113. spin_unlock_irqrestore(mEp->lock, flags);
  1114. return retval;
  1115. }
  1116. /**
  1117. * ep_set_wedge: sets the halt feature and ignores clear requests
  1118. *
  1119. * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  1120. */
  1121. static int ep_set_wedge(struct usb_ep *ep)
  1122. {
  1123. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1124. unsigned long flags;
  1125. if (ep == NULL || mEp->ep.desc == NULL)
  1126. return -EINVAL;
  1127. spin_lock_irqsave(mEp->lock, flags);
  1128. dbg_event(_usb_addr(mEp), "WEDGE", 0);
  1129. mEp->wedge = 1;
  1130. spin_unlock_irqrestore(mEp->lock, flags);
  1131. return usb_ep_set_halt(ep);
  1132. }
  1133. /**
  1134. * ep_fifo_flush: flushes contents of a fifo
  1135. *
  1136. * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
  1137. */
  1138. static void ep_fifo_flush(struct usb_ep *ep)
  1139. {
  1140. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1141. unsigned long flags;
  1142. if (ep == NULL) {
  1143. dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
  1144. return;
  1145. }
  1146. spin_lock_irqsave(mEp->lock, flags);
  1147. dbg_event(_usb_addr(mEp), "FFLUSH", 0);
  1148. hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
  1149. spin_unlock_irqrestore(mEp->lock, flags);
  1150. }
  1151. /**
  1152. * Endpoint-specific part of the API to the USB controller hardware
  1153. * Check "usb_gadget.h" for details
  1154. */
  1155. static const struct usb_ep_ops usb_ep_ops = {
  1156. .enable = ep_enable,
  1157. .disable = ep_disable,
  1158. .alloc_request = ep_alloc_request,
  1159. .free_request = ep_free_request,
  1160. .queue = ep_queue,
  1161. .dequeue = ep_dequeue,
  1162. .set_halt = ep_set_halt,
  1163. .set_wedge = ep_set_wedge,
  1164. .fifo_flush = ep_fifo_flush,
  1165. };
  1166. /******************************************************************************
  1167. * GADGET block
  1168. *****************************************************************************/
  1169. static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
  1170. {
  1171. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1172. unsigned long flags;
  1173. int gadget_ready = 0;
  1174. if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
  1175. return -EOPNOTSUPP;
  1176. spin_lock_irqsave(&ci->lock, flags);
  1177. ci->vbus_active = is_active;
  1178. if (ci->driver)
  1179. gadget_ready = 1;
  1180. spin_unlock_irqrestore(&ci->lock, flags);
  1181. if (gadget_ready) {
  1182. if (is_active) {
  1183. pm_runtime_get_sync(&_gadget->dev);
  1184. hw_device_reset(ci, USBMODE_CM_DC);
  1185. hw_enable_vbus_intr(ci);
  1186. hw_device_state(ci, ci->ep0out->qh.dma);
  1187. } else {
  1188. hw_device_state(ci, 0);
  1189. if (ci->platdata->notify_event)
  1190. ci->platdata->notify_event(ci,
  1191. CI13XXX_CONTROLLER_STOPPED_EVENT);
  1192. _gadget_stop_activity(&ci->gadget);
  1193. pm_runtime_put_sync(&_gadget->dev);
  1194. }
  1195. }
  1196. return 0;
  1197. }
  1198. static int ci13xxx_wakeup(struct usb_gadget *_gadget)
  1199. {
  1200. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1201. unsigned long flags;
  1202. int ret = 0;
  1203. spin_lock_irqsave(&ci->lock, flags);
  1204. if (!ci->remote_wakeup) {
  1205. ret = -EOPNOTSUPP;
  1206. goto out;
  1207. }
  1208. if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
  1209. ret = -EINVAL;
  1210. goto out;
  1211. }
  1212. hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
  1213. out:
  1214. spin_unlock_irqrestore(&ci->lock, flags);
  1215. return ret;
  1216. }
  1217. static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1218. {
  1219. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1220. if (ci->transceiver)
  1221. return usb_phy_set_power(ci->transceiver, mA);
  1222. return -ENOTSUPP;
  1223. }
  1224. /* Change Data+ pullup status
  1225. * this func is used by usb_gadget_connect/disconnet
  1226. */
  1227. static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
  1228. {
  1229. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1230. if (is_on)
  1231. hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
  1232. else
  1233. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  1234. return 0;
  1235. }
  1236. static int ci13xxx_start(struct usb_gadget *gadget,
  1237. struct usb_gadget_driver *driver);
  1238. static int ci13xxx_stop(struct usb_gadget *gadget,
  1239. struct usb_gadget_driver *driver);
  1240. /**
  1241. * Device operations part of the API to the USB controller hardware,
  1242. * which don't involve endpoints (or i/o)
  1243. * Check "usb_gadget.h" for details
  1244. */
  1245. static const struct usb_gadget_ops usb_gadget_ops = {
  1246. .vbus_session = ci13xxx_vbus_session,
  1247. .wakeup = ci13xxx_wakeup,
  1248. .pullup = ci13xxx_pullup,
  1249. .vbus_draw = ci13xxx_vbus_draw,
  1250. .udc_start = ci13xxx_start,
  1251. .udc_stop = ci13xxx_stop,
  1252. };
  1253. static int init_eps(struct ci13xxx *ci)
  1254. {
  1255. int retval = 0, i, j;
  1256. for (i = 0; i < ci->hw_ep_max/2; i++)
  1257. for (j = RX; j <= TX; j++) {
  1258. int k = i + j * ci->hw_ep_max/2;
  1259. struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
  1260. scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
  1261. (j == TX) ? "in" : "out");
  1262. mEp->ci = ci;
  1263. mEp->lock = &ci->lock;
  1264. mEp->td_pool = ci->td_pool;
  1265. mEp->ep.name = mEp->name;
  1266. mEp->ep.ops = &usb_ep_ops;
  1267. /*
  1268. * for ep0: maxP defined in desc, for other
  1269. * eps, maxP is set by epautoconfig() called
  1270. * by gadget layer
  1271. */
  1272. mEp->ep.maxpacket = (unsigned short)~0;
  1273. INIT_LIST_HEAD(&mEp->qh.queue);
  1274. mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
  1275. &mEp->qh.dma);
  1276. if (mEp->qh.ptr == NULL)
  1277. retval = -ENOMEM;
  1278. else
  1279. memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
  1280. /*
  1281. * set up shorthands for ep0 out and in endpoints,
  1282. * don't add to gadget's ep_list
  1283. */
  1284. if (i == 0) {
  1285. if (j == RX)
  1286. ci->ep0out = mEp;
  1287. else
  1288. ci->ep0in = mEp;
  1289. mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
  1290. continue;
  1291. }
  1292. list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
  1293. }
  1294. return retval;
  1295. }
  1296. static void destroy_eps(struct ci13xxx *ci)
  1297. {
  1298. int i;
  1299. for (i = 0; i < ci->hw_ep_max; i++) {
  1300. struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
  1301. dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
  1302. }
  1303. }
  1304. /**
  1305. * ci13xxx_start: register a gadget driver
  1306. * @gadget: our gadget
  1307. * @driver: the driver being registered
  1308. *
  1309. * Interrupts are enabled here.
  1310. */
  1311. static int ci13xxx_start(struct usb_gadget *gadget,
  1312. struct usb_gadget_driver *driver)
  1313. {
  1314. struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
  1315. unsigned long flags;
  1316. int retval = -ENOMEM;
  1317. if (driver->disconnect == NULL)
  1318. return -EINVAL;
  1319. ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
  1320. retval = usb_ep_enable(&ci->ep0out->ep);
  1321. if (retval)
  1322. return retval;
  1323. ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
  1324. retval = usb_ep_enable(&ci->ep0in->ep);
  1325. if (retval)
  1326. return retval;
  1327. spin_lock_irqsave(&ci->lock, flags);
  1328. ci->driver = driver;
  1329. pm_runtime_get_sync(&ci->gadget.dev);
  1330. if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
  1331. if (ci->vbus_active) {
  1332. if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
  1333. hw_device_reset(ci, USBMODE_CM_DC);
  1334. hw_enable_vbus_intr(ci);
  1335. }
  1336. } else {
  1337. pm_runtime_put_sync(&ci->gadget.dev);
  1338. goto done;
  1339. }
  1340. }
  1341. retval = hw_device_state(ci, ci->ep0out->qh.dma);
  1342. if (retval)
  1343. pm_runtime_put_sync(&ci->gadget.dev);
  1344. done:
  1345. spin_unlock_irqrestore(&ci->lock, flags);
  1346. return retval;
  1347. }
  1348. /**
  1349. * ci13xxx_stop: unregister a gadget driver
  1350. */
  1351. static int ci13xxx_stop(struct usb_gadget *gadget,
  1352. struct usb_gadget_driver *driver)
  1353. {
  1354. struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
  1355. unsigned long flags;
  1356. spin_lock_irqsave(&ci->lock, flags);
  1357. if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
  1358. ci->vbus_active) {
  1359. hw_device_state(ci, 0);
  1360. if (ci->platdata->notify_event)
  1361. ci->platdata->notify_event(ci,
  1362. CI13XXX_CONTROLLER_STOPPED_EVENT);
  1363. ci->driver = NULL;
  1364. spin_unlock_irqrestore(&ci->lock, flags);
  1365. _gadget_stop_activity(&ci->gadget);
  1366. spin_lock_irqsave(&ci->lock, flags);
  1367. pm_runtime_put(&ci->gadget.dev);
  1368. }
  1369. spin_unlock_irqrestore(&ci->lock, flags);
  1370. return 0;
  1371. }
  1372. /******************************************************************************
  1373. * BUS block
  1374. *****************************************************************************/
  1375. /**
  1376. * udc_irq: ci interrupt handler
  1377. *
  1378. * This function returns IRQ_HANDLED if the IRQ has been handled
  1379. * It locks access to registers
  1380. */
  1381. static irqreturn_t udc_irq(struct ci13xxx *ci)
  1382. {
  1383. irqreturn_t retval;
  1384. u32 intr;
  1385. if (ci == NULL)
  1386. return IRQ_HANDLED;
  1387. spin_lock(&ci->lock);
  1388. if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
  1389. if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
  1390. USBMODE_CM_DC) {
  1391. spin_unlock(&ci->lock);
  1392. return IRQ_NONE;
  1393. }
  1394. }
  1395. intr = hw_test_and_clear_intr_active(ci);
  1396. dbg_interrupt(intr);
  1397. if (intr) {
  1398. /* order defines priority - do NOT change it */
  1399. if (USBi_URI & intr)
  1400. isr_reset_handler(ci);
  1401. if (USBi_PCI & intr) {
  1402. ci->gadget.speed = hw_port_is_high_speed(ci) ?
  1403. USB_SPEED_HIGH : USB_SPEED_FULL;
  1404. if (ci->suspended && ci->driver->resume) {
  1405. spin_unlock(&ci->lock);
  1406. ci->driver->resume(&ci->gadget);
  1407. spin_lock(&ci->lock);
  1408. ci->suspended = 0;
  1409. }
  1410. }
  1411. if (USBi_UI & intr)
  1412. isr_tr_complete_handler(ci);
  1413. if (USBi_SLI & intr) {
  1414. if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
  1415. ci->driver->suspend) {
  1416. ci->suspended = 1;
  1417. spin_unlock(&ci->lock);
  1418. ci->driver->suspend(&ci->gadget);
  1419. spin_lock(&ci->lock);
  1420. }
  1421. }
  1422. retval = IRQ_HANDLED;
  1423. } else {
  1424. retval = IRQ_NONE;
  1425. }
  1426. intr = hw_read(ci, OP_OTGSC, ~0);
  1427. hw_write(ci, OP_OTGSC, ~0, intr);
  1428. if (intr & (OTGSC_AVVIE & OTGSC_AVVIS))
  1429. queue_work(ci->wq, &ci->vbus_work);
  1430. spin_unlock(&ci->lock);
  1431. return retval;
  1432. }
  1433. /**
  1434. * udc_release: driver release function
  1435. * @dev: device
  1436. *
  1437. * Currently does nothing
  1438. */
  1439. static void udc_release(struct device *dev)
  1440. {
  1441. }
  1442. /**
  1443. * udc_start: initialize gadget role
  1444. * @ci: chipidea controller
  1445. */
  1446. static int udc_start(struct ci13xxx *ci)
  1447. {
  1448. struct device *dev = ci->dev;
  1449. int retval = 0;
  1450. spin_lock_init(&ci->lock);
  1451. ci->gadget.ops = &usb_gadget_ops;
  1452. ci->gadget.speed = USB_SPEED_UNKNOWN;
  1453. ci->gadget.max_speed = USB_SPEED_HIGH;
  1454. ci->gadget.is_otg = 0;
  1455. ci->gadget.name = ci->platdata->name;
  1456. INIT_LIST_HEAD(&ci->gadget.ep_list);
  1457. dev_set_name(&ci->gadget.dev, "gadget");
  1458. ci->gadget.dev.dma_mask = dev->dma_mask;
  1459. ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
  1460. ci->gadget.dev.parent = dev;
  1461. ci->gadget.dev.release = udc_release;
  1462. /* alloc resources */
  1463. ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
  1464. sizeof(struct ci13xxx_qh),
  1465. 64, CI13XXX_PAGE_SIZE);
  1466. if (ci->qh_pool == NULL)
  1467. return -ENOMEM;
  1468. ci->td_pool = dma_pool_create("ci13xxx_td", dev,
  1469. sizeof(struct ci13xxx_td),
  1470. 64, CI13XXX_PAGE_SIZE);
  1471. if (ci->td_pool == NULL) {
  1472. retval = -ENOMEM;
  1473. goto free_qh_pool;
  1474. }
  1475. retval = init_eps(ci);
  1476. if (retval)
  1477. goto free_pools;
  1478. ci->gadget.ep0 = &ci->ep0in->ep;
  1479. if (ci->global_phy)
  1480. ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  1481. if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
  1482. if (ci->transceiver == NULL) {
  1483. retval = -ENODEV;
  1484. goto destroy_eps;
  1485. }
  1486. }
  1487. if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
  1488. retval = hw_device_reset(ci, USBMODE_CM_DC);
  1489. if (retval)
  1490. goto put_transceiver;
  1491. hw_enable_vbus_intr(ci);
  1492. }
  1493. retval = device_register(&ci->gadget.dev);
  1494. if (retval) {
  1495. put_device(&ci->gadget.dev);
  1496. goto put_transceiver;
  1497. }
  1498. retval = dbg_create_files(ci->dev);
  1499. if (retval)
  1500. goto unreg_device;
  1501. if (!IS_ERR_OR_NULL(ci->transceiver)) {
  1502. retval = otg_set_peripheral(ci->transceiver->otg,
  1503. &ci->gadget);
  1504. if (retval)
  1505. goto remove_dbg;
  1506. }
  1507. retval = usb_add_gadget_udc(dev, &ci->gadget);
  1508. if (retval)
  1509. goto remove_trans;
  1510. pm_runtime_no_callbacks(&ci->gadget.dev);
  1511. pm_runtime_enable(&ci->gadget.dev);
  1512. return retval;
  1513. remove_trans:
  1514. if (!IS_ERR_OR_NULL(ci->transceiver)) {
  1515. otg_set_peripheral(ci->transceiver->otg, NULL);
  1516. if (ci->global_phy)
  1517. usb_put_phy(ci->transceiver);
  1518. }
  1519. dev_err(dev, "error = %i\n", retval);
  1520. remove_dbg:
  1521. dbg_remove_files(ci->dev);
  1522. unreg_device:
  1523. device_unregister(&ci->gadget.dev);
  1524. put_transceiver:
  1525. if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
  1526. usb_put_phy(ci->transceiver);
  1527. destroy_eps:
  1528. destroy_eps(ci);
  1529. free_pools:
  1530. dma_pool_destroy(ci->td_pool);
  1531. free_qh_pool:
  1532. dma_pool_destroy(ci->qh_pool);
  1533. return retval;
  1534. }
  1535. /**
  1536. * udc_remove: parent remove must call this to remove UDC
  1537. *
  1538. * No interrupts active, the IRQ has been released
  1539. */
  1540. static void udc_stop(struct ci13xxx *ci)
  1541. {
  1542. if (ci == NULL)
  1543. return;
  1544. hw_disable_vbus_intr(ci);
  1545. cancel_work_sync(&ci->vbus_work);
  1546. usb_del_gadget_udc(&ci->gadget);
  1547. destroy_eps(ci);
  1548. dma_pool_destroy(ci->td_pool);
  1549. dma_pool_destroy(ci->qh_pool);
  1550. if (!IS_ERR_OR_NULL(ci->transceiver)) {
  1551. otg_set_peripheral(ci->transceiver->otg, NULL);
  1552. if (ci->global_phy)
  1553. usb_put_phy(ci->transceiver);
  1554. }
  1555. dbg_remove_files(ci->dev);
  1556. device_unregister(&ci->gadget.dev);
  1557. /* my kobject is dynamic, I swear! */
  1558. memset(&ci->gadget, 0, sizeof(ci->gadget));
  1559. }
  1560. /**
  1561. * ci_hdrc_gadget_init - initialize device related bits
  1562. * ci: the controller
  1563. *
  1564. * This function enables the gadget role, if the device is "device capable".
  1565. */
  1566. int ci_hdrc_gadget_init(struct ci13xxx *ci)
  1567. {
  1568. struct ci_role_driver *rdrv;
  1569. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
  1570. return -ENXIO;
  1571. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  1572. if (!rdrv)
  1573. return -ENOMEM;
  1574. rdrv->start = udc_start;
  1575. rdrv->stop = udc_stop;
  1576. rdrv->irq = udc_irq;
  1577. rdrv->name = "gadget";
  1578. ci->roles[CI_ROLE_GADGET] = rdrv;
  1579. INIT_WORK(&ci->vbus_work, vbus_work);
  1580. return 0;
  1581. }