bdc_ep.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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. 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) & 0x7ff;
  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 usb_request *ep0_usb_req;
  616. struct bdc_ep *ep;
  617. dev_dbg(bdc->dev, "%s\n", __func__);
  618. ep0_usb_req = &bdc->ep0_req.usb_req;
  619. ep = bdc->bdc_ep_array[1];
  620. bdc->ep0_req.ep = ep;
  621. bdc->ep0_req.usb_req.complete = NULL;
  622. return ep0_queue(ep, &bdc->ep0_req);
  623. }
  624. /* Queue req on ep */
  625. static int ep_queue(struct bdc_ep *ep, struct bdc_req *req)
  626. {
  627. struct bdc *bdc;
  628. int ret = 0;
  629. if (!req || !ep->usb_ep.desc)
  630. return -EINVAL;
  631. bdc = ep->bdc;
  632. req->usb_req.actual = 0;
  633. req->usb_req.status = -EINPROGRESS;
  634. req->epnum = ep->ep_num;
  635. ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
  636. if (ret) {
  637. dev_err(bdc->dev, "dma mapping failed\n");
  638. return ret;
  639. }
  640. return bdc_queue_xfr(bdc, req);
  641. }
  642. /* Dequeue a request from ep */
  643. static int ep_dequeue(struct bdc_ep *ep, struct bdc_req *req)
  644. {
  645. int start_bdi, end_bdi, tbi, eqp_bdi, curr_hw_dqpi;
  646. bool start_pending, end_pending;
  647. bool first_remove = false;
  648. struct bdc_req *first_req;
  649. struct bdc_bd *bd_start;
  650. struct bd_table *table;
  651. dma_addr_t next_bd_dma;
  652. u64 deq_ptr_64 = 0;
  653. struct bdc *bdc;
  654. u32 tmp_32;
  655. int ret;
  656. bdc = ep->bdc;
  657. start_pending = end_pending = false;
  658. eqp_bdi = ep->bd_list.eqp_bdi - 1;
  659. if (eqp_bdi < 0)
  660. eqp_bdi = ep->bd_list.max_bdi;
  661. start_bdi = req->bd_xfr.start_bdi;
  662. end_bdi = find_end_bdi(ep, req->bd_xfr.next_hwd_bdi);
  663. dev_dbg(bdc->dev, "%s ep:%s start:%d end:%d\n",
  664. __func__, ep->name, start_bdi, end_bdi);
  665. dev_dbg(bdc->dev, "ep_dequeue ep=%p ep->desc=%p\n",
  666. ep, (void *)ep->usb_ep.desc);
  667. /* Stop the ep to see where the HW is ? */
  668. ret = bdc_stop_ep(bdc, ep->ep_num);
  669. /* if there is an issue with stopping ep, then no need to go further */
  670. if (ret)
  671. return 0;
  672. /*
  673. * After endpoint is stopped, there can be 3 cases, the request
  674. * is processed, pending or in the middle of processing
  675. */
  676. /* The current hw dequeue pointer */
  677. tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS0(0));
  678. deq_ptr_64 = tmp_32;
  679. tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS1(0));
  680. deq_ptr_64 |= ((u64)tmp_32 << 32);
  681. /* we have the dma addr of next bd that will be fetched by hardware */
  682. curr_hw_dqpi = bd_add_to_bdi(ep, deq_ptr_64);
  683. if (curr_hw_dqpi < 0)
  684. return curr_hw_dqpi;
  685. /*
  686. * curr_hw_dqpi points to actual dqp of HW and HW owns bd's from
  687. * curr_hw_dqbdi..eqp_bdi.
  688. */
  689. /* Check if start_bdi and end_bdi are in range of HW owned BD's */
  690. if (curr_hw_dqpi > eqp_bdi) {
  691. /* there is a wrap from last to 0 */
  692. if (start_bdi >= curr_hw_dqpi || start_bdi <= eqp_bdi) {
  693. start_pending = true;
  694. end_pending = true;
  695. } else if (end_bdi >= curr_hw_dqpi || end_bdi <= eqp_bdi) {
  696. end_pending = true;
  697. }
  698. } else {
  699. if (start_bdi >= curr_hw_dqpi) {
  700. start_pending = true;
  701. end_pending = true;
  702. } else if (end_bdi >= curr_hw_dqpi) {
  703. end_pending = true;
  704. }
  705. }
  706. dev_dbg(bdc->dev,
  707. "start_pending:%d end_pending:%d speed:%d\n",
  708. start_pending, end_pending, bdc->gadget.speed);
  709. /* If both start till end are processes, we cannot deq req */
  710. if (!start_pending && !end_pending)
  711. return -EINVAL;
  712. /*
  713. * if ep_dequeue is called after disconnect then just return
  714. * success from here
  715. */
  716. if (bdc->gadget.speed == USB_SPEED_UNKNOWN)
  717. return 0;
  718. tbi = bdi_to_tbi(ep, req->bd_xfr.next_hwd_bdi);
  719. table = ep->bd_list.bd_table_array[tbi];
  720. next_bd_dma = table->dma +
  721. sizeof(struct bdc_bd)*(req->bd_xfr.next_hwd_bdi -
  722. tbi * ep->bd_list.num_bds_table);
  723. first_req = list_first_entry(&ep->queue, struct bdc_req,
  724. queue);
  725. if (req == first_req)
  726. first_remove = true;
  727. /*
  728. * Due to HW limitation we need to bypadd chain bd's and issue ep_bla,
  729. * incase if start is pending this is the first request in the list
  730. * then issue ep_bla instead of marking as chain bd
  731. */
  732. if (start_pending && !first_remove) {
  733. /*
  734. * Mark the start bd as Chain bd, and point the chain
  735. * bd to next_bd_dma
  736. */
  737. bd_start = bdi_to_bd(ep, start_bdi);
  738. bd_start->offset[0] = cpu_to_le32(lower_32_bits(next_bd_dma));
  739. bd_start->offset[1] = cpu_to_le32(upper_32_bits(next_bd_dma));
  740. bd_start->offset[2] = 0x0;
  741. bd_start->offset[3] = cpu_to_le32(MARK_CHAIN_BD);
  742. bdc_dbg_bd_list(bdc, ep);
  743. } else if (end_pending) {
  744. /*
  745. * The transfer is stopped in the middle, move the
  746. * HW deq pointer to next_bd_dma
  747. */
  748. ret = bdc_ep_bla(bdc, ep, next_bd_dma);
  749. if (ret) {
  750. dev_err(bdc->dev, "error in ep_bla:%d\n", ret);
  751. return ret;
  752. }
  753. }
  754. return 0;
  755. }
  756. /* Halt/Clear the ep based on value */
  757. static int ep_set_halt(struct bdc_ep *ep, u32 value)
  758. {
  759. struct bdc *bdc;
  760. int ret;
  761. bdc = ep->bdc;
  762. dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
  763. if (value) {
  764. dev_dbg(bdc->dev, "Halt\n");
  765. if (ep->ep_num == 1)
  766. bdc->ep0_state = WAIT_FOR_SETUP;
  767. ret = bdc_ep_set_stall(bdc, ep->ep_num);
  768. if (ret)
  769. dev_err(bdc->dev, "failed to set STALL on %s\n",
  770. ep->name);
  771. else
  772. ep->flags |= BDC_EP_STALL;
  773. } else {
  774. /* Clear */
  775. dev_dbg(bdc->dev, "Before Clear\n");
  776. ret = bdc_ep_clear_stall(bdc, ep->ep_num);
  777. if (ret)
  778. dev_err(bdc->dev, "failed to clear STALL on %s\n",
  779. ep->name);
  780. else
  781. ep->flags &= ~BDC_EP_STALL;
  782. dev_dbg(bdc->dev, "After Clear\n");
  783. }
  784. return ret;
  785. }
  786. /* Free all the ep */
  787. void bdc_free_ep(struct bdc *bdc)
  788. {
  789. struct bdc_ep *ep;
  790. u8 epnum;
  791. dev_dbg(bdc->dev, "%s\n", __func__);
  792. for (epnum = 1; epnum < bdc->num_eps; epnum++) {
  793. ep = bdc->bdc_ep_array[epnum];
  794. if (!ep)
  795. continue;
  796. if (ep->flags & BDC_EP_ENABLED)
  797. ep_bd_list_free(ep, ep->bd_list.num_tabs);
  798. /* ep0 is not in this gadget list */
  799. if (epnum != 1)
  800. list_del(&ep->usb_ep.ep_list);
  801. kfree(ep);
  802. }
  803. }
  804. /* USB2 spec, section 7.1.20 */
  805. static int bdc_set_test_mode(struct bdc *bdc)
  806. {
  807. u32 usb2_pm;
  808. usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
  809. usb2_pm &= ~BDC_PTC_MASK;
  810. dev_dbg(bdc->dev, "%s\n", __func__);
  811. switch (bdc->test_mode) {
  812. case TEST_J:
  813. case TEST_K:
  814. case TEST_SE0_NAK:
  815. case TEST_PACKET:
  816. case TEST_FORCE_EN:
  817. usb2_pm |= bdc->test_mode << 28;
  818. break;
  819. default:
  820. return -EINVAL;
  821. }
  822. dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
  823. bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
  824. return 0;
  825. }
  826. /*
  827. * Helper function to handle Transfer status report with status as either
  828. * success or short
  829. */
  830. static void handle_xsr_succ_status(struct bdc *bdc, struct bdc_ep *ep,
  831. struct bdc_sr *sreport)
  832. {
  833. int short_bdi, start_bdi, end_bdi, max_len_bds, chain_bds;
  834. struct bd_list *bd_list = &ep->bd_list;
  835. int actual_length, length_short;
  836. struct bd_transfer *bd_xfr;
  837. struct bdc_bd *short_bd;
  838. struct bdc_req *req;
  839. u64 deq_ptr_64 = 0;
  840. int status = 0;
  841. int sr_status;
  842. u32 tmp_32;
  843. dev_dbg(bdc->dev, "%s ep:%p\n", __func__, ep);
  844. bdc_dbg_srr(bdc, 0);
  845. /* do not process thie sr if ignore flag is set */
  846. if (ep->ignore_next_sr) {
  847. ep->ignore_next_sr = false;
  848. return;
  849. }
  850. if (unlikely(list_empty(&ep->queue))) {
  851. dev_warn(bdc->dev, "xfr srr with no BD's queued\n");
  852. return;
  853. }
  854. req = list_entry(ep->queue.next, struct bdc_req,
  855. queue);
  856. bd_xfr = &req->bd_xfr;
  857. sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
  858. /*
  859. * sr_status is short and this transfer has more than 1 bd then it needs
  860. * special handling, this is only applicable for bulk and ctrl
  861. */
  862. if (sr_status == XSF_SHORT && bd_xfr->num_bds > 1) {
  863. /*
  864. * This is multi bd xfr, lets see which bd
  865. * caused short transfer and how many bytes have been
  866. * transferred so far.
  867. */
  868. tmp_32 = le32_to_cpu(sreport->offset[0]);
  869. deq_ptr_64 = tmp_32;
  870. tmp_32 = le32_to_cpu(sreport->offset[1]);
  871. deq_ptr_64 |= ((u64)tmp_32 << 32);
  872. short_bdi = bd_add_to_bdi(ep, deq_ptr_64);
  873. if (unlikely(short_bdi < 0))
  874. dev_warn(bdc->dev, "bd doesn't exist?\n");
  875. start_bdi = bd_xfr->start_bdi;
  876. /*
  877. * We know the start_bdi and short_bdi, how many xfr
  878. * bds in between
  879. */
  880. if (start_bdi <= short_bdi) {
  881. max_len_bds = short_bdi - start_bdi;
  882. if (max_len_bds <= bd_list->num_bds_table) {
  883. if (!(bdi_to_tbi(ep, start_bdi) ==
  884. bdi_to_tbi(ep, short_bdi)))
  885. max_len_bds--;
  886. } else {
  887. chain_bds = max_len_bds/bd_list->num_bds_table;
  888. max_len_bds -= chain_bds;
  889. }
  890. } else {
  891. /* there is a wrap in the ring within a xfr */
  892. chain_bds = (bd_list->max_bdi - start_bdi)/
  893. bd_list->num_bds_table;
  894. chain_bds += short_bdi/bd_list->num_bds_table;
  895. max_len_bds = bd_list->max_bdi - start_bdi;
  896. max_len_bds += short_bdi;
  897. max_len_bds -= chain_bds;
  898. }
  899. /* max_len_bds is the number of full length bds */
  900. end_bdi = find_end_bdi(ep, bd_xfr->next_hwd_bdi);
  901. if (!(end_bdi == short_bdi))
  902. ep->ignore_next_sr = true;
  903. actual_length = max_len_bds * BD_MAX_BUFF_SIZE;
  904. short_bd = bdi_to_bd(ep, short_bdi);
  905. /* length queued */
  906. length_short = le32_to_cpu(short_bd->offset[2]) & 0x1FFFFF;
  907. /* actual length trensfered */
  908. length_short -= SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
  909. actual_length += length_short;
  910. req->usb_req.actual = actual_length;
  911. } else {
  912. req->usb_req.actual = req->usb_req.length -
  913. SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
  914. dev_dbg(bdc->dev,
  915. "len=%d actual=%d bd_xfr->next_hwd_bdi:%d\n",
  916. req->usb_req.length, req->usb_req.actual,
  917. bd_xfr->next_hwd_bdi);
  918. }
  919. /* Update the dequeue pointer */
  920. ep->bd_list.hwd_bdi = bd_xfr->next_hwd_bdi;
  921. if (req->usb_req.actual < req->usb_req.length) {
  922. dev_dbg(bdc->dev, "short xfr on %d\n", ep->ep_num);
  923. if (req->usb_req.short_not_ok)
  924. status = -EREMOTEIO;
  925. }
  926. bdc_req_complete(ep, bd_xfr->req, status);
  927. }
  928. /* EP0 setup related packet handlers */
  929. /*
  930. * Setup packet received, just store the packet and process on next DS or SS
  931. * started SR
  932. */
  933. void bdc_xsf_ep0_setup_recv(struct bdc *bdc, struct bdc_sr *sreport)
  934. {
  935. struct usb_ctrlrequest *setup_pkt;
  936. u32 len;
  937. dev_dbg(bdc->dev,
  938. "%s ep0_state:%s\n",
  939. __func__, ep0_state_string[bdc->ep0_state]);
  940. /* Store received setup packet */
  941. setup_pkt = &bdc->setup_pkt;
  942. memcpy(setup_pkt, &sreport->offset[0], sizeof(*setup_pkt));
  943. len = le16_to_cpu(setup_pkt->wLength);
  944. if (!len)
  945. bdc->ep0_state = WAIT_FOR_STATUS_START;
  946. else
  947. bdc->ep0_state = WAIT_FOR_DATA_START;
  948. dev_dbg(bdc->dev,
  949. "%s exit ep0_state:%s\n",
  950. __func__, ep0_state_string[bdc->ep0_state]);
  951. }
  952. /* Stall ep0 */
  953. static void ep0_stall(struct bdc *bdc)
  954. {
  955. struct bdc_ep *ep = bdc->bdc_ep_array[1];
  956. struct bdc_req *req;
  957. dev_dbg(bdc->dev, "%s\n", __func__);
  958. bdc->delayed_status = false;
  959. ep_set_halt(ep, 1);
  960. /* de-queue any pendig requests */
  961. while (!list_empty(&ep->queue)) {
  962. req = list_entry(ep->queue.next, struct bdc_req,
  963. queue);
  964. bdc_req_complete(ep, req, -ESHUTDOWN);
  965. }
  966. }
  967. /* SET_ADD handlers */
  968. static int ep0_set_address(struct bdc *bdc, struct usb_ctrlrequest *ctrl)
  969. {
  970. enum usb_device_state state = bdc->gadget.state;
  971. int ret = 0;
  972. u32 addr;
  973. addr = le16_to_cpu(ctrl->wValue);
  974. dev_dbg(bdc->dev,
  975. "%s addr:%d dev state:%d\n",
  976. __func__, addr, state);
  977. if (addr > 127)
  978. return -EINVAL;
  979. switch (state) {
  980. case USB_STATE_DEFAULT:
  981. case USB_STATE_ADDRESS:
  982. /* Issue Address device command */
  983. ret = bdc_address_device(bdc, addr);
  984. if (ret)
  985. return ret;
  986. if (addr)
  987. usb_gadget_set_state(&bdc->gadget, USB_STATE_ADDRESS);
  988. else
  989. usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT);
  990. bdc->dev_addr = addr;
  991. break;
  992. default:
  993. dev_warn(bdc->dev,
  994. "SET Address in wrong device state %d\n",
  995. state);
  996. ret = -EINVAL;
  997. }
  998. return ret;
  999. }
  1000. /* Handler for SET/CLEAR FEATURE requests for device */
  1001. static int ep0_handle_feature_dev(struct bdc *bdc, u16 wValue,
  1002. u16 wIndex, bool set)
  1003. {
  1004. enum usb_device_state state = bdc->gadget.state;
  1005. u32 usppms = 0;
  1006. dev_dbg(bdc->dev, "%s set:%d dev state:%d\n",
  1007. __func__, set, state);
  1008. switch (wValue) {
  1009. case USB_DEVICE_REMOTE_WAKEUP:
  1010. dev_dbg(bdc->dev, "USB_DEVICE_REMOTE_WAKEUP\n");
  1011. if (set)
  1012. bdc->devstatus |= REMOTE_WAKE_ENABLE;
  1013. else
  1014. bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
  1015. break;
  1016. case USB_DEVICE_TEST_MODE:
  1017. dev_dbg(bdc->dev, "USB_DEVICE_TEST_MODE\n");
  1018. if ((wIndex & 0xFF) ||
  1019. (bdc->gadget.speed != USB_SPEED_HIGH) || !set)
  1020. return -EINVAL;
  1021. bdc->test_mode = wIndex >> 8;
  1022. break;
  1023. case USB_DEVICE_U1_ENABLE:
  1024. dev_dbg(bdc->dev, "USB_DEVICE_U1_ENABLE\n");
  1025. if (bdc->gadget.speed != USB_SPEED_SUPER ||
  1026. state != USB_STATE_CONFIGURED)
  1027. return -EINVAL;
  1028. usppms = bdc_readl(bdc->regs, BDC_USPPMS);
  1029. if (set) {
  1030. /* clear previous u1t */
  1031. usppms &= ~BDC_U1T(BDC_U1T_MASK);
  1032. usppms |= BDC_U1T(U1_TIMEOUT);
  1033. usppms |= BDC_U1E | BDC_PORT_W1S;
  1034. bdc->devstatus |= (1 << USB_DEV_STAT_U1_ENABLED);
  1035. } else {
  1036. usppms &= ~BDC_U1E;
  1037. usppms |= BDC_PORT_W1S;
  1038. bdc->devstatus &= ~(1 << USB_DEV_STAT_U1_ENABLED);
  1039. }
  1040. bdc_writel(bdc->regs, BDC_USPPMS, usppms);
  1041. break;
  1042. case USB_DEVICE_U2_ENABLE:
  1043. dev_dbg(bdc->dev, "USB_DEVICE_U2_ENABLE\n");
  1044. if (bdc->gadget.speed != USB_SPEED_SUPER ||
  1045. state != USB_STATE_CONFIGURED)
  1046. return -EINVAL;
  1047. usppms = bdc_readl(bdc->regs, BDC_USPPMS);
  1048. if (set) {
  1049. usppms |= BDC_U2E;
  1050. usppms |= BDC_U2A;
  1051. bdc->devstatus |= (1 << USB_DEV_STAT_U2_ENABLED);
  1052. } else {
  1053. usppms &= ~BDC_U2E;
  1054. usppms &= ~BDC_U2A;
  1055. bdc->devstatus &= ~(1 << USB_DEV_STAT_U2_ENABLED);
  1056. }
  1057. bdc_writel(bdc->regs, BDC_USPPMS, usppms);
  1058. break;
  1059. case USB_DEVICE_LTM_ENABLE:
  1060. dev_dbg(bdc->dev, "USB_DEVICE_LTM_ENABLE?\n");
  1061. if (bdc->gadget.speed != USB_SPEED_SUPER ||
  1062. state != USB_STATE_CONFIGURED)
  1063. return -EINVAL;
  1064. break;
  1065. default:
  1066. dev_err(bdc->dev, "Unknown wValue:%d\n", wValue);
  1067. return -EOPNOTSUPP;
  1068. } /* USB_RECIP_DEVICE end */
  1069. return 0;
  1070. }
  1071. /* SET/CLEAR FEATURE handler */
  1072. static int ep0_handle_feature(struct bdc *bdc,
  1073. struct usb_ctrlrequest *setup_pkt, bool set)
  1074. {
  1075. enum usb_device_state state = bdc->gadget.state;
  1076. struct bdc_ep *ep;
  1077. u16 wValue;
  1078. u16 wIndex;
  1079. int epnum;
  1080. wValue = le16_to_cpu(setup_pkt->wValue);
  1081. wIndex = le16_to_cpu(setup_pkt->wIndex);
  1082. dev_dbg(bdc->dev,
  1083. "%s wValue=%d wIndex=%d devstate=%08x speed=%d set=%d",
  1084. __func__, wValue, wIndex, state,
  1085. bdc->gadget.speed, set);
  1086. switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
  1087. case USB_RECIP_DEVICE:
  1088. return ep0_handle_feature_dev(bdc, wValue, wIndex, set);
  1089. case USB_RECIP_INTERFACE:
  1090. dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
  1091. /* USB3 spec, sec 9.4.9 */
  1092. if (wValue != USB_INTRF_FUNC_SUSPEND)
  1093. return -EINVAL;
  1094. /* USB3 spec, Table 9-8 */
  1095. if (set) {
  1096. if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) {
  1097. dev_dbg(bdc->dev, "SET REMOTE_WAKEUP\n");
  1098. bdc->devstatus |= REMOTE_WAKE_ENABLE;
  1099. } else {
  1100. dev_dbg(bdc->dev, "CLEAR REMOTE_WAKEUP\n");
  1101. bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
  1102. }
  1103. }
  1104. break;
  1105. case USB_RECIP_ENDPOINT:
  1106. dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
  1107. if (wValue != USB_ENDPOINT_HALT)
  1108. return -EINVAL;
  1109. epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1110. if (epnum) {
  1111. if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1112. epnum = epnum * 2 + 1;
  1113. else
  1114. epnum *= 2;
  1115. } else {
  1116. epnum = 1; /*EP0*/
  1117. }
  1118. /*
  1119. * If CLEAR_FEATURE on ep0 then don't do anything as the stall
  1120. * condition on ep0 has already been cleared when SETUP packet
  1121. * was received.
  1122. */
  1123. if (epnum == 1 && !set) {
  1124. dev_dbg(bdc->dev, "ep0 stall already cleared\n");
  1125. return 0;
  1126. }
  1127. dev_dbg(bdc->dev, "epnum=%d\n", epnum);
  1128. ep = bdc->bdc_ep_array[epnum];
  1129. if (!ep)
  1130. return -EINVAL;
  1131. return ep_set_halt(ep, set);
  1132. default:
  1133. dev_err(bdc->dev, "Unknown recipient\n");
  1134. return -EINVAL;
  1135. }
  1136. return 0;
  1137. }
  1138. /* GET_STATUS request handler */
  1139. static int ep0_handle_status(struct bdc *bdc,
  1140. struct usb_ctrlrequest *setup_pkt)
  1141. {
  1142. enum usb_device_state state = bdc->gadget.state;
  1143. struct bdc_ep *ep;
  1144. u16 usb_status = 0;
  1145. u32 epnum;
  1146. u16 wIndex;
  1147. /* USB2.0 spec sec 9.4.5 */
  1148. if (state == USB_STATE_DEFAULT)
  1149. return -EINVAL;
  1150. wIndex = le16_to_cpu(setup_pkt->wIndex);
  1151. dev_dbg(bdc->dev, "%s\n", __func__);
  1152. usb_status = bdc->devstatus;
  1153. switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
  1154. case USB_RECIP_DEVICE:
  1155. dev_dbg(bdc->dev,
  1156. "USB_RECIP_DEVICE devstatus:%08x\n",
  1157. bdc->devstatus);
  1158. /* USB3 spec, sec 9.4.5 */
  1159. if (bdc->gadget.speed == USB_SPEED_SUPER)
  1160. usb_status &= ~REMOTE_WAKE_ENABLE;
  1161. break;
  1162. case USB_RECIP_INTERFACE:
  1163. dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
  1164. if (bdc->gadget.speed == USB_SPEED_SUPER) {
  1165. /*
  1166. * This should come from func for Func remote wkup
  1167. * usb_status |=1;
  1168. */
  1169. if (bdc->devstatus & REMOTE_WAKE_ENABLE)
  1170. usb_status |= REMOTE_WAKE_ENABLE;
  1171. } else {
  1172. usb_status = 0;
  1173. }
  1174. break;
  1175. case USB_RECIP_ENDPOINT:
  1176. dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
  1177. epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1178. if (epnum) {
  1179. if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1180. epnum = epnum*2 + 1;
  1181. else
  1182. epnum *= 2;
  1183. } else {
  1184. epnum = 1; /* EP0 */
  1185. }
  1186. ep = bdc->bdc_ep_array[epnum];
  1187. if (!ep) {
  1188. dev_err(bdc->dev, "ISSUE, GET_STATUS for invalid EP ?");
  1189. return -EINVAL;
  1190. }
  1191. if (ep->flags & BDC_EP_STALL)
  1192. usb_status |= 1 << USB_ENDPOINT_HALT;
  1193. break;
  1194. default:
  1195. dev_err(bdc->dev, "Unknown recipient for get_status\n");
  1196. return -EINVAL;
  1197. }
  1198. /* prepare a data stage for GET_STATUS */
  1199. dev_dbg(bdc->dev, "usb_status=%08x\n", usb_status);
  1200. *(__le16 *)bdc->ep0_response_buff = cpu_to_le16(usb_status);
  1201. bdc->ep0_req.usb_req.length = 2;
  1202. bdc->ep0_req.usb_req.buf = &bdc->ep0_response_buff;
  1203. ep0_queue_data_stage(bdc);
  1204. return 0;
  1205. }
  1206. static void ep0_set_sel_cmpl(struct usb_ep *_ep, struct usb_request *_req)
  1207. {
  1208. /* ep0_set_sel_cmpl */
  1209. }
  1210. /* Queue data stage to handle 6 byte SET_SEL request */
  1211. static int ep0_set_sel(struct bdc *bdc,
  1212. struct usb_ctrlrequest *setup_pkt)
  1213. {
  1214. struct bdc_ep *ep;
  1215. u16 wLength;
  1216. u16 wValue;
  1217. dev_dbg(bdc->dev, "%s\n", __func__);
  1218. wValue = le16_to_cpu(setup_pkt->wValue);
  1219. wLength = le16_to_cpu(setup_pkt->wLength);
  1220. if (unlikely(wLength != 6)) {
  1221. dev_err(bdc->dev, "%s Wrong wLength:%d\n", __func__, wLength);
  1222. return -EINVAL;
  1223. }
  1224. ep = bdc->bdc_ep_array[1];
  1225. bdc->ep0_req.ep = ep;
  1226. bdc->ep0_req.usb_req.length = 6;
  1227. bdc->ep0_req.usb_req.buf = bdc->ep0_response_buff;
  1228. bdc->ep0_req.usb_req.complete = ep0_set_sel_cmpl;
  1229. ep0_queue_data_stage(bdc);
  1230. return 0;
  1231. }
  1232. /*
  1233. * Queue a 0 byte bd only if wLength is more than the length and and length is
  1234. * a multiple of MaxPacket then queue 0 byte BD
  1235. */
  1236. static int ep0_queue_zlp(struct bdc *bdc)
  1237. {
  1238. int ret;
  1239. dev_dbg(bdc->dev, "%s\n", __func__);
  1240. bdc->ep0_req.ep = bdc->bdc_ep_array[1];
  1241. bdc->ep0_req.usb_req.length = 0;
  1242. bdc->ep0_req.usb_req.complete = NULL;
  1243. bdc->ep0_state = WAIT_FOR_DATA_START;
  1244. ret = bdc_queue_xfr(bdc, &bdc->ep0_req);
  1245. if (ret) {
  1246. dev_err(bdc->dev, "err queueing zlp :%d\n", ret);
  1247. return ret;
  1248. }
  1249. bdc->ep0_state = WAIT_FOR_DATA_XMIT;
  1250. return 0;
  1251. }
  1252. /* Control request handler */
  1253. static int handle_control_request(struct bdc *bdc)
  1254. {
  1255. enum usb_device_state state = bdc->gadget.state;
  1256. struct usb_ctrlrequest *setup_pkt;
  1257. int delegate_setup = 0;
  1258. int ret = 0;
  1259. int config = 0;
  1260. setup_pkt = &bdc->setup_pkt;
  1261. dev_dbg(bdc->dev, "%s\n", __func__);
  1262. if ((setup_pkt->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1263. switch (setup_pkt->bRequest) {
  1264. case USB_REQ_SET_ADDRESS:
  1265. dev_dbg(bdc->dev, "USB_REQ_SET_ADDRESS\n");
  1266. ret = ep0_set_address(bdc, setup_pkt);
  1267. bdc->devstatus &= DEVSTATUS_CLEAR;
  1268. break;
  1269. case USB_REQ_SET_CONFIGURATION:
  1270. dev_dbg(bdc->dev, "USB_REQ_SET_CONFIGURATION\n");
  1271. if (state == USB_STATE_ADDRESS) {
  1272. usb_gadget_set_state(&bdc->gadget,
  1273. USB_STATE_CONFIGURED);
  1274. } else if (state == USB_STATE_CONFIGURED) {
  1275. /*
  1276. * USB2 spec sec 9.4.7, if wValue is 0 then dev
  1277. * is moved to addressed state
  1278. */
  1279. config = le16_to_cpu(setup_pkt->wValue);
  1280. if (!config)
  1281. usb_gadget_set_state(
  1282. &bdc->gadget,
  1283. USB_STATE_ADDRESS);
  1284. }
  1285. delegate_setup = 1;
  1286. break;
  1287. case USB_REQ_SET_FEATURE:
  1288. dev_dbg(bdc->dev, "USB_REQ_SET_FEATURE\n");
  1289. ret = ep0_handle_feature(bdc, setup_pkt, 1);
  1290. break;
  1291. case USB_REQ_CLEAR_FEATURE:
  1292. dev_dbg(bdc->dev, "USB_REQ_CLEAR_FEATURE\n");
  1293. ret = ep0_handle_feature(bdc, setup_pkt, 0);
  1294. break;
  1295. case USB_REQ_GET_STATUS:
  1296. dev_dbg(bdc->dev, "USB_REQ_GET_STATUS\n");
  1297. ret = ep0_handle_status(bdc, setup_pkt);
  1298. break;
  1299. case USB_REQ_SET_SEL:
  1300. dev_dbg(bdc->dev, "USB_REQ_SET_SEL\n");
  1301. ret = ep0_set_sel(bdc, setup_pkt);
  1302. break;
  1303. case USB_REQ_SET_ISOCH_DELAY:
  1304. dev_warn(bdc->dev,
  1305. "USB_REQ_SET_ISOCH_DELAY not handled\n");
  1306. ret = 0;
  1307. break;
  1308. default:
  1309. delegate_setup = 1;
  1310. }
  1311. } else {
  1312. delegate_setup = 1;
  1313. }
  1314. if (delegate_setup) {
  1315. spin_unlock(&bdc->lock);
  1316. ret = bdc->gadget_driver->setup(&bdc->gadget, setup_pkt);
  1317. spin_lock(&bdc->lock);
  1318. }
  1319. return ret;
  1320. }
  1321. /* EP0: Data stage started */
  1322. void bdc_xsf_ep0_data_start(struct bdc *bdc, struct bdc_sr *sreport)
  1323. {
  1324. struct bdc_ep *ep;
  1325. int ret = 0;
  1326. dev_dbg(bdc->dev, "%s\n", __func__);
  1327. ep = bdc->bdc_ep_array[1];
  1328. /* If ep0 was stalled, the clear it first */
  1329. if (ep->flags & BDC_EP_STALL) {
  1330. ret = ep_set_halt(ep, 0);
  1331. if (ret)
  1332. goto err;
  1333. }
  1334. if (bdc->ep0_state != WAIT_FOR_DATA_START)
  1335. dev_warn(bdc->dev,
  1336. "Data stage not expected ep0_state:%s\n",
  1337. ep0_state_string[bdc->ep0_state]);
  1338. ret = handle_control_request(bdc);
  1339. if (ret == USB_GADGET_DELAYED_STATUS) {
  1340. /*
  1341. * The ep0 state will remain WAIT_FOR_DATA_START till
  1342. * we received ep_queue on ep0
  1343. */
  1344. bdc->delayed_status = true;
  1345. return;
  1346. }
  1347. if (!ret) {
  1348. bdc->ep0_state = WAIT_FOR_DATA_XMIT;
  1349. dev_dbg(bdc->dev,
  1350. "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
  1351. return;
  1352. }
  1353. err:
  1354. ep0_stall(bdc);
  1355. }
  1356. /* EP0: status stage started */
  1357. void bdc_xsf_ep0_status_start(struct bdc *bdc, struct bdc_sr *sreport)
  1358. {
  1359. struct usb_ctrlrequest *setup_pkt;
  1360. struct bdc_ep *ep;
  1361. int ret = 0;
  1362. dev_dbg(bdc->dev,
  1363. "%s ep0_state:%s",
  1364. __func__, ep0_state_string[bdc->ep0_state]);
  1365. ep = bdc->bdc_ep_array[1];
  1366. /* check if ZLP was queued? */
  1367. if (bdc->zlp_needed)
  1368. bdc->zlp_needed = false;
  1369. if (ep->flags & BDC_EP_STALL) {
  1370. ret = ep_set_halt(ep, 0);
  1371. if (ret)
  1372. goto err;
  1373. }
  1374. if ((bdc->ep0_state != WAIT_FOR_STATUS_START) &&
  1375. (bdc->ep0_state != WAIT_FOR_DATA_XMIT))
  1376. dev_err(bdc->dev,
  1377. "Status stage recv but ep0_state:%s\n",
  1378. ep0_state_string[bdc->ep0_state]);
  1379. /* check if data stage is in progress ? */
  1380. if (bdc->ep0_state == WAIT_FOR_DATA_XMIT) {
  1381. bdc->ep0_state = STATUS_PENDING;
  1382. /* Status stage will be queued upon Data stage transmit event */
  1383. dev_dbg(bdc->dev,
  1384. "status started but data not transmitted yet\n");
  1385. return;
  1386. }
  1387. setup_pkt = &bdc->setup_pkt;
  1388. /*
  1389. * 2 stage setup then only process the setup, for 3 stage setup the date
  1390. * stage is already handled
  1391. */
  1392. if (!le16_to_cpu(setup_pkt->wLength)) {
  1393. ret = handle_control_request(bdc);
  1394. if (ret == USB_GADGET_DELAYED_STATUS) {
  1395. bdc->delayed_status = true;
  1396. /* ep0_state will remain WAIT_FOR_STATUS_START */
  1397. return;
  1398. }
  1399. }
  1400. if (!ret) {
  1401. /* Queue a status stage BD */
  1402. ep0_queue_status_stage(bdc);
  1403. bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
  1404. dev_dbg(bdc->dev,
  1405. "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
  1406. return;
  1407. }
  1408. err:
  1409. ep0_stall(bdc);
  1410. }
  1411. /* Helper function to update ep0 upon SR with xsf_succ or xsf_short */
  1412. static void ep0_xsf_complete(struct bdc *bdc, struct bdc_sr *sreport)
  1413. {
  1414. dev_dbg(bdc->dev, "%s\n", __func__);
  1415. switch (bdc->ep0_state) {
  1416. case WAIT_FOR_DATA_XMIT:
  1417. bdc->ep0_state = WAIT_FOR_STATUS_START;
  1418. break;
  1419. case WAIT_FOR_STATUS_XMIT:
  1420. bdc->ep0_state = WAIT_FOR_SETUP;
  1421. if (bdc->test_mode) {
  1422. int ret;
  1423. dev_dbg(bdc->dev, "test_mode:%d\n", bdc->test_mode);
  1424. ret = bdc_set_test_mode(bdc);
  1425. if (ret < 0) {
  1426. dev_err(bdc->dev, "Err in setting Test mode\n");
  1427. return;
  1428. }
  1429. bdc->test_mode = 0;
  1430. }
  1431. break;
  1432. case STATUS_PENDING:
  1433. bdc_xsf_ep0_status_start(bdc, sreport);
  1434. break;
  1435. default:
  1436. dev_err(bdc->dev,
  1437. "Unknown ep0_state:%s\n",
  1438. ep0_state_string[bdc->ep0_state]);
  1439. }
  1440. }
  1441. /* xfr completion status report handler */
  1442. void bdc_sr_xsf(struct bdc *bdc, struct bdc_sr *sreport)
  1443. {
  1444. struct bdc_ep *ep;
  1445. u32 sr_status;
  1446. u8 ep_num;
  1447. ep_num = (le32_to_cpu(sreport->offset[3])>>4) & 0x1f;
  1448. ep = bdc->bdc_ep_array[ep_num];
  1449. if (!ep || !(ep->flags & BDC_EP_ENABLED)) {
  1450. dev_err(bdc->dev, "xsf for ep not enabled\n");
  1451. return;
  1452. }
  1453. /*
  1454. * check if this transfer is after link went from U3->U0 due
  1455. * to remote wakeup
  1456. */
  1457. if (bdc->devstatus & FUNC_WAKE_ISSUED) {
  1458. bdc->devstatus &= ~(FUNC_WAKE_ISSUED);
  1459. dev_dbg(bdc->dev, "%s clearing FUNC_WAKE_ISSUED flag\n",
  1460. __func__);
  1461. }
  1462. sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
  1463. dev_dbg_ratelimited(bdc->dev, "%s sr_status=%d ep:%s\n",
  1464. __func__, sr_status, ep->name);
  1465. switch (sr_status) {
  1466. case XSF_SUCC:
  1467. case XSF_SHORT:
  1468. handle_xsr_succ_status(bdc, ep, sreport);
  1469. if (ep_num == 1)
  1470. ep0_xsf_complete(bdc, sreport);
  1471. break;
  1472. case XSF_SETUP_RECV:
  1473. case XSF_DATA_START:
  1474. case XSF_STATUS_START:
  1475. if (ep_num != 1) {
  1476. dev_err(bdc->dev,
  1477. "ep0 related packets on non ep0 endpoint");
  1478. return;
  1479. }
  1480. bdc->sr_xsf_ep0[sr_status - XSF_SETUP_RECV](bdc, sreport);
  1481. break;
  1482. case XSF_BABB:
  1483. if (ep_num == 1) {
  1484. dev_dbg(bdc->dev, "Babble on ep0 zlp_need:%d\n",
  1485. bdc->zlp_needed);
  1486. /*
  1487. * If the last completed transfer had wLength >Data Len,
  1488. * and Len is multiple of MaxPacket,then queue ZLP
  1489. */
  1490. if (bdc->zlp_needed) {
  1491. /* queue 0 length bd */
  1492. ep0_queue_zlp(bdc);
  1493. return;
  1494. }
  1495. }
  1496. dev_warn(bdc->dev, "Babble on ep not handled\n");
  1497. break;
  1498. default:
  1499. dev_warn(bdc->dev, "sr status not handled:%x\n", sr_status);
  1500. break;
  1501. }
  1502. }
  1503. static int bdc_gadget_ep_queue(struct usb_ep *_ep,
  1504. struct usb_request *_req, gfp_t gfp_flags)
  1505. {
  1506. struct bdc_req *req;
  1507. unsigned long flags;
  1508. struct bdc_ep *ep;
  1509. struct bdc *bdc;
  1510. int ret;
  1511. if (!_ep || !_ep->desc)
  1512. return -ESHUTDOWN;
  1513. if (!_req || !_req->complete || !_req->buf)
  1514. return -EINVAL;
  1515. ep = to_bdc_ep(_ep);
  1516. req = to_bdc_req(_req);
  1517. bdc = ep->bdc;
  1518. dev_dbg(bdc->dev, "%s ep:%p req:%p\n", __func__, ep, req);
  1519. dev_dbg(bdc->dev, "queuing request %p to %s length %d zero:%d\n",
  1520. _req, ep->name, _req->length, _req->zero);
  1521. if (!ep->usb_ep.desc) {
  1522. dev_warn(bdc->dev,
  1523. "trying to queue req %p to disabled %s\n",
  1524. _req, ep->name);
  1525. return -ESHUTDOWN;
  1526. }
  1527. if (_req->length > MAX_XFR_LEN) {
  1528. dev_warn(bdc->dev,
  1529. "req length > supported MAX:%d requested:%d\n",
  1530. MAX_XFR_LEN, _req->length);
  1531. return -EOPNOTSUPP;
  1532. }
  1533. spin_lock_irqsave(&bdc->lock, flags);
  1534. if (ep == bdc->bdc_ep_array[1])
  1535. ret = ep0_queue(ep, req);
  1536. else
  1537. ret = ep_queue(ep, req);
  1538. spin_unlock_irqrestore(&bdc->lock, flags);
  1539. return ret;
  1540. }
  1541. static int bdc_gadget_ep_dequeue(struct usb_ep *_ep,
  1542. struct usb_request *_req)
  1543. {
  1544. struct bdc_req *req;
  1545. unsigned long flags;
  1546. struct bdc_ep *ep;
  1547. struct bdc *bdc;
  1548. int ret;
  1549. if (!_ep || !_req)
  1550. return -EINVAL;
  1551. ep = to_bdc_ep(_ep);
  1552. req = to_bdc_req(_req);
  1553. bdc = ep->bdc;
  1554. dev_dbg(bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
  1555. bdc_dbg_bd_list(bdc, ep);
  1556. spin_lock_irqsave(&bdc->lock, flags);
  1557. /* make sure it's still queued on this endpoint */
  1558. list_for_each_entry(req, &ep->queue, queue) {
  1559. if (&req->usb_req == _req)
  1560. break;
  1561. }
  1562. if (&req->usb_req != _req) {
  1563. spin_unlock_irqrestore(&bdc->lock, flags);
  1564. dev_err(bdc->dev, "usb_req !=req n");
  1565. return -EINVAL;
  1566. }
  1567. ret = ep_dequeue(ep, req);
  1568. if (ret) {
  1569. ret = -EOPNOTSUPP;
  1570. goto err;
  1571. }
  1572. bdc_req_complete(ep, req, -ECONNRESET);
  1573. err:
  1574. bdc_dbg_bd_list(bdc, ep);
  1575. spin_unlock_irqrestore(&bdc->lock, flags);
  1576. return ret;
  1577. }
  1578. static int bdc_gadget_ep_set_halt(struct usb_ep *_ep, int value)
  1579. {
  1580. unsigned long flags;
  1581. struct bdc_ep *ep;
  1582. struct bdc *bdc;
  1583. int ret;
  1584. ep = to_bdc_ep(_ep);
  1585. bdc = ep->bdc;
  1586. dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
  1587. spin_lock_irqsave(&bdc->lock, flags);
  1588. if (usb_endpoint_xfer_isoc(ep->usb_ep.desc))
  1589. ret = -EINVAL;
  1590. else if (!list_empty(&ep->queue))
  1591. ret = -EAGAIN;
  1592. else
  1593. ret = ep_set_halt(ep, value);
  1594. spin_unlock_irqrestore(&bdc->lock, flags);
  1595. return ret;
  1596. }
  1597. static struct usb_request *bdc_gadget_alloc_request(struct usb_ep *_ep,
  1598. gfp_t gfp_flags)
  1599. {
  1600. struct bdc_req *req;
  1601. struct bdc_ep *ep;
  1602. req = kzalloc(sizeof(*req), gfp_flags);
  1603. if (!req)
  1604. return NULL;
  1605. ep = to_bdc_ep(_ep);
  1606. req->ep = ep;
  1607. req->epnum = ep->ep_num;
  1608. req->usb_req.dma = DMA_ADDR_INVALID;
  1609. dev_dbg(ep->bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
  1610. return &req->usb_req;
  1611. }
  1612. static void bdc_gadget_free_request(struct usb_ep *_ep,
  1613. struct usb_request *_req)
  1614. {
  1615. struct bdc_req *req;
  1616. req = to_bdc_req(_req);
  1617. kfree(req);
  1618. }
  1619. /* endpoint operations */
  1620. /* configure endpoint and also allocate resources */
  1621. static int bdc_gadget_ep_enable(struct usb_ep *_ep,
  1622. const struct usb_endpoint_descriptor *desc)
  1623. {
  1624. unsigned long flags;
  1625. struct bdc_ep *ep;
  1626. struct bdc *bdc;
  1627. int ret;
  1628. if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  1629. pr_debug("bdc_gadget_ep_enable invalid parameters\n");
  1630. return -EINVAL;
  1631. }
  1632. if (!desc->wMaxPacketSize) {
  1633. pr_debug("bdc_gadget_ep_enable missing wMaxPacketSize\n");
  1634. return -EINVAL;
  1635. }
  1636. ep = to_bdc_ep(_ep);
  1637. bdc = ep->bdc;
  1638. /* Sanity check, upper layer will not send enable for ep0 */
  1639. if (ep == bdc->bdc_ep_array[1])
  1640. return -EINVAL;
  1641. if (!bdc->gadget_driver
  1642. || bdc->gadget.speed == USB_SPEED_UNKNOWN) {
  1643. return -ESHUTDOWN;
  1644. }
  1645. dev_dbg(bdc->dev, "%s Enabling %s\n", __func__, ep->name);
  1646. spin_lock_irqsave(&bdc->lock, flags);
  1647. ep->desc = desc;
  1648. ep->comp_desc = _ep->comp_desc;
  1649. ret = bdc_ep_enable(ep);
  1650. spin_unlock_irqrestore(&bdc->lock, flags);
  1651. return ret;
  1652. }
  1653. static int bdc_gadget_ep_disable(struct usb_ep *_ep)
  1654. {
  1655. unsigned long flags;
  1656. struct bdc_ep *ep;
  1657. struct bdc *bdc;
  1658. int ret;
  1659. if (!_ep) {
  1660. pr_debug("bdc: invalid parameters\n");
  1661. return -EINVAL;
  1662. }
  1663. ep = to_bdc_ep(_ep);
  1664. bdc = ep->bdc;
  1665. /* Upper layer will not call this for ep0, but do a sanity check */
  1666. if (ep == bdc->bdc_ep_array[1]) {
  1667. dev_warn(bdc->dev, "%s called for ep0\n", __func__);
  1668. return -EINVAL;
  1669. }
  1670. dev_dbg(bdc->dev,
  1671. "%s() ep:%s ep->flags:%08x\n",
  1672. __func__, ep->name, ep->flags);
  1673. if (!(ep->flags & BDC_EP_ENABLED)) {
  1674. dev_warn(bdc->dev, "%s is already disabled\n", ep->name);
  1675. return 0;
  1676. }
  1677. spin_lock_irqsave(&bdc->lock, flags);
  1678. ret = bdc_ep_disable(ep);
  1679. spin_unlock_irqrestore(&bdc->lock, flags);
  1680. return ret;
  1681. }
  1682. static const struct usb_ep_ops bdc_gadget_ep_ops = {
  1683. .enable = bdc_gadget_ep_enable,
  1684. .disable = bdc_gadget_ep_disable,
  1685. .alloc_request = bdc_gadget_alloc_request,
  1686. .free_request = bdc_gadget_free_request,
  1687. .queue = bdc_gadget_ep_queue,
  1688. .dequeue = bdc_gadget_ep_dequeue,
  1689. .set_halt = bdc_gadget_ep_set_halt
  1690. };
  1691. /* dir = 1 is IN */
  1692. static int init_ep(struct bdc *bdc, u32 epnum, u32 dir)
  1693. {
  1694. struct bdc_ep *ep;
  1695. dev_dbg(bdc->dev, "%s epnum=%d dir=%d\n", __func__, epnum, dir);
  1696. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1697. if (!ep)
  1698. return -ENOMEM;
  1699. ep->bdc = bdc;
  1700. ep->dir = dir;
  1701. if (dir)
  1702. ep->usb_ep.caps.dir_in = true;
  1703. else
  1704. ep->usb_ep.caps.dir_out = true;
  1705. /* ep->ep_num is the index inside bdc_ep */
  1706. if (epnum == 1) {
  1707. ep->ep_num = 1;
  1708. bdc->bdc_ep_array[ep->ep_num] = ep;
  1709. snprintf(ep->name, sizeof(ep->name), "ep%d", epnum - 1);
  1710. usb_ep_set_maxpacket_limit(&ep->usb_ep, EP0_MAX_PKT_SIZE);
  1711. ep->usb_ep.caps.type_control = true;
  1712. ep->comp_desc = NULL;
  1713. bdc->gadget.ep0 = &ep->usb_ep;
  1714. } else {
  1715. if (dir)
  1716. ep->ep_num = epnum * 2 - 1;
  1717. else
  1718. ep->ep_num = epnum * 2 - 2;
  1719. bdc->bdc_ep_array[ep->ep_num] = ep;
  1720. snprintf(ep->name, sizeof(ep->name), "ep%d%s", epnum - 1,
  1721. dir & 1 ? "in" : "out");
  1722. usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
  1723. ep->usb_ep.caps.type_iso = true;
  1724. ep->usb_ep.caps.type_bulk = true;
  1725. ep->usb_ep.caps.type_int = true;
  1726. ep->usb_ep.max_streams = 0;
  1727. list_add_tail(&ep->usb_ep.ep_list, &bdc->gadget.ep_list);
  1728. }
  1729. ep->usb_ep.ops = &bdc_gadget_ep_ops;
  1730. ep->usb_ep.name = ep->name;
  1731. ep->flags = 0;
  1732. ep->ignore_next_sr = false;
  1733. dev_dbg(bdc->dev, "ep=%p ep->usb_ep.name=%s epnum=%d ep->epnum=%d\n",
  1734. ep, ep->usb_ep.name, epnum, ep->ep_num);
  1735. INIT_LIST_HEAD(&ep->queue);
  1736. return 0;
  1737. }
  1738. /* Init all ep */
  1739. int bdc_init_ep(struct bdc *bdc)
  1740. {
  1741. u8 epnum;
  1742. int ret;
  1743. dev_dbg(bdc->dev, "%s()\n", __func__);
  1744. INIT_LIST_HEAD(&bdc->gadget.ep_list);
  1745. /* init ep0 */
  1746. ret = init_ep(bdc, 1, 0);
  1747. if (ret) {
  1748. dev_err(bdc->dev, "init ep ep0 fail %d\n", ret);
  1749. return ret;
  1750. }
  1751. for (epnum = 2; epnum <= bdc->num_eps / 2; epnum++) {
  1752. /* OUT */
  1753. ret = init_ep(bdc, epnum, 0);
  1754. if (ret) {
  1755. dev_err(bdc->dev,
  1756. "init ep failed for:%d error: %d\n",
  1757. epnum, ret);
  1758. return ret;
  1759. }
  1760. /* IN */
  1761. ret = init_ep(bdc, epnum, 1);
  1762. if (ret) {
  1763. dev_err(bdc->dev,
  1764. "init ep failed for:%d error: %d\n",
  1765. epnum, ret);
  1766. return ret;
  1767. }
  1768. }
  1769. return 0;
  1770. }