lowl-hid.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #elif defined(__APPLE__)
  99. //Mach-O uses dylibs for shared libraries
  100. //http://www.finkproject.org/doc/porting/porting.en.html#shared
  101. "dylib"
  102. #else
  103. "so"
  104. #endif
  105. );
  106. if (hidapi_try_lib(dlname))
  107. return true;
  108. }
  109. return false;
  110. }
  111. static
  112. char *wcs2str_dup(wchar_t *ws)
  113. {
  114. if (!(ws && ws[0]))
  115. return NULL;
  116. char *rv;
  117. int clen, i;
  118. clen = wcslen(ws);
  119. ++clen;
  120. rv = malloc(clen);
  121. for (i = 0; i < clen; ++i)
  122. rv[i] = ws[i];
  123. return rv;
  124. }
  125. static
  126. struct lowlevel_device_info *hid_devinfo_scan()
  127. {
  128. if (!hidapi_load_library())
  129. {
  130. applog(LOG_DEBUG, "%s: Failed to load any hidapi library", __func__);
  131. return NULL;
  132. }
  133. struct hid_device_info *hid_enum, *hid_item;
  134. struct lowlevel_device_info *info, *devinfo_list = NULL;
  135. if (_probe_hid_enum)
  136. {
  137. hid_enum = _probe_hid_enum;
  138. _probe_hid_enum = NULL;
  139. }
  140. else
  141. hid_enum = hid_enumerate(0, 0);
  142. if (!hid_enum)
  143. {
  144. applog(LOG_DEBUG, "%s: No HID devices found", __func__);
  145. return NULL;
  146. }
  147. LL_FOREACH(hid_enum, hid_item)
  148. {
  149. info = malloc(sizeof(struct lowlevel_device_info));
  150. char *devid;
  151. const char * const hidpath = hid_item->path;
  152. if (hidapi_libusb
  153. && strlen(hidpath) == 12
  154. && hidpath[0] == '0'
  155. && hidpath[1] == '0'
  156. && isxdigit(hidpath[2])
  157. && isxdigit(hidpath[3])
  158. && hidpath[4] == ':'
  159. && hidpath[5] == '0'
  160. && hidpath[6] == '0'
  161. && isxdigit(hidpath[7])
  162. && isxdigit(hidpath[8])
  163. && hidpath[9] == ':')
  164. {
  165. unsigned char usbbus, usbaddr;
  166. hex2bin(&usbbus , &hidpath[2], 1);
  167. hex2bin(&usbaddr, &hidpath[7], 1);
  168. devid = bfg_make_devid_usb(usbbus, usbaddr);
  169. }
  170. else
  171. {
  172. devid = malloc(4 + strlen(hid_item->path) + 1);
  173. sprintf(devid, "hid:%s", hid_item->path);
  174. }
  175. *info = (struct lowlevel_device_info){
  176. .lowl = &lowl_hid,
  177. .path = strdup(hid_item->path),
  178. .devid = devid,
  179. .vid = hid_item->vendor_id,
  180. .pid = hid_item->product_id,
  181. .manufacturer = wcs2str_dup(hid_item->manufacturer_string),
  182. .product = wcs2str_dup(hid_item->product_string),
  183. .serial = wcs2str_dup(hid_item->serial_number),
  184. };
  185. LL_PREPEND(devinfo_list, info);
  186. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  187. __func__, info->product, info->serial);
  188. }
  189. hid_free_enumeration(hid_enum);
  190. return devinfo_list;
  191. }
  192. struct lowlevel_driver lowl_hid = {
  193. .dname = "hid",
  194. .devinfo_scan = hid_devinfo_scan,
  195. };