lowl-usb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <stddef.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <libusb.h>
  15. #include "fpgautils.h"
  16. #include "logging.h"
  17. #include "lowlevel.h"
  18. #include "miner.h"
  19. #include "util.h"
  20. static
  21. char *lowl_libusb_dup_string(libusb_device_handle * const handle, const uint8_t idx, const char * const idxname, const char * const fname)
  22. {
  23. if (!idx)
  24. return NULL;
  25. unsigned char buf[0x100];
  26. const int n = libusb_get_string_descriptor_ascii(handle, idx, buf, sizeof(buf)-1);
  27. if (unlikely(n < 0)) {
  28. // This could be LOG_ERR, but it's annoyingly common :/
  29. applog(LOG_DEBUG, "%s: Error getting USB string %d (%s): %s",
  30. fname, idx, idxname, bfg_strerror(n, BST_LIBUSB));
  31. return NULL;
  32. }
  33. if (n == 0)
  34. return NULL;
  35. buf[n] = '\0';
  36. return strdup((void*)buf);
  37. }
  38. static
  39. void usb_devinfo_free(struct lowlevel_device_info * const info)
  40. {
  41. libusb_device * const dev = info->lowl_data;
  42. if (dev)
  43. libusb_unref_device(dev);
  44. }
  45. static
  46. struct lowlevel_device_info *usb_devinfo_scan()
  47. {
  48. struct lowlevel_device_info *devinfo_list = NULL;
  49. ssize_t count, i;
  50. libusb_device **list;
  51. struct libusb_device_descriptor desc;
  52. libusb_device_handle *handle;
  53. struct lowlevel_device_info *info;
  54. int err;
  55. if (unlikely(!have_libusb))
  56. return NULL;
  57. count = libusb_get_device_list(NULL, &list);
  58. if (unlikely(count < 0)) {
  59. applog(LOG_ERR, "%s: Error getting USB device list: %s",
  60. __func__, bfg_strerror(count, BST_LIBUSB));
  61. return NULL;
  62. }
  63. for (i = 0; i < count; ++i) {
  64. err = libusb_get_device_descriptor(list[i], &desc);
  65. if (unlikely(err)) {
  66. applog(LOG_ERR, "%s: Error getting device descriptor: %s",
  67. __func__, bfg_strerror(err, BST_LIBUSB));
  68. continue;
  69. }
  70. info = malloc(sizeof(struct lowlevel_device_info));
  71. *info = (struct lowlevel_device_info){
  72. .lowl = &lowl_usb,
  73. .devid = bfg_make_devid_libusb(list[i]),
  74. .lowl_data = libusb_ref_device(list[i]),
  75. .vid = desc.idVendor,
  76. .pid = desc.idProduct,
  77. };
  78. err = libusb_open(list[i], &handle);
  79. if (unlikely(err))
  80. applog(LOG_ERR, "%s: Error opening device: %s",
  81. __func__, bfg_strerror(err, BST_LIBUSB));
  82. else
  83. {
  84. info->manufacturer = lowl_libusb_dup_string(handle, desc.iManufacturer, "iManufacturer", __func__);
  85. info->product = lowl_libusb_dup_string(handle, desc.iProduct, "iProduct", __func__);
  86. info->serial = lowl_libusb_dup_string(handle, desc.iSerialNumber, "iSerialNumber", __func__);
  87. libusb_close(handle);
  88. }
  89. LL_PREPEND(devinfo_list, info);
  90. }
  91. libusb_free_device_list(list, 1);
  92. return devinfo_list;
  93. }
  94. struct libusb_device_handle *lowl_usb_open(struct lowlevel_device_info * const info)
  95. {
  96. libusb_device * const dev = info->lowl_data;
  97. if (!dev)
  98. return NULL;
  99. libusb_device_handle *devh;
  100. if (libusb_open(dev, &devh)) {
  101. applog(LOG_ERR, "%s: Error opening device", __func__);
  102. return NULL;
  103. }
  104. return devh;
  105. }
  106. void lowl_usb_close(struct libusb_device_handle * const devh)
  107. {
  108. libusb_close(devh);
  109. }
  110. struct lowlevel_driver lowl_usb = {
  111. .dname = "usb",
  112. .devinfo_scan = usb_devinfo_scan,
  113. .devinfo_free = usb_devinfo_free,
  114. };