udc.c 45 KB

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