dwc_otg_cfi.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  1. /* ==========================================================================
  2. * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  3. * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  4. * otherwise expressly agreed to in writing between Synopsys and you.
  5. *
  6. * The Software IS NOT an item of Licensed Software or Licensed Product under
  7. * any End User Software License Agreement or Agreement for Licensed Product
  8. * with Synopsys or any supplement thereto. You are permitted to use and
  9. * redistribute this Software in source and binary forms, with or without
  10. * modification, provided that redistributions of source code must retain this
  11. * notice. You may not view, use, disclose, copy or distribute this file or
  12. * any information contained herein except pursuant to this license grant from
  13. * Synopsys. If you do not agree with this notice, including the disclaimer
  14. * below, then you are not authorized to use the Software.
  15. *
  16. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. * DAMAGE.
  27. * ========================================================================== */
  28. /** @file
  29. *
  30. * This file contains the most of the CFI(Core Feature Interface)
  31. * implementation for the OTG.
  32. */
  33. #ifdef DWC_UTE_CFI
  34. #include "dwc_otg_pcd.h"
  35. #include "dwc_otg_cfi.h"
  36. /** This definition should actually migrate to the Portability Library */
  37. #define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  38. extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  39. static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  40. static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  41. struct dwc_otg_pcd *pcd,
  42. struct cfi_usb_ctrlrequest *ctrl_req);
  43. static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  44. static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  45. struct cfi_usb_ctrlrequest *req);
  46. static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  47. struct cfi_usb_ctrlrequest *req);
  48. static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  49. struct cfi_usb_ctrlrequest *req);
  50. static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  51. struct cfi_usb_ctrlrequest *req);
  52. static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  53. static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  54. static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  55. static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  56. static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  57. /** This is the header of the all features descriptor */
  58. static cfi_all_features_header_t all_props_desc_header = {
  59. .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  60. .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  61. .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  62. };
  63. /** This is an array of statically allocated feature descriptors */
  64. static cfi_feature_desc_header_t prop_descs[] = {
  65. /* FT_ID_DMA_MODE */
  66. {
  67. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  68. .bmAttributes = CFI_FEATURE_ATTR_RW,
  69. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  70. },
  71. /* FT_ID_DMA_BUFFER_SETUP */
  72. {
  73. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  74. .bmAttributes = CFI_FEATURE_ATTR_RW,
  75. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  76. },
  77. /* FT_ID_DMA_BUFF_ALIGN */
  78. {
  79. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  80. .bmAttributes = CFI_FEATURE_ATTR_RW,
  81. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  82. },
  83. /* FT_ID_DMA_CONCAT_SETUP */
  84. {
  85. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  86. .bmAttributes = CFI_FEATURE_ATTR_RW,
  87. //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  88. },
  89. /* FT_ID_DMA_CIRCULAR */
  90. {
  91. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  92. .bmAttributes = CFI_FEATURE_ATTR_RW,
  93. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  94. },
  95. /* FT_ID_THRESHOLD_SETUP */
  96. {
  97. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  98. .bmAttributes = CFI_FEATURE_ATTR_RW,
  99. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  100. },
  101. /* FT_ID_DFIFO_DEPTH */
  102. {
  103. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  104. .bmAttributes = CFI_FEATURE_ATTR_RO,
  105. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  106. },
  107. /* FT_ID_TX_FIFO_DEPTH */
  108. {
  109. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  110. .bmAttributes = CFI_FEATURE_ATTR_RW,
  111. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  112. },
  113. /* FT_ID_RX_FIFO_DEPTH */
  114. {
  115. .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  116. .bmAttributes = CFI_FEATURE_ATTR_RW,
  117. .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  118. }
  119. };
  120. /** The table of feature names */
  121. cfi_string_t prop_name_table[] = {
  122. {FT_ID_DMA_MODE, "dma_mode"},
  123. {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  124. {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  125. {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  126. {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  127. {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  128. {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  129. {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  130. {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  131. {}
  132. };
  133. /************************************************************************/
  134. /**
  135. * Returns the name of the feature by its ID
  136. * or NULL if no featute ID matches.
  137. *
  138. */
  139. const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  140. {
  141. cfi_string_t *pstr;
  142. *len = 0;
  143. for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  144. if (pstr->id == prop_id) {
  145. *len = DWC_STRLEN(pstr->s);
  146. return pstr->s;
  147. }
  148. }
  149. return NULL;
  150. }
  151. /**
  152. * This function handles all CFI specific control requests.
  153. *
  154. * Return a negative value to stall the DCE.
  155. */
  156. int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  157. {
  158. int retval = 0;
  159. dwc_otg_pcd_ep_t *ep = NULL;
  160. cfiobject_t *cfi = pcd->cfi;
  161. struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  162. uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  163. uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  164. uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  165. uint32_t regaddr = 0;
  166. uint32_t regval = 0;
  167. /* Save this Control Request in the CFI object.
  168. * The data field will be assigned in the data stage completion CB function.
  169. */
  170. cfi->ctrl_req = *ctrl;
  171. cfi->ctrl_req.data = NULL;
  172. cfi->need_gadget_att = 0;
  173. cfi->need_status_in_complete = 0;
  174. switch (ctrl->bRequest) {
  175. case VEN_CORE_GET_FEATURES:
  176. retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  177. if (retval >= 0) {
  178. //dump_msg(cfi->buf_in.buf, retval);
  179. ep = &pcd->ep0;
  180. retval = min((uint16_t) retval, wLen);
  181. /* Transfer this buffer to the host through the EP0-IN EP */
  182. ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  183. ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  184. ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  185. ep->dwc_ep.xfer_len = retval;
  186. ep->dwc_ep.xfer_count = 0;
  187. ep->dwc_ep.sent_zlp = 0;
  188. ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  189. pcd->ep0_pending = 1;
  190. dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  191. }
  192. retval = 0;
  193. break;
  194. case VEN_CORE_GET_FEATURE:
  195. CFI_INFO("VEN_CORE_GET_FEATURE\n");
  196. retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  197. pcd, ctrl);
  198. if (retval >= 0) {
  199. ep = &pcd->ep0;
  200. retval = min((uint16_t) retval, wLen);
  201. /* Transfer this buffer to the host through the EP0-IN EP */
  202. ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  203. ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  204. ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  205. ep->dwc_ep.xfer_len = retval;
  206. ep->dwc_ep.xfer_count = 0;
  207. ep->dwc_ep.sent_zlp = 0;
  208. ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  209. pcd->ep0_pending = 1;
  210. dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  211. }
  212. CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  213. dump_msg(cfi->buf_in.buf, retval);
  214. break;
  215. case VEN_CORE_SET_FEATURE:
  216. CFI_INFO("VEN_CORE_SET_FEATURE\n");
  217. /* Set up an XFER to get the data stage of the control request,
  218. * which is the new value of the feature to be modified.
  219. */
  220. ep = &pcd->ep0;
  221. ep->dwc_ep.is_in = 0;
  222. ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  223. ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  224. ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  225. ep->dwc_ep.xfer_len = wLen;
  226. ep->dwc_ep.xfer_count = 0;
  227. ep->dwc_ep.sent_zlp = 0;
  228. ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  229. pcd->ep0_pending = 1;
  230. /* Read the control write's data stage */
  231. dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  232. retval = 0;
  233. break;
  234. case VEN_CORE_RESET_FEATURES:
  235. CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  236. cfi->need_gadget_att = 1;
  237. cfi->need_status_in_complete = 1;
  238. retval = cfi_preproc_reset(pcd, ctrl);
  239. CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  240. break;
  241. case VEN_CORE_ACTIVATE_FEATURES:
  242. CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  243. break;
  244. case VEN_CORE_READ_REGISTER:
  245. CFI_INFO("VEN_CORE_READ_REGISTER\n");
  246. /* wValue optionally contains the HI WORD of the register offset and
  247. * wIndex contains the LOW WORD of the register offset
  248. */
  249. if (wValue == 0) {
  250. /* @TODO - MAS - fix the access to the base field */
  251. regaddr = 0;
  252. //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  253. //GET_CORE_IF(pcd)->co
  254. regaddr |= wIndex;
  255. } else {
  256. regaddr = (wValue << 16) | wIndex;
  257. }
  258. /* Read a 32-bit value of the memory at the regaddr */
  259. regval = DWC_READ_REG32((uint32_t *) regaddr);
  260. ep = &pcd->ep0;
  261. dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  262. ep->dwc_ep.is_in = 1;
  263. ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  264. ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  265. ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  266. ep->dwc_ep.xfer_len = wLen;
  267. ep->dwc_ep.xfer_count = 0;
  268. ep->dwc_ep.sent_zlp = 0;
  269. ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  270. pcd->ep0_pending = 1;
  271. dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  272. cfi->need_gadget_att = 0;
  273. retval = 0;
  274. break;
  275. case VEN_CORE_WRITE_REGISTER:
  276. CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  277. /* Set up an XFER to get the data stage of the control request,
  278. * which is the new value of the register to be modified.
  279. */
  280. ep = &pcd->ep0;
  281. ep->dwc_ep.is_in = 0;
  282. ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  283. ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  284. ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  285. ep->dwc_ep.xfer_len = wLen;
  286. ep->dwc_ep.xfer_count = 0;
  287. ep->dwc_ep.sent_zlp = 0;
  288. ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  289. pcd->ep0_pending = 1;
  290. /* Read the control write's data stage */
  291. dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  292. retval = 0;
  293. break;
  294. default:
  295. retval = -DWC_E_NOT_SUPPORTED;
  296. break;
  297. }
  298. return retval;
  299. }
  300. /**
  301. * This function prepares the core features descriptors and copies its
  302. * raw representation into the buffer <buf>.
  303. *
  304. * The buffer structure is as follows:
  305. * all_features_header (8 bytes)
  306. * features_#1 (8 bytes + feature name string length)
  307. * features_#2 (8 bytes + feature name string length)
  308. * .....
  309. * features_#n - where n=the total count of feature descriptors
  310. */
  311. static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  312. {
  313. cfi_feature_desc_header_t *prop_hdr = prop_descs;
  314. cfi_feature_desc_header_t *prop;
  315. cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  316. cfi_all_features_header_t *tmp;
  317. uint8_t *tmpbuf = buf;
  318. const uint8_t *pname = NULL;
  319. int i, j, namelen = 0, totlen;
  320. /* Prepare and copy the core features into the buffer */
  321. CFI_INFO("%s:\n", __func__);
  322. tmp = (cfi_all_features_header_t *) tmpbuf;
  323. *tmp = *all_props_hdr;
  324. tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  325. j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  326. for (i = 0; i < j; i++, prop_hdr++) {
  327. pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  328. prop = (cfi_feature_desc_header_t *) tmpbuf;
  329. *prop = *prop_hdr;
  330. prop->bNameLen = namelen;
  331. prop->wLength =
  332. DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  333. namelen);
  334. tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  335. dwc_memcpy(tmpbuf, pname, namelen);
  336. tmpbuf += namelen;
  337. }
  338. totlen = tmpbuf - buf;
  339. if (totlen > 0) {
  340. tmp = (cfi_all_features_header_t *) buf;
  341. tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  342. }
  343. return totlen;
  344. }
  345. /**
  346. * This function releases all the dynamic memory in the CFI object.
  347. */
  348. static void cfi_release(cfiobject_t * cfiobj)
  349. {
  350. cfi_ep_t *cfiep;
  351. dwc_list_link_t *tmp;
  352. CFI_INFO("%s\n", __func__);
  353. if (cfiobj->buf_in.buf) {
  354. DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  355. cfiobj->buf_in.addr);
  356. cfiobj->buf_in.buf = NULL;
  357. }
  358. if (cfiobj->buf_out.buf) {
  359. DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  360. cfiobj->buf_out.addr);
  361. cfiobj->buf_out.buf = NULL;
  362. }
  363. /* Free the Buffer Setup values for each EP */
  364. //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  365. DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  366. cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  367. cfi_free_ep_bs_dyn_data(cfiep);
  368. }
  369. }
  370. /**
  371. * This function frees the dynamically allocated EP buffer setup data.
  372. */
  373. static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  374. {
  375. if (cfiep->bm_sg) {
  376. DWC_FREE(cfiep->bm_sg);
  377. cfiep->bm_sg = NULL;
  378. }
  379. if (cfiep->bm_align) {
  380. DWC_FREE(cfiep->bm_align);
  381. cfiep->bm_align = NULL;
  382. }
  383. if (cfiep->bm_concat) {
  384. if (NULL != cfiep->bm_concat->wTxBytes) {
  385. DWC_FREE(cfiep->bm_concat->wTxBytes);
  386. cfiep->bm_concat->wTxBytes = NULL;
  387. }
  388. DWC_FREE(cfiep->bm_concat);
  389. cfiep->bm_concat = NULL;
  390. }
  391. }
  392. /**
  393. * This function initializes the default values of the features
  394. * for a specific endpoint and should be called only once when
  395. * the EP is enabled first time.
  396. */
  397. static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  398. {
  399. int retval = 0;
  400. cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  401. if (NULL == cfiep->bm_sg) {
  402. CFI_INFO("Failed to allocate memory for SG feature value\n");
  403. return -DWC_E_NO_MEMORY;
  404. }
  405. dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  406. /* For the Concatenation feature's default value we do not allocate
  407. * memory for the wTxBytes field - it will be done in the set_feature_value
  408. * request handler.
  409. */
  410. cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  411. if (NULL == cfiep->bm_concat) {
  412. CFI_INFO
  413. ("Failed to allocate memory for CONCATENATION feature value\n");
  414. DWC_FREE(cfiep->bm_sg);
  415. return -DWC_E_NO_MEMORY;
  416. }
  417. dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  418. cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  419. if (NULL == cfiep->bm_align) {
  420. CFI_INFO
  421. ("Failed to allocate memory for Alignment feature value\n");
  422. DWC_FREE(cfiep->bm_sg);
  423. DWC_FREE(cfiep->bm_concat);
  424. return -DWC_E_NO_MEMORY;
  425. }
  426. dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  427. return retval;
  428. }
  429. /**
  430. * The callback function that notifies the CFI on the activation of
  431. * an endpoint in the PCD. The following steps are done in this function:
  432. *
  433. * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  434. * active endpoint)
  435. * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  436. * Set the Buffer Mode to standard
  437. * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  438. * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  439. */
  440. static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  441. struct dwc_otg_pcd_ep *ep)
  442. {
  443. cfi_ep_t *cfiep;
  444. int retval = -DWC_E_NOT_SUPPORTED;
  445. CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  446. "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  447. /* MAS - Check whether this endpoint already is in the list */
  448. cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  449. if (NULL == cfiep) {
  450. /* Allocate a cfi_ep_t object */
  451. cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  452. if (NULL == cfiep) {
  453. CFI_INFO
  454. ("Unable to allocate memory for <cfiep> in function %s\n",
  455. __func__);
  456. return -DWC_E_NO_MEMORY;
  457. }
  458. dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  459. /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  460. cfiep->ep = ep;
  461. /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  462. ep->dwc_ep.descs =
  463. DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  464. sizeof(dwc_otg_dma_desc_t),
  465. &ep->dwc_ep.descs_dma_addr);
  466. if (NULL == ep->dwc_ep.descs) {
  467. DWC_FREE(cfiep);
  468. return -DWC_E_NO_MEMORY;
  469. }
  470. DWC_LIST_INIT(&cfiep->lh);
  471. /* Set the buffer mode to BM_STANDARD. It will be modified
  472. * when building descriptors for a specific buffer mode */
  473. ep->dwc_ep.buff_mode = BM_STANDARD;
  474. /* Create and initialize the default values for this EP's Buffer modes */
  475. if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  476. return retval;
  477. /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  478. DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  479. retval = 0;
  480. } else { /* The sought EP already is in the list */
  481. CFI_INFO("%s: The sought EP already is in the list\n",
  482. __func__);
  483. }
  484. return retval;
  485. }
  486. /**
  487. * This function is called when the data stage of a 3-stage Control Write request
  488. * is complete.
  489. *
  490. */
  491. static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  492. struct dwc_otg_pcd *pcd)
  493. {
  494. uint32_t addr, reg_value;
  495. uint16_t wIndex, wValue;
  496. uint8_t bRequest;
  497. uint8_t *buf = cfi->buf_out.buf;
  498. //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  499. struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  500. int retval = -DWC_E_NOT_SUPPORTED;
  501. CFI_INFO("%s\n", __func__);
  502. bRequest = ctrl_req->bRequest;
  503. wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  504. wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  505. /*
  506. * Save the pointer to the data stage in the ctrl_req's <data> field.
  507. * The request should be already saved in the command stage by now.
  508. */
  509. ctrl_req->data = cfi->buf_out.buf;
  510. cfi->need_status_in_complete = 0;
  511. cfi->need_gadget_att = 0;
  512. switch (bRequest) {
  513. case VEN_CORE_WRITE_REGISTER:
  514. /* The buffer contains raw data of the new value for the register */
  515. reg_value = *((uint32_t *) buf);
  516. if (wValue == 0) {
  517. addr = 0;
  518. //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  519. addr += wIndex;
  520. } else {
  521. addr = (wValue << 16) | wIndex;
  522. }
  523. //writel(reg_value, addr);
  524. retval = 0;
  525. cfi->need_status_in_complete = 1;
  526. break;
  527. case VEN_CORE_SET_FEATURE:
  528. /* The buffer contains raw data of the new value of the feature */
  529. retval = cfi_set_feature_value(pcd);
  530. if (retval < 0)
  531. return retval;
  532. cfi->need_status_in_complete = 1;
  533. break;
  534. default:
  535. break;
  536. }
  537. return retval;
  538. }
  539. /**
  540. * This function builds the DMA descriptors for the SG buffer mode.
  541. */
  542. static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  543. dwc_otg_pcd_request_t * req)
  544. {
  545. struct dwc_otg_pcd_ep *ep = cfiep->ep;
  546. ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  547. struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  548. struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  549. dma_addr_t buff_addr = req->dma;
  550. int i;
  551. uint32_t txsize, off;
  552. txsize = sgval->wSize;
  553. off = sgval->bOffset;
  554. // CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  555. // __func__, cfiep->ep->ep.name, txsize, off);
  556. for (i = 0; i < sgval->bCount; i++) {
  557. desc->status.b.bs = BS_HOST_BUSY;
  558. desc->buf = buff_addr;
  559. desc->status.b.l = 0;
  560. desc->status.b.ioc = 0;
  561. desc->status.b.sp = 0;
  562. desc->status.b.bytes = txsize;
  563. desc->status.b.bs = BS_HOST_READY;
  564. /* Set the next address of the buffer */
  565. buff_addr += txsize + off;
  566. desc_last = desc;
  567. desc++;
  568. }
  569. /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  570. desc_last->status.b.l = 1;
  571. desc_last->status.b.ioc = 1;
  572. desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  573. /* Save the last DMA descriptor pointer */
  574. cfiep->dma_desc_last = desc_last;
  575. cfiep->desc_count = sgval->bCount;
  576. }
  577. /**
  578. * This function builds the DMA descriptors for the Concatenation buffer mode.
  579. */
  580. static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  581. dwc_otg_pcd_request_t * req)
  582. {
  583. struct dwc_otg_pcd_ep *ep = cfiep->ep;
  584. ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  585. struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  586. struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  587. dma_addr_t buff_addr = req->dma;
  588. int i;
  589. uint16_t *txsize;
  590. txsize = concatval->wTxBytes;
  591. for (i = 0; i < concatval->hdr.bDescCount; i++) {
  592. desc->buf = buff_addr;
  593. desc->status.b.bs = BS_HOST_BUSY;
  594. desc->status.b.l = 0;
  595. desc->status.b.ioc = 0;
  596. desc->status.b.sp = 0;
  597. desc->status.b.bytes = *txsize;
  598. desc->status.b.bs = BS_HOST_READY;
  599. txsize++;
  600. /* Set the next address of the buffer */
  601. buff_addr += UGETW(ep->desc->wMaxPacketSize);
  602. desc_last = desc;
  603. desc++;
  604. }
  605. /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  606. desc_last->status.b.l = 1;
  607. desc_last->status.b.ioc = 1;
  608. desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  609. cfiep->dma_desc_last = desc_last;
  610. cfiep->desc_count = concatval->hdr.bDescCount;
  611. }
  612. /**
  613. * This function builds the DMA descriptors for the Circular buffer mode
  614. */
  615. static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  616. dwc_otg_pcd_request_t * req)
  617. {
  618. /* @todo: MAS - add implementation when this feature needs to be tested */
  619. }
  620. /**
  621. * This function builds the DMA descriptors for the Alignment buffer mode
  622. */
  623. static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  624. dwc_otg_pcd_request_t * req)
  625. {
  626. struct dwc_otg_pcd_ep *ep = cfiep->ep;
  627. ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  628. struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  629. dma_addr_t buff_addr = req->dma;
  630. desc->status.b.bs = BS_HOST_BUSY;
  631. desc->status.b.l = 1;
  632. desc->status.b.ioc = 1;
  633. desc->status.b.sp = ep->dwc_ep.sent_zlp;
  634. desc->status.b.bytes = req->length;
  635. /* Adjust the buffer alignment */
  636. desc->buf = (buff_addr + alignval->bAlign);
  637. desc->status.b.bs = BS_HOST_READY;
  638. cfiep->dma_desc_last = desc;
  639. cfiep->desc_count = 1;
  640. }
  641. /**
  642. * This function builds the DMA descriptors chain for different modes of the
  643. * buffer setup of an endpoint.
  644. */
  645. static void cfi_build_descriptors(struct cfiobject *cfi,
  646. struct dwc_otg_pcd *pcd,
  647. struct dwc_otg_pcd_ep *ep,
  648. dwc_otg_pcd_request_t * req)
  649. {
  650. cfi_ep_t *cfiep;
  651. /* Get the cfiep by the dwc_otg_pcd_ep */
  652. cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  653. if (NULL == cfiep) {
  654. CFI_INFO("%s: Unable to find a matching active endpoint\n",
  655. __func__);
  656. return;
  657. }
  658. cfiep->xfer_len = req->length;
  659. /* Iterate through all the DMA descriptors */
  660. switch (cfiep->ep->dwc_ep.buff_mode) {
  661. case BM_SG:
  662. cfi_build_sg_descs(cfi, cfiep, req);
  663. break;
  664. case BM_CONCAT:
  665. cfi_build_concat_descs(cfi, cfiep, req);
  666. break;
  667. case BM_CIRCULAR:
  668. cfi_build_circ_descs(cfi, cfiep, req);
  669. break;
  670. case BM_ALIGN:
  671. cfi_build_align_descs(cfi, cfiep, req);
  672. break;
  673. default:
  674. break;
  675. }
  676. }
  677. /**
  678. * Allocate DMA buffer for different Buffer modes.
  679. */
  680. static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  681. struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  682. unsigned size, gfp_t flags)
  683. {
  684. return DWC_DMA_ALLOC(size, dma);
  685. }
  686. /**
  687. * This function initializes the CFI object.
  688. */
  689. int init_cfi(cfiobject_t * cfiobj)
  690. {
  691. CFI_INFO("%s\n", __func__);
  692. /* Allocate a buffer for IN XFERs */
  693. cfiobj->buf_in.buf =
  694. DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  695. if (NULL == cfiobj->buf_in.buf) {
  696. CFI_INFO("Unable to allocate buffer for INs\n");
  697. return -DWC_E_NO_MEMORY;
  698. }
  699. /* Allocate a buffer for OUT XFERs */
  700. cfiobj->buf_out.buf =
  701. DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  702. if (NULL == cfiobj->buf_out.buf) {
  703. CFI_INFO("Unable to allocate buffer for OUT\n");
  704. return -DWC_E_NO_MEMORY;
  705. }
  706. /* Initialize the callback function pointers */
  707. cfiobj->ops.release = cfi_release;
  708. cfiobj->ops.ep_enable = cfi_ep_enable;
  709. cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  710. cfiobj->ops.build_descriptors = cfi_build_descriptors;
  711. cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  712. /* Initialize the list of active endpoints in the CFI object */
  713. DWC_LIST_INIT(&cfiobj->active_eps);
  714. return 0;
  715. }
  716. /**
  717. * This function reads the required feature's current value into the buffer
  718. *
  719. * @retval: Returns negative as error, or the data length of the feature
  720. */
  721. static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  722. struct dwc_otg_pcd *pcd,
  723. struct cfi_usb_ctrlrequest *ctrl_req)
  724. {
  725. int retval = -DWC_E_NOT_SUPPORTED;
  726. struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  727. uint16_t dfifo, rxfifo, txfifo;
  728. switch (ctrl_req->wIndex) {
  729. /* Whether the DDMA is enabled or not */
  730. case FT_ID_DMA_MODE:
  731. *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  732. retval = 1;
  733. break;
  734. case FT_ID_DMA_BUFFER_SETUP:
  735. retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  736. break;
  737. case FT_ID_DMA_BUFF_ALIGN:
  738. retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  739. break;
  740. case FT_ID_DMA_CONCAT_SETUP:
  741. retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  742. break;
  743. case FT_ID_DMA_CIRCULAR:
  744. CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  745. break;
  746. case FT_ID_THRESHOLD_SETUP:
  747. CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  748. break;
  749. case FT_ID_DFIFO_DEPTH:
  750. dfifo = get_dfifo_size(coreif);
  751. *((uint16_t *) buf) = dfifo;
  752. retval = sizeof(uint16_t);
  753. break;
  754. case FT_ID_TX_FIFO_DEPTH:
  755. retval = get_txfifo_size(pcd, ctrl_req->wValue);
  756. if (retval >= 0) {
  757. txfifo = retval;
  758. *((uint16_t *) buf) = txfifo;
  759. retval = sizeof(uint16_t);
  760. }
  761. break;
  762. case FT_ID_RX_FIFO_DEPTH:
  763. retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  764. if (retval >= 0) {
  765. rxfifo = retval;
  766. *((uint16_t *) buf) = rxfifo;
  767. retval = sizeof(uint16_t);
  768. }
  769. break;
  770. }
  771. return retval;
  772. }
  773. /**
  774. * This function resets the SG for the specified EP to its default value
  775. */
  776. static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  777. {
  778. dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  779. return 0;
  780. }
  781. /**
  782. * This function resets the Alignment for the specified EP to its default value
  783. */
  784. static int cfi_reset_align_val(cfi_ep_t * cfiep)
  785. {
  786. dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  787. return 0;
  788. }
  789. /**
  790. * This function resets the Concatenation for the specified EP to its default value
  791. * This function will also set the value of the wTxBytes field to NULL after
  792. * freeing the memory previously allocated for this field.
  793. */
  794. static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  795. {
  796. /* First we need to free the wTxBytes field */
  797. if (cfiep->bm_concat->wTxBytes) {
  798. DWC_FREE(cfiep->bm_concat->wTxBytes);
  799. cfiep->bm_concat->wTxBytes = NULL;
  800. }
  801. dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  802. return 0;
  803. }
  804. /**
  805. * This function resets all the buffer setups of the specified endpoint
  806. */
  807. static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  808. {
  809. cfi_reset_sg_val(cfiep);
  810. cfi_reset_align_val(cfiep);
  811. cfi_reset_concat_val(cfiep);
  812. return 0;
  813. }
  814. static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  815. uint8_t rx_rst, uint8_t tx_rst)
  816. {
  817. int retval = -DWC_E_INVALID;
  818. uint16_t tx_siz[15];
  819. uint16_t rx_siz = 0;
  820. dwc_otg_pcd_ep_t *ep = NULL;
  821. dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  822. dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  823. if (rx_rst) {
  824. rx_siz = params->dev_rx_fifo_size;
  825. params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  826. }
  827. if (tx_rst) {
  828. if (ep_addr == 0) {
  829. int i;
  830. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  831. tx_siz[i] =
  832. core_if->core_params->dev_tx_fifo_size[i];
  833. core_if->core_params->dev_tx_fifo_size[i] =
  834. core_if->init_txfsiz[i];
  835. }
  836. } else {
  837. ep = get_ep_by_addr(pcd, ep_addr);
  838. if (NULL == ep) {
  839. CFI_INFO
  840. ("%s: Unable to get the endpoint addr=0x%02x\n",
  841. __func__, ep_addr);
  842. return -DWC_E_INVALID;
  843. }
  844. tx_siz[0] =
  845. params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  846. 1];
  847. params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  848. GET_CORE_IF(pcd)->init_txfsiz[ep->
  849. dwc_ep.tx_fifo_num -
  850. 1];
  851. }
  852. }
  853. if (resize_fifos(GET_CORE_IF(pcd))) {
  854. retval = 0;
  855. } else {
  856. CFI_INFO
  857. ("%s: Error resetting the feature Reset All(FIFO size)\n",
  858. __func__);
  859. if (rx_rst) {
  860. params->dev_rx_fifo_size = rx_siz;
  861. }
  862. if (tx_rst) {
  863. if (ep_addr == 0) {
  864. int i;
  865. for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  866. i++) {
  867. core_if->
  868. core_params->dev_tx_fifo_size[i] =
  869. tx_siz[i];
  870. }
  871. } else {
  872. params->dev_tx_fifo_size[ep->
  873. dwc_ep.tx_fifo_num -
  874. 1] = tx_siz[0];
  875. }
  876. }
  877. retval = -DWC_E_INVALID;
  878. }
  879. return retval;
  880. }
  881. static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  882. {
  883. int retval = 0;
  884. cfi_ep_t *cfiep;
  885. cfiobject_t *cfi = pcd->cfi;
  886. dwc_list_link_t *tmp;
  887. retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  888. if (retval < 0) {
  889. return retval;
  890. }
  891. /* If the EP address is known then reset the features for only that EP */
  892. if (addr) {
  893. cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  894. if (NULL == cfiep) {
  895. CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  896. __func__, addr);
  897. return -DWC_E_INVALID;
  898. }
  899. retval = cfi_ep_reset_all_setup_vals(cfiep);
  900. cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  901. }
  902. /* Otherwise (wValue == 0), reset all features of all EP's */
  903. else {
  904. /* Traverse all the active EP's and reset the feature(s) value(s) */
  905. //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  906. DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  907. cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  908. retval = cfi_ep_reset_all_setup_vals(cfiep);
  909. cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  910. if (retval < 0) {
  911. CFI_INFO
  912. ("%s: Error resetting the feature Reset All\n",
  913. __func__);
  914. return retval;
  915. }
  916. }
  917. }
  918. return retval;
  919. }
  920. static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  921. uint8_t addr)
  922. {
  923. int retval = 0;
  924. cfi_ep_t *cfiep;
  925. cfiobject_t *cfi = pcd->cfi;
  926. dwc_list_link_t *tmp;
  927. /* If the EP address is known then reset the features for only that EP */
  928. if (addr) {
  929. cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  930. if (NULL == cfiep) {
  931. CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  932. __func__, addr);
  933. return -DWC_E_INVALID;
  934. }
  935. retval = cfi_reset_sg_val(cfiep);
  936. }
  937. /* Otherwise (wValue == 0), reset all features of all EP's */
  938. else {
  939. /* Traverse all the active EP's and reset the feature(s) value(s) */
  940. //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  941. DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  942. cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  943. retval = cfi_reset_sg_val(cfiep);
  944. if (retval < 0) {
  945. CFI_INFO
  946. ("%s: Error resetting the feature Buffer Setup\n",
  947. __func__);
  948. return retval;
  949. }
  950. }
  951. }
  952. return retval;
  953. }
  954. static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  955. {
  956. int retval = 0;
  957. cfi_ep_t *cfiep;
  958. cfiobject_t *cfi = pcd->cfi;
  959. dwc_list_link_t *tmp;
  960. /* If the EP address is known then reset the features for only that EP */
  961. if (addr) {
  962. cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  963. if (NULL == cfiep) {
  964. CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  965. __func__, addr);
  966. return -DWC_E_INVALID;
  967. }
  968. retval = cfi_reset_concat_val(cfiep);
  969. }
  970. /* Otherwise (wValue == 0), reset all features of all EP's */
  971. else {
  972. /* Traverse all the active EP's and reset the feature(s) value(s) */
  973. //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  974. DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  975. cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  976. retval = cfi_reset_concat_val(cfiep);
  977. if (retval < 0) {
  978. CFI_INFO
  979. ("%s: Error resetting the feature Concatenation Value\n",
  980. __func__);
  981. return retval;
  982. }
  983. }
  984. }
  985. return retval;
  986. }
  987. static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  988. {
  989. int retval = 0;
  990. cfi_ep_t *cfiep;
  991. cfiobject_t *cfi = pcd->cfi;
  992. dwc_list_link_t *tmp;
  993. /* If the EP address is known then reset the features for only that EP */
  994. if (addr) {
  995. cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  996. if (NULL == cfiep) {
  997. CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  998. __func__, addr);
  999. return -DWC_E_INVALID;
  1000. }
  1001. retval = cfi_reset_align_val(cfiep);
  1002. }
  1003. /* Otherwise (wValue == 0), reset all features of all EP's */
  1004. else {
  1005. /* Traverse all the active EP's and reset the feature(s) value(s) */
  1006. //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  1007. DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  1008. cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  1009. retval = cfi_reset_align_val(cfiep);
  1010. if (retval < 0) {
  1011. CFI_INFO
  1012. ("%s: Error resetting the feature Aliignment Value\n",
  1013. __func__);
  1014. return retval;
  1015. }
  1016. }
  1017. }
  1018. return retval;
  1019. }
  1020. static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  1021. struct cfi_usb_ctrlrequest *req)
  1022. {
  1023. int retval = 0;
  1024. switch (req->wIndex) {
  1025. case 0:
  1026. /* Reset all features */
  1027. retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  1028. break;
  1029. case FT_ID_DMA_BUFFER_SETUP:
  1030. /* Reset the SG buffer setup */
  1031. retval =
  1032. cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  1033. break;
  1034. case FT_ID_DMA_CONCAT_SETUP:
  1035. /* Reset the Concatenation buffer setup */
  1036. retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  1037. break;
  1038. case FT_ID_DMA_BUFF_ALIGN:
  1039. /* Reset the Alignment buffer setup */
  1040. retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  1041. break;
  1042. case FT_ID_TX_FIFO_DEPTH:
  1043. retval =
  1044. cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  1045. pcd->cfi->need_gadget_att = 0;
  1046. break;
  1047. case FT_ID_RX_FIFO_DEPTH:
  1048. retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  1049. pcd->cfi->need_gadget_att = 0;
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. return retval;
  1055. }
  1056. /**
  1057. * This function sets a new value for the SG buffer setup.
  1058. */
  1059. static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  1060. {
  1061. uint8_t inaddr, outaddr;
  1062. cfi_ep_t *epin, *epout;
  1063. ddma_sg_buffer_setup_t *psgval;
  1064. uint32_t desccount, size;
  1065. CFI_INFO("%s\n", __func__);
  1066. psgval = (ddma_sg_buffer_setup_t *) buf;
  1067. desccount = (uint32_t) psgval->bCount;
  1068. size = (uint32_t) psgval->wSize;
  1069. /* Check the DMA descriptor count */
  1070. if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  1071. CFI_INFO
  1072. ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  1073. __func__, MAX_DMA_DESCS_PER_EP);
  1074. return -DWC_E_INVALID;
  1075. }
  1076. /* Check the DMA descriptor count */
  1077. if (size == 0) {
  1078. CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  1079. __func__);
  1080. return -DWC_E_INVALID;
  1081. }
  1082. inaddr = psgval->bInEndpointAddress;
  1083. outaddr = psgval->bOutEndpointAddress;
  1084. epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  1085. epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  1086. if (NULL == epin || NULL == epout) {
  1087. CFI_INFO
  1088. ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  1089. __func__, inaddr, outaddr);
  1090. return -DWC_E_INVALID;
  1091. }
  1092. epin->ep->dwc_ep.buff_mode = BM_SG;
  1093. dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  1094. epout->ep->dwc_ep.buff_mode = BM_SG;
  1095. dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  1096. return 0;
  1097. }
  1098. /**
  1099. * This function sets a new value for the buffer Alignment setup.
  1100. */
  1101. static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  1102. {
  1103. cfi_ep_t *ep;
  1104. uint8_t addr;
  1105. ddma_align_buffer_setup_t *palignval;
  1106. palignval = (ddma_align_buffer_setup_t *) buf;
  1107. addr = palignval->bEndpointAddress;
  1108. ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  1109. if (NULL == ep) {
  1110. CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  1111. __func__, addr);
  1112. return -DWC_E_INVALID;
  1113. }
  1114. ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  1115. dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  1116. return 0;
  1117. }
  1118. /**
  1119. * This function sets a new value for the Concatenation buffer setup.
  1120. */
  1121. static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  1122. {
  1123. uint8_t addr;
  1124. cfi_ep_t *ep;
  1125. struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  1126. uint16_t *pVals;
  1127. uint32_t desccount;
  1128. int i;
  1129. uint16_t mps;
  1130. pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  1131. desccount = (uint32_t) pConcatValHdr->bDescCount;
  1132. pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  1133. /* Check the DMA descriptor count */
  1134. if (desccount > MAX_DMA_DESCS_PER_EP) {
  1135. CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  1136. __func__, MAX_DMA_DESCS_PER_EP);
  1137. return -DWC_E_INVALID;
  1138. }
  1139. addr = pConcatValHdr->bEndpointAddress;
  1140. ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  1141. if (NULL == ep) {
  1142. CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  1143. __func__, addr);
  1144. return -DWC_E_INVALID;
  1145. }
  1146. mps = UGETW(ep->ep->desc->wMaxPacketSize);
  1147. #if 0
  1148. for (i = 0; i < desccount; i++) {
  1149. CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  1150. }
  1151. CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  1152. #endif
  1153. /* Check the wTxSizes to be less than or equal to the mps */
  1154. for (i = 0; i < desccount; i++) {
  1155. if (pVals[i] > mps) {
  1156. CFI_INFO
  1157. ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  1158. __func__, i, pVals[i]);
  1159. return -DWC_E_INVALID;
  1160. }
  1161. }
  1162. ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  1163. dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  1164. /* Free the previously allocated storage for the wTxBytes */
  1165. if (ep->bm_concat->wTxBytes) {
  1166. DWC_FREE(ep->bm_concat->wTxBytes);
  1167. }
  1168. /* Allocate a new storage for the wTxBytes field */
  1169. ep->bm_concat->wTxBytes =
  1170. DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  1171. if (NULL == ep->bm_concat->wTxBytes) {
  1172. CFI_INFO("%s: Unable to allocate memory\n", __func__);
  1173. return -DWC_E_NO_MEMORY;
  1174. }
  1175. /* Copy the new values into the wTxBytes filed */
  1176. dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  1177. sizeof(uint16_t) * pConcatValHdr->bDescCount);
  1178. return 0;
  1179. }
  1180. /**
  1181. * This function calculates the total of all FIFO sizes
  1182. *
  1183. * @param core_if Programming view of DWC_otg controller
  1184. *
  1185. * @return The total of data FIFO sizes.
  1186. *
  1187. */
  1188. static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  1189. {
  1190. dwc_otg_core_params_t *params = core_if->core_params;
  1191. uint16_t dfifo_total = 0;
  1192. int i;
  1193. /* The shared RxFIFO size */
  1194. dfifo_total =
  1195. params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  1196. /* Add up each TxFIFO size to the total */
  1197. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1198. dfifo_total += params->dev_tx_fifo_size[i];
  1199. }
  1200. return dfifo_total;
  1201. }
  1202. /**
  1203. * This function returns Rx FIFO size
  1204. *
  1205. * @param core_if Programming view of DWC_otg controller
  1206. *
  1207. * @return The total of data FIFO sizes.
  1208. *
  1209. */
  1210. static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  1211. {
  1212. switch (wValue >> 8) {
  1213. case 0:
  1214. return (core_if->pwron_rxfsiz <
  1215. 32768) ? core_if->pwron_rxfsiz : 32768;
  1216. break;
  1217. case 1:
  1218. return core_if->core_params->dev_rx_fifo_size;
  1219. break;
  1220. default:
  1221. return -DWC_E_INVALID;
  1222. break;
  1223. }
  1224. }
  1225. /**
  1226. * This function returns Tx FIFO size for IN EP
  1227. *
  1228. * @param core_if Programming view of DWC_otg controller
  1229. *
  1230. * @return The total of data FIFO sizes.
  1231. *
  1232. */
  1233. static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  1234. {
  1235. dwc_otg_pcd_ep_t *ep;
  1236. ep = get_ep_by_addr(pcd, wValue & 0xff);
  1237. if (NULL == ep) {
  1238. CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  1239. __func__, wValue & 0xff);
  1240. return -DWC_E_INVALID;
  1241. }
  1242. if (!ep->dwc_ep.is_in) {
  1243. CFI_INFO
  1244. ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  1245. __func__, wValue & 0xff);
  1246. return -DWC_E_INVALID;
  1247. }
  1248. switch (wValue >> 8) {
  1249. case 0:
  1250. return (GET_CORE_IF(pcd)->pwron_txfsiz
  1251. [ep->dwc_ep.tx_fifo_num - 1] <
  1252. 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  1253. dwc_ep.tx_fifo_num
  1254. - 1] : 32768;
  1255. break;
  1256. case 1:
  1257. return GET_CORE_IF(pcd)->core_params->
  1258. dev_tx_fifo_size[ep->dwc_ep.num - 1];
  1259. break;
  1260. default:
  1261. return -DWC_E_INVALID;
  1262. break;
  1263. }
  1264. }
  1265. /**
  1266. * This function checks if the submitted combination of
  1267. * device mode FIFO sizes is possible or not.
  1268. *
  1269. * @param core_if Programming view of DWC_otg controller
  1270. *
  1271. * @return 1 if possible, 0 otherwise.
  1272. *
  1273. */
  1274. static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  1275. {
  1276. uint16_t dfifo_actual = 0;
  1277. dwc_otg_core_params_t *params = core_if->core_params;
  1278. uint16_t start_addr = 0;
  1279. int i;
  1280. dfifo_actual =
  1281. params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  1282. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1283. dfifo_actual += params->dev_tx_fifo_size[i];
  1284. }
  1285. if (dfifo_actual > core_if->total_fifo_size) {
  1286. return 0;
  1287. }
  1288. if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  1289. return 0;
  1290. if (params->dev_nperio_tx_fifo_size > 32768
  1291. || params->dev_nperio_tx_fifo_size < 16)
  1292. return 0;
  1293. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1294. if (params->dev_tx_fifo_size[i] > 768
  1295. || params->dev_tx_fifo_size[i] < 4)
  1296. return 0;
  1297. }
  1298. if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  1299. return 0;
  1300. start_addr = params->dev_rx_fifo_size;
  1301. if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  1302. return 0;
  1303. start_addr += params->dev_nperio_tx_fifo_size;
  1304. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1305. if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  1306. return 0;
  1307. start_addr += params->dev_tx_fifo_size[i];
  1308. }
  1309. return 1;
  1310. }
  1311. /**
  1312. * This function resizes Device mode FIFOs
  1313. *
  1314. * @param core_if Programming view of DWC_otg controller
  1315. *
  1316. * @return 1 if successful, 0 otherwise
  1317. *
  1318. */
  1319. static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  1320. {
  1321. int i = 0;
  1322. dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  1323. dwc_otg_core_params_t *params = core_if->core_params;
  1324. uint32_t rx_fifo_size;
  1325. fifosize_data_t nptxfifosize;
  1326. fifosize_data_t txfifosize[15];
  1327. uint32_t rx_fsz_bak;
  1328. uint32_t nptxfsz_bak;
  1329. uint32_t txfsz_bak[15];
  1330. uint16_t start_address;
  1331. uint8_t retval = 1;
  1332. if (!check_fifo_sizes(core_if)) {
  1333. return 0;
  1334. }
  1335. /* Configure data FIFO sizes */
  1336. if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  1337. rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  1338. rx_fifo_size = params->dev_rx_fifo_size;
  1339. DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  1340. /*
  1341. * Tx FIFOs These FIFOs are numbered from 1 to 15.
  1342. * Indexes of the FIFO size module parameters in the
  1343. * dev_tx_fifo_size array and the FIFO size registers in
  1344. * the dtxfsiz array run from 0 to 14.
  1345. */
  1346. /* Non-periodic Tx FIFO */
  1347. nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  1348. nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  1349. start_address = params->dev_rx_fifo_size;
  1350. nptxfifosize.b.startaddr = start_address;
  1351. DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  1352. start_address += nptxfifosize.b.depth;
  1353. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1354. txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  1355. txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  1356. txfifosize[i].b.startaddr = start_address;
  1357. DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  1358. txfifosize[i].d32);
  1359. start_address += txfifosize[i].b.depth;
  1360. }
  1361. /** Check if register values are set correctly */
  1362. if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  1363. retval = 0;
  1364. }
  1365. if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  1366. retval = 0;
  1367. }
  1368. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1369. if (txfifosize[i].d32 !=
  1370. DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  1371. retval = 0;
  1372. }
  1373. }
  1374. /** If register values are not set correctly, reset old values */
  1375. if (retval == 0) {
  1376. DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  1377. /* Non-periodic Tx FIFO */
  1378. DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  1379. for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  1380. DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  1381. txfsz_bak[i]);
  1382. }
  1383. }
  1384. } else {
  1385. return 0;
  1386. }
  1387. /* Flush the FIFOs */
  1388. dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  1389. dwc_otg_flush_rx_fifo(core_if);
  1390. return retval;
  1391. }
  1392. /**
  1393. * This function sets a new value for the buffer Alignment setup.
  1394. */
  1395. static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  1396. {
  1397. int retval;
  1398. uint32_t fsiz;
  1399. uint16_t size;
  1400. uint16_t ep_addr;
  1401. dwc_otg_pcd_ep_t *ep;
  1402. dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  1403. tx_fifo_size_setup_t *ptxfifoval;
  1404. ptxfifoval = (tx_fifo_size_setup_t *) buf;
  1405. ep_addr = ptxfifoval->bEndpointAddress;
  1406. size = ptxfifoval->wDepth;
  1407. ep = get_ep_by_addr(pcd, ep_addr);
  1408. CFI_INFO
  1409. ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  1410. __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  1411. if (NULL == ep) {
  1412. CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  1413. __func__, ep_addr);
  1414. return -DWC_E_INVALID;
  1415. }
  1416. fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  1417. params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  1418. if (resize_fifos(GET_CORE_IF(pcd))) {
  1419. retval = 0;
  1420. } else {
  1421. CFI_INFO
  1422. ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  1423. __func__, ep_addr);
  1424. params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  1425. retval = -DWC_E_INVALID;
  1426. }
  1427. return retval;
  1428. }
  1429. /**
  1430. * This function sets a new value for the buffer Alignment setup.
  1431. */
  1432. static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  1433. {
  1434. int retval;
  1435. uint32_t fsiz;
  1436. uint16_t size;
  1437. dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  1438. rx_fifo_size_setup_t *prxfifoval;
  1439. prxfifoval = (rx_fifo_size_setup_t *) buf;
  1440. size = prxfifoval->wDepth;
  1441. fsiz = params->dev_rx_fifo_size;
  1442. params->dev_rx_fifo_size = size;
  1443. if (resize_fifos(GET_CORE_IF(pcd))) {
  1444. retval = 0;
  1445. } else {
  1446. CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  1447. __func__);
  1448. params->dev_rx_fifo_size = fsiz;
  1449. retval = -DWC_E_INVALID;
  1450. }
  1451. return retval;
  1452. }
  1453. /**
  1454. * This function reads the SG of an EP's buffer setup into the buffer buf
  1455. */
  1456. static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  1457. struct cfi_usb_ctrlrequest *req)
  1458. {
  1459. int retval = -DWC_E_INVALID;
  1460. uint8_t addr;
  1461. cfi_ep_t *ep;
  1462. /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  1463. addr = req->wValue & 0xFF;
  1464. if (addr == 0) /* The address should be non-zero */
  1465. return retval;
  1466. ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  1467. if (NULL == ep) {
  1468. CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  1469. __func__, addr);
  1470. return retval;
  1471. }
  1472. dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  1473. retval = BS_SG_VAL_DESC_LEN;
  1474. return retval;
  1475. }
  1476. /**
  1477. * This function reads the Concatenation value of an EP's buffer mode into
  1478. * the buffer buf
  1479. */
  1480. static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  1481. struct cfi_usb_ctrlrequest *req)
  1482. {
  1483. int retval = -DWC_E_INVALID;
  1484. uint8_t addr;
  1485. cfi_ep_t *ep;
  1486. uint8_t desc_count;
  1487. /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  1488. addr = req->wValue & 0xFF;
  1489. if (addr == 0) /* The address should be non-zero */
  1490. return retval;
  1491. ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  1492. if (NULL == ep) {
  1493. CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  1494. __func__, addr);
  1495. return retval;
  1496. }
  1497. /* Copy the header to the buffer */
  1498. dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  1499. /* Advance the buffer pointer by the header size */
  1500. buf += BS_CONCAT_VAL_HDR_LEN;
  1501. desc_count = ep->bm_concat->hdr.bDescCount;
  1502. /* Copy alll the wTxBytes to the buffer */
  1503. dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  1504. retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  1505. return retval;
  1506. }
  1507. /**
  1508. * This function reads the buffer Alignment value of an EP's buffer mode into
  1509. * the buffer buf
  1510. *
  1511. * @return The total number of bytes copied to the buffer or negative error code.
  1512. */
  1513. static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  1514. struct cfi_usb_ctrlrequest *req)
  1515. {
  1516. int retval = -DWC_E_INVALID;
  1517. uint8_t addr;
  1518. cfi_ep_t *ep;
  1519. /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  1520. addr = req->wValue & 0xFF;
  1521. if (addr == 0) /* The address should be non-zero */
  1522. return retval;
  1523. ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  1524. if (NULL == ep) {
  1525. CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  1526. __func__, addr);
  1527. return retval;
  1528. }
  1529. dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  1530. retval = BS_ALIGN_VAL_HDR_LEN;
  1531. return retval;
  1532. }
  1533. /**
  1534. * This function sets a new value for the specified feature
  1535. *
  1536. * @param pcd A pointer to the PCD object
  1537. *
  1538. * @return 0 if successful, negative error code otherwise to stall the DCE.
  1539. */
  1540. static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  1541. {
  1542. int retval = -DWC_E_NOT_SUPPORTED;
  1543. uint16_t wIndex, wValue;
  1544. uint8_t bRequest;
  1545. struct dwc_otg_core_if *coreif;
  1546. cfiobject_t *cfi = pcd->cfi;
  1547. struct cfi_usb_ctrlrequest *ctrl_req;
  1548. uint8_t *buf;
  1549. ctrl_req = &cfi->ctrl_req;
  1550. buf = pcd->cfi->ctrl_req.data;
  1551. coreif = GET_CORE_IF(pcd);
  1552. bRequest = ctrl_req->bRequest;
  1553. wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  1554. wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  1555. /* See which feature is to be modified */
  1556. switch (wIndex) {
  1557. case FT_ID_DMA_BUFFER_SETUP:
  1558. /* Modify the feature */
  1559. if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  1560. return retval;
  1561. /* And send this request to the gadget */
  1562. cfi->need_gadget_att = 1;
  1563. break;
  1564. case FT_ID_DMA_BUFF_ALIGN:
  1565. if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  1566. return retval;
  1567. cfi->need_gadget_att = 1;
  1568. break;
  1569. case FT_ID_DMA_CONCAT_SETUP:
  1570. /* Modify the feature */
  1571. if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  1572. return retval;
  1573. cfi->need_gadget_att = 1;
  1574. break;
  1575. case FT_ID_DMA_CIRCULAR:
  1576. CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  1577. break;
  1578. case FT_ID_THRESHOLD_SETUP:
  1579. CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  1580. break;
  1581. case FT_ID_DFIFO_DEPTH:
  1582. CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  1583. break;
  1584. case FT_ID_TX_FIFO_DEPTH:
  1585. CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  1586. if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  1587. return retval;
  1588. cfi->need_gadget_att = 0;
  1589. break;
  1590. case FT_ID_RX_FIFO_DEPTH:
  1591. CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  1592. if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  1593. return retval;
  1594. cfi->need_gadget_att = 0;
  1595. break;
  1596. }
  1597. return retval;
  1598. }
  1599. #endif //DWC_UTE_CFI