core.c 58 KB

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