bdc_ep.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. /*
  2. * bdc_ep.c - BRCM BDC USB3.0 device controller endpoint related functions
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  5. *
  6. * Author: Ashwini Pahuja
  7. *
  8. * Based on drivers under drivers/usb/
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmapool.h>
  22. #include <linux/ioport.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/timer.h>
  28. #include <linux/list.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/device.h>
  32. #include <linux/usb/ch9.h>
  33. #include <linux/usb/gadget.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/pm.h>
  36. #include <linux/io.h>
  37. #include <linux/irq.h>
  38. #include <asm/unaligned.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/usb/composite.h>
  41. #include "bdc.h"
  42. #include "bdc_ep.h"
  43. #include "bdc_cmd.h"
  44. #include "bdc_dbg.h"
  45. static const char * const ep0_state_string[] = {
  46. "WAIT_FOR_SETUP",
  47. "WAIT_FOR_DATA_START",
  48. "WAIT_FOR_DATA_XMIT",
  49. "WAIT_FOR_STATUS_START",
  50. "WAIT_FOR_STATUS_XMIT",
  51. "STATUS_PENDING"
  52. };
  53. /* Free the bdl during ep disable */
  54. static void ep_bd_list_free(struct bdc_ep *ep, u32 num_tabs)
  55. {
  56. struct bd_list *bd_list = &ep->bd_list;
  57. struct bdc *bdc = ep->bdc;
  58. struct bd_table *bd_table;
  59. int index;
  60. dev_dbg(bdc->dev, "%s ep:%s num_tabs:%d\n",
  61. __func__, ep->name, num_tabs);
  62. if (!bd_list->bd_table_array) {
  63. dev_dbg(bdc->dev, "%s already freed\n", ep->name);
  64. return;
  65. }
  66. for (index = 0; index < num_tabs; index++) {
  67. /*
  68. * check if the bd_table struct is allocated ?
  69. * if yes, then check if bd memory has been allocated, then
  70. * free the dma_pool and also the bd_table struct memory
  71. */
  72. bd_table = bd_list->bd_table_array[index];
  73. dev_dbg(bdc->dev, "bd_table:%p index:%d\n", bd_table, index);
  74. if (!bd_table) {
  75. dev_dbg(bdc->dev, "bd_table not allocated\n");
  76. continue;
  77. }
  78. if (!bd_table->start_bd) {
  79. dev_dbg(bdc->dev, "bd dma pool not allocted\n");
  80. continue;
  81. }
  82. dev_dbg(bdc->dev,
  83. "Free dma pool start_bd:%p dma:%llx\n",
  84. bd_table->start_bd,
  85. (unsigned long long)bd_table->dma);
  86. dma_pool_free(bdc->bd_table_pool,
  87. bd_table->start_bd,
  88. bd_table->dma);
  89. /* Free the bd_table structure */
  90. kfree(bd_table);
  91. }
  92. /* Free the bd table array */
  93. kfree(ep->bd_list.bd_table_array);
  94. }
  95. /*
  96. * chain the tables, by insteting a chain bd at the end of prev_table, pointing
  97. * to next_table
  98. */
  99. static inline void chain_table(struct bd_table *prev_table,
  100. struct bd_table *next_table,
  101. u32 bd_p_tab)
  102. {
  103. /* Chain the prev table to next table */
  104. prev_table->start_bd[bd_p_tab-1].offset[0] =
  105. cpu_to_le32(lower_32_bits(next_table->dma));
  106. prev_table->start_bd[bd_p_tab-1].offset[1] =
  107. cpu_to_le32(upper_32_bits(next_table->dma));
  108. prev_table->start_bd[bd_p_tab-1].offset[2] =
  109. 0x0;
  110. prev_table->start_bd[bd_p_tab-1].offset[3] =
  111. cpu_to_le32(MARK_CHAIN_BD);
  112. }
  113. /* Allocate the bdl for ep, during config ep */
  114. static int ep_bd_list_alloc(struct bdc_ep *ep)
  115. {
  116. struct bd_table *prev_table = NULL;
  117. int index, num_tabs, bd_p_tab;
  118. struct bdc *bdc = ep->bdc;
  119. struct bd_table *bd_table;
  120. dma_addr_t dma;
  121. if (usb_endpoint_xfer_isoc(ep->desc))
  122. num_tabs = NUM_TABLES_ISOCH;
  123. else
  124. num_tabs = NUM_TABLES;
  125. bd_p_tab = NUM_BDS_PER_TABLE;
  126. /* if there is only 1 table in bd list then loop chain to self */
  127. dev_dbg(bdc->dev,
  128. "%s ep:%p num_tabs:%d\n",
  129. __func__, ep, num_tabs);
  130. /* Allocate memory for table array */
  131. ep->bd_list.bd_table_array = kzalloc(
  132. num_tabs * sizeof(struct bd_table *),
  133. GFP_ATOMIC);
  134. if (!ep->bd_list.bd_table_array)
  135. return -ENOMEM;
  136. /* Allocate memory for each table */
  137. for (index = 0; index < num_tabs; index++) {
  138. /* Allocate memory for bd_table structure */
  139. bd_table = kzalloc(sizeof(struct bd_table), GFP_ATOMIC);
  140. if (!bd_table)
  141. goto fail;
  142. bd_table->start_bd = dma_pool_alloc(bdc->bd_table_pool,
  143. GFP_ATOMIC,
  144. &dma);
  145. if (!bd_table->start_bd)
  146. goto fail;
  147. bd_table->dma = dma;
  148. dev_dbg(bdc->dev,
  149. "index:%d start_bd:%p dma=%08llx prev_table:%p\n",
  150. index, bd_table->start_bd,
  151. (unsigned long long)bd_table->dma, prev_table);
  152. ep->bd_list.bd_table_array[index] = bd_table;
  153. memset(bd_table->start_bd, 0, bd_p_tab * sizeof(struct bdc_bd));
  154. if (prev_table)
  155. chain_table(prev_table, bd_table, bd_p_tab);
  156. prev_table = bd_table;
  157. }
  158. chain_table(prev_table, ep->bd_list.bd_table_array[0], bd_p_tab);
  159. /* Memory allocation is successful, now init the internal fields */
  160. ep->bd_list.num_tabs = num_tabs;
  161. ep->bd_list.max_bdi = (num_tabs * bd_p_tab) - 1;
  162. ep->bd_list.num_tabs = num_tabs;
  163. ep->bd_list.num_bds_table = bd_p_tab;
  164. ep->bd_list.eqp_bdi = 0;
  165. ep->bd_list.hwd_bdi = 0;
  166. return 0;
  167. fail:
  168. /* Free the bd_table_array, bd_table struct, bd's */
  169. ep_bd_list_free(ep, num_tabs);
  170. return -ENOMEM;
  171. }
  172. /* returns how many bd's are need for this transfer */
  173. static inline int bd_needed_req(struct bdc_req *req)
  174. {
  175. int bd_needed = 0;
  176. int remaining;
  177. /* 1 bd needed for 0 byte transfer */
  178. if (req->usb_req.length == 0)
  179. return 1;
  180. /* remaining bytes after tranfering all max BD size BD's */
  181. remaining = req->usb_req.length % BD_MAX_BUFF_SIZE;
  182. if (remaining)
  183. bd_needed++;
  184. /* How many maximum BUFF size BD's ? */
  185. remaining = req->usb_req.length / BD_MAX_BUFF_SIZE;
  186. bd_needed += remaining;
  187. return bd_needed;
  188. }
  189. /* returns the bd index(bdi) corresponding to bd dma address */
  190. static int bd_add_to_bdi(struct bdc_ep *ep, dma_addr_t bd_dma_addr)
  191. {
  192. struct bd_list *bd_list = &ep->bd_list;
  193. dma_addr_t dma_first_bd, dma_last_bd;
  194. struct bdc *bdc = ep->bdc;
  195. struct bd_table *bd_table;
  196. bool found = false;
  197. int tbi, bdi;
  198. dma_first_bd = dma_last_bd = 0;
  199. dev_dbg(bdc->dev, "%s %llx\n",
  200. __func__, (unsigned long long)bd_dma_addr);
  201. /*
  202. * Find in which table this bd_dma_addr belongs?, go through the table
  203. * array and compare addresses of first and last address of bd of each
  204. * table
  205. */
  206. for (tbi = 0; tbi < bd_list->num_tabs; tbi++) {
  207. bd_table = bd_list->bd_table_array[tbi];
  208. dma_first_bd = bd_table->dma;
  209. dma_last_bd = bd_table->dma +
  210. (sizeof(struct bdc_bd) *
  211. (bd_list->num_bds_table - 1));
  212. dev_dbg(bdc->dev, "dma_first_bd:%llx dma_last_bd:%llx\n",
  213. (unsigned long long)dma_first_bd,
  214. (unsigned long long)dma_last_bd);
  215. if (bd_dma_addr >= dma_first_bd && bd_dma_addr <= dma_last_bd) {
  216. found = true;
  217. break;
  218. }
  219. }
  220. if (unlikely(!found)) {
  221. dev_err(bdc->dev, "%s FATAL err, bd not found\n", __func__);
  222. return -EINVAL;
  223. }
  224. /* Now we know the table, find the bdi */
  225. bdi = (bd_dma_addr - dma_first_bd) / sizeof(struct bdc_bd);
  226. /* return the global bdi, to compare with ep eqp_bdi */
  227. return (bdi + (tbi * bd_list->num_bds_table));
  228. }
  229. /* returns the table index(tbi) of the given bdi */
  230. static int bdi_to_tbi(struct bdc_ep *ep, int bdi)
  231. {
  232. int tbi;
  233. tbi = bdi / ep->bd_list.num_bds_table;
  234. dev_vdbg(ep->bdc->dev,
  235. "bdi:%d num_bds_table:%d tbi:%d\n",
  236. bdi, ep->bd_list.num_bds_table, tbi);
  237. return tbi;
  238. }
  239. /* Find the bdi last bd in the transfer */
  240. static inline int find_end_bdi(struct bdc_ep *ep, int next_hwd_bdi)
  241. {
  242. int end_bdi;
  243. end_bdi = next_hwd_bdi - 1;
  244. if (end_bdi < 0)
  245. end_bdi = ep->bd_list.max_bdi - 1;
  246. else if ((end_bdi % (ep->bd_list.num_bds_table-1)) == 0)
  247. end_bdi--;
  248. return end_bdi;
  249. }
  250. /*
  251. * How many transfer bd's are available on this ep bdl, chain bds are not
  252. * counted in available bds
  253. */
  254. static int bd_available_ep(struct bdc_ep *ep)
  255. {
  256. struct bd_list *bd_list = &ep->bd_list;
  257. int available1, available2;
  258. struct bdc *bdc = ep->bdc;
  259. int chain_bd1, chain_bd2;
  260. int available_bd = 0;
  261. available1 = available2 = chain_bd1 = chain_bd2 = 0;
  262. /* if empty then we have all bd's available - number of chain bd's */
  263. if (bd_list->eqp_bdi == bd_list->hwd_bdi)
  264. return bd_list->max_bdi - bd_list->num_tabs;
  265. /*
  266. * Depending upon where eqp and dqp pointers are, caculate number
  267. * of avaialble bd's
  268. */
  269. if (bd_list->hwd_bdi < bd_list->eqp_bdi) {
  270. /* available bd's are from eqp..max_bds + 0..dqp - chain_bds */
  271. available1 = bd_list->max_bdi - bd_list->eqp_bdi;
  272. available2 = bd_list->hwd_bdi;
  273. chain_bd1 = available1 / bd_list->num_bds_table;
  274. chain_bd2 = available2 / bd_list->num_bds_table;
  275. dev_vdbg(bdc->dev, "chain_bd1:%d chain_bd2:%d\n",
  276. chain_bd1, chain_bd2);
  277. available_bd = available1 + available2 - chain_bd1 - chain_bd2;
  278. } else {
  279. /* available bd's are from eqp..dqp - number of chain bd's */
  280. available1 = bd_list->hwd_bdi - bd_list->eqp_bdi;
  281. /* if gap between eqp and dqp is less than NUM_BDS_PER_TABLE */
  282. if ((bd_list->hwd_bdi - bd_list->eqp_bdi)
  283. <= bd_list->num_bds_table) {
  284. /* If there any chain bd in between */
  285. if (!(bdi_to_tbi(ep, bd_list->hwd_bdi)
  286. == bdi_to_tbi(ep, bd_list->eqp_bdi))) {
  287. available_bd = available1 - 1;
  288. }
  289. } else {
  290. chain_bd1 = available1 / bd_list->num_bds_table;
  291. available_bd = available1 - chain_bd1;
  292. }
  293. }
  294. /*
  295. * we need to keep one extra bd to check if ring is full or empty so
  296. * reduce by 1
  297. */
  298. available_bd--;
  299. dev_vdbg(bdc->dev, "available_bd:%d\n", available_bd);
  300. return available_bd;
  301. }
  302. /* Notify the hardware after queueing the bd to bdl */
  303. void bdc_notify_xfr(struct bdc *bdc, u32 epnum)
  304. {
  305. struct bdc_ep *ep = bdc->bdc_ep_array[epnum];
  306. dev_vdbg(bdc->dev, "%s epnum:%d\n", __func__, epnum);
  307. /*
  308. * We don't have anyway to check if ep state is running,
  309. * except the software flags.
  310. */
  311. if (unlikely(ep->flags & BDC_EP_STOP))
  312. ep->flags &= ~BDC_EP_STOP;
  313. bdc_writel(bdc->regs, BDC_XSFNTF, epnum);
  314. }
  315. /* returns the bd corresponding to bdi */
  316. static struct bdc_bd *bdi_to_bd(struct bdc_ep *ep, int bdi)
  317. {
  318. int tbi = bdi_to_tbi(ep, bdi);
  319. int local_bdi = 0;
  320. local_bdi = bdi - (tbi * ep->bd_list.num_bds_table);
  321. dev_vdbg(ep->bdc->dev,
  322. "%s bdi:%d local_bdi:%d\n",
  323. __func__, bdi, local_bdi);
  324. return (ep->bd_list.bd_table_array[tbi]->start_bd + local_bdi);
  325. }
  326. /* Advance the enqueue pointer */
  327. static void ep_bdlist_eqp_adv(struct bdc_ep *ep)
  328. {
  329. ep->bd_list.eqp_bdi++;
  330. /* if it's chain bd, then move to next */
  331. if (((ep->bd_list.eqp_bdi + 1) % ep->bd_list.num_bds_table) == 0)
  332. ep->bd_list.eqp_bdi++;
  333. /* if the eqp is pointing to last + 1 then move back to 0 */
  334. if (ep->bd_list.eqp_bdi == (ep->bd_list.max_bdi + 1))
  335. ep->bd_list.eqp_bdi = 0;
  336. }
  337. /* Setup the first bd for ep0 transfer */
  338. static int setup_first_bd_ep0(struct bdc *bdc, struct bdc_req *req, u32 *dword3)
  339. {
  340. u16 wValue;
  341. u32 req_len;
  342. req->ep->dir = 0;
  343. req_len = req->usb_req.length;
  344. switch (bdc->ep0_state) {
  345. case WAIT_FOR_DATA_START:
  346. *dword3 |= BD_TYPE_DS;
  347. if (bdc->setup_pkt.bRequestType & USB_DIR_IN)
  348. *dword3 |= BD_DIR_IN;
  349. /* check if zlp will be needed */
  350. wValue = le16_to_cpu(bdc->setup_pkt.wValue);
  351. if ((wValue > req_len) &&
  352. (req_len % bdc->gadget.ep0->maxpacket == 0)) {
  353. dev_dbg(bdc->dev, "ZLP needed wVal:%d len:%d MaxP:%d\n",
  354. wValue, req_len,
  355. bdc->gadget.ep0->maxpacket);
  356. bdc->zlp_needed = true;
  357. }
  358. break;
  359. case WAIT_FOR_STATUS_START:
  360. *dword3 |= BD_TYPE_SS;
  361. if (!le16_to_cpu(bdc->setup_pkt.wLength) ||
  362. !(bdc->setup_pkt.bRequestType & USB_DIR_IN))
  363. *dword3 |= BD_DIR_IN;
  364. break;
  365. default:
  366. dev_err(bdc->dev,
  367. "Unknown ep0 state for queueing bd ep0_state:%s\n",
  368. ep0_state_string[bdc->ep0_state]);
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. /* Setup the bd dma descriptor for a given request */
  374. static int setup_bd_list_xfr(struct bdc *bdc, struct bdc_req *req, int num_bds)
  375. {
  376. dma_addr_t buf_add = req->usb_req.dma;
  377. u32 maxp, tfs, dword2, dword3;
  378. struct bd_transfer *bd_xfr;
  379. struct bd_list *bd_list;
  380. struct bdc_ep *ep;
  381. struct bdc_bd *bd;
  382. int ret, bdnum;
  383. u32 req_len;
  384. ep = req->ep;
  385. bd_list = &ep->bd_list;
  386. bd_xfr = &req->bd_xfr;
  387. bd_xfr->req = req;
  388. bd_xfr->start_bdi = bd_list->eqp_bdi;
  389. bd = bdi_to_bd(ep, bd_list->eqp_bdi);
  390. req_len = req->usb_req.length;
  391. maxp = usb_endpoint_maxp(ep->desc) & 0x7ff;
  392. tfs = roundup(req->usb_req.length, maxp);
  393. tfs = tfs/maxp;
  394. dev_vdbg(bdc->dev, "%s ep:%s num_bds:%d tfs:%d r_len:%d bd:%p\n",
  395. __func__, ep->name, num_bds, tfs, req_len, bd);
  396. for (bdnum = 0; bdnum < num_bds; bdnum++) {
  397. dword2 = dword3 = 0;
  398. /* First bd */
  399. if (!bdnum) {
  400. dword3 |= BD_SOT|BD_SBF|(tfs<<BD_TFS_SHIFT);
  401. dword2 |= BD_LTF;
  402. /* format of first bd for ep0 is different than other */
  403. if (ep->ep_num == 1) {
  404. ret = setup_first_bd_ep0(bdc, req, &dword3);
  405. if (ret)
  406. return ret;
  407. }
  408. }
  409. if (!req->ep->dir)
  410. dword3 |= BD_ISP;
  411. if (req_len > BD_MAX_BUFF_SIZE) {
  412. dword2 |= BD_MAX_BUFF_SIZE;
  413. req_len -= BD_MAX_BUFF_SIZE;
  414. } else {
  415. /* this should be the last bd */
  416. dword2 |= req_len;
  417. dword3 |= BD_IOC;
  418. dword3 |= BD_EOT;
  419. }
  420. /* Currently only 1 INT target is supported */
  421. dword2 |= BD_INTR_TARGET(0);
  422. bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
  423. if (unlikely(!bd)) {
  424. dev_err(bdc->dev, "Err bd pointing to wrong addr\n");
  425. return -EINVAL;
  426. }
  427. /* write bd */
  428. bd->offset[0] = cpu_to_le32(lower_32_bits(buf_add));
  429. bd->offset[1] = cpu_to_le32(upper_32_bits(buf_add));
  430. bd->offset[2] = cpu_to_le32(dword2);
  431. bd->offset[3] = cpu_to_le32(dword3);
  432. /* advance eqp pointer */
  433. ep_bdlist_eqp_adv(ep);
  434. /* advance the buff pointer */
  435. buf_add += BD_MAX_BUFF_SIZE;
  436. dev_vdbg(bdc->dev, "buf_add:%08llx req_len:%d bd:%p eqp:%d\n",
  437. (unsigned long long)buf_add, req_len, bd,
  438. ep->bd_list.eqp_bdi);
  439. bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
  440. bd->offset[3] = cpu_to_le32(BD_SBF);
  441. }
  442. /* clear the STOP BD fetch bit from the first bd of this xfr */
  443. bd = bdi_to_bd(ep, bd_xfr->start_bdi);
  444. bd->offset[3] &= cpu_to_le32(~BD_SBF);
  445. /* the new eqp will be next hw dqp */
  446. bd_xfr->num_bds = num_bds;
  447. bd_xfr->next_hwd_bdi = ep->bd_list.eqp_bdi;
  448. /* everything is written correctly before notifying the HW */
  449. wmb();
  450. return 0;
  451. }
  452. /* Queue the xfr */
  453. static int bdc_queue_xfr(struct bdc *bdc, struct bdc_req *req)
  454. {
  455. int num_bds, bd_available;
  456. struct bdc_ep *ep;
  457. int ret;
  458. ep = req->ep;
  459. dev_dbg(bdc->dev, "%s req:%p\n", __func__, req);
  460. dev_dbg(bdc->dev, "eqp_bdi:%d hwd_bdi:%d\n",
  461. ep->bd_list.eqp_bdi, ep->bd_list.hwd_bdi);
  462. num_bds = bd_needed_req(req);
  463. bd_available = bd_available_ep(ep);
  464. /* how many bd's are avaialble on ep */
  465. if (num_bds > bd_available)
  466. return -ENOMEM;
  467. ret = setup_bd_list_xfr(bdc, req, num_bds);
  468. if (ret)
  469. return ret;
  470. list_add_tail(&req->queue, &ep->queue);
  471. bdc_dbg_bd_list(bdc, ep);
  472. bdc_notify_xfr(bdc, ep->ep_num);
  473. return 0;
  474. }
  475. /* callback to gadget layer when xfr completes */
  476. static void bdc_req_complete(struct bdc_ep *ep, struct bdc_req *req,
  477. int status)
  478. {
  479. struct bdc *bdc = ep->bdc;
  480. if (req == NULL || &req->queue == NULL || &req->usb_req == NULL)
  481. return;
  482. dev_dbg(bdc->dev, "%s ep:%s status:%d\n", __func__, ep->name, status);
  483. list_del(&req->queue);
  484. req->usb_req.status = status;
  485. usb_gadget_unmap_request(&bdc->gadget, &req->usb_req, ep->dir);
  486. if (req->usb_req.complete) {
  487. spin_unlock(&bdc->lock);
  488. usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
  489. spin_lock(&bdc->lock);
  490. }
  491. }
  492. /* Disable the endpoint */
  493. int bdc_ep_disable(struct bdc_ep *ep)
  494. {
  495. struct bdc_req *req;
  496. struct bdc *bdc;
  497. int ret;
  498. ret = 0;
  499. bdc = ep->bdc;
  500. dev_dbg(bdc->dev, "%s() ep->ep_num=%d\n", __func__, ep->ep_num);
  501. /* Stop the endpoint */
  502. ret = bdc_stop_ep(bdc, ep->ep_num);
  503. /*
  504. * Intentionally don't check the ret value of stop, it can fail in
  505. * disconnect scenarios, continue with dconfig
  506. */
  507. /* de-queue any pending requests */
  508. while (!list_empty(&ep->queue)) {
  509. req = list_entry(ep->queue.next, struct bdc_req,
  510. queue);
  511. bdc_req_complete(ep, req, -ESHUTDOWN);
  512. }
  513. /* deconfigure the endpoint */
  514. ret = bdc_dconfig_ep(bdc, ep);
  515. if (ret)
  516. dev_warn(bdc->dev,
  517. "dconfig fail but continue with memory free");
  518. ep->flags = 0;
  519. /* ep0 memory is not freed, but reused on next connect sr */
  520. if (ep->ep_num == 1)
  521. return 0;
  522. /* Free the bdl memory */
  523. ep_bd_list_free(ep, ep->bd_list.num_tabs);
  524. ep->desc = NULL;
  525. ep->comp_desc = NULL;
  526. ep->usb_ep.desc = NULL;
  527. ep->ep_type = 0;
  528. return ret;
  529. }
  530. /* Enable the ep */
  531. int bdc_ep_enable(struct bdc_ep *ep)
  532. {
  533. struct bdc *bdc;
  534. int ret = 0;
  535. bdc = ep->bdc;
  536. dev_dbg(bdc->dev, "%s NUM_TABLES:%d %d\n",
  537. __func__, NUM_TABLES, NUM_TABLES_ISOCH);
  538. ret = ep_bd_list_alloc(ep);
  539. if (ret) {
  540. dev_err(bdc->dev, "ep bd list allocation failed:%d\n", ret);
  541. return -ENOMEM;
  542. }
  543. bdc_dbg_bd_list(bdc, ep);
  544. /* only for ep0: config ep is called for ep0 from connect event */
  545. ep->flags |= BDC_EP_ENABLED;
  546. if (ep->ep_num == 1)
  547. return ret;
  548. /* Issue a configure endpoint command */
  549. ret = bdc_config_ep(bdc, ep);
  550. if (ret)
  551. return ret;
  552. ep->usb_ep.maxpacket = usb_endpoint_maxp(ep->desc);
  553. ep->usb_ep.desc = ep->desc;
  554. ep->usb_ep.comp_desc = ep->comp_desc;
  555. ep->ep_type = usb_endpoint_type(ep->desc);
  556. ep->flags |= BDC_EP_ENABLED;
  557. return 0;
  558. }
  559. /* EP0 related code */
  560. /* Queue a status stage BD */
  561. static int ep0_queue_status_stage(struct bdc *bdc)
  562. {
  563. struct bdc_req *status_req;
  564. struct bdc_ep *ep;
  565. status_req = &bdc->status_req;
  566. ep = bdc->bdc_ep_array[1];
  567. status_req->ep = ep;
  568. status_req->usb_req.length = 0;
  569. status_req->usb_req.status = -EINPROGRESS;
  570. status_req->usb_req.actual = 0;
  571. status_req->usb_req.complete = NULL;
  572. bdc_queue_xfr(bdc, status_req);
  573. return 0;
  574. }
  575. /* Queue xfr on ep0 */
  576. static int ep0_queue(struct bdc_ep *ep, struct bdc_req *req)
  577. {
  578. struct bdc *bdc;
  579. int ret;
  580. bdc = ep->bdc;
  581. dev_dbg(bdc->dev, "%s()\n", __func__);
  582. req->usb_req.actual = 0;
  583. req->usb_req.status = -EINPROGRESS;
  584. req->epnum = ep->ep_num;
  585. if (bdc->delayed_status) {
  586. bdc->delayed_status = false;
  587. /* if status stage was delayed? */
  588. if (bdc->ep0_state == WAIT_FOR_STATUS_START) {
  589. /* Queue a status stage BD */
  590. ep0_queue_status_stage(bdc);
  591. bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
  592. return 0;
  593. }
  594. } else {
  595. /*
  596. * if delayed status is false and 0 length transfer is requested
  597. * i.e. for status stage of some setup request, then just
  598. * return from here the status stage is queued independently
  599. */
  600. if (req->usb_req.length == 0)
  601. return 0;
  602. }
  603. ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
  604. if (ret) {
  605. dev_err(bdc->dev, "dma mapping failed %s\n", ep->name);
  606. return ret;
  607. }
  608. return bdc_queue_xfr(bdc, req);
  609. }
  610. /* Queue data stage */
  611. static int ep0_queue_data_stage(struct bdc *bdc)
  612. {
  613. struct usb_request *ep0_usb_req;
  614. struct bdc_ep *ep;
  615. dev_dbg(bdc->dev, "%s\n", __func__);
  616. ep0_usb_req = &bdc->ep0_req.usb_req;
  617. ep = bdc->bdc_ep_array[1];
  618. bdc->ep0_req.ep = ep;
  619. bdc->ep0_req.usb_req.complete = NULL;
  620. return ep0_queue(ep, &bdc->ep0_req);
  621. }
  622. /* Queue req on ep */
  623. static int ep_queue(struct bdc_ep *ep, struct bdc_req *req)
  624. {
  625. struct bdc *bdc;
  626. int ret = 0;
  627. if (!req || !ep || !ep->usb_ep.desc)
  628. return -EINVAL;
  629. bdc = ep->bdc;
  630. req->usb_req.actual = 0;
  631. req->usb_req.status = -EINPROGRESS;
  632. req->epnum = ep->ep_num;
  633. ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
  634. if (ret) {
  635. dev_err(bdc->dev, "dma mapping failed\n");
  636. return ret;
  637. }
  638. return bdc_queue_xfr(bdc, req);
  639. }
  640. /* Dequeue a request from ep */
  641. static int ep_dequeue(struct bdc_ep *ep, struct bdc_req *req)
  642. {
  643. int start_bdi, end_bdi, tbi, eqp_bdi, curr_hw_dqpi;
  644. bool start_pending, end_pending;
  645. bool first_remove = false;
  646. struct bdc_req *first_req;
  647. struct bdc_bd *bd_start;
  648. struct bd_table *table;
  649. dma_addr_t next_bd_dma;
  650. u64 deq_ptr_64 = 0;
  651. struct bdc *bdc;
  652. u32 tmp_32;
  653. int ret;
  654. bdc = ep->bdc;
  655. start_pending = end_pending = false;
  656. eqp_bdi = ep->bd_list.eqp_bdi - 1;
  657. if (eqp_bdi < 0)
  658. eqp_bdi = ep->bd_list.max_bdi;
  659. start_bdi = req->bd_xfr.start_bdi;
  660. end_bdi = find_end_bdi(ep, req->bd_xfr.next_hwd_bdi);
  661. dev_dbg(bdc->dev, "%s ep:%s start:%d end:%d\n",
  662. __func__, ep->name, start_bdi, end_bdi);
  663. dev_dbg(bdc->dev, "ep_dequeue ep=%p ep->desc=%p\n",
  664. ep, (void *)ep->usb_ep.desc);
  665. /* Stop the ep to see where the HW is ? */
  666. ret = bdc_stop_ep(bdc, ep->ep_num);
  667. /* if there is an issue with stopping ep, then no need to go further */
  668. if (ret)
  669. return 0;
  670. /*
  671. * After endpoint is stopped, there can be 3 cases, the request
  672. * is processed, pending or in the middle of processing
  673. */
  674. /* The current hw dequeue pointer */
  675. tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS0(0));
  676. deq_ptr_64 = tmp_32;
  677. tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS0(1));
  678. deq_ptr_64 |= ((u64)tmp_32 << 32);
  679. /* we have the dma addr of next bd that will be fetched by hardware */
  680. curr_hw_dqpi = bd_add_to_bdi(ep, deq_ptr_64);
  681. if (curr_hw_dqpi < 0)
  682. return curr_hw_dqpi;
  683. /*
  684. * curr_hw_dqpi points to actual dqp of HW and HW owns bd's from
  685. * curr_hw_dqbdi..eqp_bdi.
  686. */
  687. /* Check if start_bdi and end_bdi are in range of HW owned BD's */
  688. if (curr_hw_dqpi > eqp_bdi) {
  689. /* there is a wrap from last to 0 */
  690. if (start_bdi >= curr_hw_dqpi || start_bdi <= eqp_bdi) {
  691. start_pending = true;
  692. end_pending = true;
  693. } else if (end_bdi >= curr_hw_dqpi || end_bdi <= eqp_bdi) {
  694. end_pending = true;
  695. }
  696. } else {
  697. if (start_bdi >= curr_hw_dqpi) {
  698. start_pending = true;
  699. end_pending = true;
  700. } else if (end_bdi >= curr_hw_dqpi) {
  701. end_pending = true;
  702. }
  703. }
  704. dev_dbg(bdc->dev,
  705. "start_pending:%d end_pending:%d speed:%d\n",
  706. start_pending, end_pending, bdc->gadget.speed);
  707. /* If both start till end are processes, we cannot deq req */
  708. if (!start_pending && !end_pending)
  709. return -EINVAL;
  710. /*
  711. * if ep_dequeue is called after disconnect then just return
  712. * success from here
  713. */
  714. if (bdc->gadget.speed == USB_SPEED_UNKNOWN)
  715. return 0;
  716. tbi = bdi_to_tbi(ep, req->bd_xfr.next_hwd_bdi);
  717. table = ep->bd_list.bd_table_array[tbi];
  718. next_bd_dma = table->dma +
  719. sizeof(struct bdc_bd)*(req->bd_xfr.next_hwd_bdi -
  720. tbi * ep->bd_list.num_bds_table);
  721. first_req = list_first_entry(&ep->queue, struct bdc_req,
  722. queue);
  723. if (req == first_req)
  724. first_remove = true;
  725. /*
  726. * Due to HW limitation we need to bypadd chain bd's and issue ep_bla,
  727. * incase if start is pending this is the first request in the list
  728. * then issue ep_bla instead of marking as chain bd
  729. */
  730. if (start_pending && !first_remove) {
  731. /*
  732. * Mark the start bd as Chain bd, and point the chain
  733. * bd to next_bd_dma
  734. */
  735. bd_start = bdi_to_bd(ep, start_bdi);
  736. bd_start->offset[0] = cpu_to_le32(lower_32_bits(next_bd_dma));
  737. bd_start->offset[1] = cpu_to_le32(upper_32_bits(next_bd_dma));
  738. bd_start->offset[2] = 0x0;
  739. bd_start->offset[3] = cpu_to_le32(MARK_CHAIN_BD);
  740. bdc_dbg_bd_list(bdc, ep);
  741. } else if (end_pending) {
  742. /*
  743. * The transfer is stopped in the middle, move the
  744. * HW deq pointer to next_bd_dma
  745. */
  746. ret = bdc_ep_bla(bdc, ep, next_bd_dma);
  747. if (ret) {
  748. dev_err(bdc->dev, "error in ep_bla:%d\n", ret);
  749. return ret;
  750. }
  751. }
  752. return 0;
  753. }
  754. /* Halt/Clear the ep based on value */
  755. static int ep_set_halt(struct bdc_ep *ep, u32 value)
  756. {
  757. struct bdc *bdc;
  758. int ret;
  759. bdc = ep->bdc;
  760. dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
  761. if (value) {
  762. dev_dbg(bdc->dev, "Halt\n");
  763. if (ep->ep_num == 1)
  764. bdc->ep0_state = WAIT_FOR_SETUP;
  765. ret = bdc_ep_set_stall(bdc, ep->ep_num);
  766. if (ret)
  767. dev_err(bdc->dev, "failed to %s STALL on %s\n",
  768. value ? "set" : "clear", ep->name);
  769. else
  770. ep->flags |= BDC_EP_STALL;
  771. } else {
  772. /* Clear */
  773. dev_dbg(bdc->dev, "Before Clear\n");
  774. ret = bdc_ep_clear_stall(bdc, ep->ep_num);
  775. if (ret)
  776. dev_err(bdc->dev, "failed to %s STALL on %s\n",
  777. value ? "set" : "clear", ep->name);
  778. else
  779. ep->flags &= ~BDC_EP_STALL;
  780. dev_dbg(bdc->dev, "After Clear\n");
  781. }
  782. return ret;
  783. }
  784. /* Free all the ep */
  785. void bdc_free_ep(struct bdc *bdc)
  786. {
  787. struct bdc_ep *ep;
  788. u8 epnum;
  789. dev_dbg(bdc->dev, "%s\n", __func__);
  790. for (epnum = 1; epnum < bdc->num_eps; epnum++) {
  791. ep = bdc->bdc_ep_array[epnum];
  792. if (!ep)
  793. continue;
  794. if (ep->flags & BDC_EP_ENABLED)
  795. ep_bd_list_free(ep, ep->bd_list.num_tabs);
  796. /* ep0 is not in this gadget list */
  797. if (epnum != 1)
  798. list_del(&ep->usb_ep.ep_list);
  799. kfree(ep);
  800. }
  801. }
  802. /* USB2 spec, section 7.1.20 */
  803. static int bdc_set_test_mode(struct bdc *bdc)
  804. {
  805. u32 usb2_pm;
  806. usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
  807. usb2_pm &= ~BDC_PTC_MASK;
  808. dev_dbg(bdc->dev, "%s\n", __func__);
  809. switch (bdc->test_mode) {
  810. case TEST_J:
  811. case TEST_K:
  812. case TEST_SE0_NAK:
  813. case TEST_PACKET:
  814. case TEST_FORCE_EN:
  815. usb2_pm |= bdc->test_mode << 28;
  816. break;
  817. default:
  818. return -EINVAL;
  819. }
  820. dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
  821. bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
  822. return 0;
  823. }
  824. /*
  825. * Helper function to handle Transfer status report with status as either
  826. * success or short
  827. */
  828. static void handle_xsr_succ_status(struct bdc *bdc, struct bdc_ep *ep,
  829. struct bdc_sr *sreport)
  830. {
  831. int short_bdi, start_bdi, end_bdi, max_len_bds, chain_bds;
  832. struct bd_list *bd_list = &ep->bd_list;
  833. int actual_length, length_short;
  834. struct bd_transfer *bd_xfr;
  835. struct bdc_bd *short_bd;
  836. struct bdc_req *req;
  837. u64 deq_ptr_64 = 0;
  838. int status = 0;
  839. int sr_status;
  840. u32 tmp_32;
  841. dev_dbg(bdc->dev, "%s ep:%p\n", __func__, ep);
  842. bdc_dbg_srr(bdc, 0);
  843. /* do not process thie sr if ignore flag is set */
  844. if (ep->ignore_next_sr) {
  845. ep->ignore_next_sr = false;
  846. return;
  847. }
  848. if (unlikely(list_empty(&ep->queue))) {
  849. dev_warn(bdc->dev, "xfr srr with no BD's queued\n");
  850. return;
  851. }
  852. req = list_entry(ep->queue.next, struct bdc_req,
  853. queue);
  854. bd_xfr = &req->bd_xfr;
  855. sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
  856. /*
  857. * sr_status is short and this transfer has more than 1 bd then it needs
  858. * special handling, this is only applicable for bulk and ctrl
  859. */
  860. if (sr_status == XSF_SHORT && bd_xfr->num_bds > 1) {
  861. /*
  862. * This is multi bd xfr, lets see which bd
  863. * caused short transfer and how many bytes have been
  864. * transferred so far.
  865. */
  866. tmp_32 = le32_to_cpu(sreport->offset[0]);
  867. deq_ptr_64 = tmp_32;
  868. tmp_32 = le32_to_cpu(sreport->offset[1]);
  869. deq_ptr_64 |= ((u64)tmp_32 << 32);
  870. short_bdi = bd_add_to_bdi(ep, deq_ptr_64);
  871. if (unlikely(short_bdi < 0))
  872. dev_warn(bdc->dev, "bd doesn't exist?\n");
  873. start_bdi = bd_xfr->start_bdi;
  874. /*
  875. * We know the start_bdi and short_bdi, how many xfr
  876. * bds in between
  877. */
  878. if (start_bdi <= short_bdi) {
  879. max_len_bds = short_bdi - start_bdi;
  880. if (max_len_bds <= bd_list->num_bds_table) {
  881. if (!(bdi_to_tbi(ep, start_bdi) ==
  882. bdi_to_tbi(ep, short_bdi)))
  883. max_len_bds--;
  884. } else {
  885. chain_bds = max_len_bds/bd_list->num_bds_table;
  886. max_len_bds -= chain_bds;
  887. }
  888. } else {
  889. /* there is a wrap in the ring within a xfr */
  890. chain_bds = (bd_list->max_bdi - start_bdi)/
  891. bd_list->num_bds_table;
  892. chain_bds += short_bdi/bd_list->num_bds_table;
  893. max_len_bds = bd_list->max_bdi - start_bdi;
  894. max_len_bds += short_bdi;
  895. max_len_bds -= chain_bds;
  896. }
  897. /* max_len_bds is the number of full length bds */
  898. end_bdi = find_end_bdi(ep, bd_xfr->next_hwd_bdi);
  899. if (!(end_bdi == short_bdi))
  900. ep->ignore_next_sr = true;
  901. actual_length = max_len_bds * BD_MAX_BUFF_SIZE;
  902. short_bd = bdi_to_bd(ep, short_bdi);
  903. /* length queued */
  904. length_short = le32_to_cpu(short_bd->offset[2]) & 0x1FFFFF;
  905. /* actual length trensfered */
  906. length_short -= SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
  907. actual_length += length_short;
  908. req->usb_req.actual = actual_length;
  909. } else {
  910. req->usb_req.actual = req->usb_req.length -
  911. SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
  912. dev_dbg(bdc->dev,
  913. "len=%d actual=%d bd_xfr->next_hwd_bdi:%d\n",
  914. req->usb_req.length, req->usb_req.actual,
  915. bd_xfr->next_hwd_bdi);
  916. }
  917. /* Update the dequeue pointer */
  918. ep->bd_list.hwd_bdi = bd_xfr->next_hwd_bdi;
  919. if (req->usb_req.actual < req->usb_req.length) {
  920. dev_dbg(bdc->dev, "short xfr on %d\n", ep->ep_num);
  921. if (req->usb_req.short_not_ok)
  922. status = -EREMOTEIO;
  923. }
  924. bdc_req_complete(ep, bd_xfr->req, status);
  925. }
  926. /* EP0 setup related packet handlers */
  927. /*
  928. * Setup packet received, just store the packet and process on next DS or SS
  929. * started SR
  930. */
  931. void bdc_xsf_ep0_setup_recv(struct bdc *bdc, struct bdc_sr *sreport)
  932. {
  933. struct usb_ctrlrequest *setup_pkt;
  934. u32 len;
  935. dev_dbg(bdc->dev,
  936. "%s ep0_state:%s\n",
  937. __func__, ep0_state_string[bdc->ep0_state]);
  938. /* Store received setup packet */
  939. setup_pkt = &bdc->setup_pkt;
  940. memcpy(setup_pkt, &sreport->offset[0], sizeof(*setup_pkt));
  941. len = le16_to_cpu(setup_pkt->wLength);
  942. if (!len)
  943. bdc->ep0_state = WAIT_FOR_STATUS_START;
  944. else
  945. bdc->ep0_state = WAIT_FOR_DATA_START;
  946. dev_dbg(bdc->dev,
  947. "%s exit ep0_state:%s\n",
  948. __func__, ep0_state_string[bdc->ep0_state]);
  949. }
  950. /* Stall ep0 */
  951. static void ep0_stall(struct bdc *bdc)
  952. {
  953. struct bdc_ep *ep = bdc->bdc_ep_array[1];
  954. struct bdc_req *req;
  955. dev_dbg(bdc->dev, "%s\n", __func__);
  956. bdc->delayed_status = false;
  957. ep_set_halt(ep, 1);
  958. /* de-queue any pendig requests */
  959. while (!list_empty(&ep->queue)) {
  960. req = list_entry(ep->queue.next, struct bdc_req,
  961. queue);
  962. bdc_req_complete(ep, req, -ESHUTDOWN);
  963. }
  964. }
  965. /* SET_ADD handlers */
  966. static int ep0_set_address(struct bdc *bdc, struct usb_ctrlrequest *ctrl)
  967. {
  968. enum usb_device_state state = bdc->gadget.state;
  969. int ret = 0;
  970. u32 addr;
  971. addr = le16_to_cpu(ctrl->wValue);
  972. dev_dbg(bdc->dev,
  973. "%s addr:%d dev state:%d\n",
  974. __func__, addr, state);
  975. if (addr > 127)
  976. return -EINVAL;
  977. switch (state) {
  978. case USB_STATE_DEFAULT:
  979. case USB_STATE_ADDRESS:
  980. /* Issue Address device command */
  981. ret = bdc_address_device(bdc, addr);
  982. if (ret)
  983. return ret;
  984. if (addr)
  985. usb_gadget_set_state(&bdc->gadget, USB_STATE_ADDRESS);
  986. else
  987. usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT);
  988. bdc->dev_addr = addr;
  989. break;
  990. default:
  991. dev_warn(bdc->dev,
  992. "SET Address in wrong device state %d\n",
  993. state);
  994. ret = -EINVAL;
  995. }
  996. return ret;
  997. }
  998. /* Handler for SET/CLEAR FEATURE requests for device */
  999. static int ep0_handle_feature_dev(struct bdc *bdc, u16 wValue,
  1000. u16 wIndex, bool set)
  1001. {
  1002. enum usb_device_state state = bdc->gadget.state;
  1003. u32 usppms = 0;
  1004. dev_dbg(bdc->dev, "%s set:%d dev state:%d\n",
  1005. __func__, set, state);
  1006. switch (wValue) {
  1007. case USB_DEVICE_REMOTE_WAKEUP:
  1008. dev_dbg(bdc->dev, "USB_DEVICE_REMOTE_WAKEUP\n");
  1009. if (set)
  1010. bdc->devstatus |= REMOTE_WAKE_ENABLE;
  1011. else
  1012. bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
  1013. break;
  1014. case USB_DEVICE_TEST_MODE:
  1015. dev_dbg(bdc->dev, "USB_DEVICE_TEST_MODE\n");
  1016. if ((wIndex & 0xFF) ||
  1017. (bdc->gadget.speed != USB_SPEED_HIGH) || !set)
  1018. return -EINVAL;
  1019. bdc->test_mode = wIndex >> 8;
  1020. break;
  1021. case USB_DEVICE_U1_ENABLE:
  1022. dev_dbg(bdc->dev, "USB_DEVICE_U1_ENABLE\n");
  1023. if (bdc->gadget.speed != USB_SPEED_SUPER ||
  1024. state != USB_STATE_CONFIGURED)
  1025. return -EINVAL;
  1026. usppms = bdc_readl(bdc->regs, BDC_USPPMS);
  1027. if (set) {
  1028. /* clear previous u1t */
  1029. usppms &= ~BDC_U1T(BDC_U1T_MASK);
  1030. usppms |= BDC_U1T(U1_TIMEOUT);
  1031. usppms |= BDC_U1E | BDC_PORT_W1S;
  1032. bdc->devstatus |= (1 << USB_DEV_STAT_U1_ENABLED);
  1033. } else {
  1034. usppms &= ~BDC_U1E;
  1035. usppms |= BDC_PORT_W1S;
  1036. bdc->devstatus &= ~(1 << USB_DEV_STAT_U1_ENABLED);
  1037. }
  1038. bdc_writel(bdc->regs, BDC_USPPMS, usppms);
  1039. break;
  1040. case USB_DEVICE_U2_ENABLE:
  1041. dev_dbg(bdc->dev, "USB_DEVICE_U2_ENABLE\n");
  1042. if (bdc->gadget.speed != USB_SPEED_SUPER ||
  1043. state != USB_STATE_CONFIGURED)
  1044. return -EINVAL;
  1045. usppms = bdc_readl(bdc->regs, BDC_USPPMS);
  1046. if (set) {
  1047. usppms |= BDC_U2E;
  1048. usppms |= BDC_U2A;
  1049. bdc->devstatus |= (1 << USB_DEV_STAT_U2_ENABLED);
  1050. } else {
  1051. usppms &= ~BDC_U2E;
  1052. usppms &= ~BDC_U2A;
  1053. bdc->devstatus &= ~(1 << USB_DEV_STAT_U2_ENABLED);
  1054. }
  1055. bdc_writel(bdc->regs, BDC_USPPMS, usppms);
  1056. break;
  1057. case USB_DEVICE_LTM_ENABLE:
  1058. dev_dbg(bdc->dev, "USB_DEVICE_LTM_ENABLE?\n");
  1059. if (bdc->gadget.speed != USB_SPEED_SUPER ||
  1060. state != USB_STATE_CONFIGURED)
  1061. return -EINVAL;
  1062. break;
  1063. default:
  1064. dev_err(bdc->dev, "Unknown wValue:%d\n", wValue);
  1065. return -EOPNOTSUPP;
  1066. } /* USB_RECIP_DEVICE end */
  1067. return 0;
  1068. }
  1069. /* SET/CLEAR FEATURE handler */
  1070. static int ep0_handle_feature(struct bdc *bdc,
  1071. struct usb_ctrlrequest *setup_pkt, bool set)
  1072. {
  1073. enum usb_device_state state = bdc->gadget.state;
  1074. struct bdc_ep *ep;
  1075. u16 wValue;
  1076. u16 wIndex;
  1077. int epnum;
  1078. wValue = le16_to_cpu(setup_pkt->wValue);
  1079. wIndex = le16_to_cpu(setup_pkt->wIndex);
  1080. dev_dbg(bdc->dev,
  1081. "%s wValue=%d wIndex=%d devstate=%08x speed=%d set=%d",
  1082. __func__, wValue, wIndex, state,
  1083. bdc->gadget.speed, set);
  1084. switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
  1085. case USB_RECIP_DEVICE:
  1086. return ep0_handle_feature_dev(bdc, wValue, wIndex, set);
  1087. case USB_RECIP_INTERFACE:
  1088. dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
  1089. /* USB3 spec, sec 9.4.9 */
  1090. if (wValue != USB_INTRF_FUNC_SUSPEND)
  1091. return -EINVAL;
  1092. /* USB3 spec, Table 9-8 */
  1093. if (set) {
  1094. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) {
  1095. dev_dbg(bdc->dev, "SET REMOTE_WAKEUP\n");
  1096. bdc->devstatus |= REMOTE_WAKE_ENABLE;
  1097. } else {
  1098. dev_dbg(bdc->dev, "CLEAR REMOTE_WAKEUP\n");
  1099. bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
  1100. }
  1101. }
  1102. break;
  1103. case USB_RECIP_ENDPOINT:
  1104. dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
  1105. if (wValue != USB_ENDPOINT_HALT)
  1106. return -EINVAL;
  1107. epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1108. if (epnum) {
  1109. if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1110. epnum = epnum * 2 + 1;
  1111. else
  1112. epnum *= 2;
  1113. } else {
  1114. epnum = 1; /*EP0*/
  1115. }
  1116. /*
  1117. * If CLEAR_FEATURE on ep0 then don't do anything as the stall
  1118. * condition on ep0 has already been cleared when SETUP packet
  1119. * was received.
  1120. */
  1121. if (epnum == 1 && !set) {
  1122. dev_dbg(bdc->dev, "ep0 stall already cleared\n");
  1123. return 0;
  1124. }
  1125. dev_dbg(bdc->dev, "epnum=%d\n", epnum);
  1126. ep = bdc->bdc_ep_array[epnum];
  1127. if (!ep)
  1128. return -EINVAL;
  1129. return ep_set_halt(ep, set);
  1130. default:
  1131. dev_err(bdc->dev, "Unknown recipient\n");
  1132. return -EINVAL;
  1133. }
  1134. return 0;
  1135. }
  1136. /* GET_STATUS request handler */
  1137. static int ep0_handle_status(struct bdc *bdc,
  1138. struct usb_ctrlrequest *setup_pkt)
  1139. {
  1140. enum usb_device_state state = bdc->gadget.state;
  1141. struct bdc_ep *ep;
  1142. u16 usb_status = 0;
  1143. u32 epnum;
  1144. u16 wIndex;
  1145. /* USB2.0 spec sec 9.4.5 */
  1146. if (state == USB_STATE_DEFAULT)
  1147. return -EINVAL;
  1148. wIndex = le16_to_cpu(setup_pkt->wIndex);
  1149. dev_dbg(bdc->dev, "%s\n", __func__);
  1150. usb_status = bdc->devstatus;
  1151. switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
  1152. case USB_RECIP_DEVICE:
  1153. dev_dbg(bdc->dev,
  1154. "USB_RECIP_DEVICE devstatus:%08x\n",
  1155. bdc->devstatus);
  1156. /* USB3 spec, sec 9.4.5 */
  1157. if (bdc->gadget.speed == USB_SPEED_SUPER)
  1158. usb_status &= ~REMOTE_WAKE_ENABLE;
  1159. break;
  1160. case USB_RECIP_INTERFACE:
  1161. dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
  1162. if (bdc->gadget.speed == USB_SPEED_SUPER) {
  1163. /*
  1164. * This should come from func for Func remote wkup
  1165. * usb_status |=1;
  1166. */
  1167. if (bdc->devstatus & REMOTE_WAKE_ENABLE)
  1168. usb_status |= REMOTE_WAKE_ENABLE;
  1169. } else {
  1170. usb_status = 0;
  1171. }
  1172. break;
  1173. case USB_RECIP_ENDPOINT:
  1174. dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
  1175. epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1176. if (epnum) {
  1177. if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1178. epnum = epnum*2 + 1;
  1179. else
  1180. epnum *= 2;
  1181. } else {
  1182. epnum = 1; /* EP0 */
  1183. }
  1184. ep = bdc->bdc_ep_array[epnum];
  1185. if (!ep) {
  1186. dev_err(bdc->dev, "ISSUE, GET_STATUS for invalid EP ?");
  1187. return -EINVAL;
  1188. }
  1189. if (ep->flags & BDC_EP_STALL)
  1190. usb_status |= 1 << USB_ENDPOINT_HALT;
  1191. break;
  1192. default:
  1193. dev_err(bdc->dev, "Unknown recipient for get_status\n");
  1194. return -EINVAL;
  1195. }
  1196. /* prepare a data stage for GET_STATUS */
  1197. dev_dbg(bdc->dev, "usb_status=%08x\n", usb_status);
  1198. *(__le16 *)bdc->ep0_response_buff = cpu_to_le16(usb_status);
  1199. bdc->ep0_req.usb_req.length = 2;
  1200. bdc->ep0_req.usb_req.buf = &bdc->ep0_response_buff;
  1201. ep0_queue_data_stage(bdc);
  1202. return 0;
  1203. }
  1204. static void ep0_set_sel_cmpl(struct usb_ep *_ep, struct usb_request *_req)
  1205. {
  1206. /* ep0_set_sel_cmpl */
  1207. }
  1208. /* Queue data stage to handle 6 byte SET_SEL request */
  1209. static int ep0_set_sel(struct bdc *bdc,
  1210. struct usb_ctrlrequest *setup_pkt)
  1211. {
  1212. struct bdc_ep *ep;
  1213. u16 wLength;
  1214. u16 wValue;
  1215. dev_dbg(bdc->dev, "%s\n", __func__);
  1216. wValue = le16_to_cpu(setup_pkt->wValue);
  1217. wLength = le16_to_cpu(setup_pkt->wLength);
  1218. if (unlikely(wLength != 6)) {
  1219. dev_err(bdc->dev, "%s Wrong wLength:%d\n", __func__, wLength);
  1220. return -EINVAL;
  1221. }
  1222. ep = bdc->bdc_ep_array[1];
  1223. bdc->ep0_req.ep = ep;
  1224. bdc->ep0_req.usb_req.length = 6;
  1225. bdc->ep0_req.usb_req.buf = bdc->ep0_response_buff;
  1226. bdc->ep0_req.usb_req.complete = ep0_set_sel_cmpl;
  1227. ep0_queue_data_stage(bdc);
  1228. return 0;
  1229. }
  1230. /*
  1231. * Queue a 0 byte bd only if wLength is more than the length and and length is
  1232. * a multiple of MaxPacket then queue 0 byte BD
  1233. */
  1234. static int ep0_queue_zlp(struct bdc *bdc)
  1235. {
  1236. int ret;
  1237. dev_dbg(bdc->dev, "%s\n", __func__);
  1238. bdc->ep0_req.ep = bdc->bdc_ep_array[1];
  1239. bdc->ep0_req.usb_req.length = 0;
  1240. bdc->ep0_req.usb_req.complete = NULL;
  1241. bdc->ep0_state = WAIT_FOR_DATA_START;
  1242. ret = bdc_queue_xfr(bdc, &bdc->ep0_req);
  1243. if (ret) {
  1244. dev_err(bdc->dev, "err queueing zlp :%d\n", ret);
  1245. return ret;
  1246. }
  1247. bdc->ep0_state = WAIT_FOR_DATA_XMIT;
  1248. return 0;
  1249. }
  1250. /* Control request handler */
  1251. static int handle_control_request(struct bdc *bdc)
  1252. {
  1253. enum usb_device_state state = bdc->gadget.state;
  1254. struct usb_ctrlrequest *setup_pkt;
  1255. int delegate_setup = 0;
  1256. int ret = 0;
  1257. int config = 0;
  1258. setup_pkt = &bdc->setup_pkt;
  1259. dev_dbg(bdc->dev, "%s\n", __func__);
  1260. if ((setup_pkt->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1261. switch (setup_pkt->bRequest) {
  1262. case USB_REQ_SET_ADDRESS:
  1263. dev_dbg(bdc->dev, "USB_REQ_SET_ADDRESS\n");
  1264. ret = ep0_set_address(bdc, setup_pkt);
  1265. bdc->devstatus &= DEVSTATUS_CLEAR;
  1266. break;
  1267. case USB_REQ_SET_CONFIGURATION:
  1268. dev_dbg(bdc->dev, "USB_REQ_SET_CONFIGURATION\n");
  1269. if (state == USB_STATE_ADDRESS) {
  1270. usb_gadget_set_state(&bdc->gadget,
  1271. USB_STATE_CONFIGURED);
  1272. } else if (state == USB_STATE_CONFIGURED) {
  1273. /*
  1274. * USB2 spec sec 9.4.7, if wValue is 0 then dev
  1275. * is moved to addressed state
  1276. */
  1277. config = le16_to_cpu(setup_pkt->wValue);
  1278. if (!config)
  1279. usb_gadget_set_state(
  1280. &bdc->gadget,
  1281. USB_STATE_ADDRESS);
  1282. }
  1283. delegate_setup = 1;
  1284. break;
  1285. case USB_REQ_SET_FEATURE:
  1286. dev_dbg(bdc->dev, "USB_REQ_SET_FEATURE\n");
  1287. ret = ep0_handle_feature(bdc, setup_pkt, 1);
  1288. break;
  1289. case USB_REQ_CLEAR_FEATURE:
  1290. dev_dbg(bdc->dev, "USB_REQ_CLEAR_FEATURE\n");
  1291. ret = ep0_handle_feature(bdc, setup_pkt, 0);
  1292. break;
  1293. case USB_REQ_GET_STATUS:
  1294. dev_dbg(bdc->dev, "USB_REQ_GET_STATUS\n");
  1295. ret = ep0_handle_status(bdc, setup_pkt);
  1296. break;
  1297. case USB_REQ_SET_SEL:
  1298. dev_dbg(bdc->dev, "USB_REQ_SET_SEL\n");
  1299. ret = ep0_set_sel(bdc, setup_pkt);
  1300. break;
  1301. case USB_REQ_SET_ISOCH_DELAY:
  1302. dev_warn(bdc->dev,
  1303. "USB_REQ_SET_ISOCH_DELAY not handled\n");
  1304. ret = 0;
  1305. break;
  1306. default:
  1307. delegate_setup = 1;
  1308. }
  1309. } else {
  1310. delegate_setup = 1;
  1311. }
  1312. if (delegate_setup) {
  1313. spin_unlock(&bdc->lock);
  1314. ret = bdc->gadget_driver->setup(&bdc->gadget, setup_pkt);
  1315. spin_lock(&bdc->lock);
  1316. }
  1317. return ret;
  1318. }
  1319. /* EP0: Data stage started */
  1320. void bdc_xsf_ep0_data_start(struct bdc *bdc, struct bdc_sr *sreport)
  1321. {
  1322. struct bdc_ep *ep;
  1323. int ret = 0;
  1324. dev_dbg(bdc->dev, "%s\n", __func__);
  1325. ep = bdc->bdc_ep_array[1];
  1326. /* If ep0 was stalled, the clear it first */
  1327. if (ep->flags & BDC_EP_STALL) {
  1328. ret = ep_set_halt(ep, 0);
  1329. if (ret)
  1330. goto err;
  1331. }
  1332. if (bdc->ep0_state != WAIT_FOR_DATA_START)
  1333. dev_warn(bdc->dev,
  1334. "Data stage not expected ep0_state:%s\n",
  1335. ep0_state_string[bdc->ep0_state]);
  1336. ret = handle_control_request(bdc);
  1337. if (ret == USB_GADGET_DELAYED_STATUS) {
  1338. /*
  1339. * The ep0 state will remain WAIT_FOR_DATA_START till
  1340. * we received ep_queue on ep0
  1341. */
  1342. bdc->delayed_status = true;
  1343. return;
  1344. }
  1345. if (!ret) {
  1346. bdc->ep0_state = WAIT_FOR_DATA_XMIT;
  1347. dev_dbg(bdc->dev,
  1348. "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
  1349. return;
  1350. }
  1351. err:
  1352. ep0_stall(bdc);
  1353. }
  1354. /* EP0: status stage started */
  1355. void bdc_xsf_ep0_status_start(struct bdc *bdc, struct bdc_sr *sreport)
  1356. {
  1357. struct usb_ctrlrequest *setup_pkt;
  1358. struct bdc_ep *ep;
  1359. int ret = 0;
  1360. dev_dbg(bdc->dev,
  1361. "%s ep0_state:%s",
  1362. __func__, ep0_state_string[bdc->ep0_state]);
  1363. ep = bdc->bdc_ep_array[1];
  1364. /* check if ZLP was queued? */
  1365. if (bdc->zlp_needed)
  1366. bdc->zlp_needed = false;
  1367. if (ep->flags & BDC_EP_STALL) {
  1368. ret = ep_set_halt(ep, 0);
  1369. if (ret)
  1370. goto err;
  1371. }
  1372. if ((bdc->ep0_state != WAIT_FOR_STATUS_START) &&
  1373. (bdc->ep0_state != WAIT_FOR_DATA_XMIT))
  1374. dev_err(bdc->dev,
  1375. "Status stage recv but ep0_state:%s\n",
  1376. ep0_state_string[bdc->ep0_state]);
  1377. /* check if data stage is in progress ? */
  1378. if (bdc->ep0_state == WAIT_FOR_DATA_XMIT) {
  1379. bdc->ep0_state = STATUS_PENDING;
  1380. /* Status stage will be queued upon Data stage transmit event */
  1381. dev_dbg(bdc->dev,
  1382. "status started but data not transmitted yet\n");
  1383. return;
  1384. }
  1385. setup_pkt = &bdc->setup_pkt;
  1386. /*
  1387. * 2 stage setup then only process the setup, for 3 stage setup the date
  1388. * stage is already handled
  1389. */
  1390. if (!le16_to_cpu(setup_pkt->wLength)) {
  1391. ret = handle_control_request(bdc);
  1392. if (ret == USB_GADGET_DELAYED_STATUS) {
  1393. bdc->delayed_status = true;
  1394. /* ep0_state will remain WAIT_FOR_STATUS_START */
  1395. return;
  1396. }
  1397. }
  1398. if (!ret) {
  1399. /* Queue a status stage BD */
  1400. ep0_queue_status_stage(bdc);
  1401. bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
  1402. dev_dbg(bdc->dev,
  1403. "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
  1404. return;
  1405. }
  1406. err:
  1407. ep0_stall(bdc);
  1408. }
  1409. /* Helper function to update ep0 upon SR with xsf_succ or xsf_short */
  1410. static void ep0_xsf_complete(struct bdc *bdc, struct bdc_sr *sreport)
  1411. {
  1412. dev_dbg(bdc->dev, "%s\n", __func__);
  1413. switch (bdc->ep0_state) {
  1414. case WAIT_FOR_DATA_XMIT:
  1415. bdc->ep0_state = WAIT_FOR_STATUS_START;
  1416. break;
  1417. case WAIT_FOR_STATUS_XMIT:
  1418. bdc->ep0_state = WAIT_FOR_SETUP;
  1419. if (bdc->test_mode) {
  1420. int ret;
  1421. dev_dbg(bdc->dev, "test_mode:%d\n", bdc->test_mode);
  1422. ret = bdc_set_test_mode(bdc);
  1423. if (ret < 0) {
  1424. dev_err(bdc->dev, "Err in setting Test mode\n");
  1425. return;
  1426. }
  1427. bdc->test_mode = 0;
  1428. }
  1429. break;
  1430. case STATUS_PENDING:
  1431. bdc_xsf_ep0_status_start(bdc, sreport);
  1432. break;
  1433. default:
  1434. dev_err(bdc->dev,
  1435. "Unknown ep0_state:%s\n",
  1436. ep0_state_string[bdc->ep0_state]);
  1437. }
  1438. }
  1439. /* xfr completion status report handler */
  1440. void bdc_sr_xsf(struct bdc *bdc, struct bdc_sr *sreport)
  1441. {
  1442. struct bdc_ep *ep;
  1443. u32 sr_status;
  1444. u8 ep_num;
  1445. ep_num = (le32_to_cpu(sreport->offset[3])>>4) & 0x1f;
  1446. ep = bdc->bdc_ep_array[ep_num];
  1447. if (!ep || !(ep->flags & BDC_EP_ENABLED)) {
  1448. dev_err(bdc->dev, "xsf for ep not enabled\n");
  1449. return;
  1450. }
  1451. /*
  1452. * check if this transfer is after link went from U3->U0 due
  1453. * to remote wakeup
  1454. */
  1455. if (bdc->devstatus & FUNC_WAKE_ISSUED) {
  1456. bdc->devstatus &= ~(FUNC_WAKE_ISSUED);
  1457. dev_dbg(bdc->dev, "%s clearing FUNC_WAKE_ISSUED flag\n",
  1458. __func__);
  1459. }
  1460. sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
  1461. dev_dbg_ratelimited(bdc->dev, "%s sr_status=%d ep:%s\n",
  1462. __func__, sr_status, ep->name);
  1463. switch (sr_status) {
  1464. case XSF_SUCC:
  1465. case XSF_SHORT:
  1466. handle_xsr_succ_status(bdc, ep, sreport);
  1467. if (ep_num == 1)
  1468. ep0_xsf_complete(bdc, sreport);
  1469. break;
  1470. case XSF_SETUP_RECV:
  1471. case XSF_DATA_START:
  1472. case XSF_STATUS_START:
  1473. if (ep_num != 1) {
  1474. dev_err(bdc->dev,
  1475. "ep0 related packets on non ep0 endpoint");
  1476. return;
  1477. }
  1478. bdc->sr_xsf_ep0[sr_status - XSF_SETUP_RECV](bdc, sreport);
  1479. break;
  1480. case XSF_BABB:
  1481. if (ep_num == 1) {
  1482. dev_dbg(bdc->dev, "Babble on ep0 zlp_need:%d\n",
  1483. bdc->zlp_needed);
  1484. /*
  1485. * If the last completed transfer had wLength >Data Len,
  1486. * and Len is multiple of MaxPacket,then queue ZLP
  1487. */
  1488. if (bdc->zlp_needed) {
  1489. /* queue 0 length bd */
  1490. ep0_queue_zlp(bdc);
  1491. return;
  1492. }
  1493. }
  1494. dev_warn(bdc->dev, "Babble on ep not handled\n");
  1495. break;
  1496. default:
  1497. dev_warn(bdc->dev, "sr status not handled:%x\n", sr_status);
  1498. break;
  1499. }
  1500. }
  1501. static int bdc_gadget_ep_queue(struct usb_ep *_ep,
  1502. struct usb_request *_req, gfp_t gfp_flags)
  1503. {
  1504. struct bdc_req *req;
  1505. unsigned long flags;
  1506. struct bdc_ep *ep;
  1507. struct bdc *bdc;
  1508. int ret;
  1509. if (!_ep || !_ep->desc)
  1510. return -ESHUTDOWN;
  1511. if (!_req || !_req->complete || !_req->buf)
  1512. return -EINVAL;
  1513. ep = to_bdc_ep(_ep);
  1514. req = to_bdc_req(_req);
  1515. bdc = ep->bdc;
  1516. dev_dbg(bdc->dev, "%s ep:%p req:%p\n", __func__, ep, req);
  1517. dev_dbg(bdc->dev, "queuing request %p to %s length %d zero:%d\n",
  1518. _req, ep->name, _req->length, _req->zero);
  1519. if (!ep->usb_ep.desc) {
  1520. dev_warn(bdc->dev,
  1521. "trying to queue req %p to disabled %s\n",
  1522. _req, ep->name);
  1523. return -ESHUTDOWN;
  1524. }
  1525. if (_req->length > MAX_XFR_LEN) {
  1526. dev_warn(bdc->dev,
  1527. "req length > supported MAX:%d requested:%d\n",
  1528. MAX_XFR_LEN, _req->length);
  1529. return -EOPNOTSUPP;
  1530. }
  1531. spin_lock_irqsave(&bdc->lock, flags);
  1532. if (ep == bdc->bdc_ep_array[1])
  1533. ret = ep0_queue(ep, req);
  1534. else
  1535. ret = ep_queue(ep, req);
  1536. spin_unlock_irqrestore(&bdc->lock, flags);
  1537. return ret;
  1538. }
  1539. static int bdc_gadget_ep_dequeue(struct usb_ep *_ep,
  1540. struct usb_request *_req)
  1541. {
  1542. struct bdc_req *req;
  1543. unsigned long flags;
  1544. struct bdc_ep *ep;
  1545. struct bdc *bdc;
  1546. int ret;
  1547. if (!_ep || !_req)
  1548. return -EINVAL;
  1549. ep = to_bdc_ep(_ep);
  1550. req = to_bdc_req(_req);
  1551. bdc = ep->bdc;
  1552. dev_dbg(bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
  1553. bdc_dbg_bd_list(bdc, ep);
  1554. spin_lock_irqsave(&bdc->lock, flags);
  1555. /* make sure it's still queued on this endpoint */
  1556. list_for_each_entry(req, &ep->queue, queue) {
  1557. if (&req->usb_req == _req)
  1558. break;
  1559. }
  1560. if (&req->usb_req != _req) {
  1561. spin_unlock_irqrestore(&bdc->lock, flags);
  1562. dev_err(bdc->dev, "usb_req !=req n");
  1563. return -EINVAL;
  1564. }
  1565. ret = ep_dequeue(ep, req);
  1566. if (ret) {
  1567. ret = -EOPNOTSUPP;
  1568. goto err;
  1569. }
  1570. bdc_req_complete(ep, req, -ECONNRESET);
  1571. err:
  1572. bdc_dbg_bd_list(bdc, ep);
  1573. spin_unlock_irqrestore(&bdc->lock, flags);
  1574. return ret;
  1575. }
  1576. static int bdc_gadget_ep_set_halt(struct usb_ep *_ep, int value)
  1577. {
  1578. unsigned long flags;
  1579. struct bdc_ep *ep;
  1580. struct bdc *bdc;
  1581. int ret;
  1582. ep = to_bdc_ep(_ep);
  1583. bdc = ep->bdc;
  1584. dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
  1585. spin_lock_irqsave(&bdc->lock, flags);
  1586. if (usb_endpoint_xfer_isoc(ep->usb_ep.desc))
  1587. ret = -EINVAL;
  1588. else if (!list_empty(&ep->queue))
  1589. ret = -EAGAIN;
  1590. else
  1591. ret = ep_set_halt(ep, value);
  1592. spin_unlock_irqrestore(&bdc->lock, flags);
  1593. return ret;
  1594. }
  1595. static struct usb_request *bdc_gadget_alloc_request(struct usb_ep *_ep,
  1596. gfp_t gfp_flags)
  1597. {
  1598. struct bdc_req *req;
  1599. struct bdc_ep *ep;
  1600. req = kzalloc(sizeof(*req), gfp_flags);
  1601. if (!req)
  1602. return NULL;
  1603. ep = to_bdc_ep(_ep);
  1604. req->ep = ep;
  1605. req->epnum = ep->ep_num;
  1606. req->usb_req.dma = DMA_ADDR_INVALID;
  1607. dev_dbg(ep->bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
  1608. return &req->usb_req;
  1609. }
  1610. static void bdc_gadget_free_request(struct usb_ep *_ep,
  1611. struct usb_request *_req)
  1612. {
  1613. struct bdc_req *req;
  1614. req = to_bdc_req(_req);
  1615. kfree(req);
  1616. }
  1617. /* endpoint operations */
  1618. /* configure endpoint and also allocate resources */
  1619. static int bdc_gadget_ep_enable(struct usb_ep *_ep,
  1620. const struct usb_endpoint_descriptor *desc)
  1621. {
  1622. unsigned long flags;
  1623. struct bdc_ep *ep;
  1624. struct bdc *bdc;
  1625. int ret;
  1626. if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  1627. pr_debug("bdc_gadget_ep_enable invalid parameters\n");
  1628. return -EINVAL;
  1629. }
  1630. if (!desc->wMaxPacketSize) {
  1631. pr_debug("bdc_gadget_ep_enable missing wMaxPacketSize\n");
  1632. return -EINVAL;
  1633. }
  1634. ep = to_bdc_ep(_ep);
  1635. bdc = ep->bdc;
  1636. /* Sanity check, upper layer will not send enable for ep0 */
  1637. if (ep == bdc->bdc_ep_array[1])
  1638. return -EINVAL;
  1639. if (!bdc->gadget_driver
  1640. || bdc->gadget.speed == USB_SPEED_UNKNOWN) {
  1641. return -ESHUTDOWN;
  1642. }
  1643. dev_dbg(bdc->dev, "%s Enabling %s\n", __func__, ep->name);
  1644. spin_lock_irqsave(&bdc->lock, flags);
  1645. ep->desc = desc;
  1646. ep->comp_desc = _ep->comp_desc;
  1647. ret = bdc_ep_enable(ep);
  1648. spin_unlock_irqrestore(&bdc->lock, flags);
  1649. return ret;
  1650. }
  1651. static int bdc_gadget_ep_disable(struct usb_ep *_ep)
  1652. {
  1653. unsigned long flags;
  1654. struct bdc_ep *ep;
  1655. struct bdc *bdc;
  1656. int ret;
  1657. if (!_ep) {
  1658. pr_debug("bdc: invalid parameters\n");
  1659. return -EINVAL;
  1660. }
  1661. ep = to_bdc_ep(_ep);
  1662. bdc = ep->bdc;
  1663. /* Upper layer will not call this for ep0, but do a sanity check */
  1664. if (ep == bdc->bdc_ep_array[1]) {
  1665. dev_warn(bdc->dev, "%s called for ep0\n", __func__);
  1666. return -EINVAL;
  1667. }
  1668. dev_dbg(bdc->dev,
  1669. "%s() ep:%s ep->flags:%08x\n",
  1670. __func__, ep->name, ep->flags);
  1671. if (!(ep->flags & BDC_EP_ENABLED)) {
  1672. dev_warn(bdc->dev, "%s is already disabled\n", ep->name);
  1673. return 0;
  1674. }
  1675. spin_lock_irqsave(&bdc->lock, flags);
  1676. ret = bdc_ep_disable(ep);
  1677. spin_unlock_irqrestore(&bdc->lock, flags);
  1678. return ret;
  1679. }
  1680. static const struct usb_ep_ops bdc_gadget_ep_ops = {
  1681. .enable = bdc_gadget_ep_enable,
  1682. .disable = bdc_gadget_ep_disable,
  1683. .alloc_request = bdc_gadget_alloc_request,
  1684. .free_request = bdc_gadget_free_request,
  1685. .queue = bdc_gadget_ep_queue,
  1686. .dequeue = bdc_gadget_ep_dequeue,
  1687. .set_halt = bdc_gadget_ep_set_halt
  1688. };
  1689. /* dir = 1 is IN */
  1690. static int init_ep(struct bdc *bdc, u32 epnum, u32 dir)
  1691. {
  1692. struct bdc_ep *ep;
  1693. dev_dbg(bdc->dev, "%s epnum=%d dir=%d\n", __func__, epnum, dir);
  1694. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1695. if (!ep)
  1696. return -ENOMEM;
  1697. ep->bdc = bdc;
  1698. ep->dir = dir;
  1699. /* ep->ep_num is the index inside bdc_ep */
  1700. if (epnum == 1) {
  1701. ep->ep_num = 1;
  1702. bdc->bdc_ep_array[ep->ep_num] = ep;
  1703. snprintf(ep->name, sizeof(ep->name), "ep%d", epnum - 1);
  1704. usb_ep_set_maxpacket_limit(&ep->usb_ep, EP0_MAX_PKT_SIZE);
  1705. ep->comp_desc = NULL;
  1706. bdc->gadget.ep0 = &ep->usb_ep;
  1707. } else {
  1708. if (dir)
  1709. ep->ep_num = epnum * 2 - 1;
  1710. else
  1711. ep->ep_num = epnum * 2 - 2;
  1712. bdc->bdc_ep_array[ep->ep_num] = ep;
  1713. snprintf(ep->name, sizeof(ep->name), "ep%d%s", epnum - 1,
  1714. dir & 1 ? "in" : "out");
  1715. usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
  1716. ep->usb_ep.max_streams = 0;
  1717. list_add_tail(&ep->usb_ep.ep_list, &bdc->gadget.ep_list);
  1718. }
  1719. ep->usb_ep.ops = &bdc_gadget_ep_ops;
  1720. ep->usb_ep.name = ep->name;
  1721. ep->flags = 0;
  1722. ep->ignore_next_sr = false;
  1723. dev_dbg(bdc->dev, "ep=%p ep->usb_ep.name=%s epnum=%d ep->epnum=%d\n",
  1724. ep, ep->usb_ep.name, epnum, ep->ep_num);
  1725. INIT_LIST_HEAD(&ep->queue);
  1726. return 0;
  1727. }
  1728. /* Init all ep */
  1729. int bdc_init_ep(struct bdc *bdc)
  1730. {
  1731. u8 epnum;
  1732. int ret;
  1733. dev_dbg(bdc->dev, "%s()\n", __func__);
  1734. INIT_LIST_HEAD(&bdc->gadget.ep_list);
  1735. /* init ep0 */
  1736. ret = init_ep(bdc, 1, 0);
  1737. if (ret) {
  1738. dev_err(bdc->dev, "init ep ep0 fail %d\n", ret);
  1739. return ret;
  1740. }
  1741. for (epnum = 2; epnum <= bdc->num_eps / 2; epnum++) {
  1742. /* OUT */
  1743. ret = init_ep(bdc, epnum, 0);
  1744. if (ret) {
  1745. dev_err(bdc->dev,
  1746. "init ep failed for:%d error: %d\n",
  1747. epnum, ret);
  1748. return ret;
  1749. }
  1750. /* IN */
  1751. ret = init_ep(bdc, epnum, 1);
  1752. if (ret) {
  1753. dev_err(bdc->dev,
  1754. "init ep failed for:%d error: %d\n",
  1755. epnum, ret);
  1756. return ret;
  1757. }
  1758. }
  1759. return 0;
  1760. }