udc.c 44 KB

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