lowlevel.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_DELETE(info, info2);
  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. return devinfo_list;
  118. }
  119. bool _lowlevel_match_product(const struct lowlevel_device_info * const info, const char ** const needles)
  120. {
  121. if (!info->product)
  122. return false;
  123. for (int i = 0; needles[i]; ++i)
  124. if (!strstr(info->product, needles[i]))
  125. return false;
  126. return true;
  127. }
  128. 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)
  129. {
  130. if (info->lowl != lowl)
  131. return false;
  132. if (vid != -1 && vid != info->vid)
  133. return false;
  134. if (pid != -1 && pid != info->pid)
  135. return false;
  136. return true;
  137. }
  138. #define DETECT_BEGIN \
  139. struct lowlevel_device_info *info, *tmp; \
  140. int found = 0; \
  141. \
  142. LL_FOREACH_SAFE(devinfo_list, info, tmp) \
  143. { \
  144. // END DETECT_BEGIN
  145. #define DETECT_END \
  146. if (!cb(info, userp)) \
  147. continue; \
  148. LL_DELETE(devinfo_list, info); \
  149. ++found; \
  150. } \
  151. return found; \
  152. // END DETECT_END
  153. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void * const userp)
  154. {
  155. DETECT_BEGIN
  156. if (serial && ((!info->serial) || strcmp(serial, info->serial)))
  157. continue;
  158. if (product_needles[0] && !_lowlevel_match_product(info, product_needles))
  159. continue;
  160. DETECT_END
  161. }
  162. 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)
  163. {
  164. DETECT_BEGIN
  165. if (!lowlevel_match_id(info, lowl, vid, pid))
  166. continue;
  167. DETECT_END
  168. }
  169. struct _device_claim {
  170. struct device_drv *drv;
  171. char *devpath;
  172. UT_hash_handle hh;
  173. };
  174. struct device_drv *bfg_claim_any(struct device_drv * const api, const char *verbose, const char * const devpath)
  175. {
  176. static struct _device_claim *claims = NULL;
  177. struct _device_claim *c;
  178. HASH_FIND_STR(claims, devpath, c);
  179. if (c)
  180. {
  181. if (verbose && opt_debug)
  182. {
  183. char logbuf[LOGBUFSIZ];
  184. logbuf[0] = '\0';
  185. if (api)
  186. tailsprintf(logbuf, sizeof(logbuf), "%s device ", api->dname);
  187. if (verbose[0])
  188. tailsprintf(logbuf, sizeof(logbuf), "%s (%s)", verbose, devpath);
  189. else
  190. tailsprintf(logbuf, sizeof(logbuf), "%s", devpath);
  191. tailsprintf(logbuf, sizeof(logbuf), " already claimed by ");
  192. if (api)
  193. tailsprintf(logbuf, sizeof(logbuf), "other ");
  194. tailsprintf(logbuf, sizeof(logbuf), "driver: %s", c->drv->dname);
  195. _applog(LOG_DEBUG, logbuf);
  196. }
  197. return c->drv;
  198. }
  199. if (!api)
  200. return NULL;
  201. c = malloc(sizeof(*c));
  202. c->devpath = strdup(devpath);
  203. c->drv = api;
  204. HASH_ADD_KEYPTR(hh, claims, c->devpath, strlen(devpath), c);
  205. return NULL;
  206. }
  207. struct device_drv *bfg_claim_any2(struct device_drv * const api, const char * const verbose, const char * const llname, const char * const path)
  208. {
  209. const size_t llnamesz = strlen(llname);
  210. const size_t pathsz = strlen(path);
  211. char devpath[llnamesz + 1 + pathsz + 1];
  212. memcpy(devpath, llname, llnamesz);
  213. devpath[llnamesz] = ':';
  214. memcpy(&devpath[llnamesz+1], path, pathsz + 1);
  215. return bfg_claim_any(api, verbose, devpath);
  216. }