1
0

core.c 39 KB

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