lowl-hid.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <windows.h>
  15. #define dlopen(lib, flags) LoadLibrary(lib)
  16. #define dlsym(h, sym) ((void*)GetProcAddress(h, sym))
  17. #define dlerror() "unknown"
  18. #define dlclose(h) FreeLibrary(h)
  19. typedef HMODULE dlh_t;
  20. #endif
  21. #include <ctype.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <hidapi.h>
  27. #include <utlist.h>
  28. #include "logging.h"
  29. #include "lowlevel.h"
  30. #include "miner.h"
  31. struct hid_device_info HID_API_EXPORT *(*dlsym_hid_enumerate)(unsigned short, unsigned short);
  32. void HID_API_EXPORT (*dlsym_hid_free_enumeration)(struct hid_device_info *);
  33. hid_device * HID_API_EXPORT (*dlsym_hid_open_path)(const char *);
  34. void HID_API_EXPORT (*dlsym_hid_close)(hid_device *);
  35. int HID_API_EXPORT (*dlsym_hid_read)(hid_device *, unsigned char *, size_t);
  36. int HID_API_EXPORT (*dlsym_hid_write)(hid_device *, const unsigned char *, size_t);
  37. #define LOAD_SYM(sym) do { \
  38. if (!(dlsym_ ## sym = dlsym(dlh, #sym))) { \
  39. applog(LOG_DEBUG, "%s: Failed to load %s in %s", __func__, #sym, dlname); \
  40. goto fail; \
  41. } \
  42. } while(0)
  43. static bool hidapi_libusb;
  44. static struct hid_device_info *_probe_hid_enum;
  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. _probe_hid_enum = 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. if (_probe_hid_enum)
  132. {
  133. hid_enum = _probe_hid_enum;
  134. _probe_hid_enum = NULL;
  135. }
  136. else
  137. hid_enum = hid_enumerate(0, 0);
  138. if (!hid_enum)
  139. {
  140. applog(LOG_DEBUG, "%s: No HID devices found", __func__);
  141. return NULL;
  142. }
  143. LL_FOREACH(hid_enum, hid_item)
  144. {
  145. info = malloc(sizeof(struct lowlevel_device_info));
  146. char *devid;
  147. const char * const hidpath = hid_item->path;
  148. if (hidapi_libusb
  149. && strlen(hidpath) == 12
  150. && hidpath[0] == '0'
  151. && hidpath[1] == '0'
  152. && isxdigit(hidpath[2])
  153. && isxdigit(hidpath[3])
  154. && hidpath[4] == ':'
  155. && hidpath[5] == '0'
  156. && hidpath[6] == '0'
  157. && isxdigit(hidpath[7])
  158. && isxdigit(hidpath[8])
  159. && hidpath[9] == ':')
  160. {
  161. unsigned char usbbus, usbaddr;
  162. hex2bin(&usbbus , &hidpath[2], 1);
  163. hex2bin(&usbaddr, &hidpath[7], 1);
  164. devid = bfg_make_devid_usb(usbbus, usbaddr);
  165. }
  166. else
  167. {
  168. devid = malloc(4 + strlen(hid_item->path) + 1);
  169. sprintf(devid, "hid:%s", hid_item->path);
  170. }
  171. *info = (struct lowlevel_device_info){
  172. .lowl = &lowl_hid,
  173. .path = strdup(hid_item->path),
  174. .devid = devid,
  175. .vid = hid_item->vendor_id,
  176. .pid = hid_item->product_id,
  177. .manufacturer = wcs2str_dup(hid_item->manufacturer_string),
  178. .product = wcs2str_dup(hid_item->product_string),
  179. .serial = wcs2str_dup(hid_item->serial_number),
  180. };
  181. LL_PREPEND(devinfo_list, info);
  182. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  183. __func__, info->product, info->serial);
  184. }
  185. hid_free_enumeration(hid_enum);
  186. return devinfo_list;
  187. }
  188. struct lowlevel_driver lowl_hid = {
  189. .dname = "hid",
  190. .devinfo_scan = hid_devinfo_scan,
  191. };