core.c 35 KB

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