core.c 58 KB

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