lowl-usb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #ifdef WIN32
  11. #include <winsock2.h>
  12. #endif
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <libusb.h>
  18. #include "fpgautils.h"
  19. #include "logging.h"
  20. #include "lowlevel.h"
  21. #include "miner.h"
  22. #include "util.h"
  23. static
  24. char *lowl_libusb_dup_string(libusb_device_handle * const handle, const uint8_t idx, const char * const idxname, const char * const fname)
  25. {
  26. if (!idx)
  27. return NULL;
  28. unsigned char buf[0x100];
  29. const int n = libusb_get_string_descriptor_ascii(handle, idx, buf, sizeof(buf)-1);
  30. if (unlikely(n < 0)) {
  31. // This could be LOG_ERR, but it's annoyingly common :/
  32. applog(LOG_DEBUG, "%s: Error getting USB string %d (%s): %s",
  33. fname, idx, idxname, bfg_strerror(n, BST_LIBUSB));
  34. return NULL;
  35. }
  36. if (n == 0)
  37. return NULL;
  38. buf[n] = '\0';
  39. return strdup((void*)buf);
  40. }
  41. static
  42. void usb_devinfo_free(struct lowlevel_device_info * const info)
  43. {
  44. libusb_device * const dev = info->lowl_data;
  45. if (dev)
  46. libusb_unref_device(dev);
  47. }
  48. static
  49. struct lowlevel_device_info *usb_devinfo_scan()
  50. {
  51. struct lowlevel_device_info *devinfo_list = NULL;
  52. ssize_t count, i;
  53. libusb_device **list;
  54. struct libusb_device_descriptor desc;
  55. libusb_device_handle *handle;
  56. struct lowlevel_device_info *info;
  57. int err;
  58. if (unlikely(!have_libusb))
  59. return NULL;
  60. count = libusb_get_device_list(NULL, &list);
  61. if (unlikely(count < 0)) {
  62. applog(LOG_ERR, "%s: Error getting USB device list: %s",
  63. __func__, bfg_strerror(count, BST_LIBUSB));
  64. return NULL;
  65. }
  66. for (i = 0; i < count; ++i) {
  67. err = libusb_get_device_descriptor(list[i], &desc);
  68. if (unlikely(err)) {
  69. applog(LOG_ERR, "%s: Error getting device descriptor: %s",
  70. __func__, bfg_strerror(err, BST_LIBUSB));
  71. continue;
  72. }
  73. info = malloc(sizeof(struct lowlevel_device_info));
  74. *info = (struct lowlevel_device_info){
  75. .lowl = &lowl_usb,
  76. .devid = bfg_make_devid_libusb(list[i]),
  77. .lowl_data = libusb_ref_device(list[i]),
  78. .vid = desc.idVendor,
  79. .pid = desc.idProduct,
  80. };
  81. err = libusb_open(list[i], &handle);
  82. if (unlikely(err))
  83. applog(LOG_DEBUG, "%s: Error opening device %s: %s",
  84. __func__, info->devid, bfg_strerror(err, BST_LIBUSB));
  85. else
  86. {
  87. info->manufacturer = lowl_libusb_dup_string(handle, desc.iManufacturer, "iManufacturer", __func__);
  88. info->product = lowl_libusb_dup_string(handle, desc.iProduct, "iProduct", __func__);
  89. info->serial = lowl_libusb_dup_string(handle, desc.iSerialNumber, "iSerialNumber", __func__);
  90. libusb_close(handle);
  91. }
  92. LL_PREPEND(devinfo_list, info);
  93. }
  94. libusb_free_device_list(list, 1);
  95. return devinfo_list;
  96. }
  97. struct libusb_device_handle *lowl_usb_open(struct lowlevel_device_info * const info)
  98. {
  99. libusb_device * const dev = info->lowl_data;
  100. if (!dev)
  101. return NULL;
  102. libusb_device_handle *devh;
  103. if (libusb_open(dev, &devh)) {
  104. applog(LOG_ERR, "%s: Error opening device", __func__);
  105. return NULL;
  106. }
  107. return devh;
  108. }
  109. void lowl_usb_close(struct libusb_device_handle * const devh)
  110. {
  111. libusb_close(devh);
  112. }
  113. struct lowlevel_driver lowl_usb = {
  114. .dname = "usb",
  115. .devinfo_scan = usb_devinfo_scan,
  116. .devinfo_free = usb_devinfo_free,
  117. };