core.c 54 KB

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