udc.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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 = cpu_to_le32(TD_TERMINATE);
  354. mReq->zptr->token = cpu_to_le32(TD_STATUS_ACTIVE);
  355. if (!mReq->req.no_interrupt)
  356. mReq->zptr->token |= cpu_to_le32(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 = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
  367. mReq->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
  368. mReq->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
  369. if (mReq->zptr) {
  370. mReq->ptr->next = cpu_to_le32(mReq->zdma);
  371. } else {
  372. mReq->ptr->next = cpu_to_le32(TD_TERMINATE);
  373. if (!mReq->req.no_interrupt)
  374. mReq->ptr->token |= cpu_to_le32(TD_IOC);
  375. }
  376. mReq->ptr->page[0] = cpu_to_le32(mReq->req.dma);
  377. for (i = 1; i < 5; i++) {
  378. u32 page = mReq->req.dma + i * CI13XXX_PAGE_SIZE;
  379. page &= ~TD_RESERVED_MASK;
  380. mReq->ptr->page[i] = cpu_to_le32(page);
  381. }
  382. if (!list_empty(&mEp->qh.queue)) {
  383. struct ci13xxx_req *mReqPrev;
  384. int n = hw_ep_bit(mEp->num, mEp->dir);
  385. int tmp_stat;
  386. u32 next = mReq->dma & TD_ADDR_MASK;
  387. mReqPrev = list_entry(mEp->qh.queue.prev,
  388. struct ci13xxx_req, queue);
  389. if (mReqPrev->zptr)
  390. mReqPrev->zptr->next = cpu_to_le32(next);
  391. else
  392. mReqPrev->ptr->next = cpu_to_le32(next);
  393. wmb();
  394. if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
  395. goto done;
  396. do {
  397. hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
  398. tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
  399. } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
  400. hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
  401. if (tmp_stat)
  402. goto done;
  403. }
  404. /* QH configuration */
  405. mEp->qh.ptr->td.next = cpu_to_le32(mReq->dma); /* TERMINATE = 0 */
  406. mEp->qh.ptr->td.token &= cpu_to_le32(~TD_STATUS); /* clear status */
  407. mEp->qh.ptr->cap |= cpu_to_le32(QH_ZLT);
  408. wmb(); /* synchronize before ep prime */
  409. ret = hw_ep_prime(ci, mEp->num, mEp->dir,
  410. mEp->type == USB_ENDPOINT_XFER_CONTROL);
  411. done:
  412. return ret;
  413. }
  414. /**
  415. * _hardware_dequeue: handles a request at hardware level
  416. * @gadget: gadget
  417. * @mEp: endpoint
  418. *
  419. * This function returns an error code
  420. */
  421. static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  422. {
  423. if (mReq->req.status != -EALREADY)
  424. return -EINVAL;
  425. if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->ptr->token) != 0)
  426. return -EBUSY;
  427. if (mReq->zptr) {
  428. if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
  429. return -EBUSY;
  430. dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
  431. mReq->zptr = NULL;
  432. }
  433. mReq->req.status = 0;
  434. usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
  435. mReq->req.status = le32_to_cpu(mReq->ptr->token) & TD_STATUS;
  436. if ((TD_STATUS_HALTED & mReq->req.status) != 0)
  437. mReq->req.status = -1;
  438. else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
  439. mReq->req.status = -1;
  440. else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
  441. mReq->req.status = -1;
  442. mReq->req.actual = le32_to_cpu(mReq->ptr->token) & TD_TOTAL_BYTES;
  443. mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
  444. mReq->req.actual = mReq->req.length - mReq->req.actual;
  445. mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
  446. return mReq->req.actual;
  447. }
  448. /**
  449. * _ep_nuke: dequeues all endpoint requests
  450. * @mEp: endpoint
  451. *
  452. * This function returns an error code
  453. * Caller must hold lock
  454. */
  455. static int _ep_nuke(struct ci13xxx_ep *mEp)
  456. __releases(mEp->lock)
  457. __acquires(mEp->lock)
  458. {
  459. if (mEp == NULL)
  460. return -EINVAL;
  461. hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
  462. while (!list_empty(&mEp->qh.queue)) {
  463. /* pop oldest request */
  464. struct ci13xxx_req *mReq = \
  465. list_entry(mEp->qh.queue.next,
  466. struct ci13xxx_req, queue);
  467. list_del_init(&mReq->queue);
  468. mReq->req.status = -ESHUTDOWN;
  469. if (mReq->req.complete != NULL) {
  470. spin_unlock(mEp->lock);
  471. mReq->req.complete(&mEp->ep, &mReq->req);
  472. spin_lock(mEp->lock);
  473. }
  474. }
  475. return 0;
  476. }
  477. /**
  478. * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
  479. * @gadget: gadget
  480. *
  481. * This function returns an error code
  482. */
  483. static int _gadget_stop_activity(struct usb_gadget *gadget)
  484. {
  485. struct usb_ep *ep;
  486. struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
  487. unsigned long flags;
  488. spin_lock_irqsave(&ci->lock, flags);
  489. ci->gadget.speed = USB_SPEED_UNKNOWN;
  490. ci->remote_wakeup = 0;
  491. ci->suspended = 0;
  492. spin_unlock_irqrestore(&ci->lock, flags);
  493. /* flush all endpoints */
  494. gadget_for_each_ep(ep, gadget) {
  495. usb_ep_fifo_flush(ep);
  496. }
  497. usb_ep_fifo_flush(&ci->ep0out->ep);
  498. usb_ep_fifo_flush(&ci->ep0in->ep);
  499. if (ci->driver)
  500. ci->driver->disconnect(gadget);
  501. /* make sure to disable all endpoints */
  502. gadget_for_each_ep(ep, gadget) {
  503. usb_ep_disable(ep);
  504. }
  505. if (ci->status != NULL) {
  506. usb_ep_free_request(&ci->ep0in->ep, ci->status);
  507. ci->status = NULL;
  508. }
  509. return 0;
  510. }
  511. /******************************************************************************
  512. * ISR block
  513. *****************************************************************************/
  514. /**
  515. * isr_reset_handler: USB reset interrupt handler
  516. * @ci: UDC device
  517. *
  518. * This function resets USB engine after a bus reset occurred
  519. */
  520. static void isr_reset_handler(struct ci13xxx *ci)
  521. __releases(ci->lock)
  522. __acquires(ci->lock)
  523. {
  524. int retval;
  525. spin_unlock(&ci->lock);
  526. retval = _gadget_stop_activity(&ci->gadget);
  527. if (retval)
  528. goto done;
  529. retval = hw_usb_reset(ci);
  530. if (retval)
  531. goto done;
  532. ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
  533. if (ci->status == NULL)
  534. retval = -ENOMEM;
  535. done:
  536. spin_lock(&ci->lock);
  537. if (retval)
  538. dev_err(ci->dev, "error: %i\n", retval);
  539. }
  540. /**
  541. * isr_get_status_complete: get_status request complete function
  542. * @ep: endpoint
  543. * @req: request handled
  544. *
  545. * Caller must release lock
  546. */
  547. static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
  548. {
  549. if (ep == NULL || req == NULL)
  550. return;
  551. kfree(req->buf);
  552. usb_ep_free_request(ep, req);
  553. }
  554. /**
  555. * isr_get_status_response: get_status request response
  556. * @ci: ci struct
  557. * @setup: setup request packet
  558. *
  559. * This function returns an error code
  560. */
  561. static int isr_get_status_response(struct ci13xxx *ci,
  562. struct usb_ctrlrequest *setup)
  563. __releases(mEp->lock)
  564. __acquires(mEp->lock)
  565. {
  566. struct ci13xxx_ep *mEp = ci->ep0in;
  567. struct usb_request *req = NULL;
  568. gfp_t gfp_flags = GFP_ATOMIC;
  569. int dir, num, retval;
  570. if (mEp == NULL || setup == NULL)
  571. return -EINVAL;
  572. spin_unlock(mEp->lock);
  573. req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
  574. spin_lock(mEp->lock);
  575. if (req == NULL)
  576. return -ENOMEM;
  577. req->complete = isr_get_status_complete;
  578. req->length = 2;
  579. req->buf = kzalloc(req->length, gfp_flags);
  580. if (req->buf == NULL) {
  581. retval = -ENOMEM;
  582. goto err_free_req;
  583. }
  584. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  585. /* Assume that device is bus powered for now. */
  586. *(u16 *)req->buf = ci->remote_wakeup << 1;
  587. retval = 0;
  588. } else if ((setup->bRequestType & USB_RECIP_MASK) \
  589. == USB_RECIP_ENDPOINT) {
  590. dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
  591. TX : RX;
  592. num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  593. *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
  594. }
  595. /* else do nothing; reserved for future use */
  596. spin_unlock(mEp->lock);
  597. retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
  598. spin_lock(mEp->lock);
  599. if (retval)
  600. goto err_free_buf;
  601. return 0;
  602. err_free_buf:
  603. kfree(req->buf);
  604. err_free_req:
  605. spin_unlock(mEp->lock);
  606. usb_ep_free_request(&mEp->ep, req);
  607. spin_lock(mEp->lock);
  608. return retval;
  609. }
  610. /**
  611. * isr_setup_status_complete: setup_status request complete function
  612. * @ep: endpoint
  613. * @req: request handled
  614. *
  615. * Caller must release lock. Put the port in test mode if test mode
  616. * feature is selected.
  617. */
  618. static void
  619. isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
  620. {
  621. struct ci13xxx *ci = req->context;
  622. unsigned long flags;
  623. if (ci->setaddr) {
  624. hw_usb_set_address(ci, ci->address);
  625. ci->setaddr = false;
  626. }
  627. spin_lock_irqsave(&ci->lock, flags);
  628. if (ci->test_mode)
  629. hw_port_test_set(ci, ci->test_mode);
  630. spin_unlock_irqrestore(&ci->lock, flags);
  631. }
  632. /**
  633. * isr_setup_status_phase: queues the status phase of a setup transation
  634. * @ci: ci struct
  635. *
  636. * This function returns an error code
  637. */
  638. static int isr_setup_status_phase(struct ci13xxx *ci)
  639. __releases(mEp->lock)
  640. __acquires(mEp->lock)
  641. {
  642. int retval;
  643. struct ci13xxx_ep *mEp;
  644. mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
  645. ci->status->context = ci;
  646. ci->status->complete = isr_setup_status_complete;
  647. spin_unlock(mEp->lock);
  648. retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
  649. spin_lock(mEp->lock);
  650. return retval;
  651. }
  652. /**
  653. * isr_tr_complete_low: transaction complete low level handler
  654. * @mEp: endpoint
  655. *
  656. * This function returns an error code
  657. * Caller must hold lock
  658. */
  659. static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
  660. __releases(mEp->lock)
  661. __acquires(mEp->lock)
  662. {
  663. struct ci13xxx_req *mReq, *mReqTemp;
  664. struct ci13xxx_ep *mEpTemp = mEp;
  665. int retval = 0;
  666. list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
  667. queue) {
  668. retval = _hardware_dequeue(mEp, mReq);
  669. if (retval < 0)
  670. break;
  671. list_del_init(&mReq->queue);
  672. if (mReq->req.complete != NULL) {
  673. spin_unlock(mEp->lock);
  674. if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
  675. mReq->req.length)
  676. mEpTemp = mEp->ci->ep0in;
  677. mReq->req.complete(&mEpTemp->ep, &mReq->req);
  678. spin_lock(mEp->lock);
  679. }
  680. }
  681. if (retval == -EBUSY)
  682. retval = 0;
  683. return retval;
  684. }
  685. /**
  686. * isr_tr_complete_handler: transaction complete interrupt handler
  687. * @ci: UDC descriptor
  688. *
  689. * This function handles traffic events
  690. */
  691. static void isr_tr_complete_handler(struct ci13xxx *ci)
  692. __releases(ci->lock)
  693. __acquires(ci->lock)
  694. {
  695. unsigned i;
  696. u8 tmode = 0;
  697. for (i = 0; i < ci->hw_ep_max; i++) {
  698. struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
  699. int type, num, dir, err = -EINVAL;
  700. struct usb_ctrlrequest req;
  701. if (mEp->ep.desc == NULL)
  702. continue; /* not configured */
  703. if (hw_test_and_clear_complete(ci, i)) {
  704. err = isr_tr_complete_low(mEp);
  705. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  706. if (err > 0) /* needs status phase */
  707. err = isr_setup_status_phase(ci);
  708. if (err < 0) {
  709. spin_unlock(&ci->lock);
  710. if (usb_ep_set_halt(&mEp->ep))
  711. dev_err(ci->dev,
  712. "error: ep_set_halt\n");
  713. spin_lock(&ci->lock);
  714. }
  715. }
  716. }
  717. if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
  718. !hw_test_and_clear_setup_status(ci, i))
  719. continue;
  720. if (i != 0) {
  721. dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
  722. continue;
  723. }
  724. /*
  725. * Flush data and handshake transactions of previous
  726. * setup packet.
  727. */
  728. _ep_nuke(ci->ep0out);
  729. _ep_nuke(ci->ep0in);
  730. /* read_setup_packet */
  731. do {
  732. hw_test_and_set_setup_guard(ci);
  733. memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
  734. } while (!hw_test_and_clear_setup_guard(ci));
  735. type = req.bRequestType;
  736. ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
  737. switch (req.bRequest) {
  738. case USB_REQ_CLEAR_FEATURE:
  739. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  740. le16_to_cpu(req.wValue) ==
  741. USB_ENDPOINT_HALT) {
  742. if (req.wLength != 0)
  743. break;
  744. num = le16_to_cpu(req.wIndex);
  745. dir = num & USB_ENDPOINT_DIR_MASK;
  746. num &= USB_ENDPOINT_NUMBER_MASK;
  747. if (dir) /* TX */
  748. num += ci->hw_ep_max/2;
  749. if (!ci->ci13xxx_ep[num].wedge) {
  750. spin_unlock(&ci->lock);
  751. err = usb_ep_clear_halt(
  752. &ci->ci13xxx_ep[num].ep);
  753. spin_lock(&ci->lock);
  754. if (err)
  755. break;
  756. }
  757. err = isr_setup_status_phase(ci);
  758. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
  759. le16_to_cpu(req.wValue) ==
  760. USB_DEVICE_REMOTE_WAKEUP) {
  761. if (req.wLength != 0)
  762. break;
  763. ci->remote_wakeup = 0;
  764. err = isr_setup_status_phase(ci);
  765. } else {
  766. goto delegate;
  767. }
  768. break;
  769. case USB_REQ_GET_STATUS:
  770. if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
  771. type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
  772. type != (USB_DIR_IN|USB_RECIP_INTERFACE))
  773. goto delegate;
  774. if (le16_to_cpu(req.wLength) != 2 ||
  775. le16_to_cpu(req.wValue) != 0)
  776. break;
  777. err = isr_get_status_response(ci, &req);
  778. break;
  779. case USB_REQ_SET_ADDRESS:
  780. if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
  781. goto delegate;
  782. if (le16_to_cpu(req.wLength) != 0 ||
  783. le16_to_cpu(req.wIndex) != 0)
  784. break;
  785. ci->address = (u8)le16_to_cpu(req.wValue);
  786. ci->setaddr = true;
  787. err = isr_setup_status_phase(ci);
  788. break;
  789. case USB_REQ_SET_FEATURE:
  790. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  791. le16_to_cpu(req.wValue) ==
  792. USB_ENDPOINT_HALT) {
  793. if (req.wLength != 0)
  794. break;
  795. num = le16_to_cpu(req.wIndex);
  796. dir = num & USB_ENDPOINT_DIR_MASK;
  797. num &= USB_ENDPOINT_NUMBER_MASK;
  798. if (dir) /* TX */
  799. num += ci->hw_ep_max/2;
  800. spin_unlock(&ci->lock);
  801. err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
  802. spin_lock(&ci->lock);
  803. if (!err)
  804. isr_setup_status_phase(ci);
  805. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
  806. if (req.wLength != 0)
  807. break;
  808. switch (le16_to_cpu(req.wValue)) {
  809. case USB_DEVICE_REMOTE_WAKEUP:
  810. ci->remote_wakeup = 1;
  811. err = isr_setup_status_phase(ci);
  812. break;
  813. case USB_DEVICE_TEST_MODE:
  814. tmode = le16_to_cpu(req.wIndex) >> 8;
  815. switch (tmode) {
  816. case TEST_J:
  817. case TEST_K:
  818. case TEST_SE0_NAK:
  819. case TEST_PACKET:
  820. case TEST_FORCE_EN:
  821. ci->test_mode = tmode;
  822. err = isr_setup_status_phase(
  823. ci);
  824. break;
  825. default:
  826. break;
  827. }
  828. default:
  829. goto delegate;
  830. }
  831. } else {
  832. goto delegate;
  833. }
  834. break;
  835. default:
  836. delegate:
  837. if (req.wLength == 0) /* no data phase */
  838. ci->ep0_dir = TX;
  839. spin_unlock(&ci->lock);
  840. err = ci->driver->setup(&ci->gadget, &req);
  841. spin_lock(&ci->lock);
  842. break;
  843. }
  844. if (err < 0) {
  845. spin_unlock(&ci->lock);
  846. if (usb_ep_set_halt(&mEp->ep))
  847. dev_err(ci->dev, "error: ep_set_halt\n");
  848. spin_lock(&ci->lock);
  849. }
  850. }
  851. }
  852. /******************************************************************************
  853. * ENDPT block
  854. *****************************************************************************/
  855. /**
  856. * ep_enable: configure endpoint, making it usable
  857. *
  858. * Check usb_ep_enable() at "usb_gadget.h" for details
  859. */
  860. static int ep_enable(struct usb_ep *ep,
  861. const struct usb_endpoint_descriptor *desc)
  862. {
  863. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  864. int retval = 0;
  865. unsigned long flags;
  866. if (ep == NULL || desc == NULL)
  867. return -EINVAL;
  868. spin_lock_irqsave(mEp->lock, flags);
  869. /* only internal SW should enable ctrl endpts */
  870. mEp->ep.desc = desc;
  871. if (!list_empty(&mEp->qh.queue))
  872. dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
  873. mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
  874. mEp->num = usb_endpoint_num(desc);
  875. mEp->type = usb_endpoint_type(desc);
  876. mEp->ep.maxpacket = usb_endpoint_maxp(desc);
  877. mEp->qh.ptr->cap = 0;
  878. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  879. mEp->qh.ptr->cap |= cpu_to_le32(QH_IOS);
  880. else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
  881. mEp->qh.ptr->cap &= cpu_to_le32(~QH_MULT);
  882. else
  883. mEp->qh.ptr->cap &= cpu_to_le32(~QH_ZLT);
  884. mEp->qh.ptr->cap |= cpu_to_le32((mEp->ep.maxpacket << __ffs(QH_MAX_PKT))
  885. & QH_MAX_PKT);
  886. mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
  887. /*
  888. * Enable endpoints in the HW other than ep0 as ep0
  889. * is always enabled
  890. */
  891. if (mEp->num)
  892. retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
  893. spin_unlock_irqrestore(mEp->lock, flags);
  894. return retval;
  895. }
  896. /**
  897. * ep_disable: endpoint is no longer usable
  898. *
  899. * Check usb_ep_disable() at "usb_gadget.h" for details
  900. */
  901. static int ep_disable(struct usb_ep *ep)
  902. {
  903. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  904. int direction, retval = 0;
  905. unsigned long flags;
  906. if (ep == NULL)
  907. return -EINVAL;
  908. else if (mEp->ep.desc == NULL)
  909. return -EBUSY;
  910. spin_lock_irqsave(mEp->lock, flags);
  911. /* only internal SW should disable ctrl endpts */
  912. direction = mEp->dir;
  913. do {
  914. retval |= _ep_nuke(mEp);
  915. retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
  916. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  917. mEp->dir = (mEp->dir == TX) ? RX : TX;
  918. } while (mEp->dir != direction);
  919. mEp->ep.desc = NULL;
  920. spin_unlock_irqrestore(mEp->lock, flags);
  921. return retval;
  922. }
  923. /**
  924. * ep_alloc_request: allocate a request object to use with this endpoint
  925. *
  926. * Check usb_ep_alloc_request() at "usb_gadget.h" for details
  927. */
  928. static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  929. {
  930. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  931. struct ci13xxx_req *mReq = NULL;
  932. if (ep == NULL)
  933. return NULL;
  934. mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
  935. if (mReq != NULL) {
  936. INIT_LIST_HEAD(&mReq->queue);
  937. mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
  938. &mReq->dma);
  939. if (mReq->ptr == NULL) {
  940. kfree(mReq);
  941. mReq = NULL;
  942. }
  943. }
  944. return (mReq == NULL) ? NULL : &mReq->req;
  945. }
  946. /**
  947. * ep_free_request: frees a request object
  948. *
  949. * Check usb_ep_free_request() at "usb_gadget.h" for details
  950. */
  951. static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
  952. {
  953. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  954. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  955. unsigned long flags;
  956. if (ep == NULL || req == NULL) {
  957. return;
  958. } else if (!list_empty(&mReq->queue)) {
  959. dev_err(mEp->ci->dev, "freeing queued request\n");
  960. return;
  961. }
  962. spin_lock_irqsave(mEp->lock, flags);
  963. if (mReq->ptr)
  964. dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
  965. kfree(mReq);
  966. spin_unlock_irqrestore(mEp->lock, flags);
  967. }
  968. /**
  969. * ep_queue: queues (submits) an I/O request to an endpoint
  970. *
  971. * Check usb_ep_queue()* at usb_gadget.h" for details
  972. */
  973. static int ep_queue(struct usb_ep *ep, struct usb_request *req,
  974. gfp_t __maybe_unused gfp_flags)
  975. {
  976. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  977. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  978. struct ci13xxx *ci = mEp->ci;
  979. int retval = 0;
  980. unsigned long flags;
  981. if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
  982. return -EINVAL;
  983. spin_lock_irqsave(mEp->lock, flags);
  984. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  985. if (req->length)
  986. mEp = (ci->ep0_dir == RX) ?
  987. ci->ep0out : ci->ep0in;
  988. if (!list_empty(&mEp->qh.queue)) {
  989. _ep_nuke(mEp);
  990. retval = -EOVERFLOW;
  991. dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
  992. _usb_addr(mEp));
  993. }
  994. }
  995. /* first nuke then test link, e.g. previous status has not sent */
  996. if (!list_empty(&mReq->queue)) {
  997. retval = -EBUSY;
  998. dev_err(mEp->ci->dev, "request already in queue\n");
  999. goto done;
  1000. }
  1001. if (req->length > 4 * CI13XXX_PAGE_SIZE) {
  1002. req->length = 4 * CI13XXX_PAGE_SIZE;
  1003. retval = -EMSGSIZE;
  1004. dev_warn(mEp->ci->dev, "request length truncated\n");
  1005. }
  1006. /* push request */
  1007. mReq->req.status = -EINPROGRESS;
  1008. mReq->req.actual = 0;
  1009. retval = _hardware_enqueue(mEp, mReq);
  1010. if (retval == -EALREADY)
  1011. retval = 0;
  1012. if (!retval)
  1013. list_add_tail(&mReq->queue, &mEp->qh.queue);
  1014. done:
  1015. spin_unlock_irqrestore(mEp->lock, flags);
  1016. return retval;
  1017. }
  1018. /**
  1019. * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
  1020. *
  1021. * Check usb_ep_dequeue() at "usb_gadget.h" for details
  1022. */
  1023. static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  1024. {
  1025. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1026. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1027. unsigned long flags;
  1028. if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
  1029. mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
  1030. list_empty(&mEp->qh.queue))
  1031. return -EINVAL;
  1032. spin_lock_irqsave(mEp->lock, flags);
  1033. hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
  1034. /* pop request */
  1035. list_del_init(&mReq->queue);
  1036. usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
  1037. req->status = -ECONNRESET;
  1038. if (mReq->req.complete != NULL) {
  1039. spin_unlock(mEp->lock);
  1040. mReq->req.complete(&mEp->ep, &mReq->req);
  1041. spin_lock(mEp->lock);
  1042. }
  1043. spin_unlock_irqrestore(mEp->lock, flags);
  1044. return 0;
  1045. }
  1046. /**
  1047. * ep_set_halt: sets the endpoint halt feature
  1048. *
  1049. * Check usb_ep_set_halt() at "usb_gadget.h" for details
  1050. */
  1051. static int ep_set_halt(struct usb_ep *ep, int value)
  1052. {
  1053. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1054. int direction, retval = 0;
  1055. unsigned long flags;
  1056. if (ep == NULL || mEp->ep.desc == NULL)
  1057. return -EINVAL;
  1058. spin_lock_irqsave(mEp->lock, flags);
  1059. #ifndef STALL_IN
  1060. /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
  1061. if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
  1062. !list_empty(&mEp->qh.queue)) {
  1063. spin_unlock_irqrestore(mEp->lock, flags);
  1064. return -EAGAIN;
  1065. }
  1066. #endif
  1067. direction = mEp->dir;
  1068. do {
  1069. retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
  1070. if (!value)
  1071. mEp->wedge = 0;
  1072. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  1073. mEp->dir = (mEp->dir == TX) ? RX : TX;
  1074. } while (mEp->dir != direction);
  1075. spin_unlock_irqrestore(mEp->lock, flags);
  1076. return retval;
  1077. }
  1078. /**
  1079. * ep_set_wedge: sets the halt feature and ignores clear requests
  1080. *
  1081. * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  1082. */
  1083. static int ep_set_wedge(struct usb_ep *ep)
  1084. {
  1085. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1086. unsigned long flags;
  1087. if (ep == NULL || mEp->ep.desc == NULL)
  1088. return -EINVAL;
  1089. spin_lock_irqsave(mEp->lock, flags);
  1090. mEp->wedge = 1;
  1091. spin_unlock_irqrestore(mEp->lock, flags);
  1092. return usb_ep_set_halt(ep);
  1093. }
  1094. /**
  1095. * ep_fifo_flush: flushes contents of a fifo
  1096. *
  1097. * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
  1098. */
  1099. static void ep_fifo_flush(struct usb_ep *ep)
  1100. {
  1101. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1102. unsigned long flags;
  1103. if (ep == NULL) {
  1104. dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
  1105. return;
  1106. }
  1107. spin_lock_irqsave(mEp->lock, flags);
  1108. hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
  1109. spin_unlock_irqrestore(mEp->lock, flags);
  1110. }
  1111. /**
  1112. * Endpoint-specific part of the API to the USB controller hardware
  1113. * Check "usb_gadget.h" for details
  1114. */
  1115. static const struct usb_ep_ops usb_ep_ops = {
  1116. .enable = ep_enable,
  1117. .disable = ep_disable,
  1118. .alloc_request = ep_alloc_request,
  1119. .free_request = ep_free_request,
  1120. .queue = ep_queue,
  1121. .dequeue = ep_dequeue,
  1122. .set_halt = ep_set_halt,
  1123. .set_wedge = ep_set_wedge,
  1124. .fifo_flush = ep_fifo_flush,
  1125. };
  1126. /******************************************************************************
  1127. * GADGET block
  1128. *****************************************************************************/
  1129. static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
  1130. {
  1131. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1132. unsigned long flags;
  1133. int gadget_ready = 0;
  1134. if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
  1135. return -EOPNOTSUPP;
  1136. spin_lock_irqsave(&ci->lock, flags);
  1137. ci->vbus_active = is_active;
  1138. if (ci->driver)
  1139. gadget_ready = 1;
  1140. spin_unlock_irqrestore(&ci->lock, flags);
  1141. if (gadget_ready) {
  1142. if (is_active) {
  1143. pm_runtime_get_sync(&_gadget->dev);
  1144. hw_device_reset(ci, USBMODE_CM_DC);
  1145. hw_device_state(ci, ci->ep0out->qh.dma);
  1146. } else {
  1147. hw_device_state(ci, 0);
  1148. if (ci->platdata->notify_event)
  1149. ci->platdata->notify_event(ci,
  1150. CI13XXX_CONTROLLER_STOPPED_EVENT);
  1151. _gadget_stop_activity(&ci->gadget);
  1152. pm_runtime_put_sync(&_gadget->dev);
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. static int ci13xxx_wakeup(struct usb_gadget *_gadget)
  1158. {
  1159. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1160. unsigned long flags;
  1161. int ret = 0;
  1162. spin_lock_irqsave(&ci->lock, flags);
  1163. if (!ci->remote_wakeup) {
  1164. ret = -EOPNOTSUPP;
  1165. goto out;
  1166. }
  1167. if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
  1168. ret = -EINVAL;
  1169. goto out;
  1170. }
  1171. hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
  1172. out:
  1173. spin_unlock_irqrestore(&ci->lock, flags);
  1174. return ret;
  1175. }
  1176. static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1177. {
  1178. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1179. if (ci->transceiver)
  1180. return usb_phy_set_power(ci->transceiver, mA);
  1181. return -ENOTSUPP;
  1182. }
  1183. /* Change Data+ pullup status
  1184. * this func is used by usb_gadget_connect/disconnet
  1185. */
  1186. static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
  1187. {
  1188. struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
  1189. if (is_on)
  1190. hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
  1191. else
  1192. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  1193. return 0;
  1194. }
  1195. static int ci13xxx_start(struct usb_gadget *gadget,
  1196. struct usb_gadget_driver *driver);
  1197. static int ci13xxx_stop(struct usb_gadget *gadget,
  1198. struct usb_gadget_driver *driver);
  1199. /**
  1200. * Device operations part of the API to the USB controller hardware,
  1201. * which don't involve endpoints (or i/o)
  1202. * Check "usb_gadget.h" for details
  1203. */
  1204. static const struct usb_gadget_ops usb_gadget_ops = {
  1205. .vbus_session = ci13xxx_vbus_session,
  1206. .wakeup = ci13xxx_wakeup,
  1207. .pullup = ci13xxx_pullup,
  1208. .vbus_draw = ci13xxx_vbus_draw,
  1209. .udc_start = ci13xxx_start,
  1210. .udc_stop = ci13xxx_stop,
  1211. };
  1212. static int init_eps(struct ci13xxx *ci)
  1213. {
  1214. int retval = 0, i, j;
  1215. for (i = 0; i < ci->hw_ep_max/2; i++)
  1216. for (j = RX; j <= TX; j++) {
  1217. int k = i + j * ci->hw_ep_max/2;
  1218. struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
  1219. scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
  1220. (j == TX) ? "in" : "out");
  1221. mEp->ci = ci;
  1222. mEp->lock = &ci->lock;
  1223. mEp->td_pool = ci->td_pool;
  1224. mEp->ep.name = mEp->name;
  1225. mEp->ep.ops = &usb_ep_ops;
  1226. /*
  1227. * for ep0: maxP defined in desc, for other
  1228. * eps, maxP is set by epautoconfig() called
  1229. * by gadget layer
  1230. */
  1231. mEp->ep.maxpacket = (unsigned short)~0;
  1232. INIT_LIST_HEAD(&mEp->qh.queue);
  1233. mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
  1234. &mEp->qh.dma);
  1235. if (mEp->qh.ptr == NULL)
  1236. retval = -ENOMEM;
  1237. else
  1238. memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
  1239. /*
  1240. * set up shorthands for ep0 out and in endpoints,
  1241. * don't add to gadget's ep_list
  1242. */
  1243. if (i == 0) {
  1244. if (j == RX)
  1245. ci->ep0out = mEp;
  1246. else
  1247. ci->ep0in = mEp;
  1248. mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
  1249. continue;
  1250. }
  1251. list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
  1252. }
  1253. return retval;
  1254. }
  1255. static void destroy_eps(struct ci13xxx *ci)
  1256. {
  1257. int i;
  1258. for (i = 0; i < ci->hw_ep_max; i++) {
  1259. struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
  1260. dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
  1261. }
  1262. }
  1263. /**
  1264. * ci13xxx_start: register a gadget driver
  1265. * @gadget: our gadget
  1266. * @driver: the driver being registered
  1267. *
  1268. * Interrupts are enabled here.
  1269. */
  1270. static int ci13xxx_start(struct usb_gadget *gadget,
  1271. struct usb_gadget_driver *driver)
  1272. {
  1273. struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
  1274. unsigned long flags;
  1275. int retval = -ENOMEM;
  1276. if (driver->disconnect == NULL)
  1277. return -EINVAL;
  1278. ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
  1279. retval = usb_ep_enable(&ci->ep0out->ep);
  1280. if (retval)
  1281. return retval;
  1282. ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
  1283. retval = usb_ep_enable(&ci->ep0in->ep);
  1284. if (retval)
  1285. return retval;
  1286. spin_lock_irqsave(&ci->lock, flags);
  1287. ci->driver = driver;
  1288. pm_runtime_get_sync(&ci->gadget.dev);
  1289. if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
  1290. if (ci->vbus_active) {
  1291. if (ci->platdata->flags & CI13XXX_REGS_SHARED)
  1292. hw_device_reset(ci, USBMODE_CM_DC);
  1293. } else {
  1294. pm_runtime_put_sync(&ci->gadget.dev);
  1295. goto done;
  1296. }
  1297. }
  1298. retval = hw_device_state(ci, ci->ep0out->qh.dma);
  1299. if (retval)
  1300. pm_runtime_put_sync(&ci->gadget.dev);
  1301. done:
  1302. spin_unlock_irqrestore(&ci->lock, flags);
  1303. return retval;
  1304. }
  1305. /**
  1306. * ci13xxx_stop: unregister a gadget driver
  1307. */
  1308. static int ci13xxx_stop(struct usb_gadget *gadget,
  1309. struct usb_gadget_driver *driver)
  1310. {
  1311. struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
  1312. unsigned long flags;
  1313. spin_lock_irqsave(&ci->lock, flags);
  1314. if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
  1315. ci->vbus_active) {
  1316. hw_device_state(ci, 0);
  1317. if (ci->platdata->notify_event)
  1318. ci->platdata->notify_event(ci,
  1319. CI13XXX_CONTROLLER_STOPPED_EVENT);
  1320. ci->driver = NULL;
  1321. spin_unlock_irqrestore(&ci->lock, flags);
  1322. _gadget_stop_activity(&ci->gadget);
  1323. spin_lock_irqsave(&ci->lock, flags);
  1324. pm_runtime_put(&ci->gadget.dev);
  1325. }
  1326. spin_unlock_irqrestore(&ci->lock, flags);
  1327. return 0;
  1328. }
  1329. /******************************************************************************
  1330. * BUS block
  1331. *****************************************************************************/
  1332. /**
  1333. * udc_irq: ci interrupt handler
  1334. *
  1335. * This function returns IRQ_HANDLED if the IRQ has been handled
  1336. * It locks access to registers
  1337. */
  1338. static irqreturn_t udc_irq(struct ci13xxx *ci)
  1339. {
  1340. irqreturn_t retval;
  1341. u32 intr;
  1342. if (ci == NULL)
  1343. return IRQ_HANDLED;
  1344. spin_lock(&ci->lock);
  1345. if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
  1346. if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
  1347. USBMODE_CM_DC) {
  1348. spin_unlock(&ci->lock);
  1349. return IRQ_NONE;
  1350. }
  1351. }
  1352. intr = hw_test_and_clear_intr_active(ci);
  1353. if (intr) {
  1354. /* order defines priority - do NOT change it */
  1355. if (USBi_URI & intr)
  1356. isr_reset_handler(ci);
  1357. if (USBi_PCI & intr) {
  1358. ci->gadget.speed = hw_port_is_high_speed(ci) ?
  1359. USB_SPEED_HIGH : USB_SPEED_FULL;
  1360. if (ci->suspended && ci->driver->resume) {
  1361. spin_unlock(&ci->lock);
  1362. ci->driver->resume(&ci->gadget);
  1363. spin_lock(&ci->lock);
  1364. ci->suspended = 0;
  1365. }
  1366. }
  1367. if (USBi_UI & intr)
  1368. isr_tr_complete_handler(ci);
  1369. if (USBi_SLI & intr) {
  1370. if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
  1371. ci->driver->suspend) {
  1372. ci->suspended = 1;
  1373. spin_unlock(&ci->lock);
  1374. ci->driver->suspend(&ci->gadget);
  1375. spin_lock(&ci->lock);
  1376. }
  1377. }
  1378. retval = IRQ_HANDLED;
  1379. } else {
  1380. retval = IRQ_NONE;
  1381. }
  1382. spin_unlock(&ci->lock);
  1383. return retval;
  1384. }
  1385. /**
  1386. * udc_release: driver release function
  1387. * @dev: device
  1388. *
  1389. * Currently does nothing
  1390. */
  1391. static void udc_release(struct device *dev)
  1392. {
  1393. }
  1394. /**
  1395. * udc_start: initialize gadget role
  1396. * @ci: chipidea controller
  1397. */
  1398. static int udc_start(struct ci13xxx *ci)
  1399. {
  1400. struct device *dev = ci->dev;
  1401. int retval = 0;
  1402. spin_lock_init(&ci->lock);
  1403. ci->gadget.ops = &usb_gadget_ops;
  1404. ci->gadget.speed = USB_SPEED_UNKNOWN;
  1405. ci->gadget.max_speed = USB_SPEED_HIGH;
  1406. ci->gadget.is_otg = 0;
  1407. ci->gadget.name = ci->platdata->name;
  1408. INIT_LIST_HEAD(&ci->gadget.ep_list);
  1409. dev_set_name(&ci->gadget.dev, "gadget");
  1410. ci->gadget.dev.dma_mask = dev->dma_mask;
  1411. ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
  1412. ci->gadget.dev.parent = dev;
  1413. ci->gadget.dev.release = udc_release;
  1414. /* alloc resources */
  1415. ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
  1416. sizeof(struct ci13xxx_qh),
  1417. 64, CI13XXX_PAGE_SIZE);
  1418. if (ci->qh_pool == NULL)
  1419. return -ENOMEM;
  1420. ci->td_pool = dma_pool_create("ci13xxx_td", dev,
  1421. sizeof(struct ci13xxx_td),
  1422. 64, CI13XXX_PAGE_SIZE);
  1423. if (ci->td_pool == NULL) {
  1424. retval = -ENOMEM;
  1425. goto free_qh_pool;
  1426. }
  1427. retval = init_eps(ci);
  1428. if (retval)
  1429. goto free_pools;
  1430. ci->gadget.ep0 = &ci->ep0in->ep;
  1431. if (ci->global_phy)
  1432. ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  1433. if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
  1434. if (ci->transceiver == NULL) {
  1435. retval = -ENODEV;
  1436. goto destroy_eps;
  1437. }
  1438. }
  1439. if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
  1440. retval = hw_device_reset(ci, USBMODE_CM_DC);
  1441. if (retval)
  1442. goto put_transceiver;
  1443. }
  1444. retval = device_register(&ci->gadget.dev);
  1445. if (retval) {
  1446. put_device(&ci->gadget.dev);
  1447. goto put_transceiver;
  1448. }
  1449. if (!IS_ERR_OR_NULL(ci->transceiver)) {
  1450. retval = otg_set_peripheral(ci->transceiver->otg,
  1451. &ci->gadget);
  1452. if (retval)
  1453. goto unreg_device;
  1454. }
  1455. retval = usb_add_gadget_udc(dev, &ci->gadget);
  1456. if (retval)
  1457. goto remove_trans;
  1458. pm_runtime_no_callbacks(&ci->gadget.dev);
  1459. pm_runtime_enable(&ci->gadget.dev);
  1460. return retval;
  1461. remove_trans:
  1462. if (!IS_ERR_OR_NULL(ci->transceiver)) {
  1463. otg_set_peripheral(ci->transceiver->otg, NULL);
  1464. if (ci->global_phy)
  1465. usb_put_phy(ci->transceiver);
  1466. }
  1467. dev_err(dev, "error = %i\n", retval);
  1468. unreg_device:
  1469. device_unregister(&ci->gadget.dev);
  1470. put_transceiver:
  1471. if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
  1472. usb_put_phy(ci->transceiver);
  1473. destroy_eps:
  1474. destroy_eps(ci);
  1475. free_pools:
  1476. dma_pool_destroy(ci->td_pool);
  1477. free_qh_pool:
  1478. dma_pool_destroy(ci->qh_pool);
  1479. return retval;
  1480. }
  1481. /**
  1482. * udc_remove: parent remove must call this to remove UDC
  1483. *
  1484. * No interrupts active, the IRQ has been released
  1485. */
  1486. static void udc_stop(struct ci13xxx *ci)
  1487. {
  1488. if (ci == NULL)
  1489. return;
  1490. usb_del_gadget_udc(&ci->gadget);
  1491. destroy_eps(ci);
  1492. dma_pool_destroy(ci->td_pool);
  1493. dma_pool_destroy(ci->qh_pool);
  1494. if (!IS_ERR_OR_NULL(ci->transceiver)) {
  1495. otg_set_peripheral(ci->transceiver->otg, NULL);
  1496. if (ci->global_phy)
  1497. usb_put_phy(ci->transceiver);
  1498. }
  1499. device_unregister(&ci->gadget.dev);
  1500. /* my kobject is dynamic, I swear! */
  1501. memset(&ci->gadget, 0, sizeof(ci->gadget));
  1502. }
  1503. /**
  1504. * ci_hdrc_gadget_init - initialize device related bits
  1505. * ci: the controller
  1506. *
  1507. * This function enables the gadget role, if the device is "device capable".
  1508. */
  1509. int ci_hdrc_gadget_init(struct ci13xxx *ci)
  1510. {
  1511. struct ci_role_driver *rdrv;
  1512. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
  1513. return -ENXIO;
  1514. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  1515. if (!rdrv)
  1516. return -ENOMEM;
  1517. rdrv->start = udc_start;
  1518. rdrv->stop = udc_stop;
  1519. rdrv->irq = udc_irq;
  1520. rdrv->name = "gadget";
  1521. ci->roles[CI_ROLE_GADGET] = rdrv;
  1522. return 0;
  1523. }