core.c 58 KB

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