udc.c 43 KB

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