lowl-hid.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifndef WIN32
  11. #include <dlfcn.h>
  12. typedef void *dlh_t;
  13. #else
  14. #include <winsock2.h>
  15. #include <windows.h>
  16. #define dlopen(lib, flags) LoadLibrary(lib)
  17. #define dlsym(h, sym) ((void*)GetProcAddress(h, sym))
  18. #define dlerror() "unknown"
  19. #define dlclose(h) FreeLibrary(h)
  20. typedef HMODULE dlh_t;
  21. #endif
  22. #include <ctype.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <hidapi.h>
  28. #include <utlist.h>
  29. #include "fpgautils.h"
  30. #include "logging.h"
  31. #include "lowlevel.h"
  32. #include "miner.h"
  33. struct hid_device_info HID_API_EXPORT *(*dlsym_hid_enumerate)(unsigned short, unsigned short);
  34. void HID_API_EXPORT (*dlsym_hid_free_enumeration)(struct hid_device_info *);
  35. hid_device * HID_API_EXPORT (*dlsym_hid_open_path)(const char *);
  36. void HID_API_EXPORT (*dlsym_hid_close)(hid_device *);
  37. int HID_API_EXPORT (*dlsym_hid_read)(hid_device *, unsigned char *, size_t);
  38. int HID_API_EXPORT (*dlsym_hid_write)(hid_device *, const unsigned char *, size_t);
  39. #define LOAD_SYM(sym) do { \
  40. if (!(dlsym_ ## sym = dlsym(dlh, #sym))) { \
  41. applog(LOG_DEBUG, "%s: Failed to load %s in %s", __func__, #sym, dlname); \
  42. goto fail; \
  43. } \
  44. } while(0)
  45. static bool hidapi_libusb;
  46. static
  47. bool hidapi_try_lib(const char * const dlname)
  48. {
  49. struct hid_device_info *hid_enum;
  50. dlh_t dlh;
  51. dlh = dlopen(dlname, RTLD_NOW);
  52. if (!dlh)
  53. {
  54. applog(LOG_DEBUG, "%s: Couldn't load %s: %s", __func__, dlname, dlerror());
  55. return false;
  56. }
  57. LOAD_SYM(hid_enumerate);
  58. LOAD_SYM(hid_free_enumeration);
  59. hid_enum = dlsym_hid_enumerate(0, 0);
  60. if (!hid_enum)
  61. {
  62. applog(LOG_DEBUG, "%s: Loaded %s, but no devices enumerated; trying other libraries", __func__, dlname);
  63. goto fail;
  64. }
  65. dlsym_hid_free_enumeration(hid_enum);
  66. LOAD_SYM(hid_open_path);
  67. LOAD_SYM(hid_close);
  68. LOAD_SYM(hid_read);
  69. LOAD_SYM(hid_write);
  70. if (strstr(dlname, "libusb"))
  71. hidapi_libusb = true;
  72. applog(LOG_DEBUG, "%s: Successfully loaded %s", __func__, dlname);
  73. return true;
  74. fail:
  75. dlclose(dlh);
  76. return false;
  77. }
  78. // #defines hid_* calls, so must be after library loader
  79. #include "lowl-hid.h"
  80. static
  81. bool hidapi_load_library()
  82. {
  83. if (dlsym_hid_write)
  84. return true;
  85. const char **p;
  86. char dlname[23] = "libhidapi";
  87. const char *dltry[] = {
  88. "",
  89. "-0",
  90. "-hidraw",
  91. "-libusb",
  92. NULL
  93. };
  94. for (p = &dltry[0]; *p; ++p)
  95. {
  96. sprintf(&dlname[9], "%s.%s", *p,
  97. #ifdef WIN32
  98. "dll"
  99. #else
  100. "so"
  101. #endif
  102. );
  103. if (hidapi_try_lib(dlname))
  104. return true;
  105. }
  106. return false;
  107. }
  108. static
  109. char *wcs2str_dup(wchar_t *ws)
  110. {
  111. if (!(ws && ws[0]))
  112. return NULL;
  113. char *rv;
  114. int clen, i;
  115. clen = wcslen(ws);
  116. ++clen;
  117. rv = malloc(clen);
  118. for (i = 0; i < clen; ++i)
  119. rv[i] = ws[i];
  120. return rv;
  121. }
  122. static
  123. struct lowlevel_device_info *hid_devinfo_scan()
  124. {
  125. if (!hidapi_load_library())
  126. {
  127. applog(LOG_DEBUG, "%s: Failed to load any hidapi library", __func__);
  128. return NULL;
  129. }
  130. struct hid_device_info *hid_enum, *hid_item;
  131. struct lowlevel_device_info *info, *devinfo_list = NULL;
  132. hid_enum = hid_enumerate(0, 0);
  133. if (!hid_enum)
  134. {
  135. applog(LOG_DEBUG, "%s: No HID devices found", __func__);
  136. return NULL;
  137. }
  138. LL_FOREACH(hid_enum, hid_item)
  139. {
  140. info = malloc(sizeof(struct lowlevel_device_info));
  141. char *devid;
  142. const char * const hidpath = hid_item->path;
  143. if (hidapi_libusb
  144. && strlen(hidpath) == 12
  145. && hidpath[0] == '0'
  146. && hidpath[1] == '0'
  147. && isxdigit(hidpath[2])
  148. && isxdigit(hidpath[3])
  149. && hidpath[4] == ':'
  150. && hidpath[5] == '0'
  151. && hidpath[6] == '0'
  152. && isxdigit(hidpath[7])
  153. && isxdigit(hidpath[8])
  154. && hidpath[9] == ':')
  155. {
  156. unsigned char usbbus, usbaddr;
  157. hex2bin(&usbbus , &hidpath[2], 1);
  158. hex2bin(&usbaddr, &hidpath[7], 1);
  159. devid = bfg_make_devid_usb(usbbus, usbaddr);
  160. }
  161. else
  162. {
  163. devid = malloc(4 + strlen(hid_item->path) + 1);
  164. sprintf(devid, "hid:%s", hid_item->path);
  165. }
  166. *info = (struct lowlevel_device_info){
  167. .lowl = &lowl_hid,
  168. .path = strdup(hid_item->path),
  169. .devid = devid,
  170. .vid = hid_item->vendor_id,
  171. .pid = hid_item->product_id,
  172. .manufacturer = wcs2str_dup(hid_item->manufacturer_string),
  173. .product = wcs2str_dup(hid_item->product_string),
  174. .serial = wcs2str_dup(hid_item->serial_number),
  175. };
  176. LL_PREPEND(devinfo_list, info);
  177. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  178. __func__, info->product, info->serial);
  179. }
  180. hid_free_enumeration(hid_enum);
  181. return devinfo_list;
  182. }
  183. struct lowlevel_driver lowl_hid = {
  184. .dname = "hid",
  185. .devinfo_scan = hid_devinfo_scan,
  186. };