core.c 57 KB

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