core.c 57 KB

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