core.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. * File : core.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-10-01 Yi Qiu first version
  13. * 2012-12-12 heyuanjie87 change endpoint and class handler
  14. * 2012-12-30 heyuanjie87 change inferface handler
  15. */
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. static rt_list_t device_list;
  19. /**
  20. * This function will handle get_device_descriptor request.
  21. *
  22. * @param device the usb device object.
  23. * @param setup the setup request.
  24. *
  25. * @return RT_EOK on successful.
  26. */
  27. static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
  28. {
  29. rt_size_t size;
  30. /* parameter check */
  31. RT_ASSERT(device != RT_NULL);
  32. RT_ASSERT(setup != RT_NULL);
  33. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
  34. /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
  35. size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
  36. USB_DESC_LENGTH_DEVICE : setup->length;
  37. /* send device descriptor to endpoint 0 */
  38. dcd_ep_write(device->dcd, 0, (rt_uint8_t*)&device->dev_desc,
  39. size);
  40. return RT_EOK;
  41. }
  42. /**
  43. * This function will handle get_config_descriptor request.
  44. *
  45. * @param device the usb device object.
  46. * @param setup the setup request.
  47. *
  48. * @return RT_EOK on successful.
  49. */
  50. static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
  51. {
  52. rt_size_t size;
  53. ucfg_desc_t cfg_desc;
  54. /* parameter check */
  55. RT_ASSERT(device != RT_NULL);
  56. RT_ASSERT(setup != RT_NULL);
  57. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
  58. cfg_desc = &device->curr_cfg->cfg_desc;
  59. size = (setup->length > cfg_desc->wTotalLength) ?
  60. cfg_desc->wTotalLength : setup->length;
  61. /* send configuration descriptor to endpoint 0 */
  62. dcd_ep_write(device->dcd, 0, (rt_uint8_t*)cfg_desc, size);
  63. return RT_EOK;
  64. }
  65. /**
  66. * This function will handle get_string_descriptor request.
  67. *
  68. * @param device the usb device object.
  69. * @param setup the setup request.
  70. *
  71. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  72. */
  73. static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
  74. {
  75. struct ustring_descriptor str_desc;
  76. rt_uint8_t index, i;
  77. rt_uint32_t len;
  78. /* parameter check */
  79. RT_ASSERT(device != RT_NULL);
  80. RT_ASSERT(setup != RT_NULL);
  81. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
  82. str_desc.type = USB_DESC_TYPE_STRING;
  83. index = setup->value & 0xFF;
  84. if(index > USB_STRING_INTERFACE_INDEX)
  85. {
  86. rt_kprintf("unknown string index\n");
  87. dcd_ep_stall(device->dcd, 0);
  88. return -RT_ERROR;
  89. }
  90. if(index == 0)
  91. {
  92. str_desc.bLength = 4;
  93. str_desc.String[0] = 0x09;
  94. str_desc.String[1] = 0x04;
  95. }
  96. else
  97. {
  98. len = rt_strlen(device->str[index]);
  99. str_desc.bLength = len*2 + 2;
  100. for(i=0; i<len; i++)
  101. {
  102. str_desc.String[i*2] = device->str[index][i];
  103. str_desc.String[i*2 + 1] = 0;
  104. }
  105. }
  106. if(setup->length > len)
  107. len = str_desc.bLength;
  108. else
  109. len = setup->length;
  110. /* send string descriptor to endpoint 0 */
  111. dcd_ep_write(device->dcd, 0, (rt_uint8_t*)&str_desc, len);
  112. return RT_EOK;
  113. }
  114. /**
  115. * This function will handle get_descriptor request.
  116. *
  117. * @param device the usb device object.
  118. * @param setup the setup request.
  119. *
  120. * @return RT_EOK on successful.
  121. */
  122. static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
  123. {
  124. /* parameter check */
  125. RT_ASSERT(device != RT_NULL);
  126. RT_ASSERT(setup != RT_NULL);
  127. if(setup->request_type == USB_REQ_TYPE_DIR_IN)
  128. {
  129. switch(setup->value >> 8)
  130. {
  131. case USB_DESC_TYPE_DEVICE:
  132. _get_device_descriptor(device, setup);
  133. break;
  134. case USB_DESC_TYPE_CONFIGURATION:
  135. _get_config_descriptor(device, setup);
  136. break;
  137. case USB_DESC_TYPE_STRING:
  138. _get_string_descriptor(device, setup);
  139. break;
  140. case USB_DESC_TYPE_DEVICEQUALIFIER:
  141. dcd_ep_stall(device->dcd, 0);
  142. break;
  143. default:
  144. rt_kprintf("unsupported descriptor request\n");
  145. dcd_ep_stall(device->dcd, 0);
  146. break;
  147. }
  148. }
  149. else
  150. {
  151. rt_kprintf("request direction error\n");
  152. dcd_ep_stall(device->dcd, 0);
  153. }
  154. return RT_EOK;
  155. }
  156. /**
  157. * This function will handle get_interface request.
  158. *
  159. * @param device the usb device object.
  160. * @param setup the setup request.
  161. *
  162. * @return RT_EOK on successful.
  163. */
  164. static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
  165. {
  166. rt_uint8_t value;
  167. uintf_t intf;
  168. /* parameter check */
  169. RT_ASSERT(device != RT_NULL);
  170. RT_ASSERT(setup != RT_NULL);
  171. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_interface\n"));
  172. if (device->state != USB_STATE_CONFIGURED)
  173. {
  174. dcd_ep_stall(device->dcd, 0);
  175. return -RT_ERROR;
  176. }
  177. /* find the specified interface and its alternate setting */
  178. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  179. value = intf->curr_setting->intf_desc->bAlternateSetting;
  180. /* send the interface alternate setting to endpoint 0*/
  181. dcd_ep_write(device->dcd, 0, &value, 1);
  182. return RT_EOK;
  183. }
  184. /**
  185. * This function will handle set_interface request.
  186. *
  187. * @param device the usb device object.
  188. * @param setup the setup request.
  189. *
  190. * @return RT_EOK on successful.
  191. */
  192. static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
  193. {
  194. uintf_t intf;
  195. uep_t ep;
  196. struct rt_list_node* i;
  197. ualtsetting_t setting;
  198. /* parameter check */
  199. RT_ASSERT(device != RT_NULL);
  200. RT_ASSERT(setup != RT_NULL);
  201. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));
  202. if (device->state != USB_STATE_CONFIGURED)
  203. {
  204. dcd_ep_stall(device->dcd, 0);
  205. return -RT_ERROR;
  206. }
  207. /* find the specified interface */
  208. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  209. /* set alternate setting to the interface */
  210. rt_usbd_set_altsetting(intf, setup->value & 0xFF);
  211. setting = intf->curr_setting;
  212. /* start all endpoints of the interface alternate setting */
  213. for(i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
  214. {
  215. ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
  216. dcd_ep_stop(device->dcd, ep);
  217. dcd_ep_run(device->dcd, ep);
  218. }
  219. return RT_EOK;
  220. }
  221. /**
  222. * This function will handle get_config request.
  223. *
  224. * @param device the usb device object.
  225. * @param setup the setup request.
  226. *
  227. * @return RT_EOK on successful.
  228. */
  229. static rt_err_t _get_config(struct udevice* device, ureq_t setup)
  230. {
  231. rt_uint8_t value;
  232. /* parameter check */
  233. RT_ASSERT(device != RT_NULL);
  234. RT_ASSERT(setup != RT_NULL);
  235. RT_ASSERT(device->curr_cfg != RT_NULL);
  236. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
  237. if (device->state == USB_STATE_CONFIGURED)
  238. {
  239. /* get current configuration */
  240. value = device->curr_cfg->cfg_desc.bConfigurationValue;
  241. }
  242. else
  243. {
  244. value = 0;
  245. }
  246. /* write the current configuration to endpoint 0 */
  247. dcd_ep_write(device->dcd, 0, &value, 1);
  248. return RT_EOK;
  249. }
  250. /**
  251. * This function will handle set_config request.
  252. *
  253. * @param device the usb device object.
  254. * @param setup the setup request.
  255. *
  256. * @return RT_EOK on successful.
  257. */
  258. static rt_err_t _set_config(struct udevice* device, ureq_t setup)
  259. {
  260. struct rt_list_node *i, *j, *k;
  261. uconfig_t cfg;
  262. uintf_t intf;
  263. ualtsetting_t setting;
  264. uep_t ep;
  265. /* parameter check */
  266. RT_ASSERT(device != RT_NULL);
  267. RT_ASSERT(setup != RT_NULL);
  268. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
  269. if (setup->value > device->dev_desc.bNumConfigurations)
  270. {
  271. dcd_ep_stall(device->dcd, 0);
  272. return -RT_ERROR;
  273. }
  274. if (setup->value == 0)
  275. {
  276. RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
  277. device->state = USB_STATE_ADDRESS;
  278. goto _exit;
  279. }
  280. /* set current configuration */
  281. rt_usbd_set_config(device, setup->value);
  282. cfg = device->curr_cfg;
  283. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  284. {
  285. /* run all classes and their endpoints in the configuration */
  286. uclass_t cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  287. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  288. {
  289. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  290. setting = intf->curr_setting;
  291. for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
  292. {
  293. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  294. /* first stop then start endpoint */
  295. dcd_ep_stop(device->dcd, ep);
  296. dcd_ep_run(device->dcd, ep);
  297. }
  298. }
  299. /* after running all endpoints, then run class */
  300. if(cls->ops->run != RT_NULL)
  301. cls->ops->run(device, cls);
  302. }
  303. device->state = USB_STATE_CONFIGURED;
  304. _exit:
  305. /* issue status stage */
  306. dcd_send_status(device->dcd);
  307. return RT_EOK;
  308. }
  309. /**
  310. * This function will handle set_address request.
  311. *
  312. * @param device the usb device object.
  313. * @param setup the setup request.
  314. *
  315. * @return RT_EOK on successful.
  316. */
  317. static rt_err_t _set_address(struct udevice* device, ureq_t setup)
  318. {
  319. /* parameter check */
  320. RT_ASSERT(device != RT_NULL);
  321. RT_ASSERT(setup != RT_NULL);
  322. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
  323. /* set address in device control driver */
  324. dcd_set_address(device->dcd, setup->value);
  325. device->state = USB_STATE_ADDRESS;
  326. /* issue status stage */
  327. dcd_send_status(device->dcd);
  328. return RT_EOK;
  329. }
  330. /**
  331. * This function will handle standard request to
  332. * interface that defined in class-specifics
  333. *
  334. * @param device the usb device object.
  335. * @param setup the setup request.
  336. *
  337. * @return RT_EOK on successful.
  338. */
  339. static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
  340. {
  341. uintf_t intf;
  342. uclass_t cls;
  343. rt_err_t ret;
  344. /* parameter check */
  345. RT_ASSERT(device != RT_NULL);
  346. RT_ASSERT(setup != RT_NULL);
  347. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
  348. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  349. if (intf != RT_NULL)
  350. {
  351. ret = intf->handler(device, cls, setup);
  352. }
  353. else
  354. {
  355. ret = -RT_ERROR;
  356. }
  357. return ret;
  358. }
  359. /**
  360. * This function will handle standard request.
  361. *
  362. * @param device the usb device object.
  363. * @param setup the setup request.
  364. *
  365. * @return RT_EOK on successful.
  366. */
  367. static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
  368. {
  369. udcd_t dcd;
  370. rt_uint16_t value = 0;
  371. /* parameter check */
  372. RT_ASSERT(device != RT_NULL);
  373. RT_ASSERT(setup != RT_NULL);
  374. dcd = device->dcd;
  375. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  376. {
  377. case USB_REQ_TYPE_DEVICE:
  378. switch(setup->request)
  379. {
  380. case USB_REQ_GET_STATUS:
  381. dcd_ep_write(device->dcd, 0, &value, 2);
  382. break;
  383. case USB_REQ_CLEAR_FEATURE:
  384. dcd_clear_feature(dcd, setup->value);
  385. break;
  386. case USB_REQ_SET_FEATURE:
  387. dcd_set_feature(dcd, setup->value);
  388. break;
  389. case USB_REQ_SET_ADDRESS:
  390. _set_address(device, setup);
  391. break;
  392. case USB_REQ_GET_DESCRIPTOR:
  393. _get_descriptor(device, setup);
  394. break;
  395. case USB_REQ_SET_DESCRIPTOR:
  396. dcd_ep_stall(dcd, 0);
  397. break;
  398. case USB_REQ_GET_CONFIGURATION:
  399. _get_config(device, setup);
  400. break;
  401. case USB_REQ_SET_CONFIGURATION:
  402. _set_config(device, setup);
  403. break;
  404. default:
  405. rt_kprintf("unknown device request\n");
  406. dcd_ep_stall(device->dcd, 0);
  407. break;
  408. }
  409. break;
  410. case USB_REQ_TYPE_INTERFACE:
  411. switch(setup->request)
  412. {
  413. case USB_REQ_GET_INTERFACE:
  414. _get_interface(device, setup);
  415. break;
  416. case USB_REQ_SET_INTERFACE:
  417. _set_interface(device, setup);
  418. break;
  419. default:
  420. if (_request_interface(device, setup) != RT_EOK)
  421. {
  422. rt_kprintf("unknown interface request\n");
  423. dcd_ep_stall(device->dcd, 0);
  424. return - RT_ERROR;
  425. }
  426. else
  427. break;
  428. }
  429. break;
  430. case USB_REQ_TYPE_ENDPOINT:
  431. switch(setup->request)
  432. {
  433. case USB_REQ_GET_STATUS:
  434. /* TODO */
  435. dcd_ep_write(dcd, 0, &value, 2);
  436. break;
  437. case USB_REQ_CLEAR_FEATURE:
  438. dcd_clear_feature(dcd, setup->value);
  439. break;
  440. case USB_REQ_SET_FEATURE:
  441. dcd_set_feature(dcd, setup->value);
  442. break;
  443. case USB_REQ_SYNCH_FRAME:
  444. break;
  445. default:
  446. rt_kprintf("unknown endpoint request\n");
  447. dcd_ep_stall(device->dcd, 0);
  448. break;
  449. }
  450. break;
  451. case USB_REQ_TYPE_OTHER:
  452. rt_kprintf("unknown other type request\n");
  453. dcd_ep_stall(device->dcd, 0);
  454. break;
  455. default:
  456. rt_kprintf("unknown type request\n");
  457. dcd_ep_stall(device->dcd, 0);
  458. break;
  459. }
  460. return RT_EOK;
  461. }
  462. /**
  463. * This function will handle class request.
  464. *
  465. * @param device the usb device object.
  466. * @param setup the setup request.
  467. *
  468. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  469. */
  470. static rt_err_t _class_request(udevice_t device, ureq_t setup)
  471. {
  472. uintf_t intf;
  473. uclass_t cls;
  474. /* parameter check */
  475. RT_ASSERT(device != RT_NULL);
  476. RT_ASSERT(setup != RT_NULL);
  477. /* verify request value */
  478. if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
  479. {
  480. dcd_ep_stall(device->dcd, 0);
  481. return -RT_ERROR;
  482. }
  483. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  484. {
  485. case USB_REQ_TYPE_INTERFACE:
  486. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  487. intf->handler(device, cls, setup);
  488. break;
  489. case USB_REQ_TYPE_ENDPOINT:
  490. break;
  491. default:
  492. rt_kprintf("unknown class request type\n");
  493. dcd_ep_stall(device->dcd, 0);
  494. break;
  495. }
  496. return RT_EOK;
  497. }
  498. /**
  499. * This function will handle setup request.
  500. *
  501. * @param device the usb device object.
  502. * @param setup the setup request.
  503. *
  504. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  505. */
  506. static rt_err_t _setup_request(udevice_t device, ureq_t setup)
  507. {
  508. /* parameter check */
  509. RT_ASSERT(device != RT_NULL);
  510. RT_ASSERT(setup != RT_NULL);
  511. RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
  512. RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request_handler 0x%x\n",
  513. setup->request_type));
  514. RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
  515. RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
  516. RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
  517. RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
  518. RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
  519. switch((setup->request_type & USB_REQ_TYPE_MASK))
  520. {
  521. case USB_REQ_TYPE_STANDARD:
  522. _standard_request(device, setup);
  523. break;
  524. case USB_REQ_TYPE_CLASS:
  525. _class_request(device, setup);
  526. break;
  527. case USB_REQ_TYPE_VENDOR:
  528. rt_kprintf("vendor type request\n");
  529. break;
  530. default:
  531. rt_kprintf("unknown setup request type\n");
  532. dcd_ep_stall(device->dcd, 0);
  533. return -RT_ERROR;
  534. }
  535. return RT_EOK;
  536. }
  537. /**
  538. * This function will notity sof event to all of class.
  539. *
  540. * @param device the usb device object.
  541. *
  542. * @return RT_EOK.
  543. */
  544. rt_err_t _sof_notify(udevice_t device)
  545. {
  546. struct rt_list_node *i;
  547. uclass_t cls;
  548. RT_ASSERT(device != RT_NULL);
  549. /* to notity every class that sof event comes */
  550. for (i=device->curr_cfg->cls_list.next;
  551. i!=&device->curr_cfg->cls_list; i=i->next)
  552. {
  553. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  554. if(cls->ops->sof_handler != RT_NULL)
  555. cls->ops->sof_handler(device, cls);
  556. }
  557. return RT_EOK;
  558. }
  559. /**
  560. * This function will create an usb device object.
  561. *
  562. * @param ustring the usb string array to contain string descriptor.
  563. *
  564. * @return an usb device object on success, RT_NULL on fail.
  565. */
  566. udevice_t rt_usbd_device_create(void)
  567. {
  568. udevice_t udevice;
  569. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_create\n"));
  570. /* allocate memory for the object */
  571. udevice = rt_malloc(sizeof(struct udevice));
  572. if(udevice == RT_NULL)
  573. {
  574. rt_kprintf("alloc memery failed\n");
  575. return RT_NULL;
  576. }
  577. rt_memset(udevice, 0, sizeof(struct udevice));
  578. /* to initialize configuration list */
  579. rt_list_init(&udevice->cfg_list);
  580. /* insert the device object to device list */
  581. rt_list_insert_after(&device_list, &udevice->list);
  582. return udevice;
  583. }
  584. /**
  585. * This function will set usb device string description.
  586. *
  587. * @param device the usb device object.
  588. * @param ustring pointer to string pointer array.
  589. *
  590. * @return RT_EOK.
  591. */
  592. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  593. {
  594. /* parameter check */
  595. RT_ASSERT(device != RT_NULL);
  596. RT_ASSERT(ustring != RT_NULL);
  597. /* set string descriptor array to the device object */
  598. device->str = ustring;
  599. return RT_EOK;
  600. }
  601. /**
  602. * This function will set an usb controller driver to a device.
  603. *
  604. * @param device the usb device object.
  605. * @param dcd the usb device controller driver.
  606. *
  607. * @return RT_EOK on successful.
  608. */
  609. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  610. {
  611. /* parameter check */
  612. RT_ASSERT(device != RT_NULL);
  613. RT_ASSERT(dcd != RT_NULL);
  614. /* set usb device controller driver to the device */
  615. device->dcd = dcd;
  616. return RT_EOK;
  617. }
  618. /**
  619. * This function will set an usb device descriptor to a device.
  620. *
  621. * @param device the usb device object.
  622. * @param dev_desc the usb device descriptor.
  623. *
  624. * @return RT_EOK on successful.
  625. */
  626. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  627. {
  628. /* parameter check */
  629. RT_ASSERT(device != RT_NULL);
  630. RT_ASSERT(dev_desc != RT_NULL);
  631. /* copy the usb device descriptor to the device */
  632. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  633. return RT_EOK;
  634. }
  635. /**
  636. * This function will create an usb configuration object.
  637. *
  638. * @param none.
  639. *
  640. * @return an usb configuration object.
  641. */
  642. uconfig_t rt_usbd_config_create(void)
  643. {
  644. uconfig_t cfg;
  645. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_create\n"));
  646. /* allocate memory for the object */
  647. cfg = rt_malloc(sizeof(struct uconfig));
  648. if(cfg == RT_NULL)
  649. {
  650. rt_kprintf("alloc memery failed\n");
  651. return RT_NULL;
  652. }
  653. rt_memset(cfg, 0, sizeof(struct uconfig));
  654. /* set default value */
  655. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  656. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  657. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  658. cfg->cfg_desc.bmAttributes = 0xC0;
  659. cfg->cfg_desc.MaxPower = 0x32;
  660. /* to initialize class object list */
  661. rt_list_init(&cfg->cls_list);
  662. return cfg;
  663. }
  664. /**
  665. * This function will create an usb interface object.
  666. *
  667. * @param device the usb device object.
  668. * @handler the callback handler of object
  669. *
  670. * @return an usb interface object on success, RT_NULL on fail.
  671. */
  672. uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler)
  673. {
  674. uintf_t intf;
  675. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_create\n"));
  676. /* parameter check */
  677. RT_ASSERT(device != RT_NULL);
  678. /* allocate memory for the object */
  679. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  680. if(intf == RT_NULL)
  681. {
  682. rt_kprintf("alloc memery failed\n");
  683. return RT_NULL;
  684. }
  685. intf->intf_num = device->nr_intf;
  686. device->nr_intf++;
  687. intf->handler = handler;
  688. intf->curr_setting = RT_NULL;
  689. /* to initialize the alternate setting object list */
  690. rt_list_init(&intf->setting_list);
  691. return intf;
  692. }
  693. /**
  694. * This function will create an usb alternate setting object.
  695. *
  696. * @param intf_desc the interface descriptor.
  697. * @desc_size the size of the interface descriptor.
  698. *
  699. * @return an usb alternate setting object on success, RT_NULL on fail.
  700. */
  701. ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size)
  702. {
  703. ualtsetting_t setting;
  704. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_create\n"));
  705. /* parameter check */
  706. RT_ASSERT(desc_size > 0);
  707. /* allocate memory for the object */
  708. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  709. if(setting == RT_NULL)
  710. {
  711. rt_kprintf("alloc memery failed\n");
  712. return RT_NULL;
  713. }
  714. /* allocate memory for the desc */
  715. setting->desc = rt_malloc(desc_size);
  716. if (setting->desc == RT_NULL)
  717. {
  718. rt_kprintf("alloc desc memery failed\n");
  719. rt_free(setting);
  720. return RT_NULL;
  721. }
  722. setting->desc_size = desc_size;
  723. setting->intf_desc = RT_NULL;
  724. /* to initialize endpoint list */
  725. rt_list_init(&setting->ep_list);
  726. return setting;
  727. }
  728. /**
  729. * This function will config an desc in alternate setting object.
  730. *
  731. * @param setting the altsetting to be config.
  732. * @param desc use it to init desc in setting.
  733. * @param intf_pos the offset of interface descriptor in desc.
  734. *
  735. * @return RT_EOK.
  736. */
  737. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  738. {
  739. RT_ASSERT(setting != RT_NULL);
  740. RT_ASSERT(setting->desc !=RT_NULL);
  741. rt_memcpy(setting->desc, desc, setting->desc_size);
  742. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  743. return RT_EOK;
  744. }
  745. /**
  746. * This function will create an usb class object.
  747. *
  748. * @param device the usb device object.
  749. * @param dev_desc the device descriptor.
  750. * @param ops the operation set.
  751. *
  752. * @return an usb class object on success, RT_NULL on fail.
  753. */
  754. uclass_t rt_usbd_class_create(udevice_t device, udev_desc_t dev_desc,
  755. uclass_ops_t ops)
  756. {
  757. uclass_t cls;
  758. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));
  759. /* parameter check */
  760. RT_ASSERT(device != RT_NULL);
  761. RT_ASSERT(dev_desc != RT_NULL);
  762. /* allocate memory for the object */
  763. cls = (uclass_t)rt_malloc(sizeof(struct uclass));
  764. if(cls == RT_NULL)
  765. {
  766. rt_kprintf("alloc memery failed\n");
  767. return RT_NULL;
  768. }
  769. cls->dev_desc = dev_desc;
  770. cls->ops = ops;
  771. cls->device = device;
  772. /* to initialize interface list */
  773. rt_list_init(&cls->intf_list);
  774. return cls;
  775. }
  776. /**
  777. * This function will create an usb endpoint object.
  778. *
  779. * @param ep_desc the endpoint descriptor.
  780. * @handler the callback handler of object
  781. *
  782. * @return an usb endpoint object on success, RT_NULL on fail.
  783. */
  784. uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
  785. {
  786. uep_t ep;
  787. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));
  788. /* parameter check */
  789. RT_ASSERT(ep_desc != RT_NULL);
  790. /* allocate memory for the object */
  791. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  792. if(ep == RT_NULL)
  793. {
  794. rt_kprintf("alloc memery failed\n");
  795. return RT_NULL;
  796. }
  797. ep->ep_desc = ep_desc;
  798. ep->handler = handler;
  799. ep->buffer = RT_NULL;
  800. return ep;
  801. }
  802. /**
  803. * This function will find an usb device object.
  804. *
  805. * @dcd usd device controller driver.
  806. *
  807. * @return an usb device object on found or RT_NULL on not found.
  808. */
  809. udevice_t rt_usbd_find_device(udcd_t dcd)
  810. {
  811. struct rt_list_node* node;
  812. udevice_t device;
  813. /* parameter check */
  814. RT_ASSERT(dcd != RT_NULL);
  815. /* search a device in the the device list */
  816. for (node = device_list.next; node != &device_list; node = node->next)
  817. {
  818. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  819. if(device->dcd == dcd) return device;
  820. }
  821. rt_kprintf("can't find device\n");
  822. return RT_NULL;
  823. }
  824. /**
  825. * This function will find an usb configuration object.
  826. *
  827. * @param device the usb device object.
  828. * @param value the configuration number.
  829. *
  830. * @return an usb configuration object on found or RT_NULL on not found.
  831. */
  832. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  833. {
  834. struct rt_list_node* node;
  835. uconfig_t cfg = RT_NULL;
  836. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  837. /* parameter check */
  838. RT_ASSERT(device != RT_NULL);
  839. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  840. /* search a configration in the the device */
  841. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  842. {
  843. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  844. if(cfg->cfg_desc.bConfigurationValue == value) return cfg;
  845. }
  846. rt_kprintf("can't find configuration %d\n", value);
  847. return RT_NULL;
  848. }
  849. /**
  850. * This function will find an usb interface object.
  851. *
  852. * @param device the usb device object.
  853. * @param value the interface number.
  854. *
  855. * @return an usb configuration object on found or RT_NULL on not found.
  856. */
  857. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, uclass_t *pcls)
  858. {
  859. struct rt_list_node *i, *j;
  860. uclass_t cls;
  861. uintf_t intf;
  862. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  863. /* parameter check */
  864. RT_ASSERT(device != RT_NULL);
  865. RT_ASSERT(value < device->nr_intf);
  866. /* search an interface in the current configuration */
  867. for (i=device->curr_cfg->cls_list.next;
  868. i!=&device->curr_cfg->cls_list; i=i->next)
  869. {
  870. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  871. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  872. {
  873. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  874. if(intf->intf_num == value)
  875. {
  876. if (pcls != RT_NULL)
  877. *pcls = cls;
  878. return intf;
  879. }
  880. }
  881. }
  882. rt_kprintf("can't find interface %d\n", value);
  883. return RT_NULL;
  884. }
  885. /**
  886. * This function will find an usb interface alternate setting object.
  887. *
  888. * @param device the usb device object.
  889. * @param value the alternate setting number.
  890. *
  891. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  892. */
  893. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  894. {
  895. struct rt_list_node *i;
  896. ualtsetting_t setting;
  897. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  898. /* parameter check */
  899. RT_ASSERT(intf != RT_NULL);
  900. if(intf->curr_setting != RT_NULL)
  901. {
  902. /* if the value equal to the current alternate setting, then do not search */
  903. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  904. return intf->curr_setting;
  905. }
  906. /* search a setting in the alternate setting list */
  907. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  908. {
  909. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  910. if(setting->intf_desc->bAlternateSetting == value)
  911. return setting;
  912. }
  913. rt_kprintf("can't find alternate setting %d\n", value);
  914. return RT_NULL;
  915. }
  916. /**
  917. * This function will find an usb endpoint object.
  918. *
  919. * @param device the usb device object.
  920. * @param ep_addr endpoint address.
  921. *
  922. * @return an usb endpoint object on found or RT_NULL on not found.
  923. */
  924. uep_t rt_usbd_find_endpoint(udevice_t device, uclass_t* pcls, rt_uint8_t ep_addr)
  925. {
  926. uep_t ep;
  927. struct rt_list_node *i, *j, *k;
  928. uclass_t cls;
  929. uintf_t intf;
  930. /* parameter check */
  931. RT_ASSERT(device != RT_NULL);
  932. /* search a endpoint in the current configuration */
  933. for (i=device->curr_cfg->cls_list.next;
  934. i!=&device->curr_cfg->cls_list; i=i->next)
  935. {
  936. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  937. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  938. {
  939. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  940. for(k=intf->curr_setting->ep_list.next;
  941. k!=&intf->curr_setting->ep_list; k=k->next)
  942. {
  943. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  944. if(ep->ep_desc->bEndpointAddress == ep_addr)
  945. {
  946. if (pcls != RT_NULL)
  947. *pcls = cls;
  948. return ep;
  949. }
  950. }
  951. }
  952. }
  953. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  954. return RT_NULL;
  955. }
  956. /**
  957. * This function will add a configuration to an usb device.
  958. *
  959. * @param device the usb device object.
  960. * @param cfg the configuration object.
  961. *
  962. * @return RT_EOK.
  963. */
  964. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  965. {
  966. struct rt_list_node *i, *j, *k;
  967. uclass_t cls;
  968. uintf_t intf;
  969. uep_t ep;
  970. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  971. /* parameter check */
  972. RT_ASSERT(device != RT_NULL);
  973. RT_ASSERT(cfg != RT_NULL);
  974. /* set configuration number to the configuration descriptor */
  975. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  976. device->dev_desc.bNumConfigurations++;
  977. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  978. {
  979. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  980. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  981. {
  982. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  983. cfg->cfg_desc.bNumInterfaces++;
  984. /* allocate address for every endpoint in the interface alternate setting */
  985. for(k=intf->curr_setting->ep_list.next;
  986. k!=&intf->curr_setting->ep_list; k=k->next)
  987. {
  988. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  989. dcd_ep_alloc(device->dcd, ep);
  990. }
  991. /* construct complete configuration descriptor */
  992. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength -
  993. USB_DESC_LENGTH_CONFIG], (void*)intf->curr_setting->desc,
  994. intf->curr_setting->desc_size);
  995. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  996. }
  997. }
  998. /* insert the configuration to the list */
  999. rt_list_insert_after(&device->cfg_list, &cfg->list);
  1000. return RT_EOK;
  1001. }
  1002. /**
  1003. * This function will add a class to a configuration.
  1004. *
  1005. * @param cfg the configuration object.
  1006. * @param cls the class object.
  1007. *
  1008. * @return RT_EOK.
  1009. */
  1010. rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
  1011. {
  1012. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));
  1013. /* parameter check */
  1014. RT_ASSERT(cfg != RT_NULL);
  1015. RT_ASSERT(cls != RT_NULL);
  1016. /* insert the class to the list */
  1017. rt_list_insert_after(&cfg->cls_list, &cls->list);
  1018. return RT_EOK;
  1019. }
  1020. /**
  1021. * This function will add an interface to a class.
  1022. *
  1023. * @param cls the class object.
  1024. * @param intf the interface object.
  1025. *
  1026. * @return RT_EOK.
  1027. */
  1028. rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
  1029. {
  1030. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));
  1031. /* parameter check */
  1032. RT_ASSERT(cls != RT_NULL);
  1033. RT_ASSERT(intf != RT_NULL);
  1034. /* insert the interface to the list */
  1035. rt_list_insert_after(&cls->intf_list, &intf->list);
  1036. return RT_EOK;
  1037. }
  1038. /**
  1039. * This function will add an alternate setting to an interface.
  1040. *
  1041. * @param intf the interface object.
  1042. * @param setting the alternate setting object.
  1043. *
  1044. * @return RT_EOK.
  1045. */
  1046. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1047. {
  1048. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1049. /* parameter check */
  1050. RT_ASSERT(intf != RT_NULL);
  1051. RT_ASSERT(setting != RT_NULL);
  1052. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1053. /* insert the alternate setting to the list */
  1054. rt_list_insert_after(&intf->setting_list, &setting->list);
  1055. return RT_EOK;
  1056. }
  1057. /**
  1058. * This function will add an endpoint to an alternate setting.
  1059. *
  1060. * @param setting the alternate setting object.
  1061. * @param ep the endpoint object.
  1062. *
  1063. * @return RT_EOK.
  1064. */
  1065. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1066. {
  1067. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1068. /* parameter check */
  1069. RT_ASSERT(setting != RT_NULL);
  1070. RT_ASSERT(ep != RT_NULL);
  1071. /* insert the endpoint to the list */
  1072. rt_list_insert_after(&setting->ep_list, &ep->list);
  1073. return RT_EOK;
  1074. }
  1075. /**
  1076. * This function will set an alternate setting for an interface.
  1077. *
  1078. * @param intf_desc the interface descriptor.
  1079. * @param value the alternate setting number.
  1080. *
  1081. * @return RT_EOK.
  1082. */
  1083. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1084. {
  1085. ualtsetting_t setting;
  1086. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1087. /* parameter check */
  1088. RT_ASSERT(intf != RT_NULL);
  1089. /* find an alternate setting */
  1090. setting = rt_usbd_find_altsetting(intf, value);
  1091. /* set as current alternate setting */
  1092. intf->curr_setting = setting;
  1093. return RT_EOK;
  1094. }
  1095. /**
  1096. * This function will set a configuration for an usb device.
  1097. *
  1098. * @param device the usb device object.
  1099. * @param value the configuration number.
  1100. *
  1101. * @return RT_EOK.
  1102. */
  1103. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1104. {
  1105. uconfig_t cfg;
  1106. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1107. /* parameter check */
  1108. RT_ASSERT(device != RT_NULL);
  1109. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1110. /* find a configuration */
  1111. cfg = rt_usbd_find_config(device, value);
  1112. /* set as current configuration */
  1113. device->curr_cfg = cfg;
  1114. return RT_TRUE;
  1115. }
  1116. static struct rt_messagequeue usb_mq;
  1117. /**
  1118. * This function is the main entry of usb device thread, it is in charge of
  1119. * processing all messages received from the usb message buffer.
  1120. *
  1121. * @param parameter the parameter of the usb device thread.
  1122. *
  1123. * @return none.
  1124. */
  1125. static void rt_usbd_thread_entry(void* parameter)
  1126. {
  1127. while(1)
  1128. {
  1129. struct udev_msg msg;
  1130. udevice_t device;
  1131. uclass_t cls;
  1132. uep_t ep;
  1133. /* receive message */
  1134. if(rt_mq_recv(&usb_mq,
  1135. &msg, sizeof(struct udev_msg),
  1136. RT_WAITING_FOREVER) != RT_EOK )
  1137. continue;
  1138. device = rt_usbd_find_device(msg.dcd);
  1139. if(device == RT_NULL)
  1140. {
  1141. rt_kprintf("invalid usb device\n");
  1142. continue;
  1143. }
  1144. switch (msg.type)
  1145. {
  1146. case USB_MSG_SOF:
  1147. _sof_notify(device);
  1148. break;
  1149. case USB_MSG_DATA_NOTIFY:
  1150. ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
  1151. if(ep != RT_NULL)
  1152. ep->handler(device, cls, msg.content.ep_msg.size);
  1153. else
  1154. rt_kprintf("invalid endpoint\n");
  1155. break;
  1156. case USB_MSG_SETUP_NOTIFY:
  1157. _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
  1158. break;
  1159. default:
  1160. rt_kprintf("unknown msg type\n");
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. /**
  1166. * This function will post an message to usb message queue,
  1167. *
  1168. * @param msg the message to be posted
  1169. * @param size the size of the message .
  1170. *
  1171. * @return the error code, RT_EOK on successfully.
  1172. */
  1173. rt_err_t rt_usbd_post_event(struct udev_msg* msg, rt_size_t size)
  1174. {
  1175. RT_ASSERT(msg != RT_NULL);
  1176. /* send message to usb message queue */
  1177. return rt_mq_send(&usb_mq, (void*)msg, size);
  1178. }
  1179. ALIGN(RT_ALIGN_SIZE)
  1180. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1181. static struct rt_thread usb_thread;
  1182. #define USBD_MQ_MSG_SZ 32
  1183. #define USBD_MQ_MAX_MSG 16
  1184. /* internal of the message queue: every message is associated with a pointer,
  1185. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1186. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1187. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1188. /**
  1189. * This function will initialize usb device thread.
  1190. *
  1191. * @return none.
  1192. *
  1193. */
  1194. rt_err_t rt_usbd_core_init(void)
  1195. {
  1196. rt_list_init(&device_list);
  1197. /* create an usb message queue */
  1198. rt_mq_init(&usb_mq, "usbd", usb_mq_pool, USBD_MQ_MSG_SZ,
  1199. sizeof(usb_mq_pool), RT_IPC_FLAG_FIFO);
  1200. /* init usb device thread */
  1201. rt_thread_init(&usb_thread, "usbd", rt_usbd_thread_entry, RT_NULL,
  1202. usb_thread_stack, RT_USBD_THREAD_STACK_SZ, RT_USBD_THREAD_PRIO, 20);
  1203. /* rt_thread_init should always be OK, so start the thread without further
  1204. * checking. */
  1205. return rt_thread_startup(&usb_thread);
  1206. }