core.c 35 KB

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