lowl-hid.c 4.7 KB

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