lowl-usb.c 3.8 KB

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