usbd_desc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 by Sergey Fetisov <fsenok@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. /*
  25. * version: 1.0 demo (7.02.2015)
  26. */
  27. #include "usbd_core.h"
  28. #include "usbd_desc.h"
  29. #include "usbd_conf.h"
  30. #include "usbd_rndis_core.h"
  31. #include "common_config.h"
  32. #define USB_DEVICE_DESCRIPTOR_TYPE 0x01
  33. #define USB_STRING_DESCRIPTOR_TYPE 0x03
  34. #define USB_SIZ_STRING_LANGID 4
  35. usbd_desc_t *get_device_descriptor(void);
  36. usbd_desc_t *get_device_qualifier(void);
  37. usbd_desc_t *get_device_configuration(void);
  38. usbd_desc_t *get_device_other_speed(void);
  39. usbd_desc_t *get_device_lang_id(void);
  40. usbd_desc_t *get_device_manufacturer_string(void);
  41. usbd_desc_t *get_device_product_string(void);
  42. usbd_desc_t *get_device_serial_string(void);
  43. usbd_desc_t *get_device_config_string(void);
  44. usbd_desc_t *get_device_interface_string(void);
  45. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
  46. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  47. extern usbd_core_type usb_core_dev;
  48. extern uint8_t usbd_cdc_CfgDesc[];
  49. usbd_desc_handler USR_desc =
  50. {
  51. get_device_descriptor,
  52. get_device_qualifier,
  53. get_device_configuration,
  54. get_device_other_speed,
  55. get_device_lang_id,
  56. get_device_manufacturer_string,
  57. get_device_product_string,
  58. get_device_serial_string,
  59. get_device_interface_string,
  60. get_device_config_string,
  61. };
  62. #if defined ( __ICCARM__ ) /* iar compiler */
  63. #pragma data_alignment=4
  64. #endif
  65. uint8_t USBD_StrDesc[USB_MAX_STR_DESC_SIZ];
  66. #if defined ( __ICCARM__ ) /* iar compiler */
  67. #pragma data_alignment=4
  68. #endif
  69. ALIGNED_HEAD uint8_t USBD_DeviceDesc[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
  70. {
  71. 18, // bLength = 18 bytes.
  72. USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType = DEVICE
  73. 0x00, 0x02, // bcdUSB = 1.1 0x10,0x01 2.0 0x00,0x02
  74. 0xE0, // bDeviceClass = Communication Device Class
  75. 0x00, // bDeviceSubClass = Unused at this time.
  76. 0x00, // bDeviceProtocol = Unused at this time.
  77. 0x40, // bMaxPacketSize0 = EP0 buffer size..
  78. LBYTE(USBD_VID), HBYTE(USBD_VID), // Vendor ID
  79. LBYTE(USBD_PID), HBYTE(USBD_PID), // Product ID
  80. 0xFF, 0xFF, // bcdDevice
  81. USB_MFC_STRING,
  82. USB_PRODUCT_STRING,
  83. USB_SERIAL_STRING,
  84. 1
  85. };
  86. #if defined ( __ICCARM__ ) /* iar compiler */
  87. #pragma data_alignment=4
  88. #endif
  89. /* USB Standard Device Descriptor */
  90. ALIGNED_HEAD uint8_t USBD_DeviceQualifierDesc[USB_DEVICE_QUALIFIER_DESC_LEN] ALIGNED_TAIL =
  91. {
  92. USB_DEVICE_QUALIFIER_DESC_LEN,
  93. USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER,
  94. 0x00,
  95. 0x02,
  96. 0x00,
  97. 0x00,
  98. 0x00,
  99. 0x40,
  100. 0x01,
  101. 0x00,
  102. };
  103. #if defined ( __ICCARM__ ) /* iar compiler */
  104. #pragma data_alignment=4
  105. #endif
  106. /* USB Standard Device Descriptor */
  107. ALIGNED_HEAD uint8_t USBD_LangIDDesc[USB_SIZ_STRING_LANGID] ALIGNED_TAIL =
  108. {
  109. USB_SIZ_STRING_LANGID,
  110. USB_DESCIPTOR_TYPE_STRING,
  111. LBYTE(USBD_LANGID_STRING),
  112. HBYTE(USBD_LANGID_STRING),
  113. };
  114. /* device descriptor */
  115. usbd_desc_t device_descriptor =
  116. {
  117. USB_DEVICE_DESC_LEN,
  118. USBD_DeviceDesc
  119. };
  120. /* device qualifier */
  121. usbd_desc_t device_qualifier =
  122. {
  123. USB_DEVICE_QUALIFIER_DESC_LEN,
  124. USBD_DeviceQualifierDesc
  125. };
  126. /* langid descriptor */
  127. usbd_desc_t langid_descriptor =
  128. {
  129. USB_SIZ_STRING_LANGID,
  130. USBD_LangIDDesc
  131. };
  132. usbd_desc_t vp_desc;
  133. /**
  134. * @brief standard usb unicode convert
  135. * @param string: source string
  136. * @param unicode_buf: unicode buffer
  137. * @retval length
  138. */
  139. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  140. {
  141. uint16_t str_len = 0, id_pos = 2;
  142. uint8_t *tmp_str = string;
  143. while(*tmp_str != '\0')
  144. {
  145. str_len ++;
  146. unicode_buf[id_pos ++] = *tmp_str ++;
  147. unicode_buf[id_pos ++] = 0x00;
  148. }
  149. str_len = str_len * 2 + 2;
  150. unicode_buf[0] = str_len;
  151. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  152. return str_len;
  153. }
  154. /**
  155. * @brief usb int convert to unicode
  156. * @param value: int value
  157. * @param pbus: unicode buffer
  158. * @param len: length
  159. * @retval none
  160. */
  161. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  162. {
  163. uint8_t idx = 0;
  164. for( idx = 0 ; idx < len ; idx ++)
  165. {
  166. if( ((value >> 28)) < 0xA )
  167. {
  168. pbuf[ 2 * idx] = (value >> 28) + '0';
  169. }
  170. else
  171. {
  172. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  173. }
  174. value = value << 4;
  175. pbuf[2 * idx + 1] = 0;
  176. }
  177. }
  178. /**
  179. * @brief get device descriptor
  180. * @param none
  181. * @retval usbd_desc
  182. */
  183. usbd_desc_t *get_device_descriptor(void)
  184. {
  185. UDBG printf("DeviceDescriptor\r\n");
  186. return &device_descriptor;
  187. }
  188. usbd_desc_t *get_device_qualifier(void)
  189. {
  190. UDBG printf("DeviceQulifier\r\n");
  191. return &device_qualifier;
  192. }
  193. usbd_desc_t *get_device_configuration(void)
  194. {
  195. //return NULL;
  196. //vp_desc.length = sizeof (usbd_cdc_CfgDesc);
  197. //uint8_t *dara;
  198. vp_desc.descriptor = usbd_rndis_get_cfg(0, &vp_desc.length);
  199. //vp_desc.descriptor = (uint8_t *)10;
  200. // usbd_cdc_CfgDesc[2] = sizeof(usbd_cdc_CfgDesc) & 0xFF;
  201. // usbd_cdc_CfgDesc[3] = (sizeof(usbd_cdc_CfgDesc) >> 8) & 0xFF;
  202. // vp_desc.descriptor = usbd_cdc_CfgDesc;
  203. // return NULL;
  204. return &vp_desc;
  205. }
  206. usbd_desc_t *get_device_other_speed(void)
  207. {
  208. return NULL;
  209. }
  210. /**
  211. * @brief get lang id descriptor
  212. * @param none
  213. * @retval usbd_desc
  214. */
  215. usbd_desc_t *get_device_lang_id(void)
  216. {
  217. UDBG printf("LangIDStrDescriptor\r\n");
  218. return &langid_descriptor;
  219. }
  220. /**
  221. * @brief get product descriptor
  222. * @param none
  223. * @retval usbd_desc
  224. */
  225. usbd_desc_t *get_device_product_string(void)
  226. {
  227. UDBG printf("ProductStrDescriptor\r\n");
  228. static usbd_desc_t device_product_string_disc;
  229. device_product_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc);
  230. device_product_string_disc.descriptor = USBD_StrDesc;
  231. return &device_product_string_disc;
  232. //vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_PRODUCT_FS_STRING, g_usbd_desc_buffer);
  233. //vp_desc.descriptor = g_usbd_desc_buffer;
  234. //return &vp_desc;
  235. }
  236. /**
  237. * @brief get manufacturer descriptor
  238. * @param none
  239. * @retval usbd_desc
  240. */
  241. usbd_desc_t *get_device_manufacturer_string(void)
  242. {
  243. UDBG printf("ManufacturerStrDescriptor\r\n");
  244. static usbd_desc_t device_manufacturer_string_disc;
  245. device_manufacturer_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc);
  246. device_manufacturer_string_disc.descriptor = USBD_StrDesc;
  247. return &device_manufacturer_string_disc;
  248. //vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MANUFACTURER_STRING, g_usbd_desc_buffer);
  249. //vp_desc.descriptor = g_usbd_desc_buffer;
  250. //return &vp_desc;
  251. }
  252. /**
  253. * @brief get serial descriptor
  254. * @param none
  255. * @retval usbd_desc
  256. */
  257. usbd_desc_t *get_device_serial_string(void)
  258. {
  259. UDBG printf("SerialStrDescriptor\r\n");
  260. static usbd_desc_t device_serial_string_disc;
  261. device_serial_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc);
  262. device_serial_string_disc.descriptor = USBD_StrDesc;
  263. return &device_serial_string_disc;
  264. //vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_SERIALNUMBER_FS_STRING, g_usbd_desc_buffer);
  265. //vp_desc.descriptor = g_usbd_desc_buffer;
  266. //return &vp_desc;
  267. }
  268. /**
  269. * @brief get device config descriptor
  270. * @param none
  271. * @retval usbd_desc
  272. */
  273. usbd_desc_t *get_device_config_string(void)
  274. {
  275. UDBG printf("ConfigStrDescriptor\r\n");
  276. static usbd_desc_t device_config_string_disc;
  277. device_config_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc);
  278. device_config_string_disc.descriptor = USBD_StrDesc;
  279. return &device_config_string_disc;
  280. //vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_CONFIGURATION_FS_STRING, g_usbd_desc_buffer);
  281. //vp_desc.descriptor = g_usbd_desc_buffer;
  282. //return &vp_desc;
  283. }
  284. /**
  285. * @brief get interface descriptor
  286. * @param none
  287. * @retval usbd_desc
  288. */
  289. usbd_desc_t *get_device_interface_string(void)
  290. {
  291. UDBG printf("InterfaceStrDescriptor\r\n");
  292. static usbd_desc_t device_interface_string_disc;
  293. device_interface_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc);
  294. device_interface_string_disc.descriptor = USBD_StrDesc;
  295. return &device_interface_string_disc;
  296. //vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_INTERFACE_FS_STRING, g_usbd_desc_buffer);
  297. //vp_desc.descriptor = g_usbd_desc_buffer;
  298. //return &vp_desc;
  299. }