core.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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. dcd_send_status(device->dcd);
  220. return RT_EOK;
  221. }
  222. /**
  223. * This function will handle get_config request.
  224. *
  225. * @param device the usb device object.
  226. * @param setup the setup request.
  227. *
  228. * @return RT_EOK on successful.
  229. */
  230. static rt_err_t _get_config(struct udevice* device, ureq_t setup)
  231. {
  232. rt_uint8_t value;
  233. /* parameter check */
  234. RT_ASSERT(device != RT_NULL);
  235. RT_ASSERT(setup != RT_NULL);
  236. RT_ASSERT(device->curr_cfg != RT_NULL);
  237. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
  238. if (device->state == USB_STATE_CONFIGURED)
  239. {
  240. /* get current configuration */
  241. value = device->curr_cfg->cfg_desc.bConfigurationValue;
  242. }
  243. else
  244. {
  245. value = 0;
  246. }
  247. /* write the current configuration to endpoint 0 */
  248. dcd_ep_write(device->dcd, 0, &value, 1);
  249. return RT_EOK;
  250. }
  251. /**
  252. * This function will handle set_config request.
  253. *
  254. * @param device the usb device object.
  255. * @param setup the setup request.
  256. *
  257. * @return RT_EOK on successful.
  258. */
  259. static rt_err_t _set_config(struct udevice* device, ureq_t setup)
  260. {
  261. struct rt_list_node *i, *j, *k;
  262. uconfig_t cfg;
  263. uintf_t intf;
  264. ualtsetting_t setting;
  265. uep_t ep;
  266. /* parameter check */
  267. RT_ASSERT(device != RT_NULL);
  268. RT_ASSERT(setup != RT_NULL);
  269. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
  270. if (setup->value > device->dev_desc.bNumConfigurations)
  271. {
  272. dcd_ep_stall(device->dcd, 0);
  273. return -RT_ERROR;
  274. }
  275. if (setup->value == 0)
  276. {
  277. RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
  278. device->state = USB_STATE_ADDRESS;
  279. goto _exit;
  280. }
  281. /* set current configuration */
  282. rt_usbd_set_config(device, setup->value);
  283. cfg = device->curr_cfg;
  284. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  285. {
  286. /* run all classes and their endpoints in the configuration */
  287. uclass_t cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  288. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  289. {
  290. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  291. setting = intf->curr_setting;
  292. for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
  293. {
  294. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  295. /* first stop then start endpoint */
  296. dcd_ep_stop(device->dcd, ep);
  297. dcd_ep_run(device->dcd, ep);
  298. }
  299. }
  300. /* after running all endpoints, then run class */
  301. if(cls->ops->run != RT_NULL)
  302. cls->ops->run(device, cls);
  303. }
  304. device->state = USB_STATE_CONFIGURED;
  305. _exit:
  306. /* issue status stage */
  307. dcd_send_status(device->dcd);
  308. return RT_EOK;
  309. }
  310. /**
  311. * This function will handle set_address request.
  312. *
  313. * @param device the usb device object.
  314. * @param setup the setup request.
  315. *
  316. * @return RT_EOK on successful.
  317. */
  318. static rt_err_t _set_address(struct udevice* device, ureq_t setup)
  319. {
  320. /* parameter check */
  321. RT_ASSERT(device != RT_NULL);
  322. RT_ASSERT(setup != RT_NULL);
  323. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
  324. /* set address in device control driver */
  325. dcd_set_address(device->dcd, setup->value);
  326. device->state = USB_STATE_ADDRESS;
  327. /* issue status stage */
  328. dcd_send_status(device->dcd);
  329. return RT_EOK;
  330. }
  331. /**
  332. * This function will handle standard request to
  333. * interface that defined in class-specifics
  334. *
  335. * @param device the usb device object.
  336. * @param setup the setup request.
  337. *
  338. * @return RT_EOK on successful.
  339. */
  340. static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
  341. {
  342. uintf_t intf;
  343. uclass_t cls;
  344. rt_err_t ret;
  345. /* parameter check */
  346. RT_ASSERT(device != RT_NULL);
  347. RT_ASSERT(setup != RT_NULL);
  348. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
  349. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  350. if (intf != RT_NULL)
  351. {
  352. ret = intf->handler(device, cls, setup);
  353. }
  354. else
  355. {
  356. ret = -RT_ERROR;
  357. }
  358. return ret;
  359. }
  360. /**
  361. * This function will handle standard request.
  362. *
  363. * @param device the usb device object.
  364. * @param setup the setup request.
  365. *
  366. * @return RT_EOK on successful.
  367. */
  368. static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
  369. {
  370. udcd_t dcd;
  371. rt_uint16_t value = 0;
  372. /* parameter check */
  373. RT_ASSERT(device != RT_NULL);
  374. RT_ASSERT(setup != RT_NULL);
  375. dcd = device->dcd;
  376. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  377. {
  378. case USB_REQ_TYPE_DEVICE:
  379. switch(setup->request)
  380. {
  381. case USB_REQ_GET_STATUS:
  382. dcd_ep_write(device->dcd, 0, &value, 2);
  383. break;
  384. case USB_REQ_CLEAR_FEATURE:
  385. dcd_clear_feature(dcd, setup->value, setup->index);
  386. dcd_send_status(dcd);
  387. break;
  388. case USB_REQ_SET_FEATURE:
  389. dcd_set_feature(dcd, setup->value, setup->index);
  390. break;
  391. case USB_REQ_SET_ADDRESS:
  392. _set_address(device, setup);
  393. break;
  394. case USB_REQ_GET_DESCRIPTOR:
  395. _get_descriptor(device, setup);
  396. break;
  397. case USB_REQ_SET_DESCRIPTOR:
  398. dcd_ep_stall(dcd, 0);
  399. break;
  400. case USB_REQ_GET_CONFIGURATION:
  401. _get_config(device, setup);
  402. break;
  403. case USB_REQ_SET_CONFIGURATION:
  404. _set_config(device, setup);
  405. break;
  406. default:
  407. rt_kprintf("unknown device request\n");
  408. dcd_ep_stall(device->dcd, 0);
  409. break;
  410. }
  411. break;
  412. case USB_REQ_TYPE_INTERFACE:
  413. switch(setup->request)
  414. {
  415. case USB_REQ_GET_INTERFACE:
  416. _get_interface(device, setup);
  417. break;
  418. case USB_REQ_SET_INTERFACE:
  419. _set_interface(device, setup);
  420. break;
  421. default:
  422. if (_request_interface(device, setup) != RT_EOK)
  423. {
  424. rt_kprintf("unknown interface request\n");
  425. dcd_ep_stall(device->dcd, 0);
  426. return - RT_ERROR;
  427. }
  428. else
  429. break;
  430. }
  431. break;
  432. case USB_REQ_TYPE_ENDPOINT:
  433. switch(setup->request)
  434. {
  435. case USB_REQ_GET_STATUS:
  436. {
  437. /* TODO */
  438. uep_t ep;
  439. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  440. value = ep->is_stall;
  441. dcd_ep_write(dcd, 0, &value, 2);
  442. }
  443. break;
  444. case USB_REQ_CLEAR_FEATURE:
  445. {
  446. uep_t ep;
  447. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  448. ep->is_stall = 0;
  449. dcd_clear_feature(dcd, setup->value, setup->index);
  450. dcd_send_status(dcd);
  451. }
  452. break;
  453. case USB_REQ_SET_FEATURE:
  454. {
  455. uep_t ep;
  456. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  457. ep->is_stall = 1;
  458. dcd_set_feature(dcd, setup->value, setup->index);
  459. dcd_send_status(dcd);
  460. }
  461. break;
  462. case USB_REQ_SYNCH_FRAME:
  463. break;
  464. default:
  465. rt_kprintf("unknown endpoint request\n");
  466. dcd_ep_stall(device->dcd, 0);
  467. break;
  468. }
  469. break;
  470. case USB_REQ_TYPE_OTHER:
  471. rt_kprintf("unknown other type request\n");
  472. dcd_ep_stall(device->dcd, 0);
  473. break;
  474. default:
  475. rt_kprintf("unknown type request\n");
  476. dcd_ep_stall(device->dcd, 0);
  477. break;
  478. }
  479. return RT_EOK;
  480. }
  481. /**
  482. * This function will handle class request.
  483. *
  484. * @param device the usb device object.
  485. * @param setup the setup request.
  486. *
  487. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  488. */
  489. static rt_err_t _class_request(udevice_t device, ureq_t setup)
  490. {
  491. uintf_t intf;
  492. uclass_t cls;
  493. /* parameter check */
  494. RT_ASSERT(device != RT_NULL);
  495. RT_ASSERT(setup != RT_NULL);
  496. /* verify request value */
  497. if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
  498. {
  499. dcd_ep_stall(device->dcd, 0);
  500. return -RT_ERROR;
  501. }
  502. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  503. {
  504. case USB_REQ_TYPE_INTERFACE:
  505. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  506. intf->handler(device, cls, setup);
  507. break;
  508. case USB_REQ_TYPE_ENDPOINT:
  509. break;
  510. default:
  511. rt_kprintf("unknown class request type\n");
  512. dcd_ep_stall(device->dcd, 0);
  513. break;
  514. }
  515. return RT_EOK;
  516. }
  517. /**
  518. * This function will handle setup request.
  519. *
  520. * @param device the usb device object.
  521. * @param setup the setup request.
  522. *
  523. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  524. */
  525. static rt_err_t _setup_request(udevice_t device, ureq_t setup)
  526. {
  527. /* parameter check */
  528. RT_ASSERT(device != RT_NULL);
  529. RT_ASSERT(setup != RT_NULL);
  530. RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
  531. RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request_handler 0x%x\n",
  532. setup->request_type));
  533. RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
  534. RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
  535. RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
  536. RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
  537. RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
  538. switch((setup->request_type & USB_REQ_TYPE_MASK))
  539. {
  540. case USB_REQ_TYPE_STANDARD:
  541. _standard_request(device, setup);
  542. break;
  543. case USB_REQ_TYPE_CLASS:
  544. _class_request(device, setup);
  545. break;
  546. case USB_REQ_TYPE_VENDOR:
  547. rt_kprintf("vendor type request\n");
  548. break;
  549. default:
  550. rt_kprintf("unknown setup request type\n");
  551. dcd_ep_stall(device->dcd, 0);
  552. return -RT_ERROR;
  553. }
  554. return RT_EOK;
  555. }
  556. /**
  557. * This function will notity sof event to all of class.
  558. *
  559. * @param device the usb device object.
  560. *
  561. * @return RT_EOK.
  562. */
  563. rt_err_t _sof_notify(udevice_t device)
  564. {
  565. struct rt_list_node *i;
  566. uclass_t cls;
  567. RT_ASSERT(device != RT_NULL);
  568. /* to notity every class that sof event comes */
  569. for (i=device->curr_cfg->cls_list.next;
  570. i!=&device->curr_cfg->cls_list; i=i->next)
  571. {
  572. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  573. if(cls->ops->sof_handler != RT_NULL)
  574. cls->ops->sof_handler(device, cls);
  575. }
  576. return RT_EOK;
  577. }
  578. /**
  579. * This function will reset all class.
  580. *
  581. * @param device the usb device object.
  582. *
  583. * @return RT_EOK.
  584. */
  585. rt_err_t _reset_notify(udevice_t device)
  586. {
  587. struct rt_list_node *i;
  588. uclass_t cls;
  589. RT_ASSERT(device != RT_NULL);
  590. /* to notity every class that sof event comes */
  591. for (i=device->curr_cfg->cls_list.next;
  592. i!=&device->curr_cfg->cls_list; i=i->next)
  593. {
  594. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  595. if(cls->ops->stop != RT_NULL)
  596. cls->ops->stop(device, cls);
  597. if(cls->ops->run != RT_NULL)
  598. cls->ops->run(device, cls);
  599. }
  600. return RT_EOK;
  601. }
  602. /**
  603. * This function will create an usb device object.
  604. *
  605. * @param ustring the usb string array to contain string descriptor.
  606. *
  607. * @return an usb device object on success, RT_NULL on fail.
  608. */
  609. udevice_t rt_usbd_device_create(void)
  610. {
  611. udevice_t udevice;
  612. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_create\n"));
  613. /* allocate memory for the object */
  614. udevice = rt_malloc(sizeof(struct udevice));
  615. if(udevice == RT_NULL)
  616. {
  617. rt_kprintf("alloc memery failed\n");
  618. return RT_NULL;
  619. }
  620. rt_memset(udevice, 0, sizeof(struct udevice));
  621. /* to initialize configuration list */
  622. rt_list_init(&udevice->cfg_list);
  623. /* insert the device object to device list */
  624. rt_list_insert_after(&device_list, &udevice->list);
  625. return udevice;
  626. }
  627. /**
  628. * This function will set usb device string description.
  629. *
  630. * @param device the usb device object.
  631. * @param ustring pointer to string pointer array.
  632. *
  633. * @return RT_EOK.
  634. */
  635. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  636. {
  637. /* parameter check */
  638. RT_ASSERT(device != RT_NULL);
  639. RT_ASSERT(ustring != RT_NULL);
  640. /* set string descriptor array to the device object */
  641. device->str = ustring;
  642. return RT_EOK;
  643. }
  644. /**
  645. * This function will set an usb controller driver to a device.
  646. *
  647. * @param device the usb device object.
  648. * @param dcd the usb device controller driver.
  649. *
  650. * @return RT_EOK on successful.
  651. */
  652. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  653. {
  654. /* parameter check */
  655. RT_ASSERT(device != RT_NULL);
  656. RT_ASSERT(dcd != RT_NULL);
  657. /* set usb device controller driver to the device */
  658. device->dcd = dcd;
  659. return RT_EOK;
  660. }
  661. /**
  662. * This function will set an usb device descriptor to a device.
  663. *
  664. * @param device the usb device object.
  665. * @param dev_desc the usb device descriptor.
  666. *
  667. * @return RT_EOK on successful.
  668. */
  669. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  670. {
  671. /* parameter check */
  672. RT_ASSERT(device != RT_NULL);
  673. RT_ASSERT(dev_desc != RT_NULL);
  674. /* copy the usb device descriptor to the device */
  675. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  676. return RT_EOK;
  677. }
  678. /**
  679. * This function will create an usb configuration object.
  680. *
  681. * @param none.
  682. *
  683. * @return an usb configuration object.
  684. */
  685. uconfig_t rt_usbd_config_create(void)
  686. {
  687. uconfig_t cfg;
  688. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_create\n"));
  689. /* allocate memory for the object */
  690. cfg = rt_malloc(sizeof(struct uconfig));
  691. if(cfg == RT_NULL)
  692. {
  693. rt_kprintf("alloc memery failed\n");
  694. return RT_NULL;
  695. }
  696. rt_memset(cfg, 0, sizeof(struct uconfig));
  697. /* set default value */
  698. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  699. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  700. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  701. cfg->cfg_desc.bmAttributes = 0xC0;
  702. cfg->cfg_desc.MaxPower = 0x32;
  703. /* to initialize class object list */
  704. rt_list_init(&cfg->cls_list);
  705. return cfg;
  706. }
  707. /**
  708. * This function will create an usb interface object.
  709. *
  710. * @param device the usb device object.
  711. * @handler the callback handler of object
  712. *
  713. * @return an usb interface object on success, RT_NULL on fail.
  714. */
  715. uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler)
  716. {
  717. uintf_t intf;
  718. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_create\n"));
  719. /* parameter check */
  720. RT_ASSERT(device != RT_NULL);
  721. /* allocate memory for the object */
  722. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  723. if(intf == RT_NULL)
  724. {
  725. rt_kprintf("alloc memery failed\n");
  726. return RT_NULL;
  727. }
  728. intf->intf_num = device->nr_intf;
  729. device->nr_intf++;
  730. intf->handler = handler;
  731. intf->curr_setting = RT_NULL;
  732. /* to initialize the alternate setting object list */
  733. rt_list_init(&intf->setting_list);
  734. return intf;
  735. }
  736. /**
  737. * This function will create an usb alternate setting object.
  738. *
  739. * @param intf_desc the interface descriptor.
  740. * @desc_size the size of the interface descriptor.
  741. *
  742. * @return an usb alternate setting object on success, RT_NULL on fail.
  743. */
  744. ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size)
  745. {
  746. ualtsetting_t setting;
  747. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_create\n"));
  748. /* parameter check */
  749. RT_ASSERT(desc_size > 0);
  750. /* allocate memory for the object */
  751. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  752. if(setting == RT_NULL)
  753. {
  754. rt_kprintf("alloc memery failed\n");
  755. return RT_NULL;
  756. }
  757. /* allocate memory for the desc */
  758. setting->desc = rt_malloc(desc_size);
  759. if (setting->desc == RT_NULL)
  760. {
  761. rt_kprintf("alloc desc memery failed\n");
  762. rt_free(setting);
  763. return RT_NULL;
  764. }
  765. setting->desc_size = desc_size;
  766. setting->intf_desc = RT_NULL;
  767. /* to initialize endpoint list */
  768. rt_list_init(&setting->ep_list);
  769. return setting;
  770. }
  771. /**
  772. * This function will config an desc in alternate setting object.
  773. *
  774. * @param setting the altsetting to be config.
  775. * @param desc use it to init desc in setting.
  776. * @param intf_pos the offset of interface descriptor in desc.
  777. *
  778. * @return RT_EOK.
  779. */
  780. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  781. {
  782. RT_ASSERT(setting != RT_NULL);
  783. RT_ASSERT(setting->desc !=RT_NULL);
  784. rt_memcpy(setting->desc, desc, setting->desc_size);
  785. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  786. return RT_EOK;
  787. }
  788. /**
  789. * This function will create an usb class object.
  790. *
  791. * @param device the usb device object.
  792. * @param dev_desc the device descriptor.
  793. * @param ops the operation set.
  794. *
  795. * @return an usb class object on success, RT_NULL on fail.
  796. */
  797. uclass_t rt_usbd_class_create(udevice_t device, udev_desc_t dev_desc,
  798. uclass_ops_t ops)
  799. {
  800. uclass_t cls;
  801. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));
  802. /* parameter check */
  803. RT_ASSERT(device != RT_NULL);
  804. RT_ASSERT(dev_desc != RT_NULL);
  805. /* allocate memory for the object */
  806. cls = (uclass_t)rt_malloc(sizeof(struct uclass));
  807. if(cls == RT_NULL)
  808. {
  809. rt_kprintf("alloc memery failed\n");
  810. return RT_NULL;
  811. }
  812. cls->dev_desc = dev_desc;
  813. cls->ops = ops;
  814. cls->device = device;
  815. /* to initialize interface list */
  816. rt_list_init(&cls->intf_list);
  817. return cls;
  818. }
  819. /**
  820. * This function will create an usb endpoint object.
  821. *
  822. * @param ep_desc the endpoint descriptor.
  823. * @handler the callback handler of object
  824. *
  825. * @return an usb endpoint object on success, RT_NULL on fail.
  826. */
  827. uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
  828. {
  829. uep_t ep;
  830. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));
  831. /* parameter check */
  832. RT_ASSERT(ep_desc != RT_NULL);
  833. /* allocate memory for the object */
  834. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  835. if(ep == RT_NULL)
  836. {
  837. rt_kprintf("alloc memery failed\n");
  838. return RT_NULL;
  839. }
  840. ep->ep_desc = ep_desc;
  841. ep->handler = handler;
  842. ep->buffer = RT_NULL;
  843. return ep;
  844. }
  845. /**
  846. * This function will find an usb device object.
  847. *
  848. * @dcd usd device controller driver.
  849. *
  850. * @return an usb device object on found or RT_NULL on not found.
  851. */
  852. udevice_t rt_usbd_find_device(udcd_t dcd)
  853. {
  854. struct rt_list_node* node;
  855. udevice_t device;
  856. /* parameter check */
  857. RT_ASSERT(dcd != RT_NULL);
  858. /* search a device in the the device list */
  859. for (node = device_list.next; node != &device_list; node = node->next)
  860. {
  861. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  862. if(device->dcd == dcd) return device;
  863. }
  864. rt_kprintf("can't find device\n");
  865. return RT_NULL;
  866. }
  867. /**
  868. * This function will find an usb configuration object.
  869. *
  870. * @param device the usb device object.
  871. * @param value the configuration number.
  872. *
  873. * @return an usb configuration object on found or RT_NULL on not found.
  874. */
  875. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  876. {
  877. struct rt_list_node* node;
  878. uconfig_t cfg = RT_NULL;
  879. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  880. /* parameter check */
  881. RT_ASSERT(device != RT_NULL);
  882. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  883. /* search a configration in the the device */
  884. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  885. {
  886. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  887. if(cfg->cfg_desc.bConfigurationValue == value) return cfg;
  888. }
  889. rt_kprintf("can't find configuration %d\n", value);
  890. return RT_NULL;
  891. }
  892. /**
  893. * This function will find an usb interface object.
  894. *
  895. * @param device the usb device object.
  896. * @param value the interface number.
  897. *
  898. * @return an usb configuration object on found or RT_NULL on not found.
  899. */
  900. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, uclass_t *pcls)
  901. {
  902. struct rt_list_node *i, *j;
  903. uclass_t cls;
  904. uintf_t intf;
  905. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  906. /* parameter check */
  907. RT_ASSERT(device != RT_NULL);
  908. RT_ASSERT(value < device->nr_intf);
  909. /* search an interface in the current configuration */
  910. for (i=device->curr_cfg->cls_list.next;
  911. i!=&device->curr_cfg->cls_list; i=i->next)
  912. {
  913. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  914. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  915. {
  916. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  917. if(intf->intf_num == value)
  918. {
  919. if (pcls != RT_NULL)
  920. *pcls = cls;
  921. return intf;
  922. }
  923. }
  924. }
  925. rt_kprintf("can't find interface %d\n", value);
  926. return RT_NULL;
  927. }
  928. /**
  929. * This function will find an usb interface alternate setting object.
  930. *
  931. * @param device the usb device object.
  932. * @param value the alternate setting number.
  933. *
  934. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  935. */
  936. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  937. {
  938. struct rt_list_node *i;
  939. ualtsetting_t setting;
  940. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  941. /* parameter check */
  942. RT_ASSERT(intf != RT_NULL);
  943. if(intf->curr_setting != RT_NULL)
  944. {
  945. /* if the value equal to the current alternate setting, then do not search */
  946. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  947. return intf->curr_setting;
  948. }
  949. /* search a setting in the alternate setting list */
  950. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  951. {
  952. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  953. if(setting->intf_desc->bAlternateSetting == value)
  954. return setting;
  955. }
  956. rt_kprintf("can't find alternate setting %d\n", value);
  957. return RT_NULL;
  958. }
  959. /**
  960. * This function will find an usb endpoint object.
  961. *
  962. * @param device the usb device object.
  963. * @param ep_addr endpoint address.
  964. *
  965. * @return an usb endpoint object on found or RT_NULL on not found.
  966. */
  967. uep_t rt_usbd_find_endpoint(udevice_t device, uclass_t* pcls, rt_uint8_t ep_addr)
  968. {
  969. uep_t ep;
  970. struct rt_list_node *i, *j, *k;
  971. uclass_t cls;
  972. uintf_t intf;
  973. /* parameter check */
  974. RT_ASSERT(device != RT_NULL);
  975. /* search a endpoint in the current configuration */
  976. for (i=device->curr_cfg->cls_list.next;
  977. i!=&device->curr_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. for(k=intf->curr_setting->ep_list.next;
  984. k!=&intf->curr_setting->ep_list; k=k->next)
  985. {
  986. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  987. if(ep->ep_desc->bEndpointAddress == ep_addr)
  988. {
  989. if (pcls != RT_NULL)
  990. *pcls = cls;
  991. return ep;
  992. }
  993. }
  994. }
  995. }
  996. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  997. return RT_NULL;
  998. }
  999. /**
  1000. * This function will add a configuration to an usb device.
  1001. *
  1002. * @param device the usb device object.
  1003. * @param cfg the configuration object.
  1004. *
  1005. * @return RT_EOK.
  1006. */
  1007. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  1008. {
  1009. struct rt_list_node *i, *j, *k;
  1010. uclass_t cls;
  1011. uintf_t intf;
  1012. uep_t ep;
  1013. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  1014. /* parameter check */
  1015. RT_ASSERT(device != RT_NULL);
  1016. RT_ASSERT(cfg != RT_NULL);
  1017. /* set configuration number to the configuration descriptor */
  1018. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  1019. device->dev_desc.bNumConfigurations++;
  1020. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  1021. {
  1022. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  1023. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  1024. {
  1025. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1026. cfg->cfg_desc.bNumInterfaces++;
  1027. /* allocate address for every endpoint in the interface alternate setting */
  1028. for(k=intf->curr_setting->ep_list.next;
  1029. k!=&intf->curr_setting->ep_list; k=k->next)
  1030. {
  1031. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1032. dcd_ep_alloc(device->dcd, ep);
  1033. }
  1034. /* construct complete configuration descriptor */
  1035. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength -
  1036. USB_DESC_LENGTH_CONFIG], (void*)intf->curr_setting->desc,
  1037. intf->curr_setting->desc_size);
  1038. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  1039. }
  1040. }
  1041. /* insert the configuration to the list */
  1042. rt_list_insert_after(&device->cfg_list, &cfg->list);
  1043. return RT_EOK;
  1044. }
  1045. /**
  1046. * This function will add a class to a configuration.
  1047. *
  1048. * @param cfg the configuration object.
  1049. * @param cls the class object.
  1050. *
  1051. * @return RT_EOK.
  1052. */
  1053. rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
  1054. {
  1055. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));
  1056. /* parameter check */
  1057. RT_ASSERT(cfg != RT_NULL);
  1058. RT_ASSERT(cls != RT_NULL);
  1059. /* insert the class to the list */
  1060. rt_list_insert_after(&cfg->cls_list, &cls->list);
  1061. return RT_EOK;
  1062. }
  1063. /**
  1064. * This function will add an interface to a class.
  1065. *
  1066. * @param cls the class object.
  1067. * @param intf the interface object.
  1068. *
  1069. * @return RT_EOK.
  1070. */
  1071. rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
  1072. {
  1073. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));
  1074. /* parameter check */
  1075. RT_ASSERT(cls != RT_NULL);
  1076. RT_ASSERT(intf != RT_NULL);
  1077. /* insert the interface to the list */
  1078. rt_list_insert_after(&cls->intf_list, &intf->list);
  1079. return RT_EOK;
  1080. }
  1081. /**
  1082. * This function will add an alternate setting to an interface.
  1083. *
  1084. * @param intf the interface object.
  1085. * @param setting the alternate setting object.
  1086. *
  1087. * @return RT_EOK.
  1088. */
  1089. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1090. {
  1091. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1092. /* parameter check */
  1093. RT_ASSERT(intf != RT_NULL);
  1094. RT_ASSERT(setting != RT_NULL);
  1095. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1096. /* insert the alternate setting to the list */
  1097. rt_list_insert_after(&intf->setting_list, &setting->list);
  1098. return RT_EOK;
  1099. }
  1100. /**
  1101. * This function will add an endpoint to an alternate setting.
  1102. *
  1103. * @param setting the alternate setting object.
  1104. * @param ep the endpoint object.
  1105. *
  1106. * @return RT_EOK.
  1107. */
  1108. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1109. {
  1110. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1111. /* parameter check */
  1112. RT_ASSERT(setting != RT_NULL);
  1113. RT_ASSERT(ep != RT_NULL);
  1114. /* insert the endpoint to the list */
  1115. rt_list_insert_after(&setting->ep_list, &ep->list);
  1116. return RT_EOK;
  1117. }
  1118. /**
  1119. * This function will set an alternate setting for an interface.
  1120. *
  1121. * @param intf_desc the interface descriptor.
  1122. * @param value the alternate setting number.
  1123. *
  1124. * @return RT_EOK.
  1125. */
  1126. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1127. {
  1128. ualtsetting_t setting;
  1129. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1130. /* parameter check */
  1131. RT_ASSERT(intf != RT_NULL);
  1132. /* find an alternate setting */
  1133. setting = rt_usbd_find_altsetting(intf, value);
  1134. /* set as current alternate setting */
  1135. intf->curr_setting = setting;
  1136. return RT_EOK;
  1137. }
  1138. /**
  1139. * This function will set a configuration for an usb device.
  1140. *
  1141. * @param device the usb device object.
  1142. * @param value the configuration number.
  1143. *
  1144. * @return RT_EOK.
  1145. */
  1146. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1147. {
  1148. uconfig_t cfg;
  1149. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1150. /* parameter check */
  1151. RT_ASSERT(device != RT_NULL);
  1152. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1153. /* find a configuration */
  1154. cfg = rt_usbd_find_config(device, value);
  1155. /* set as current configuration */
  1156. device->curr_cfg = cfg;
  1157. return RT_TRUE;
  1158. }
  1159. static struct rt_messagequeue usb_mq;
  1160. /**
  1161. * This function is the main entry of usb device thread, it is in charge of
  1162. * processing all messages received from the usb message buffer.
  1163. *
  1164. * @param parameter the parameter of the usb device thread.
  1165. *
  1166. * @return none.
  1167. */
  1168. static void rt_usbd_thread_entry(void* parameter)
  1169. {
  1170. while(1)
  1171. {
  1172. struct udev_msg msg;
  1173. udevice_t device;
  1174. uclass_t cls;
  1175. uep_t ep;
  1176. /* receive message */
  1177. if(rt_mq_recv(&usb_mq,
  1178. &msg, sizeof(struct udev_msg),
  1179. RT_WAITING_FOREVER) != RT_EOK )
  1180. continue;
  1181. device = rt_usbd_find_device(msg.dcd);
  1182. if(device == RT_NULL)
  1183. {
  1184. rt_kprintf("invalid usb device\n");
  1185. continue;
  1186. }
  1187. switch (msg.type)
  1188. {
  1189. case USB_MSG_SOF:
  1190. _sof_notify(device);
  1191. break;
  1192. case USB_MSG_DATA_NOTIFY:
  1193. ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
  1194. if(ep != RT_NULL)
  1195. ep->handler(device, cls, msg.content.ep_msg.size);
  1196. else
  1197. rt_kprintf("invalid endpoint\n");
  1198. break;
  1199. case USB_MSG_SETUP_NOTIFY:
  1200. _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
  1201. break;
  1202. case USB_MSG_RESET:
  1203. if (device->state == USB_STATE_ADDRESS)
  1204. _reset_notify(device);
  1205. break;
  1206. default:
  1207. rt_kprintf("unknown msg type\n");
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. /**
  1213. * This function will post an message to usb message queue,
  1214. *
  1215. * @param msg the message to be posted
  1216. * @param size the size of the message .
  1217. *
  1218. * @return the error code, RT_EOK on successfully.
  1219. */
  1220. rt_err_t rt_usbd_post_event(struct udev_msg* msg, rt_size_t size)
  1221. {
  1222. RT_ASSERT(msg != RT_NULL);
  1223. /* send message to usb message queue */
  1224. return rt_mq_send(&usb_mq, (void*)msg, size);
  1225. }
  1226. ALIGN(RT_ALIGN_SIZE)
  1227. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1228. static struct rt_thread usb_thread;
  1229. #define USBD_MQ_MSG_SZ 32
  1230. #define USBD_MQ_MAX_MSG 16
  1231. /* internal of the message queue: every message is associated with a pointer,
  1232. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1233. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1234. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1235. /**
  1236. * This function will initialize usb device thread.
  1237. *
  1238. * @return none.
  1239. *
  1240. */
  1241. rt_err_t rt_usbd_core_init(void)
  1242. {
  1243. rt_list_init(&device_list);
  1244. /* create an usb message queue */
  1245. rt_mq_init(&usb_mq, "usbd", usb_mq_pool, USBD_MQ_MSG_SZ,
  1246. sizeof(usb_mq_pool), RT_IPC_FLAG_FIFO);
  1247. /* init usb device thread */
  1248. rt_thread_init(&usb_thread, "usbd", rt_usbd_thread_entry, RT_NULL,
  1249. usb_thread_stack, RT_USBD_THREAD_STACK_SZ, RT_USBD_THREAD_PRIO, 20);
  1250. /* rt_thread_init should always be OK, so start the thread without further
  1251. * checking. */
  1252. return rt_thread_startup(&usb_thread);
  1253. }