lowlevel.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2012-2014 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. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <utlist.h>
  14. #include "compat.h"
  15. #include "logging.h"
  16. #include "lowlevel.h"
  17. #include "miner.h"
  18. static struct lowlevel_device_info *devinfo_list;
  19. #if defined(HAVE_LIBUSB) || defined(NEED_BFG_LOWL_HID)
  20. char *bfg_make_devid_usb(const uint8_t usbbus, const uint8_t usbaddr)
  21. {
  22. char * const devpath = malloc(12);
  23. sprintf(devpath, "usb:%03u:%03u", (unsigned)usbbus, (unsigned)usbaddr);
  24. return devpath;
  25. }
  26. #endif
  27. void lowlevel_devinfo_semicpy(struct lowlevel_device_info * const dst, const struct lowlevel_device_info * const src)
  28. {
  29. #define COPYSTR(key) BFGINIT(dst->key, maybe_strdup(src->key))
  30. COPYSTR(manufacturer);
  31. COPYSTR(product);
  32. COPYSTR(serial);
  33. COPYSTR(path);
  34. COPYSTR(devid);
  35. BFGINIT(dst->vid, src->vid);
  36. BFGINIT(dst->pid, src->pid);
  37. }
  38. void lowlevel_devinfo_free(struct lowlevel_device_info * const info)
  39. {
  40. if (info->ref--)
  41. return;
  42. if (info->lowl->devinfo_free)
  43. info->lowl->devinfo_free(info);
  44. free(info->manufacturer);
  45. free(info->product);
  46. free(info->serial);
  47. free(info->path);
  48. free(info->devid);
  49. free(info);
  50. }
  51. struct lowlevel_device_info *lowlevel_ref(const struct lowlevel_device_info * const cinfo)
  52. {
  53. struct lowlevel_device_info * const info = (void*)cinfo;
  54. ++info->ref;
  55. return info;
  56. }
  57. void lowlevel_scan_free()
  58. {
  59. if (!devinfo_list)
  60. return;
  61. struct lowlevel_device_info *info, *tmp;
  62. struct lowlevel_device_info *info2, *tmp2;
  63. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  64. {
  65. LL_DELETE(devinfo_list, info);
  66. LL_FOREACH_SAFE2(info, info2, tmp2, same_devid_next)
  67. {
  68. LL_DELETE2(info, info2, same_devid_next);
  69. lowlevel_devinfo_free(info2);
  70. }
  71. }
  72. }
  73. struct lowlevel_device_info *lowlevel_scan()
  74. {
  75. struct lowlevel_device_info *devinfo_mid_list;
  76. lowlevel_scan_free();
  77. #ifdef HAVE_LIBUSB
  78. devinfo_mid_list = lowl_usb.devinfo_scan();
  79. LL_CONCAT(devinfo_list, devinfo_mid_list);
  80. #endif
  81. #ifdef USE_X6500
  82. devinfo_mid_list = lowl_ft232r.devinfo_scan();
  83. LL_CONCAT(devinfo_list, devinfo_mid_list);
  84. #endif
  85. #ifdef NEED_BFG_LOWL_HID
  86. devinfo_mid_list = lowl_hid.devinfo_scan();
  87. LL_CONCAT(devinfo_list, devinfo_mid_list);
  88. #endif
  89. #ifdef USE_NANOFURY
  90. devinfo_mid_list = lowl_mcp2210.devinfo_scan();
  91. LL_CONCAT(devinfo_list, devinfo_mid_list);
  92. #endif
  93. #ifdef NEED_BFG_LOWL_VCOM
  94. devinfo_mid_list = lowl_vcom.devinfo_scan();
  95. LL_CONCAT(devinfo_list, devinfo_mid_list);
  96. #endif
  97. struct lowlevel_device_info *devinfo_same_prev_ht = NULL, *devinfo_same_list;
  98. LL_FOREACH(devinfo_list, devinfo_mid_list)
  99. {
  100. // Check for devid overlapping, and build a secondary linked list for them, only including the devid in the main list once (high level to low level)
  101. HASH_FIND_STR(devinfo_same_prev_ht, devinfo_mid_list->devid, devinfo_same_list);
  102. if (devinfo_same_list)
  103. {
  104. HASH_DEL(devinfo_same_prev_ht, devinfo_same_list);
  105. LL_DELETE(devinfo_list, devinfo_same_list);
  106. }
  107. LL_PREPEND2(devinfo_same_list, devinfo_mid_list, same_devid_next);
  108. HASH_ADD_KEYPTR(hh, devinfo_same_prev_ht, devinfo_mid_list->devid, strlen(devinfo_mid_list->devid), devinfo_same_list);
  109. applog(LOG_DEBUG, "%s: Found %s device at %s (path=%s, vid=%04x, pid=%04x, manuf=%s, prod=%s, serial=%s)",
  110. __func__,
  111. devinfo_mid_list->lowl->dname,
  112. devinfo_mid_list->devid,
  113. devinfo_mid_list->path,
  114. (unsigned)devinfo_mid_list->vid, (unsigned)devinfo_mid_list->pid,
  115. devinfo_mid_list->manufacturer, devinfo_mid_list->product, devinfo_mid_list->serial);
  116. }
  117. HASH_CLEAR(hh, devinfo_same_prev_ht);
  118. return devinfo_list;
  119. }
  120. bool _lowlevel_match_product(const struct lowlevel_device_info * const info, const char ** const needles)
  121. {
  122. if (!info->product)
  123. return false;
  124. for (int i = 0; needles[i]; ++i)
  125. if (!strstr(info->product, needles[i]))
  126. return false;
  127. return true;
  128. }
  129. bool lowlevel_match_id(const struct lowlevel_device_info * const info, const struct lowlevel_driver * const lowl, const int32_t vid, const int32_t pid)
  130. {
  131. if (info->lowl != lowl)
  132. return false;
  133. if (vid != -1 && vid != info->vid)
  134. return false;
  135. if (pid != -1 && pid != info->pid)
  136. return false;
  137. return true;
  138. }
  139. #define DETECT_BEGIN \
  140. struct lowlevel_device_info *info, *tmp; \
  141. int found = 0; \
  142. \
  143. LL_FOREACH_SAFE(devinfo_list, info, tmp) \
  144. { \
  145. // END DETECT_BEGIN
  146. #define DETECT_END \
  147. if (!cb(info, userp)) \
  148. continue; \
  149. LL_DELETE(devinfo_list, info); \
  150. ++found; \
  151. } \
  152. return found; \
  153. // END DETECT_END
  154. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void * const userp)
  155. {
  156. DETECT_BEGIN
  157. if (serial && ((!info->serial) || strcmp(serial, info->serial)))
  158. continue;
  159. if (product_needles[0] && !_lowlevel_match_product(info, product_needles))
  160. continue;
  161. DETECT_END
  162. }
  163. int lowlevel_detect_id(const lowl_found_devinfo_func_t cb, void * const userp, const struct lowlevel_driver * const lowl, const int32_t vid, const int32_t pid)
  164. {
  165. DETECT_BEGIN
  166. if (!lowlevel_match_id(info, lowl, vid, pid))
  167. continue;
  168. DETECT_END
  169. }
  170. struct _device_claim {
  171. struct device_drv *drv;
  172. char *devpath;
  173. UT_hash_handle hh;
  174. };
  175. struct device_drv *bfg_claim_any(struct device_drv * const api, const char *verbose, const char * const devpath)
  176. {
  177. static struct _device_claim *claims = NULL;
  178. static pthread_mutex_t claims_lock = PTHREAD_MUTEX_INITIALIZER;
  179. struct _device_claim *c;
  180. mutex_lock(&claims_lock);
  181. HASH_FIND_STR(claims, devpath, c);
  182. if (c)
  183. {
  184. mutex_unlock(&claims_lock);
  185. if (verbose && opt_debug)
  186. {
  187. char logbuf[LOGBUFSIZ];
  188. logbuf[0] = '\0';
  189. if (api)
  190. tailsprintf(logbuf, sizeof(logbuf), "%s device ", api->dname);
  191. if (verbose[0])
  192. tailsprintf(logbuf, sizeof(logbuf), "%s (%s)", verbose, devpath);
  193. else
  194. tailsprintf(logbuf, sizeof(logbuf), "%s", devpath);
  195. tailsprintf(logbuf, sizeof(logbuf), " already claimed by ");
  196. if (api)
  197. tailsprintf(logbuf, sizeof(logbuf), "other ");
  198. tailsprintf(logbuf, sizeof(logbuf), "driver: %s", c->drv->dname);
  199. _applog(LOG_DEBUG, logbuf);
  200. }
  201. return c->drv;
  202. }
  203. if (!api)
  204. {
  205. mutex_unlock(&claims_lock);
  206. return NULL;
  207. }
  208. c = malloc(sizeof(*c));
  209. c->devpath = strdup(devpath);
  210. c->drv = api;
  211. HASH_ADD_KEYPTR(hh, claims, c->devpath, strlen(devpath), c);
  212. mutex_unlock(&claims_lock);
  213. return NULL;
  214. }
  215. struct device_drv *bfg_claim_any2(struct device_drv * const api, const char * const verbose, const char * const llname, const char * const path)
  216. {
  217. const size_t llnamesz = strlen(llname);
  218. const size_t pathsz = strlen(path);
  219. char devpath[llnamesz + 1 + pathsz + 1];
  220. memcpy(devpath, llname, llnamesz);
  221. devpath[llnamesz] = ':';
  222. memcpy(&devpath[llnamesz+1], path, pathsz + 1);
  223. return bfg_claim_any(api, verbose, devpath);
  224. }