udc.c 45 KB

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