fpgautils.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Andrew Smith
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <sys/types.h>
  12. #include <dirent.h>
  13. #include <string.h>
  14. #ifndef WIN32
  15. #include <termios.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #ifndef O_CLOEXEC
  20. #define O_CLOEXEC 0
  21. #endif
  22. #else
  23. #include <windows.h>
  24. #include <io.h>
  25. #endif
  26. #ifdef HAVE_LIBUDEV
  27. #include <libudev.h>
  28. #endif
  29. #include "elist.h"
  30. #include "fpgautils.h"
  31. #include "logging.h"
  32. #include "miner.h"
  33. #ifdef HAVE_LIBUDEV
  34. char
  35. serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  36. {
  37. struct udev *udev = udev_new();
  38. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  39. struct udev_list_entry *list_entry;
  40. char found = 0;
  41. udev_enumerate_add_match_subsystem(enumerate, "tty");
  42. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  43. udev_enumerate_scan_devices(enumerate);
  44. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  45. struct udev_device *device = udev_device_new_from_syspath(
  46. udev_enumerate_get_udev(enumerate),
  47. udev_list_entry_get_name(list_entry)
  48. );
  49. if (!device)
  50. continue;
  51. const char *devpath = udev_device_get_devnode(device);
  52. if (devpath && detectone(devpath))
  53. ++found;
  54. udev_device_unref(device);
  55. }
  56. udev_enumerate_unref(enumerate);
  57. udev_unref(udev);
  58. return found;
  59. }
  60. #else
  61. char
  62. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  63. {
  64. return 0;
  65. }
  66. #endif
  67. char
  68. serial_autodetect_devserial(detectone_func_t detectone, const char*prodname)
  69. {
  70. #ifndef WIN32
  71. DIR *D;
  72. struct dirent *de;
  73. const char udevdir[] = "/dev/serial/by-id";
  74. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  75. char *devfile = devpath + sizeof(udevdir);
  76. char found = 0;
  77. D = opendir(udevdir);
  78. if (!D)
  79. return 0;
  80. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  81. devpath[sizeof(udevdir) - 1] = '/';
  82. while ( (de = readdir(D)) ) {
  83. if (!strstr(de->d_name, prodname))
  84. continue;
  85. strcpy(devfile, de->d_name);
  86. if (detectone(devpath))
  87. ++found;
  88. }
  89. closedir(D);
  90. return found;
  91. #else
  92. return 0;
  93. #endif
  94. }
  95. char
  96. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  97. {
  98. struct string_elist *iter, *tmp;
  99. const char*s, *p;
  100. bool inhibitauto = false;
  101. char found = 0;
  102. size_t dnamel = strlen(dname);
  103. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  104. s = iter->string;
  105. if ((p = strchr(s, ':')) && p[1] != '\0') {
  106. size_t plen = p - s;
  107. if (plen != dnamel || strncasecmp(s, dname, plen))
  108. continue;
  109. s = p + 1;
  110. }
  111. if (!strcmp(s, "auto"))
  112. forceauto = true;
  113. else
  114. if (!strcmp(s, "noauto"))
  115. inhibitauto = true;
  116. else
  117. if (detectone(s)) {
  118. string_elist_del(iter);
  119. inhibitauto = true;
  120. ++found;
  121. }
  122. }
  123. if ((forceauto || !inhibitauto) && autoscan)
  124. found += autoscan();
  125. return found;
  126. }
  127. int
  128. serial_open(const char*devpath, unsigned long baud, signed short timeout, bool purge)
  129. {
  130. #ifdef WIN32
  131. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  132. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  133. return -1;
  134. // thanks to af_newbie for pointers about this
  135. COMMCONFIG comCfg = {0};
  136. comCfg.dwSize = sizeof(COMMCONFIG);
  137. comCfg.wVersion = 1;
  138. comCfg.dcb.DCBlength = sizeof(DCB);
  139. comCfg.dcb.BaudRate = baud;
  140. comCfg.dcb.fBinary = 1;
  141. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  142. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  143. comCfg.dcb.ByteSize = 8;
  144. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  145. const DWORD ctoms = (timeout == -1) ? 30000 : (timeout * 100);
  146. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  147. SetCommTimeouts(hSerial, &cto);
  148. if (purge) {
  149. PurgeComm(hSerial, PURGE_RXABORT);
  150. PurgeComm(hSerial, PURGE_TXABORT);
  151. PurgeComm(hSerial, PURGE_RXCLEAR);
  152. PurgeComm(hSerial, PURGE_TXCLEAR);
  153. }
  154. return _open_osfhandle((LONG)hSerial, 0);
  155. #else
  156. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  157. if (unlikely(fdDev == -1))
  158. return -1;
  159. struct termios pattr;
  160. tcgetattr(fdDev, &pattr);
  161. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  162. pattr.c_oflag &= ~OPOST;
  163. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  164. pattr.c_cflag &= ~(CSIZE | PARENB);
  165. pattr.c_cflag |= CS8;
  166. switch (baud) {
  167. case 0: break;
  168. case 115200: pattr.c_cflag = B115200; break;
  169. default:
  170. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  171. }
  172. pattr.c_cflag |= CREAD | CLOCAL;
  173. if (timeout >= 0) {
  174. pattr.c_cc[VTIME] = (cc_t)timeout;
  175. pattr.c_cc[VMIN] = 0;
  176. }
  177. tcsetattr(fdDev, TCSANOW, &pattr);
  178. if (purge)
  179. tcflush(fdDev, TCIOFLUSH);
  180. return fdDev;
  181. #endif
  182. }
  183. ssize_t
  184. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  185. {
  186. ssize_t len, tlen = 0;
  187. while (bufsiz) {
  188. len = read(fd, buf, eol ? 1 : bufsiz);
  189. if (len < 1)
  190. break;
  191. tlen += len;
  192. if (eol && *eol == buf[0])
  193. break;
  194. buf += len;
  195. bufsiz -= len;
  196. }
  197. return tlen;
  198. }
  199. static FILE*
  200. _open_bitstream(const char*path, const char*subdir, const char*filename)
  201. {
  202. char fullpath[PATH_MAX];
  203. strcpy(fullpath, path);
  204. strcat(fullpath, "/");
  205. if (subdir) {
  206. strcat(fullpath, subdir);
  207. strcat(fullpath, "/");
  208. }
  209. strcat(fullpath, filename);
  210. return fopen(fullpath, "rb");
  211. }
  212. #define _open_bitstream(path, subdir) do { \
  213. f = _open_bitstream(path, subdir, filename); \
  214. if (f) \
  215. return f; \
  216. } while(0)
  217. #define _open_bitstream3(path) do { \
  218. _open_bitstream(path, dname); \
  219. _open_bitstream(path, "bitstreams"); \
  220. _open_bitstream(path, NULL); \
  221. } while(0)
  222. FILE*
  223. open_bitstream(const char*dname, const char*filename)
  224. {
  225. FILE *f;
  226. _open_bitstream3(opt_kernel_path);
  227. _open_bitstream3(cgminer_path);
  228. _open_bitstream3(".");
  229. return NULL;
  230. }