usbdevice.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * File : usbdevice.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-02 Yi Qiu first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <rtservice.h>
  27. #ifdef RT_USING_USB_DEVICE
  28. #define USB_DEVICE_CONTROLLER_NAME "usbd"
  29. #ifdef RT_USB_DEVICE_COMPOSITE
  30. const static char* ustring[] =
  31. {
  32. "Language",
  33. "RT-Thread Team.",
  34. "RTT Composite Device",
  35. "320219198301",
  36. "Configuration",
  37. "Interface",
  38. USB_STRING_OS
  39. };
  40. static struct udevice_descriptor compsit_desc =
  41. {
  42. USB_DESC_LENGTH_DEVICE, //bLength;
  43. USB_DESC_TYPE_DEVICE, //type;
  44. USB_BCD_VERSION, //bcdUSB;
  45. USB_CLASS_MISC, //bDeviceClass;
  46. 0x02, //bDeviceSubClass;
  47. 0x01, //bDeviceProtocol;
  48. 0x40, //bMaxPacketSize0;
  49. _VENDOR_ID, //idVendor;
  50. _PRODUCT_ID, //idProduct;
  51. USB_BCD_DEVICE, //bcdDevice;
  52. USB_STRING_MANU_INDEX, //iManufacturer;
  53. USB_STRING_PRODUCT_INDEX, //iProduct;
  54. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  55. USB_DYNAMIC, //bNumConfigurations;
  56. };
  57. //FS and HS needed
  58. static struct usb_qualifier_descriptor dev_qualifier =
  59. {
  60. sizeof(dev_qualifier), //bLength
  61. USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
  62. 0x0200, //bcdUSB
  63. USB_CLASS_MISC, //bDeviceClass
  64. 0x02, //bDeviceSubClass
  65. 0x01, //bDeviceProtocol
  66. 64, //bMaxPacketSize0
  67. 0x01, //bNumConfigurations
  68. 0,
  69. };
  70. #endif
  71. struct usb_os_comp_id_descriptor usb_comp_id_desc =
  72. {
  73. //head section
  74. USB_DYNAMIC,
  75. 0x0100,
  76. 0x04,
  77. USB_DYNAMIC,
  78. {0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  79. };
  80. rt_err_t rt_usb_device_init(void)
  81. {
  82. rt_device_t udc;
  83. udevice_t udevice;
  84. uconfig_t cfg;
  85. ufunction_t func;
  86. /* create and startup usb device thread */
  87. rt_usbd_core_init();
  88. /* create a device object */
  89. udevice = rt_usbd_device_new();
  90. udc = rt_device_find(USB_DEVICE_CONTROLLER_NAME);
  91. if(udc == RT_NULL)
  92. {
  93. rt_kprintf("can't find usb device controller %s\n", USB_DEVICE_CONTROLLER_NAME);
  94. return -RT_ERROR;
  95. }
  96. /* set usb controller driver to the device */
  97. rt_usbd_device_set_controller(udevice, (udcd_t)udc);
  98. /* create a configuration object */
  99. cfg = rt_usbd_config_new();
  100. rt_usbd_device_set_os_comp_id_desc(udevice,&usb_comp_id_desc);
  101. #ifdef RT_USB_DEVICE_MSTORAGE
  102. {
  103. extern ufunction_t rt_usbd_function_mstorage_create(udevice_t device);
  104. /* create a mass storage function object */
  105. func = rt_usbd_function_mstorage_create(udevice);
  106. /* add the function to the configuration */
  107. rt_usbd_config_add_function(cfg, func);
  108. }
  109. #endif
  110. #ifdef RT_USB_DEVICE_CDC
  111. {
  112. extern ufunction_t rt_usbd_function_cdc_create(udevice_t device);
  113. /* create a cdc function object */
  114. func = rt_usbd_function_cdc_create(udevice);
  115. /* add the function to the configuration */
  116. rt_usbd_config_add_function(cfg, func);
  117. }
  118. #endif
  119. #ifdef RT_USB_DEVICE_HID
  120. {
  121. extern ufunction_t rt_usbd_function_hid_create(udevice_t device);
  122. /* create a cdc function object */
  123. func = rt_usbd_function_hid_create(udevice);
  124. /* add the function to the configuration */
  125. rt_usbd_config_add_function(cfg, func);
  126. }
  127. #endif
  128. #ifdef RT_USB_DEVICE_RNDIS
  129. {
  130. extern ufunction_t rt_usbd_function_rndis_create(udevice_t device);
  131. /* create a rndis function object */
  132. func = rt_usbd_function_rndis_create(udevice);
  133. /* add the function to the configuration */
  134. rt_usbd_config_add_function(cfg, func);
  135. }
  136. #endif
  137. #ifdef RT_USB_DEVICE_ECM
  138. {
  139. extern ufunction_t rt_usbd_function_ecm_create(udevice_t device);
  140. /* create a rndis function object */
  141. func = rt_usbd_function_ecm_create(udevice);
  142. /* add the function to the configuration */
  143. rt_usbd_config_add_function(cfg, func);
  144. }
  145. #endif
  146. #ifdef RT_USB_DEVICE_WINUSB
  147. {
  148. extern ufunction_t rt_usbd_function_winusb_create(udevice_t device);
  149. /* create a rndis function object */
  150. func = rt_usbd_function_winusb_create(udevice);
  151. /* add the function to the configuration */
  152. rt_usbd_config_add_function(cfg, func);
  153. }
  154. #endif
  155. /* set device descriptor to the device */
  156. #ifdef RT_USB_DEVICE_COMPOSITE
  157. rt_usbd_device_set_descriptor(udevice, &compsit_desc);
  158. rt_usbd_device_set_string(udevice, ustring);
  159. rt_usbd_device_set_qualifier(device, &dev_qualifier);
  160. #else
  161. rt_usbd_device_set_descriptor(udevice, func->dev_desc);
  162. #endif
  163. /* add the configuration to the device */
  164. rt_usbd_device_add_config(udevice, cfg);
  165. /* initialize usb device controller */
  166. rt_device_init(udc);
  167. /* set default configuration to 1 */
  168. rt_usbd_set_config(udevice, 1);
  169. return RT_EOK;
  170. }
  171. #endif