core.c 58 KB

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