core.c 58 KB

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