bdc_ep.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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 allocated\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. kfree(bd_table);
  147. goto fail;
  148. }
  149. bd_table->dma = dma;
  150. dev_dbg(bdc->dev,
  151. "index:%d start_bd:%p dma=%08llx prev_table:%p\n",
  152. index, bd_table->start_bd,
  153. (unsigned long long)bd_table->dma, prev_table);
  154. ep->bd_list.bd_table_array[index] = bd_table;
  155. memset(bd_table->start_bd, 0, bd_p_tab * sizeof(struct bdc_bd));
  156. if (prev_table)
  157. chain_table(prev_table, bd_table, bd_p_tab);
  158. prev_table = bd_table;
  159. }
  160. chain_table(prev_table, ep->bd_list.bd_table_array[0], bd_p_tab);
  161. /* Memory allocation is successful, now init the internal fields */
  162. ep->bd_list.num_tabs = num_tabs;
  163. ep->bd_list.max_bdi = (num_tabs * bd_p_tab) - 1;
  164. ep->bd_list.num_tabs = num_tabs;
  165. ep->bd_list.num_bds_table = bd_p_tab;
  166. ep->bd_list.eqp_bdi = 0;
  167. ep->bd_list.hwd_bdi = 0;
  168. return 0;
  169. fail:
  170. /* Free the bd_table_array, bd_table struct, bd's */
  171. ep_bd_list_free(ep, num_tabs);
  172. return -ENOMEM;
  173. }
  174. /* returns how many bd's are need for this transfer */
  175. static inline int bd_needed_req(struct bdc_req *req)
  176. {
  177. int bd_needed = 0;
  178. int remaining;
  179. /* 1 bd needed for 0 byte transfer */
  180. if (req->usb_req.length == 0)
  181. return 1;
  182. /* remaining bytes after tranfering all max BD size BD's */
  183. remaining = req->usb_req.length % BD_MAX_BUFF_SIZE;
  184. if (remaining)
  185. bd_needed++;
  186. /* How many maximum BUFF size BD's ? */
  187. remaining = req->usb_req.length / BD_MAX_BUFF_SIZE;
  188. bd_needed += remaining;
  189. return bd_needed;
  190. }
  191. /* returns the bd index(bdi) corresponding to bd dma address */
  192. static int bd_add_to_bdi(struct bdc_ep *ep, dma_addr_t bd_dma_addr)
  193. {
  194. struct bd_list *bd_list = &ep->bd_list;
  195. dma_addr_t dma_first_bd, dma_last_bd;
  196. struct bdc *bdc = ep->bdc;
  197. struct bd_table *bd_table;
  198. bool found = false;
  199. int tbi, bdi;
  200. dma_first_bd = dma_last_bd = 0;
  201. dev_dbg(bdc->dev, "%s %llx\n",
  202. __func__, (unsigned long long)bd_dma_addr);
  203. /*
  204. * Find in which table this bd_dma_addr belongs?, go through the table
  205. * array and compare addresses of first and last address of bd of each
  206. * table
  207. */
  208. for (tbi = 0; tbi < bd_list->num_tabs; tbi++) {
  209. bd_table = bd_list->bd_table_array[tbi];
  210. dma_first_bd = bd_table->dma;
  211. dma_last_bd = bd_table->dma +
  212. (sizeof(struct bdc_bd) *
  213. (bd_list->num_bds_table - 1));
  214. dev_dbg(bdc->dev, "dma_first_bd:%llx dma_last_bd:%llx\n",
  215. (unsigned long long)dma_first_bd,
  216. (unsigned long long)dma_last_bd);
  217. if (bd_dma_addr >= dma_first_bd && bd_dma_addr <= dma_last_bd) {
  218. found = true;
  219. break;
  220. }
  221. }
  222. if (unlikely(!found)) {
  223. dev_err(bdc->dev, "%s FATAL err, bd not found\n", __func__);
  224. return -EINVAL;
  225. }
  226. /* Now we know the table, find the bdi */
  227. bdi = (bd_dma_addr - dma_first_bd) / sizeof(struct bdc_bd);
  228. /* return the global bdi, to compare with ep eqp_bdi */
  229. return (bdi + (tbi * bd_list->num_bds_table));
  230. }
  231. /* returns the table index(tbi) of the given bdi */
  232. static int bdi_to_tbi(struct bdc_ep *ep, int bdi)
  233. {
  234. int tbi;
  235. tbi = bdi / ep->bd_list.num_bds_table;
  236. dev_vdbg(ep->bdc->dev,
  237. "bdi:%d num_bds_table:%d tbi:%d\n",
  238. bdi, ep->bd_list.num_bds_table, tbi);
  239. return tbi;
  240. }
  241. /* Find the bdi last bd in the transfer */
  242. static inline int find_end_bdi(struct bdc_ep *ep, int next_hwd_bdi)
  243. {
  244. int end_bdi;
  245. end_bdi = next_hwd_bdi - 1;
  246. if (end_bdi < 0)
  247. end_bdi = ep->bd_list.max_bdi - 1;
  248. else if ((end_bdi % (ep->bd_list.num_bds_table-1)) == 0)
  249. end_bdi--;
  250. return end_bdi;
  251. }
  252. /*
  253. * How many transfer bd's are available on this ep bdl, chain bds are not
  254. * counted in available bds
  255. */
  256. static int bd_available_ep(struct bdc_ep *ep)
  257. {
  258. struct bd_list *bd_list = &ep->bd_list;
  259. int available1, available2;
  260. struct bdc *bdc = ep->bdc;
  261. int chain_bd1, chain_bd2;
  262. int available_bd = 0;
  263. available1 = available2 = chain_bd1 = chain_bd2 = 0;
  264. /* if empty then we have all bd's available - number of chain bd's */
  265. if (bd_list->eqp_bdi == bd_list->hwd_bdi)
  266. return bd_list->max_bdi - bd_list->num_tabs;
  267. /*
  268. * Depending upon where eqp and dqp pointers are, caculate number
  269. * of avaialble bd's
  270. */
  271. if (bd_list->hwd_bdi < bd_list->eqp_bdi) {
  272. /* available bd's are from eqp..max_bds + 0..dqp - chain_bds */
  273. available1 = bd_list->max_bdi - bd_list->eqp_bdi;
  274. available2 = bd_list->hwd_bdi;
  275. chain_bd1 = available1 / bd_list->num_bds_table;
  276. chain_bd2 = available2 / bd_list->num_bds_table;
  277. dev_vdbg(bdc->dev, "chain_bd1:%d chain_bd2:%d\n",
  278. chain_bd1, chain_bd2);
  279. available_bd = available1 + available2 - chain_bd1 - chain_bd2;
  280. } else {
  281. /* available bd's are from eqp..dqp - number of chain bd's */
  282. available1 = bd_list->hwd_bdi - bd_list->eqp_bdi;
  283. /* if gap between eqp and dqp is less than NUM_BDS_PER_TABLE */
  284. if ((bd_list->hwd_bdi - bd_list->eqp_bdi)
  285. <= bd_list->num_bds_table) {
  286. /* If there any chain bd in between */
  287. if (!(bdi_to_tbi(ep, bd_list->hwd_bdi)
  288. == bdi_to_tbi(ep, bd_list->eqp_bdi))) {
  289. available_bd = available1 - 1;
  290. }
  291. } else {
  292. chain_bd1 = available1 / bd_list->num_bds_table;
  293. available_bd = available1 - chain_bd1;
  294. }
  295. }
  296. /*
  297. * we need to keep one extra bd to check if ring is full or empty so
  298. * reduce by 1
  299. */
  300. available_bd--;
  301. dev_vdbg(bdc->dev, "available_bd:%d\n", available_bd);
  302. return available_bd;
  303. }
  304. /* Notify the hardware after queueing the bd to bdl */
  305. void bdc_notify_xfr(struct bdc *bdc, u32 epnum)
  306. {
  307. struct bdc_ep *ep = bdc->bdc_ep_array[epnum];
  308. dev_vdbg(bdc->dev, "%s epnum:%d\n", __func__, epnum);
  309. /*
  310. * We don't have anyway to check if ep state is running,
  311. * except the software flags.
  312. */
  313. if (unlikely(ep->flags & BDC_EP_STOP))
  314. ep->flags &= ~BDC_EP_STOP;
  315. bdc_writel(bdc->regs, BDC_XSFNTF, epnum);
  316. }
  317. /* returns the bd corresponding to bdi */
  318. static struct bdc_bd *bdi_to_bd(struct bdc_ep *ep, int bdi)
  319. {
  320. int tbi = bdi_to_tbi(ep, bdi);
  321. int local_bdi = 0;
  322. local_bdi = bdi - (tbi * ep->bd_list.num_bds_table);
  323. dev_vdbg(ep->bdc->dev,
  324. "%s bdi:%d local_bdi:%d\n",
  325. __func__, bdi, local_bdi);
  326. return (ep->bd_list.bd_table_array[tbi]->start_bd + local_bdi);
  327. }
  328. /* Advance the enqueue pointer */
  329. static void ep_bdlist_eqp_adv(struct bdc_ep *ep)
  330. {
  331. ep->bd_list.eqp_bdi++;
  332. /* if it's chain bd, then move to next */
  333. if (((ep->bd_list.eqp_bdi + 1) % ep->bd_list.num_bds_table) == 0)
  334. ep->bd_list.eqp_bdi++;
  335. /* if the eqp is pointing to last + 1 then move back to 0 */
  336. if (ep->bd_list.eqp_bdi == (ep->bd_list.max_bdi + 1))
  337. ep->bd_list.eqp_bdi = 0;
  338. }
  339. /* Setup the first bd for ep0 transfer */
  340. static int setup_first_bd_ep0(struct bdc *bdc, struct bdc_req *req, u32 *dword3)
  341. {
  342. u16 wValue;
  343. u32 req_len;
  344. req->ep->dir = 0;
  345. req_len = req->usb_req.length;
  346. switch (bdc->ep0_state) {
  347. case WAIT_FOR_DATA_START:
  348. *dword3 |= BD_TYPE_DS;
  349. if (bdc->setup_pkt.bRequestType & USB_DIR_IN)
  350. *dword3 |= BD_DIR_IN;
  351. /* check if zlp will be needed */
  352. wValue = le16_to_cpu(bdc->setup_pkt.wValue);
  353. if ((wValue > req_len) &&
  354. (req_len % bdc->gadget.ep0->maxpacket == 0)) {
  355. dev_dbg(bdc->dev, "ZLP needed wVal:%d len:%d MaxP:%d\n",
  356. wValue, req_len,
  357. bdc->gadget.ep0->maxpacket);
  358. bdc->zlp_needed = true;
  359. }
  360. break;
  361. case WAIT_FOR_STATUS_START:
  362. *dword3 |= BD_TYPE_SS;
  363. if (!le16_to_cpu(bdc->setup_pkt.wLength) ||
  364. !(bdc->setup_pkt.bRequestType & USB_DIR_IN))
  365. *dword3 |= BD_DIR_IN;
  366. break;
  367. default:
  368. dev_err(bdc->dev,
  369. "Unknown ep0 state for queueing bd ep0_state:%s\n",
  370. ep0_state_string[bdc->ep0_state]);
  371. return -EINVAL;
  372. }
  373. return 0;
  374. }
  375. /* Setup the bd dma descriptor for a given request */
  376. static int setup_bd_list_xfr(struct bdc *bdc, struct bdc_req *req, int num_bds)
  377. {
  378. dma_addr_t buf_add = req->usb_req.dma;
  379. u32 maxp, tfs, dword2, dword3;
  380. struct bd_transfer *bd_xfr;
  381. struct bd_list *bd_list;
  382. struct bdc_ep *ep;
  383. struct bdc_bd *bd;
  384. int ret, bdnum;
  385. u32 req_len;
  386. ep = req->ep;
  387. bd_list = &ep->bd_list;
  388. bd_xfr = &req->bd_xfr;
  389. bd_xfr->req = req;
  390. bd_xfr->start_bdi = bd_list->eqp_bdi;
  391. bd = bdi_to_bd(ep, bd_list->eqp_bdi);
  392. req_len = req->usb_req.length;
  393. maxp = usb_endpoint_maxp(ep->desc);
  394. tfs = roundup(req->usb_req.length, maxp);
  395. tfs = tfs/maxp;
  396. dev_vdbg(bdc->dev, "%s ep:%s num_bds:%d tfs:%d r_len:%d bd:%p\n",
  397. __func__, ep->name, num_bds, tfs, req_len, bd);
  398. for (bdnum = 0; bdnum < num_bds; bdnum++) {
  399. dword2 = dword3 = 0;
  400. /* First bd */
  401. if (!bdnum) {
  402. dword3 |= BD_SOT|BD_SBF|(tfs<<BD_TFS_SHIFT);
  403. dword2 |= BD_LTF;
  404. /* format of first bd for ep0 is different than other */
  405. if (ep->ep_num == 1) {
  406. ret = setup_first_bd_ep0(bdc, req, &dword3);
  407. if (ret)
  408. return ret;
  409. }
  410. }
  411. if (!req->ep->dir)
  412. dword3 |= BD_ISP;
  413. if (req_len > BD_MAX_BUFF_SIZE) {
  414. dword2 |= BD_MAX_BUFF_SIZE;
  415. req_len -= BD_MAX_BUFF_SIZE;
  416. } else {
  417. /* this should be the last bd */
  418. dword2 |= req_len;
  419. dword3 |= BD_IOC;
  420. dword3 |= BD_EOT;
  421. }
  422. /* Currently only 1 INT target is supported */
  423. dword2 |= BD_INTR_TARGET(0);
  424. bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
  425. if (unlikely(!bd)) {
  426. dev_err(bdc->dev, "Err bd pointing to wrong addr\n");
  427. return -EINVAL;
  428. }
  429. /* write bd */
  430. bd->offset[0] = cpu_to_le32(lower_32_bits(buf_add));
  431. bd->offset[1] = cpu_to_le32(upper_32_bits(buf_add));
  432. bd->offset[2] = cpu_to_le32(dword2);
  433. bd->offset[3] = cpu_to_le32(dword3);
  434. /* advance eqp pointer */
  435. ep_bdlist_eqp_adv(ep);
  436. /* advance the buff pointer */
  437. buf_add += BD_MAX_BUFF_SIZE;
  438. dev_vdbg(bdc->dev, "buf_add:%08llx req_len:%d bd:%p eqp:%d\n",
  439. (unsigned long long)buf_add, req_len, bd,
  440. ep->bd_list.eqp_bdi);
  441. bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
  442. bd->offset[3] = cpu_to_le32(BD_SBF);
  443. }
  444. /* clear the STOP BD fetch bit from the first bd of this xfr */
  445. bd = bdi_to_bd(ep, bd_xfr->start_bdi);
  446. bd->offset[3] &= cpu_to_le32(~BD_SBF);
  447. /* the new eqp will be next hw dqp */
  448. bd_xfr->num_bds = num_bds;
  449. bd_xfr->next_hwd_bdi = ep->bd_list.eqp_bdi;
  450. /* everything is written correctly before notifying the HW */
  451. wmb();
  452. return 0;
  453. }
  454. /* Queue the xfr */
  455. static int bdc_queue_xfr(struct bdc *bdc, struct bdc_req *req)
  456. {
  457. int num_bds, bd_available;
  458. struct bdc_ep *ep;
  459. int ret;
  460. ep = req->ep;
  461. dev_dbg(bdc->dev, "%s req:%p\n", __func__, req);
  462. dev_dbg(bdc->dev, "eqp_bdi:%d hwd_bdi:%d\n",
  463. ep->bd_list.eqp_bdi, ep->bd_list.hwd_bdi);
  464. num_bds = bd_needed_req(req);
  465. bd_available = bd_available_ep(ep);
  466. /* how many bd's are avaialble on ep */
  467. if (num_bds > bd_available)
  468. return -ENOMEM;
  469. ret = setup_bd_list_xfr(bdc, req, num_bds);
  470. if (ret)
  471. return ret;
  472. list_add_tail(&req->queue, &ep->queue);
  473. bdc_dbg_bd_list(bdc, ep);
  474. bdc_notify_xfr(bdc, ep->ep_num);
  475. return 0;
  476. }
  477. /* callback to gadget layer when xfr completes */
  478. static void bdc_req_complete(struct bdc_ep *ep, struct bdc_req *req,
  479. int status)
  480. {
  481. struct bdc *bdc = ep->bdc;
  482. if (req == NULL || &req->queue == NULL || &req->usb_req == NULL)
  483. return;
  484. dev_dbg(bdc->dev, "%s ep:%s status:%d\n", __func__, ep->name, status);
  485. list_del(&req->queue);
  486. req->usb_req.status = status;
  487. usb_gadget_unmap_request(&bdc->gadget, &req->usb_req, ep->dir);
  488. if (req->usb_req.complete) {
  489. spin_unlock(&bdc->lock);
  490. usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
  491. spin_lock(&bdc->lock);
  492. }
  493. }
  494. /* Disable the endpoint */
  495. int bdc_ep_disable(struct bdc_ep *ep)
  496. {
  497. struct bdc_req *req;
  498. struct bdc *bdc;
  499. int ret;
  500. ret = 0;
  501. bdc = ep->bdc;
  502. dev_dbg(bdc->dev, "%s() ep->ep_num=%d\n", __func__, ep->ep_num);
  503. /* Stop the endpoint */
  504. ret = bdc_stop_ep(bdc, ep->ep_num);
  505. /*
  506. * Intentionally don't check the ret value of stop, it can fail in
  507. * disconnect scenarios, continue with dconfig
  508. */
  509. /* de-queue any pending requests */
  510. while (!list_empty(&ep->queue)) {
  511. req = list_entry(ep->queue.next, struct bdc_req,
  512. queue);
  513. bdc_req_complete(ep, req, -ESHUTDOWN);
  514. }
  515. /* deconfigure the endpoint */
  516. ret = bdc_dconfig_ep(bdc, ep);
  517. if (ret)
  518. dev_warn(bdc->dev,
  519. "dconfig fail but continue with memory free");
  520. ep->flags = 0;
  521. /* ep0 memory is not freed, but reused on next connect sr */
  522. if (ep->ep_num == 1)
  523. return 0;
  524. /* Free the bdl memory */
  525. ep_bd_list_free(ep, ep->bd_list.num_tabs);
  526. ep->desc = NULL;
  527. ep->comp_desc = NULL;
  528. ep->usb_ep.desc = NULL;
  529. ep->ep_type = 0;
  530. return ret;
  531. }
  532. /* Enable the ep */
  533. int bdc_ep_enable(struct bdc_ep *ep)
  534. {
  535. struct bdc *bdc;
  536. int ret = 0;
  537. bdc = ep->bdc;
  538. dev_dbg(bdc->dev, "%s NUM_TABLES:%d %d\n",
  539. __func__, NUM_TABLES, NUM_TABLES_ISOCH);
  540. ret = ep_bd_list_alloc(ep);
  541. if (ret) {
  542. dev_err(bdc->dev, "ep bd list allocation failed:%d\n", ret);
  543. return -ENOMEM;
  544. }
  545. bdc_dbg_bd_list(bdc, ep);
  546. /* only for ep0: config ep is called for ep0 from connect event */
  547. ep->flags |= BDC_EP_ENABLED;
  548. if (ep->ep_num == 1)
  549. return ret;
  550. /* Issue a configure endpoint command */
  551. ret = bdc_config_ep(bdc, ep);
  552. if (ret)
  553. return ret;
  554. ep->usb_ep.maxpacket = usb_endpoint_maxp(ep->desc);
  555. ep->usb_ep.desc = ep->desc;
  556. ep->usb_ep.comp_desc = ep->comp_desc;
  557. ep->ep_type = usb_endpoint_type(ep->desc);
  558. ep->flags |= BDC_EP_ENABLED;
  559. return 0;
  560. }
  561. /* EP0 related code */
  562. /* Queue a status stage BD */
  563. static int ep0_queue_status_stage(struct bdc *bdc)
  564. {
  565. struct bdc_req *status_req;
  566. struct bdc_ep *ep;
  567. status_req = &bdc->status_req;
  568. ep = bdc->bdc_ep_array[1];
  569. status_req->ep = ep;
  570. status_req->usb_req.length = 0;
  571. status_req->usb_req.status = -EINPROGRESS;
  572. status_req->usb_req.actual = 0;
  573. status_req->usb_req.complete = NULL;
  574. bdc_queue_xfr(bdc, status_req);
  575. return 0;
  576. }
  577. /* Queue xfr on ep0 */
  578. static int ep0_queue(struct bdc_ep *ep, struct bdc_req *req)
  579. {
  580. struct bdc *bdc;
  581. int ret;
  582. bdc = ep->bdc;
  583. dev_dbg(bdc->dev, "%s()\n", __func__);
  584. req->usb_req.actual = 0;
  585. req->usb_req.status = -EINPROGRESS;
  586. req->epnum = ep->ep_num;
  587. if (bdc->delayed_status) {
  588. bdc->delayed_status = false;
  589. /* if status stage was delayed? */
  590. if (bdc->ep0_state == WAIT_FOR_STATUS_START) {
  591. /* Queue a status stage BD */
  592. ep0_queue_status_stage(bdc);
  593. bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
  594. return 0;
  595. }
  596. } else {
  597. /*
  598. * if delayed status is false and 0 length transfer is requested
  599. * i.e. for status stage of some setup request, then just
  600. * return from here the status stage is queued independently
  601. */
  602. if (req->usb_req.length == 0)
  603. return 0;
  604. }
  605. ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
  606. if (ret) {
  607. dev_err(bdc->dev, "dma mapping failed %s\n", ep->name);
  608. return ret;
  609. }
  610. return bdc_queue_xfr(bdc, req);
  611. }
  612. /* Queue data stage */
  613. static int ep0_queue_data_stage(struct bdc *bdc)
  614. {
  615. struct bdc_ep *ep;
  616. dev_dbg(bdc->dev, "%s\n", __func__);
  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->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_EPSTS1(0));
  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 set STALL on %s\n",
  768. 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 clear STALL on %s\n",
  777. 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. dev_dbg(bdc->dev, "%s\n", __func__);
  1215. wLength = le16_to_cpu(setup_pkt->wLength);
  1216. if (unlikely(wLength != 6)) {
  1217. dev_err(bdc->dev, "%s Wrong wLength:%d\n", __func__, wLength);
  1218. return -EINVAL;
  1219. }
  1220. ep = bdc->bdc_ep_array[1];
  1221. bdc->ep0_req.ep = ep;
  1222. bdc->ep0_req.usb_req.length = 6;
  1223. bdc->ep0_req.usb_req.buf = bdc->ep0_response_buff;
  1224. bdc->ep0_req.usb_req.complete = ep0_set_sel_cmpl;
  1225. ep0_queue_data_stage(bdc);
  1226. return 0;
  1227. }
  1228. /*
  1229. * Queue a 0 byte bd only if wLength is more than the length and and length is
  1230. * a multiple of MaxPacket then queue 0 byte BD
  1231. */
  1232. static int ep0_queue_zlp(struct bdc *bdc)
  1233. {
  1234. int ret;
  1235. dev_dbg(bdc->dev, "%s\n", __func__);
  1236. bdc->ep0_req.ep = bdc->bdc_ep_array[1];
  1237. bdc->ep0_req.usb_req.length = 0;
  1238. bdc->ep0_req.usb_req.complete = NULL;
  1239. bdc->ep0_state = WAIT_FOR_DATA_START;
  1240. ret = bdc_queue_xfr(bdc, &bdc->ep0_req);
  1241. if (ret) {
  1242. dev_err(bdc->dev, "err queueing zlp :%d\n", ret);
  1243. return ret;
  1244. }
  1245. bdc->ep0_state = WAIT_FOR_DATA_XMIT;
  1246. return 0;
  1247. }
  1248. /* Control request handler */
  1249. static int handle_control_request(struct bdc *bdc)
  1250. {
  1251. enum usb_device_state state = bdc->gadget.state;
  1252. struct usb_ctrlrequest *setup_pkt;
  1253. int delegate_setup = 0;
  1254. int ret = 0;
  1255. int config = 0;
  1256. setup_pkt = &bdc->setup_pkt;
  1257. dev_dbg(bdc->dev, "%s\n", __func__);
  1258. if ((setup_pkt->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1259. switch (setup_pkt->bRequest) {
  1260. case USB_REQ_SET_ADDRESS:
  1261. dev_dbg(bdc->dev, "USB_REQ_SET_ADDRESS\n");
  1262. ret = ep0_set_address(bdc, setup_pkt);
  1263. bdc->devstatus &= DEVSTATUS_CLEAR;
  1264. break;
  1265. case USB_REQ_SET_CONFIGURATION:
  1266. dev_dbg(bdc->dev, "USB_REQ_SET_CONFIGURATION\n");
  1267. if (state == USB_STATE_ADDRESS) {
  1268. usb_gadget_set_state(&bdc->gadget,
  1269. USB_STATE_CONFIGURED);
  1270. } else if (state == USB_STATE_CONFIGURED) {
  1271. /*
  1272. * USB2 spec sec 9.4.7, if wValue is 0 then dev
  1273. * is moved to addressed state
  1274. */
  1275. config = le16_to_cpu(setup_pkt->wValue);
  1276. if (!config)
  1277. usb_gadget_set_state(
  1278. &bdc->gadget,
  1279. USB_STATE_ADDRESS);
  1280. }
  1281. delegate_setup = 1;
  1282. break;
  1283. case USB_REQ_SET_FEATURE:
  1284. dev_dbg(bdc->dev, "USB_REQ_SET_FEATURE\n");
  1285. ret = ep0_handle_feature(bdc, setup_pkt, 1);
  1286. break;
  1287. case USB_REQ_CLEAR_FEATURE:
  1288. dev_dbg(bdc->dev, "USB_REQ_CLEAR_FEATURE\n");
  1289. ret = ep0_handle_feature(bdc, setup_pkt, 0);
  1290. break;
  1291. case USB_REQ_GET_STATUS:
  1292. dev_dbg(bdc->dev, "USB_REQ_GET_STATUS\n");
  1293. ret = ep0_handle_status(bdc, setup_pkt);
  1294. break;
  1295. case USB_REQ_SET_SEL:
  1296. dev_dbg(bdc->dev, "USB_REQ_SET_SEL\n");
  1297. ret = ep0_set_sel(bdc, setup_pkt);
  1298. break;
  1299. case USB_REQ_SET_ISOCH_DELAY:
  1300. dev_warn(bdc->dev,
  1301. "USB_REQ_SET_ISOCH_DELAY not handled\n");
  1302. ret = 0;
  1303. break;
  1304. default:
  1305. delegate_setup = 1;
  1306. }
  1307. } else {
  1308. delegate_setup = 1;
  1309. }
  1310. if (delegate_setup) {
  1311. spin_unlock(&bdc->lock);
  1312. ret = bdc->gadget_driver->setup(&bdc->gadget, setup_pkt);
  1313. spin_lock(&bdc->lock);
  1314. }
  1315. return ret;
  1316. }
  1317. /* EP0: Data stage started */
  1318. void bdc_xsf_ep0_data_start(struct bdc *bdc, struct bdc_sr *sreport)
  1319. {
  1320. struct bdc_ep *ep;
  1321. int ret = 0;
  1322. dev_dbg(bdc->dev, "%s\n", __func__);
  1323. ep = bdc->bdc_ep_array[1];
  1324. /* If ep0 was stalled, the clear it first */
  1325. if (ep->flags & BDC_EP_STALL) {
  1326. ret = ep_set_halt(ep, 0);
  1327. if (ret)
  1328. goto err;
  1329. }
  1330. if (bdc->ep0_state != WAIT_FOR_DATA_START)
  1331. dev_warn(bdc->dev,
  1332. "Data stage not expected ep0_state:%s\n",
  1333. ep0_state_string[bdc->ep0_state]);
  1334. ret = handle_control_request(bdc);
  1335. if (ret == USB_GADGET_DELAYED_STATUS) {
  1336. /*
  1337. * The ep0 state will remain WAIT_FOR_DATA_START till
  1338. * we received ep_queue on ep0
  1339. */
  1340. bdc->delayed_status = true;
  1341. return;
  1342. }
  1343. if (!ret) {
  1344. bdc->ep0_state = WAIT_FOR_DATA_XMIT;
  1345. dev_dbg(bdc->dev,
  1346. "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
  1347. return;
  1348. }
  1349. err:
  1350. ep0_stall(bdc);
  1351. }
  1352. /* EP0: status stage started */
  1353. void bdc_xsf_ep0_status_start(struct bdc *bdc, struct bdc_sr *sreport)
  1354. {
  1355. struct usb_ctrlrequest *setup_pkt;
  1356. struct bdc_ep *ep;
  1357. int ret = 0;
  1358. dev_dbg(bdc->dev,
  1359. "%s ep0_state:%s",
  1360. __func__, ep0_state_string[bdc->ep0_state]);
  1361. ep = bdc->bdc_ep_array[1];
  1362. /* check if ZLP was queued? */
  1363. if (bdc->zlp_needed)
  1364. bdc->zlp_needed = false;
  1365. if (ep->flags & BDC_EP_STALL) {
  1366. ret = ep_set_halt(ep, 0);
  1367. if (ret)
  1368. goto err;
  1369. }
  1370. if ((bdc->ep0_state != WAIT_FOR_STATUS_START) &&
  1371. (bdc->ep0_state != WAIT_FOR_DATA_XMIT))
  1372. dev_err(bdc->dev,
  1373. "Status stage recv but ep0_state:%s\n",
  1374. ep0_state_string[bdc->ep0_state]);
  1375. /* check if data stage is in progress ? */
  1376. if (bdc->ep0_state == WAIT_FOR_DATA_XMIT) {
  1377. bdc->ep0_state = STATUS_PENDING;
  1378. /* Status stage will be queued upon Data stage transmit event */
  1379. dev_dbg(bdc->dev,
  1380. "status started but data not transmitted yet\n");
  1381. return;
  1382. }
  1383. setup_pkt = &bdc->setup_pkt;
  1384. /*
  1385. * 2 stage setup then only process the setup, for 3 stage setup the date
  1386. * stage is already handled
  1387. */
  1388. if (!le16_to_cpu(setup_pkt->wLength)) {
  1389. ret = handle_control_request(bdc);
  1390. if (ret == USB_GADGET_DELAYED_STATUS) {
  1391. bdc->delayed_status = true;
  1392. /* ep0_state will remain WAIT_FOR_STATUS_START */
  1393. return;
  1394. }
  1395. }
  1396. if (!ret) {
  1397. /* Queue a status stage BD */
  1398. ep0_queue_status_stage(bdc);
  1399. bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
  1400. dev_dbg(bdc->dev,
  1401. "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
  1402. return;
  1403. }
  1404. err:
  1405. ep0_stall(bdc);
  1406. }
  1407. /* Helper function to update ep0 upon SR with xsf_succ or xsf_short */
  1408. static void ep0_xsf_complete(struct bdc *bdc, struct bdc_sr *sreport)
  1409. {
  1410. dev_dbg(bdc->dev, "%s\n", __func__);
  1411. switch (bdc->ep0_state) {
  1412. case WAIT_FOR_DATA_XMIT:
  1413. bdc->ep0_state = WAIT_FOR_STATUS_START;
  1414. break;
  1415. case WAIT_FOR_STATUS_XMIT:
  1416. bdc->ep0_state = WAIT_FOR_SETUP;
  1417. if (bdc->test_mode) {
  1418. int ret;
  1419. dev_dbg(bdc->dev, "test_mode:%d\n", bdc->test_mode);
  1420. ret = bdc_set_test_mode(bdc);
  1421. if (ret < 0) {
  1422. dev_err(bdc->dev, "Err in setting Test mode\n");
  1423. return;
  1424. }
  1425. bdc->test_mode = 0;
  1426. }
  1427. break;
  1428. case STATUS_PENDING:
  1429. bdc_xsf_ep0_status_start(bdc, sreport);
  1430. break;
  1431. default:
  1432. dev_err(bdc->dev,
  1433. "Unknown ep0_state:%s\n",
  1434. ep0_state_string[bdc->ep0_state]);
  1435. }
  1436. }
  1437. /* xfr completion status report handler */
  1438. void bdc_sr_xsf(struct bdc *bdc, struct bdc_sr *sreport)
  1439. {
  1440. struct bdc_ep *ep;
  1441. u32 sr_status;
  1442. u8 ep_num;
  1443. ep_num = (le32_to_cpu(sreport->offset[3])>>4) & 0x1f;
  1444. ep = bdc->bdc_ep_array[ep_num];
  1445. if (!ep || !(ep->flags & BDC_EP_ENABLED)) {
  1446. dev_err(bdc->dev, "xsf for ep not enabled\n");
  1447. return;
  1448. }
  1449. /*
  1450. * check if this transfer is after link went from U3->U0 due
  1451. * to remote wakeup
  1452. */
  1453. if (bdc->devstatus & FUNC_WAKE_ISSUED) {
  1454. bdc->devstatus &= ~(FUNC_WAKE_ISSUED);
  1455. dev_dbg(bdc->dev, "%s clearing FUNC_WAKE_ISSUED flag\n",
  1456. __func__);
  1457. }
  1458. sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
  1459. dev_dbg_ratelimited(bdc->dev, "%s sr_status=%d ep:%s\n",
  1460. __func__, sr_status, ep->name);
  1461. switch (sr_status) {
  1462. case XSF_SUCC:
  1463. case XSF_SHORT:
  1464. handle_xsr_succ_status(bdc, ep, sreport);
  1465. if (ep_num == 1)
  1466. ep0_xsf_complete(bdc, sreport);
  1467. break;
  1468. case XSF_SETUP_RECV:
  1469. case XSF_DATA_START:
  1470. case XSF_STATUS_START:
  1471. if (ep_num != 1) {
  1472. dev_err(bdc->dev,
  1473. "ep0 related packets on non ep0 endpoint");
  1474. return;
  1475. }
  1476. bdc->sr_xsf_ep0[sr_status - XSF_SETUP_RECV](bdc, sreport);
  1477. break;
  1478. case XSF_BABB:
  1479. if (ep_num == 1) {
  1480. dev_dbg(bdc->dev, "Babble on ep0 zlp_need:%d\n",
  1481. bdc->zlp_needed);
  1482. /*
  1483. * If the last completed transfer had wLength >Data Len,
  1484. * and Len is multiple of MaxPacket,then queue ZLP
  1485. */
  1486. if (bdc->zlp_needed) {
  1487. /* queue 0 length bd */
  1488. ep0_queue_zlp(bdc);
  1489. return;
  1490. }
  1491. }
  1492. dev_warn(bdc->dev, "Babble on ep not handled\n");
  1493. break;
  1494. default:
  1495. dev_warn(bdc->dev, "sr status not handled:%x\n", sr_status);
  1496. break;
  1497. }
  1498. }
  1499. static int bdc_gadget_ep_queue(struct usb_ep *_ep,
  1500. struct usb_request *_req, gfp_t gfp_flags)
  1501. {
  1502. struct bdc_req *req;
  1503. unsigned long flags;
  1504. struct bdc_ep *ep;
  1505. struct bdc *bdc;
  1506. int ret;
  1507. if (!_ep || !_ep->desc)
  1508. return -ESHUTDOWN;
  1509. if (!_req || !_req->complete || !_req->buf)
  1510. return -EINVAL;
  1511. ep = to_bdc_ep(_ep);
  1512. req = to_bdc_req(_req);
  1513. bdc = ep->bdc;
  1514. dev_dbg(bdc->dev, "%s ep:%p req:%p\n", __func__, ep, req);
  1515. dev_dbg(bdc->dev, "queuing request %p to %s length %d zero:%d\n",
  1516. _req, ep->name, _req->length, _req->zero);
  1517. if (!ep->usb_ep.desc) {
  1518. dev_warn(bdc->dev,
  1519. "trying to queue req %p to disabled %s\n",
  1520. _req, ep->name);
  1521. return -ESHUTDOWN;
  1522. }
  1523. if (_req->length > MAX_XFR_LEN) {
  1524. dev_warn(bdc->dev,
  1525. "req length > supported MAX:%d requested:%d\n",
  1526. MAX_XFR_LEN, _req->length);
  1527. return -EOPNOTSUPP;
  1528. }
  1529. spin_lock_irqsave(&bdc->lock, flags);
  1530. if (ep == bdc->bdc_ep_array[1])
  1531. ret = ep0_queue(ep, req);
  1532. else
  1533. ret = ep_queue(ep, req);
  1534. spin_unlock_irqrestore(&bdc->lock, flags);
  1535. return ret;
  1536. }
  1537. static int bdc_gadget_ep_dequeue(struct usb_ep *_ep,
  1538. struct usb_request *_req)
  1539. {
  1540. struct bdc_req *req;
  1541. unsigned long flags;
  1542. struct bdc_ep *ep;
  1543. struct bdc *bdc;
  1544. int ret;
  1545. if (!_ep || !_req)
  1546. return -EINVAL;
  1547. ep = to_bdc_ep(_ep);
  1548. req = to_bdc_req(_req);
  1549. bdc = ep->bdc;
  1550. dev_dbg(bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
  1551. bdc_dbg_bd_list(bdc, ep);
  1552. spin_lock_irqsave(&bdc->lock, flags);
  1553. /* make sure it's still queued on this endpoint */
  1554. list_for_each_entry(req, &ep->queue, queue) {
  1555. if (&req->usb_req == _req)
  1556. break;
  1557. }
  1558. if (&req->usb_req != _req) {
  1559. spin_unlock_irqrestore(&bdc->lock, flags);
  1560. dev_err(bdc->dev, "usb_req !=req n");
  1561. return -EINVAL;
  1562. }
  1563. ret = ep_dequeue(ep, req);
  1564. if (ret) {
  1565. ret = -EOPNOTSUPP;
  1566. goto err;
  1567. }
  1568. bdc_req_complete(ep, req, -ECONNRESET);
  1569. err:
  1570. bdc_dbg_bd_list(bdc, ep);
  1571. spin_unlock_irqrestore(&bdc->lock, flags);
  1572. return ret;
  1573. }
  1574. static int bdc_gadget_ep_set_halt(struct usb_ep *_ep, int value)
  1575. {
  1576. unsigned long flags;
  1577. struct bdc_ep *ep;
  1578. struct bdc *bdc;
  1579. int ret;
  1580. ep = to_bdc_ep(_ep);
  1581. bdc = ep->bdc;
  1582. dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
  1583. spin_lock_irqsave(&bdc->lock, flags);
  1584. if (usb_endpoint_xfer_isoc(ep->usb_ep.desc))
  1585. ret = -EINVAL;
  1586. else if (!list_empty(&ep->queue))
  1587. ret = -EAGAIN;
  1588. else
  1589. ret = ep_set_halt(ep, value);
  1590. spin_unlock_irqrestore(&bdc->lock, flags);
  1591. return ret;
  1592. }
  1593. static struct usb_request *bdc_gadget_alloc_request(struct usb_ep *_ep,
  1594. gfp_t gfp_flags)
  1595. {
  1596. struct bdc_req *req;
  1597. struct bdc_ep *ep;
  1598. req = kzalloc(sizeof(*req), gfp_flags);
  1599. if (!req)
  1600. return NULL;
  1601. ep = to_bdc_ep(_ep);
  1602. req->ep = ep;
  1603. req->epnum = ep->ep_num;
  1604. req->usb_req.dma = DMA_ADDR_INVALID;
  1605. dev_dbg(ep->bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
  1606. return &req->usb_req;
  1607. }
  1608. static void bdc_gadget_free_request(struct usb_ep *_ep,
  1609. struct usb_request *_req)
  1610. {
  1611. struct bdc_req *req;
  1612. req = to_bdc_req(_req);
  1613. kfree(req);
  1614. }
  1615. /* endpoint operations */
  1616. /* configure endpoint and also allocate resources */
  1617. static int bdc_gadget_ep_enable(struct usb_ep *_ep,
  1618. const struct usb_endpoint_descriptor *desc)
  1619. {
  1620. unsigned long flags;
  1621. struct bdc_ep *ep;
  1622. struct bdc *bdc;
  1623. int ret;
  1624. if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  1625. pr_debug("bdc_gadget_ep_enable invalid parameters\n");
  1626. return -EINVAL;
  1627. }
  1628. if (!desc->wMaxPacketSize) {
  1629. pr_debug("bdc_gadget_ep_enable missing wMaxPacketSize\n");
  1630. return -EINVAL;
  1631. }
  1632. ep = to_bdc_ep(_ep);
  1633. bdc = ep->bdc;
  1634. /* Sanity check, upper layer will not send enable for ep0 */
  1635. if (ep == bdc->bdc_ep_array[1])
  1636. return -EINVAL;
  1637. if (!bdc->gadget_driver
  1638. || bdc->gadget.speed == USB_SPEED_UNKNOWN) {
  1639. return -ESHUTDOWN;
  1640. }
  1641. dev_dbg(bdc->dev, "%s Enabling %s\n", __func__, ep->name);
  1642. spin_lock_irqsave(&bdc->lock, flags);
  1643. ep->desc = desc;
  1644. ep->comp_desc = _ep->comp_desc;
  1645. ret = bdc_ep_enable(ep);
  1646. spin_unlock_irqrestore(&bdc->lock, flags);
  1647. return ret;
  1648. }
  1649. static int bdc_gadget_ep_disable(struct usb_ep *_ep)
  1650. {
  1651. unsigned long flags;
  1652. struct bdc_ep *ep;
  1653. struct bdc *bdc;
  1654. int ret;
  1655. if (!_ep) {
  1656. pr_debug("bdc: invalid parameters\n");
  1657. return -EINVAL;
  1658. }
  1659. ep = to_bdc_ep(_ep);
  1660. bdc = ep->bdc;
  1661. /* Upper layer will not call this for ep0, but do a sanity check */
  1662. if (ep == bdc->bdc_ep_array[1]) {
  1663. dev_warn(bdc->dev, "%s called for ep0\n", __func__);
  1664. return -EINVAL;
  1665. }
  1666. dev_dbg(bdc->dev,
  1667. "%s() ep:%s ep->flags:%08x\n",
  1668. __func__, ep->name, ep->flags);
  1669. if (!(ep->flags & BDC_EP_ENABLED)) {
  1670. dev_warn(bdc->dev, "%s is already disabled\n", ep->name);
  1671. return 0;
  1672. }
  1673. spin_lock_irqsave(&bdc->lock, flags);
  1674. ret = bdc_ep_disable(ep);
  1675. spin_unlock_irqrestore(&bdc->lock, flags);
  1676. return ret;
  1677. }
  1678. static const struct usb_ep_ops bdc_gadget_ep_ops = {
  1679. .enable = bdc_gadget_ep_enable,
  1680. .disable = bdc_gadget_ep_disable,
  1681. .alloc_request = bdc_gadget_alloc_request,
  1682. .free_request = bdc_gadget_free_request,
  1683. .queue = bdc_gadget_ep_queue,
  1684. .dequeue = bdc_gadget_ep_dequeue,
  1685. .set_halt = bdc_gadget_ep_set_halt
  1686. };
  1687. /* dir = 1 is IN */
  1688. static int init_ep(struct bdc *bdc, u32 epnum, u32 dir)
  1689. {
  1690. struct bdc_ep *ep;
  1691. dev_dbg(bdc->dev, "%s epnum=%d dir=%d\n", __func__, epnum, dir);
  1692. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1693. if (!ep)
  1694. return -ENOMEM;
  1695. ep->bdc = bdc;
  1696. ep->dir = dir;
  1697. if (dir)
  1698. ep->usb_ep.caps.dir_in = true;
  1699. else
  1700. ep->usb_ep.caps.dir_out = true;
  1701. /* ep->ep_num is the index inside bdc_ep */
  1702. if (epnum == 1) {
  1703. ep->ep_num = 1;
  1704. bdc->bdc_ep_array[ep->ep_num] = ep;
  1705. snprintf(ep->name, sizeof(ep->name), "ep%d", epnum - 1);
  1706. usb_ep_set_maxpacket_limit(&ep->usb_ep, EP0_MAX_PKT_SIZE);
  1707. ep->usb_ep.caps.type_control = true;
  1708. ep->comp_desc = NULL;
  1709. bdc->gadget.ep0 = &ep->usb_ep;
  1710. } else {
  1711. if (dir)
  1712. ep->ep_num = epnum * 2 - 1;
  1713. else
  1714. ep->ep_num = epnum * 2 - 2;
  1715. bdc->bdc_ep_array[ep->ep_num] = ep;
  1716. snprintf(ep->name, sizeof(ep->name), "ep%d%s", epnum - 1,
  1717. dir & 1 ? "in" : "out");
  1718. usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
  1719. ep->usb_ep.caps.type_iso = true;
  1720. ep->usb_ep.caps.type_bulk = true;
  1721. ep->usb_ep.caps.type_int = true;
  1722. ep->usb_ep.max_streams = 0;
  1723. list_add_tail(&ep->usb_ep.ep_list, &bdc->gadget.ep_list);
  1724. }
  1725. ep->usb_ep.ops = &bdc_gadget_ep_ops;
  1726. ep->usb_ep.name = ep->name;
  1727. ep->flags = 0;
  1728. ep->ignore_next_sr = false;
  1729. dev_dbg(bdc->dev, "ep=%p ep->usb_ep.name=%s epnum=%d ep->epnum=%d\n",
  1730. ep, ep->usb_ep.name, epnum, ep->ep_num);
  1731. INIT_LIST_HEAD(&ep->queue);
  1732. return 0;
  1733. }
  1734. /* Init all ep */
  1735. int bdc_init_ep(struct bdc *bdc)
  1736. {
  1737. u8 epnum;
  1738. int ret;
  1739. dev_dbg(bdc->dev, "%s()\n", __func__);
  1740. INIT_LIST_HEAD(&bdc->gadget.ep_list);
  1741. /* init ep0 */
  1742. ret = init_ep(bdc, 1, 0);
  1743. if (ret) {
  1744. dev_err(bdc->dev, "init ep ep0 fail %d\n", ret);
  1745. return ret;
  1746. }
  1747. for (epnum = 2; epnum <= bdc->num_eps / 2; epnum++) {
  1748. /* OUT */
  1749. ret = init_ep(bdc, epnum, 0);
  1750. if (ret) {
  1751. dev_err(bdc->dev,
  1752. "init ep failed for:%d error: %d\n",
  1753. epnum, ret);
  1754. return ret;
  1755. }
  1756. /* IN */
  1757. ret = init_ep(bdc, epnum, 1);
  1758. if (ret) {
  1759. dev_err(bdc->dev,
  1760. "init ep failed for:%d error: %d\n",
  1761. epnum, ret);
  1762. return ret;
  1763. }
  1764. }
  1765. return 0;
  1766. }