core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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(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(desc_size > 0);
  627. /* allocate memory for the object */
  628. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  629. if(setting == RT_NULL)
  630. {
  631. rt_kprintf("alloc memery failed\n");
  632. return RT_NULL;
  633. }
  634. /* allocate memory for the desc */
  635. setting->desc = rt_malloc(desc_size);
  636. if (setting->desc == RT_NULL)
  637. {
  638. rt_kprintf("alloc desc memery failed\n");
  639. rt_free(setting);
  640. return RT_NULL;
  641. }
  642. setting->desc_size = desc_size;
  643. setting->intf_desc = RT_NULL;
  644. /* to initialize endpoint list */
  645. rt_list_init(&setting->ep_list);
  646. return setting;
  647. }
  648. /**
  649. * This function will config an desc in alternate setting object.
  650. *
  651. * @param setting the altsetting to be config.
  652. * @param desc use it to init desc in setting.
  653. * @param intf_pos the offset of interface descriptor in desc.
  654. *
  655. * @return RT_EOK.
  656. */
  657. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  658. {
  659. RT_ASSERT(setting != RT_NULL);
  660. RT_ASSERT(setting->desc !=RT_NULL);
  661. rt_memcpy(setting->desc, desc, setting->desc_size);
  662. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  663. return RT_EOK;
  664. }
  665. /**
  666. * This function will create an usb class object.
  667. *
  668. * @param device the usb device object.
  669. * @param dev_desc the device descriptor.
  670. * @param ops the operation set.
  671. *
  672. * @return an usb class object on success, RT_NULL on fail.
  673. */
  674. uclass_t rt_usbd_class_create(udevice_t device, udev_desc_t dev_desc,
  675. uclass_ops_t ops)
  676. {
  677. uclass_t cls;
  678. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));
  679. /* parameter check */
  680. RT_ASSERT(device != RT_NULL);
  681. RT_ASSERT(dev_desc != RT_NULL);
  682. /* allocate memory for the object */
  683. cls = (uclass_t)rt_malloc(sizeof(struct uclass));
  684. if(cls == RT_NULL)
  685. {
  686. rt_kprintf("alloc memery failed\n");
  687. return RT_NULL;
  688. }
  689. cls->dev_desc = dev_desc;
  690. cls->ops = ops;
  691. cls->device = device;
  692. /* to initialize interface list */
  693. rt_list_init(&cls->intf_list);
  694. return cls;
  695. }
  696. /**
  697. * This function will create an usb endpoint object.
  698. *
  699. * @param ep_desc the endpoint descriptor.
  700. * @handler the callback handler of object
  701. *
  702. * @return an usb endpoint object on success, RT_NULL on fail.
  703. */
  704. uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
  705. {
  706. uep_t ep;
  707. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));
  708. /* parameter check */
  709. RT_ASSERT(ep_desc != RT_NULL);
  710. /* allocate memory for the object */
  711. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  712. if(ep == RT_NULL)
  713. {
  714. rt_kprintf("alloc memery failed\n");
  715. return RT_NULL;
  716. }
  717. ep->ep_desc = ep_desc;
  718. ep->handler = handler;
  719. ep->buffer = RT_NULL;
  720. return ep;
  721. }
  722. /**
  723. * This function will find an usb device object.
  724. *
  725. * @dcd usd device controller driver.
  726. *
  727. * @return an usb device object on found or RT_NULL on not found.
  728. */
  729. udevice_t rt_usbd_find_device(udcd_t dcd)
  730. {
  731. struct rt_list_node* node;
  732. udevice_t device;
  733. /* parameter check */
  734. RT_ASSERT(dcd != RT_NULL);
  735. /* search a device in the the device list */
  736. for (node = device_list.next; node != &device_list; node = node->next)
  737. {
  738. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  739. if(device->dcd == dcd) return device;
  740. }
  741. rt_kprintf("can't find device\n");
  742. return RT_NULL;
  743. }
  744. /**
  745. * This function will find an usb configuration object.
  746. *
  747. * @param device the usb device object.
  748. * @param value the configuration number.
  749. *
  750. * @return an usb configuration object on found or RT_NULL on not found.
  751. */
  752. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  753. {
  754. struct rt_list_node* node;
  755. uconfig_t cfg = RT_NULL;
  756. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  757. /* parameter check */
  758. RT_ASSERT(device != RT_NULL);
  759. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  760. /* search a configration in the the device */
  761. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  762. {
  763. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  764. if(cfg->cfg_desc.bConfigurationValue == value) return cfg;
  765. }
  766. rt_kprintf("can't find configuration %d\n", value);
  767. return RT_NULL;
  768. }
  769. /**
  770. * This function will find an usb interface object.
  771. *
  772. * @param device the usb device object.
  773. * @param value the interface number.
  774. *
  775. * @return an usb configuration object on found or RT_NULL on not found.
  776. */
  777. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value)
  778. {
  779. struct rt_list_node *i, *j;
  780. uclass_t cls;
  781. uintf_t intf;
  782. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  783. /* parameter check */
  784. RT_ASSERT(device != RT_NULL);
  785. RT_ASSERT(value < device->nr_intf);
  786. /* search an interface in the current configuration */
  787. for (i=device->curr_cfg->cls_list.next;
  788. i!=&device->curr_cfg->cls_list; i=i->next)
  789. {
  790. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  791. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  792. {
  793. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  794. if(intf->intf_num == value)
  795. return intf;
  796. }
  797. }
  798. rt_kprintf("can't find interface %d\n", value);
  799. return RT_NULL;
  800. }
  801. /**
  802. * This function will find an usb interface alternate setting object.
  803. *
  804. * @param device the usb device object.
  805. * @param value the alternate setting number.
  806. *
  807. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  808. */
  809. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  810. {
  811. struct rt_list_node *i;
  812. ualtsetting_t setting;
  813. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  814. /* parameter check */
  815. RT_ASSERT(intf != RT_NULL);
  816. if(intf->curr_setting != RT_NULL)
  817. {
  818. /* if the value equal to the current alternate setting, then do not search */
  819. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  820. return intf->curr_setting;
  821. }
  822. /* search a setting in the alternate setting list */
  823. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  824. {
  825. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  826. if(setting->intf_desc->bAlternateSetting == value)
  827. return setting;
  828. }
  829. rt_kprintf("can't find alternate setting %d\n", value);
  830. return RT_NULL;
  831. }
  832. /**
  833. * This function will find an usb endpoint object.
  834. *
  835. * @param device the usb device object.
  836. * @param ep_addr endpoint address.
  837. *
  838. * @return an usb endpoint object on found or RT_NULL on not found.
  839. */
  840. uep_t rt_usbd_find_endpoint(udevice_t device, uclass_t* pcls, rt_uint8_t ep_addr)
  841. {
  842. uep_t ep;
  843. struct rt_list_node *i, *j, *k;
  844. uclass_t cls;
  845. uintf_t intf;
  846. /* parameter check */
  847. RT_ASSERT(device != RT_NULL);
  848. /* search a endpoint in the current configuration */
  849. for (i=device->curr_cfg->cls_list.next;
  850. i!=&device->curr_cfg->cls_list; i=i->next)
  851. {
  852. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  853. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  854. {
  855. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  856. for(k=intf->curr_setting->ep_list.next;
  857. k!=&intf->curr_setting->ep_list; k=k->next)
  858. {
  859. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  860. if(ep->ep_desc->bEndpointAddress == ep_addr)
  861. {
  862. if (pcls != RT_NULL)
  863. *pcls = cls;
  864. return ep;
  865. }
  866. }
  867. }
  868. }
  869. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  870. return RT_NULL;
  871. }
  872. /**
  873. * This function will add a configuration to an usb device.
  874. *
  875. * @param device the usb device object.
  876. * @param cfg the configuration object.
  877. *
  878. * @return RT_EOK.
  879. */
  880. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  881. {
  882. struct rt_list_node *i, *j, *k;
  883. uclass_t cls;
  884. uintf_t intf;
  885. uep_t ep;
  886. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  887. /* parameter check */
  888. RT_ASSERT(device != RT_NULL);
  889. RT_ASSERT(cfg != RT_NULL);
  890. /* set configuration number to the configuration descriptor */
  891. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  892. device->dev_desc.bNumConfigurations++;
  893. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  894. {
  895. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  896. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  897. {
  898. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  899. cfg->cfg_desc.bNumInterfaces++;
  900. /* allocate address for every endpoint in the interface alternate setting */
  901. for(k=intf->curr_setting->ep_list.next;
  902. k!=&intf->curr_setting->ep_list; k=k->next)
  903. {
  904. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  905. dcd_ep_alloc(device->dcd, ep);
  906. }
  907. /* construct complete configuration descriptor */
  908. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength -
  909. USB_DESC_LENGTH_CONFIG], (void*)intf->curr_setting->desc,
  910. intf->curr_setting->desc_size);
  911. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  912. }
  913. }
  914. /* insert the configuration to the list */
  915. rt_list_insert_after(&device->cfg_list, &cfg->list);
  916. return RT_EOK;
  917. }
  918. /**
  919. * This function will add a class to a configuration.
  920. *
  921. * @param cfg the configuration object.
  922. * @param cls the class object.
  923. *
  924. * @return RT_EOK.
  925. */
  926. rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
  927. {
  928. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));
  929. /* parameter check */
  930. RT_ASSERT(cfg != RT_NULL);
  931. RT_ASSERT(cls != RT_NULL);
  932. /* insert the class to the list */
  933. rt_list_insert_after(&cfg->cls_list, &cls->list);
  934. return RT_EOK;
  935. }
  936. /**
  937. * This function will add an interface to a class.
  938. *
  939. * @param cls the class object.
  940. * @param intf the interface object.
  941. *
  942. * @return RT_EOK.
  943. */
  944. rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
  945. {
  946. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));
  947. /* parameter check */
  948. RT_ASSERT(cls != RT_NULL);
  949. RT_ASSERT(intf != RT_NULL);
  950. /* insert the interface to the list */
  951. rt_list_insert_after(&cls->intf_list, &intf->list);
  952. return RT_EOK;
  953. }
  954. /**
  955. * This function will add an alternate setting to an interface.
  956. *
  957. * @param intf the interface object.
  958. * @param setting the alternate setting object.
  959. *
  960. * @return RT_EOK.
  961. */
  962. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  963. {
  964. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  965. /* parameter check */
  966. RT_ASSERT(intf != RT_NULL);
  967. RT_ASSERT(setting != RT_NULL);
  968. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  969. /* insert the alternate setting to the list */
  970. rt_list_insert_after(&intf->setting_list, &setting->list);
  971. return RT_EOK;
  972. }
  973. /**
  974. * This function will add an endpoint to an alternate setting.
  975. *
  976. * @param setting the alternate setting object.
  977. * @param ep the endpoint object.
  978. *
  979. * @return RT_EOK.
  980. */
  981. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  982. {
  983. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  984. /* parameter check */
  985. RT_ASSERT(setting != RT_NULL);
  986. RT_ASSERT(ep != RT_NULL);
  987. /* insert the endpoint to the list */
  988. rt_list_insert_after(&setting->ep_list, &ep->list);
  989. return RT_EOK;
  990. }
  991. /**
  992. * This function will set an alternate setting for an interface.
  993. *
  994. * @param intf_desc the interface descriptor.
  995. * @param value the alternate setting number.
  996. *
  997. * @return RT_EOK.
  998. */
  999. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1000. {
  1001. ualtsetting_t setting;
  1002. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1003. /* parameter check */
  1004. RT_ASSERT(intf != RT_NULL);
  1005. RT_ASSERT(setting != RT_NULL);
  1006. /* find an alternate setting */
  1007. setting = rt_usbd_find_altsetting(intf, value);
  1008. /* set as current alternate setting */
  1009. intf->curr_setting = setting;
  1010. return RT_EOK;
  1011. }
  1012. /**
  1013. * This function will set a configuration for an usb device.
  1014. *
  1015. * @param device the usb device object.
  1016. * @param value the configuration number.
  1017. *
  1018. * @return RT_EOK.
  1019. */
  1020. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1021. {
  1022. uconfig_t cfg;
  1023. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1024. /* parameter check */
  1025. RT_ASSERT(device != RT_NULL);
  1026. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1027. /* find a configuration */
  1028. cfg = rt_usbd_find_config(device, value);
  1029. /* set as current configuration */
  1030. device->curr_cfg = cfg;
  1031. return RT_TRUE;
  1032. }
  1033. static struct rt_messagequeue *usb_mq;
  1034. /**
  1035. * This function is the main entry of usb device thread, it is in charge of
  1036. * processing all messages received from the usb message buffer.
  1037. *
  1038. * @param parameter the parameter of the usb device thread.
  1039. *
  1040. * @return none.
  1041. */
  1042. static void rt_usbd_thread_entry(void* parameter)
  1043. {
  1044. while(1)
  1045. {
  1046. struct udev_msg msg;
  1047. udevice_t device;
  1048. uclass_t cls;
  1049. uep_t ep;
  1050. /* receive message */
  1051. if(rt_mq_recv(usb_mq, &msg, sizeof(struct udev_msg), RT_WAITING_FOREVER)
  1052. != RT_EOK ) continue;
  1053. switch (msg.type)
  1054. {
  1055. case USB_MSG_SETUP_NOTIFY:
  1056. device = rt_usbd_find_device(msg.dcd);
  1057. if(device != RT_NULL)
  1058. _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
  1059. else
  1060. rt_kprintf("invalid usb device\n");
  1061. break;
  1062. case USB_MSG_DATA_NOTIFY:
  1063. ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
  1064. if(ep != RT_NULL)
  1065. ep->handler(device, cls, msg.content.ep_msg.size);
  1066. else
  1067. rt_kprintf("invalid endpoint\n");
  1068. break;
  1069. case USB_MSG_SOF:
  1070. device = rt_usbd_find_device(msg.dcd);
  1071. if(device != RT_NULL)
  1072. _sof_notify(device);
  1073. else
  1074. rt_kprintf("invalid usb device\n");
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. }
  1080. }
  1081. /**
  1082. * This function will post an message to usb message queue,
  1083. *
  1084. * @param msg the message to be posted
  1085. * @param size the size of the message .
  1086. *
  1087. * @return the error code, RT_EOK on successfully.
  1088. */
  1089. rt_err_t rt_usbd_post_event(struct udev_msg* msg, rt_size_t size)
  1090. {
  1091. RT_ASSERT(msg != RT_NULL);
  1092. /* send message to usb message queue */
  1093. return rt_mq_send(usb_mq, (void*)msg, size);
  1094. }
  1095. /**
  1096. * This function will initialize usb device thread.
  1097. *
  1098. * @return none.
  1099. *
  1100. */
  1101. rt_err_t rt_usbd_core_init(void)
  1102. {
  1103. rt_thread_t thread;
  1104. rt_list_init(&device_list);
  1105. /* create an usb message queue */
  1106. usb_mq = rt_mq_create("usbd", 32, 16, RT_IPC_FLAG_FIFO);
  1107. /* create usb device thread */
  1108. thread = rt_thread_create("usbd", rt_usbd_thread_entry, RT_NULL,
  1109. 2048, 8, 20);
  1110. if(thread != RT_NULL)
  1111. {
  1112. /* startup usb device thread */
  1113. rt_thread_startup(thread);
  1114. }
  1115. return RT_EOK;
  1116. }