core.c 35 KB

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