core.c 34 KB

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