lowl-hid.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 struct hid_device_info *_probe_hid_enum;
  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. _probe_hid_enum = 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. if (_probe_hid_enum)
  133. {
  134. hid_enum = _probe_hid_enum;
  135. _probe_hid_enum = NULL;
  136. }
  137. else
  138. hid_enum = hid_enumerate(0, 0);
  139. if (!hid_enum)
  140. {
  141. applog(LOG_DEBUG, "%s: No HID devices found", __func__);
  142. return NULL;
  143. }
  144. LL_FOREACH(hid_enum, hid_item)
  145. {
  146. info = malloc(sizeof(struct lowlevel_device_info));
  147. char *devid;
  148. const char * const hidpath = hid_item->path;
  149. if (hidapi_libusb
  150. && strlen(hidpath) == 12
  151. && hidpath[0] == '0'
  152. && hidpath[1] == '0'
  153. && isxdigit(hidpath[2])
  154. && isxdigit(hidpath[3])
  155. && hidpath[4] == ':'
  156. && hidpath[5] == '0'
  157. && hidpath[6] == '0'
  158. && isxdigit(hidpath[7])
  159. && isxdigit(hidpath[8])
  160. && hidpath[9] == ':')
  161. {
  162. unsigned char usbbus, usbaddr;
  163. hex2bin(&usbbus , &hidpath[2], 1);
  164. hex2bin(&usbaddr, &hidpath[7], 1);
  165. devid = bfg_make_devid_usb(usbbus, usbaddr);
  166. }
  167. else
  168. {
  169. devid = malloc(4 + strlen(hid_item->path) + 1);
  170. sprintf(devid, "hid:%s", hid_item->path);
  171. }
  172. *info = (struct lowlevel_device_info){
  173. .lowl = &lowl_hid,
  174. .path = strdup(hid_item->path),
  175. .devid = devid,
  176. .vid = hid_item->vendor_id,
  177. .pid = hid_item->product_id,
  178. .manufacturer = wcs2str_dup(hid_item->manufacturer_string),
  179. .product = wcs2str_dup(hid_item->product_string),
  180. .serial = wcs2str_dup(hid_item->serial_number),
  181. };
  182. LL_PREPEND(devinfo_list, info);
  183. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  184. __func__, info->product, info->serial);
  185. }
  186. hid_free_enumeration(hid_enum);
  187. return devinfo_list;
  188. }
  189. struct lowlevel_driver lowl_hid = {
  190. .dname = "hid",
  191. .devinfo_scan = hid_devinfo_scan,
  192. };