core.c 54 KB

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