core.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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 function handler
  24. * 2012-12-30 heyuanjie87 change inferface handler
  25. * 2013-04-26 aozima add DEVICEQUALIFIER support.
  26. * 2013-07-25 Yi Qiu update for USB CV test
  27. */
  28. #include <rtthread.h>
  29. #include <rtdevice.h>
  30. static rt_list_t device_list;
  31. static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
  32. static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
  33. static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep);
  34. static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep);
  35. /**
  36. * This function will handle get_device_descriptor request.
  37. *
  38. * @param device the usb device object.
  39. * @param setup the setup request.
  40. *
  41. * @return RT_EOK on successful.
  42. */
  43. static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
  44. {
  45. rt_size_t size;
  46. /* parameter check */
  47. RT_ASSERT(device != RT_NULL);
  48. RT_ASSERT(setup != RT_NULL);
  49. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
  50. /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
  51. size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
  52. USB_DESC_LENGTH_DEVICE : setup->length;
  53. /* send device descriptor to endpoint 0 */
  54. rt_usbd_ep0_write(device, (rt_uint8_t*)&device->dev_desc,
  55. size);
  56. return RT_EOK;
  57. }
  58. /**
  59. * This function will handle get_config_descriptor request.
  60. *
  61. * @param device the usb device object.
  62. * @param setup the setup request.
  63. *
  64. * @return RT_EOK on successful.
  65. */
  66. static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
  67. {
  68. rt_size_t size;
  69. ucfg_desc_t cfg_desc;
  70. /* parameter check */
  71. RT_ASSERT(device != RT_NULL);
  72. RT_ASSERT(setup != RT_NULL);
  73. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
  74. cfg_desc = &device->curr_cfg->cfg_desc;
  75. size = (setup->length > cfg_desc->wTotalLength) ?
  76. cfg_desc->wTotalLength : setup->length;
  77. /* send configuration descriptor to endpoint 0 */
  78. rt_usbd_ep0_write(device, (rt_uint8_t*)cfg_desc, size);
  79. return RT_EOK;
  80. }
  81. /**
  82. * This function will handle get_string_descriptor request.
  83. *
  84. * @param device the usb device object.
  85. * @param setup the setup request.
  86. *
  87. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  88. */
  89. static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
  90. {
  91. struct ustring_descriptor str_desc;
  92. rt_uint8_t index, i;
  93. rt_uint32_t len;
  94. /* parameter check */
  95. RT_ASSERT(device != RT_NULL);
  96. RT_ASSERT(setup != RT_NULL);
  97. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
  98. str_desc.type = USB_DESC_TYPE_STRING;
  99. index = setup->value & 0xFF;
  100. if(index > USB_STRING_INTERFACE_INDEX)
  101. {
  102. rt_kprintf("unknown string index\n");
  103. rt_usbd_ep0_set_stall(device);
  104. return -RT_ERROR;
  105. }
  106. if(index == 0)
  107. {
  108. str_desc.bLength = 4;
  109. str_desc.String[0] = 0x09;
  110. str_desc.String[1] = 0x04;
  111. }
  112. else
  113. {
  114. len = rt_strlen(device->str[index]);
  115. str_desc.bLength = len*2 + 2;
  116. for(i=0; i<len; i++)
  117. {
  118. str_desc.String[i*2] = device->str[index][i];
  119. str_desc.String[i*2 + 1] = 0;
  120. }
  121. }
  122. if (setup->length > str_desc.bLength)
  123. len = str_desc.bLength;
  124. else
  125. len = setup->length;
  126. /* send string descriptor to endpoint 0 */
  127. rt_usbd_ep0_write(device, (rt_uint8_t*)&str_desc, len);
  128. return RT_EOK;
  129. }
  130. static rt_err_t _get_qualifier_descriptor(struct udevice* device, ureq_t setup)
  131. {
  132. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_qualifier_descriptor\n"));
  133. /* parameter check */
  134. RT_ASSERT(device != RT_NULL);
  135. RT_ASSERT(setup != RT_NULL);
  136. if(device->dev_qualifier)
  137. {
  138. /* send device qualifier descriptor to endpoint 0 */
  139. rt_usbd_ep0_write(device, (rt_uint8_t*)device->dev_qualifier,
  140. sizeof(struct usb_qualifier_descriptor));
  141. }
  142. else
  143. {
  144. rt_usbd_ep0_set_stall(device);
  145. }
  146. return RT_EOK;
  147. }
  148. /**
  149. * This function will handle get_descriptor request.
  150. *
  151. * @param device the usb device object.
  152. * @param setup the setup request.
  153. *
  154. * @return RT_EOK on successful.
  155. */
  156. static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
  157. {
  158. /* parameter check */
  159. RT_ASSERT(device != RT_NULL);
  160. RT_ASSERT(setup != RT_NULL);
  161. if(setup->request_type == USB_REQ_TYPE_DIR_IN)
  162. {
  163. switch(setup->value >> 8)
  164. {
  165. case USB_DESC_TYPE_DEVICE:
  166. _get_device_descriptor(device, setup);
  167. break;
  168. case USB_DESC_TYPE_CONFIGURATION:
  169. _get_config_descriptor(device, setup);
  170. break;
  171. case USB_DESC_TYPE_STRING:
  172. _get_string_descriptor(device, setup);
  173. break;
  174. case USB_DESC_TYPE_DEVICEQUALIFIER:
  175. _get_qualifier_descriptor(device, setup);
  176. break;
  177. default:
  178. rt_kprintf("unsupported descriptor request\n");
  179. rt_usbd_ep0_set_stall(device);
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. rt_kprintf("request direction error\n");
  186. rt_usbd_ep0_set_stall(device);
  187. }
  188. return RT_EOK;
  189. }
  190. /**
  191. * This function will handle get_interface request.
  192. *
  193. * @param device the usb device object.
  194. * @param setup the setup request.
  195. *
  196. * @return RT_EOK on successful.
  197. */
  198. static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
  199. {
  200. rt_uint8_t value;
  201. uintf_t intf;
  202. /* parameter check */
  203. RT_ASSERT(device != RT_NULL);
  204. RT_ASSERT(setup != RT_NULL);
  205. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_interface\n"));
  206. if (device->state != USB_STATE_CONFIGURED)
  207. {
  208. rt_usbd_ep0_set_stall(device);
  209. return -RT_ERROR;
  210. }
  211. /* find the specified interface and its alternate setting */
  212. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  213. value = intf->curr_setting->intf_desc->bAlternateSetting;
  214. /* send the interface alternate setting to endpoint 0*/
  215. rt_usbd_ep0_write(device, &value, 1);
  216. return RT_EOK;
  217. }
  218. /**
  219. * This function will handle set_interface request.
  220. *
  221. * @param device the usb device object.
  222. * @param setup the setup request.
  223. *
  224. * @return RT_EOK on successful.
  225. */
  226. static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
  227. {
  228. uintf_t intf;
  229. uep_t ep;
  230. struct rt_list_node* i;
  231. ualtsetting_t setting;
  232. /* parameter check */
  233. RT_ASSERT(device != RT_NULL);
  234. RT_ASSERT(setup != RT_NULL);
  235. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));
  236. if (device->state != USB_STATE_CONFIGURED)
  237. {
  238. rt_usbd_ep0_set_stall(device);
  239. return -RT_ERROR;
  240. }
  241. /* find the specified interface */
  242. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  243. /* set alternate setting to the interface */
  244. rt_usbd_set_altsetting(intf, setup->value & 0xFF);
  245. setting = intf->curr_setting;
  246. /* start all endpoints of the interface alternate setting */
  247. for(i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
  248. {
  249. ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
  250. dcd_ep_disable(device->dcd, ep);
  251. dcd_ep_enable(device->dcd, ep);
  252. }
  253. dcd_ep0_send_status(device->dcd);
  254. return RT_EOK;
  255. }
  256. /**
  257. * This function will handle get_config request.
  258. *
  259. * @param device the usb device object.
  260. * @param setup the setup request.
  261. *
  262. * @return RT_EOK on successful.
  263. */
  264. static rt_err_t _get_config(struct udevice* device, ureq_t setup)
  265. {
  266. rt_uint8_t value;
  267. /* parameter check */
  268. RT_ASSERT(device != RT_NULL);
  269. RT_ASSERT(setup != RT_NULL);
  270. RT_ASSERT(device->curr_cfg != RT_NULL);
  271. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
  272. if (device->state == USB_STATE_CONFIGURED)
  273. {
  274. /* get current configuration */
  275. value = device->curr_cfg->cfg_desc.bConfigurationValue;
  276. }
  277. else
  278. {
  279. value = 0;
  280. }
  281. /* write the current configuration to endpoint 0 */
  282. rt_usbd_ep0_write(device, &value, 1);
  283. return RT_EOK;
  284. }
  285. /**
  286. * This function will handle set_config request.
  287. *
  288. * @param device the usb device object.
  289. * @param setup the setup request.
  290. *
  291. * @return RT_EOK on successful.
  292. */
  293. static rt_err_t _set_config(struct udevice* device, ureq_t setup)
  294. {
  295. struct rt_list_node *i, *j, *k;
  296. uconfig_t cfg;
  297. uintf_t intf;
  298. ualtsetting_t setting;
  299. uep_t ep;
  300. /* parameter check */
  301. RT_ASSERT(device != RT_NULL);
  302. RT_ASSERT(setup != RT_NULL);
  303. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
  304. if (setup->value > device->dev_desc.bNumConfigurations)
  305. {
  306. rt_usbd_ep0_set_stall(device);
  307. return -RT_ERROR;
  308. }
  309. if (setup->value == 0)
  310. {
  311. RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
  312. device->state = USB_STATE_ADDRESS;
  313. goto _exit;
  314. }
  315. /* set current configuration */
  316. rt_usbd_set_config(device, setup->value);
  317. dcd_set_config(device->dcd, setup->value);
  318. cfg = device->curr_cfg;
  319. for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
  320. {
  321. /* run all functiones and their endpoints in the configuration */
  322. ufunction_t func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  323. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  324. {
  325. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  326. setting = intf->curr_setting;
  327. for(k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
  328. {
  329. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  330. /* first disable then enable an endpoint */
  331. dcd_ep_disable(device->dcd, ep);
  332. dcd_ep_enable(device->dcd, ep);
  333. }
  334. }
  335. /* after enabled endpoints, then enable function */
  336. FUNC_ENABLE(func);
  337. }
  338. device->state = USB_STATE_CONFIGURED;
  339. _exit:
  340. /* issue status stage */
  341. dcd_ep0_send_status(device->dcd);
  342. return RT_EOK;
  343. }
  344. /**
  345. * This function will handle set_address request.
  346. *
  347. * @param device the usb device object.
  348. * @param setup the setup request.
  349. *
  350. * @return RT_EOK on successful.
  351. */
  352. static rt_err_t _set_address(struct udevice* device, ureq_t setup)
  353. {
  354. /* parameter check */
  355. RT_ASSERT(device != RT_NULL);
  356. RT_ASSERT(setup != RT_NULL);
  357. /* issue status stage */
  358. dcd_ep0_send_status(device->dcd);
  359. /* set address in device control driver */
  360. dcd_set_address(device->dcd, setup->value);
  361. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
  362. device->state = USB_STATE_ADDRESS;
  363. return RT_EOK;
  364. }
  365. /**
  366. * This function will handle standard request to
  367. * interface that defined in function-specifics
  368. *
  369. * @param device the usb device object.
  370. * @param setup the setup request.
  371. *
  372. * @return RT_EOK on successful.
  373. */
  374. static rt_err_t _request_interface(struct udevice* device, ureq_t setup)
  375. {
  376. uintf_t intf;
  377. ufunction_t func;
  378. rt_err_t ret;
  379. /* parameter check */
  380. RT_ASSERT(device != RT_NULL);
  381. RT_ASSERT(setup != RT_NULL);
  382. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
  383. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &func);
  384. if (intf != RT_NULL)
  385. {
  386. ret = intf->handler(func, setup);
  387. }
  388. else
  389. {
  390. ret = -RT_ERROR;
  391. }
  392. return ret;
  393. }
  394. /**
  395. * This function will handle standard request.
  396. *
  397. * @param device the usb device object.
  398. * @param setup the setup request.
  399. *
  400. * @return RT_EOK on successful.
  401. */
  402. static rt_err_t _standard_request(struct udevice* device, ureq_t setup)
  403. {
  404. udcd_t dcd;
  405. rt_uint16_t value = 0;
  406. /* parameter check */
  407. RT_ASSERT(device != RT_NULL);
  408. RT_ASSERT(setup != RT_NULL);
  409. dcd = device->dcd;
  410. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  411. {
  412. case USB_REQ_TYPE_DEVICE:
  413. switch(setup->request)
  414. {
  415. case USB_REQ_GET_STATUS:
  416. rt_usbd_ep0_write(device, &value, 2);
  417. break;
  418. case USB_REQ_CLEAR_FEATURE:
  419. rt_usbd_clear_feature(device, setup->value, setup->index);
  420. dcd_ep0_send_status(dcd);
  421. break;
  422. case USB_REQ_SET_FEATURE:
  423. rt_usbd_set_feature(device, setup->value, setup->index);
  424. break;
  425. case USB_REQ_SET_ADDRESS:
  426. _set_address(device, setup);
  427. break;
  428. case USB_REQ_GET_DESCRIPTOR:
  429. _get_descriptor(device, setup);
  430. break;
  431. case USB_REQ_SET_DESCRIPTOR:
  432. rt_usbd_ep0_set_stall(device);
  433. break;
  434. case USB_REQ_GET_CONFIGURATION:
  435. _get_config(device, setup);
  436. break;
  437. case USB_REQ_SET_CONFIGURATION:
  438. _set_config(device, setup);
  439. break;
  440. default:
  441. rt_kprintf("unknown device request\n");
  442. rt_usbd_ep0_set_stall(device);
  443. break;
  444. }
  445. break;
  446. case USB_REQ_TYPE_INTERFACE:
  447. switch(setup->request)
  448. {
  449. case USB_REQ_GET_INTERFACE:
  450. _get_interface(device, setup);
  451. break;
  452. case USB_REQ_SET_INTERFACE:
  453. _set_interface(device, setup);
  454. break;
  455. default:
  456. if (_request_interface(device, setup) != RT_EOK)
  457. {
  458. rt_kprintf("unknown interface request\n");
  459. rt_usbd_ep0_set_stall(device);
  460. return - RT_ERROR;
  461. }
  462. else
  463. break;
  464. }
  465. break;
  466. case USB_REQ_TYPE_ENDPOINT:
  467. switch(setup->request)
  468. {
  469. case USB_REQ_GET_STATUS:
  470. {
  471. uep_t ep;
  472. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  473. value = ep->stalled;
  474. rt_usbd_ep0_write(device, &value, 2);
  475. }
  476. break;
  477. case USB_REQ_CLEAR_FEATURE:
  478. {
  479. uep_t ep;
  480. uio_request_t req;
  481. struct rt_list_node *node;
  482. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  483. if(USB_EP_HALT == setup->value && ep->stalled == RT_TRUE)
  484. {
  485. rt_usbd_clear_feature(device, setup->value, setup->index);
  486. dcd_ep0_send_status(dcd);
  487. ep->stalled = RT_FALSE;
  488. for (node = ep->request_list.next; node != &ep->request_list; node = node->next)
  489. {
  490. req = (uio_request_t)rt_list_entry(node, struct uio_request, list);
  491. rt_usbd_io_request(device, ep, req);
  492. RT_DEBUG_LOG(RT_DEBUG_USB, ("fired a request\n"));
  493. }
  494. rt_list_init(&ep->request_list);
  495. }
  496. }
  497. break;
  498. case USB_REQ_SET_FEATURE:
  499. {
  500. uep_t ep;
  501. if(USB_EP_HALT == setup->value)
  502. {
  503. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  504. ep->stalled = RT_TRUE;
  505. rt_usbd_set_feature(device, setup->value, setup->index);
  506. dcd_ep0_send_status(dcd);
  507. }
  508. }
  509. break;
  510. case USB_REQ_SYNCH_FRAME:
  511. break;
  512. default:
  513. rt_kprintf("unknown endpoint request\n");
  514. rt_usbd_ep0_set_stall(device);
  515. break;
  516. }
  517. break;
  518. case USB_REQ_TYPE_OTHER:
  519. rt_kprintf("unknown other type request\n");
  520. rt_usbd_ep0_set_stall(device);
  521. break;
  522. default:
  523. rt_kprintf("unknown type request\n");
  524. rt_usbd_ep0_set_stall(device);
  525. break;
  526. }
  527. return RT_EOK;
  528. }
  529. /**
  530. * This function will handle function request.
  531. *
  532. * @param device the usb device object.
  533. * @param setup the setup request.
  534. *
  535. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  536. */
  537. static rt_err_t _function_request(udevice_t device, ureq_t setup)
  538. {
  539. uintf_t intf;
  540. ufunction_t func;
  541. /* parameter check */
  542. RT_ASSERT(device != RT_NULL);
  543. RT_ASSERT(setup != RT_NULL);
  544. /* verify request value */
  545. if(setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
  546. {
  547. rt_usbd_ep0_set_stall(device);
  548. return -RT_ERROR;
  549. }
  550. switch(setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  551. {
  552. case USB_REQ_TYPE_INTERFACE:
  553. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &func);
  554. if(intf == RT_NULL)
  555. {
  556. rt_kprintf("unkwown interface request\n");
  557. rt_usbd_ep0_set_stall(device);
  558. }
  559. else
  560. {
  561. intf->handler(func, setup);
  562. }
  563. break;
  564. case USB_REQ_TYPE_ENDPOINT:
  565. break;
  566. default:
  567. rt_kprintf("unknown function request type\n");
  568. rt_usbd_ep0_set_stall(device);
  569. break;
  570. }
  571. return RT_EOK;
  572. }
  573. static rt_err_t _dump_setup_packet(ureq_t setup)
  574. {
  575. RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
  576. RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request 0x%x\n",
  577. setup->request_type));
  578. RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
  579. RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
  580. RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
  581. RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
  582. RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
  583. return RT_EOK;
  584. }
  585. /**
  586. * This function will handle setup request.
  587. *
  588. * @param device the usb device object.
  589. * @param setup the setup request.
  590. *
  591. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  592. */
  593. static rt_err_t _setup_request(udevice_t device, ureq_t setup)
  594. {
  595. /* parameter check */
  596. RT_ASSERT(device != RT_NULL);
  597. RT_ASSERT(setup != RT_NULL);
  598. _dump_setup_packet(setup);
  599. switch((setup->request_type & USB_REQ_TYPE_MASK))
  600. {
  601. case USB_REQ_TYPE_STANDARD:
  602. _standard_request(device, setup);
  603. break;
  604. case USB_REQ_TYPE_CLASS:
  605. _function_request(device, setup);
  606. break;
  607. case USB_REQ_TYPE_VENDOR:
  608. rt_kprintf("vendor type request\n");
  609. break;
  610. default:
  611. rt_kprintf("unknown setup request type\n");
  612. rt_usbd_ep0_set_stall(device);
  613. return -RT_ERROR;
  614. }
  615. return RT_EOK;
  616. }
  617. /**
  618. * This function will hanle data notify event.
  619. *
  620. * @param device the usb device object.
  621. * @param ep_msg the endpoint message.
  622. *
  623. * @return RT_EOK.
  624. */
  625. static rt_err_t _data_notify(udevice_t device, struct ep_msg* ep_msg)
  626. {
  627. uep_t ep;
  628. ufunction_t func;
  629. rt_size_t size = 0;
  630. RT_ASSERT(device != RT_NULL);
  631. RT_ASSERT(ep_msg != RT_NULL);
  632. if (device->state != USB_STATE_CONFIGURED)
  633. {
  634. return -RT_ERROR;
  635. }
  636. ep = rt_usbd_find_endpoint(device, &func, ep_msg->ep_addr);
  637. if(ep == RT_NULL)
  638. {
  639. rt_kprintf("invalid endpoint\n");
  640. return -RT_ERROR;
  641. }
  642. if(EP_ADDRESS(ep) & USB_DIR_IN)
  643. {
  644. if(ep->request.remain_size >= EP_MAXPACKET(ep))
  645. {
  646. dcd_ep_write(device->dcd, EP_ADDRESS(ep),
  647. ep->request.buffer, EP_MAXPACKET(ep));
  648. ep->request.remain_size -= EP_MAXPACKET(ep);
  649. ep->request.buffer += EP_MAXPACKET(ep);
  650. }
  651. else if(ep->request.remain_size > 0)
  652. {
  653. dcd_ep_write(device->dcd, EP_ADDRESS(ep),
  654. ep->request.buffer, ep->request.remain_size);
  655. ep->request.remain_size = 0;
  656. }
  657. else
  658. {
  659. EP_HANDLER(ep, func, size);
  660. }
  661. }
  662. else
  663. {
  664. size = ep_msg->size;
  665. if(ep->request.remain_size == 0)
  666. {
  667. return RT_EOK;
  668. }
  669. if(size == 0)
  670. {
  671. size = dcd_ep_read(device->dcd, EP_ADDRESS(ep),
  672. ep->request.buffer);
  673. }
  674. if(size > ep->request.remain_size)
  675. {
  676. ep->request.remain_size = 0;
  677. }
  678. else
  679. {
  680. ep->request.remain_size -= size;
  681. ep->request.buffer += size;
  682. }
  683. if(ep->request.req_type == UIO_REQUEST_READ_MOST)
  684. {
  685. EP_HANDLER(ep, func, size);
  686. }
  687. else if(ep->request.remain_size == 0)
  688. {
  689. EP_HANDLER(ep, func, ep->request.size);
  690. }
  691. }
  692. return RT_EOK;
  693. }
  694. static rt_err_t _ep0_out_notify(udevice_t device, struct ep_msg* ep_msg)
  695. {
  696. uep_t ep0;
  697. rt_size_t size;
  698. RT_ASSERT(device != RT_NULL);
  699. RT_ASSERT(ep_msg != RT_NULL);
  700. RT_ASSERT(device->dcd != RT_NULL);
  701. ep0 = &device->dcd->ep0;
  702. size = ep_msg->size;
  703. if(ep0->request.remain_size == 0)
  704. {
  705. return RT_EOK;
  706. }
  707. if(size == 0)
  708. {
  709. size = dcd_ep_read(device->dcd, EP0_OUT_ADDR, ep0->request.buffer);
  710. if(size == 0)
  711. {
  712. return RT_EOK;
  713. }
  714. }
  715. ep0->request.remain_size -= size;
  716. ep0->request.buffer += size;
  717. if(ep0->request.remain_size == 0)
  718. {
  719. /* invoke callback */
  720. if(ep0->rx_indicate != RT_NULL)
  721. {
  722. ep0->rx_indicate(device, size);
  723. }
  724. }
  725. return RT_EOK;
  726. }
  727. /**
  728. * This function will notity sof event to all of function.
  729. *
  730. * @param device the usb device object.
  731. *
  732. * @return RT_EOK.
  733. */
  734. static rt_err_t _sof_notify(udevice_t device)
  735. {
  736. struct rt_list_node *i;
  737. ufunction_t func;
  738. RT_ASSERT(device != RT_NULL);
  739. /* to notity every function that sof event comes */
  740. for (i=device->curr_cfg->func_list.next;
  741. i!=&device->curr_cfg->func_list; i=i->next)
  742. {
  743. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  744. if(func->ops->sof_handler != RT_NULL)
  745. func->ops->sof_handler(func);
  746. }
  747. return RT_EOK;
  748. }
  749. /**
  750. * This function will disable all USB functions.
  751. *
  752. * @param device the usb device object.
  753. *
  754. * @return RT_EOK.
  755. */
  756. static rt_err_t _stop_notify(udevice_t device)
  757. {
  758. struct rt_list_node *i;
  759. ufunction_t func;
  760. RT_ASSERT(device != RT_NULL);
  761. /* to notity every function */
  762. for (i = device->curr_cfg->func_list.next;
  763. i != &device->curr_cfg->func_list;
  764. i = i->next)
  765. {
  766. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  767. FUNC_DISABLE(func);
  768. }
  769. return RT_EOK;
  770. }
  771. static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
  772. {
  773. rt_uint16_t maxpacket;
  774. rt_size_t sent_size;
  775. RT_ASSERT(device != RT_NULL);
  776. RT_ASSERT(device->dcd != RT_NULL);
  777. RT_ASSERT(ep != RT_NULL);
  778. maxpacket = EP_MAXPACKET(ep);
  779. if(ep->request.remain_size >= maxpacket)
  780. {
  781. sent_size = dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket);
  782. ep->request.remain_size -= sent_size;
  783. ep->request.buffer += maxpacket;
  784. }
  785. else
  786. {
  787. sent_size = dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer,
  788. ep->request.remain_size);
  789. ep->request.remain_size -= sent_size;
  790. }
  791. return sent_size;
  792. }
  793. static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
  794. {
  795. RT_ASSERT(device != RT_NULL);
  796. RT_ASSERT(device->dcd != RT_NULL);
  797. RT_ASSERT(ep != RT_NULL);
  798. RT_ASSERT(buffer != RT_NULL);
  799. RT_ASSERT(ep->ep_desc != RT_NULL);
  800. return dcd_ep_read_prepare(device->dcd, EP_ADDRESS(ep), buffer, size);
  801. }
  802. /**
  803. * This function will create an usb device object.
  804. *
  805. * @param ustring the usb string array to contain string descriptor.
  806. *
  807. * @return an usb device object on success, RT_NULL on fail.
  808. */
  809. udevice_t rt_usbd_device_new(void)
  810. {
  811. udevice_t udevice;
  812. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_new\n"));
  813. /* allocate memory for the object */
  814. udevice = rt_malloc(sizeof(struct udevice));
  815. if(udevice == RT_NULL)
  816. {
  817. rt_kprintf("alloc memery failed\n");
  818. return RT_NULL;
  819. }
  820. rt_memset(udevice, 0, sizeof(struct udevice));
  821. /* to initialize configuration list */
  822. rt_list_init(&udevice->cfg_list);
  823. /* insert the device object to device list */
  824. rt_list_insert_before(&device_list, &udevice->list);
  825. return udevice;
  826. }
  827. /**
  828. * This function will set usb device string description.
  829. *
  830. * @param device the usb device object.
  831. * @param ustring pointer to string pointer array.
  832. *
  833. * @return RT_EOK.
  834. */
  835. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  836. {
  837. /* parameter check */
  838. RT_ASSERT(device != RT_NULL);
  839. RT_ASSERT(ustring != RT_NULL);
  840. /* set string descriptor array to the device object */
  841. device->str = ustring;
  842. return RT_EOK;
  843. }
  844. rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier)
  845. {
  846. /* parameter check */
  847. RT_ASSERT(device != RT_NULL);
  848. RT_ASSERT(qualifier != RT_NULL);
  849. device->dev_qualifier = qualifier;
  850. return RT_EOK;
  851. }
  852. /**
  853. * This function will set an usb controller driver to a device.
  854. *
  855. * @param device the usb device object.
  856. * @param dcd the usb device controller driver.
  857. *
  858. * @return RT_EOK on successful.
  859. */
  860. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  861. {
  862. /* parameter check */
  863. RT_ASSERT(device != RT_NULL);
  864. RT_ASSERT(dcd != RT_NULL);
  865. /* set usb device controller driver to the device */
  866. device->dcd = dcd;
  867. return RT_EOK;
  868. }
  869. /**
  870. * This function will set an usb device descriptor to a device.
  871. *
  872. * @param device the usb device object.
  873. * @param dev_desc the usb device descriptor.
  874. *
  875. * @return RT_EOK on successful.
  876. */
  877. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  878. {
  879. /* parameter check */
  880. RT_ASSERT(device != RT_NULL);
  881. RT_ASSERT(dev_desc != RT_NULL);
  882. /* copy the usb device descriptor to the device */
  883. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  884. return RT_EOK;
  885. }
  886. /**
  887. * This function will create an usb configuration object.
  888. *
  889. * @param none.
  890. *
  891. * @return an usb configuration object.
  892. */
  893. uconfig_t rt_usbd_config_new(void)
  894. {
  895. uconfig_t cfg;
  896. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_new\n"));
  897. /* allocate memory for the object */
  898. cfg = rt_malloc(sizeof(struct uconfig));
  899. if(cfg == RT_NULL)
  900. {
  901. rt_kprintf("alloc memery failed\n");
  902. return RT_NULL;
  903. }
  904. rt_memset(cfg, 0, sizeof(struct uconfig));
  905. /* set default value */
  906. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  907. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  908. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  909. cfg->cfg_desc.bmAttributes = 0xC0;
  910. cfg->cfg_desc.MaxPower = 0x32;
  911. /* to initialize function object list */
  912. rt_list_init(&cfg->func_list);
  913. return cfg;
  914. }
  915. /**
  916. * This function will create an usb interface object.
  917. *
  918. * @param device the usb device object.
  919. * @handler the callback handler of object
  920. *
  921. * @return an usb interface object on success, RT_NULL on fail.
  922. */
  923. uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler)
  924. {
  925. uintf_t intf;
  926. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_new\n"));
  927. /* parameter check */
  928. RT_ASSERT(device != RT_NULL);
  929. /* allocate memory for the object */
  930. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  931. if(intf == RT_NULL)
  932. {
  933. rt_kprintf("alloc memery failed\n");
  934. return RT_NULL;
  935. }
  936. intf->intf_num = device->nr_intf;
  937. device->nr_intf++;
  938. intf->handler = handler;
  939. intf->curr_setting = RT_NULL;
  940. /* to initialize the alternate setting object list */
  941. rt_list_init(&intf->setting_list);
  942. return intf;
  943. }
  944. /**
  945. * This function will create an usb alternate setting object.
  946. *
  947. * @param intf_desc the interface descriptor.
  948. * @desc_size the size of the interface descriptor.
  949. *
  950. * @return an usb alternate setting object on success, RT_NULL on fail.
  951. */
  952. ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size)
  953. {
  954. ualtsetting_t setting;
  955. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_new\n"));
  956. /* parameter check */
  957. RT_ASSERT(desc_size > 0);
  958. /* allocate memory for the object */
  959. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  960. if(setting == RT_NULL)
  961. {
  962. rt_kprintf("alloc memery failed\n");
  963. return RT_NULL;
  964. }
  965. /* allocate memory for the desc */
  966. setting->desc = rt_malloc(desc_size);
  967. if (setting->desc == RT_NULL)
  968. {
  969. rt_kprintf("alloc desc memery failed\n");
  970. rt_free(setting);
  971. return RT_NULL;
  972. }
  973. setting->desc_size = desc_size;
  974. setting->intf_desc = RT_NULL;
  975. /* to initialize endpoint list */
  976. rt_list_init(&setting->ep_list);
  977. return setting;
  978. }
  979. /**
  980. * This function will config an desc in alternate setting object.
  981. *
  982. * @param setting the altsetting to be config.
  983. * @param desc use it to init desc in setting.
  984. * @param intf_pos the offset of interface descriptor in desc.
  985. *
  986. * @return RT_EOK.
  987. */
  988. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  989. {
  990. RT_ASSERT(setting != RT_NULL);
  991. RT_ASSERT(setting->desc !=RT_NULL);
  992. rt_memcpy(setting->desc, desc, setting->desc_size);
  993. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  994. return RT_EOK;
  995. }
  996. /**
  997. * This function will create an usb function object.
  998. *
  999. * @param device the usb device object.
  1000. * @param dev_desc the device descriptor.
  1001. * @param ops the operation set.
  1002. *
  1003. * @return an usb function object on success, RT_NULL on fail.
  1004. */
  1005. ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
  1006. ufunction_ops_t ops)
  1007. {
  1008. ufunction_t func;
  1009. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_new\n"));
  1010. /* parameter check */
  1011. RT_ASSERT(device != RT_NULL);
  1012. RT_ASSERT(dev_desc != RT_NULL);
  1013. /* allocate memory for the object */
  1014. func = (ufunction_t)rt_malloc(sizeof(struct ufunction));
  1015. if(func == RT_NULL)
  1016. {
  1017. rt_kprintf("alloc memery failed\n");
  1018. return RT_NULL;
  1019. }
  1020. func->dev_desc = dev_desc;
  1021. func->ops = ops;
  1022. func->device = device;
  1023. func->enabled = RT_FALSE;
  1024. /* to initialize interface list */
  1025. rt_list_init(&func->intf_list);
  1026. return func;
  1027. }
  1028. /**
  1029. * This function will create an usb endpoint object.
  1030. *
  1031. * @param ep_desc the endpoint descriptor.
  1032. * @handler the callback handler of object
  1033. *
  1034. * @return an usb endpoint object on success, RT_NULL on fail.
  1035. */
  1036. uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler)
  1037. {
  1038. uep_t ep;
  1039. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_new\n"));
  1040. /* parameter check */
  1041. RT_ASSERT(ep_desc != RT_NULL);
  1042. /* allocate memory for the object */
  1043. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  1044. if(ep == RT_NULL)
  1045. {
  1046. rt_kprintf("alloc memery failed\n");
  1047. return RT_NULL;
  1048. }
  1049. ep->ep_desc = ep_desc;
  1050. ep->handler = handler;
  1051. ep->buffer = RT_NULL;
  1052. ep->stalled = RT_FALSE;
  1053. rt_list_init(&ep->request_list);
  1054. return ep;
  1055. }
  1056. /**
  1057. * This function will find an usb device object.
  1058. *
  1059. * @dcd usd device controller driver.
  1060. *
  1061. * @return an usb device object on found or RT_NULL on not found.
  1062. */
  1063. udevice_t rt_usbd_find_device(udcd_t dcd)
  1064. {
  1065. struct rt_list_node* node;
  1066. udevice_t device;
  1067. /* parameter check */
  1068. RT_ASSERT(dcd != RT_NULL);
  1069. /* search a device in the the device list */
  1070. for (node = device_list.next; node != &device_list; node = node->next)
  1071. {
  1072. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  1073. if(device->dcd == dcd) return device;
  1074. }
  1075. rt_kprintf("can't find device\n");
  1076. return RT_NULL;
  1077. }
  1078. /**
  1079. * This function will find an usb configuration object.
  1080. *
  1081. * @param device the usb device object.
  1082. * @param value the configuration number.
  1083. *
  1084. * @return an usb configuration object on found or RT_NULL on not found.
  1085. */
  1086. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  1087. {
  1088. struct rt_list_node* node;
  1089. uconfig_t cfg = RT_NULL;
  1090. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  1091. /* parameter check */
  1092. RT_ASSERT(device != RT_NULL);
  1093. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1094. /* search a configration in the the device */
  1095. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  1096. {
  1097. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  1098. if(cfg->cfg_desc.bConfigurationValue == value)
  1099. {
  1100. return cfg;
  1101. }
  1102. }
  1103. rt_kprintf("can't find configuration %d\n", value);
  1104. return RT_NULL;
  1105. }
  1106. /**
  1107. * This function will find an usb interface object.
  1108. *
  1109. * @param device the usb device object.
  1110. * @param value the interface number.
  1111. *
  1112. * @return an usb configuration object on found or RT_NULL on not found.
  1113. */
  1114. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc)
  1115. {
  1116. struct rt_list_node *i, *j;
  1117. ufunction_t func;
  1118. uintf_t intf;
  1119. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  1120. /* parameter check */
  1121. RT_ASSERT(device != RT_NULL);
  1122. RT_ASSERT(value < device->nr_intf);
  1123. /* search an interface in the current configuration */
  1124. for (i=device->curr_cfg->func_list.next;
  1125. i!=&device->curr_cfg->func_list; i=i->next)
  1126. {
  1127. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1128. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1129. {
  1130. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1131. if(intf->intf_num == value)
  1132. {
  1133. if (pfunc != RT_NULL)
  1134. *pfunc = func;
  1135. return intf;
  1136. }
  1137. }
  1138. }
  1139. rt_kprintf("can't find interface %d\n", value);
  1140. return RT_NULL;
  1141. }
  1142. /**
  1143. * This function will find an usb interface alternate setting object.
  1144. *
  1145. * @param device the usb device object.
  1146. * @param value the alternate setting number.
  1147. *
  1148. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  1149. */
  1150. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  1151. {
  1152. struct rt_list_node *i;
  1153. ualtsetting_t setting;
  1154. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  1155. /* parameter check */
  1156. RT_ASSERT(intf != RT_NULL);
  1157. if(intf->curr_setting != RT_NULL)
  1158. {
  1159. /* if the value equal to the current alternate setting, then do not search */
  1160. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  1161. return intf->curr_setting;
  1162. }
  1163. /* search a setting in the alternate setting list */
  1164. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  1165. {
  1166. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  1167. if(setting->intf_desc->bAlternateSetting == value)
  1168. return setting;
  1169. }
  1170. rt_kprintf("can't find alternate setting %d\n", value);
  1171. return RT_NULL;
  1172. }
  1173. /**
  1174. * This function will find an usb endpoint object.
  1175. *
  1176. * @param device the usb device object.
  1177. * @param ep_addr endpoint address.
  1178. *
  1179. * @return an usb endpoint object on found or RT_NULL on not found.
  1180. */
  1181. uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr)
  1182. {
  1183. uep_t ep;
  1184. struct rt_list_node *i, *j, *k;
  1185. ufunction_t func;
  1186. uintf_t intf;
  1187. /* parameter check */
  1188. RT_ASSERT(device != RT_NULL);
  1189. /* search a endpoint in the current configuration */
  1190. for (i=device->curr_cfg->func_list.next;
  1191. i!=&device->curr_cfg->func_list; i=i->next)
  1192. {
  1193. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1194. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1195. {
  1196. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1197. for(k=intf->curr_setting->ep_list.next;
  1198. k!=&intf->curr_setting->ep_list; k=k->next)
  1199. {
  1200. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1201. if(EP_ADDRESS(ep) == ep_addr)
  1202. {
  1203. if (pfunc != RT_NULL)
  1204. *pfunc = func;
  1205. return ep;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  1211. return RT_NULL;
  1212. }
  1213. /**
  1214. * This function will add a configuration to an usb device.
  1215. *
  1216. * @param device the usb device object.
  1217. * @param cfg the configuration object.
  1218. *
  1219. * @return RT_EOK.
  1220. */
  1221. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  1222. {
  1223. struct rt_list_node *i, *j, *k;
  1224. ufunction_t func;
  1225. uintf_t intf;
  1226. uep_t ep;
  1227. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  1228. /* parameter check */
  1229. RT_ASSERT(device != RT_NULL);
  1230. RT_ASSERT(cfg != RT_NULL);
  1231. /* set configuration number to the configuration descriptor */
  1232. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  1233. device->dev_desc.bNumConfigurations++;
  1234. for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
  1235. {
  1236. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1237. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1238. {
  1239. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1240. cfg->cfg_desc.bNumInterfaces++;
  1241. /* allocate address for every endpoint in the interface alternate setting */
  1242. for(k=intf->curr_setting->ep_list.next;
  1243. k!=&intf->curr_setting->ep_list; k=k->next)
  1244. {
  1245. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1246. if(rt_usbd_ep_assign(device, ep) != RT_EOK)
  1247. {
  1248. rt_kprintf("endpoint assign error\n");
  1249. }
  1250. }
  1251. /* construct complete configuration descriptor */
  1252. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
  1253. (void*)intf->curr_setting->desc,
  1254. intf->curr_setting->desc_size);
  1255. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  1256. }
  1257. }
  1258. /* insert the configuration to the list */
  1259. rt_list_insert_before(&device->cfg_list, &cfg->list);
  1260. return RT_EOK;
  1261. }
  1262. /**
  1263. * This function will add a function to a configuration.
  1264. *
  1265. * @param cfg the configuration object.
  1266. * @param func the function object.
  1267. *
  1268. * @return RT_EOK.
  1269. */
  1270. rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func)
  1271. {
  1272. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_function\n"));
  1273. /* parameter check */
  1274. RT_ASSERT(cfg != RT_NULL);
  1275. RT_ASSERT(func != RT_NULL);
  1276. /* insert the function to the list */
  1277. rt_list_insert_before(&cfg->func_list, &func->list);
  1278. return RT_EOK;
  1279. }
  1280. /**
  1281. * This function will add an interface to a function.
  1282. *
  1283. * @param func the function object.
  1284. * @param intf the interface object.
  1285. *
  1286. * @return RT_EOK.
  1287. */
  1288. rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf)
  1289. {
  1290. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_add_interface\n"));
  1291. /* parameter check */
  1292. RT_ASSERT(func != RT_NULL);
  1293. RT_ASSERT(intf != RT_NULL);
  1294. /* insert the interface to the list */
  1295. rt_list_insert_before(&func->intf_list, &intf->list);
  1296. return RT_EOK;
  1297. }
  1298. /**
  1299. * This function will add an alternate setting to an interface.
  1300. *
  1301. * @param intf the interface object.
  1302. * @param setting the alternate setting object.
  1303. *
  1304. * @return RT_EOK.
  1305. */
  1306. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1307. {
  1308. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1309. /* parameter check */
  1310. RT_ASSERT(intf != RT_NULL);
  1311. RT_ASSERT(setting != RT_NULL);
  1312. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1313. /* insert the alternate setting to the list */
  1314. rt_list_insert_before(&intf->setting_list, &setting->list);
  1315. return RT_EOK;
  1316. }
  1317. /**
  1318. * This function will add an endpoint to an alternate setting.
  1319. *
  1320. * @param setting the alternate setting object.
  1321. * @param ep the endpoint object.
  1322. *
  1323. * @return RT_EOK.
  1324. */
  1325. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1326. {
  1327. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1328. /* parameter check */
  1329. RT_ASSERT(setting != RT_NULL);
  1330. RT_ASSERT(ep != RT_NULL);
  1331. /* insert the endpoint to the list */
  1332. rt_list_insert_before(&setting->ep_list, &ep->list);
  1333. return RT_EOK;
  1334. }
  1335. /**
  1336. * This function will set an alternate setting for an interface.
  1337. *
  1338. * @param intf_desc the interface descriptor.
  1339. * @param value the alternate setting number.
  1340. *
  1341. * @return RT_EOK.
  1342. */
  1343. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1344. {
  1345. ualtsetting_t setting;
  1346. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1347. /* parameter check */
  1348. RT_ASSERT(intf != RT_NULL);
  1349. /* find an alternate setting */
  1350. setting = rt_usbd_find_altsetting(intf, value);
  1351. /* set as current alternate setting */
  1352. intf->curr_setting = setting;
  1353. return RT_EOK;
  1354. }
  1355. /**
  1356. * This function will set a configuration for an usb device.
  1357. *
  1358. * @param device the usb device object.
  1359. * @param value the configuration number.
  1360. *
  1361. * @return RT_EOK.
  1362. */
  1363. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1364. {
  1365. uconfig_t cfg;
  1366. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1367. /* parameter check */
  1368. RT_ASSERT(device != RT_NULL);
  1369. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1370. /* find a configuration */
  1371. cfg = rt_usbd_find_config(device, value);
  1372. /* set as current configuration */
  1373. device->curr_cfg = cfg;
  1374. return RT_TRUE;
  1375. }
  1376. /**
  1377. * This function will request an IO transaction.
  1378. *
  1379. * @param device the usb device object.
  1380. * @param ep the endpoint object.
  1381. * @param req IO request.
  1382. *
  1383. * @return RT_EOK.
  1384. */
  1385. rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
  1386. {
  1387. rt_size_t size = 0;
  1388. RT_ASSERT(device != RT_NULL);
  1389. RT_ASSERT(req != RT_NULL);
  1390. if(ep->stalled == RT_FALSE)
  1391. {
  1392. switch(req->req_type)
  1393. {
  1394. case UIO_REQUEST_READ_MOST:
  1395. case UIO_REQUEST_READ_FULL:
  1396. ep->request.remain_size = ep->request.size;
  1397. size = rt_usbd_ep_read_prepare(device, ep, req->buffer, req->size);
  1398. break;
  1399. case UIO_REQUEST_WRITE:
  1400. ep->request.remain_size = ep->request.size;
  1401. size = rt_usbd_ep_write(device, ep, req->buffer, req->size);
  1402. break;
  1403. default:
  1404. rt_kprintf("unknown request type\n");
  1405. break;
  1406. }
  1407. }
  1408. else
  1409. {
  1410. rt_list_insert_before(&ep->request_list, &req->list);
  1411. RT_DEBUG_LOG(RT_DEBUG_USB, ("suspend a request\n"));
  1412. }
  1413. return size;
  1414. }
  1415. /**
  1416. * This function will set feature for an usb device.
  1417. *
  1418. * @param device the usb device object.
  1419. * @param value the configuration number.
  1420. *
  1421. * @return RT_EOK.
  1422. */
  1423. rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
  1424. {
  1425. RT_ASSERT(device != RT_NULL);
  1426. if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
  1427. {
  1428. RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature remote wakeup\n"));
  1429. }
  1430. else if (value == USB_FEATURE_ENDPOINT_HALT)
  1431. {
  1432. RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature stall\n"));
  1433. dcd_ep_set_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
  1434. }
  1435. return RT_EOK;
  1436. }
  1437. /**
  1438. * This function will clear feature for an usb device.
  1439. *
  1440. * @param device the usb device object.
  1441. * @param value the configuration number.
  1442. *
  1443. * @return RT_EOK.
  1444. */
  1445. rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
  1446. {
  1447. RT_ASSERT(device != RT_NULL);
  1448. if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
  1449. {
  1450. RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature remote wakeup\n"));
  1451. }
  1452. else if (value == USB_FEATURE_ENDPOINT_HALT)
  1453. {
  1454. RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature stall\n"));
  1455. dcd_ep_clear_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
  1456. }
  1457. return RT_EOK;
  1458. }
  1459. rt_err_t rt_usbd_ep0_set_stall(udevice_t device)
  1460. {
  1461. RT_ASSERT(device != RT_NULL);
  1462. return dcd_ep_set_stall(device->dcd, 0);
  1463. }
  1464. rt_err_t rt_usbd_ep0_clear_stall(udevice_t device)
  1465. {
  1466. RT_ASSERT(device != RT_NULL);
  1467. return dcd_ep_clear_stall(device->dcd, 0);
  1468. }
  1469. rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep)
  1470. {
  1471. rt_err_t ret;
  1472. RT_ASSERT(device != RT_NULL);
  1473. RT_ASSERT(ep != RT_NULL);
  1474. RT_ASSERT(ep->ep_desc != RT_NULL);
  1475. ret = dcd_ep_set_stall(device->dcd, EP_ADDRESS(ep));
  1476. if(ret == RT_EOK)
  1477. {
  1478. ep->stalled = RT_TRUE;
  1479. }
  1480. return ret;
  1481. }
  1482. rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep)
  1483. {
  1484. rt_err_t ret;
  1485. RT_ASSERT(device != RT_NULL);
  1486. RT_ASSERT(ep != RT_NULL);
  1487. RT_ASSERT(ep->ep_desc != RT_NULL);
  1488. ret = dcd_ep_clear_stall(device->dcd, EP_ADDRESS(ep));
  1489. if(ret == RT_EOK)
  1490. {
  1491. ep->stalled = RT_FALSE;
  1492. }
  1493. return ret;
  1494. }
  1495. static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep)
  1496. {
  1497. int i = 0;
  1498. RT_ASSERT(device != RT_NULL);
  1499. RT_ASSERT(device->dcd != RT_NULL);
  1500. RT_ASSERT(device->dcd->ep_pool != RT_NULL);
  1501. RT_ASSERT(ep != RT_NULL);
  1502. RT_ASSERT(ep->ep_desc != RT_NULL);
  1503. while(device->dcd->ep_pool[i].addr != 0xFF)
  1504. {
  1505. if(device->dcd->ep_pool[i].status == ID_UNASSIGNED &&
  1506. ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type &&
  1507. ep->ep_desc->bEndpointAddress == device->dcd->ep_pool[i].dir)
  1508. {
  1509. EP_ADDRESS(ep) |= device->dcd->ep_pool[i].addr;
  1510. ep->id = &device->dcd->ep_pool[i];
  1511. device->dcd->ep_pool[i].status = ID_ASSIGNED;
  1512. RT_DEBUG_LOG(RT_DEBUG_USB, ("assigned %d\n", device->dcd->ep_pool[i].addr));
  1513. return RT_EOK;
  1514. }
  1515. i++;
  1516. }
  1517. return -RT_ERROR;
  1518. }
  1519. static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep)
  1520. {
  1521. RT_ASSERT(device != RT_NULL);
  1522. RT_ASSERT(device->dcd != RT_NULL);
  1523. RT_ASSERT(device->dcd->ep_pool != RT_NULL);
  1524. RT_ASSERT(ep != RT_NULL);
  1525. RT_ASSERT(ep->ep_desc != RT_NULL);
  1526. ep->id->status = ID_UNASSIGNED;
  1527. return RT_EOK;
  1528. }
  1529. rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct urequest* setup)
  1530. {
  1531. struct udev_msg msg;
  1532. rt_size_t size;
  1533. RT_ASSERT(dcd != RT_NULL);
  1534. if(setup == RT_NULL)
  1535. {
  1536. size = dcd_ep_read(dcd, EP0_OUT_ADDR, (void*)&msg.content.setup);
  1537. if(size != sizeof(struct urequest))
  1538. {
  1539. rt_kprintf("read setup packet error\n");
  1540. return -RT_ERROR;
  1541. }
  1542. }
  1543. else
  1544. {
  1545. rt_memcpy((void*)&msg.content.setup, (void*)setup, sizeof(struct urequest));
  1546. }
  1547. msg.type = USB_MSG_SETUP_NOTIFY;
  1548. msg.dcd = dcd;
  1549. rt_usbd_event_signal(&msg);
  1550. return RT_EOK;
  1551. }
  1552. rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd)
  1553. {
  1554. RT_ASSERT(dcd != RT_NULL);
  1555. if(dcd->ep0.request.remain_size >= dcd->ep0.id->maxpacket)
  1556. {
  1557. dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, dcd->ep0.id->maxpacket);
  1558. dcd->ep0.request.remain_size -= dcd->ep0.id->maxpacket;
  1559. }
  1560. else if(dcd->ep0.request.remain_size > 0)
  1561. {
  1562. dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, dcd->ep0.request.remain_size);
  1563. dcd->ep0.request.remain_size = 0;
  1564. }
  1565. else
  1566. {
  1567. dcd_ep_write(dcd, EP0_IN_ADDR, RT_NULL, 0);
  1568. }
  1569. return RT_EOK;
  1570. }
  1571. rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size)
  1572. {
  1573. struct udev_msg msg;
  1574. RT_ASSERT(dcd != RT_NULL);
  1575. msg.type = USB_MSG_EP0_OUT;
  1576. msg.dcd = dcd;
  1577. msg.content.ep_msg.size = size;
  1578. rt_usbd_event_signal(&msg);
  1579. return RT_EOK;
  1580. }
  1581. rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address)
  1582. {
  1583. struct udev_msg msg;
  1584. RT_ASSERT(dcd != RT_NULL);
  1585. msg.type = USB_MSG_DATA_NOTIFY;
  1586. msg.dcd = dcd;
  1587. msg.content.ep_msg.ep_addr = address;
  1588. msg.content.ep_msg.size = 0;
  1589. rt_usbd_event_signal(&msg);
  1590. return RT_EOK;
  1591. }
  1592. rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
  1593. {
  1594. struct udev_msg msg;
  1595. RT_ASSERT(dcd != RT_NULL);
  1596. msg.type = USB_MSG_DATA_NOTIFY;
  1597. msg.dcd = dcd;
  1598. msg.content.ep_msg.ep_addr = address;
  1599. msg.content.ep_msg.size = size;
  1600. rt_usbd_event_signal(&msg);
  1601. return RT_EOK;
  1602. }
  1603. rt_err_t rt_usbd_reset_handler(udcd_t dcd)
  1604. {
  1605. struct udev_msg msg;
  1606. RT_ASSERT(dcd != RT_NULL);
  1607. msg.type = USB_MSG_RESET;
  1608. msg.dcd = dcd;
  1609. rt_usbd_event_signal(&msg);
  1610. return RT_EOK;
  1611. }
  1612. rt_err_t rt_usbd_connect_handler(udcd_t dcd)
  1613. {
  1614. struct udev_msg msg;
  1615. RT_ASSERT(dcd != RT_NULL);
  1616. msg.type = USB_MSG_PLUG_IN;
  1617. msg.dcd = dcd;
  1618. rt_usbd_event_signal(&msg);
  1619. return RT_EOK;
  1620. }
  1621. rt_err_t rt_usbd_disconnect_handler(udcd_t dcd)
  1622. {
  1623. struct udev_msg msg;
  1624. RT_ASSERT(dcd != RT_NULL);
  1625. msg.type = USB_MSG_PLUG_OUT;
  1626. msg.dcd = dcd;
  1627. rt_usbd_event_signal(&msg);
  1628. return RT_EOK;
  1629. }
  1630. rt_err_t rt_usbd_sof_handler(udcd_t dcd)
  1631. {
  1632. struct udev_msg msg;
  1633. RT_ASSERT(dcd != RT_NULL);
  1634. msg.type = USB_MSG_SOF;
  1635. msg.dcd = dcd;
  1636. rt_usbd_event_signal(&msg);
  1637. return RT_EOK;
  1638. }
  1639. rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
  1640. {
  1641. uep_t ep0;
  1642. rt_size_t sent_size = 0;
  1643. RT_ASSERT(device != RT_NULL);
  1644. RT_ASSERT(device->dcd != RT_NULL);
  1645. RT_ASSERT(buffer != RT_NULL);
  1646. RT_ASSERT(size > 0);
  1647. ep0 = &device->dcd->ep0;
  1648. ep0->request.size = size;
  1649. ep0->request.buffer = buffer;
  1650. ep0->request.remain_size = size;
  1651. if(ep0->request.remain_size >= ep0->id->maxpacket)
  1652. {
  1653. sent_size = dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->id->maxpacket);
  1654. ep0->request.remain_size -= sent_size;
  1655. ep0->request.buffer += ep0->id->maxpacket;
  1656. }
  1657. else
  1658. {
  1659. sent_size = dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, ep0->request.remain_size);
  1660. ep0->request.remain_size -= sent_size;
  1661. }
  1662. return sent_size;
  1663. }
  1664. rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
  1665. rt_err_t (*rx_ind)(udevice_t device, rt_size_t size))
  1666. {
  1667. uep_t ep0;
  1668. RT_ASSERT(device != RT_NULL);
  1669. RT_ASSERT(device->dcd != RT_NULL);
  1670. RT_ASSERT(buffer != RT_NULL);
  1671. ep0 = &device->dcd->ep0;
  1672. ep0->request.size = size;
  1673. ep0->request.buffer = buffer;
  1674. ep0->request.remain_size = size;
  1675. ep0->rx_indicate = rx_ind;
  1676. dcd_ep_read_prepare(device->dcd, EP0_OUT_ADDR, buffer, size);
  1677. return size;
  1678. }
  1679. static struct rt_messagequeue usb_mq;
  1680. /**
  1681. * This function is the main entry of usb device thread, it is in charge of
  1682. * processing all messages received from the usb message buffer.
  1683. *
  1684. * @param parameter the parameter of the usb device thread.
  1685. *
  1686. * @return none.
  1687. */
  1688. static void rt_usbd_thread_entry(void* parameter)
  1689. {
  1690. while(1)
  1691. {
  1692. struct udev_msg msg;
  1693. udevice_t device;
  1694. /* receive message */
  1695. if(rt_mq_recv(&usb_mq, &msg, sizeof(struct udev_msg),
  1696. RT_WAITING_FOREVER) != RT_EOK )
  1697. continue;
  1698. device = rt_usbd_find_device(msg.dcd);
  1699. if(device == RT_NULL)
  1700. {
  1701. rt_kprintf("invalid usb device\n");
  1702. continue;
  1703. }
  1704. RT_DEBUG_LOG(RT_DEBUG_USB, ("message type %d\n", msg.type));
  1705. switch (msg.type)
  1706. {
  1707. case USB_MSG_SOF:
  1708. _sof_notify(device);
  1709. break;
  1710. case USB_MSG_DATA_NOTIFY:
  1711. /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
  1712. * got configured. */
  1713. _data_notify(device, &msg.content.ep_msg);
  1714. break;
  1715. case USB_MSG_SETUP_NOTIFY:
  1716. _setup_request(device, &msg.content.setup);
  1717. break;
  1718. case USB_MSG_EP0_OUT:
  1719. _ep0_out_notify(device, &msg.content.ep_msg);
  1720. break;
  1721. case USB_MSG_RESET:
  1722. RT_DEBUG_LOG(RT_DEBUG_USB, ("reset %d\n", device->state));
  1723. if (device->state == USB_STATE_ADDRESS)
  1724. _stop_notify(device);
  1725. break;
  1726. case USB_MSG_PLUG_IN:
  1727. device->state = USB_STATE_ATTACHED;
  1728. break;
  1729. case USB_MSG_PLUG_OUT:
  1730. device->state = USB_STATE_NOTATTACHED;
  1731. _stop_notify(device);
  1732. break;
  1733. default:
  1734. rt_kprintf("unknown msg type %d\n", msg.type);
  1735. break;
  1736. }
  1737. }
  1738. }
  1739. /**
  1740. * This function will post an message to usb message queue,
  1741. *
  1742. * @param msg the message to be posted
  1743. * @param size the size of the message .
  1744. *
  1745. * @return the error code, RT_EOK on successfully.
  1746. */
  1747. rt_err_t rt_usbd_event_signal(struct udev_msg* msg)
  1748. {
  1749. RT_ASSERT(msg != RT_NULL);
  1750. /* send message to usb message queue */
  1751. return rt_mq_send(&usb_mq, (void*)msg, sizeof(struct udev_msg));
  1752. }
  1753. ALIGN(RT_ALIGN_SIZE)
  1754. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1755. static struct rt_thread usb_thread;
  1756. #define USBD_MQ_MSG_SZ 32
  1757. #define USBD_MQ_MAX_MSG 16
  1758. /* internal of the message queue: every message is associated with a pointer,
  1759. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1760. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1761. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1762. /**
  1763. * This function will initialize usb device thread.
  1764. *
  1765. * @return none.
  1766. *
  1767. */
  1768. rt_err_t rt_usbd_core_init(void)
  1769. {
  1770. rt_list_init(&device_list);
  1771. /* create an usb message queue */
  1772. rt_mq_init(&usb_mq, "usbd", usb_mq_pool, USBD_MQ_MSG_SZ,
  1773. sizeof(usb_mq_pool), RT_IPC_FLAG_FIFO);
  1774. /* init usb device thread */
  1775. rt_thread_init(&usb_thread, "usbd", rt_usbd_thread_entry, RT_NULL,
  1776. usb_thread_stack, RT_USBD_THREAD_STACK_SZ, RT_USBD_THREAD_PRIO, 20);
  1777. /* rt_thread_init should always be OK, so start the thread without further
  1778. * checking. */
  1779. return rt_thread_startup(&usb_thread);
  1780. }