lowlevel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #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 NEED_BFG_LOWL_FTDI
  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_PCI
  94. devinfo_mid_list = lowl_pci.devinfo_scan();
  95. LL_CONCAT(devinfo_list, devinfo_mid_list);
  96. #endif
  97. #ifdef NEED_BFG_LOWL_VCOM
  98. devinfo_mid_list = lowl_vcom.devinfo_scan();
  99. LL_CONCAT(devinfo_list, devinfo_mid_list);
  100. #endif
  101. struct lowlevel_device_info *devinfo_same_prev_ht = NULL, *devinfo_same_list;
  102. LL_FOREACH(devinfo_list, devinfo_mid_list)
  103. {
  104. // 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)
  105. HASH_FIND_STR(devinfo_same_prev_ht, devinfo_mid_list->devid, devinfo_same_list);
  106. if (devinfo_same_list)
  107. {
  108. HASH_DEL(devinfo_same_prev_ht, devinfo_same_list);
  109. LL_DELETE(devinfo_list, devinfo_same_list);
  110. }
  111. LL_PREPEND2(devinfo_same_list, devinfo_mid_list, same_devid_next);
  112. HASH_ADD_KEYPTR(hh, devinfo_same_prev_ht, devinfo_mid_list->devid, strlen(devinfo_mid_list->devid), devinfo_same_list);
  113. applog(LOG_DEBUG, "%s: Found %s device at %s (path=%s, vid=%04x, pid=%04x, manuf=%s, prod=%s, serial=%s)",
  114. __func__,
  115. devinfo_mid_list->lowl->dname,
  116. devinfo_mid_list->devid,
  117. devinfo_mid_list->path,
  118. (unsigned)devinfo_mid_list->vid, (unsigned)devinfo_mid_list->pid,
  119. devinfo_mid_list->manufacturer, devinfo_mid_list->product, devinfo_mid_list->serial);
  120. }
  121. HASH_CLEAR(hh, devinfo_same_prev_ht);
  122. return devinfo_list;
  123. }
  124. bool _lowlevel_match_product(const struct lowlevel_device_info * const info, const char ** const needles)
  125. {
  126. if (!info->product)
  127. return false;
  128. for (int i = 0; needles[i]; ++i)
  129. if (!strstr(info->product, needles[i]))
  130. return false;
  131. return true;
  132. }
  133. 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)
  134. {
  135. if (info->lowl != lowl)
  136. return false;
  137. if (vid != -1 && vid != info->vid)
  138. return false;
  139. if (pid != -1 && pid != info->pid)
  140. return false;
  141. return true;
  142. }
  143. #define DETECT_BEGIN \
  144. struct lowlevel_device_info *info, *tmp; \
  145. int found = 0; \
  146. \
  147. LL_FOREACH_SAFE(devinfo_list, info, tmp) \
  148. { \
  149. // END DETECT_BEGIN
  150. #define DETECT_END \
  151. if (!cb(info, userp)) \
  152. continue; \
  153. LL_DELETE(devinfo_list, info); \
  154. ++found; \
  155. } \
  156. return found; \
  157. // END DETECT_END
  158. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void * const userp)
  159. {
  160. DETECT_BEGIN
  161. if (serial && ((!info->serial) || strcmp(serial, info->serial)))
  162. continue;
  163. if (product_needles[0] && !_lowlevel_match_product(info, product_needles))
  164. continue;
  165. DETECT_END
  166. }
  167. 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)
  168. {
  169. DETECT_BEGIN
  170. if (!lowlevel_match_id(info, lowl, vid, pid))
  171. continue;
  172. DETECT_END
  173. }
  174. struct _device_claim {
  175. struct device_drv *drv;
  176. char *devpath;
  177. UT_hash_handle hh;
  178. };
  179. struct device_drv *bfg_claim_any(struct device_drv * const api, const char *verbose, const char * const devpath)
  180. {
  181. static struct _device_claim *claims = NULL;
  182. static pthread_mutex_t claims_lock = PTHREAD_MUTEX_INITIALIZER;
  183. struct _device_claim *c;
  184. mutex_lock(&claims_lock);
  185. HASH_FIND_STR(claims, devpath, c);
  186. if (c)
  187. {
  188. mutex_unlock(&claims_lock);
  189. if (verbose && opt_debug)
  190. {
  191. char logbuf[LOGBUFSIZ];
  192. logbuf[0] = '\0';
  193. if (api)
  194. tailsprintf(logbuf, sizeof(logbuf), "%s device ", api->dname);
  195. if (verbose[0])
  196. tailsprintf(logbuf, sizeof(logbuf), "%s (%s)", verbose, devpath);
  197. else
  198. tailsprintf(logbuf, sizeof(logbuf), "%s", devpath);
  199. tailsprintf(logbuf, sizeof(logbuf), " already claimed by ");
  200. if (api)
  201. tailsprintf(logbuf, sizeof(logbuf), "other ");
  202. tailsprintf(logbuf, sizeof(logbuf), "driver: %s", c->drv->dname);
  203. _applog(LOG_DEBUG, logbuf);
  204. }
  205. return c->drv;
  206. }
  207. if (!api)
  208. {
  209. mutex_unlock(&claims_lock);
  210. return NULL;
  211. }
  212. c = malloc(sizeof(*c));
  213. c->devpath = strdup(devpath);
  214. c->drv = api;
  215. HASH_ADD_KEYPTR(hh, claims, c->devpath, strlen(devpath), c);
  216. mutex_unlock(&claims_lock);
  217. return NULL;
  218. }
  219. struct device_drv *bfg_claim_any2(struct device_drv * const api, const char * const verbose, const char * const llname, const char * const path)
  220. {
  221. const size_t llnamesz = strlen(llname);
  222. const size_t pathsz = strlen(path);
  223. char devpath[llnamesz + 1 + pathsz + 1];
  224. memcpy(devpath, llname, llnamesz);
  225. devpath[llnamesz] = ':';
  226. memcpy(&devpath[llnamesz+1], path, pathsz + 1);
  227. return bfg_claim_any(api, verbose, devpath);
  228. }