core.c 35 KB

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