lowlevel.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <stdlib.h>
  11. #include <string.h>
  12. #include <utlist.h>
  13. #include "logging.h"
  14. #include "lowlevel.h"
  15. static struct lowlevel_device_info *devinfo_list;
  16. void lowlevel_devinfo_free(struct lowlevel_device_info * const info)
  17. {
  18. if (info->lowl->devinfo_free)
  19. info->lowl->devinfo_free(info);
  20. free(info->manufacturer);
  21. free(info->product);
  22. free(info->serial);
  23. free(info->path);
  24. free(info->devid);
  25. free(info);
  26. }
  27. void lowlevel_scan_free()
  28. {
  29. if (!devinfo_list)
  30. return;
  31. struct lowlevel_device_info *info, *tmp;
  32. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  33. {
  34. LL_DELETE(devinfo_list, info);
  35. lowlevel_devinfo_free(info);
  36. }
  37. }
  38. void lowlevel_scan()
  39. {
  40. struct lowlevel_device_info *devinfo_mid_list;
  41. lowlevel_scan_free();
  42. #ifdef USE_X6500
  43. devinfo_mid_list = lowl_ft232r.devinfo_scan();
  44. LL_CONCAT(devinfo_list, devinfo_mid_list);
  45. #endif
  46. #ifdef USE_NANOFURY
  47. devinfo_mid_list = lowl_mcp2210.devinfo_scan();
  48. LL_CONCAT(devinfo_list, devinfo_mid_list);
  49. #endif
  50. #ifdef HAVE_FPGAUTILS
  51. devinfo_mid_list = lowl_vcom.devinfo_scan();
  52. LL_CONCAT(devinfo_list, devinfo_mid_list);
  53. #endif
  54. LL_FOREACH(devinfo_list, devinfo_mid_list)
  55. {
  56. applog(LOG_DEBUG, "%s: Found %s (path=%s, manuf=%s, prod=%s, serial=%s)",
  57. __func__,
  58. devinfo_mid_list->devid,
  59. devinfo_mid_list->path,
  60. devinfo_mid_list->manufacturer, devinfo_mid_list->product, devinfo_mid_list->serial);
  61. }
  62. }
  63. int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void *userp)
  64. {
  65. struct lowlevel_device_info *info, *tmp;
  66. int found = 0, i;
  67. LL_FOREACH_SAFE(devinfo_list, info, tmp)
  68. {
  69. if (serial && ((!info->serial) || strcmp(serial, info->serial)))
  70. continue;
  71. if (product_needles[0] && !info->product)
  72. continue;
  73. for (i = 0; product_needles[i]; ++i)
  74. if (!strstr(info->product, product_needles[i]))
  75. goto next;
  76. if (!cb(info, userp))
  77. continue;
  78. LL_DELETE(devinfo_list, info);
  79. ++found;
  80. next: ;
  81. }
  82. return found;
  83. }