core.c 57 KB

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