core.c 54 KB

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