core.c 34 KB

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