core.c 54 KB

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