lowl-usb.c 3.3 KB

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