lowlevel.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. static struct lowlevel_device_info *devinfo_list;
  18. void lowlevel_devinfo_semicpy(struct lowlevel_device_info * const dst, const struct lowlevel_device_info * const src)
  19. {
  20. #define COPYSTR(key) BFGINIT(dst->key, maybe_strdup(src->key))
  21. COPYSTR(manufacturer);
  22. COPYSTR(product);
  23. COPYSTR(serial);
  24. COPYSTR(path);
  25. COPYSTR(devid);
  26. BFGINIT(dst->vid, src->vid);
  27. BFGINIT(dst->pid, src->pid);
  28. }
  29. void lowlevel_devinfo_free(struct lowlevel_device_info * const info)
  30. {
  31. if (info->ref--)
  32. return;
  33. if (info->lowl->devinfo_free)
  34. info->lowl->devinfo_free(info);
  35. free(info->manufacturer);
  36. free(info->product);
  37. free(info->serial);
  38. free(info->path);
  39. free(info->devid);
  40. free(info);
  41. }
  42. struct lowlevel_device_info *lowlevel_ref(const struct lowlevel_device_info * const cinfo)
  43. {
  44. struct lowlevel_device_info * const info = (void*)cinfo;
  45. ++info->ref;
  46. return info;
  47. }
  48. void lowlevel_scan_free()
  49. {
  50. if (!devinfo_list)
  51. return;
  52. struct lowlevel_device_info *info, *tmp;
  53. struct lowlevel_device_info *info2, *tmp2;
  54. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  55. {
  56. LL_DELETE(devinfo_list, info);
  57. LL_FOREACH_SAFE2(info, info2, tmp2, same_devid_next)
  58. {
  59. LL_DELETE(info, info2);
  60. lowlevel_devinfo_free(info2);
  61. }
  62. }
  63. }
  64. struct lowlevel_device_info *lowlevel_scan()
  65. {
  66. struct lowlevel_device_info *devinfo_mid_list;
  67. lowlevel_scan_free();
  68. #ifdef HAVE_LIBUSB
  69. devinfo_mid_list = lowl_usb.devinfo_scan();
  70. LL_CONCAT(devinfo_list, devinfo_mid_list);
  71. #endif
  72. #ifdef USE_X6500
  73. devinfo_mid_list = lowl_ft232r.devinfo_scan();
  74. LL_CONCAT(devinfo_list, devinfo_mid_list);
  75. #endif
  76. #ifdef NEED_BFG_LOWL_HID
  77. devinfo_mid_list = lowl_hid.devinfo_scan();
  78. LL_CONCAT(devinfo_list, devinfo_mid_list);
  79. #endif
  80. #ifdef USE_NANOFURY
  81. devinfo_mid_list = lowl_mcp2210.devinfo_scan();
  82. LL_CONCAT(devinfo_list, devinfo_mid_list);
  83. #endif
  84. #ifdef HAVE_FPGAUTILS
  85. devinfo_mid_list = lowl_vcom.devinfo_scan();
  86. LL_CONCAT(devinfo_list, devinfo_mid_list);
  87. #endif
  88. struct lowlevel_device_info *devinfo_same_prev_ht = NULL, *devinfo_same_list;
  89. LL_FOREACH(devinfo_list, devinfo_mid_list)
  90. {
  91. // 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)
  92. HASH_FIND_STR(devinfo_same_prev_ht, devinfo_mid_list->devid, devinfo_same_list);
  93. if (devinfo_same_list)
  94. {
  95. HASH_DEL(devinfo_same_prev_ht, devinfo_same_list);
  96. LL_DELETE(devinfo_list, devinfo_same_list);
  97. }
  98. LL_PREPEND2(devinfo_same_list, devinfo_mid_list, same_devid_next);
  99. HASH_ADD_KEYPTR(hh, devinfo_same_prev_ht, devinfo_mid_list->devid, strlen(devinfo_mid_list->devid), devinfo_same_list);
  100. applog(LOG_DEBUG, "%s: Found %s device at %s (path=%s, vid=%04x, pid=%04x, manuf=%s, prod=%s, serial=%s)",
  101. __func__,
  102. devinfo_mid_list->lowl->dname,
  103. devinfo_mid_list->devid,
  104. devinfo_mid_list->path,
  105. (unsigned)devinfo_mid_list->vid, (unsigned)devinfo_mid_list->pid,
  106. devinfo_mid_list->manufacturer, devinfo_mid_list->product, devinfo_mid_list->serial);
  107. }
  108. return devinfo_list;
  109. }
  110. bool _lowlevel_match_product(const struct lowlevel_device_info * const info, const char ** const needles)
  111. {
  112. if (!info->product)
  113. return false;
  114. for (int i = 0; needles[i]; ++i)
  115. if (!strstr(info->product, needles[i]))
  116. return false;
  117. return true;
  118. }
  119. 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)
  120. {
  121. if (info->lowl != lowl)
  122. return false;
  123. if (vid != -1 && vid != info->vid)
  124. return false;
  125. if (pid != -1 && pid != info->pid)
  126. return false;
  127. return true;
  128. }
  129. #define DETECT_BEGIN \
  130. struct lowlevel_device_info *info, *tmp; \
  131. int found = 0; \
  132. \
  133. LL_FOREACH_SAFE(devinfo_list, info, tmp) \
  134. { \
  135. // END DETECT_BEGIN
  136. #define DETECT_END \
  137. if (!cb(info, userp)) \
  138. continue; \
  139. LL_DELETE(devinfo_list, info); \
  140. ++found; \
  141. } \
  142. return found; \
  143. // END DETECT_END
  144. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void * const userp)
  145. {
  146. DETECT_BEGIN
  147. if (serial && ((!info->serial) || strcmp(serial, info->serial)))
  148. continue;
  149. if (product_needles[0] && !_lowlevel_match_product(info, product_needles))
  150. continue;
  151. DETECT_END
  152. }
  153. 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)
  154. {
  155. DETECT_BEGIN
  156. if (!lowlevel_match_id(info, lowl, vid, pid))
  157. continue;
  158. DETECT_END
  159. }