usb_dfu_dev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @brief DFU class driver
  3. *
  4. * USB DFU device class driver
  5. *
  6. */
  7. #include <kernel.h>
  8. #include <init.h>
  9. #include <string.h>
  10. #include <sys/byteorder.h>
  11. #include <usb/usb_device.h>
  12. #include <usb/usb_common.h>
  13. #include "usb_dfu_descriptor.h"
  14. #ifdef CONFIG_NVRAM_CONFIG
  15. #include <drivers/nvram_config.h>
  16. #endif
  17. #define LOG_LEVEL CONFIG_SYS_LOG_USB_DFU_LEVEL
  18. #include <logging/log.h>
  19. LOG_MODULE_REGISTER(usb_dfu_dev);
  20. static void usb_dfu_status_cb(enum usb_dc_status_code status, u8_t *param)
  21. {
  22. switch (status) {
  23. case USB_DC_ERROR:
  24. LOG_DBG("USB device error");
  25. break;
  26. case USB_DC_RESET:
  27. LOG_DBG("USB device reset detected");
  28. break;
  29. case USB_DC_CONNECTED:
  30. LOG_DBG("USB device connected");
  31. break;
  32. case USB_DC_CONFIGURED:
  33. LOG_DBG("USB device configured");
  34. break;
  35. case USB_DC_DISCONNECTED:
  36. LOG_DBG("USB device disconnected");
  37. break;
  38. case USB_DC_SUSPEND:
  39. LOG_DBG("USB device suspended");
  40. break;
  41. case USB_DC_RESUME:
  42. LOG_DBG("USB device resumed");
  43. break;
  44. case USB_DC_HIGHSPEED:
  45. LOG_DBG("USB device high-speed");
  46. break;
  47. case USB_DC_SOF:
  48. LOG_DBG("USB device sof-inter");
  49. break;
  50. case USB_DC_UNKNOWN:
  51. default:
  52. LOG_DBG("USB unknown state");
  53. break;
  54. }
  55. }
  56. static int usb_dfu_class_handle_req(struct usb_setup_packet *setup, s32_t *len,
  57. u8_t **data)
  58. {
  59. LOG_DBG("class request bRequest: 0x%x bmRequestType: 0x%x len: %d",
  60. setup->bRequest, setup->bmRequestType, *len);
  61. return 0;
  62. }
  63. static int usb_dfu_custom_handle_req(struct usb_setup_packet *setup, s32_t *len,
  64. u8_t **data)
  65. {
  66. LOG_DBG("custom request bRequest: 0x%x bmRequestType: 0x%x len: %d",
  67. setup->bRequest, setup->bmRequestType, *len);
  68. return -1;
  69. }
  70. static int usb_dfu_vendor_handle_req(struct usb_setup_packet *setup, s32_t *len,
  71. u8_t **data)
  72. {
  73. LOG_DBG("vendor request bRequest: 0x%x bmRequestType: 0x%x len: %d",
  74. setup->bRequest, setup->bmRequestType, *len);
  75. return 0;
  76. }
  77. static const struct usb_cfg_data usb_dfu_config = {
  78. .usb_device_description = NULL,
  79. .cb_usb_status = usb_dfu_status_cb,
  80. .interface = {
  81. .class_handler = usb_dfu_class_handle_req,
  82. .custom_handler = usb_dfu_custom_handle_req,
  83. .vendor_handler = usb_dfu_vendor_handle_req,
  84. },
  85. .num_endpoints = 0,
  86. .endpoint = NULL,
  87. };
  88. static int usb_dfu_fix_dev_sn(void)
  89. {
  90. int ret;
  91. #ifdef CONFIG_NVRAM_CONFIG
  92. int read_len;
  93. u8_t mac_str[CONFIG_USB_DEVICE_STRING_DESC_MAX_LEN];
  94. read_len = nvram_config_get(CONFIG_USB_DFU_DEVICE_SN_NVRAM, mac_str, CONFIG_USB_DEVICE_STRING_DESC_MAX_LEN);
  95. if (read_len < 0) {
  96. LOG_DBG("no sn data in nvram: %d", read_len);
  97. ret = usb_device_register_string_descriptor(DEV_SN_DESC, CONFIG_USB_DFU_DEVICE_SN, strlen(CONFIG_USB_DFU_DEVICE_SN));
  98. if (ret)
  99. return ret;
  100. } else {
  101. ret = usb_device_register_string_descriptor(DEV_SN_DESC, mac_str, read_len);
  102. if (ret)
  103. return ret;
  104. }
  105. #else
  106. ret = usb_device_register_string_descriptor(DEV_SN_DESC, CONFIG_USB_DFU_DEVICE_SN, strlen(CONFIG_USB_DFU_DEVICE_SN));
  107. if (ret)
  108. return ret;
  109. #endif
  110. return 0;
  111. }
  112. /*
  113. * API: initialize USB STUB
  114. */
  115. int usb_dfu_init(struct device *dev)
  116. {
  117. int ret;
  118. /* register string descriptor */
  119. ret = usb_device_register_string_descriptor(MANUFACTURE_STR_DESC, CONFIG_USB_DFU_DEVICE_MANUFACTURER, strlen(CONFIG_USB_DFU_DEVICE_MANUFACTURER));
  120. if (ret) {
  121. return ret;
  122. }
  123. ret = usb_device_register_string_descriptor(PRODUCT_STR_DESC, CONFIG_USB_DFU_DEVICE_PRODUCT, strlen(CONFIG_USB_DFU_DEVICE_PRODUCT));
  124. if (ret) {
  125. return ret;
  126. }
  127. ret = usb_dfu_fix_dev_sn();
  128. if (ret) {
  129. return ret;
  130. }
  131. /* register device descriptor */
  132. usb_device_register_descriptors(usb_dfu_fs_descriptor, usb_dfu_hs_descriptor);
  133. /* initialize the USB driver with the right configuration */
  134. ret = usb_set_config(&usb_dfu_config);
  135. if (ret < 0) {
  136. return ret;
  137. }
  138. /* enable USB driver */
  139. ret = usb_enable(&usb_dfu_config);
  140. if (ret < 0) {
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * API: deinitialize USB Stub
  147. */
  148. int usb_dfu_exit(void)
  149. {
  150. int ret;
  151. ret = usb_disable();
  152. if (ret) {
  153. LOG_ERR("failed to disable USB Stub device");
  154. return ret;
  155. }
  156. usb_deconfig();
  157. return 0;
  158. }