core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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 > str_desc.bLength)
  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. if (device->state != USB_STATE_CONFIGURED)
  173. {
  174. dcd_ep_stall(device->dcd, 0);
  175. return -RT_ERROR;
  176. }
  177. /* find the specified interface and its alternate setting */
  178. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  179. value = intf->curr_setting->intf_desc->bAlternateSetting;
  180. /* send the interface alternate setting to endpoint 0*/
  181. dcd_ep_write(device->dcd, 0, &value, 1);
  182. return RT_EOK;
  183. }
  184. /**
  185. * This function will handle set_interface request.
  186. *
  187. * @param device the usb device object.
  188. * @param setup the setup request.
  189. *
  190. * @return RT_EOK on successful.
  191. */
  192. static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
  193. {
  194. uintf_t intf;
  195. uep_t ep;
  196. struct rt_list_node* i;
  197. ualtsetting_t setting;
  198. /* parameter check */
  199. RT_ASSERT(device != RT_NULL);
  200. RT_ASSERT(setup != RT_NULL);
  201. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));
  202. if (device->state != USB_STATE_CONFIGURED)
  203. {
  204. dcd_ep_stall(device->dcd, 0);
  205. return -RT_ERROR;
  206. }
  207. /* find the specified interface */
  208. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  209. /* set alternate setting to the interface */
  210. rt_usbd_set_altsetting(intf, setup->value & 0xFF);
  211. setting = intf->curr_setting;
  212. /* start all endpoints of the interface alternate setting */
  213. for (i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
  214. {
  215. ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
  216. dcd_ep_stop(device->dcd, ep);
  217. dcd_ep_run(device->dcd, ep);
  218. }
  219. dcd_send_status(device->dcd);
  220. return RT_EOK;
  221. }
  222. /**
  223. * This function will handle get_config request.
  224. *
  225. * @param device the usb device object.
  226. * @param setup the setup request.
  227. *
  228. * @return RT_EOK on successful.
  229. */
  230. static rt_err_t _get_config(struct udevice* device, ureq_t setup)
  231. {
  232. rt_uint8_t value;
  233. /* parameter check */
  234. RT_ASSERT(device != RT_NULL);
  235. RT_ASSERT(setup != RT_NULL);
  236. RT_ASSERT(device->curr_cfg != RT_NULL);
  237. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
  238. if (device->state == USB_STATE_CONFIGURED)
  239. {
  240. /* get current configuration */
  241. value = device->curr_cfg->cfg_desc.bConfigurationValue;
  242. }
  243. else
  244. {
  245. value = 0;
  246. }
  247. /* write the current configuration to endpoint 0 */
  248. dcd_ep_write(device->dcd, 0, &value, 1);
  249. return RT_EOK;
  250. }
  251. /**
  252. * This function will handle set_config request.
  253. *
  254. * @param device the usb device object.
  255. * @param setup the setup request.
  256. *
  257. * @return RT_EOK on successful.
  258. */
  259. static rt_err_t _set_config(struct udevice* device, ureq_t setup)
  260. {
  261. struct rt_list_node *i, *j, *k;
  262. uconfig_t cfg;
  263. uintf_t intf;
  264. ualtsetting_t setting;
  265. uep_t ep;
  266. /* parameter check */
  267. RT_ASSERT(device != RT_NULL);
  268. RT_ASSERT(setup != RT_NULL);
  269. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
  270. if (setup->value > device->dev_desc.bNumConfigurations)
  271. {
  272. dcd_ep_stall(device->dcd, 0);
  273. return -RT_ERROR;
  274. }
  275. if (setup->value == 0)
  276. {
  277. RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
  278. device->state = USB_STATE_ADDRESS;
  279. goto _exit;
  280. }
  281. /* set current configuration */
  282. rt_usbd_set_config(device, setup->value);
  283. cfg = device->curr_cfg;
  284. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  285. {
  286. /* run all classes and their endpoints in the configuration */
  287. uclass_t cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  288. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  289. {
  290. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  291. setting = intf->curr_setting;
  292. for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
  293. {
  294. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  295. /* first stop then start endpoint */
  296. dcd_ep_stop(device->dcd, ep);
  297. dcd_ep_run(device->dcd, ep);
  298. }
  299. }
  300. /* after running all endpoints, then run class */
  301. if(cls->ops->run != RT_NULL)
  302. cls->ops->run(device, cls);
  303. }
  304. device->state = USB_STATE_CONFIGURED;
  305. _exit:
  306. /* issue status stage */
  307. dcd_send_status(device->dcd);
  308. return RT_EOK;
  309. }
  310. /**
  311. * This function will handle set_address request.
  312. *
  313. * @param device the usb device object.
  314. * @param setup the setup request.
  315. *
  316. * @return RT_EOK on successful.
  317. */
  318. static rt_err_t _set_address(struct udevice* device, ureq_t setup)
  319. {
  320. /* parameter check */
  321. RT_ASSERT(device != RT_NULL);
  322. RT_ASSERT(setup != RT_NULL);
  323. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
  324. /* set address in device control driver */
  325. dcd_set_address(device->dcd, setup->value);
  326. device->state = USB_STATE_ADDRESS;
  327. /* issue status stage */
  328. dcd_send_status(device->dcd);
  329. return RT_EOK;
  330. }
  331. /**
  332. * This function will handle standard request to
  333. * interface that defined in class-specifics
  334. *
  335. * @param device the usb device object.
  336. * @param setup the setup request.
  337. *
  338. * @return RT_EOK on successful.
  339. */
  340. static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
  341. {
  342. uintf_t intf;
  343. uclass_t cls;
  344. rt_err_t ret;
  345. /* parameter check */
  346. RT_ASSERT(device != RT_NULL);
  347. RT_ASSERT(setup != RT_NULL);
  348. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
  349. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  350. if (intf != RT_NULL)
  351. {
  352. ret = intf->handler(device, cls, setup);
  353. }
  354. else
  355. {
  356. ret = -RT_ERROR;
  357. }
  358. return ret;
  359. }
  360. /**
  361. * This function will handle standard request.
  362. *
  363. * @param device the usb device object.
  364. * @param setup the setup request.
  365. *
  366. * @return RT_EOK on successful.
  367. */
  368. static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
  369. {
  370. udcd_t dcd;
  371. rt_uint16_t value = 0;
  372. /* parameter check */
  373. RT_ASSERT(device != RT_NULL);
  374. RT_ASSERT(setup != RT_NULL);
  375. dcd = device->dcd;
  376. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  377. {
  378. case USB_REQ_TYPE_DEVICE:
  379. switch(setup->request)
  380. {
  381. case USB_REQ_GET_STATUS:
  382. dcd_ep_write(device->dcd, 0, &value, 2);
  383. break;
  384. case USB_REQ_CLEAR_FEATURE:
  385. dcd_clear_feature(dcd, setup->value, setup->index);
  386. dcd_send_status(dcd);
  387. break;
  388. case USB_REQ_SET_FEATURE:
  389. dcd_set_feature(dcd, setup->value, setup->index);
  390. break;
  391. case USB_REQ_SET_ADDRESS:
  392. _set_address(device, setup);
  393. break;
  394. case USB_REQ_GET_DESCRIPTOR:
  395. _get_descriptor(device, setup);
  396. break;
  397. case USB_REQ_SET_DESCRIPTOR:
  398. dcd_ep_stall(dcd, 0);
  399. break;
  400. case USB_REQ_GET_CONFIGURATION:
  401. _get_config(device, setup);
  402. break;
  403. case USB_REQ_SET_CONFIGURATION:
  404. _set_config(device, setup);
  405. break;
  406. default:
  407. rt_kprintf("unknown device request\n");
  408. dcd_ep_stall(device->dcd, 0);
  409. break;
  410. }
  411. break;
  412. case USB_REQ_TYPE_INTERFACE:
  413. switch(setup->request)
  414. {
  415. case USB_REQ_GET_INTERFACE:
  416. _get_interface(device, setup);
  417. break;
  418. case USB_REQ_SET_INTERFACE:
  419. _set_interface(device, setup);
  420. break;
  421. default:
  422. if (_request_interface(device, setup) != RT_EOK)
  423. {
  424. rt_kprintf("unknown interface request\n");
  425. dcd_ep_stall(device->dcd, 0);
  426. return - RT_ERROR;
  427. }
  428. else
  429. break;
  430. }
  431. break;
  432. case USB_REQ_TYPE_ENDPOINT:
  433. switch(setup->request)
  434. {
  435. case USB_REQ_GET_STATUS:
  436. {
  437. /* TODO */
  438. uep_t ep;
  439. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  440. value = ep->is_stall;
  441. dcd_ep_write(dcd, 0, &value, 2);
  442. }
  443. break;
  444. case USB_REQ_CLEAR_FEATURE:
  445. {
  446. uep_t ep;
  447. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  448. ep->is_stall = 0;
  449. dcd_clear_feature(dcd, setup->value, setup->index);
  450. dcd_send_status(dcd);
  451. }
  452. break;
  453. case USB_REQ_SET_FEATURE:
  454. {
  455. uep_t ep;
  456. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  457. ep->is_stall = 1;
  458. dcd_set_feature(dcd, setup->value, setup->index);
  459. dcd_send_status(dcd);
  460. }
  461. break;
  462. case USB_REQ_SYNCH_FRAME:
  463. break;
  464. default:
  465. rt_kprintf("unknown endpoint request\n");
  466. dcd_ep_stall(device->dcd, 0);
  467. break;
  468. }
  469. break;
  470. case USB_REQ_TYPE_OTHER:
  471. rt_kprintf("unknown other type request\n");
  472. dcd_ep_stall(device->dcd, 0);
  473. break;
  474. default:
  475. rt_kprintf("unknown type request\n");
  476. dcd_ep_stall(device->dcd, 0);
  477. break;
  478. }
  479. return RT_EOK;
  480. }
  481. /**
  482. * This function will handle class request.
  483. *
  484. * @param device the usb device object.
  485. * @param setup the setup request.
  486. *
  487. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  488. */
  489. static rt_err_t _class_request(udevice_t device, ureq_t setup)
  490. {
  491. uintf_t intf;
  492. uclass_t cls;
  493. /* parameter check */
  494. RT_ASSERT(device != RT_NULL);
  495. RT_ASSERT(setup != RT_NULL);
  496. /* verify request value */
  497. if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
  498. {
  499. dcd_ep_stall(device->dcd, 0);
  500. return -RT_ERROR;
  501. }
  502. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  503. {
  504. case USB_REQ_TYPE_INTERFACE:
  505. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  506. intf->handler(device, cls, setup);
  507. break;
  508. case USB_REQ_TYPE_ENDPOINT:
  509. break;
  510. default:
  511. rt_kprintf("unknown class request type\n");
  512. dcd_ep_stall(device->dcd, 0);
  513. break;
  514. }
  515. return RT_EOK;
  516. }
  517. /**
  518. * This function will handle setup request.
  519. *
  520. * @param device the usb device object.
  521. * @param setup the setup request.
  522. *
  523. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  524. */
  525. static rt_err_t _setup_request(udevice_t device, ureq_t setup)
  526. {
  527. /* parameter check */
  528. RT_ASSERT(device != RT_NULL);
  529. RT_ASSERT(setup != RT_NULL);
  530. RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
  531. RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request_handler 0x%x\n",
  532. setup->request_type));
  533. RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
  534. RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
  535. RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
  536. RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
  537. RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
  538. switch((setup->request_type & USB_REQ_TYPE_MASK))
  539. {
  540. case USB_REQ_TYPE_STANDARD:
  541. _standard_request(device, setup);
  542. break;
  543. case USB_REQ_TYPE_CLASS:
  544. _class_request(device, setup);
  545. break;
  546. case USB_REQ_TYPE_VENDOR:
  547. rt_kprintf("vendor type request\n");
  548. break;
  549. default:
  550. rt_kprintf("unknown setup request type\n");
  551. dcd_ep_stall(device->dcd, 0);
  552. return -RT_ERROR;
  553. }
  554. return RT_EOK;
  555. }
  556. /**
  557. * This function will notity sof event to all of class.
  558. *
  559. * @param device the usb device object.
  560. *
  561. * @return RT_EOK.
  562. */
  563. rt_err_t _sof_notify(udevice_t device)
  564. {
  565. struct rt_list_node *i;
  566. uclass_t cls;
  567. RT_ASSERT(device != RT_NULL);
  568. /* to notity every class that sof event comes */
  569. for (i=device->curr_cfg->cls_list.next;
  570. i!=&device->curr_cfg->cls_list; i=i->next)
  571. {
  572. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  573. if(cls->ops->sof_handler != RT_NULL)
  574. cls->ops->sof_handler(device, cls);
  575. }
  576. return RT_EOK;
  577. }
  578. /**
  579. * This function will stop all class.
  580. *
  581. * @param device the usb device object.
  582. *
  583. * @return RT_EOK.
  584. */
  585. rt_err_t _stop_notify(udevice_t device)
  586. {
  587. struct rt_list_node *i;
  588. uclass_t cls;
  589. RT_ASSERT(device != RT_NULL);
  590. /* to notity every class that sof event comes */
  591. for (i = device->curr_cfg->cls_list.next;
  592. i != &device->curr_cfg->cls_list;
  593. i = i->next)
  594. {
  595. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  596. if(cls->ops->stop != RT_NULL)
  597. cls->ops->stop(device, cls);
  598. }
  599. return RT_EOK;
  600. }
  601. /**
  602. * This function will run all class.
  603. *
  604. * @param device the usb device object.
  605. *
  606. * @return RT_EOK.
  607. */
  608. rt_err_t _run_notify(udevice_t device)
  609. {
  610. struct rt_list_node *i;
  611. uclass_t cls;
  612. RT_ASSERT(device != RT_NULL);
  613. /* to notity every class that sof event comes */
  614. for (i = device->curr_cfg->cls_list.next;
  615. i != &device->curr_cfg->cls_list;
  616. i = i->next)
  617. {
  618. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  619. if(cls->ops->run != RT_NULL)
  620. cls->ops->run(device, cls);
  621. }
  622. return RT_EOK;
  623. }
  624. /**
  625. * This function will reset all class.
  626. *
  627. * @param device the usb device object.
  628. *
  629. * @return RT_EOK.
  630. */
  631. rt_err_t _reset_notify(udevice_t device)
  632. {
  633. struct rt_list_node *i;
  634. uclass_t cls;
  635. RT_ASSERT(device != RT_NULL);
  636. _stop_notify(device);
  637. _run_notify(device);
  638. return RT_EOK;
  639. }
  640. /**
  641. * This function will create an usb device object.
  642. *
  643. * @param ustring the usb string array to contain string descriptor.
  644. *
  645. * @return an usb device object on success, RT_NULL on fail.
  646. */
  647. udevice_t rt_usbd_device_create(void)
  648. {
  649. udevice_t udevice;
  650. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_create\n"));
  651. /* allocate memory for the object */
  652. udevice = rt_malloc(sizeof(struct udevice));
  653. if(udevice == RT_NULL)
  654. {
  655. rt_kprintf("alloc memery failed\n");
  656. return RT_NULL;
  657. }
  658. rt_memset(udevice, 0, sizeof(struct udevice));
  659. /* to initialize configuration list */
  660. rt_list_init(&udevice->cfg_list);
  661. /* insert the device object to device list */
  662. rt_list_insert_after(&device_list, &udevice->list);
  663. return udevice;
  664. }
  665. /**
  666. * This function will set usb device string description.
  667. *
  668. * @param device the usb device object.
  669. * @param ustring pointer to string pointer array.
  670. *
  671. * @return RT_EOK.
  672. */
  673. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  674. {
  675. /* parameter check */
  676. RT_ASSERT(device != RT_NULL);
  677. RT_ASSERT(ustring != RT_NULL);
  678. /* set string descriptor array to the device object */
  679. device->str = ustring;
  680. return RT_EOK;
  681. }
  682. /**
  683. * This function will set an usb controller driver to a device.
  684. *
  685. * @param device the usb device object.
  686. * @param dcd the usb device controller driver.
  687. *
  688. * @return RT_EOK on successful.
  689. */
  690. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  691. {
  692. /* parameter check */
  693. RT_ASSERT(device != RT_NULL);
  694. RT_ASSERT(dcd != RT_NULL);
  695. /* set usb device controller driver to the device */
  696. device->dcd = dcd;
  697. return RT_EOK;
  698. }
  699. /**
  700. * This function will set an usb device descriptor to a device.
  701. *
  702. * @param device the usb device object.
  703. * @param dev_desc the usb device descriptor.
  704. *
  705. * @return RT_EOK on successful.
  706. */
  707. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  708. {
  709. /* parameter check */
  710. RT_ASSERT(device != RT_NULL);
  711. RT_ASSERT(dev_desc != RT_NULL);
  712. /* copy the usb device descriptor to the device */
  713. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  714. return RT_EOK;
  715. }
  716. /**
  717. * This function will create an usb configuration object.
  718. *
  719. * @param none.
  720. *
  721. * @return an usb configuration object.
  722. */
  723. uconfig_t rt_usbd_config_create(void)
  724. {
  725. uconfig_t cfg;
  726. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_create\n"));
  727. /* allocate memory for the object */
  728. cfg = rt_malloc(sizeof(struct uconfig));
  729. if(cfg == RT_NULL)
  730. {
  731. rt_kprintf("alloc memery failed\n");
  732. return RT_NULL;
  733. }
  734. rt_memset(cfg, 0, sizeof(struct uconfig));
  735. /* set default value */
  736. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  737. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  738. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  739. cfg->cfg_desc.bmAttributes = 0xC0;
  740. cfg->cfg_desc.MaxPower = 0x32;
  741. /* to initialize class object list */
  742. rt_list_init(&cfg->cls_list);
  743. return cfg;
  744. }
  745. /**
  746. * This function will create an usb interface object.
  747. *
  748. * @param device the usb device object.
  749. * @handler the callback handler of object
  750. *
  751. * @return an usb interface object on success, RT_NULL on fail.
  752. */
  753. uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler)
  754. {
  755. uintf_t intf;
  756. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_create\n"));
  757. /* parameter check */
  758. RT_ASSERT(device != RT_NULL);
  759. /* allocate memory for the object */
  760. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  761. if(intf == RT_NULL)
  762. {
  763. rt_kprintf("alloc memery failed\n");
  764. return RT_NULL;
  765. }
  766. intf->intf_num = device->nr_intf;
  767. device->nr_intf++;
  768. intf->handler = handler;
  769. intf->curr_setting = RT_NULL;
  770. /* to initialize the alternate setting object list */
  771. rt_list_init(&intf->setting_list);
  772. return intf;
  773. }
  774. /**
  775. * This function will create an usb alternate setting object.
  776. *
  777. * @param intf_desc the interface descriptor.
  778. * @desc_size the size of the interface descriptor.
  779. *
  780. * @return an usb alternate setting object on success, RT_NULL on fail.
  781. */
  782. ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size)
  783. {
  784. ualtsetting_t setting;
  785. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_create\n"));
  786. /* parameter check */
  787. RT_ASSERT(desc_size > 0);
  788. /* allocate memory for the object */
  789. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  790. if(setting == RT_NULL)
  791. {
  792. rt_kprintf("alloc memery failed\n");
  793. return RT_NULL;
  794. }
  795. /* allocate memory for the desc */
  796. setting->desc = rt_malloc(desc_size);
  797. if (setting->desc == RT_NULL)
  798. {
  799. rt_kprintf("alloc desc memery failed\n");
  800. rt_free(setting);
  801. return RT_NULL;
  802. }
  803. setting->desc_size = desc_size;
  804. setting->intf_desc = RT_NULL;
  805. /* to initialize endpoint list */
  806. rt_list_init(&setting->ep_list);
  807. return setting;
  808. }
  809. /**
  810. * This function will config an desc in alternate setting object.
  811. *
  812. * @param setting the altsetting to be config.
  813. * @param desc use it to init desc in setting.
  814. * @param intf_pos the offset of interface descriptor in desc.
  815. *
  816. * @return RT_EOK.
  817. */
  818. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  819. {
  820. RT_ASSERT(setting != RT_NULL);
  821. RT_ASSERT(setting->desc !=RT_NULL);
  822. rt_memcpy(setting->desc, desc, setting->desc_size);
  823. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  824. return RT_EOK;
  825. }
  826. /**
  827. * This function will create an usb class object.
  828. *
  829. * @param device the usb device object.
  830. * @param dev_desc the device descriptor.
  831. * @param ops the operation set.
  832. *
  833. * @return an usb class object on success, RT_NULL on fail.
  834. */
  835. uclass_t rt_usbd_class_create(udevice_t device, udev_desc_t dev_desc,
  836. uclass_ops_t ops)
  837. {
  838. uclass_t cls;
  839. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));
  840. /* parameter check */
  841. RT_ASSERT(device != RT_NULL);
  842. RT_ASSERT(dev_desc != RT_NULL);
  843. /* allocate memory for the object */
  844. cls = (uclass_t)rt_malloc(sizeof(struct uclass));
  845. if(cls == RT_NULL)
  846. {
  847. rt_kprintf("alloc memery failed\n");
  848. return RT_NULL;
  849. }
  850. cls->dev_desc = dev_desc;
  851. cls->ops = ops;
  852. cls->device = device;
  853. /* to initialize interface list */
  854. rt_list_init(&cls->intf_list);
  855. return cls;
  856. }
  857. /**
  858. * This function will create an usb endpoint object.
  859. *
  860. * @param ep_desc the endpoint descriptor.
  861. * @handler the callback handler of object
  862. *
  863. * @return an usb endpoint object on success, RT_NULL on fail.
  864. */
  865. uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
  866. {
  867. uep_t ep;
  868. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));
  869. /* parameter check */
  870. RT_ASSERT(ep_desc != RT_NULL);
  871. /* allocate memory for the object */
  872. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  873. if(ep == RT_NULL)
  874. {
  875. rt_kprintf("alloc memery failed\n");
  876. return RT_NULL;
  877. }
  878. ep->ep_desc = ep_desc;
  879. ep->handler = handler;
  880. ep->buffer = RT_NULL;
  881. return ep;
  882. }
  883. /**
  884. * This function will find an usb device object.
  885. *
  886. * @dcd usd device controller driver.
  887. *
  888. * @return an usb device object on found or RT_NULL on not found.
  889. */
  890. udevice_t rt_usbd_find_device(udcd_t dcd)
  891. {
  892. struct rt_list_node* node;
  893. udevice_t device;
  894. /* parameter check */
  895. RT_ASSERT(dcd != RT_NULL);
  896. /* search a device in the the device list */
  897. for (node = device_list.next; node != &device_list; node = node->next)
  898. {
  899. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  900. if(device->dcd == dcd) return device;
  901. }
  902. rt_kprintf("can't find device\n");
  903. return RT_NULL;
  904. }
  905. /**
  906. * This function will find an usb configuration object.
  907. *
  908. * @param device the usb device object.
  909. * @param value the configuration number.
  910. *
  911. * @return an usb configuration object on found or RT_NULL on not found.
  912. */
  913. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  914. {
  915. struct rt_list_node* node;
  916. uconfig_t cfg = RT_NULL;
  917. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  918. /* parameter check */
  919. RT_ASSERT(device != RT_NULL);
  920. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  921. /* search a configration in the the device */
  922. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  923. {
  924. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  925. if(cfg->cfg_desc.bConfigurationValue == value) return cfg;
  926. }
  927. rt_kprintf("can't find configuration %d\n", value);
  928. return RT_NULL;
  929. }
  930. /**
  931. * This function will find an usb interface object.
  932. *
  933. * @param device the usb device object.
  934. * @param value the interface number.
  935. *
  936. * @return an usb configuration object on found or RT_NULL on not found.
  937. */
  938. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, uclass_t *pcls)
  939. {
  940. struct rt_list_node *i, *j;
  941. uclass_t cls;
  942. uintf_t intf;
  943. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  944. /* parameter check */
  945. RT_ASSERT(device != RT_NULL);
  946. RT_ASSERT(value < device->nr_intf);
  947. /* search an interface in the current configuration */
  948. for (i=device->curr_cfg->cls_list.next;
  949. i!=&device->curr_cfg->cls_list; i=i->next)
  950. {
  951. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  952. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  953. {
  954. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  955. if(intf->intf_num == value)
  956. {
  957. if (pcls != RT_NULL)
  958. *pcls = cls;
  959. return intf;
  960. }
  961. }
  962. }
  963. rt_kprintf("can't find interface %d\n", value);
  964. return RT_NULL;
  965. }
  966. /**
  967. * This function will find an usb interface alternate setting object.
  968. *
  969. * @param device the usb device object.
  970. * @param value the alternate setting number.
  971. *
  972. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  973. */
  974. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  975. {
  976. struct rt_list_node *i;
  977. ualtsetting_t setting;
  978. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  979. /* parameter check */
  980. RT_ASSERT(intf != RT_NULL);
  981. if(intf->curr_setting != RT_NULL)
  982. {
  983. /* if the value equal to the current alternate setting, then do not search */
  984. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  985. return intf->curr_setting;
  986. }
  987. /* search a setting in the alternate setting list */
  988. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  989. {
  990. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  991. if(setting->intf_desc->bAlternateSetting == value)
  992. return setting;
  993. }
  994. rt_kprintf("can't find alternate setting %d\n", value);
  995. return RT_NULL;
  996. }
  997. /**
  998. * This function will find an usb endpoint object.
  999. *
  1000. * @param device the usb device object.
  1001. * @param ep_addr endpoint address.
  1002. *
  1003. * @return an usb endpoint object on found or RT_NULL on not found.
  1004. */
  1005. uep_t rt_usbd_find_endpoint(udevice_t device, uclass_t* pcls, rt_uint8_t ep_addr)
  1006. {
  1007. uep_t ep;
  1008. struct rt_list_node *i, *j, *k;
  1009. uclass_t cls;
  1010. uintf_t intf;
  1011. /* parameter check */
  1012. RT_ASSERT(device != RT_NULL);
  1013. /* search a endpoint in the current configuration */
  1014. for (i=device->curr_cfg->cls_list.next;
  1015. i!=&device->curr_cfg->cls_list; i=i->next)
  1016. {
  1017. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  1018. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  1019. {
  1020. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1021. for(k=intf->curr_setting->ep_list.next;
  1022. k!=&intf->curr_setting->ep_list; k=k->next)
  1023. {
  1024. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1025. if(ep->ep_desc->bEndpointAddress == ep_addr)
  1026. {
  1027. if (pcls != RT_NULL)
  1028. *pcls = cls;
  1029. return ep;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  1035. return RT_NULL;
  1036. }
  1037. /**
  1038. * This function will add a configuration to an usb device.
  1039. *
  1040. * @param device the usb device object.
  1041. * @param cfg the configuration object.
  1042. *
  1043. * @return RT_EOK.
  1044. */
  1045. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  1046. {
  1047. struct rt_list_node *i, *j, *k;
  1048. uclass_t cls;
  1049. uintf_t intf;
  1050. uep_t ep;
  1051. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  1052. /* parameter check */
  1053. RT_ASSERT(device != RT_NULL);
  1054. RT_ASSERT(cfg != RT_NULL);
  1055. /* set configuration number to the configuration descriptor */
  1056. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  1057. device->dev_desc.bNumConfigurations++;
  1058. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  1059. {
  1060. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  1061. for(j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  1062. {
  1063. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1064. cfg->cfg_desc.bNumInterfaces++;
  1065. /* allocate address for every endpoint in the interface alternate setting */
  1066. for(k=intf->curr_setting->ep_list.next;
  1067. k!=&intf->curr_setting->ep_list; k=k->next)
  1068. {
  1069. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1070. dcd_ep_alloc(device->dcd, ep);
  1071. }
  1072. /* construct complete configuration descriptor */
  1073. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength -
  1074. USB_DESC_LENGTH_CONFIG], (void*)intf->curr_setting->desc,
  1075. intf->curr_setting->desc_size);
  1076. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  1077. }
  1078. }
  1079. /* insert the configuration to the list */
  1080. rt_list_insert_after(&device->cfg_list, &cfg->list);
  1081. return RT_EOK;
  1082. }
  1083. /**
  1084. * This function will add a class to a configuration.
  1085. *
  1086. * @param cfg the configuration object.
  1087. * @param cls the class object.
  1088. *
  1089. * @return RT_EOK.
  1090. */
  1091. rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
  1092. {
  1093. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));
  1094. /* parameter check */
  1095. RT_ASSERT(cfg != RT_NULL);
  1096. RT_ASSERT(cls != RT_NULL);
  1097. /* insert the class to the list */
  1098. rt_list_insert_after(&cfg->cls_list, &cls->list);
  1099. return RT_EOK;
  1100. }
  1101. /**
  1102. * This function will add an interface to a class.
  1103. *
  1104. * @param cls the class object.
  1105. * @param intf the interface object.
  1106. *
  1107. * @return RT_EOK.
  1108. */
  1109. rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
  1110. {
  1111. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));
  1112. /* parameter check */
  1113. RT_ASSERT(cls != RT_NULL);
  1114. RT_ASSERT(intf != RT_NULL);
  1115. /* insert the interface to the list */
  1116. rt_list_insert_after(&cls->intf_list, &intf->list);
  1117. return RT_EOK;
  1118. }
  1119. /**
  1120. * This function will add an alternate setting to an interface.
  1121. *
  1122. * @param intf the interface object.
  1123. * @param setting the alternate setting object.
  1124. *
  1125. * @return RT_EOK.
  1126. */
  1127. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1128. {
  1129. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1130. /* parameter check */
  1131. RT_ASSERT(intf != RT_NULL);
  1132. RT_ASSERT(setting != RT_NULL);
  1133. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1134. /* insert the alternate setting to the list */
  1135. rt_list_insert_after(&intf->setting_list, &setting->list);
  1136. return RT_EOK;
  1137. }
  1138. /**
  1139. * This function will add an endpoint to an alternate setting.
  1140. *
  1141. * @param setting the alternate setting object.
  1142. * @param ep the endpoint object.
  1143. *
  1144. * @return RT_EOK.
  1145. */
  1146. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1147. {
  1148. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1149. /* parameter check */
  1150. RT_ASSERT(setting != RT_NULL);
  1151. RT_ASSERT(ep != RT_NULL);
  1152. /* insert the endpoint to the list */
  1153. rt_list_insert_after(&setting->ep_list, &ep->list);
  1154. return RT_EOK;
  1155. }
  1156. /**
  1157. * This function will set an alternate setting for an interface.
  1158. *
  1159. * @param intf_desc the interface descriptor.
  1160. * @param value the alternate setting number.
  1161. *
  1162. * @return RT_EOK.
  1163. */
  1164. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1165. {
  1166. ualtsetting_t setting;
  1167. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1168. /* parameter check */
  1169. RT_ASSERT(intf != RT_NULL);
  1170. /* find an alternate setting */
  1171. setting = rt_usbd_find_altsetting(intf, value);
  1172. /* set as current alternate setting */
  1173. intf->curr_setting = setting;
  1174. return RT_EOK;
  1175. }
  1176. /**
  1177. * This function will set a configuration for an usb device.
  1178. *
  1179. * @param device the usb device object.
  1180. * @param value the configuration number.
  1181. *
  1182. * @return RT_EOK.
  1183. */
  1184. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1185. {
  1186. uconfig_t cfg;
  1187. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1188. /* parameter check */
  1189. RT_ASSERT(device != RT_NULL);
  1190. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1191. /* find a configuration */
  1192. cfg = rt_usbd_find_config(device, value);
  1193. /* set as current configuration */
  1194. device->curr_cfg = cfg;
  1195. return RT_TRUE;
  1196. }
  1197. static struct rt_messagequeue usb_mq;
  1198. /**
  1199. * This function is the main entry of usb device thread, it is in charge of
  1200. * processing all messages received from the usb message buffer.
  1201. *
  1202. * @param parameter the parameter of the usb device thread.
  1203. *
  1204. * @return none.
  1205. */
  1206. static void rt_usbd_thread_entry(void* parameter)
  1207. {
  1208. while(1)
  1209. {
  1210. struct udev_msg msg;
  1211. udevice_t device;
  1212. uclass_t cls;
  1213. uep_t ep;
  1214. /* receive message */
  1215. if(rt_mq_recv(&usb_mq,
  1216. &msg, sizeof(struct udev_msg),
  1217. RT_WAITING_FOREVER) != RT_EOK )
  1218. continue;
  1219. device = rt_usbd_find_device(msg.dcd);
  1220. if(device == RT_NULL)
  1221. {
  1222. rt_kprintf("invalid usb device\n");
  1223. continue;
  1224. }
  1225. switch (msg.type)
  1226. {
  1227. case USB_MSG_SOF:
  1228. _sof_notify(device);
  1229. break;
  1230. case USB_MSG_DATA_NOTIFY:
  1231. /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
  1232. * got configured. */
  1233. if (device->state != USB_STATE_CONFIGURED)
  1234. break;
  1235. ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
  1236. if(ep != RT_NULL)
  1237. ep->handler(device, cls, msg.content.ep_msg.size);
  1238. else
  1239. rt_kprintf("invalid endpoint\n");
  1240. break;
  1241. case USB_MSG_SETUP_NOTIFY:
  1242. _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
  1243. break;
  1244. case USB_MSG_RESET:
  1245. if (device->state == USB_STATE_ADDRESS)
  1246. _reset_notify(device);
  1247. break;
  1248. case USB_MSG_PLUG_OUT:
  1249. _stop_notify(device);
  1250. break;
  1251. default:
  1252. rt_kprintf("unknown msg type\n");
  1253. break;
  1254. }
  1255. }
  1256. }
  1257. /**
  1258. * This function will post an message to usb message queue,
  1259. *
  1260. * @param msg the message to be posted
  1261. * @param size the size of the message .
  1262. *
  1263. * @return the error code, RT_EOK on successfully.
  1264. */
  1265. rt_err_t rt_usbd_post_event(struct udev_msg* msg, rt_size_t size)
  1266. {
  1267. RT_ASSERT(msg != RT_NULL);
  1268. /* send message to usb message queue */
  1269. return rt_mq_send(&usb_mq, (void*)msg, size);
  1270. }
  1271. ALIGN(RT_ALIGN_SIZE)
  1272. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1273. static struct rt_thread usb_thread;
  1274. #define USBD_MQ_MSG_SZ 32
  1275. #define USBD_MQ_MAX_MSG 16
  1276. /* internal of the message queue: every message is associated with a pointer,
  1277. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1278. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1279. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1280. /**
  1281. * This function will initialize usb device thread.
  1282. *
  1283. * @return none.
  1284. *
  1285. */
  1286. rt_err_t rt_usbd_core_init(void)
  1287. {
  1288. rt_list_init(&device_list);
  1289. /* create an usb message queue */
  1290. rt_mq_init(&usb_mq, "usbd", usb_mq_pool, USBD_MQ_MSG_SZ,
  1291. sizeof(usb_mq_pool), RT_IPC_FLAG_FIFO);
  1292. /* init usb device thread */
  1293. rt_thread_init(&usb_thread, "usbd", rt_usbd_thread_entry, RT_NULL,
  1294. usb_thread_stack, RT_USBD_THREAD_STACK_SZ, RT_USBD_THREAD_PRIO, 20);
  1295. /* rt_thread_init should always be OK, so start the thread without further
  1296. * checking. */
  1297. return rt_thread_startup(&usb_thread);
  1298. }