fpgautils.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 my_termios;
  160. tcgetattr(fdDev, &my_termios);
  161. switch (baud) {
  162. case 0: break;
  163. case 115200: my_termios.c_cflag = B115200; break;
  164. default:
  165. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  166. }
  167. my_termios.c_cflag |= CS8;
  168. my_termios.c_cflag |= CREAD;
  169. my_termios.c_cflag |= CLOCAL;
  170. my_termios.c_cflag &= ~(CSIZE | PARENB);
  171. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  172. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  173. my_termios.c_oflag &= ~OPOST;
  174. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  175. if (timeout >= 0) {
  176. my_termios.c_cc[VTIME] = (cc_t)timeout;
  177. my_termios.c_cc[VMIN] = 0;
  178. }
  179. tcsetattr(fdDev, TCSANOW, &my_termios);
  180. if (purge)
  181. tcflush(fdDev, TCIOFLUSH);
  182. return fdDev;
  183. #endif
  184. }
  185. ssize_t
  186. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  187. {
  188. ssize_t len, tlen = 0;
  189. while (bufsiz) {
  190. len = read(fd, buf, eol ? 1 : bufsiz);
  191. if (len < 1)
  192. break;
  193. tlen += len;
  194. if (eol && *eol == buf[0])
  195. break;
  196. buf += len;
  197. bufsiz -= len;
  198. }
  199. return tlen;
  200. }
  201. static FILE*
  202. _open_bitstream(const char*path, const char*subdir, const char*filename)
  203. {
  204. char fullpath[PATH_MAX];
  205. strcpy(fullpath, path);
  206. strcat(fullpath, "/");
  207. if (subdir) {
  208. strcat(fullpath, subdir);
  209. strcat(fullpath, "/");
  210. }
  211. strcat(fullpath, filename);
  212. return fopen(fullpath, "rb");
  213. }
  214. #define _open_bitstream(path, subdir) do { \
  215. f = _open_bitstream(path, subdir, filename); \
  216. if (f) \
  217. return f; \
  218. } while(0)
  219. #define _open_bitstream3(path) do { \
  220. _open_bitstream(path, dname); \
  221. _open_bitstream(path, "bitstreams"); \
  222. _open_bitstream(path, NULL); \
  223. } while(0)
  224. FILE*
  225. open_bitstream(const char*dname, const char*filename)
  226. {
  227. FILE *f;
  228. _open_bitstream3(opt_kernel_path);
  229. _open_bitstream3(cgminer_path);
  230. _open_bitstream3(".");
  231. return NULL;
  232. }