udc.c 42 KB

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