udc.c 43 KB

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