dwc_otg_pcd_linux.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /* ==========================================================================
  2. * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  3. * $Revision: #19 $
  4. * $Date: 2011/10/26 $
  5. * $Change: 1873028 $
  6. *
  7. * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  8. * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  9. * otherwise expressly agreed to in writing between Synopsys and you.
  10. *
  11. * The Software IS NOT an item of Licensed Software or Licensed Product under
  12. * any End User Software License Agreement or Agreement for Licensed Product
  13. * with Synopsys or any supplement thereto. You are permitted to use and
  14. * redistribute this Software in source and binary forms, with or without
  15. * modification, provided that redistributions of source code must retain this
  16. * notice. You may not view, use, disclose, copy or distribute this file or
  17. * any information contained herein except pursuant to this license grant from
  18. * Synopsys. If you do not agree with this notice, including the disclaimer
  19. * below, then you are not authorized to use the Software.
  20. *
  21. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31. * DAMAGE.
  32. * ========================================================================== */
  33. #ifndef DWC_HOST_ONLY
  34. /** @file
  35. * This file implements the Peripheral Controller Driver.
  36. *
  37. * The Peripheral Controller Driver (PCD) is responsible for
  38. * translating requests from the Function Driver into the appropriate
  39. * actions on the DWC_otg controller. It isolates the Function Driver
  40. * from the specifics of the controller by providing an API to the
  41. * Function Driver.
  42. *
  43. * The Peripheral Controller Driver for Linux will implement the
  44. * Gadget API, so that the existing Gadget drivers can be used.
  45. * (Gadget Driver is the Linux terminology for a Function Driver.)
  46. *
  47. * The Linux Gadget API is defined in the header file
  48. * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  49. * defined in the structure <code>usb_ep_ops</code> and the USB
  50. * Controller API is defined in the structure
  51. * <code>usb_gadget_ops</code>.
  52. *
  53. */
  54. #include "dwc_otg_os_dep.h"
  55. #include "dwc_otg_pcd_if.h"
  56. #include "dwc_otg_pcd.h"
  57. #include "dwc_otg_driver.h"
  58. #include "dwc_otg_dbg.h"
  59. static struct gadget_wrapper {
  60. dwc_otg_pcd_t *pcd;
  61. struct usb_gadget gadget;
  62. struct usb_gadget_driver *driver;
  63. struct usb_ep ep0;
  64. struct usb_ep in_ep[16];
  65. struct usb_ep out_ep[16];
  66. } *gadget_wrapper;
  67. /* Display the contents of the buffer */
  68. extern void dump_msg(const u8 * buf, unsigned int length);
  69. /**
  70. * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  71. * if the endpoint is not found
  72. */
  73. static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  74. {
  75. int i;
  76. if (pcd->ep0.priv == handle) {
  77. return &pcd->ep0;
  78. }
  79. for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  80. if (pcd->in_ep[i].priv == handle)
  81. return &pcd->in_ep[i];
  82. if (pcd->out_ep[i].priv == handle)
  83. return &pcd->out_ep[i];
  84. }
  85. return NULL;
  86. }
  87. /* USB Endpoint Operations */
  88. /*
  89. * The following sections briefly describe the behavior of the Gadget
  90. * API endpoint operations implemented in the DWC_otg driver
  91. * software. Detailed descriptions of the generic behavior of each of
  92. * these functions can be found in the Linux header file
  93. * include/linux/usb_gadget.h.
  94. *
  95. * The Gadget API provides wrapper functions for each of the function
  96. * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  97. * function, which then calls the underlying PCD function. The
  98. * following sections are named according to the wrapper
  99. * functions. Within each section, the corresponding DWC_otg PCD
  100. * function name is specified.
  101. *
  102. */
  103. /**
  104. * This function is called by the Gadget Driver for each EP to be
  105. * configured for the current configuration (SET_CONFIGURATION).
  106. *
  107. * This function initializes the dwc_otg_ep_t data structure, and then
  108. * calls dwc_otg_ep_activate.
  109. */
  110. static int ep_enable(struct usb_ep *usb_ep,
  111. const struct usb_endpoint_descriptor *ep_desc)
  112. {
  113. int retval;
  114. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  115. if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  116. DWC_WARN("%s, bad ep or descriptor\n", __func__);
  117. return -EINVAL;
  118. }
  119. if (usb_ep == &gadget_wrapper->ep0) {
  120. DWC_WARN("%s, bad ep(0)\n", __func__);
  121. return -EINVAL;
  122. }
  123. /* Check FIFO size? */
  124. if (!ep_desc->wMaxPacketSize) {
  125. DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  126. return -ERANGE;
  127. }
  128. if (!gadget_wrapper->driver ||
  129. gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  130. DWC_WARN("%s, bogus device state\n", __func__);
  131. return -ESHUTDOWN;
  132. }
  133. /* Delete after check - MAS */
  134. #if 0
  135. nat = (uint32_t) ep_desc->wMaxPacketSize;
  136. printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  137. nat = (nat >> 11) & 0x03;
  138. printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  139. #endif
  140. retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  141. (const uint8_t *)ep_desc,
  142. (void *)usb_ep);
  143. if (retval) {
  144. DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  145. return -EINVAL;
  146. }
  147. usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  148. return 0;
  149. }
  150. /**
  151. * This function is called when an EP is disabled due to disconnect or
  152. * change in configuration. Any pending requests will terminate with a
  153. * status of -ESHUTDOWN.
  154. *
  155. * This function modifies the dwc_otg_ep_t data structure for this EP,
  156. * and then calls dwc_otg_ep_deactivate.
  157. */
  158. static int ep_disable(struct usb_ep *usb_ep)
  159. {
  160. int retval;
  161. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  162. if (!usb_ep) {
  163. DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  164. usb_ep ? usb_ep->name : NULL);
  165. return -EINVAL;
  166. }
  167. retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  168. if (retval) {
  169. retval = -EINVAL;
  170. }
  171. return retval;
  172. }
  173. /**
  174. * This function allocates a request object to use with the specified
  175. * endpoint.
  176. *
  177. * @param ep The endpoint to be used with with the request
  178. * @param gfp_flags the GFP_* flags to use.
  179. */
  180. static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  181. gfp_t gfp_flags)
  182. {
  183. struct usb_request *usb_req;
  184. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  185. if (0 == ep) {
  186. DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  187. return 0;
  188. }
  189. usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  190. if (0 == usb_req) {
  191. DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  192. return 0;
  193. }
  194. memset(usb_req, 0, sizeof(*usb_req));
  195. usb_req->dma = DWC_DMA_ADDR_INVALID;
  196. return usb_req;
  197. }
  198. /**
  199. * This function frees a request object.
  200. *
  201. * @param ep The endpoint associated with the request
  202. * @param req The request being freed
  203. */
  204. static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  205. {
  206. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  207. if (0 == ep || 0 == req) {
  208. DWC_WARN("%s() %s\n", __func__,
  209. "Invalid ep or req argument!\n");
  210. return;
  211. }
  212. kfree(req);
  213. }
  214. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  215. /**
  216. * This function allocates an I/O buffer to be used for a transfer
  217. * to/from the specified endpoint.
  218. *
  219. * @param usb_ep The endpoint to be used with with the request
  220. * @param bytes The desired number of bytes for the buffer
  221. * @param dma Pointer to the buffer's DMA address; must be valid
  222. * @param gfp_flags the GFP_* flags to use.
  223. * @return address of a new buffer or null is buffer could not be allocated.
  224. */
  225. static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  226. dma_addr_t * dma, gfp_t gfp_flags)
  227. {
  228. void *buf;
  229. dwc_otg_pcd_t *pcd = 0;
  230. pcd = gadget_wrapper->pcd;
  231. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  232. dma, gfp_flags);
  233. /* Check dword alignment */
  234. if ((bytes & 0x3UL) != 0) {
  235. DWC_WARN("%s() Buffer size is not a multiple of"
  236. "DWORD size (%d)", __func__, bytes);
  237. }
  238. buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  239. /* Check dword alignment */
  240. if (((int)buf & 0x3UL) != 0) {
  241. DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  242. __func__, buf);
  243. }
  244. return buf;
  245. }
  246. /**
  247. * This function frees an I/O buffer that was allocated by alloc_buffer.
  248. *
  249. * @param usb_ep the endpoint associated with the buffer
  250. * @param buf address of the buffer
  251. * @param dma The buffer's DMA address
  252. * @param bytes The number of bytes of the buffer
  253. */
  254. static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  255. dma_addr_t dma, unsigned bytes)
  256. {
  257. dwc_otg_pcd_t *pcd = 0;
  258. pcd = gadget_wrapper->pcd;
  259. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  260. dma_free_coherent(NULL, bytes, buf, dma);
  261. }
  262. #endif
  263. /**
  264. * This function is used to submit an I/O Request to an EP.
  265. *
  266. * - When the request completes the request's completion callback
  267. * is called to return the request to the driver.
  268. * - An EP, except control EPs, may have multiple requests
  269. * pending.
  270. * - Once submitted the request cannot be examined or modified.
  271. * - Each request is turned into one or more packets.
  272. * - A BULK EP can queue any amount of data; the transfer is
  273. * packetized.
  274. * - Zero length Packets are specified with the request 'zero'
  275. * flag.
  276. */
  277. static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  278. gfp_t gfp_flags)
  279. {
  280. dwc_otg_pcd_t *pcd;
  281. struct dwc_otg_pcd_ep *ep = NULL;
  282. int retval = 0, is_isoc_ep = 0;
  283. dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  284. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  285. __func__, usb_ep, usb_req, gfp_flags);
  286. if (!usb_req || !usb_req->complete || !usb_req->buf) {
  287. DWC_WARN("bad params\n");
  288. return -EINVAL;
  289. }
  290. if (!usb_ep) {
  291. DWC_WARN("bad ep\n");
  292. return -EINVAL;
  293. }
  294. pcd = gadget_wrapper->pcd;
  295. if (!gadget_wrapper->driver ||
  296. gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  297. DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  298. gadget_wrapper->gadget.speed);
  299. DWC_WARN("bogus device state\n");
  300. return -ESHUTDOWN;
  301. }
  302. DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  303. usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  304. usb_req->status = -EINPROGRESS;
  305. usb_req->actual = 0;
  306. ep = ep_from_handle(pcd, usb_ep);
  307. if (ep == NULL)
  308. is_isoc_ep = 0;
  309. else
  310. is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  311. if (GET_CORE_IF(pcd)->dma_enable) {
  312. #ifdef LM_INTERFACE
  313. struct device * dev = &gadget_wrapper->pcd->otg_dev->os_dep.lmdev->dev;
  314. if (usb_req->length != 0/* && usb_req->dma == DWC_DMA_ADDR_INVALID*/) {
  315. dma_addr = dma_map_single(dev, usb_req->buf, usb_req->length,
  316. ep->dwc_ep.is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  317. usb_req->dma = dma_addr;
  318. }
  319. #elif defined(PCI_INTERFACE)
  320. struct pci_dev *dev = gadget_wrapper->pcd->otg_dev->os_dep.pcidev;
  321. if (usb_req->length != 0 && usb_req->dma == DWC_DMA_ADDR_INVALID) {
  322. dma_addr = pci_map_single(dev, usb_req->buf, usb_req->length,
  323. ep->dwc_ep.is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  324. }
  325. #endif
  326. }
  327. #ifdef DWC_UTE_PER_IO
  328. if (is_isoc_ep == 1) {
  329. retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  330. usb_req->length, usb_req->zero, usb_req,
  331. gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  332. if (retval)
  333. return -EINVAL;
  334. return 0;
  335. }
  336. #endif
  337. retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  338. usb_req->length, usb_req->zero, usb_req,
  339. gfp_flags == GFP_ATOMIC ? 1 : 0);
  340. if (retval) {
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. /**
  346. * This function cancels an I/O request from an EP.
  347. */
  348. static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  349. {
  350. DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  351. if (!usb_ep || !usb_req) {
  352. DWC_WARN("bad argument\n");
  353. return -EINVAL;
  354. }
  355. if (!gadget_wrapper->driver ||
  356. gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  357. DWC_WARN("bogus device state\n");
  358. return -ESHUTDOWN;
  359. }
  360. if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * usb_ep_set_halt stalls an endpoint.
  367. *
  368. * usb_ep_clear_halt clears an endpoint halt and resets its data
  369. * toggle.
  370. *
  371. * Both of these functions are implemented with the same underlying
  372. * function. The behavior depends on the value argument.
  373. *
  374. * @param[in] usb_ep the Endpoint to halt or clear halt.
  375. * @param[in] value
  376. * - 0 means clear_halt.
  377. * - 1 means set_halt,
  378. * - 2 means clear stall lock flag.
  379. * - 3 means set stall lock flag.
  380. */
  381. static int ep_halt(struct usb_ep *usb_ep, int value)
  382. {
  383. int retval = 0;
  384. DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  385. if (!usb_ep) {
  386. DWC_WARN("bad ep\n");
  387. return -EINVAL;
  388. }
  389. retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  390. if (retval == -DWC_E_AGAIN) {
  391. return -EAGAIN;
  392. } else if (retval) {
  393. retval = -EINVAL;
  394. }
  395. return retval;
  396. }
  397. #ifdef DWC_EN_ISOC
  398. /**
  399. * This function is used to submit an ISOC Transfer Request to an EP.
  400. *
  401. * - Every time a sync period completes the request's completion callback
  402. * is called to provide data to the gadget driver.
  403. * - Once submitted the request cannot be modified.
  404. * - Each request is turned into periodic data packets untill ISO
  405. * Transfer is stopped..
  406. */
  407. static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  408. gfp_t gfp_flags)
  409. {
  410. int retval = 0;
  411. if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  412. DWC_WARN("bad params\n");
  413. return -EINVAL;
  414. }
  415. if (!usb_ep) {
  416. DWC_PRINTF("bad params\n");
  417. return -EINVAL;
  418. }
  419. req->status = -EINPROGRESS;
  420. retval =
  421. dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  422. req->buf1, req->dma0, req->dma1,
  423. req->sync_frame, req->data_pattern_frame,
  424. req->data_per_frame,
  425. req->flags & USB_REQ_ISO_ASAP ? -1 : req->
  426. start_frame, req->buf_proc_intrvl, req,
  427. gfp_flags == GFP_ATOMIC ? 1 : 0);
  428. if (retval) {
  429. return -EINVAL;
  430. }
  431. return retval;
  432. }
  433. /**
  434. * This function stops ISO EP Periodic Data Transfer.
  435. */
  436. static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  437. {
  438. int retval = 0;
  439. if (!usb_ep) {
  440. DWC_WARN("bad ep\n");
  441. }
  442. if (!gadget_wrapper->driver ||
  443. gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  444. DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  445. gadget_wrapper->gadget.speed);
  446. DWC_WARN("bogus device state\n");
  447. }
  448. dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  449. if (retval) {
  450. retval = -EINVAL;
  451. }
  452. return retval;
  453. }
  454. static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  455. int packets, gfp_t gfp_flags)
  456. {
  457. struct usb_iso_request *pReq = NULL;
  458. uint32_t req_size;
  459. req_size = sizeof(struct usb_iso_request);
  460. req_size +=
  461. (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  462. pReq = kmalloc(req_size, gfp_flags);
  463. if (!pReq) {
  464. DWC_WARN("Can't allocate Iso Request\n");
  465. return 0;
  466. }
  467. pReq->iso_packet_desc0 = (void *)(pReq + 1);
  468. pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  469. return pReq;
  470. }
  471. static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  472. {
  473. kfree(req);
  474. }
  475. static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  476. .ep_ops = {
  477. .enable = ep_enable,
  478. .disable = ep_disable,
  479. .alloc_request = dwc_otg_pcd_alloc_request,
  480. .free_request = dwc_otg_pcd_free_request,
  481. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  482. .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  483. .free_buffer = dwc_otg_pcd_free_buffer,
  484. #endif
  485. .queue = ep_queue,
  486. .dequeue = ep_dequeue,
  487. .set_halt = ep_halt,
  488. .fifo_status = 0,
  489. .fifo_flush = 0,
  490. },
  491. .iso_ep_start = iso_ep_start,
  492. .iso_ep_stop = iso_ep_stop,
  493. .alloc_iso_request = alloc_iso_request,
  494. .free_iso_request = free_iso_request,
  495. };
  496. #else
  497. static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  498. .enable = ep_enable,
  499. .disable = ep_disable,
  500. .alloc_request = dwc_otg_pcd_alloc_request,
  501. .free_request = dwc_otg_pcd_free_request,
  502. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  503. .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  504. .free_buffer = dwc_otg_pcd_free_buffer,
  505. #endif
  506. .queue = ep_queue,
  507. .dequeue = ep_dequeue,
  508. .set_halt = ep_halt,
  509. .fifo_status = 0,
  510. .fifo_flush = 0,
  511. };
  512. #endif /* _EN_ISOC_ */
  513. /* Gadget Operations */
  514. /**
  515. * The following gadget operations will be implemented in the DWC_otg
  516. * PCD. Functions in the API that are not described below are not
  517. * implemented.
  518. *
  519. * The Gadget API provides wrapper functions for each of the function
  520. * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  521. * wrapper function, which then calls the underlying PCD function. The
  522. * following sections are named according to the wrapper functions
  523. * (except for ioctl, which doesn't have a wrapper function). Within
  524. * each section, the corresponding DWC_otg PCD function name is
  525. * specified.
  526. *
  527. */
  528. /**
  529. *Gets the USB Frame number of the last SOF.
  530. */
  531. static int get_frame_number(struct usb_gadget *gadget)
  532. {
  533. struct gadget_wrapper *d;
  534. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  535. if (gadget == 0) {
  536. return -ENODEV;
  537. }
  538. d = container_of(gadget, struct gadget_wrapper, gadget);
  539. return dwc_otg_pcd_get_frame_number(d->pcd);
  540. }
  541. #ifdef CONFIG_USB_DWC_OTG_LPM
  542. static int test_lpm_enabled(struct usb_gadget *gadget)
  543. {
  544. struct gadget_wrapper *d;
  545. d = container_of(gadget, struct gadget_wrapper, gadget);
  546. return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  547. }
  548. #endif
  549. /**
  550. * Initiates Session Request Protocol (SRP) to wakeup the host if no
  551. * session is in progress. If a session is already in progress, but
  552. * the device is suspended, remote wakeup signaling is started.
  553. *
  554. */
  555. static int wakeup(struct usb_gadget *gadget)
  556. {
  557. struct gadget_wrapper *d;
  558. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  559. if (gadget == 0) {
  560. return -ENODEV;
  561. } else {
  562. d = container_of(gadget, struct gadget_wrapper, gadget);
  563. }
  564. dwc_otg_pcd_wakeup(d->pcd);
  565. return 0;
  566. }
  567. //extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  568. static int dwc_otg_pcd_pullup(struct usb_gadget *_gadget, int is_on)
  569. {
  570. struct gadget_wrapper *d;
  571. dwc_otg_core_if_t * core_if;
  572. DWC_DEBUGPL(DBG_PCDV, "%s(%p), is_on %d\n", __func__, _gadget,is_on);
  573. if (_gadget == 0)
  574. return -ENODEV;
  575. d = container_of(_gadget, struct gadget_wrapper, gadget);
  576. core_if = GET_CORE_IF(d->pcd);
  577. DWC_SPINLOCK(core_if->lock);
  578. if(is_on){
  579. if(core_if->dev_if->vbus_on && !core_if->dev_if->pull_up)
  580. dwc_otg_device_soft_connect(core_if);
  581. core_if->dev_if->pull_up = 1;
  582. }else{
  583. if(core_if->dev_if->vbus_on && core_if->dev_if->pull_up)
  584. dwc_otg_device_soft_disconnect(core_if);
  585. core_if->dev_if->pull_up = 0;
  586. }
  587. DWC_SPINUNLOCK(core_if->lock);
  588. return 0;
  589. }
  590. static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  591. .get_frame = get_frame_number,
  592. .wakeup = wakeup,
  593. #ifdef CONFIG_USB_DWC_OTG_LPM
  594. .lpm_support = test_lpm_enabled,
  595. #endif
  596. .pullup = dwc_otg_pcd_pullup,
  597. // current versions must always be self-powered
  598. };
  599. static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  600. {
  601. int retval = -DWC_E_NOT_SUPPORTED;
  602. if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  603. retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  604. (struct usb_ctrlrequest
  605. *)bytes);
  606. }else{
  607. DWC_WARN("no gadget is registered!\n");
  608. }
  609. if (retval == -ENOTSUPP) {
  610. retval = -DWC_E_NOT_SUPPORTED;
  611. } else if (retval < 0) {
  612. retval = -DWC_E_INVALID;
  613. }
  614. return retval;
  615. }
  616. #ifdef DWC_EN_ISOC
  617. static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  618. void *req_handle, int proc_buf_num)
  619. {
  620. int i, packet_count;
  621. struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  622. struct usb_iso_request *iso_req = req_handle;
  623. if (proc_buf_num) {
  624. iso_packet = iso_req->iso_packet_desc1;
  625. } else {
  626. iso_packet = iso_req->iso_packet_desc0;
  627. }
  628. packet_count =
  629. dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  630. for (i = 0; i < packet_count; ++i) {
  631. int status;
  632. int actual;
  633. int offset;
  634. dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  635. i, &status, &actual, &offset);
  636. switch (status) {
  637. case -DWC_E_NO_DATA:
  638. status = -ENODATA;
  639. break;
  640. default:
  641. if (status) {
  642. DWC_PRINTF("unknown status in isoc packet\n");
  643. }
  644. }
  645. iso_packet[i].status = status;
  646. iso_packet[i].offset = offset;
  647. iso_packet[i].actual_length = actual;
  648. }
  649. iso_req->status = 0;
  650. iso_req->process_buffer(ep_handle, iso_req);
  651. return 0;
  652. }
  653. #endif /* DWC_EN_ISOC */
  654. #ifdef DWC_UTE_PER_IO
  655. /**
  656. * Copy the contents of the extended request to the Linux usb_request's
  657. * extended part and call the gadget's completion.
  658. *
  659. * @param pcd Pointer to the pcd structure
  660. * @param ep_handle Void pointer to the usb_ep structure
  661. * @param req_handle Void pointer to the usb_request structure
  662. * @param status Request status returned from the portable logic
  663. * @param ereq_port Void pointer to the extended request structure
  664. * created in the the portable part that contains the
  665. * results of the processed iso packets.
  666. */
  667. static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  668. void *req_handle, int32_t status, void *ereq_port)
  669. {
  670. struct dwc_ute_iso_req_ext *ereqorg = NULL;
  671. struct dwc_iso_xreq_port *ereqport = NULL;
  672. struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  673. int i;
  674. struct usb_request *req;
  675. //struct dwc_ute_iso_packet_descriptor *
  676. //int status = 0;
  677. req = (struct usb_request *)req_handle;
  678. ereqorg = &req->ext_req;
  679. ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  680. desc_org = ereqorg->per_io_frame_descs;
  681. if (req && req->complete) {
  682. /* Copy the request data from the portable logic to our request */
  683. for (i = 0; i < ereqport->pio_pkt_count; i++) {
  684. desc_org[i].actual_length =
  685. ereqport->per_io_frame_descs[i].actual_length;
  686. desc_org[i].status =
  687. ereqport->per_io_frame_descs[i].status;
  688. }
  689. switch (status) {
  690. case -DWC_E_SHUTDOWN:
  691. req->status = -ESHUTDOWN;
  692. break;
  693. case -DWC_E_RESTART:
  694. req->status = -ECONNRESET;
  695. break;
  696. case -DWC_E_INVALID:
  697. req->status = -EINVAL;
  698. break;
  699. case -DWC_E_TIMEOUT:
  700. req->status = -ETIMEDOUT;
  701. break;
  702. default:
  703. req->status = status;
  704. }
  705. /* And call the gadget's completion */
  706. req->complete(ep_handle, req);
  707. }
  708. return 0;
  709. }
  710. #endif /* DWC_UTE_PER_IO */
  711. static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  712. void *req_handle, int32_t status, uint32_t actual)
  713. {
  714. struct usb_request *req = (struct usb_request *)req_handle;
  715. struct dwc_otg_pcd_ep *ep = NULL;
  716. #ifdef PCI_INTERFACE
  717. struct pci_dev *dev = NULL;
  718. #else
  719. struct device * dev = NULL;
  720. #endif
  721. if (req && req->complete) {
  722. switch (status) {
  723. case -DWC_E_SHUTDOWN:
  724. req->status = -ESHUTDOWN;
  725. break;
  726. case -DWC_E_RESTART:
  727. req->status = -ECONNRESET;
  728. break;
  729. case -DWC_E_INVALID:
  730. req->status = -EINVAL;
  731. break;
  732. case -DWC_E_TIMEOUT:
  733. req->status = -ETIMEDOUT;
  734. break;
  735. default:
  736. req->status = status;
  737. }
  738. #ifdef PCI_INTERFACE
  739. dev = gadget_wrapper->pcd->otg_dev->os_dep.pcidev;
  740. #else
  741. dev = &gadget_wrapper->pcd->otg_dev->os_dep.lmdev->dev;
  742. #endif
  743. ep = ep_from_handle(pcd, ep_handle);
  744. if (GET_CORE_IF(pcd)->dma_enable) {
  745. if (req->length != 0)
  746. #ifdef PCI_INTERFACE
  747. pci_unmap_single(dev, req->dma, req->length,
  748. ep->dwc_ep.
  749. is_in ? PCI_DMA_TODEVICE :
  750. PCI_DMA_FROMDEVICE);
  751. #else
  752. dma_unmap_single(dev, req->dma, req->length,
  753. ep->dwc_ep.
  754. is_in ? DMA_TO_DEVICE :
  755. DMA_FROM_DEVICE);
  756. #endif
  757. }
  758. req->actual = actual;
  759. DWC_SPINUNLOCK(pcd->lock);
  760. req->complete(ep_handle, req);
  761. DWC_SPINLOCK(pcd->lock);
  762. }
  763. return 0;
  764. }
  765. static int _connect(dwc_otg_pcd_t * pcd, int speed)
  766. {
  767. gadget_wrapper->gadget.speed = speed;
  768. return 0;
  769. }
  770. static int _disconnect(dwc_otg_pcd_t * pcd)
  771. {
  772. if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  773. gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  774. }
  775. return 0;
  776. }
  777. static int _resume(dwc_otg_pcd_t * pcd)
  778. {
  779. if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  780. gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  781. }
  782. return 0;
  783. }
  784. static int _suspend(dwc_otg_pcd_t * pcd)
  785. {
  786. if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  787. gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  788. }
  789. return 0;
  790. }
  791. /**
  792. * This function updates the otg values in the gadget structure.
  793. */
  794. static int _hnp_changed(dwc_otg_pcd_t * pcd)
  795. {
  796. if (!gadget_wrapper->gadget.is_otg)
  797. return 0;
  798. gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  799. gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  800. gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  801. return 0;
  802. }
  803. static int _reset(dwc_otg_pcd_t * pcd)
  804. {
  805. return 0;
  806. }
  807. #ifdef DWC_UTE_CFI
  808. static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  809. {
  810. int retval = -DWC_E_INVALID;
  811. if (gadget_wrapper->driver->cfi_feature_setup) {
  812. retval =
  813. gadget_wrapper->driver->
  814. cfi_feature_setup(&gadget_wrapper->gadget,
  815. (struct cfi_usb_ctrlrequest *)cfi_req);
  816. }
  817. return retval;
  818. }
  819. #endif
  820. static const struct dwc_otg_pcd_function_ops fops = {
  821. .complete = _complete,
  822. #ifdef DWC_EN_ISOC
  823. .isoc_complete = _isoc_complete,
  824. #endif
  825. .setup = _setup,
  826. .disconnect = _disconnect,
  827. .connect = _connect,
  828. .resume = _resume,
  829. .suspend = _suspend,
  830. .hnp_changed = _hnp_changed,
  831. .reset = _reset,
  832. #ifdef DWC_UTE_CFI
  833. .cfi_setup = _cfi_setup,
  834. #endif
  835. #ifdef DWC_UTE_PER_IO
  836. .xisoc_complete = _xisoc_complete,
  837. #endif
  838. };
  839. /**
  840. * This function is the top level PCD interrupt handler.
  841. */
  842. static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  843. {
  844. dwc_otg_pcd_t *pcd = dev;
  845. int32_t retval = IRQ_NONE;
  846. retval = dwc_otg_pcd_handle_intr(pcd);
  847. if (retval != 0) {
  848. S3C2410X_CLEAR_EINTPEND();
  849. }
  850. return IRQ_RETVAL(retval);
  851. }
  852. /**
  853. * This function initialized the usb_ep structures to there default
  854. * state.
  855. *
  856. * @param d Pointer on gadget_wrapper.
  857. */
  858. void gadget_add_eps(struct gadget_wrapper *d)
  859. {
  860. static const char *names[] = {
  861. "ep0",
  862. "ep1in",
  863. "ep2in",
  864. "ep3in",
  865. "ep4in",
  866. "ep5in",
  867. "ep6in",
  868. "ep7in",
  869. "ep8in",
  870. "ep9in",
  871. "ep10in",
  872. "ep11in",
  873. "ep12in",
  874. "ep13in",
  875. "ep14in",
  876. "ep15in",
  877. "ep1out",
  878. "ep2out",
  879. "ep3out",
  880. "ep4out",
  881. "ep5out",
  882. "ep6out",
  883. "ep7out",
  884. "ep8out",
  885. "ep9out",
  886. "ep10out",
  887. "ep11out",
  888. "ep12out",
  889. "ep13out",
  890. "ep14out",
  891. "ep15out"
  892. };
  893. int i;
  894. struct usb_ep *ep;
  895. int8_t dev_endpoints;
  896. DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  897. INIT_LIST_HEAD(&d->gadget.ep_list);
  898. d->gadget.ep0 = &d->ep0;
  899. d->gadget.speed = USB_SPEED_UNKNOWN;
  900. INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  901. /**
  902. * Initialize the EP0 structure.
  903. */
  904. ep = &d->ep0;
  905. /* Init the usb_ep structure. */
  906. ep->name = names[0];
  907. ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  908. /**
  909. * @todo NGS: What should the max packet size be set to
  910. * here? Before EP type is set?
  911. */
  912. ep->maxpacket = MAX_PACKET_SIZE;
  913. dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  914. list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  915. /**
  916. * Initialize the EP structures.
  917. */
  918. dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  919. for (i = 0; i < dev_endpoints; i++) {
  920. ep = &d->in_ep[i];
  921. /* Init the usb_ep structure. */
  922. ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  923. ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  924. /**
  925. * @todo NGS: What should the max packet size be set to
  926. * here? Before EP type is set?
  927. */
  928. ep->maxpacket = MAX_PACKET_SIZE;
  929. list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  930. }
  931. dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  932. for (i = 0; i < dev_endpoints; i++) {
  933. ep = &d->out_ep[i];
  934. /* Init the usb_ep structure. */
  935. ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  936. ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  937. /**
  938. * @todo NGS: What should the max packet size be set to
  939. * here? Before EP type is set?
  940. */
  941. ep->maxpacket = MAX_PACKET_SIZE;
  942. list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  943. }
  944. /* remove ep0 from the list. There is a ep0 pointer. */
  945. list_del_init(&d->ep0.ep_list);
  946. d->ep0.maxpacket = MAX_EP0_SIZE;
  947. }
  948. /**
  949. * This function releases the Gadget device.
  950. * required by device_unregister().
  951. *
  952. * @todo Should this do something? Should it free the PCD?
  953. */
  954. static void dwc_otg_pcd_gadget_release(struct device *dev)
  955. {
  956. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  957. }
  958. static struct gadget_wrapper *alloc_wrapper(
  959. #ifdef LM_INTERFACE
  960. struct lm_device *_dev
  961. #elif defined(PCI_INTERFACE)
  962. struct pci_dev *_dev
  963. #endif
  964. )
  965. {
  966. static char pcd_name[] = "dwc_otg_pcd";
  967. #ifdef LM_INTERFACE
  968. dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  969. #elif defined(PCI_INTERFACE)
  970. dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  971. #endif
  972. struct gadget_wrapper *d;
  973. int retval;
  974. d = DWC_ALLOC(sizeof(*d));
  975. if (d == NULL) {
  976. return NULL;
  977. }
  978. memset(d, 0, sizeof(*d));
  979. d->gadget.name = pcd_name;
  980. d->pcd = otg_dev->pcd;
  981. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  982. strcpy(d->gadget.dev.bus_id, "gadget");
  983. #else
  984. dev_set_name(&d->gadget.dev, "%s", "gadget");
  985. #endif
  986. d->gadget.dev.parent = &_dev->dev;
  987. d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  988. d->gadget.ops = &dwc_otg_pcd_ops;
  989. d->gadget.is_dualspeed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd);
  990. d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  991. d->driver = 0;
  992. /* Register the gadget device */
  993. retval = device_register(&d->gadget.dev);
  994. if (retval != 0) {
  995. DWC_ERROR("device_register failed\n");
  996. DWC_FREE(d);
  997. return NULL;
  998. }
  999. printk("alloc_wrapper: %p\n",d);
  1000. return d;
  1001. }
  1002. static void free_wrapper(struct gadget_wrapper *d)
  1003. {
  1004. if (d->driver) {
  1005. /* should have been done already by driver model core */
  1006. DWC_WARN("driver '%s' is still registered\n",
  1007. d->driver->driver.name);
  1008. usb_gadget_unregister_driver(d->driver);
  1009. }
  1010. device_unregister(&d->gadget.dev);
  1011. DWC_FREE(d);
  1012. }
  1013. /**
  1014. * This function initialized the PCD portion of the driver.
  1015. *
  1016. */
  1017. int pcd_init(
  1018. #ifdef LM_INTERFACE
  1019. struct lm_device *_dev
  1020. #elif defined(PCI_INTERFACE)
  1021. struct pci_dev *_dev
  1022. #endif
  1023. )
  1024. {
  1025. #ifdef LM_INTERFACE
  1026. dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  1027. #elif defined(PCI_INTERFACE)
  1028. dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  1029. #endif
  1030. int retval = 0;
  1031. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _dev);
  1032. otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  1033. if (!otg_dev->pcd) {
  1034. DWC_ERROR("dwc_otg_pcd_init failed\n");
  1035. return -ENOMEM;
  1036. }
  1037. otg_dev->pcd->otg_dev = otg_dev;
  1038. gadget_wrapper = alloc_wrapper(_dev);
  1039. /*
  1040. * Initialize EP structures
  1041. */
  1042. gadget_add_eps(gadget_wrapper);
  1043. /*
  1044. * Setup interupt handler
  1045. */
  1046. DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n", _dev->irq);
  1047. retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  1048. IRQF_SHARED | IRQF_DISABLED,
  1049. gadget_wrapper->gadget.name, otg_dev->pcd);
  1050. if (retval != 0) {
  1051. DWC_ERROR("request of irq%d failed\n", _dev->irq);
  1052. free_wrapper(gadget_wrapper);
  1053. return -EBUSY;
  1054. }
  1055. dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  1056. return retval;
  1057. }
  1058. /**
  1059. * Cleanup the PCD.
  1060. */
  1061. void pcd_remove(
  1062. #ifdef LM_INTERFACE
  1063. struct lm_device *_dev
  1064. #elif defined(PCI_INTERFACE)
  1065. struct pci_dev *_dev
  1066. #endif
  1067. )
  1068. {
  1069. #ifdef LM_INTERFACE
  1070. dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  1071. #elif defined(PCI_INTERFACE)
  1072. dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  1073. #endif
  1074. dwc_otg_pcd_t *pcd = otg_dev->pcd;
  1075. DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _dev);
  1076. /*
  1077. * Free the IRQ
  1078. */
  1079. free_irq(_dev->irq, pcd);
  1080. dwc_otg_pcd_remove(otg_dev->pcd);
  1081. free_wrapper(gadget_wrapper);
  1082. otg_dev->pcd = 0;
  1083. }
  1084. #ifdef CONFIG_HAS_EARLYSUSPEND
  1085. int get_pcd_ums_state(dwc_otg_pcd_t *pcd)
  1086. {
  1087. if((pcd == gadget_wrapper->pcd) && gadget_wrapper->gadget.priv_data)
  1088. return *(int *)gadget_wrapper->gadget.priv_data;
  1089. return 0;
  1090. }
  1091. #endif
  1092. /**
  1093. * This function registers a gadget driver with the PCD.
  1094. *
  1095. * When a driver is successfully registered, it will receive control
  1096. * requests including set_configuration(), which enables non-control
  1097. * requests. then usb traffic follows until a disconnect is reported.
  1098. * then a host may connect again, or the driver might get unbound.
  1099. *
  1100. * @param driver The driver being registered
  1101. * @param bind The bind function of gadget driver
  1102. */
  1103. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  1104. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1105. #else
  1106. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  1107. int (*bind)(struct usb_gadget *))
  1108. #endif
  1109. {
  1110. int retval;
  1111. DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  1112. driver->driver.name);
  1113. if (!driver || driver->speed == USB_SPEED_UNKNOWN ||
  1114. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  1115. !driver->bind ||
  1116. #else
  1117. !bind ||
  1118. #endif
  1119. !driver->unbind || !driver->disconnect || !driver->setup) {
  1120. DWC_ERROR("usb_gadget_register_driver EINVAL\n");
  1121. return -EINVAL;
  1122. }
  1123. if (gadget_wrapper == 0) {
  1124. DWC_ERROR("usb_gadget_register_driver ENODEV\n");
  1125. return -ENODEV;
  1126. }
  1127. if (gadget_wrapper->driver != 0) {
  1128. DWC_ERROR("usb_gadget_register_driver EBUSY (%p)\n", gadget_wrapper->driver);
  1129. return -EBUSY;
  1130. }
  1131. /* hook up the driver */
  1132. gadget_wrapper->driver = driver;
  1133. gadget_wrapper->gadget.dev.driver = &driver->driver;
  1134. DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  1135. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  1136. retval = driver->bind(&gadget_wrapper->gadget);
  1137. #else
  1138. retval = bind(&gadget_wrapper->gadget);
  1139. #endif
  1140. if (retval) {
  1141. DWC_ERROR("bind to driver %s --> error %d\n",
  1142. driver->driver.name, retval);
  1143. gadget_wrapper->driver = 0;
  1144. gadget_wrapper->gadget.dev.driver = 0;
  1145. return retval;
  1146. }
  1147. DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  1148. driver->driver.name);
  1149. return 0;
  1150. }
  1151. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  1152. EXPORT_SYMBOL(usb_gadget_register_driver);
  1153. #else
  1154. EXPORT_SYMBOL(usb_gadget_probe_driver);
  1155. #endif
  1156. /**
  1157. * This function unregisters a gadget driver
  1158. *
  1159. * @param driver The driver being unregistered
  1160. */
  1161. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1162. {
  1163. //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  1164. if (gadget_wrapper == 0) {
  1165. DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  1166. -ENODEV);
  1167. return -ENODEV;
  1168. }
  1169. if (driver == 0 || driver != gadget_wrapper->driver) {
  1170. DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  1171. -EINVAL);
  1172. return -EINVAL;
  1173. }
  1174. driver->unbind(&gadget_wrapper->gadget);
  1175. gadget_wrapper->driver = 0;
  1176. DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  1177. return 0;
  1178. }
  1179. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1180. #endif /* DWC_HOST_ONLY */