lowl-hid.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <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
  44. bool hidapi_try_lib(const char * const dlname)
  45. {
  46. struct hid_device_info *hid_enum;
  47. dlh_t dlh;
  48. dlh = dlopen(dlname, RTLD_NOW);
  49. if (!dlh)
  50. {
  51. applog(LOG_DEBUG, "%s: Couldn't load %s: %s", __func__, dlname, dlerror());
  52. return false;
  53. }
  54. LOAD_SYM(hid_enumerate);
  55. LOAD_SYM(hid_free_enumeration);
  56. hid_enum = dlsym_hid_enumerate(0, 0);
  57. if (!hid_enum)
  58. {
  59. applog(LOG_DEBUG, "%s: Loaded %s, but no devices enumerated; trying other libraries", __func__, dlname);
  60. goto fail;
  61. }
  62. dlsym_hid_free_enumeration(hid_enum);
  63. LOAD_SYM(hid_open_path);
  64. LOAD_SYM(hid_close);
  65. LOAD_SYM(hid_read);
  66. LOAD_SYM(hid_write);
  67. applog(LOG_DEBUG, "%s: Successfully loaded %s", __func__, dlname);
  68. return true;
  69. fail:
  70. dlclose(dlh);
  71. return false;
  72. }
  73. // #defines hid_* calls, so must be after library loader
  74. #include "lowl-hid.h"
  75. static
  76. bool hidapi_load_library()
  77. {
  78. if (dlsym_hid_write)
  79. return true;
  80. const char **p;
  81. char dlname[23] = "libhidapi";
  82. const char *dltry[] = {
  83. "",
  84. "-0",
  85. "-hidraw",
  86. "-libusb",
  87. NULL
  88. };
  89. for (p = &dltry[0]; *p; ++p)
  90. {
  91. sprintf(&dlname[9], "%s.%s", *p,
  92. #ifdef WIN32
  93. "dll"
  94. #elif defined(__APPLE__)
  95. //Mach-O uses dylibs for shared libraries
  96. //http://www.finkproject.org/doc/porting/porting.en.html#shared
  97. "dylib"
  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 * const devid = malloc(4 + strlen(hid_item->path) + 1);
  141. sprintf(devid, "hid:%s", hid_item->path);
  142. *info = (struct lowlevel_device_info){
  143. .lowl = &lowl_hid,
  144. .path = strdup(hid_item->path),
  145. .devid = devid,
  146. .vid = hid_item->vendor_id,
  147. .pid = hid_item->product_id,
  148. .manufacturer = wcs2str_dup(hid_item->manufacturer_string),
  149. .product = wcs2str_dup(hid_item->product_string),
  150. .serial = wcs2str_dup(hid_item->serial_number),
  151. };
  152. LL_PREPEND(devinfo_list, info);
  153. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  154. __func__, info->product, info->serial);
  155. }
  156. hid_free_enumeration(hid_enum);
  157. return devinfo_list;
  158. }
  159. struct lowlevel_driver lowl_hid = {
  160. .dname = "hid",
  161. .devinfo_scan = hid_devinfo_scan,
  162. };