core.c 34 KB

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