usbd_desc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #define USB_SIZ_DEVICE_DESC 0x12
  36. usbd_desc_t *get_device_descriptor(void);
  37. usbd_desc_t *get_device_qualifier(void);
  38. usbd_desc_t *get_device_configuration(void);
  39. usbd_desc_t *get_device_other_speed(void);
  40. usbd_desc_t *get_device_lang_id(void);
  41. usbd_desc_t *get_device_manufacturer_string(void);
  42. usbd_desc_t *get_device_product_string(void);
  43. usbd_desc_t *get_device_serial_string(void);
  44. usbd_desc_t *get_device_config_string(void);
  45. usbd_desc_t *get_device_interface_string(void);
  46. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
  47. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  48. extern usbd_core_type usb_core_dev;
  49. extern uint8_t usbd_cdc_CfgDesc[];
  50. usbd_desc_handler USR_desc =
  51. {
  52. get_device_descriptor,
  53. get_device_qualifier,
  54. get_device_configuration,
  55. get_device_other_speed,
  56. get_device_lang_id,
  57. get_device_manufacturer_string,
  58. get_device_product_string,
  59. get_device_serial_string,
  60. get_device_interface_string,
  61. get_device_config_string,
  62. };
  63. #if defined ( __ICCARM__ ) /* iar compiler */
  64. #pragma data_alignment=4
  65. #endif
  66. uint8_t USBD_StrDesc[USB_MAX_STR_DESC_SIZ];
  67. #if defined ( __ICCARM__ ) /* iar compiler */
  68. #pragma data_alignment=4
  69. #endif
  70. ALIGNED_HEAD uint8_t USBD_DeviceDesc[USB_SIZ_DEVICE_DESC] ALIGNED_TAIL =
  71. {
  72. 18, // bLength = 18 bytes.
  73. USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType = DEVICE
  74. 0x00, 0x02, // bcdUSB = 1.1 0x10,0x01 2.0 0x00,0x02
  75. 0xE0, // bDeviceClass = Communication Device Class
  76. 0x00, // bDeviceSubClass = Unused at this time.
  77. 0x00, // bDeviceProtocol = Unused at this time.
  78. 0x40, // bMaxPacketSize0 = EP0 buffer size..
  79. LBYTE(USBD_VID), HBYTE(USBD_VID), // Vendor ID
  80. LBYTE(USBD_PID), HBYTE(USBD_PID), // Product ID
  81. 0xFF, 0xFF, // bcdDevice
  82. USB_MFC_STRING,
  83. USB_PRODUCT_STRING,
  84. USB_SERIAL_STRING,
  85. 1
  86. };
  87. #if defined ( __ICCARM__ ) /* iar compiler */
  88. #pragma data_alignment=4
  89. #endif
  90. /* USB Standard Device Descriptor */
  91. ALIGNED_HEAD uint8_t USBD_DeviceQualifierDesc[USB_DEVICE_QUALIFIER_DESC_LEN] ALIGNED_TAIL =
  92. {
  93. USB_DEVICE_QUALIFIER_DESC_LEN,
  94. USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER,
  95. 0x00,
  96. 0x02,
  97. 0x00,
  98. 0x00,
  99. 0x00,
  100. 0x40,
  101. 0x01,
  102. 0x00,
  103. };
  104. #if defined ( __ICCARM__ ) /* iar compiler */
  105. #pragma data_alignment=4
  106. #endif
  107. /* USB Standard Device Descriptor */
  108. ALIGNED_HEAD uint8_t USBD_LangIDDesc[USB_SIZ_STRING_LANGID] ALIGNED_TAIL =
  109. {
  110. USB_SIZ_STRING_LANGID,
  111. USB_DESCIPTOR_TYPE_STRING,
  112. LBYTE(USBD_LANGID_STRING),
  113. HBYTE(USBD_LANGID_STRING),
  114. };
  115. /* device descriptor */
  116. usbd_desc_t device_descriptor =
  117. {
  118. USB_DEVICE_DESC_LEN,
  119. USBD_DeviceDesc
  120. };
  121. /* device qualifier */
  122. usbd_desc_t device_qualifier =
  123. {
  124. USB_DEVICE_QUALIFIER_DESC_LEN,
  125. USBD_DeviceQualifierDesc
  126. };
  127. /* langid descriptor */
  128. usbd_desc_t langid_descriptor =
  129. {
  130. USB_SIZ_STRING_LANGID,
  131. USBD_LangIDDesc
  132. };
  133. usbd_desc_t vp_desc;
  134. /**
  135. * @brief standard usb unicode convert
  136. * @param string: source string
  137. * @param unicode_buf: unicode buffer
  138. * @retval length
  139. */
  140. uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
  141. {
  142. uint16_t str_len = 0, id_pos = 2;
  143. uint8_t *tmp_str = string;
  144. while(*tmp_str != '\0')
  145. {
  146. str_len ++;
  147. unicode_buf[id_pos ++] = *tmp_str ++;
  148. unicode_buf[id_pos ++] = 0x00;
  149. }
  150. str_len = str_len * 2 + 2;
  151. unicode_buf[0] = str_len;
  152. unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
  153. return str_len;
  154. }
  155. /**
  156. * @brief usb int convert to unicode
  157. * @param value: int value
  158. * @param pbus: unicode buffer
  159. * @param len: length
  160. * @retval none
  161. */
  162. static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  163. {
  164. uint8_t idx = 0;
  165. for( idx = 0 ; idx < len ; idx ++)
  166. {
  167. if( ((value >> 28)) < 0xA )
  168. {
  169. pbuf[ 2 * idx] = (value >> 28) + '0';
  170. }
  171. else
  172. {
  173. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  174. }
  175. value = value << 4;
  176. pbuf[2 * idx + 1] = 0;
  177. }
  178. }
  179. /**
  180. * @brief get device descriptor
  181. * @param none
  182. * @retval usbd_desc
  183. */
  184. usbd_desc_t *get_device_descriptor(void)
  185. {
  186. DBG printf("DeviceDescriptor\r\n");
  187. return &device_descriptor;
  188. }
  189. usbd_desc_t *get_device_qualifier(void)
  190. {
  191. DBG printf("DeviceQulifier\r\n");
  192. return &device_qualifier;
  193. }
  194. usbd_desc_t *get_device_configuration(void)
  195. {
  196. vp_desc.descriptor = usbd_rndis_get_cfg(0, &vp_desc.length);
  197. return &vp_desc;
  198. }
  199. usbd_desc_t *get_device_other_speed(void)
  200. {
  201. return NULL;
  202. }
  203. /**
  204. * @brief get lang id descriptor
  205. * @param none
  206. * @retval usbd_desc
  207. */
  208. usbd_desc_t *get_device_lang_id(void)
  209. {
  210. DBG printf("LangIDStrDescriptor\r\n");
  211. return &langid_descriptor;
  212. }
  213. /**
  214. * @brief get product descriptor
  215. * @param none
  216. * @retval usbd_desc
  217. */
  218. usbd_desc_t *get_device_product_string(void)
  219. {
  220. DBG printf("ProductStrDescriptor\r\n");
  221. static usbd_desc_t device_product_string_disc;
  222. device_product_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc);
  223. device_product_string_disc.descriptor = USBD_StrDesc;
  224. return &device_product_string_disc;
  225. }
  226. /**
  227. * @brief get manufacturer descriptor
  228. * @param none
  229. * @retval usbd_desc
  230. */
  231. usbd_desc_t *get_device_manufacturer_string(void)
  232. {
  233. DBG printf("ManufacturerStrDescriptor\r\n");
  234. static usbd_desc_t device_manufacturer_string_disc;
  235. device_manufacturer_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc);
  236. device_manufacturer_string_disc.descriptor = USBD_StrDesc;
  237. return &device_manufacturer_string_disc;
  238. }
  239. /**
  240. * @brief get serial descriptor
  241. * @param none
  242. * @retval usbd_desc
  243. */
  244. usbd_desc_t *get_device_serial_string(void)
  245. {
  246. DBG printf("SerialStrDescriptor\r\n");
  247. static usbd_desc_t device_serial_string_disc;
  248. device_serial_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc);
  249. device_serial_string_disc.descriptor = USBD_StrDesc;
  250. return &device_serial_string_disc;
  251. }
  252. /**
  253. * @brief get device config descriptor
  254. * @param none
  255. * @retval usbd_desc
  256. */
  257. usbd_desc_t *get_device_config_string(void)
  258. {
  259. DBG printf("ConfigStrDescriptor\r\n");
  260. static usbd_desc_t device_config_string_disc;
  261. device_config_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc);
  262. device_config_string_disc.descriptor = USBD_StrDesc;
  263. return &device_config_string_disc;
  264. }
  265. /**
  266. * @brief get interface descriptor
  267. * @param none
  268. * @retval usbd_desc
  269. */
  270. usbd_desc_t *get_device_interface_string(void)
  271. {
  272. DBG printf("InterfaceStrDescriptor\r\n");
  273. static usbd_desc_t device_interface_string_disc;
  274. device_interface_string_disc.length = usbd_unicode_convert((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc);
  275. device_interface_string_disc.descriptor = USBD_StrDesc;
  276. return &device_interface_string_disc;
  277. }