core.c 35 KB

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