core.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  1. /*
  2. * File : core.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-10-01 Yi Qiu first version
  23. * 2012-12-12 heyuanjie87 change endpoint and function handler
  24. * 2012-12-30 heyuanjie87 change inferface handler
  25. * 2013-04-26 aozima add DEVICEQUALIFIER support.
  26. * 2013-07-25 Yi Qiu update for USB CV test
  27. * 2017-11-15 ZYH fix ep0 transform error
  28. */
  29. #include <rtthread.h>
  30. #include "drivers/usb_common.h"
  31. #include "drivers/usb_device.h"
  32. static rt_list_t device_list;
  33. static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
  34. static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
  35. static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep);
  36. static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep);
  37. /**
  38. * This function will handle get_device_descriptor bRequest.
  39. *
  40. * @param device the usb device object.
  41. * @param setup the setup bRequest.
  42. *
  43. * @return RT_EOK on successful.
  44. */
  45. static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
  46. {
  47. rt_size_t size;
  48. /* parameter check */
  49. RT_ASSERT(device != RT_NULL);
  50. RT_ASSERT(setup != RT_NULL);
  51. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
  52. /* device descriptor wLength should less than USB_DESC_LENGTH_DEVICE*/
  53. size = (setup->wLength > USB_DESC_LENGTH_DEVICE) ?
  54. USB_DESC_LENGTH_DEVICE : setup->wLength;
  55. /* send device descriptor to endpoint 0 */
  56. rt_usbd_ep0_write(device, (rt_uint8_t*) &device->dev_desc, size);
  57. return RT_EOK;
  58. }
  59. /**
  60. * This function will handle get_config_descriptor bRequest.
  61. *
  62. * @param device the usb device object.
  63. * @param setup the setup bRequest.
  64. *
  65. * @return RT_EOK on successful.
  66. */
  67. static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
  68. {
  69. rt_size_t size;
  70. ucfg_desc_t cfg_desc;
  71. /* parameter check */
  72. RT_ASSERT(device != RT_NULL);
  73. RT_ASSERT(setup != RT_NULL);
  74. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
  75. cfg_desc = &device->curr_cfg->cfg_desc;
  76. size = (setup->wLength > cfg_desc->wTotalLength) ?
  77. cfg_desc->wTotalLength : setup->wLength;
  78. /* send configuration descriptor to endpoint 0 */
  79. rt_usbd_ep0_write(device, (rt_uint8_t*)cfg_desc, size);
  80. return RT_EOK;
  81. }
  82. /**
  83. * This function will handle get_string_descriptor bRequest.
  84. *
  85. * @param device the usb device object.
  86. * @param setup the setup bRequest.
  87. *
  88. * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
  89. */
  90. static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
  91. {
  92. struct ustring_descriptor str_desc;
  93. rt_uint8_t index, i;
  94. rt_uint32_t len;
  95. /* parameter check */
  96. RT_ASSERT(device != RT_NULL);
  97. RT_ASSERT(setup != RT_NULL);
  98. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
  99. str_desc.type = USB_DESC_TYPE_STRING;
  100. index = setup->wValue & 0xFF;
  101. if(index == 0xEE)
  102. {
  103. index = USB_STRING_OS_INDEX;
  104. }
  105. if(index > USB_STRING_MAX)
  106. {
  107. rt_kprintf("unknown string index\n");
  108. rt_usbd_ep0_set_stall(device);
  109. return -RT_ERROR;
  110. }
  111. else if(index == USB_STRING_LANGID_INDEX)
  112. {
  113. str_desc.bLength = 4;
  114. str_desc.String[0] = 0x09;
  115. str_desc.String[1] = 0x04;
  116. }
  117. else
  118. {
  119. len = rt_strlen(device->str[index]);
  120. str_desc.bLength = len*2 + 2;
  121. for(i=0; i<len; i++)
  122. {
  123. str_desc.String[i*2] = device->str[index][i];
  124. str_desc.String[i*2 + 1] = 0;
  125. }
  126. }
  127. if (setup->wLength > str_desc.bLength)
  128. len = str_desc.bLength;
  129. else
  130. len = setup->wLength;
  131. /* send string descriptor to endpoint 0 */
  132. rt_usbd_ep0_write(device, (rt_uint8_t*)&str_desc, len);
  133. return RT_EOK;
  134. }
  135. static rt_err_t _get_qualifier_descriptor(struct udevice* device, ureq_t setup)
  136. {
  137. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_qualifier_descriptor\n"));
  138. /* parameter check */
  139. RT_ASSERT(device != RT_NULL);
  140. RT_ASSERT(setup != RT_NULL);
  141. if(device->dev_qualifier)
  142. {
  143. /* send device qualifier descriptor to endpoint 0 */
  144. rt_usbd_ep0_write(device, (rt_uint8_t*)device->dev_qualifier,
  145. sizeof(struct usb_qualifier_descriptor));
  146. }
  147. else
  148. {
  149. rt_usbd_ep0_set_stall(device);
  150. }
  151. return RT_EOK;
  152. }
  153. /**
  154. * This function will handle get_descriptor bRequest.
  155. *
  156. * @param device the usb device object.
  157. * @param setup the setup bRequest.
  158. *
  159. * @return RT_EOK on successful.
  160. */
  161. static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
  162. {
  163. /* parameter check */
  164. RT_ASSERT(device != RT_NULL);
  165. RT_ASSERT(setup != RT_NULL);
  166. if(setup->request_type == USB_REQ_TYPE_DIR_IN)
  167. {
  168. switch(setup->wValue >> 8)
  169. {
  170. case USB_DESC_TYPE_DEVICE:
  171. _get_device_descriptor(device, setup);
  172. break;
  173. case USB_DESC_TYPE_CONFIGURATION:
  174. _get_config_descriptor(device, setup);
  175. break;
  176. case USB_DESC_TYPE_STRING:
  177. _get_string_descriptor(device, setup);
  178. break;
  179. case USB_DESC_TYPE_DEVICEQUALIFIER:
  180. _get_qualifier_descriptor(device, setup);
  181. break;
  182. default:
  183. rt_kprintf("unsupported descriptor request\n");
  184. rt_usbd_ep0_set_stall(device);
  185. break;
  186. }
  187. }
  188. else
  189. {
  190. rt_kprintf("request direction error\n");
  191. rt_usbd_ep0_set_stall(device);
  192. }
  193. return RT_EOK;
  194. }
  195. /**
  196. * This function will handle get_interface bRequest.
  197. *
  198. * @param device the usb device object.
  199. * @param setup the setup bRequest.
  200. *
  201. * @return RT_EOK on successful.
  202. */
  203. static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
  204. {
  205. rt_uint8_t value;
  206. uintf_t intf;
  207. /* parameter check */
  208. RT_ASSERT(device != RT_NULL);
  209. RT_ASSERT(setup != RT_NULL);
  210. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_interface\n"));
  211. if (device->state != USB_STATE_CONFIGURED)
  212. {
  213. rt_usbd_ep0_set_stall(device);
  214. return -RT_ERROR;
  215. }
  216. /* find the specified interface and its alternate setting */
  217. intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
  218. value = intf->curr_setting->intf_desc->bAlternateSetting;
  219. /* send the interface alternate setting to endpoint 0*/
  220. rt_usbd_ep0_write(device, &value, 1);
  221. return RT_EOK;
  222. }
  223. /**
  224. * This function will handle set_interface bRequest.
  225. *
  226. * @param device the usb device object.
  227. * @param setup the setup bRequest.
  228. *
  229. * @return RT_EOK on successful.
  230. */
  231. static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
  232. {
  233. uintf_t intf;
  234. uep_t ep;
  235. struct rt_list_node* i;
  236. ualtsetting_t setting;
  237. /* parameter check */
  238. RT_ASSERT(device != RT_NULL);
  239. RT_ASSERT(setup != RT_NULL);
  240. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));
  241. if (device->state != USB_STATE_CONFIGURED)
  242. {
  243. rt_usbd_ep0_set_stall(device);
  244. return -RT_ERROR;
  245. }
  246. /* find the specified interface */
  247. intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
  248. /* set alternate setting to the interface */
  249. rt_usbd_set_altsetting(intf, setup->wValue & 0xFF);
  250. setting = intf->curr_setting;
  251. /* start all endpoints of the interface alternate setting */
  252. for(i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
  253. {
  254. ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
  255. dcd_ep_disable(device->dcd, ep);
  256. dcd_ep_enable(device->dcd, ep);
  257. }
  258. dcd_ep0_send_status(device->dcd);
  259. return RT_EOK;
  260. }
  261. /**
  262. * This function will handle get_config bRequest.
  263. *
  264. * @param device the usb device object.
  265. * @param setup the setup bRequest.
  266. *
  267. * @return RT_EOK on successful.
  268. */
  269. static rt_err_t _get_config(struct udevice* device, ureq_t setup)
  270. {
  271. rt_uint8_t value;
  272. /* parameter check */
  273. RT_ASSERT(device != RT_NULL);
  274. RT_ASSERT(setup != RT_NULL);
  275. RT_ASSERT(device->curr_cfg != RT_NULL);
  276. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
  277. if (device->state == USB_STATE_CONFIGURED)
  278. {
  279. /* get current configuration */
  280. value = device->curr_cfg->cfg_desc.bConfigurationValue;
  281. }
  282. else
  283. {
  284. value = 0;
  285. }
  286. /* write the current configuration to endpoint 0 */
  287. rt_usbd_ep0_write(device, &value, 1);
  288. return RT_EOK;
  289. }
  290. /**
  291. * This function will handle set_config bRequest.
  292. *
  293. * @param device the usb device object.
  294. * @param setup the setup bRequest.
  295. *
  296. * @return RT_EOK on successful.
  297. */
  298. static rt_err_t _set_config(struct udevice* device, ureq_t setup)
  299. {
  300. struct rt_list_node *i, *j, *k;
  301. uconfig_t cfg;
  302. uintf_t intf;
  303. ualtsetting_t setting;
  304. uep_t ep;
  305. /* parameter check */
  306. RT_ASSERT(device != RT_NULL);
  307. RT_ASSERT(setup != RT_NULL);
  308. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
  309. if (setup->wValue > device->dev_desc.bNumConfigurations)
  310. {
  311. rt_usbd_ep0_set_stall(device);
  312. return -RT_ERROR;
  313. }
  314. if (setup->wValue == 0)
  315. {
  316. RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
  317. device->state = USB_STATE_ADDRESS;
  318. goto _exit;
  319. }
  320. /* set current configuration */
  321. rt_usbd_set_config(device, setup->wValue);
  322. cfg = device->curr_cfg;
  323. for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
  324. {
  325. /* run all functiones and their endpoints in the configuration */
  326. ufunction_t func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  327. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  328. {
  329. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  330. setting = intf->curr_setting;
  331. for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
  332. {
  333. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  334. /* first disable then enable an endpoint */
  335. dcd_ep_disable(device->dcd, ep);
  336. dcd_ep_enable(device->dcd, ep);
  337. }
  338. }
  339. /* after enabled endpoints, then enable function */
  340. FUNC_ENABLE(func);
  341. }
  342. device->state = USB_STATE_CONFIGURED;
  343. _exit:
  344. /* issue status stage */
  345. dcd_ep0_send_status(device->dcd);
  346. return RT_EOK;
  347. }
  348. /**
  349. * This function will handle set_address bRequest.
  350. *
  351. * @param device the usb device object.
  352. * @param setup the setup bRequest.
  353. *
  354. * @return RT_EOK on successful.
  355. */
  356. static rt_err_t _set_address(struct udevice* device, ureq_t setup)
  357. {
  358. /* parameter check */
  359. RT_ASSERT(device != RT_NULL);
  360. RT_ASSERT(setup != RT_NULL);
  361. /* set address in device control driver */
  362. dcd_set_address(device->dcd, setup->wValue);
  363. /* issue status stage */
  364. dcd_ep0_send_status(device->dcd);
  365. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
  366. device->state = USB_STATE_ADDRESS;
  367. return RT_EOK;
  368. }
  369. /**
  370. * This function will handle standard bRequest to
  371. * interface that defined in function-specifics
  372. *
  373. * @param device the usb device object.
  374. * @param setup the setup bRequest.
  375. *
  376. * @return RT_EOK on successful.
  377. */
  378. static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
  379. {
  380. uintf_t intf;
  381. ufunction_t func;
  382. rt_err_t ret;
  383. /* parameter check */
  384. RT_ASSERT(device != RT_NULL);
  385. RT_ASSERT(setup != RT_NULL);
  386. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
  387. intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
  388. if (intf != RT_NULL)
  389. {
  390. ret = intf->handler(func, setup);
  391. }
  392. else
  393. {
  394. ret = -RT_ERROR;
  395. }
  396. return ret;
  397. }
  398. /**
  399. * This function will handle standard bRequest.
  400. *
  401. * @param device the usb device object.
  402. * @param setup the setup bRequest.
  403. *
  404. * @return RT_EOK on successful.
  405. */
  406. static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
  407. {
  408. udcd_t dcd;
  409. rt_uint16_t value = 0;
  410. /* parameter check */
  411. RT_ASSERT(device != RT_NULL);
  412. RT_ASSERT(setup != RT_NULL);
  413. dcd = device->dcd;
  414. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  415. {
  416. case USB_REQ_TYPE_DEVICE:
  417. switch(setup->bRequest)
  418. {
  419. case USB_REQ_GET_STATUS:
  420. rt_usbd_ep0_write(device, &value, 2);
  421. break;
  422. case USB_REQ_CLEAR_FEATURE:
  423. rt_usbd_clear_feature(device, setup->wValue, setup->wIndex);
  424. dcd_ep0_send_status(dcd);
  425. break;
  426. case USB_REQ_SET_FEATURE:
  427. rt_usbd_set_feature(device, setup->wValue, setup->wIndex);
  428. break;
  429. case USB_REQ_SET_ADDRESS:
  430. _set_address(device, setup);
  431. break;
  432. case USB_REQ_GET_DESCRIPTOR:
  433. _get_descriptor(device, setup);
  434. break;
  435. case USB_REQ_SET_DESCRIPTOR:
  436. rt_usbd_ep0_set_stall(device);
  437. break;
  438. case USB_REQ_GET_CONFIGURATION:
  439. _get_config(device, setup);
  440. break;
  441. case USB_REQ_SET_CONFIGURATION:
  442. _set_config(device, setup);
  443. break;
  444. default:
  445. rt_kprintf("unknown device request\n");
  446. rt_usbd_ep0_set_stall(device);
  447. break;
  448. }
  449. break;
  450. case USB_REQ_TYPE_INTERFACE:
  451. switch(setup->bRequest)
  452. {
  453. case USB_REQ_GET_INTERFACE:
  454. _get_interface(device, setup);
  455. break;
  456. case USB_REQ_SET_INTERFACE:
  457. _set_interface(device, setup);
  458. break;
  459. default:
  460. if (_request_interface(device, setup) != RT_EOK)
  461. {
  462. rt_kprintf("unknown interface request\n");
  463. rt_usbd_ep0_set_stall(device);
  464. return - RT_ERROR;
  465. }
  466. else
  467. break;
  468. }
  469. break;
  470. case USB_REQ_TYPE_ENDPOINT:
  471. switch(setup->bRequest)
  472. {
  473. case USB_REQ_GET_STATUS:
  474. {
  475. uep_t ep;
  476. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
  477. value = ep->stalled;
  478. rt_usbd_ep0_write(device, &value, 2);
  479. }
  480. break;
  481. case USB_REQ_CLEAR_FEATURE:
  482. {
  483. uep_t ep;
  484. uio_request_t req;
  485. struct rt_list_node *node;
  486. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
  487. if(USB_EP_HALT == setup->wValue && ep->stalled == RT_TRUE)
  488. {
  489. rt_usbd_clear_feature(device, setup->wValue, setup->wIndex);
  490. dcd_ep0_send_status(dcd);
  491. ep->stalled = RT_FALSE;
  492. for (node = ep->request_list.next; node != &ep->request_list; node = node->next)
  493. {
  494. req = (uio_request_t)rt_list_entry(node, struct uio_request, list);
  495. rt_usbd_io_request(device, ep, req);
  496. RT_DEBUG_LOG(RT_DEBUG_USB, ("fired a request\n"));
  497. }
  498. rt_list_init(&ep->request_list);
  499. }
  500. }
  501. break;
  502. case USB_REQ_SET_FEATURE:
  503. {
  504. uep_t ep;
  505. if(USB_EP_HALT == setup->wValue)
  506. {
  507. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->wIndex);
  508. ep->stalled = RT_TRUE;
  509. rt_usbd_set_feature(device, setup->wValue, setup->wIndex);
  510. dcd_ep0_send_status(dcd);
  511. }
  512. }
  513. break;
  514. case USB_REQ_SYNCH_FRAME:
  515. break;
  516. default:
  517. rt_kprintf("unknown endpoint request\n");
  518. rt_usbd_ep0_set_stall(device);
  519. break;
  520. }
  521. break;
  522. case USB_REQ_TYPE_OTHER:
  523. rt_kprintf("unknown other type request\n");
  524. rt_usbd_ep0_set_stall(device);
  525. break;
  526. default:
  527. rt_kprintf("unknown type request\n");
  528. rt_usbd_ep0_set_stall(device);
  529. break;
  530. }
  531. return RT_EOK;
  532. }
  533. /**
  534. * This function will handle function bRequest.
  535. *
  536. * @param device the usb device object.
  537. * @param setup the setup bRequest.
  538. *
  539. * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
  540. */
  541. static rt_err_t _function_request(udevice_t device, ureq_t setup)
  542. {
  543. uintf_t intf;
  544. ufunction_t func;
  545. /* parameter check */
  546. RT_ASSERT(device != RT_NULL);
  547. RT_ASSERT(setup != RT_NULL);
  548. /* verify bRequest wValue */
  549. if(setup->wIndex > device->curr_cfg->cfg_desc.bNumInterfaces)
  550. {
  551. rt_usbd_ep0_set_stall(device);
  552. return -RT_ERROR;
  553. }
  554. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  555. {
  556. case USB_REQ_TYPE_INTERFACE:
  557. intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
  558. if(intf == RT_NULL)
  559. {
  560. rt_kprintf("unkwown interface request\n");
  561. rt_usbd_ep0_set_stall(device);
  562. }
  563. else
  564. {
  565. intf->handler(func, setup);
  566. }
  567. break;
  568. case USB_REQ_TYPE_ENDPOINT:
  569. break;
  570. default:
  571. rt_kprintf("unknown function request type\n");
  572. rt_usbd_ep0_set_stall(device);
  573. break;
  574. }
  575. return RT_EOK;
  576. }
  577. static rt_err_t _vendor_request(udevice_t device, ureq_t setup)
  578. {
  579. static rt_uint8_t * usb_comp_id_desc = RT_NULL;
  580. static rt_uint32_t usb_comp_id_desc_size = 0;
  581. usb_os_func_comp_id_desc_t func_comp_id_desc;
  582. uintf_t intf;
  583. ufunction_t func;
  584. switch(setup->bRequest)
  585. {
  586. case 'A':
  587. switch(setup->wIndex)
  588. {
  589. case 0x04:
  590. if(rt_list_len(&device->os_comp_id_desc->func_desc) == 0)
  591. {
  592. rt_usbd_ep0_set_stall(device);
  593. return RT_EOK;
  594. }
  595. if(usb_comp_id_desc == RT_NULL)
  596. {
  597. rt_uint8_t * pusb_comp_id_desc;
  598. rt_list_t *p;
  599. usb_comp_id_desc_size = sizeof(struct usb_os_header_comp_id_descriptor) +
  600. (sizeof(struct usb_os_function_comp_id_descriptor)-sizeof(rt_list_t))*rt_list_len(&device->os_comp_id_desc->func_desc);
  601. usb_comp_id_desc = (rt_uint8_t *)rt_malloc(usb_comp_id_desc_size);
  602. RT_ASSERT(usb_comp_id_desc != RT_NULL);
  603. device->os_comp_id_desc->head_desc.dwLength = usb_comp_id_desc_size;
  604. pusb_comp_id_desc = usb_comp_id_desc;
  605. rt_memcpy((void *)pusb_comp_id_desc,(void *)&device->os_comp_id_desc->head_desc,sizeof(struct usb_os_header_comp_id_descriptor));
  606. pusb_comp_id_desc += sizeof(struct usb_os_header_comp_id_descriptor);
  607. for (p = device->os_comp_id_desc->func_desc.next; p != &device->os_comp_id_desc->func_desc; p = p->next)
  608. {
  609. func_comp_id_desc = rt_list_entry(p,struct usb_os_function_comp_id_descriptor,list);
  610. rt_memcpy(pusb_comp_id_desc,(void *)&func_comp_id_desc->bFirstInterfaceNumber,
  611. sizeof(struct usb_os_function_comp_id_descriptor)-sizeof(rt_list_t));
  612. pusb_comp_id_desc += sizeof(struct usb_os_function_comp_id_descriptor)-sizeof(rt_list_t);
  613. }
  614. }
  615. rt_usbd_ep0_write(device, (void*)usb_comp_id_desc, setup->wLength);
  616. break;
  617. case 0x05:
  618. intf = rt_usbd_find_interface(device, setup->wValue & 0xFF, &func);
  619. if(intf != RT_NULL)
  620. {
  621. intf->handler(func, setup);
  622. }
  623. break;
  624. }
  625. break;
  626. }
  627. return RT_EOK;
  628. }
  629. static rt_err_t _dump_setup_packet(ureq_t setup)
  630. {
  631. RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
  632. RT_DEBUG_LOG(RT_DEBUG_USB, (" setup_request : 0x%x\n",
  633. setup->request_type));
  634. RT_DEBUG_LOG(RT_DEBUG_USB, (" value : 0x%x\n", setup->wValue));
  635. RT_DEBUG_LOG(RT_DEBUG_USB, (" length : 0x%x\n", setup->wLength));
  636. RT_DEBUG_LOG(RT_DEBUG_USB, (" index : 0x%x\n", setup->wIndex));
  637. RT_DEBUG_LOG(RT_DEBUG_USB, (" request : 0x%x\n", setup->bRequest));
  638. RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
  639. return RT_EOK;
  640. }
  641. /**
  642. * This function will handle setup bRequest.
  643. *
  644. * @param device the usb device object.
  645. * @param setup the setup bRequest.
  646. *
  647. * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
  648. */
  649. static rt_err_t _setup_request(udevice_t device, ureq_t setup)
  650. {
  651. /* parameter check */
  652. RT_ASSERT(device != RT_NULL);
  653. RT_ASSERT(setup != RT_NULL);
  654. _dump_setup_packet(setup);
  655. switch((setup->request_type & USB_REQ_TYPE_MASK))
  656. {
  657. case USB_REQ_TYPE_STANDARD:
  658. _standard_request(device, setup);
  659. break;
  660. case USB_REQ_TYPE_CLASS:
  661. _function_request(device, setup);
  662. break;
  663. case USB_REQ_TYPE_VENDOR:
  664. _vendor_request(device, setup);
  665. break;
  666. default:
  667. rt_kprintf("unknown setup request type\n");
  668. rt_usbd_ep0_set_stall(device);
  669. return -RT_ERROR;
  670. }
  671. return RT_EOK;
  672. }
  673. /**
  674. * This function will hanle data notify event.
  675. *
  676. * @param device the usb device object.
  677. * @param ep_msg the endpoint message.
  678. *
  679. * @return RT_EOK.
  680. */
  681. static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg)
  682. {
  683. uep_t ep;
  684. ufunction_t func;
  685. rt_size_t size = 0;
  686. RT_ASSERT(device != RT_NULL);
  687. RT_ASSERT(ep_msg != RT_NULL);
  688. if (device->state != USB_STATE_CONFIGURED)
  689. {
  690. return -RT_ERROR;
  691. }
  692. ep = rt_usbd_find_endpoint(device, &func, ep_msg->ep_addr);
  693. if(ep == RT_NULL)
  694. {
  695. rt_kprintf("invalid endpoint\n");
  696. return -RT_ERROR;
  697. }
  698. if(EP_ADDRESS(ep) & USB_DIR_IN)
  699. {
  700. size = ep_msg->size;
  701. if(ep->request.remain_size >= EP_MAXPACKET(ep))
  702. {
  703. dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, EP_MAXPACKET(ep));
  704. ep->request.remain_size -= EP_MAXPACKET(ep);
  705. ep->request.buffer += EP_MAXPACKET(ep);
  706. }
  707. else if(ep->request.remain_size > 0)
  708. {
  709. dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size);
  710. ep->request.remain_size = 0;
  711. }
  712. else
  713. {
  714. EP_HANDLER(ep, func, size);
  715. }
  716. }
  717. else
  718. {
  719. size = ep_msg->size;
  720. if(ep->request.remain_size == 0)
  721. {
  722. return RT_EOK;
  723. }
  724. if(size == 0)
  725. {
  726. size = dcd_ep_read(device->dcd, EP_ADDRESS(ep), ep->request.buffer);
  727. }
  728. ep->request.remain_size -= size;
  729. ep->request.buffer += size;
  730. if(ep->request.req_type == UIO_REQUEST_READ_BEST)
  731. {
  732. EP_HANDLER(ep, func, size);
  733. }
  734. else if(ep->request.remain_size == 0)
  735. {
  736. EP_HANDLER(ep, func, ep->request.size);
  737. }
  738. else
  739. {
  740. dcd_ep_read_prepare(device->dcd, EP_ADDRESS(ep), ep->request.buffer, ep->request.remain_size > EP_MAXPACKET(ep) ? EP_MAXPACKET(ep) : ep->request.remain_size);
  741. }
  742. }
  743. return RT_EOK;
  744. }
  745. static rt_err_t _ep0_out_notify(udevice_t device, struct ep_msg* ep_msg)
  746. {
  747. uep_t ep0;
  748. rt_size_t size;
  749. RT_ASSERT(device != RT_NULL);
  750. RT_ASSERT(ep_msg != RT_NULL);
  751. RT_ASSERT(device->dcd != RT_NULL);
  752. ep0 = &device->dcd->ep0;
  753. size = ep_msg->size;
  754. if(ep0->request.remain_size == 0)
  755. {
  756. return RT_EOK;
  757. }
  758. if(size == 0)
  759. {
  760. size = dcd_ep_read(device->dcd, EP0_OUT_ADDR, ep0->request.buffer);
  761. if(size == 0)
  762. {
  763. return RT_EOK;
  764. }
  765. }
  766. ep0->request.remain_size -= size;
  767. ep0->request.buffer += size;
  768. if(ep0->request.remain_size == 0)
  769. {
  770. /* invoke callback */
  771. if(ep0->rx_indicate != RT_NULL)
  772. {
  773. ep0->rx_indicate(device, size);
  774. }
  775. }
  776. else
  777. {
  778. rt_usbd_ep0_read(device, ep0->request.buffer, ep0->request.remain_size,ep0->rx_indicate);
  779. }
  780. return RT_EOK;
  781. }
  782. /**
  783. * This function will notity sof event to all of function.
  784. *
  785. * @param device the usb device object.
  786. *
  787. * @return RT_EOK.
  788. */
  789. static rt_err_t _sof_notify(udevice_t device)
  790. {
  791. struct rt_list_node *i;
  792. ufunction_t func;
  793. RT_ASSERT(device != RT_NULL);
  794. /* to notity every function that sof event comes */
  795. for (i=device->curr_cfg->func_list.next;
  796. i!=&device->curr_cfg->func_list; i=i->next)
  797. {
  798. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  799. if(func->ops->sof_handler != RT_NULL)
  800. func->ops->sof_handler(func);
  801. }
  802. return RT_EOK;
  803. }
  804. /**
  805. * This function will disable all USB functions.
  806. *
  807. * @param device the usb device object.
  808. *
  809. * @return RT_EOK.
  810. */
  811. static rt_err_t _stop_notify(udevice_t device)
  812. {
  813. struct rt_list_node *i;
  814. ufunction_t func;
  815. RT_ASSERT(device != RT_NULL);
  816. /* to notity every function */
  817. for (i = device->curr_cfg->func_list.next;
  818. i != &device->curr_cfg->func_list;
  819. i = i->next)
  820. {
  821. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  822. FUNC_DISABLE(func);
  823. }
  824. return RT_EOK;
  825. }
  826. static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
  827. {
  828. rt_uint16_t maxpacket;
  829. RT_ASSERT(device != RT_NULL);
  830. RT_ASSERT(device->dcd != RT_NULL);
  831. RT_ASSERT(ep != RT_NULL);
  832. maxpacket = EP_MAXPACKET(ep);
  833. if(ep->request.remain_size >= maxpacket)
  834. {
  835. dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket);
  836. ep->request.remain_size -= maxpacket;
  837. ep->request.buffer += maxpacket;
  838. }
  839. else
  840. {
  841. dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer,
  842. ep->request.remain_size);
  843. ep->request.remain_size = 0;
  844. }
  845. return size;
  846. }
  847. static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
  848. {
  849. RT_ASSERT(device != RT_NULL);
  850. RT_ASSERT(device->dcd != RT_NULL);
  851. RT_ASSERT(ep != RT_NULL);
  852. RT_ASSERT(buffer != RT_NULL);
  853. RT_ASSERT(ep->ep_desc != RT_NULL);
  854. return dcd_ep_read_prepare(device->dcd, EP_ADDRESS(ep), buffer, size > EP_MAXPACKET(ep) ? EP_MAXPACKET(ep) : size);
  855. }
  856. /**
  857. * This function will create an usb device object.
  858. *
  859. * @param ustring the usb string array to contain string descriptor.
  860. *
  861. * @return an usb device object on success, RT_NULL on fail.
  862. */
  863. udevice_t rt_usbd_device_new(void)
  864. {
  865. udevice_t udevice;
  866. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_new\n"));
  867. /* allocate memory for the object */
  868. udevice = rt_malloc(sizeof(struct udevice));
  869. if(udevice == RT_NULL)
  870. {
  871. rt_kprintf("alloc memery failed\n");
  872. return RT_NULL;
  873. }
  874. rt_memset(udevice, 0, sizeof(struct udevice));
  875. /* to initialize configuration list */
  876. rt_list_init(&udevice->cfg_list);
  877. /* insert the device object to device list */
  878. rt_list_insert_before(&device_list, &udevice->list);
  879. return udevice;
  880. }
  881. /**
  882. * This function will set usb device string description.
  883. *
  884. * @param device the usb device object.
  885. * @param ustring pointer to string pointer array.
  886. *
  887. * @return RT_EOK.
  888. */
  889. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  890. {
  891. /* parameter check */
  892. RT_ASSERT(device != RT_NULL);
  893. RT_ASSERT(ustring != RT_NULL);
  894. /* set string descriptor array to the device object */
  895. device->str = ustring;
  896. return RT_EOK;
  897. }
  898. rt_err_t rt_usbd_device_set_os_comp_id_desc(udevice_t device, usb_os_comp_id_desc_t os_comp_id_desc)
  899. {
  900. /* parameter check */
  901. RT_ASSERT(device != RT_NULL);
  902. RT_ASSERT(os_comp_id_desc != RT_NULL);
  903. /* set string descriptor array to the device object */
  904. device->os_comp_id_desc = os_comp_id_desc;
  905. rt_list_init(&device->os_comp_id_desc->func_desc);
  906. return RT_EOK;
  907. }
  908. rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier)
  909. {
  910. /* parameter check */
  911. RT_ASSERT(device != RT_NULL);
  912. RT_ASSERT(qualifier != RT_NULL);
  913. device->dev_qualifier = qualifier;
  914. return RT_EOK;
  915. }
  916. /**
  917. * This function will set an usb controller driver to a device.
  918. *
  919. * @param device the usb device object.
  920. * @param dcd the usb device controller driver.
  921. *
  922. * @return RT_EOK on successful.
  923. */
  924. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  925. {
  926. /* parameter check */
  927. RT_ASSERT(device != RT_NULL);
  928. RT_ASSERT(dcd != RT_NULL);
  929. /* set usb device controller driver to the device */
  930. device->dcd = dcd;
  931. return RT_EOK;
  932. }
  933. /**
  934. * This function will set an usb device descriptor to a device.
  935. *
  936. * @param device the usb device object.
  937. * @param dev_desc the usb device descriptor.
  938. *
  939. * @return RT_EOK on successful.
  940. */
  941. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  942. {
  943. /* parameter check */
  944. RT_ASSERT(device != RT_NULL);
  945. RT_ASSERT(dev_desc != RT_NULL);
  946. /* copy the usb device descriptor to the device */
  947. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  948. return RT_EOK;
  949. }
  950. /**
  951. * This function will create an usb configuration object.
  952. *
  953. * @param none.
  954. *
  955. * @return an usb configuration object.
  956. */
  957. uconfig_t rt_usbd_config_new(void)
  958. {
  959. uconfig_t cfg;
  960. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_new\n"));
  961. /* allocate memory for the object */
  962. cfg = rt_malloc(sizeof(struct uconfig));
  963. if(cfg == RT_NULL)
  964. {
  965. rt_kprintf("alloc memery failed\n");
  966. return RT_NULL;
  967. }
  968. rt_memset(cfg, 0, sizeof(struct uconfig));
  969. /* set default wValue */
  970. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  971. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  972. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  973. cfg->cfg_desc.bmAttributes = 0xC0;
  974. cfg->cfg_desc.MaxPower = 0x32;
  975. /* to initialize function object list */
  976. rt_list_init(&cfg->func_list);
  977. return cfg;
  978. }
  979. /**
  980. * This function will create an usb interface object.
  981. *
  982. * @param device the usb device object.
  983. * @handler the callback handler of object
  984. *
  985. * @return an usb interface object on success, RT_NULL on fail.
  986. */
  987. uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler)
  988. {
  989. uintf_t intf;
  990. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_new\n"));
  991. /* parameter check */
  992. RT_ASSERT(device != RT_NULL);
  993. /* allocate memory for the object */
  994. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  995. if(intf == RT_NULL)
  996. {
  997. rt_kprintf("alloc memery failed\n");
  998. return RT_NULL;
  999. }
  1000. intf->intf_num = device->nr_intf;
  1001. device->nr_intf++;
  1002. intf->handler = handler;
  1003. intf->curr_setting = RT_NULL;
  1004. /* to initialize the alternate setting object list */
  1005. rt_list_init(&intf->setting_list);
  1006. return intf;
  1007. }
  1008. /**
  1009. * This function will create an usb alternate setting object.
  1010. *
  1011. * @param intf_desc the interface descriptor.
  1012. * @desc_size the size of the interface descriptor.
  1013. *
  1014. * @return an usb alternate setting object on success, RT_NULL on fail.
  1015. */
  1016. ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size)
  1017. {
  1018. ualtsetting_t setting;
  1019. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_new\n"));
  1020. /* parameter check */
  1021. RT_ASSERT(desc_size > 0);
  1022. /* allocate memory for the object */
  1023. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  1024. if(setting == RT_NULL)
  1025. {
  1026. rt_kprintf("alloc memery failed\n");
  1027. return RT_NULL;
  1028. }
  1029. /* allocate memory for the desc */
  1030. setting->desc = rt_malloc(desc_size);
  1031. if (setting->desc == RT_NULL)
  1032. {
  1033. rt_kprintf("alloc desc memery failed\n");
  1034. rt_free(setting);
  1035. return RT_NULL;
  1036. }
  1037. setting->desc_size = desc_size;
  1038. setting->intf_desc = RT_NULL;
  1039. /* to initialize endpoint list */
  1040. rt_list_init(&setting->ep_list);
  1041. return setting;
  1042. }
  1043. /**
  1044. * This function will config an desc in alternate setting object.
  1045. *
  1046. * @param setting the altsetting to be config.
  1047. * @param desc use it to init desc in setting.
  1048. * @param intf_pos the offset of interface descriptor in desc.
  1049. *
  1050. * @return RT_EOK.
  1051. */
  1052. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  1053. {
  1054. RT_ASSERT(setting != RT_NULL);
  1055. RT_ASSERT(setting->desc !=RT_NULL);
  1056. rt_memcpy(setting->desc, desc, setting->desc_size);
  1057. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  1058. return RT_EOK;
  1059. }
  1060. /**
  1061. * This function will create an usb function object.
  1062. *
  1063. * @param device the usb device object.
  1064. * @param dev_desc the device descriptor.
  1065. * @param ops the operation set.
  1066. *
  1067. * @return an usb function object on success, RT_NULL on fail.
  1068. */
  1069. ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
  1070. ufunction_ops_t ops)
  1071. {
  1072. ufunction_t func;
  1073. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_new\n"));
  1074. /* parameter check */
  1075. RT_ASSERT(device != RT_NULL);
  1076. RT_ASSERT(dev_desc != RT_NULL);
  1077. /* allocate memory for the object */
  1078. func = (ufunction_t)rt_malloc(sizeof(struct ufunction));
  1079. if(func == RT_NULL)
  1080. {
  1081. rt_kprintf("alloc memery failed\n");
  1082. return RT_NULL;
  1083. }
  1084. func->dev_desc = dev_desc;
  1085. func->ops = ops;
  1086. func->device = device;
  1087. func->enabled = RT_FALSE;
  1088. /* to initialize interface list */
  1089. rt_list_init(&func->intf_list);
  1090. return func;
  1091. }
  1092. /**
  1093. * This function will create an usb endpoint object.
  1094. *
  1095. * @param ep_desc the endpoint descriptor.
  1096. * @handler the callback handler of object
  1097. *
  1098. * @return an usb endpoint object on success, RT_NULL on fail.
  1099. */
  1100. uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler)
  1101. {
  1102. uep_t ep;
  1103. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_new\n"));
  1104. /* parameter check */
  1105. RT_ASSERT(ep_desc != RT_NULL);
  1106. /* allocate memory for the object */
  1107. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  1108. if(ep == RT_NULL)
  1109. {
  1110. rt_kprintf("alloc memery failed\n");
  1111. return RT_NULL;
  1112. }
  1113. ep->ep_desc = ep_desc;
  1114. ep->handler = handler;
  1115. ep->buffer = RT_NULL;
  1116. ep->stalled = RT_FALSE;
  1117. rt_list_init(&ep->request_list);
  1118. return ep;
  1119. }
  1120. /**
  1121. * This function will find an usb device object.
  1122. *
  1123. * @dcd usd device controller driver.
  1124. *
  1125. * @return an usb device object on found or RT_NULL on not found.
  1126. */
  1127. udevice_t rt_usbd_find_device(udcd_t dcd)
  1128. {
  1129. struct rt_list_node* node;
  1130. udevice_t device;
  1131. /* parameter check */
  1132. RT_ASSERT(dcd != RT_NULL);
  1133. /* search a device in the the device list */
  1134. for (node = device_list.next; node != &device_list; node = node->next)
  1135. {
  1136. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  1137. if(device->dcd == dcd) return device;
  1138. }
  1139. rt_kprintf("can't find device\n");
  1140. return RT_NULL;
  1141. }
  1142. /**
  1143. * This function will find an usb configuration object.
  1144. *
  1145. * @param device the usb device object.
  1146. * @param wValue the configuration number.
  1147. *
  1148. * @return an usb configuration object on found or RT_NULL on not found.
  1149. */
  1150. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  1151. {
  1152. struct rt_list_node* node;
  1153. uconfig_t cfg = RT_NULL;
  1154. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  1155. /* parameter check */
  1156. RT_ASSERT(device != RT_NULL);
  1157. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1158. /* search a configration in the the device */
  1159. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  1160. {
  1161. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  1162. if(cfg->cfg_desc.bConfigurationValue == value)
  1163. {
  1164. return cfg;
  1165. }
  1166. }
  1167. rt_kprintf("can't find configuration %d\n", value);
  1168. return RT_NULL;
  1169. }
  1170. /**
  1171. * This function will find an usb interface object.
  1172. *
  1173. * @param device the usb device object.
  1174. * @param wValue the interface number.
  1175. *
  1176. * @return an usb configuration object on found or RT_NULL on not found.
  1177. */
  1178. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc)
  1179. {
  1180. struct rt_list_node *i, *j;
  1181. ufunction_t func;
  1182. uintf_t intf;
  1183. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  1184. /* parameter check */
  1185. RT_ASSERT(device != RT_NULL);
  1186. RT_ASSERT(value < device->nr_intf);
  1187. /* search an interface in the current configuration */
  1188. for (i=device->curr_cfg->func_list.next;
  1189. i!=&device->curr_cfg->func_list; i=i->next)
  1190. {
  1191. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1192. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1193. {
  1194. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1195. if(intf->intf_num == value)
  1196. {
  1197. if (pfunc != RT_NULL)
  1198. *pfunc = func;
  1199. return intf;
  1200. }
  1201. }
  1202. }
  1203. rt_kprintf("can't find interface %d\n", value);
  1204. return RT_NULL;
  1205. }
  1206. /**
  1207. * This function will find an usb interface alternate setting object.
  1208. *
  1209. * @param device the usb device object.
  1210. * @param wValue the alternate setting number.
  1211. *
  1212. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  1213. */
  1214. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  1215. {
  1216. struct rt_list_node *i;
  1217. ualtsetting_t setting;
  1218. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  1219. /* parameter check */
  1220. RT_ASSERT(intf != RT_NULL);
  1221. if(intf->curr_setting != RT_NULL)
  1222. {
  1223. /* if the wValue equal to the current alternate setting, then do not search */
  1224. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  1225. return intf->curr_setting;
  1226. }
  1227. /* search a setting in the alternate setting list */
  1228. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  1229. {
  1230. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  1231. if(setting->intf_desc->bAlternateSetting == value)
  1232. return setting;
  1233. }
  1234. rt_kprintf("can't find alternate setting %d\n", value);
  1235. return RT_NULL;
  1236. }
  1237. /**
  1238. * This function will find an usb endpoint object.
  1239. *
  1240. * @param device the usb device object.
  1241. * @param ep_addr endpoint address.
  1242. *
  1243. * @return an usb endpoint object on found or RT_NULL on not found.
  1244. */
  1245. uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr)
  1246. {
  1247. uep_t ep;
  1248. struct rt_list_node *i, *j, *k;
  1249. ufunction_t func;
  1250. uintf_t intf;
  1251. /* parameter check */
  1252. RT_ASSERT(device != RT_NULL);
  1253. /* search a endpoint in the current configuration */
  1254. for (i=device->curr_cfg->func_list.next; i!=&device->curr_cfg->func_list; i=i->next)
  1255. {
  1256. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1257. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1258. {
  1259. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1260. for(k=intf->curr_setting->ep_list.next;
  1261. k!=&intf->curr_setting->ep_list; k=k->next)
  1262. {
  1263. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1264. if(EP_ADDRESS(ep) == ep_addr)
  1265. {
  1266. if (pfunc != RT_NULL)
  1267. *pfunc = func;
  1268. return ep;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  1274. return RT_NULL;
  1275. }
  1276. /**
  1277. * This function will add a configuration to an usb device.
  1278. *
  1279. * @param device the usb device object.
  1280. * @param cfg the configuration object.
  1281. *
  1282. * @return RT_EOK.
  1283. */
  1284. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  1285. {
  1286. struct rt_list_node *i, *j, *k;
  1287. ufunction_t func;
  1288. uintf_t intf;
  1289. uep_t ep;
  1290. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  1291. /* parameter check */
  1292. RT_ASSERT(device != RT_NULL);
  1293. RT_ASSERT(cfg != RT_NULL);
  1294. /* set configuration number to the configuration descriptor */
  1295. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  1296. device->dev_desc.bNumConfigurations++;
  1297. for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
  1298. {
  1299. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1300. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1301. {
  1302. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1303. cfg->cfg_desc.bNumInterfaces++;
  1304. /* allocate address for every endpoint in the interface alternate setting */
  1305. for(k=intf->curr_setting->ep_list.next;
  1306. k!=&intf->curr_setting->ep_list; k=k->next)
  1307. {
  1308. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1309. if(rt_usbd_ep_assign(device, ep) != RT_EOK)
  1310. {
  1311. rt_kprintf("endpoint assign error\n");
  1312. }
  1313. }
  1314. /* construct complete configuration descriptor */
  1315. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
  1316. (void*)intf->curr_setting->desc,
  1317. intf->curr_setting->desc_size);
  1318. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  1319. }
  1320. }
  1321. /* insert the configuration to the list */
  1322. rt_list_insert_before(&device->cfg_list, &cfg->list);
  1323. return RT_EOK;
  1324. }
  1325. /**
  1326. * This function will add a function to a configuration.
  1327. *
  1328. * @param cfg the configuration object.
  1329. * @param func the function object.
  1330. *
  1331. * @return RT_EOK.
  1332. */
  1333. rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func)
  1334. {
  1335. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_function\n"));
  1336. /* parameter check */
  1337. RT_ASSERT(cfg != RT_NULL);
  1338. RT_ASSERT(func != RT_NULL);
  1339. /* insert the function to the list */
  1340. rt_list_insert_before(&cfg->func_list, &func->list);
  1341. return RT_EOK;
  1342. }
  1343. /**
  1344. * This function will add an interface to a function.
  1345. *
  1346. * @param func the function object.
  1347. * @param intf the interface object.
  1348. *
  1349. * @return RT_EOK.
  1350. */
  1351. rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf)
  1352. {
  1353. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_add_interface\n"));
  1354. /* parameter check */
  1355. RT_ASSERT(func != RT_NULL);
  1356. RT_ASSERT(intf != RT_NULL);
  1357. /* insert the interface to the list */
  1358. rt_list_insert_before(&func->intf_list, &intf->list);
  1359. return RT_EOK;
  1360. }
  1361. /**
  1362. * This function will add an alternate setting to an interface.
  1363. *
  1364. * @param intf the interface object.
  1365. * @param setting the alternate setting object.
  1366. *
  1367. * @return RT_EOK.
  1368. */
  1369. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1370. {
  1371. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1372. /* parameter check */
  1373. RT_ASSERT(intf != RT_NULL);
  1374. RT_ASSERT(setting != RT_NULL);
  1375. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1376. /* insert the alternate setting to the list */
  1377. rt_list_insert_before(&intf->setting_list, &setting->list);
  1378. return RT_EOK;
  1379. }
  1380. /**
  1381. * This function will add an endpoint to an alternate setting.
  1382. *
  1383. * @param setting the alternate setting object.
  1384. * @param ep the endpoint object.
  1385. *
  1386. * @return RT_EOK.
  1387. */
  1388. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1389. {
  1390. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1391. /* parameter check */
  1392. RT_ASSERT(setting != RT_NULL);
  1393. RT_ASSERT(ep != RT_NULL);
  1394. /* insert the endpoint to the list */
  1395. rt_list_insert_before(&setting->ep_list, &ep->list);
  1396. return RT_EOK;
  1397. }
  1398. rt_err_t rt_usbd_os_comp_id_desc_add_os_func_comp_id_desc(usb_os_comp_id_desc_t os_comp_id_desc, usb_os_func_comp_id_desc_t os_func_comp_id_desc)
  1399. {
  1400. RT_ASSERT(os_comp_id_desc != RT_NULL);
  1401. RT_ASSERT(os_func_comp_id_desc != RT_NULL);
  1402. rt_list_insert_before(&os_comp_id_desc->func_desc, &os_func_comp_id_desc->list);
  1403. os_comp_id_desc->head_desc.bCount++;
  1404. return RT_EOK;
  1405. }
  1406. /**
  1407. * This function will set an alternate setting for an interface.
  1408. *
  1409. * @param intf_desc the interface descriptor.
  1410. * @param wValue the alternate setting number.
  1411. *
  1412. * @return RT_EOK.
  1413. */
  1414. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1415. {
  1416. ualtsetting_t setting;
  1417. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1418. /* parameter check */
  1419. RT_ASSERT(intf != RT_NULL);
  1420. /* find an alternate setting */
  1421. setting = rt_usbd_find_altsetting(intf, value);
  1422. /* set as current alternate setting */
  1423. intf->curr_setting = setting;
  1424. return RT_EOK;
  1425. }
  1426. /**
  1427. * This function will set a configuration for an usb device.
  1428. *
  1429. * @param device the usb device object.
  1430. * @param wValue the configuration number.
  1431. *
  1432. * @return RT_EOK.
  1433. */
  1434. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1435. {
  1436. uconfig_t cfg;
  1437. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1438. /* parameter check */
  1439. RT_ASSERT(device != RT_NULL);
  1440. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1441. /* find a configuration */
  1442. cfg = rt_usbd_find_config(device, value);
  1443. /* set as current configuration */
  1444. device->curr_cfg = cfg;
  1445. dcd_set_config(device->dcd, value);
  1446. return RT_TRUE;
  1447. }
  1448. /**
  1449. * This function will bRequest an IO transaction.
  1450. *
  1451. * @param device the usb device object.
  1452. * @param ep the endpoint object.
  1453. * @param req IO bRequest.
  1454. *
  1455. * @return RT_EOK.
  1456. */
  1457. rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
  1458. {
  1459. rt_size_t size = 0;
  1460. RT_ASSERT(device != RT_NULL);
  1461. RT_ASSERT(req != RT_NULL);
  1462. if(ep->stalled == RT_FALSE)
  1463. {
  1464. switch(req->req_type)
  1465. {
  1466. case UIO_REQUEST_READ_BEST:
  1467. case UIO_REQUEST_READ_FULL:
  1468. ep->request.remain_size = ep->request.size;
  1469. size = rt_usbd_ep_read_prepare(device, ep, req->buffer, req->size);
  1470. break;
  1471. case UIO_REQUEST_WRITE:
  1472. ep->request.remain_size = ep->request.size;
  1473. size = rt_usbd_ep_write(device, ep, req->buffer, req->size);
  1474. break;
  1475. default:
  1476. rt_kprintf("unknown request type\n");
  1477. break;
  1478. }
  1479. }
  1480. else
  1481. {
  1482. rt_list_insert_before(&ep->request_list, &req->list);
  1483. RT_DEBUG_LOG(RT_DEBUG_USB, ("suspend a request\n"));
  1484. }
  1485. return size;
  1486. }
  1487. /**
  1488. * This function will set feature for an usb device.
  1489. *
  1490. * @param device the usb device object.
  1491. * @param wValue the configuration number.
  1492. *
  1493. * @return RT_EOK.
  1494. */
  1495. rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
  1496. {
  1497. RT_ASSERT(device != RT_NULL);
  1498. if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
  1499. {
  1500. RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature remote wakeup\n"));
  1501. }
  1502. else if (value == USB_FEATURE_ENDPOINT_HALT)
  1503. {
  1504. RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature stall\n"));
  1505. dcd_ep_set_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
  1506. }
  1507. return RT_EOK;
  1508. }
  1509. /**
  1510. * This function will clear feature for an usb device.
  1511. *
  1512. * @param device the usb device object.
  1513. * @param wValue the configuration number.
  1514. *
  1515. * @return RT_EOK.
  1516. */
  1517. rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
  1518. {
  1519. RT_ASSERT(device != RT_NULL);
  1520. if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
  1521. {
  1522. RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature remote wakeup\n"));
  1523. }
  1524. else if (value == USB_FEATURE_ENDPOINT_HALT)
  1525. {
  1526. RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature stall\n"));
  1527. dcd_ep_clear_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
  1528. }
  1529. return RT_EOK;
  1530. }
  1531. rt_err_t rt_usbd_ep0_set_stall(udevice_t device)
  1532. {
  1533. RT_ASSERT(device != RT_NULL);
  1534. return dcd_ep_set_stall(device->dcd, 0);
  1535. }
  1536. rt_err_t rt_usbd_ep0_clear_stall(udevice_t device)
  1537. {
  1538. RT_ASSERT(device != RT_NULL);
  1539. return dcd_ep_clear_stall(device->dcd, 0);
  1540. }
  1541. rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep)
  1542. {
  1543. rt_err_t ret;
  1544. RT_ASSERT(device != RT_NULL);
  1545. RT_ASSERT(ep != RT_NULL);
  1546. RT_ASSERT(ep->ep_desc != RT_NULL);
  1547. ret = dcd_ep_set_stall(device->dcd, EP_ADDRESS(ep));
  1548. if(ret == RT_EOK)
  1549. {
  1550. ep->stalled = RT_TRUE;
  1551. }
  1552. return ret;
  1553. }
  1554. rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep)
  1555. {
  1556. rt_err_t ret;
  1557. RT_ASSERT(device != RT_NULL);
  1558. RT_ASSERT(ep != RT_NULL);
  1559. RT_ASSERT(ep->ep_desc != RT_NULL);
  1560. ret = dcd_ep_clear_stall(device->dcd, EP_ADDRESS(ep));
  1561. if(ret == RT_EOK)
  1562. {
  1563. ep->stalled = RT_FALSE;
  1564. }
  1565. return ret;
  1566. }
  1567. static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep)
  1568. {
  1569. int i = 0;
  1570. RT_ASSERT(device != RT_NULL);
  1571. RT_ASSERT(device->dcd != RT_NULL);
  1572. RT_ASSERT(device->dcd->ep_pool != RT_NULL);
  1573. RT_ASSERT(ep != RT_NULL);
  1574. RT_ASSERT(ep->ep_desc != RT_NULL);
  1575. while(device->dcd->ep_pool[i].addr != 0xFF)
  1576. {
  1577. if(device->dcd->ep_pool[i].status == ID_UNASSIGNED &&
  1578. ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type)
  1579. {
  1580. EP_ADDRESS(ep) |= device->dcd->ep_pool[i].addr;
  1581. ep->id = &device->dcd->ep_pool[i];
  1582. device->dcd->ep_pool[i].status = ID_ASSIGNED;
  1583. RT_DEBUG_LOG(RT_DEBUG_USB, ("assigned %d\n", device->dcd->ep_pool[i].addr));
  1584. return RT_EOK;
  1585. }
  1586. i++;
  1587. }
  1588. return -RT_ERROR;
  1589. }
  1590. static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep)
  1591. {
  1592. RT_ASSERT(device != RT_NULL);
  1593. RT_ASSERT(device->dcd != RT_NULL);
  1594. RT_ASSERT(device->dcd->ep_pool != RT_NULL);
  1595. RT_ASSERT(ep != RT_NULL);
  1596. RT_ASSERT(ep->ep_desc != RT_NULL);
  1597. ep->id->status = ID_UNASSIGNED;
  1598. return RT_EOK;
  1599. }
  1600. rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct urequest* setup)
  1601. {
  1602. struct udev_msg msg;
  1603. rt_size_t size;
  1604. RT_ASSERT(dcd != RT_NULL);
  1605. if(setup == RT_NULL)
  1606. {
  1607. size = dcd_ep_read(dcd, EP0_OUT_ADDR, (void*)&msg.content.setup);
  1608. if(size != sizeof(struct urequest))
  1609. {
  1610. rt_kprintf("read setup packet error\n");
  1611. return -RT_ERROR;
  1612. }
  1613. }
  1614. else
  1615. {
  1616. rt_memcpy((void*)&msg.content.setup, (void*)setup, sizeof(struct urequest));
  1617. }
  1618. msg.type = USB_MSG_SETUP_NOTIFY;
  1619. msg.dcd = dcd;
  1620. rt_usbd_event_signal(&msg);
  1621. return RT_EOK;
  1622. }
  1623. rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd)
  1624. {
  1625. rt_int32_t remain, mps;
  1626. RT_ASSERT(dcd != RT_NULL);
  1627. if (dcd->stage != STAGE_DIN)
  1628. return RT_EOK;
  1629. mps = dcd->ep0.id->maxpacket;
  1630. dcd->ep0.request.remain_size -= mps;
  1631. remain = dcd->ep0.request.remain_size;
  1632. if (remain > 0)
  1633. {
  1634. if (remain >= mps)
  1635. {
  1636. remain = mps;
  1637. }
  1638. dcd->ep0.request.buffer += mps;
  1639. dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, remain);
  1640. }
  1641. else
  1642. {
  1643. /* last packet is MPS multiple, so send ZLP packet */
  1644. if ((remain == 0) && (dcd->ep0.request.size > 0))
  1645. {
  1646. dcd->ep0.request.size = 0;
  1647. dcd_ep_write(dcd, EP0_IN_ADDR, RT_NULL, 0);
  1648. }
  1649. else
  1650. {
  1651. /* receive status */
  1652. dcd->stage = STAGE_STATUS_OUT;
  1653. dcd_ep_read_prepare(dcd, EP0_OUT_ADDR, RT_NULL, 0);
  1654. }
  1655. }
  1656. return RT_EOK;
  1657. }
  1658. rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size)
  1659. {
  1660. struct udev_msg msg;
  1661. RT_ASSERT(dcd != RT_NULL);
  1662. msg.type = USB_MSG_EP0_OUT;
  1663. msg.dcd = dcd;
  1664. msg.content.ep_msg.size = size;
  1665. rt_usbd_event_signal(&msg);
  1666. return RT_EOK;
  1667. }
  1668. rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
  1669. {
  1670. struct udev_msg msg;
  1671. RT_ASSERT(dcd != RT_NULL);
  1672. msg.type = USB_MSG_DATA_NOTIFY;
  1673. msg.dcd = dcd;
  1674. msg.content.ep_msg.ep_addr = address;
  1675. msg.content.ep_msg.size = size;
  1676. rt_usbd_event_signal(&msg);
  1677. return RT_EOK;
  1678. }
  1679. rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
  1680. {
  1681. struct udev_msg msg;
  1682. RT_ASSERT(dcd != RT_NULL);
  1683. msg.type = USB_MSG_DATA_NOTIFY;
  1684. msg.dcd = dcd;
  1685. msg.content.ep_msg.ep_addr = address;
  1686. msg.content.ep_msg.size = size;
  1687. rt_usbd_event_signal(&msg);
  1688. return RT_EOK;
  1689. }
  1690. rt_err_t rt_usbd_reset_handler(udcd_t dcd)
  1691. {
  1692. struct udev_msg msg;
  1693. RT_ASSERT(dcd != RT_NULL);
  1694. msg.type = USB_MSG_RESET;
  1695. msg.dcd = dcd;
  1696. rt_usbd_event_signal(&msg);
  1697. return RT_EOK;
  1698. }
  1699. rt_err_t rt_usbd_connect_handler(udcd_t dcd)
  1700. {
  1701. struct udev_msg msg;
  1702. RT_ASSERT(dcd != RT_NULL);
  1703. msg.type = USB_MSG_PLUG_IN;
  1704. msg.dcd = dcd;
  1705. rt_usbd_event_signal(&msg);
  1706. return RT_EOK;
  1707. }
  1708. rt_err_t rt_usbd_disconnect_handler(udcd_t dcd)
  1709. {
  1710. struct udev_msg msg;
  1711. RT_ASSERT(dcd != RT_NULL);
  1712. msg.type = USB_MSG_PLUG_OUT;
  1713. msg.dcd = dcd;
  1714. rt_usbd_event_signal(&msg);
  1715. return RT_EOK;
  1716. }
  1717. rt_err_t rt_usbd_sof_handler(udcd_t dcd)
  1718. {
  1719. struct udev_msg msg;
  1720. RT_ASSERT(dcd != RT_NULL);
  1721. msg.type = USB_MSG_SOF;
  1722. msg.dcd = dcd;
  1723. rt_usbd_event_signal(&msg);
  1724. return RT_EOK;
  1725. }
  1726. rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
  1727. {
  1728. uep_t ep0;
  1729. rt_size_t sent_size = 0;
  1730. RT_ASSERT(device != RT_NULL);
  1731. RT_ASSERT(device->dcd != RT_NULL);
  1732. RT_ASSERT(buffer != RT_NULL);
  1733. RT_ASSERT(size > 0);
  1734. ep0 = &device->dcd->ep0;
  1735. ep0->request.size = size;
  1736. ep0->request.buffer = buffer;
  1737. ep0->request.remain_size = size;
  1738. if(size >= ep0->id->maxpacket)
  1739. {
  1740. sent_size = ep0->id->maxpacket;
  1741. }
  1742. else
  1743. {
  1744. sent_size = size;
  1745. }
  1746. device->dcd->stage = STAGE_DIN;
  1747. return dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, sent_size);
  1748. }
  1749. rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
  1750. rt_err_t (*rx_ind)(udevice_t device, rt_size_t size))
  1751. {
  1752. uep_t ep0;
  1753. rt_size_t read_size = 0;
  1754. RT_ASSERT(device != RT_NULL);
  1755. RT_ASSERT(device->dcd != RT_NULL);
  1756. RT_ASSERT(buffer != RT_NULL);
  1757. ep0 = &device->dcd->ep0;
  1758. ep0->request.buffer = buffer;
  1759. ep0->request.remain_size = size;
  1760. ep0->rx_indicate = rx_ind;
  1761. if(size >= ep0->id->maxpacket)
  1762. {
  1763. read_size = ep0->id->maxpacket;
  1764. }
  1765. else
  1766. {
  1767. read_size = size;
  1768. }
  1769. device->dcd->stage = STAGE_DOUT;
  1770. dcd_ep_read_prepare(device->dcd, EP0_OUT_ADDR, buffer, read_size);
  1771. return size;
  1772. }
  1773. static struct rt_messagequeue usb_mq;
  1774. /**
  1775. * This function is the main entry of usb device thread, it is in charge of
  1776. * processing all messages received from the usb message buffer.
  1777. *
  1778. * @param parameter the parameter of the usb device thread.
  1779. *
  1780. * @return none.
  1781. */
  1782. static void rt_usbd_thread_entry(void* parameter)
  1783. {
  1784. while(1)
  1785. {
  1786. struct udev_msg msg;
  1787. udevice_t device;
  1788. /* receive message */
  1789. if(rt_mq_recv(&usb_mq, &msg, sizeof(struct udev_msg),
  1790. RT_WAITING_FOREVER) != RT_EOK )
  1791. continue;
  1792. device = rt_usbd_find_device(msg.dcd);
  1793. if(device == RT_NULL)
  1794. {
  1795. rt_kprintf("invalid usb device\n");
  1796. continue;
  1797. }
  1798. RT_DEBUG_LOG(RT_DEBUG_USB, ("message type %d\n", msg.type));
  1799. switch (msg.type)
  1800. {
  1801. case USB_MSG_SOF:
  1802. _sof_notify(device);
  1803. break;
  1804. case USB_MSG_DATA_NOTIFY:
  1805. /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
  1806. * got configured. */
  1807. _data_notify(device, &msg.content.ep_msg);
  1808. break;
  1809. case USB_MSG_SETUP_NOTIFY:
  1810. _setup_request(device, &msg.content.setup);
  1811. break;
  1812. case USB_MSG_EP0_OUT:
  1813. _ep0_out_notify(device, &msg.content.ep_msg);
  1814. break;
  1815. case USB_MSG_RESET:
  1816. RT_DEBUG_LOG(RT_DEBUG_USB, ("reset %d\n", device->state));
  1817. if (device->state == USB_STATE_ADDRESS || device->state == USB_STATE_CONFIGURED)
  1818. _stop_notify(device);
  1819. device->state = USB_STATE_NOTATTACHED;
  1820. break;
  1821. case USB_MSG_PLUG_IN:
  1822. device->state = USB_STATE_ATTACHED;
  1823. break;
  1824. case USB_MSG_PLUG_OUT:
  1825. device->state = USB_STATE_NOTATTACHED;
  1826. _stop_notify(device);
  1827. break;
  1828. default:
  1829. rt_kprintf("unknown msg type %d\n", msg.type);
  1830. break;
  1831. }
  1832. }
  1833. }
  1834. /**
  1835. * This function will post an message to usb message queue,
  1836. *
  1837. * @param msg the message to be posted
  1838. * @param size the size of the message .
  1839. *
  1840. * @return the error code, RT_EOK on successfully.
  1841. */
  1842. rt_err_t rt_usbd_event_signal(struct udev_msg* msg)
  1843. {
  1844. RT_ASSERT(msg != RT_NULL);
  1845. /* send message to usb message queue */
  1846. return rt_mq_send(&usb_mq, (void*)msg, sizeof(struct udev_msg));
  1847. }
  1848. ALIGN(RT_ALIGN_SIZE)
  1849. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1850. static struct rt_thread usb_thread;
  1851. #define USBD_MQ_MSG_SZ 32
  1852. #define USBD_MQ_MAX_MSG 16
  1853. /* internal of the message queue: every message is associated with a pointer,
  1854. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1855. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1856. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1857. /**
  1858. * This function will initialize usb device thread.
  1859. *
  1860. * @return none.
  1861. *
  1862. */
  1863. rt_err_t rt_usbd_core_init(void)
  1864. {
  1865. rt_list_init(&device_list);
  1866. /* create an usb message queue */
  1867. rt_mq_init(&usb_mq,
  1868. "usbd",
  1869. usb_mq_pool, USBD_MQ_MSG_SZ,
  1870. sizeof(usb_mq_pool),
  1871. RT_IPC_FLAG_FIFO);
  1872. /* init usb device thread */
  1873. rt_thread_init(&usb_thread,
  1874. "usbd",
  1875. rt_usbd_thread_entry, RT_NULL,
  1876. usb_thread_stack, RT_USBD_THREAD_STACK_SZ,
  1877. RT_USBD_THREAD_PRIO, 20);
  1878. /* rt_thread_init should always be OK, so start the thread without further
  1879. * checking. */
  1880. return rt_thread_startup(&usb_thread);
  1881. }