core.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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 class handler
  24. * 2012-12-30 heyuanjie87 change inferface handler
  25. */
  26. #include <rtthread.h>
  27. #include <rtdevice.h>
  28. static rt_list_t device_list;
  29. /**
  30. * This function will handle get_device_descriptor request.
  31. *
  32. * @param device the usb device object.
  33. * @param setup the setup request.
  34. *
  35. * @return RT_EOK on successful.
  36. */
  37. static rt_err_t _get_device_descriptor(struct udevice *device, ureq_t setup)
  38. {
  39. rt_size_t size;
  40. /* parameter check */
  41. RT_ASSERT(device != RT_NULL);
  42. RT_ASSERT(setup != RT_NULL);
  43. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));
  44. /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
  45. size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
  46. USB_DESC_LENGTH_DEVICE : setup->length;
  47. /* send device descriptor to endpoint 0 */
  48. dcd_ep_write(device->dcd, 0, (rt_uint8_t *)&device->dev_desc, size);
  49. return RT_EOK;
  50. }
  51. /**
  52. * This function will handle get_config_descriptor request.
  53. *
  54. * @param device the usb device object.
  55. * @param setup the setup request.
  56. *
  57. * @return RT_EOK on successful.
  58. */
  59. static rt_err_t _get_config_descriptor(struct udevice *device, ureq_t setup)
  60. {
  61. rt_size_t size;
  62. ucfg_desc_t cfg_desc;
  63. /* parameter check */
  64. RT_ASSERT(device != RT_NULL);
  65. RT_ASSERT(setup != RT_NULL);
  66. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));
  67. cfg_desc = &device->curr_cfg->cfg_desc;
  68. size = (setup->length > cfg_desc->wTotalLength) ?
  69. cfg_desc->wTotalLength : setup->length;
  70. /* send configuration descriptor to endpoint 0 */
  71. dcd_ep_write(device->dcd, 0, (rt_uint8_t *)cfg_desc, size);
  72. return RT_EOK;
  73. }
  74. /**
  75. * This function will handle get_string_descriptor request.
  76. *
  77. * @param device the usb device object.
  78. * @param setup the setup request.
  79. *
  80. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  81. */
  82. static rt_err_t _get_string_descriptor(struct udevice *device, ureq_t setup)
  83. {
  84. struct ustring_descriptor str_desc;
  85. rt_uint8_t index, i;
  86. rt_uint32_t len;
  87. /* parameter check */
  88. RT_ASSERT(device != RT_NULL);
  89. RT_ASSERT(setup != RT_NULL);
  90. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));
  91. str_desc.type = USB_DESC_TYPE_STRING;
  92. index = setup->value & 0xFF;
  93. if (index > USB_STRING_INTERFACE_INDEX)
  94. {
  95. rt_kprintf("unknown string index\n");
  96. dcd_ep_stall(device->dcd, 0);
  97. return -RT_ERROR;
  98. }
  99. if (index == 0)
  100. {
  101. str_desc.bLength = 4;
  102. str_desc.String[0] = 0x09;
  103. str_desc.String[1] = 0x04;
  104. }
  105. else
  106. {
  107. len = rt_strlen(device->str[index]);
  108. str_desc.bLength = len*2 + 2;
  109. for (i=0; i<len; i++)
  110. {
  111. str_desc.String[i*2] = device->str[index][i];
  112. str_desc.String[i*2 + 1] = 0;
  113. }
  114. }
  115. if (setup->length > str_desc.bLength)
  116. len = str_desc.bLength;
  117. else
  118. len = setup->length;
  119. /* send string descriptor to endpoint 0 */
  120. dcd_ep_write(device->dcd, 0, (rt_uint8_t *)&str_desc, len);
  121. return RT_EOK;
  122. }
  123. /**
  124. * This function will handle get_descriptor request.
  125. *
  126. * @param device the usb device object.
  127. * @param setup the setup request.
  128. *
  129. * @return RT_EOK on successful.
  130. */
  131. static rt_err_t _get_descriptor(struct udevice *device, ureq_t setup)
  132. {
  133. /* parameter check */
  134. RT_ASSERT(device != RT_NULL);
  135. RT_ASSERT(setup != RT_NULL);
  136. if (setup->request_type == USB_REQ_TYPE_DIR_IN)
  137. {
  138. switch (setup->value >> 8)
  139. {
  140. case USB_DESC_TYPE_DEVICE:
  141. _get_device_descriptor(device, setup);
  142. break;
  143. case USB_DESC_TYPE_CONFIGURATION:
  144. _get_config_descriptor(device, setup);
  145. break;
  146. case USB_DESC_TYPE_STRING:
  147. _get_string_descriptor(device, setup);
  148. break;
  149. case USB_DESC_TYPE_DEVICEQUALIFIER:
  150. dcd_ep_stall(device->dcd, 0);
  151. break;
  152. default:
  153. rt_kprintf("unsupported descriptor request\n");
  154. dcd_ep_stall(device->dcd, 0);
  155. break;
  156. }
  157. }
  158. else
  159. {
  160. rt_kprintf("request direction error\n");
  161. dcd_ep_stall(device->dcd, 0);
  162. }
  163. return RT_EOK;
  164. }
  165. /**
  166. * This function will handle get_interface request.
  167. *
  168. * @param device the usb device object.
  169. * @param setup the setup request.
  170. *
  171. * @return RT_EOK on successful.
  172. */
  173. static rt_err_t _get_interface(struct udevice *device, ureq_t setup)
  174. {
  175. rt_uint8_t value;
  176. uintf_t intf;
  177. /* parameter check */
  178. RT_ASSERT(device != RT_NULL);
  179. RT_ASSERT(setup != RT_NULL);
  180. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_interface\n"));
  181. if (device->state != USB_STATE_CONFIGURED)
  182. {
  183. dcd_ep_stall(device->dcd, 0);
  184. return -RT_ERROR;
  185. }
  186. /* find the specified interface and its alternate setting */
  187. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  188. value = intf->curr_setting->intf_desc->bAlternateSetting;
  189. /* send the interface alternate setting to endpoint 0*/
  190. dcd_ep_write(device->dcd, 0, &value, 1);
  191. return RT_EOK;
  192. }
  193. /**
  194. * This function will handle set_interface request.
  195. *
  196. * @param device the usb device object.
  197. * @param setup the setup request.
  198. *
  199. * @return RT_EOK on successful.
  200. */
  201. static rt_err_t _set_interface(struct udevice *device, ureq_t setup)
  202. {
  203. uintf_t intf;
  204. uep_t ep;
  205. struct rt_list_node *i;
  206. ualtsetting_t setting;
  207. /* parameter check */
  208. RT_ASSERT(device != RT_NULL);
  209. RT_ASSERT(setup != RT_NULL);
  210. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_interface\n"));
  211. if (device->state != USB_STATE_CONFIGURED)
  212. {
  213. dcd_ep_stall(device->dcd, 0);
  214. return -RT_ERROR;
  215. }
  216. /* find the specified interface */
  217. intf = rt_usbd_find_interface(device, setup->index & 0xFF, RT_NULL);
  218. /* set alternate setting to the interface */
  219. rt_usbd_set_altsetting(intf, setup->value & 0xFF);
  220. setting = intf->curr_setting;
  221. /* start all endpoints of the interface alternate setting */
  222. for (i=setting->ep_list.next; i != &setting->ep_list; i=i->next)
  223. {
  224. ep = (uep_t)rt_list_entry(i, struct uendpoint, list);
  225. dcd_ep_stop(device->dcd, ep);
  226. dcd_ep_run(device->dcd, ep);
  227. }
  228. dcd_send_status(device->dcd);
  229. return RT_EOK;
  230. }
  231. /**
  232. * This function will handle get_config request.
  233. *
  234. * @param device the usb device object.
  235. * @param setup the setup request.
  236. *
  237. * @return RT_EOK on successful.
  238. */
  239. static rt_err_t _get_config(struct udevice *device, ureq_t setup)
  240. {
  241. rt_uint8_t value;
  242. /* parameter check */
  243. RT_ASSERT(device != RT_NULL);
  244. RT_ASSERT(setup != RT_NULL);
  245. RT_ASSERT(device->curr_cfg != RT_NULL);
  246. RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config\n"));
  247. if (device->state == USB_STATE_CONFIGURED)
  248. {
  249. /* get current configuration */
  250. value = device->curr_cfg->cfg_desc.bConfigurationValue;
  251. }
  252. else
  253. {
  254. value = 0;
  255. }
  256. /* write the current configuration to endpoint 0 */
  257. dcd_ep_write(device->dcd, 0, &value, 1);
  258. return RT_EOK;
  259. }
  260. /**
  261. * This function will handle set_config request.
  262. *
  263. * @param device the usb device object.
  264. * @param setup the setup request.
  265. *
  266. * @return RT_EOK on successful.
  267. */
  268. static rt_err_t _set_config(struct udevice *device, ureq_t setup)
  269. {
  270. struct rt_list_node *i, *j, *k;
  271. uconfig_t cfg;
  272. uintf_t intf;
  273. ualtsetting_t setting;
  274. uep_t ep;
  275. /* parameter check */
  276. RT_ASSERT(device != RT_NULL);
  277. RT_ASSERT(setup != RT_NULL);
  278. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_config\n"));
  279. if (setup->value > device->dev_desc.bNumConfigurations)
  280. {
  281. dcd_ep_stall(device->dcd, 0);
  282. return -RT_ERROR;
  283. }
  284. if (setup->value == 0)
  285. {
  286. RT_DEBUG_LOG(RT_DEBUG_USB, ("address state\n"));
  287. device->state = USB_STATE_ADDRESS;
  288. goto _exit;
  289. }
  290. /* set current configuration */
  291. rt_usbd_set_config(device, setup->value);
  292. cfg = device->curr_cfg;
  293. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  294. {
  295. /* run all classes and their endpoints in the configuration */
  296. uclass_t cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  297. for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  298. {
  299. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  300. setting = intf->curr_setting;
  301. for (k=setting->ep_list.next; k != &setting->ep_list; k=k->next)
  302. {
  303. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  304. /* first stop then start endpoint */
  305. dcd_ep_stop(device->dcd, ep);
  306. dcd_ep_run(device->dcd, ep);
  307. }
  308. }
  309. /* after running all endpoints, then run class */
  310. if (cls->ops->run != RT_NULL)
  311. cls->ops->run(device, cls);
  312. }
  313. device->state = USB_STATE_CONFIGURED;
  314. _exit:
  315. /* issue status stage */
  316. dcd_send_status(device->dcd);
  317. return RT_EOK;
  318. }
  319. /**
  320. * This function will handle set_address request.
  321. *
  322. * @param device the usb device object.
  323. * @param setup the setup request.
  324. *
  325. * @return RT_EOK on successful.
  326. */
  327. static rt_err_t _set_address(struct udevice *device, ureq_t setup)
  328. {
  329. /* parameter check */
  330. RT_ASSERT(device != RT_NULL);
  331. RT_ASSERT(setup != RT_NULL);
  332. RT_DEBUG_LOG(RT_DEBUG_USB, ("_set_address\n"));
  333. /* set address in device control driver */
  334. dcd_set_address(device->dcd, setup->value);
  335. device->state = USB_STATE_ADDRESS;
  336. /* issue status stage */
  337. dcd_send_status(device->dcd);
  338. return RT_EOK;
  339. }
  340. /**
  341. * This function will handle standard request to
  342. * interface that defined in class-specifics
  343. *
  344. * @param device the usb device object.
  345. * @param setup the setup request.
  346. *
  347. * @return RT_EOK on successful.
  348. */
  349. static rt_err_t _request_interface(struct udevice *device, ureq_t setup)
  350. {
  351. uintf_t intf;
  352. uclass_t cls;
  353. rt_err_t ret;
  354. /* parameter check */
  355. RT_ASSERT(device != RT_NULL);
  356. RT_ASSERT(setup != RT_NULL);
  357. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_interface\n"));
  358. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  359. if (intf != RT_NULL)
  360. {
  361. ret = intf->handler(device, cls, setup);
  362. }
  363. else
  364. {
  365. ret = -RT_ERROR;
  366. }
  367. return ret;
  368. }
  369. /**
  370. * This function will handle standard request.
  371. *
  372. * @param device the usb device object.
  373. * @param setup the setup request.
  374. *
  375. * @return RT_EOK on successful.
  376. */
  377. static rt_err_t _standard_request(struct udevice *device, ureq_t setup)
  378. {
  379. udcd_t dcd;
  380. rt_uint16_t value = 0;
  381. /* parameter check */
  382. RT_ASSERT(device != RT_NULL);
  383. RT_ASSERT(setup != RT_NULL);
  384. dcd = device->dcd;
  385. switch (setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  386. {
  387. case USB_REQ_TYPE_DEVICE:
  388. switch (setup->request)
  389. {
  390. case USB_REQ_GET_STATUS:
  391. dcd_ep_write(device->dcd, 0, &value, 2);
  392. break;
  393. case USB_REQ_CLEAR_FEATURE:
  394. dcd_clear_feature(dcd, setup->value, setup->index);
  395. dcd_send_status(dcd);
  396. break;
  397. case USB_REQ_SET_FEATURE:
  398. dcd_set_feature(dcd, setup->value, setup->index);
  399. break;
  400. case USB_REQ_SET_ADDRESS:
  401. _set_address(device, setup);
  402. break;
  403. case USB_REQ_GET_DESCRIPTOR:
  404. _get_descriptor(device, setup);
  405. break;
  406. case USB_REQ_SET_DESCRIPTOR:
  407. dcd_ep_stall(dcd, 0);
  408. break;
  409. case USB_REQ_GET_CONFIGURATION:
  410. _get_config(device, setup);
  411. break;
  412. case USB_REQ_SET_CONFIGURATION:
  413. _set_config(device, setup);
  414. break;
  415. default:
  416. rt_kprintf("unknown device request\n");
  417. dcd_ep_stall(device->dcd, 0);
  418. break;
  419. }
  420. break;
  421. case USB_REQ_TYPE_INTERFACE:
  422. switch (setup->request)
  423. {
  424. case USB_REQ_GET_INTERFACE:
  425. _get_interface(device, setup);
  426. break;
  427. case USB_REQ_SET_INTERFACE:
  428. _set_interface(device, setup);
  429. break;
  430. default:
  431. if (_request_interface(device, setup) != RT_EOK)
  432. {
  433. rt_kprintf("unknown interface request\n");
  434. dcd_ep_stall(device->dcd, 0);
  435. return - RT_ERROR;
  436. }
  437. else
  438. break;
  439. }
  440. break;
  441. case USB_REQ_TYPE_ENDPOINT:
  442. switch (setup->request)
  443. {
  444. case USB_REQ_GET_STATUS:
  445. {
  446. /* TODO */
  447. uep_t ep;
  448. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  449. value = ep->is_stall;
  450. dcd_ep_write(dcd, 0, &value, 2);
  451. }
  452. break;
  453. case USB_REQ_CLEAR_FEATURE:
  454. {
  455. uep_t ep;
  456. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  457. ep->is_stall = 0;
  458. dcd_clear_feature(dcd, setup->value, setup->index);
  459. dcd_send_status(dcd);
  460. }
  461. break;
  462. case USB_REQ_SET_FEATURE:
  463. {
  464. uep_t ep;
  465. ep = rt_usbd_find_endpoint(device, RT_NULL, setup->index);
  466. ep->is_stall = 1;
  467. dcd_set_feature(dcd, setup->value, setup->index);
  468. dcd_send_status(dcd);
  469. }
  470. break;
  471. case USB_REQ_SYNCH_FRAME:
  472. break;
  473. default:
  474. rt_kprintf("unknown endpoint request\n");
  475. dcd_ep_stall(device->dcd, 0);
  476. break;
  477. }
  478. break;
  479. case USB_REQ_TYPE_OTHER:
  480. rt_kprintf("unknown other type request\n");
  481. dcd_ep_stall(device->dcd, 0);
  482. break;
  483. default:
  484. rt_kprintf("unknown type request\n");
  485. dcd_ep_stall(device->dcd, 0);
  486. break;
  487. }
  488. return RT_EOK;
  489. }
  490. /**
  491. * This function will handle class request.
  492. *
  493. * @param device the usb device object.
  494. * @param setup the setup request.
  495. *
  496. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  497. */
  498. static rt_err_t _class_request(udevice_t device, ureq_t setup)
  499. {
  500. uintf_t intf;
  501. uclass_t cls;
  502. /* parameter check */
  503. RT_ASSERT(device != RT_NULL);
  504. RT_ASSERT(setup != RT_NULL);
  505. /* verify request value */
  506. if (setup->index > device->curr_cfg->cfg_desc.bNumInterfaces)
  507. {
  508. dcd_ep_stall(device->dcd, 0);
  509. return -RT_ERROR;
  510. }
  511. switch (setup->request_type & USB_REQ_TYPE_RECIPIENT_MASK)
  512. {
  513. case USB_REQ_TYPE_INTERFACE:
  514. intf = rt_usbd_find_interface(device, setup->index & 0xFF, &cls);
  515. intf->handler(device, cls, setup);
  516. break;
  517. case USB_REQ_TYPE_ENDPOINT:
  518. break;
  519. default:
  520. rt_kprintf("unknown class request type\n");
  521. dcd_ep_stall(device->dcd, 0);
  522. break;
  523. }
  524. return RT_EOK;
  525. }
  526. /**
  527. * This function will handle setup request.
  528. *
  529. * @param device the usb device object.
  530. * @param setup the setup request.
  531. *
  532. * @return RT_EOK on successful, -RT_ERROR on invalid request.
  533. */
  534. static rt_err_t _setup_request(udevice_t device, ureq_t setup)
  535. {
  536. /* parameter check */
  537. RT_ASSERT(device != RT_NULL);
  538. RT_ASSERT(setup != RT_NULL);
  539. RT_DEBUG_LOG(RT_DEBUG_USB, ("[\n"));
  540. RT_DEBUG_LOG(RT_DEBUG_USB, ("setup_request_handler 0x%x\n",
  541. setup->request_type));
  542. RT_DEBUG_LOG(RT_DEBUG_USB, ("value 0x%x\n", setup->value));
  543. RT_DEBUG_LOG(RT_DEBUG_USB, ("length 0x%x\n", setup->length));
  544. RT_DEBUG_LOG(RT_DEBUG_USB, ("index 0x%x\n", setup->index));
  545. RT_DEBUG_LOG(RT_DEBUG_USB, ("request 0x%x\n", setup->request));
  546. RT_DEBUG_LOG(RT_DEBUG_USB, ("]\n"));
  547. switch ((setup->request_type & USB_REQ_TYPE_MASK))
  548. {
  549. case USB_REQ_TYPE_STANDARD:
  550. _standard_request(device, setup);
  551. break;
  552. case USB_REQ_TYPE_CLASS:
  553. _class_request(device, setup);
  554. break;
  555. case USB_REQ_TYPE_VENDOR:
  556. rt_kprintf("vendor type request\n");
  557. break;
  558. default:
  559. rt_kprintf("unknown setup request type\n");
  560. dcd_ep_stall(device->dcd, 0);
  561. return -RT_ERROR;
  562. }
  563. return RT_EOK;
  564. }
  565. /**
  566. * This function will notity sof event to all of class.
  567. *
  568. * @param device the usb device object.
  569. *
  570. * @return RT_EOK.
  571. */
  572. rt_err_t _sof_notify(udevice_t device)
  573. {
  574. struct rt_list_node *i;
  575. uclass_t cls;
  576. RT_ASSERT(device != RT_NULL);
  577. /* to notity every class that sof event comes */
  578. for (i = device->curr_cfg->cls_list.next;
  579. i != &device->curr_cfg->cls_list;
  580. i = i->next)
  581. {
  582. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  583. if (cls->ops->sof_handler != RT_NULL)
  584. cls->ops->sof_handler(device, cls);
  585. }
  586. return RT_EOK;
  587. }
  588. /**
  589. * This function will stop all class.
  590. *
  591. * @param device the usb device object.
  592. *
  593. * @return RT_EOK.
  594. */
  595. rt_err_t _stop_notify(udevice_t device)
  596. {
  597. struct rt_list_node *i;
  598. uclass_t cls;
  599. RT_ASSERT(device != RT_NULL);
  600. /* to notity every class that sof event comes */
  601. for (i = device->curr_cfg->cls_list.next;
  602. i != &device->curr_cfg->cls_list;
  603. i = i->next)
  604. {
  605. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  606. if (cls->ops->stop != RT_NULL)
  607. cls->ops->stop(device, cls);
  608. }
  609. return RT_EOK;
  610. }
  611. /**
  612. * This function will run all class.
  613. *
  614. * @param device the usb device object.
  615. *
  616. * @return RT_EOK.
  617. */
  618. rt_err_t _run_notify(udevice_t device)
  619. {
  620. struct rt_list_node *i;
  621. uclass_t cls;
  622. RT_ASSERT(device != RT_NULL);
  623. /* to notity every class that sof event comes */
  624. for (i = device->curr_cfg->cls_list.next;
  625. i != &device->curr_cfg->cls_list;
  626. i = i->next)
  627. {
  628. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  629. if (cls->ops->run != RT_NULL)
  630. cls->ops->run(device, cls);
  631. }
  632. return RT_EOK;
  633. }
  634. /**
  635. * This function will reset all class.
  636. *
  637. * @param device the usb device object.
  638. *
  639. * @return RT_EOK.
  640. */
  641. rt_err_t _reset_notify(udevice_t device)
  642. {
  643. struct rt_list_node *i;
  644. uclass_t cls;
  645. RT_ASSERT(device != RT_NULL);
  646. _stop_notify(device);
  647. _run_notify(device);
  648. return RT_EOK;
  649. }
  650. /**
  651. * This function will create an usb device object.
  652. *
  653. * @param ustring the usb string array to contain string descriptor.
  654. *
  655. * @return an usb device object on success, RT_NULL on fail.
  656. */
  657. udevice_t rt_usbd_device_create(void)
  658. {
  659. udevice_t udevice;
  660. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_create\n"));
  661. /* allocate memory for the object */
  662. udevice = rt_malloc(sizeof(struct udevice));
  663. if (udevice == RT_NULL)
  664. {
  665. rt_kprintf("alloc memery failed\n");
  666. return RT_NULL;
  667. }
  668. rt_memset(udevice, 0, sizeof(struct udevice));
  669. /* to initialize configuration list */
  670. rt_list_init(&udevice->cfg_list);
  671. /* insert the device object to device list */
  672. rt_list_insert_after(&device_list, &udevice->list);
  673. return udevice;
  674. }
  675. /**
  676. * This function will set usb device string description.
  677. *
  678. * @param device the usb device object.
  679. * @param ustring pointer to string pointer array.
  680. *
  681. * @return RT_EOK.
  682. */
  683. rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring)
  684. {
  685. /* parameter check */
  686. RT_ASSERT(device != RT_NULL);
  687. RT_ASSERT(ustring != RT_NULL);
  688. /* set string descriptor array to the device object */
  689. device->str = ustring;
  690. return RT_EOK;
  691. }
  692. /**
  693. * This function will set an usb controller driver to a device.
  694. *
  695. * @param device the usb device object.
  696. * @param dcd the usb device controller driver.
  697. *
  698. * @return RT_EOK on successful.
  699. */
  700. rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd)
  701. {
  702. /* parameter check */
  703. RT_ASSERT(device != RT_NULL);
  704. RT_ASSERT(dcd != RT_NULL);
  705. /* set usb device controller driver to the device */
  706. device->dcd = dcd;
  707. return RT_EOK;
  708. }
  709. /**
  710. * This function will set an usb device descriptor to a device.
  711. *
  712. * @param device the usb device object.
  713. * @param dev_desc the usb device descriptor.
  714. *
  715. * @return RT_EOK on successful.
  716. */
  717. rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc)
  718. {
  719. /* parameter check */
  720. RT_ASSERT(device != RT_NULL);
  721. RT_ASSERT(dev_desc != RT_NULL);
  722. /* copy the usb device descriptor to the device */
  723. rt_memcpy((void *)&device->dev_desc, (void *)dev_desc, USB_DESC_LENGTH_DEVICE);
  724. return RT_EOK;
  725. }
  726. /**
  727. * This function will create an usb configuration object.
  728. *
  729. * @param none.
  730. *
  731. * @return an usb configuration object.
  732. */
  733. uconfig_t rt_usbd_config_create(void)
  734. {
  735. uconfig_t cfg;
  736. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_create\n"));
  737. /* allocate memory for the object */
  738. cfg = rt_malloc(sizeof(struct uconfig));
  739. if (cfg == RT_NULL)
  740. {
  741. rt_kprintf("alloc memery failed\n");
  742. return RT_NULL;
  743. }
  744. rt_memset(cfg, 0, sizeof(struct uconfig));
  745. /* set default value */
  746. cfg->cfg_desc.bLength = USB_DESC_LENGTH_CONFIG;
  747. cfg->cfg_desc.type = USB_DESC_TYPE_CONFIGURATION;
  748. cfg->cfg_desc.wTotalLength = USB_DESC_LENGTH_CONFIG;
  749. cfg->cfg_desc.bmAttributes = 0xC0;
  750. cfg->cfg_desc.MaxPower = 0x32;
  751. /* to initialize class object list */
  752. rt_list_init(&cfg->cls_list);
  753. return cfg;
  754. }
  755. /**
  756. * This function will create an usb interface object.
  757. *
  758. * @param device the usb device object.
  759. * @handler the callback handler of object
  760. *
  761. * @return an usb interface object on success, RT_NULL on fail.
  762. */
  763. uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler)
  764. {
  765. uintf_t intf;
  766. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_create\n"));
  767. /* parameter check */
  768. RT_ASSERT(device != RT_NULL);
  769. /* allocate memory for the object */
  770. intf = (uintf_t)rt_malloc(sizeof(struct uinterface));
  771. if (intf == RT_NULL)
  772. {
  773. rt_kprintf("alloc memery failed\n");
  774. return RT_NULL;
  775. }
  776. intf->intf_num = device->nr_intf;
  777. device->nr_intf++;
  778. intf->handler = handler;
  779. intf->curr_setting = RT_NULL;
  780. /* to initialize the alternate setting object list */
  781. rt_list_init(&intf->setting_list);
  782. return intf;
  783. }
  784. /**
  785. * This function will create an usb alternate setting object.
  786. *
  787. * @param intf_desc the interface descriptor.
  788. * @desc_size the size of the interface descriptor.
  789. *
  790. * @return an usb alternate setting object on success, RT_NULL on fail.
  791. */
  792. ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size)
  793. {
  794. ualtsetting_t setting;
  795. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_create\n"));
  796. /* parameter check */
  797. RT_ASSERT(desc_size > 0);
  798. /* allocate memory for the object */
  799. setting = (ualtsetting_t)rt_malloc(sizeof(struct ualtsetting));
  800. if (setting == RT_NULL)
  801. {
  802. rt_kprintf("alloc memery failed\n");
  803. return RT_NULL;
  804. }
  805. /* allocate memory for the desc */
  806. setting->desc = rt_malloc(desc_size);
  807. if (setting->desc == RT_NULL)
  808. {
  809. rt_kprintf("alloc desc memery failed\n");
  810. rt_free(setting);
  811. return RT_NULL;
  812. }
  813. setting->desc_size = desc_size;
  814. setting->intf_desc = RT_NULL;
  815. /* to initialize endpoint list */
  816. rt_list_init(&setting->ep_list);
  817. return setting;
  818. }
  819. /**
  820. * This function will config an desc in alternate setting object.
  821. *
  822. * @param setting the altsetting to be config.
  823. * @param desc use it to init desc in setting.
  824. * @param intf_pos the offset of interface descriptor in desc.
  825. *
  826. * @return RT_EOK.
  827. */
  828. rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting,
  829. const void *desc,
  830. rt_off_t intf_pos)
  831. {
  832. RT_ASSERT(setting != RT_NULL);
  833. RT_ASSERT(setting->desc !=RT_NULL);
  834. rt_memcpy(setting->desc, desc, setting->desc_size);
  835. setting->intf_desc = (uintf_desc_t)((char *)setting->desc + intf_pos);
  836. return RT_EOK;
  837. }
  838. /**
  839. * This function will create an usb class object.
  840. *
  841. * @param device the usb device object.
  842. * @param dev_desc the device descriptor.
  843. * @param ops the operation set.
  844. *
  845. * @return an usb class object on success, RT_NULL on fail.
  846. */
  847. uclass_t rt_usbd_class_create(udevice_t device,
  848. udev_desc_t dev_desc,
  849. uclass_ops_t ops)
  850. {
  851. uclass_t cls;
  852. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_create\n"));
  853. /* parameter check */
  854. RT_ASSERT(device != RT_NULL);
  855. RT_ASSERT(dev_desc != RT_NULL);
  856. /* allocate memory for the object */
  857. cls = (uclass_t)rt_malloc(sizeof(struct uclass));
  858. if (cls == RT_NULL)
  859. {
  860. rt_kprintf("alloc memery failed\n");
  861. return RT_NULL;
  862. }
  863. cls->dev_desc = dev_desc;
  864. cls->ops = ops;
  865. cls->device = device;
  866. /* to initialize interface list */
  867. rt_list_init(&cls->intf_list);
  868. return cls;
  869. }
  870. /**
  871. * This function will create an usb endpoint object.
  872. *
  873. * @param ep_desc the endpoint descriptor.
  874. * @handler the callback handler of object
  875. *
  876. * @return an usb endpoint object on success, RT_NULL on fail.
  877. */
  878. uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler)
  879. {
  880. uep_t ep;
  881. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_endpoint_create\n"));
  882. /* parameter check */
  883. RT_ASSERT(ep_desc != RT_NULL);
  884. /* allocate memory for the object */
  885. ep = (uep_t)rt_malloc(sizeof(struct uendpoint));
  886. if (ep == RT_NULL)
  887. {
  888. rt_kprintf("alloc memery failed\n");
  889. return RT_NULL;
  890. }
  891. ep->ep_desc = ep_desc;
  892. ep->handler = handler;
  893. ep->buffer = RT_NULL;
  894. return ep;
  895. }
  896. /**
  897. * This function will find an usb device object.
  898. *
  899. * @dcd usd device controller driver.
  900. *
  901. * @return an usb device object on found or RT_NULL on not found.
  902. */
  903. udevice_t rt_usbd_find_device(udcd_t dcd)
  904. {
  905. struct rt_list_node *node;
  906. udevice_t device;
  907. /* parameter check */
  908. RT_ASSERT(dcd != RT_NULL);
  909. /* search a device in the the device list */
  910. for (node = device_list.next; node != &device_list; node = node->next)
  911. {
  912. device = (udevice_t)rt_list_entry(node, struct udevice, list);
  913. if (device->dcd == dcd)
  914. return device;
  915. }
  916. rt_kprintf("can't find device\n");
  917. return RT_NULL;
  918. }
  919. /**
  920. * This function will find an usb configuration object.
  921. *
  922. * @param device the usb device object.
  923. * @param value the configuration number.
  924. *
  925. * @return an usb configuration object on found or RT_NULL on not found.
  926. */
  927. uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value)
  928. {
  929. struct rt_list_node *node;
  930. uconfig_t cfg = RT_NULL;
  931. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_config\n"));
  932. /* parameter check */
  933. RT_ASSERT(device != RT_NULL);
  934. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  935. /* search a configration in the the device */
  936. for (node = device->cfg_list.next;
  937. node != &device->cfg_list;
  938. node = node->next)
  939. {
  940. cfg = (uconfig_t)rt_list_entry(node, struct udevice, list);
  941. if (cfg->cfg_desc.bConfigurationValue == value)
  942. return cfg;
  943. }
  944. rt_kprintf("can't find configuration %d\n", value);
  945. return RT_NULL;
  946. }
  947. /**
  948. * This function will find an usb interface object.
  949. *
  950. * @param device the usb device object.
  951. * @param value the interface number.
  952. *
  953. * @return an usb configuration object on found or RT_NULL on not found.
  954. */
  955. uintf_t rt_usbd_find_interface(udevice_t device,
  956. rt_uint8_t value,
  957. uclass_t *pcls)
  958. {
  959. struct rt_list_node *i, *j;
  960. uclass_t cls;
  961. uintf_t intf;
  962. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_interface\n"));
  963. /* parameter check */
  964. RT_ASSERT(device != RT_NULL);
  965. RT_ASSERT(value < device->nr_intf);
  966. /* search an interface in the current configuration */
  967. for (i = device->curr_cfg->cls_list.next;
  968. i != &device->curr_cfg->cls_list;
  969. i = i->next)
  970. {
  971. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  972. for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  973. {
  974. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  975. if (intf->intf_num == value)
  976. {
  977. if (pcls != RT_NULL)
  978. *pcls = cls;
  979. return intf;
  980. }
  981. }
  982. }
  983. rt_kprintf("can't find interface %d\n", value);
  984. return RT_NULL;
  985. }
  986. /**
  987. * This function will find an usb interface alternate setting object.
  988. *
  989. * @param device the usb device object.
  990. * @param value the alternate setting number.
  991. *
  992. * @return an usb interface alternate setting object on found or RT_NULL on not found.
  993. */
  994. ualtsetting_t rt_usbd_find_altsetting(uintf_t intf, rt_uint8_t value)
  995. {
  996. struct rt_list_node *i;
  997. ualtsetting_t setting;
  998. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_find_altsetting\n"));
  999. /* parameter check */
  1000. RT_ASSERT(intf != RT_NULL);
  1001. if (intf->curr_setting != RT_NULL)
  1002. {
  1003. /* if the value equal to the current alternate setting, then do not search */
  1004. if (intf->curr_setting->intf_desc->bAlternateSetting == value)
  1005. return intf->curr_setting;
  1006. }
  1007. /* search a setting in the alternate setting list */
  1008. for (i=intf->setting_list.next; i!=&intf->setting_list; i=i->next)
  1009. {
  1010. setting =(ualtsetting_t)rt_list_entry(i, struct ualtsetting, list);
  1011. if (setting->intf_desc->bAlternateSetting == value)
  1012. return setting;
  1013. }
  1014. rt_kprintf("can't find alternate setting %d\n", value);
  1015. return RT_NULL;
  1016. }
  1017. /**
  1018. * This function will find an usb endpoint object.
  1019. *
  1020. * @param device the usb device object.
  1021. * @param ep_addr endpoint address.
  1022. *
  1023. * @return an usb endpoint object on found or RT_NULL on not found.
  1024. */
  1025. uep_t rt_usbd_find_endpoint(udevice_t device,
  1026. uclass_t *pcls,
  1027. rt_uint8_t ep_addr)
  1028. {
  1029. uep_t ep;
  1030. struct rt_list_node *i, *j, *k;
  1031. uclass_t cls;
  1032. uintf_t intf;
  1033. /* parameter check */
  1034. RT_ASSERT(device != RT_NULL);
  1035. /* search a endpoint in the current configuration */
  1036. for (i = device->curr_cfg->cls_list.next;
  1037. i != &device->curr_cfg->cls_list;
  1038. i = i->next)
  1039. {
  1040. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  1041. for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  1042. {
  1043. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1044. for (k = intf->curr_setting->ep_list.next;
  1045. k != &intf->curr_setting->ep_list;
  1046. k = k->next)
  1047. {
  1048. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1049. if (ep->ep_desc->bEndpointAddress == ep_addr)
  1050. {
  1051. if (pcls != RT_NULL)
  1052. *pcls = cls;
  1053. return ep;
  1054. }
  1055. }
  1056. }
  1057. }
  1058. rt_kprintf("can't find endpoint 0x%x\n", ep_addr);
  1059. return RT_NULL;
  1060. }
  1061. /**
  1062. * This function will add a configuration to an usb device.
  1063. *
  1064. * @param device the usb device object.
  1065. * @param cfg the configuration object.
  1066. *
  1067. * @return RT_EOK.
  1068. */
  1069. rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
  1070. {
  1071. struct rt_list_node *i, *j, *k;
  1072. uclass_t cls;
  1073. uintf_t intf;
  1074. uep_t ep;
  1075. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
  1076. /* parameter check */
  1077. RT_ASSERT(device != RT_NULL);
  1078. RT_ASSERT(cfg != RT_NULL);
  1079. /* set configuration number to the configuration descriptor */
  1080. cfg->cfg_desc.bConfigurationValue = device->dev_desc.bNumConfigurations + 1;
  1081. device->dev_desc.bNumConfigurations++;
  1082. for (i=cfg->cls_list.next; i!=&cfg->cls_list; i=i->next)
  1083. {
  1084. cls = (uclass_t)rt_list_entry(i, struct uclass, list);
  1085. for (j=cls->intf_list.next; j!=&cls->intf_list; j=j->next)
  1086. {
  1087. intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
  1088. cfg->cfg_desc.bNumInterfaces++;
  1089. /* allocate address for every endpoint in the interface alternate setting */
  1090. for (k = intf->curr_setting->ep_list.next;
  1091. k != &intf->curr_setting->ep_list;
  1092. k = k->next)
  1093. {
  1094. ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
  1095. dcd_ep_alloc(device->dcd, ep);
  1096. }
  1097. /* construct complete configuration descriptor */
  1098. rt_memcpy((void *)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
  1099. (void *)intf->curr_setting->desc,
  1100. intf->curr_setting->desc_size);
  1101. cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
  1102. }
  1103. }
  1104. /* insert the configuration to the list */
  1105. rt_list_insert_after(&device->cfg_list, &cfg->list);
  1106. return RT_EOK;
  1107. }
  1108. /**
  1109. * This function will add a class to a configuration.
  1110. *
  1111. * @param cfg the configuration object.
  1112. * @param cls the class object.
  1113. *
  1114. * @return RT_EOK.
  1115. */
  1116. rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls)
  1117. {
  1118. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_add_class\n"));
  1119. /* parameter check */
  1120. RT_ASSERT(cfg != RT_NULL);
  1121. RT_ASSERT(cls != RT_NULL);
  1122. /* insert the class to the list */
  1123. rt_list_insert_after(&cfg->cls_list, &cls->list);
  1124. return RT_EOK;
  1125. }
  1126. /**
  1127. * This function will add an interface to a class.
  1128. *
  1129. * @param cls the class object.
  1130. * @param intf the interface object.
  1131. *
  1132. * @return RT_EOK.
  1133. */
  1134. rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf)
  1135. {
  1136. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_class_add_interface\n"));
  1137. /* parameter check */
  1138. RT_ASSERT(cls != RT_NULL);
  1139. RT_ASSERT(intf != RT_NULL);
  1140. /* insert the interface to the list */
  1141. rt_list_insert_after(&cls->intf_list, &intf->list);
  1142. return RT_EOK;
  1143. }
  1144. /**
  1145. * This function will add an alternate setting to an interface.
  1146. *
  1147. * @param intf the interface object.
  1148. * @param setting the alternate setting object.
  1149. *
  1150. * @return RT_EOK.
  1151. */
  1152. rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting)
  1153. {
  1154. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_interface_add_altsetting\n"));
  1155. /* parameter check */
  1156. RT_ASSERT(intf != RT_NULL);
  1157. RT_ASSERT(setting != RT_NULL);
  1158. setting->intf_desc->bInterfaceNumber = intf->intf_num;
  1159. /* insert the alternate setting to the list */
  1160. rt_list_insert_after(&intf->setting_list, &setting->list);
  1161. return RT_EOK;
  1162. }
  1163. /**
  1164. * This function will add an endpoint to an alternate setting.
  1165. *
  1166. * @param setting the alternate setting object.
  1167. * @param ep the endpoint object.
  1168. *
  1169. * @return RT_EOK.
  1170. */
  1171. rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep)
  1172. {
  1173. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_altsetting_add_endpoint\n"));
  1174. /* parameter check */
  1175. RT_ASSERT(setting != RT_NULL);
  1176. RT_ASSERT(ep != RT_NULL);
  1177. /* insert the endpoint to the list */
  1178. rt_list_insert_after(&setting->ep_list, &ep->list);
  1179. return RT_EOK;
  1180. }
  1181. /**
  1182. * This function will set an alternate setting for an interface.
  1183. *
  1184. * @param intf_desc the interface descriptor.
  1185. * @param value the alternate setting number.
  1186. *
  1187. * @return RT_EOK.
  1188. */
  1189. rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value)
  1190. {
  1191. ualtsetting_t setting;
  1192. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_altsetting\n"));
  1193. /* parameter check */
  1194. RT_ASSERT(intf != RT_NULL);
  1195. /* find an alternate setting */
  1196. setting = rt_usbd_find_altsetting(intf, value);
  1197. /* set as current alternate setting */
  1198. intf->curr_setting = setting;
  1199. return RT_EOK;
  1200. }
  1201. /**
  1202. * This function will set a configuration for an usb device.
  1203. *
  1204. * @param device the usb device object.
  1205. * @param value the configuration number.
  1206. *
  1207. * @return RT_EOK.
  1208. */
  1209. rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value)
  1210. {
  1211. uconfig_t cfg;
  1212. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_set_config\n"));
  1213. /* parameter check */
  1214. RT_ASSERT(device != RT_NULL);
  1215. RT_ASSERT(value <= device->dev_desc.bNumConfigurations);
  1216. /* find a configuration */
  1217. cfg = rt_usbd_find_config(device, value);
  1218. /* set as current configuration */
  1219. device->curr_cfg = cfg;
  1220. return RT_TRUE;
  1221. }
  1222. static struct rt_messagequeue usb_mq;
  1223. /**
  1224. * This function is the main entry of usb device thread, it is in charge of
  1225. * processing all messages received from the usb message buffer.
  1226. *
  1227. * @param parameter the parameter of the usb device thread.
  1228. *
  1229. * @return none.
  1230. */
  1231. static void rt_usbd_thread_entry(void *parameter)
  1232. {
  1233. while (1)
  1234. {
  1235. struct udev_msg msg;
  1236. udevice_t device;
  1237. uclass_t cls;
  1238. uep_t ep;
  1239. /* receive message */
  1240. if (rt_mq_recv(&usb_mq, &msg, sizeof(struct udev_msg), RT_WAITING_FOREVER) != RT_EOK)
  1241. continue;
  1242. device = rt_usbd_find_device(msg.dcd);
  1243. if (device == RT_NULL)
  1244. {
  1245. rt_kprintf("invalid usb device\n");
  1246. continue;
  1247. }
  1248. switch (msg.type)
  1249. {
  1250. case USB_MSG_SOF:
  1251. _sof_notify(device);
  1252. break;
  1253. case USB_MSG_DATA_NOTIFY:
  1254. /* some buggy drivers will have USB_MSG_DATA_NOTIFY before the core
  1255. * got configured. */
  1256. if (device->state != USB_STATE_CONFIGURED)
  1257. break;
  1258. ep = rt_usbd_find_endpoint(device, &cls, msg.content.ep_msg.ep_addr);
  1259. if (ep != RT_NULL)
  1260. ep->handler(device, cls, msg.content.ep_msg.size);
  1261. else
  1262. rt_kprintf("invalid endpoint\n");
  1263. break;
  1264. case USB_MSG_SETUP_NOTIFY:
  1265. _setup_request(device, (ureq_t)msg.content.setup_msg.packet);
  1266. break;
  1267. case USB_MSG_RESET:
  1268. if (device->state == USB_STATE_ADDRESS)
  1269. _reset_notify(device);
  1270. break;
  1271. case USB_MSG_PLUG_OUT:
  1272. _stop_notify(device);
  1273. break;
  1274. default:
  1275. rt_kprintf("unknown msg type\n");
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. /**
  1281. * This function will post an message to usb message queue,
  1282. *
  1283. * @param msg the message to be posted
  1284. * @param size the size of the message .
  1285. *
  1286. * @return the error code, RT_EOK on successfully.
  1287. */
  1288. rt_err_t rt_usbd_post_event(struct udev_msg *msg, rt_size_t size)
  1289. {
  1290. RT_ASSERT(msg != RT_NULL);
  1291. /* send message to usb message queue */
  1292. return rt_mq_send(&usb_mq, (void *)msg, size);
  1293. }
  1294. ALIGN(RT_ALIGN_SIZE)
  1295. static rt_uint8_t usb_thread_stack[RT_USBD_THREAD_STACK_SZ];
  1296. static struct rt_thread usb_thread;
  1297. #define USBD_MQ_MSG_SZ 32
  1298. #define USBD_MQ_MAX_MSG 16
  1299. /* internal of the message queue: every message is associated with a pointer,
  1300. * so in order to recveive USBD_MQ_MAX_MSG messages, we have to allocate more
  1301. * than USBD_MQ_MSG_SZ*USBD_MQ_MAX_MSG memery. */
  1302. static rt_uint8_t usb_mq_pool[(USBD_MQ_MSG_SZ+sizeof(void*))*USBD_MQ_MAX_MSG];
  1303. /**
  1304. * This function will initialize usb device thread.
  1305. *
  1306. * @return none.
  1307. */
  1308. rt_err_t rt_usbd_core_init(void)
  1309. {
  1310. rt_list_init(&device_list);
  1311. /* create an usb message queue */
  1312. rt_mq_init(&usb_mq,
  1313. "usbd",
  1314. usb_mq_pool,
  1315. USBD_MQ_MSG_SZ,
  1316. sizeof(usb_mq_pool),
  1317. RT_IPC_FLAG_FIFO);
  1318. /* init usb device thread */
  1319. rt_thread_init(&usb_thread,
  1320. "usbd",
  1321. rt_usbd_thread_entry,
  1322. RT_NULL,
  1323. usb_thread_stack,
  1324. RT_USBD_THREAD_STACK_SZ,
  1325. RT_USBD_THREAD_PRIO,
  1326. 20);
  1327. /* rt_thread_init should always be OK, so start the thread without further
  1328. * checking. */
  1329. return rt_thread_startup(&usb_thread);
  1330. }