core.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-10-01 Yi Qiu first version
  9. * 2012-12-12 heyuanjie87 change endpoint and function handler
  10. * 2012-12-30 heyuanjie87 change inferface handler
  11. * 2013-04-26 aozima add DEVICEQUALIFIER support.
  12. * 2013-07-25 Yi Qiu update for USB CV test
  13. * 2017-11-15 ZYH fix ep0 transform error
  14. */
  15. #include <rtthread.h>
  16. #include "drivers/usb_common.h"
  17. #include "drivers/usb_device.h"
  18. static rt_list_t device_list;
  19. static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
  20. static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size);
  21. static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep);
  22. rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep);
  23. /**
  24. * This function will handle get_device_descriptor bRequest.
  25. *
  26. * @param device the usb device object.
  27. * @param setup the setup bRequest.
  28. *
  29. * @return RT_EOK on successful.
  30. */
  31. static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
  32. {
  33. rt_size_t size;
  34. /* parameter check */
  35. RT_ASSERT(device != RT_NULL);
  36. RT_ASSERT(setup != RT_NULL);
  37. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
  38. /* device descriptor wLength should less than USB_DESC_LENGTH_DEVICE*/
  39. size = (setup->wLength > USB_DESC_LENGTH_DEVICE) ?
  40. USB_DESC_LENGTH_DEVICE : setup->wLength;
  41. /* send device descriptor to endpoint 0 */
  42. rt_usbd_ep0_write(device, (rt_uint8_t*) &device->dev_desc, size);
  43. return RT_EOK;
  44. }
  45. /**
  46. * This function will handle get_config_descriptor bRequest.
  47. *
  48. * @param device the usb device object.
  49. * @param setup the setup bRequest.
  50. *
  51. * @return RT_EOK on successful.
  52. */
  53. static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
  54. {
  55. rt_size_t size;
  56. ucfg_desc_t cfg_desc;
  57. /* parameter check */
  58. RT_ASSERT(device != RT_NULL);
  59. RT_ASSERT(setup != RT_NULL);
  60. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
  61. cfg_desc = &device->curr_cfg->cfg_desc;
  62. size = (setup->wLength > cfg_desc->wTotalLength) ?
  63. cfg_desc->wTotalLength : setup->wLength;
  64. /* send configuration descriptor to endpoint 0 */
  65. rt_usbd_ep0_write(device, (rt_uint8_t*)cfg_desc, size);
  66. return RT_EOK;
  67. }
  68. /**
  69. * This function will handle get_string_descriptor bRequest.
  70. *
  71. * @param device the usb device object.
  72. * @param setup the setup bRequest.
  73. *
  74. * @return RT_EOK on successful, -RT_ERROR on invalid bRequest.
  75. */
  76. static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
  77. {
  78. struct ustring_descriptor str_desc;
  79. rt_uint8_t index, i;
  80. rt_uint32_t len;
  81. /* parameter check */
  82. RT_ASSERT(device != RT_NULL);
  83. RT_ASSERT(setup != RT_NULL);
  84. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
  85. str_desc.type = USB_DESC_TYPE_STRING;
  86. index = setup->wValue & 0xFF;
  87. if(index == 0xEE)
  88. {
  89. index = USB_STRING_OS_INDEX;
  90. }
  91. if(index > USB_STRING_MAX)
  92. {
  93. rt_kprintf("unknown string index\n");
  94. rt_usbd_ep0_set_stall(device);
  95. return -RT_ERROR;
  96. }
  97. else if(index == USB_STRING_LANGID_INDEX)
  98. {
  99. str_desc.bLength = 4;
  100. str_desc.String[0] = 0x09;
  101. str_desc.String[1] = 0x04;
  102. }
  103. else
  104. {
  105. len = rt_strlen(device->str[index]);
  106. str_desc.bLength = len*2 + 2;
  107. for(i=0; i<len; i++)
  108. {
  109. str_desc.String[i*2] = device->str[index][i];
  110. str_desc.String[i*2 + 1] = 0;
  111. }
  112. }
  113. if (setup->wLength > str_desc.bLength)
  114. len = str_desc.bLength;
  115. else
  116. len = setup->wLength;
  117. /* send string descriptor to endpoint 0 */
  118. rt_usbd_ep0_write(device, (rt_uint8_t*)&str_desc, len);
  119. return RT_EOK;
  120. }
  121. static rt_err_t _get_qualifier_descriptor(struct udevice* device, ureq_t setup)
  122. {
  123. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_qualifier_descriptor\n"));
  124. /* parameter check */
  125. RT_ASSERT(device != RT_NULL);
  126. RT_ASSERT(setup != RT_NULL);
  127. if(device->dev_qualifier && device->dcd->device_is_hs)
  128. {
  129. /* send device qualifier descriptor to endpoint 0 */
  130. rt_usbd_ep0_write(device, (rt_uint8_t*)device->dev_qualifier,
  131. sizeof(struct usb_qualifier_descriptor));
  132. }
  133. else
  134. {
  135. rt_usbd_ep0_set_stall(device);
  136. }
  137. return RT_EOK;
  138. }
  139. /**
  140. * This function will handle get_descriptor bRequest.
  141. *
  142. * @param device the usb device object.
  143. * @param setup the setup bRequest.
  144. *
  145. * @return RT_EOK on successful.
  146. */
  147. static rt_err_t _get_descriptor(struct udevice* device, ureq_t setup)
  148. {
  149. /* parameter check */
  150. RT_ASSERT(device != RT_NULL);
  151. RT_ASSERT(setup != RT_NULL);
  152. if(setup->request_type == USB_REQ_TYPE_DIR_IN)
  153. {
  154. switch(setup->wValue >> 8)
  155. {
  156. case USB_DESC_TYPE_DEVICE:
  157. _get_device_descriptor(device, setup);
  158. break;
  159. case USB_DESC_TYPE_CONFIGURATION:
  160. _get_config_descriptor(device, setup);
  161. break;
  162. case USB_DESC_TYPE_STRING:
  163. _get_string_descriptor(device, setup);
  164. break;
  165. case USB_DESC_TYPE_DEVICEQUALIFIER:
  166. /* If a full-speed only device (with a device descriptor version number equal to 0200H) receives a
  167. GetDescriptor() request for a device_qualifier, it must respond with a request error. The host must not make
  168. a request for an other_speed_configuration descriptor unless it first successfully retrieves the
  169. device_qualifier descriptor. */
  170. if(device->dcd->device_is_hs)
  171. {
  172. _get_qualifier_descriptor(device, setup);
  173. }
  174. else
  175. {
  176. rt_usbd_ep0_set_stall(device);
  177. }
  178. break;
  179. case USB_DESC_TYPE_OTHERSPEED:
  180. _get_config_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. else
  739. {
  740. 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);
  741. }
  742. }
  743. return RT_EOK;
  744. }
  745. static rt_err_t _ep0_out_notify(udevice_t device, struct ep_msg* ep_msg)
  746. {
  747. uep_t ep0;
  748. rt_size_t size;
  749. RT_ASSERT(device != RT_NULL);
  750. RT_ASSERT(ep_msg != RT_NULL);
  751. RT_ASSERT(device->dcd != RT_NULL);
  752. ep0 = &device->dcd->ep0;
  753. size = ep_msg->size;
  754. if(ep0->request.remain_size == 0)
  755. {
  756. return RT_EOK;
  757. }
  758. if(size == 0)
  759. {
  760. size = dcd_ep_read(device->dcd, EP0_OUT_ADDR, ep0->request.buffer);
  761. if(size == 0)
  762. {
  763. return RT_EOK;
  764. }
  765. }
  766. ep0->request.remain_size -= size;
  767. ep0->request.buffer += size;
  768. if(ep0->request.remain_size == 0)
  769. {
  770. /* invoke callback */
  771. if(ep0->rx_indicate != RT_NULL)
  772. {
  773. ep0->rx_indicate(device, size);
  774. }
  775. }
  776. else
  777. {
  778. rt_usbd_ep0_read(device, ep0->request.buffer, ep0->request.remain_size,ep0->rx_indicate);
  779. }
  780. return RT_EOK;
  781. }
  782. /**
  783. * This function will notity sof event to all of function.
  784. *
  785. * @param device the usb device object.
  786. *
  787. * @return RT_EOK.
  788. */
  789. static rt_err_t _sof_notify(udevice_t device)
  790. {
  791. struct rt_list_node *i;
  792. ufunction_t func;
  793. RT_ASSERT(device != RT_NULL);
  794. /* to notity every function that sof event comes */
  795. for (i=device->curr_cfg->func_list.next;
  796. i!=&device->curr_cfg->func_list; i=i->next)
  797. {
  798. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  799. if(func->ops->sof_handler != RT_NULL)
  800. func->ops->sof_handler(func);
  801. }
  802. return RT_EOK;
  803. }
  804. /**
  805. * This function will disable all USB functions.
  806. *
  807. * @param device the usb device object.
  808. *
  809. * @return RT_EOK.
  810. */
  811. static rt_err_t _stop_notify(udevice_t device)
  812. {
  813. struct rt_list_node *i;
  814. ufunction_t func;
  815. RT_ASSERT(device != RT_NULL);
  816. /* to notity every function */
  817. for (i = device->curr_cfg->func_list.next;
  818. i != &device->curr_cfg->func_list;
  819. i = i->next)
  820. {
  821. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  822. FUNC_DISABLE(func);
  823. }
  824. return RT_EOK;
  825. }
  826. static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
  827. {
  828. rt_uint16_t maxpacket;
  829. RT_ASSERT(device != RT_NULL);
  830. RT_ASSERT(device->dcd != RT_NULL);
  831. RT_ASSERT(ep != RT_NULL);
  832. rt_enter_critical();
  833. maxpacket = EP_MAXPACKET(ep);
  834. if(ep->request.remain_size >= maxpacket)
  835. {
  836. dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer, maxpacket);
  837. ep->request.remain_size -= maxpacket;
  838. ep->request.buffer += maxpacket;
  839. }
  840. else
  841. {
  842. dcd_ep_write(device->dcd, EP_ADDRESS(ep), ep->request.buffer,
  843. ep->request.remain_size);
  844. ep->request.remain_size = 0;
  845. }
  846. rt_exit_critical();
  847. return size;
  848. }
  849. static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size)
  850. {
  851. RT_ASSERT(device != RT_NULL);
  852. RT_ASSERT(device->dcd != RT_NULL);
  853. RT_ASSERT(ep != RT_NULL);
  854. RT_ASSERT(buffer != RT_NULL);
  855. RT_ASSERT(ep->ep_desc != RT_NULL);
  856. return dcd_ep_read_prepare(device->dcd, EP_ADDRESS(ep), buffer, size > EP_MAXPACKET(ep) ? EP_MAXPACKET(ep) : size);
  857. }
  858. /**
  859. * This function will create an usb device object.
  860. *
  861. * @param ustring the usb string array to contain string descriptor.
  862. *
  863. * @return an usb device object on success, RT_NULL on fail.
  864. */
  865. udevice_t rt_usbd_device_new(void)
  866. {
  867. udevice_t udevice;
  868. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_new\n"));
  869. /* allocate memory for the object */
  870. udevice = rt_malloc(sizeof(struct udevice));
  871. if(udevice == RT_NULL)
  872. {
  873. rt_kprintf("alloc memery failed\n");
  874. return RT_NULL;
  875. }
  876. rt_memset(udevice, 0, sizeof(struct udevice));
  877. /* to initialize configuration list */
  878. rt_list_init(&udevice->cfg_list);
  879. /* insert the device object to device list */
  880. rt_list_insert_before(&device_list, &udevice->list);
  881. return udevice;
  882. }
  883. /**
  884. * This function will set usb device string description.
  885. *
  886. * @param device the usb device object.
  887. * @param ustring pointer to string pointer array.
  888. *
  889. * @return RT_EOK.
  890. */
  891. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  892. {
  893. /* parameter check */
  894. RT_ASSERT(device != RT_NULL);
  895. RT_ASSERT(ustring != RT_NULL);
  896. /* set string descriptor array to the device object */
  897. device->str = ustring;
  898. return RT_EOK;
  899. }
  900. rt_err_t rt_usbd_device_set_os_comp_id_desc(udevice_t device, usb_os_comp_id_desc_t os_comp_id_desc)
  901. {
  902. /* parameter check */
  903. RT_ASSERT(device != RT_NULL);
  904. RT_ASSERT(os_comp_id_desc != RT_NULL);
  905. /* set string descriptor array to the device object */
  906. device->os_comp_id_desc = os_comp_id_desc;
  907. rt_list_init(&device->os_comp_id_desc->func_desc);
  908. return RT_EOK;
  909. }
  910. rt_err_t rt_usbd_device_set_qualifier(udevice_t device, struct usb_qualifier_descriptor* qualifier)
  911. {
  912. /* parameter check */
  913. RT_ASSERT(device != RT_NULL);
  914. RT_ASSERT(qualifier != RT_NULL);
  915. device->dev_qualifier = qualifier;
  916. return RT_EOK;
  917. }
  918. /**
  919. * This function will set an usb controller driver to a device.
  920. *
  921. * @param device the usb device object.
  922. * @param dcd the usb device controller driver.
  923. *
  924. * @return RT_EOK on successful.
  925. */
  926. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  927. {
  928. /* parameter check */
  929. RT_ASSERT(device != RT_NULL);
  930. RT_ASSERT(dcd != RT_NULL);
  931. /* set usb device controller driver to the device */
  932. device->dcd = dcd;
  933. return RT_EOK;
  934. }
  935. /**
  936. * This function will set an usb device descriptor to a device.
  937. *
  938. * @param device the usb device object.
  939. * @param dev_desc the usb device descriptor.
  940. *
  941. * @return RT_EOK on successful.
  942. */
  943. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  944. {
  945. /* parameter check */
  946. RT_ASSERT(device != RT_NULL);
  947. RT_ASSERT(dev_desc != RT_NULL);
  948. /* copy the usb device descriptor to the device */
  949. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  950. return RT_EOK;
  951. }
  952. /**
  953. * This function will create an usb configuration object.
  954. *
  955. * @param none.
  956. *
  957. * @return an usb configuration object.
  958. */
  959. uconfig_t rt_usbd_config_new(void)
  960. {
  961. uconfig_t cfg;
  962. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_new\n"));
  963. /* allocate memory for the object */
  964. cfg = rt_malloc(sizeof(struct uconfig));
  965. if(cfg == RT_NULL)
  966. {
  967. rt_kprintf("alloc memery failed\n");
  968. return RT_NULL;
  969. }
  970. rt_memset(cfg, 0, sizeof(struct uconfig));
  971. /* set default wValue */
  972. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  973. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  974. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  975. cfg->cfg_desc.bmAttributes = 0xC0;
  976. cfg->cfg_desc.MaxPower = 0x32;
  977. /* to initialize function object list */
  978. rt_list_init(&cfg->func_list);
  979. return cfg;
  980. }
  981. /**
  982. * This function will create an usb interface object.
  983. *
  984. * @param device the usb device object.
  985. * @handler the callback handler of object
  986. *
  987. * @return an usb interface object on success, RT_NULL on fail.
  988. */
  989. uintf_t rt_usbd_interface_new(udevice_t device, uintf_handler_t handler)
  990. {
  991. uintf_t intf;
  992. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_new\n"));
  993. /* parameter check */
  994. RT_ASSERT(device != RT_NULL);
  995. /* allocate memory for the object */
  996. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  997. if(intf == RT_NULL)
  998. {
  999. rt_kprintf("alloc memery failed\n");
  1000. return RT_NULL;
  1001. }
  1002. intf->intf_num = device->nr_intf;
  1003. device->nr_intf++;
  1004. intf->handler = handler;
  1005. intf->curr_setting = RT_NULL;
  1006. /* to initialize the alternate setting object list */
  1007. rt_list_init(&intf->setting_list);
  1008. return intf;
  1009. }
  1010. /**
  1011. * This function will create an usb alternate setting object.
  1012. *
  1013. * @param intf_desc the interface descriptor.
  1014. * @desc_size the size of the interface descriptor.
  1015. *
  1016. * @return an usb alternate setting object on success, RT_NULL on fail.
  1017. */
  1018. ualtsetting_t rt_usbd_altsetting_new(rt_size_t desc_size)
  1019. {
  1020. ualtsetting_t setting;
  1021. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_new\n"));
  1022. /* parameter check */
  1023. RT_ASSERT(desc_size > 0);
  1024. /* allocate memory for the object */
  1025. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  1026. if(setting == RT_NULL)
  1027. {
  1028. rt_kprintf("alloc memery failed\n");
  1029. return RT_NULL;
  1030. }
  1031. /* allocate memory for the desc */
  1032. setting->desc = rt_malloc(desc_size);
  1033. if (setting->desc == RT_NULL)
  1034. {
  1035. rt_kprintf("alloc desc memery failed\n");
  1036. rt_free(setting);
  1037. return RT_NULL;
  1038. }
  1039. setting->desc_size = desc_size;
  1040. setting->intf_desc = RT_NULL;
  1041. /* to initialize endpoint list */
  1042. rt_list_init(&setting->ep_list);
  1043. return setting;
  1044. }
  1045. /**
  1046. * This function will config an desc in alternate setting object.
  1047. *
  1048. * @param setting the altsetting to be config.
  1049. * @param desc use it to init desc in setting.
  1050. * @param intf_pos the offset of interface descriptor in desc.
  1051. *
  1052. * @return RT_EOK.
  1053. */
  1054. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting, const void* desc, rt_off_t intf_pos)
  1055. {
  1056. RT_ASSERT(setting != RT_NULL);
  1057. RT_ASSERT(setting->desc !=RT_NULL);
  1058. rt_memcpy(setting->desc, desc, setting->desc_size);
  1059. setting->intf_desc = (uintf_desc_t)((char*)setting->desc + intf_pos);
  1060. return RT_EOK;
  1061. }
  1062. /**
  1063. * This function will create an usb function object.
  1064. *
  1065. * @param device the usb device object.
  1066. * @param dev_desc the device descriptor.
  1067. * @param ops the operation set.
  1068. *
  1069. * @return an usb function object on success, RT_NULL on fail.
  1070. */
  1071. ufunction_t rt_usbd_function_new(udevice_t device, udev_desc_t dev_desc,
  1072. ufunction_ops_t ops)
  1073. {
  1074. ufunction_t func;
  1075. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_new\n"));
  1076. /* parameter check */
  1077. RT_ASSERT(device != RT_NULL);
  1078. RT_ASSERT(dev_desc != RT_NULL);
  1079. /* allocate memory for the object */
  1080. func = (ufunction_t)rt_malloc(sizeof(struct ufunction));
  1081. if(func == RT_NULL)
  1082. {
  1083. rt_kprintf("alloc memery failed\n");
  1084. return RT_NULL;
  1085. }
  1086. func->dev_desc = dev_desc;
  1087. func->ops = ops;
  1088. func->device = device;
  1089. func->enabled = RT_FALSE;
  1090. /* to initialize interface list */
  1091. rt_list_init(&func->intf_list);
  1092. return func;
  1093. }
  1094. /**
  1095. * This function will create an usb endpoint object.
  1096. *
  1097. * @param ep_desc the endpoint descriptor.
  1098. * @handler the callback handler of object
  1099. *
  1100. * @return an usb endpoint object on success, RT_NULL on fail.
  1101. */
  1102. uep_t rt_usbd_endpoint_new(uep_desc_t ep_desc, udep_handler_t handler)
  1103. {
  1104. uep_t ep;
  1105. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_new\n"));
  1106. /* parameter check */
  1107. RT_ASSERT(ep_desc != RT_NULL);
  1108. /* allocate memory for the object */
  1109. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  1110. if(ep == RT_NULL)
  1111. {
  1112. rt_kprintf("alloc memery failed\n");
  1113. return RT_NULL;
  1114. }
  1115. ep->ep_desc = ep_desc;
  1116. ep->handler = handler;
  1117. ep->buffer = RT_NULL;
  1118. ep->stalled = RT_FALSE;
  1119. rt_list_init(&ep->request_list);
  1120. return ep;
  1121. }
  1122. /**
  1123. * This function will find an usb device object.
  1124. *
  1125. * @dcd usd device controller driver.
  1126. *
  1127. * @return an usb device object on found or RT_NULL on not found.
  1128. */
  1129. udevice_t rt_usbd_find_device(udcd_t dcd)
  1130. {
  1131. struct rt_list_node* node;
  1132. udevice_t device;
  1133. /* parameter check */
  1134. RT_ASSERT(dcd != RT_NULL);
  1135. /* search a device in the the device list */
  1136. for (node = device_list.next; node != &device_list; node = node->next)
  1137. {
  1138. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  1139. if(device->dcd == dcd) return device;
  1140. }
  1141. rt_kprintf("can't find device\n");
  1142. return RT_NULL;
  1143. }
  1144. /**
  1145. * This function will find an usb configuration object.
  1146. *
  1147. * @param device the usb device object.
  1148. * @param wValue the configuration number.
  1149. *
  1150. * @return an usb configuration object on found or RT_NULL on not found.
  1151. */
  1152. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  1153. {
  1154. struct rt_list_node* node;
  1155. uconfig_t cfg = RT_NULL;
  1156. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  1157. /* parameter check */
  1158. RT_ASSERT(device != RT_NULL);
  1159. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1160. /* search a configration in the the device */
  1161. for (node = device->cfg_list.next; node != &device->cfg_list; node = node->next)
  1162. {
  1163. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  1164. if(cfg->cfg_desc.bConfigurationValue == value)
  1165. {
  1166. return cfg;
  1167. }
  1168. }
  1169. rt_kprintf("can't find configuration %d\n", value);
  1170. return RT_NULL;
  1171. }
  1172. /**
  1173. * This function will find an usb interface object.
  1174. *
  1175. * @param device the usb device object.
  1176. * @param wValue the interface number.
  1177. *
  1178. * @return an usb configuration object on found or RT_NULL on not found.
  1179. */
  1180. uintf_t rt_usbd_find_interface(udevice_t device, rt_uint8_t value, ufunction_t *pfunc)
  1181. {
  1182. struct rt_list_node *i, *j;
  1183. ufunction_t func;
  1184. uintf_t intf;
  1185. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  1186. /* parameter check */
  1187. RT_ASSERT(device != RT_NULL);
  1188. RT_ASSERT(value < device->nr_intf);
  1189. /* search an interface in the current configuration */
  1190. for (i=device->curr_cfg->func_list.next;
  1191. i!=&device->curr_cfg->func_list; i=i->next)
  1192. {
  1193. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1194. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1195. {
  1196. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1197. if(intf->intf_num == value)
  1198. {
  1199. if (pfunc != RT_NULL)
  1200. *pfunc = func;
  1201. return intf;
  1202. }
  1203. }
  1204. }
  1205. rt_kprintf("can't find interface %d\n", value);
  1206. return RT_NULL;
  1207. }
  1208. /**
  1209. * This function will find an usb interface alternate setting object.
  1210. *
  1211. * @param device the usb device object.
  1212. * @param wValue the alternate setting number.
  1213. *
  1214. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  1215. */
  1216. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  1217. {
  1218. struct rt_list_node *i;
  1219. ualtsetting_t setting;
  1220. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  1221. /* parameter check */
  1222. RT_ASSERT(intf != RT_NULL);
  1223. if(intf->curr_setting != RT_NULL)
  1224. {
  1225. /* if the wValue equal to the current alternate setting, then do not search */
  1226. if(intf->curr_setting->intf_desc->bAlternateSetting == value)
  1227. return intf->curr_setting;
  1228. }
  1229. /* search a setting in the alternate setting list */
  1230. for(i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  1231. {
  1232. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  1233. if(setting->intf_desc->bAlternateSetting == value)
  1234. return setting;
  1235. }
  1236. rt_kprintf("can't find alternate setting %d\n", value);
  1237. return RT_NULL;
  1238. }
  1239. /**
  1240. * This function will find an usb endpoint object.
  1241. *
  1242. * @param device the usb device object.
  1243. * @param ep_addr endpoint address.
  1244. *
  1245. * @return an usb endpoint object on found or RT_NULL on not found.
  1246. */
  1247. uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_addr)
  1248. {
  1249. uep_t ep;
  1250. struct rt_list_node *i, *j, *k;
  1251. ufunction_t func;
  1252. uintf_t intf;
  1253. /* parameter check */
  1254. RT_ASSERT(device != RT_NULL);
  1255. /* search a endpoint in the current configuration */
  1256. for (i=device->curr_cfg->func_list.next; i!=&device->curr_cfg->func_list; i=i->next)
  1257. {
  1258. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1259. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1260. {
  1261. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1262. for(k=intf->curr_setting->ep_list.next;
  1263. k!=&intf->curr_setting->ep_list; k=k->next)
  1264. {
  1265. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1266. if(EP_ADDRESS(ep) == ep_addr)
  1267. {
  1268. if (pfunc != RT_NULL)
  1269. *pfunc = func;
  1270. return ep;
  1271. }
  1272. }
  1273. }
  1274. }
  1275. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  1276. return RT_NULL;
  1277. }
  1278. /**
  1279. * This function will add a configuration to an usb device.
  1280. *
  1281. * @param device the usb device object.
  1282. * @param cfg the configuration object.
  1283. *
  1284. * @return RT_EOK.
  1285. */
  1286. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  1287. {
  1288. struct rt_list_node *i, *j, *k;
  1289. ufunction_t func;
  1290. uintf_t intf;
  1291. uep_t ep;
  1292. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  1293. /* parameter check */
  1294. RT_ASSERT(device != RT_NULL);
  1295. RT_ASSERT(cfg != RT_NULL);
  1296. /* set configuration number to the configuration descriptor */
  1297. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  1298. device->dev_desc.bNumConfigurations++;
  1299. for (i=cfg->func_list.next; i!=&cfg->func_list; i=i->next)
  1300. {
  1301. func = (ufunction_t)rt_list_entry(i, struct ufunction, list);
  1302. for(j=func->intf_list.next; j!=&func->intf_list; j=j->next)
  1303. {
  1304. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1305. cfg->cfg_desc.bNumInterfaces++;
  1306. /* allocate address for every endpoint in the interface alternate setting */
  1307. for(k=intf->curr_setting->ep_list.next;
  1308. k!=&intf->curr_setting->ep_list; k=k->next)
  1309. {
  1310. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1311. if(rt_usbd_ep_assign(device, ep) != RT_EOK)
  1312. {
  1313. rt_kprintf("endpoint assign error\n");
  1314. }
  1315. }
  1316. /* construct complete configuration descriptor */
  1317. rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
  1318. (void*)intf->curr_setting->desc,
  1319. intf->curr_setting->desc_size);
  1320. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  1321. }
  1322. }
  1323. /* insert the configuration to the list */
  1324. rt_list_insert_before(&device->cfg_list, &cfg->list);
  1325. return RT_EOK;
  1326. }
  1327. /**
  1328. * This function will add a function to a configuration.
  1329. *
  1330. * @param cfg the configuration object.
  1331. * @param func the function object.
  1332. *
  1333. * @return RT_EOK.
  1334. */
  1335. rt_err_t rt_usbd_config_add_function(uconfig_t cfg, ufunction_t func)
  1336. {
  1337. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_function\n"));
  1338. /* parameter check */
  1339. RT_ASSERT(cfg != RT_NULL);
  1340. RT_ASSERT(func != RT_NULL);
  1341. /* insert the function to the list */
  1342. rt_list_insert_before(&cfg->func_list, &func->list);
  1343. return RT_EOK;
  1344. }
  1345. /**
  1346. * This function will add an interface to a function.
  1347. *
  1348. * @param func the function object.
  1349. * @param intf the interface object.
  1350. *
  1351. * @return RT_EOK.
  1352. */
  1353. rt_err_t rt_usbd_function_add_interface(ufunction_t func, uintf_t intf)
  1354. {
  1355. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_function_add_interface\n"));
  1356. /* parameter check */
  1357. RT_ASSERT(func != RT_NULL);
  1358. RT_ASSERT(intf != RT_NULL);
  1359. /* insert the interface to the list */
  1360. rt_list_insert_before(&func->intf_list, &intf->list);
  1361. return RT_EOK;
  1362. }
  1363. /**
  1364. * This function will add an alternate setting to an interface.
  1365. *
  1366. * @param intf the interface object.
  1367. * @param setting the alternate setting object.
  1368. *
  1369. * @return RT_EOK.
  1370. */
  1371. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1372. {
  1373. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1374. /* parameter check */
  1375. RT_ASSERT(intf != RT_NULL);
  1376. RT_ASSERT(setting != RT_NULL);
  1377. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1378. /* insert the alternate setting to the list */
  1379. rt_list_insert_before(&intf->setting_list, &setting->list);
  1380. return RT_EOK;
  1381. }
  1382. /**
  1383. * This function will add an endpoint to an alternate setting.
  1384. *
  1385. * @param setting the alternate setting object.
  1386. * @param ep the endpoint object.
  1387. *
  1388. * @return RT_EOK.
  1389. */
  1390. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1391. {
  1392. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1393. /* parameter check */
  1394. RT_ASSERT(setting != RT_NULL);
  1395. RT_ASSERT(ep != RT_NULL);
  1396. /* insert the endpoint to the list */
  1397. rt_list_insert_before(&setting->ep_list, &ep->list);
  1398. return RT_EOK;
  1399. }
  1400. 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)
  1401. {
  1402. RT_ASSERT(os_comp_id_desc != RT_NULL);
  1403. RT_ASSERT(os_func_comp_id_desc != RT_NULL);
  1404. rt_list_insert_before(&os_comp_id_desc->func_desc, &os_func_comp_id_desc->list);
  1405. os_comp_id_desc->head_desc.bCount++;
  1406. return RT_EOK;
  1407. }
  1408. /**
  1409. * This function will set an alternate setting for an interface.
  1410. *
  1411. * @param intf_desc the interface descriptor.
  1412. * @param wValue the alternate setting number.
  1413. *
  1414. * @return RT_EOK.
  1415. */
  1416. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1417. {
  1418. ualtsetting_t setting;
  1419. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1420. /* parameter check */
  1421. RT_ASSERT(intf != RT_NULL);
  1422. /* find an alternate setting */
  1423. setting = rt_usbd_find_altsetting(intf, value);
  1424. /* set as current alternate setting */
  1425. intf->curr_setting = setting;
  1426. return RT_EOK;
  1427. }
  1428. /**
  1429. * This function will set a configuration for an usb device.
  1430. *
  1431. * @param device the usb device object.
  1432. * @param wValue the configuration number.
  1433. *
  1434. * @return RT_EOK.
  1435. */
  1436. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1437. {
  1438. uconfig_t cfg;
  1439. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1440. /* parameter check */
  1441. RT_ASSERT(device != RT_NULL);
  1442. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1443. /* find a configuration */
  1444. cfg = rt_usbd_find_config(device, value);
  1445. /* set as current configuration */
  1446. device->curr_cfg = cfg;
  1447. dcd_set_config(device->dcd, value);
  1448. return RT_TRUE;
  1449. }
  1450. /**
  1451. * This function will bRequest an IO transaction.
  1452. *
  1453. * @param device the usb device object.
  1454. * @param ep the endpoint object.
  1455. * @param req IO bRequest.
  1456. *
  1457. * @return RT_EOK.
  1458. */
  1459. rt_size_t rt_usbd_io_request(udevice_t device, uep_t ep, uio_request_t req)
  1460. {
  1461. rt_size_t size = 0;
  1462. RT_ASSERT(device != RT_NULL);
  1463. RT_ASSERT(req != RT_NULL);
  1464. if(ep->stalled == RT_FALSE)
  1465. {
  1466. switch(req->req_type)
  1467. {
  1468. case UIO_REQUEST_READ_BEST:
  1469. case UIO_REQUEST_READ_FULL:
  1470. ep->request.remain_size = ep->request.size;
  1471. size = rt_usbd_ep_read_prepare(device, ep, req->buffer, req->size);
  1472. break;
  1473. case UIO_REQUEST_WRITE:
  1474. ep->request.remain_size = ep->request.size;
  1475. size = rt_usbd_ep_write(device, ep, req->buffer, req->size);
  1476. break;
  1477. default:
  1478. rt_kprintf("unknown request type\n");
  1479. break;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. rt_list_insert_before(&ep->request_list, &req->list);
  1485. RT_DEBUG_LOG(RT_DEBUG_USB, ("suspend a request\n"));
  1486. }
  1487. return size;
  1488. }
  1489. /**
  1490. * This function will set feature for an usb device.
  1491. *
  1492. * @param device the usb device object.
  1493. * @param wValue the configuration number.
  1494. *
  1495. * @return RT_EOK.
  1496. */
  1497. rt_err_t rt_usbd_set_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
  1498. {
  1499. RT_ASSERT(device != RT_NULL);
  1500. if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
  1501. {
  1502. RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature remote wakeup\n"));
  1503. }
  1504. else if (value == USB_FEATURE_ENDPOINT_HALT)
  1505. {
  1506. RT_DEBUG_LOG(RT_DEBUG_USB, ("set feature stall\n"));
  1507. dcd_ep_set_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
  1508. }
  1509. return RT_EOK;
  1510. }
  1511. /**
  1512. * This function will clear feature for an usb device.
  1513. *
  1514. * @param device the usb device object.
  1515. * @param wValue the configuration number.
  1516. *
  1517. * @return RT_EOK.
  1518. */
  1519. rt_err_t rt_usbd_clear_feature(udevice_t device, rt_uint16_t value, rt_uint16_t index)
  1520. {
  1521. RT_ASSERT(device != RT_NULL);
  1522. if (value == USB_FEATURE_DEV_REMOTE_WAKEUP)
  1523. {
  1524. RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature remote wakeup\n"));
  1525. }
  1526. else if (value == USB_FEATURE_ENDPOINT_HALT)
  1527. {
  1528. RT_DEBUG_LOG(RT_DEBUG_USB, ("clear feature stall\n"));
  1529. dcd_ep_clear_stall(device->dcd, (rt_uint32_t)(index & 0xFF));
  1530. }
  1531. return RT_EOK;
  1532. }
  1533. rt_err_t rt_usbd_ep0_set_stall(udevice_t device)
  1534. {
  1535. RT_ASSERT(device != RT_NULL);
  1536. return dcd_ep_set_stall(device->dcd, 0);
  1537. }
  1538. rt_err_t rt_usbd_ep0_clear_stall(udevice_t device)
  1539. {
  1540. RT_ASSERT(device != RT_NULL);
  1541. return dcd_ep_clear_stall(device->dcd, 0);
  1542. }
  1543. rt_err_t rt_usbd_ep_set_stall(udevice_t device, uep_t ep)
  1544. {
  1545. rt_err_t ret;
  1546. RT_ASSERT(device != RT_NULL);
  1547. RT_ASSERT(ep != RT_NULL);
  1548. RT_ASSERT(ep->ep_desc != RT_NULL);
  1549. ret = dcd_ep_set_stall(device->dcd, EP_ADDRESS(ep));
  1550. if(ret == RT_EOK)
  1551. {
  1552. ep->stalled = RT_TRUE;
  1553. }
  1554. return ret;
  1555. }
  1556. rt_err_t rt_usbd_ep_clear_stall(udevice_t device, uep_t ep)
  1557. {
  1558. rt_err_t ret;
  1559. RT_ASSERT(device != RT_NULL);
  1560. RT_ASSERT(ep != RT_NULL);
  1561. RT_ASSERT(ep->ep_desc != RT_NULL);
  1562. ret = dcd_ep_clear_stall(device->dcd, EP_ADDRESS(ep));
  1563. if(ret == RT_EOK)
  1564. {
  1565. ep->stalled = RT_FALSE;
  1566. }
  1567. return ret;
  1568. }
  1569. static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep)
  1570. {
  1571. int i = 0;
  1572. RT_ASSERT(device != RT_NULL);
  1573. RT_ASSERT(device->dcd != RT_NULL);
  1574. RT_ASSERT(device->dcd->ep_pool != RT_NULL);
  1575. RT_ASSERT(ep != RT_NULL);
  1576. RT_ASSERT(ep->ep_desc != RT_NULL);
  1577. while(device->dcd->ep_pool[i].addr != 0xFF)
  1578. {
  1579. if(device->dcd->ep_pool[i].status == ID_UNASSIGNED &&
  1580. ep->ep_desc->bmAttributes == device->dcd->ep_pool[i].type && (EP_ADDRESS(ep) & 0x80) == device->dcd->ep_pool[i].dir)
  1581. {
  1582. EP_ADDRESS(ep) |= device->dcd->ep_pool[i].addr;
  1583. ep->id = &device->dcd->ep_pool[i];
  1584. device->dcd->ep_pool[i].status = ID_ASSIGNED;
  1585. RT_DEBUG_LOG(RT_DEBUG_USB, ("assigned %d\n", device->dcd->ep_pool[i].addr));
  1586. return RT_EOK;
  1587. }
  1588. i++;
  1589. }
  1590. return -RT_ERROR;
  1591. }
  1592. rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep)
  1593. {
  1594. RT_ASSERT(device != RT_NULL);
  1595. RT_ASSERT(device->dcd != RT_NULL);
  1596. RT_ASSERT(device->dcd->ep_pool != RT_NULL);
  1597. RT_ASSERT(ep != RT_NULL);
  1598. RT_ASSERT(ep->ep_desc != RT_NULL);
  1599. ep->id->status = ID_UNASSIGNED;
  1600. return RT_EOK;
  1601. }
  1602. rt_err_t rt_usbd_ep0_setup_handler(udcd_t dcd, struct urequest* setup)
  1603. {
  1604. struct udev_msg msg;
  1605. rt_size_t size;
  1606. RT_ASSERT(dcd != RT_NULL);
  1607. if(setup == RT_NULL)
  1608. {
  1609. size = dcd_ep_read(dcd, EP0_OUT_ADDR, (void*)&msg.content.setup);
  1610. if(size != sizeof(struct urequest))
  1611. {
  1612. rt_kprintf("read setup packet error\n");
  1613. return -RT_ERROR;
  1614. }
  1615. }
  1616. else
  1617. {
  1618. rt_memcpy((void*)&msg.content.setup, (void*)setup, sizeof(struct urequest));
  1619. }
  1620. msg.type = USB_MSG_SETUP_NOTIFY;
  1621. msg.dcd = dcd;
  1622. rt_usbd_event_signal(&msg);
  1623. return RT_EOK;
  1624. }
  1625. rt_err_t rt_usbd_ep0_in_handler(udcd_t dcd)
  1626. {
  1627. rt_int32_t remain, mps;
  1628. RT_ASSERT(dcd != RT_NULL);
  1629. if (dcd->stage != STAGE_DIN)
  1630. return RT_EOK;
  1631. mps = dcd->ep0.id->maxpacket;
  1632. dcd->ep0.request.remain_size -= mps;
  1633. remain = dcd->ep0.request.remain_size;
  1634. if (remain > 0)
  1635. {
  1636. if (remain >= mps)
  1637. {
  1638. remain = mps;
  1639. }
  1640. dcd->ep0.request.buffer += mps;
  1641. dcd_ep_write(dcd, EP0_IN_ADDR, dcd->ep0.request.buffer, remain);
  1642. }
  1643. else
  1644. {
  1645. /* last packet is MPS multiple, so send ZLP packet */
  1646. if ((remain == 0) && (dcd->ep0.request.size > 0))
  1647. {
  1648. dcd->ep0.request.size = 0;
  1649. dcd_ep_write(dcd, EP0_IN_ADDR, RT_NULL, 0);
  1650. }
  1651. else
  1652. {
  1653. /* receive status */
  1654. dcd->stage = STAGE_STATUS_OUT;
  1655. dcd_ep_read_prepare(dcd, EP0_OUT_ADDR, RT_NULL, 0);
  1656. }
  1657. }
  1658. return RT_EOK;
  1659. }
  1660. rt_err_t rt_usbd_ep0_out_handler(udcd_t dcd, rt_size_t size)
  1661. {
  1662. struct udev_msg msg;
  1663. RT_ASSERT(dcd != RT_NULL);
  1664. msg.type = USB_MSG_EP0_OUT;
  1665. msg.dcd = dcd;
  1666. msg.content.ep_msg.size = size;
  1667. rt_usbd_event_signal(&msg);
  1668. return RT_EOK;
  1669. }
  1670. rt_err_t rt_usbd_ep_in_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
  1671. {
  1672. struct udev_msg msg;
  1673. RT_ASSERT(dcd != RT_NULL);
  1674. msg.type = USB_MSG_DATA_NOTIFY;
  1675. msg.dcd = dcd;
  1676. msg.content.ep_msg.ep_addr = address;
  1677. msg.content.ep_msg.size = size;
  1678. rt_usbd_event_signal(&msg);
  1679. return RT_EOK;
  1680. }
  1681. rt_err_t rt_usbd_ep_out_handler(udcd_t dcd, rt_uint8_t address, rt_size_t size)
  1682. {
  1683. struct udev_msg msg;
  1684. RT_ASSERT(dcd != RT_NULL);
  1685. msg.type = USB_MSG_DATA_NOTIFY;
  1686. msg.dcd = dcd;
  1687. msg.content.ep_msg.ep_addr = address;
  1688. msg.content.ep_msg.size = size;
  1689. rt_usbd_event_signal(&msg);
  1690. return RT_EOK;
  1691. }
  1692. rt_err_t rt_usbd_reset_handler(udcd_t dcd)
  1693. {
  1694. struct udev_msg msg;
  1695. RT_ASSERT(dcd != RT_NULL);
  1696. msg.type = USB_MSG_RESET;
  1697. msg.dcd = dcd;
  1698. rt_usbd_event_signal(&msg);
  1699. return RT_EOK;
  1700. }
  1701. rt_err_t rt_usbd_connect_handler(udcd_t dcd)
  1702. {
  1703. struct udev_msg msg;
  1704. RT_ASSERT(dcd != RT_NULL);
  1705. msg.type = USB_MSG_PLUG_IN;
  1706. msg.dcd = dcd;
  1707. rt_usbd_event_signal(&msg);
  1708. return RT_EOK;
  1709. }
  1710. rt_err_t rt_usbd_disconnect_handler(udcd_t dcd)
  1711. {
  1712. struct udev_msg msg;
  1713. RT_ASSERT(dcd != RT_NULL);
  1714. msg.type = USB_MSG_PLUG_OUT;
  1715. msg.dcd = dcd;
  1716. rt_usbd_event_signal(&msg);
  1717. return RT_EOK;
  1718. }
  1719. rt_err_t rt_usbd_sof_handler(udcd_t dcd)
  1720. {
  1721. struct udev_msg msg;
  1722. RT_ASSERT(dcd != RT_NULL);
  1723. msg.type = USB_MSG_SOF;
  1724. msg.dcd = dcd;
  1725. rt_usbd_event_signal(&msg);
  1726. return RT_EOK;
  1727. }
  1728. rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
  1729. {
  1730. uep_t ep0;
  1731. rt_size_t sent_size = 0;
  1732. RT_ASSERT(device != RT_NULL);
  1733. RT_ASSERT(device->dcd != RT_NULL);
  1734. RT_ASSERT(buffer != RT_NULL);
  1735. RT_ASSERT(size > 0);
  1736. ep0 = &device->dcd->ep0;
  1737. ep0->request.size = size;
  1738. ep0->request.buffer = buffer;
  1739. ep0->request.remain_size = size;
  1740. if(size >= ep0->id->maxpacket)
  1741. {
  1742. sent_size = ep0->id->maxpacket;
  1743. }
  1744. else
  1745. {
  1746. sent_size = size;
  1747. }
  1748. device->dcd->stage = STAGE_DIN;
  1749. return dcd_ep_write(device->dcd, EP0_IN_ADDR, ep0->request.buffer, sent_size);
  1750. }
  1751. rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
  1752. rt_err_t (*rx_ind)(udevice_t device, rt_size_t size))
  1753. {
  1754. uep_t ep0;
  1755. rt_size_t read_size = 0;
  1756. RT_ASSERT(device != RT_NULL);
  1757. RT_ASSERT(device->dcd != RT_NULL);
  1758. RT_ASSERT(buffer != RT_NULL);
  1759. ep0 = &device->dcd->ep0;
  1760. ep0->request.buffer = buffer;
  1761. ep0->request.remain_size = size;
  1762. ep0->rx_indicate = rx_ind;
  1763. if(size >= ep0->id->maxpacket)
  1764. {
  1765. read_size = ep0->id->maxpacket;
  1766. }
  1767. else
  1768. {
  1769. read_size = size;
  1770. }
  1771. device->dcd->stage = STAGE_DOUT;
  1772. dcd_ep_read_prepare(device->dcd, EP0_OUT_ADDR, buffer, read_size);
  1773. return size;
  1774. }
  1775. static struct rt_messagequeue usb_mq;
  1776. /**
  1777. * This function is the main entry of usb device thread, it is in charge of
  1778. * processing all messages received from the usb message buffer.
  1779. *
  1780. * @param parameter the parameter of the usb device thread.
  1781. *
  1782. * @return none.
  1783. */
  1784. static void rt_usbd_thread_entry(void* parameter)
  1785. {
  1786. while(1)
  1787. {
  1788. struct udev_msg msg;
  1789. udevice_t device;
  1790. /* receive message */
  1791. if(rt_mq_recv(&usb_mq, &msg, sizeof(struct udev_msg),
  1792. RT_WAITING_FOREVER) != RT_EOK )
  1793. continue;
  1794. device = rt_usbd_find_device(msg.dcd);
  1795. if(device == RT_NULL)
  1796. {
  1797. rt_kprintf("invalid usb device\n");
  1798. continue;
  1799. }
  1800. RT_DEBUG_LOG(RT_DEBUG_USB, ("message type %d\n", msg.type));
  1801. switch (msg.type)
  1802. {
  1803. case USB_MSG_SOF:
  1804. _sof_notify(device);
  1805. break;
  1806. case USB_MSG_DATA_NOTIFY:
  1807. /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
  1808. * got configured. */
  1809. _data_notify(device, &msg.content.ep_msg);
  1810. break;
  1811. case USB_MSG_SETUP_NOTIFY:
  1812. _setup_request(device, &msg.content.setup);
  1813. break;
  1814. case USB_MSG_EP0_OUT:
  1815. _ep0_out_notify(device, &msg.content.ep_msg);
  1816. break;
  1817. case USB_MSG_RESET:
  1818. RT_DEBUG_LOG(RT_DEBUG_USB, ("reset %d\n", device->state));
  1819. if (device->state == USB_STATE_ADDRESS || device->state == USB_STATE_CONFIGURED)
  1820. _stop_notify(device);
  1821. device->state = USB_STATE_NOTATTACHED;
  1822. break;
  1823. case USB_MSG_PLUG_IN:
  1824. device->state = USB_STATE_ATTACHED;
  1825. break;
  1826. case USB_MSG_PLUG_OUT:
  1827. device->state = USB_STATE_NOTATTACHED;
  1828. _stop_notify(device);
  1829. break;
  1830. default:
  1831. rt_kprintf("unknown msg type %d\n", msg.type);
  1832. break;
  1833. }
  1834. }
  1835. }
  1836. /**
  1837. * This function will post an message to usb message queue,
  1838. *
  1839. * @param msg the message to be posted
  1840. * @param size the size of the message .
  1841. *
  1842. * @return the error code, RT_EOK on successfully.
  1843. */
  1844. rt_err_t rt_usbd_event_signal(struct udev_msg* msg)
  1845. {
  1846. RT_ASSERT(msg != RT_NULL);
  1847. /* send message to usb message queue */
  1848. return rt_mq_send(&usb_mq, (void*)msg, sizeof(struct udev_msg));
  1849. }
  1850. ALIGN(RT_ALIGN_SIZE)
  1851. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1852. static struct rt_thread usb_thread;
  1853. #define USBD_MQ_MSG_SZ 32
  1854. #define USBD_MQ_MAX_MSG 16
  1855. /* internal of the message queue: every message is associated with a pointer,
  1856. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1857. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1858. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1859. /**
  1860. * This function will initialize usb device thread.
  1861. *
  1862. * @return none.
  1863. *
  1864. */
  1865. rt_err_t rt_usbd_core_init(void)
  1866. {
  1867. rt_list_init(&device_list);
  1868. /* create an usb message queue */
  1869. rt_mq_init(&usb_mq,
  1870. "usbd",
  1871. usb_mq_pool, USBD_MQ_MSG_SZ,
  1872. sizeof(usb_mq_pool),
  1873. RT_IPC_FLAG_FIFO);
  1874. /* init usb device thread */
  1875. rt_thread_init(&usb_thread,
  1876. "usbd",
  1877. rt_usbd_thread_entry, RT_NULL,
  1878. usb_thread_stack, RT_USBD_THREAD_STACK_SZ,
  1879. RT_USBD_THREAD_PRIO, 20);
  1880. /* rt_thread_init should always be OK, so start the thread without further
  1881. * checking. */
  1882. return rt_thread_startup(&usb_thread);
  1883. }