mcp2210.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <dlfcn.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <hidapi/hidapi.h> /* FIXME */
  15. #include <utlist.h>
  16. #include "logging.h"
  17. #include "lowlevel.h"
  18. #define MCP2210_IDVENDOR 0x04d8
  19. #define MCP2210_IDPRODUCT 0x00de
  20. #ifdef WIN32
  21. #define HID_API_EXPORT __declspec(dllexport)
  22. #else
  23. #define HID_API_EXPORT /* */
  24. #endif
  25. struct hid_device_info HID_API_EXPORT *(*dlsym_hid_enumerate)(unsigned short, unsigned short);
  26. #define hid_enumerate dlsym_hid_enumerate
  27. void HID_API_EXPORT (*dlsym_hid_free_enumeration)(struct hid_device_info *);
  28. #define hid_free_enumeration dlsym_hid_free_enumeration
  29. #define LOAD_SYM(sym) do { \
  30. if (!(dlsym_ ## sym = dlsym(dlh, #sym))) { \
  31. applog(LOG_DEBUG, "%s: Failed to load %s in %s", __func__, #sym, dlname); \
  32. goto fail; \
  33. } \
  34. } while(0)
  35. static
  36. bool hidapi_try_lib(const char * const dlname)
  37. {
  38. struct hid_device_info *hid_enum;
  39. void *dlh;
  40. dlh = dlopen(dlname, RTLD_NOW);
  41. if (!dlh)
  42. {
  43. applog(LOG_DEBUG, "%s: Couldn't load %s: %s", __func__, dlname, dlerror());
  44. return false;
  45. }
  46. LOAD_SYM(hid_enumerate);
  47. hid_enum = hid_enumerate(0, 0);
  48. if (!hid_enum)
  49. {
  50. applog(LOG_DEBUG, "%s: Loaded %s, but no devices enumerated; trying other libraries", __func__, dlname);
  51. goto fail;
  52. }
  53. LOAD_SYM(hid_free_enumeration);
  54. hid_free_enumeration(hid_enum);
  55. applog(LOG_DEBUG, "%s: Successfully loaded %s", __func__, dlname);
  56. return true;
  57. fail:
  58. dlclose(dlh);
  59. return false;
  60. }
  61. static
  62. bool hidapi_load_library()
  63. {
  64. if (dlsym_hid_free_enumeration)
  65. return true;
  66. const char **p;
  67. char dlname[23] = "libhidapi";
  68. const char *dltry[] = {
  69. "",
  70. "-0",
  71. "-hidraw",
  72. "-libusb",
  73. NULL
  74. };
  75. for (p = &dltry[0]; *p; ++p)
  76. {
  77. sprintf(&dlname[9], "%s.%s", *p,
  78. #ifdef WIN32
  79. "dll"
  80. #else
  81. "so"
  82. #endif
  83. );
  84. if (hidapi_try_lib(dlname))
  85. return true;
  86. }
  87. return false;
  88. }
  89. static
  90. void mcp2210_devinfo_free(struct lowlevel_device_info * const info)
  91. {
  92. free(info->lowl_data);
  93. }
  94. static
  95. char *wcs2str_dup(wchar_t *ws)
  96. {
  97. char tmp, *rv;
  98. int clen;
  99. clen = snprintf(&tmp, 1, "%ls", ws);
  100. ++clen;
  101. rv = malloc(clen);
  102. snprintf(rv, clen, "%ls", ws);
  103. return rv;
  104. }
  105. static
  106. struct lowlevel_device_info *mcp2210_devinfo_scan()
  107. {
  108. if (!hidapi_load_library())
  109. {
  110. applog(LOG_DEBUG, "%s: Failed to load any hidapi library", __func__);
  111. return NULL;
  112. }
  113. struct hid_device_info *hid_enum, *hid_item;
  114. struct lowlevel_device_info *info, *devinfo_list = NULL;
  115. hid_enum = hid_enumerate(MCP2210_IDVENDOR, MCP2210_IDPRODUCT);
  116. if (!hid_enum)
  117. {
  118. applog(LOG_DEBUG, "%s: No MCP2210 devices found", __func__);
  119. return NULL;
  120. }
  121. LL_FOREACH(hid_enum, hid_item)
  122. {
  123. info = malloc(sizeof(struct lowlevel_device_info));
  124. *info = (struct lowlevel_device_info){
  125. .lowl = &lowl_mcp2210,
  126. .lowl_data = strdup(hid_item->path),
  127. .product = wcs2str_dup(hid_item->product_string),
  128. .serial = wcs2str_dup(hid_item->serial_number),
  129. };
  130. LL_PREPEND(devinfo_list, info);
  131. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  132. __func__, info->product, info->serial);
  133. }
  134. hid_free_enumeration(hid_enum);
  135. return devinfo_list;
  136. }
  137. struct lowlevel_driver lowl_mcp2210 = {
  138. .devinfo_scan = mcp2210_devinfo_scan,
  139. .devinfo_free = mcp2210_devinfo_free,
  140. };