fpgautils.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <errno.h>
  16. #include <termios.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #ifndef O_CLOEXEC
  21. #define O_CLOEXEC 0
  22. #endif
  23. #else
  24. #include <windows.h>
  25. #include <io.h>
  26. #endif
  27. #ifdef HAVE_LIBUDEV
  28. #include <libudev.h>
  29. #endif
  30. #include "elist.h"
  31. #include "fpgautils.h"
  32. #include "logging.h"
  33. #include "miner.h"
  34. #ifdef HAVE_LIBUDEV
  35. char
  36. serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  37. {
  38. struct udev *udev = udev_new();
  39. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  40. struct udev_list_entry *list_entry;
  41. char found = 0;
  42. udev_enumerate_add_match_subsystem(enumerate, "tty");
  43. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  44. udev_enumerate_scan_devices(enumerate);
  45. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  46. struct udev_device *device = udev_device_new_from_syspath(
  47. udev_enumerate_get_udev(enumerate),
  48. udev_list_entry_get_name(list_entry)
  49. );
  50. if (!device)
  51. continue;
  52. const char *devpath = udev_device_get_devnode(device);
  53. if (devpath && detectone(devpath))
  54. ++found;
  55. udev_device_unref(device);
  56. }
  57. udev_enumerate_unref(enumerate);
  58. udev_unref(udev);
  59. return found;
  60. }
  61. #else
  62. char
  63. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  64. {
  65. return 0;
  66. }
  67. #endif
  68. char
  69. serial_autodetect_devserial(detectone_func_t detectone, const char*prodname)
  70. {
  71. #ifndef WIN32
  72. DIR *D;
  73. struct dirent *de;
  74. const char udevdir[] = "/dev/serial/by-id";
  75. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  76. char *devfile = devpath + sizeof(udevdir);
  77. char found = 0;
  78. D = opendir(udevdir);
  79. if (!D)
  80. return 0;
  81. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  82. devpath[sizeof(udevdir) - 1] = '/';
  83. while ( (de = readdir(D)) ) {
  84. if (!strstr(de->d_name, prodname))
  85. continue;
  86. strcpy(devfile, de->d_name);
  87. if (detectone(devpath))
  88. ++found;
  89. }
  90. closedir(D);
  91. return found;
  92. #else
  93. return 0;
  94. #endif
  95. }
  96. char
  97. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  98. {
  99. struct string_elist *iter, *tmp;
  100. const char*s, *p;
  101. bool inhibitauto = false;
  102. char found = 0;
  103. size_t dnamel = strlen(dname);
  104. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  105. s = iter->string;
  106. if ((p = strchr(s, ':')) && p[1] != '\0') {
  107. size_t plen = p - s;
  108. if (plen != dnamel || strncasecmp(s, dname, plen))
  109. continue;
  110. s = p + 1;
  111. }
  112. if (!strcmp(s, "auto"))
  113. forceauto = true;
  114. else
  115. if (!strcmp(s, "noauto"))
  116. inhibitauto = true;
  117. else
  118. if (detectone(s)) {
  119. string_elist_del(iter);
  120. inhibitauto = true;
  121. ++found;
  122. }
  123. }
  124. if ((forceauto || !inhibitauto) && autoscan)
  125. found += autoscan();
  126. return found;
  127. }
  128. int
  129. serial_open(const char*devpath, unsigned long baud, signed short timeout, bool purge)
  130. {
  131. #ifdef WIN32
  132. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  133. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  134. {
  135. DWORD e = GetLastError();
  136. switch (e) {
  137. case ERROR_ACCESS_DENIED:
  138. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  139. break;
  140. case ERROR_SHARING_VIOLATION:
  141. applog(LOG_ERR, "%s is already in use by another process", devpath);
  142. break;
  143. default:
  144. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  145. break;
  146. }
  147. return -1;
  148. }
  149. // thanks to af_newbie for pointers about this
  150. COMMCONFIG comCfg = {0};
  151. comCfg.dwSize = sizeof(COMMCONFIG);
  152. comCfg.wVersion = 1;
  153. comCfg.dcb.DCBlength = sizeof(DCB);
  154. comCfg.dcb.BaudRate = baud;
  155. comCfg.dcb.fBinary = 1;
  156. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  157. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  158. comCfg.dcb.ByteSize = 8;
  159. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  160. // Code must specify a valid timeout value (0 means don't timeout)
  161. const DWORD ctoms = (timeout * 100);
  162. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  163. SetCommTimeouts(hSerial, &cto);
  164. if (purge) {
  165. PurgeComm(hSerial, PURGE_RXABORT);
  166. PurgeComm(hSerial, PURGE_TXABORT);
  167. PurgeComm(hSerial, PURGE_RXCLEAR);
  168. PurgeComm(hSerial, PURGE_TXCLEAR);
  169. }
  170. return _open_osfhandle((LONG)hSerial, 0);
  171. #else
  172. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  173. if (unlikely(fdDev == -1))
  174. {
  175. if (errno == EACCES)
  176. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  177. else
  178. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  179. return -1;
  180. }
  181. struct termios my_termios;
  182. tcgetattr(fdDev, &my_termios);
  183. switch (baud) {
  184. case 0:
  185. break;
  186. case 57600:
  187. cfsetispeed( &my_termios, B57600 );
  188. cfsetospeed( &my_termios, B57600 );
  189. break;
  190. case 115200:
  191. cfsetispeed( &my_termios, B115200 );
  192. cfsetospeed( &my_termios, B115200 );
  193. break;
  194. // TODO: try some higher speeds with the Icarus and BFL to see
  195. // if they support them and if setting them makes any difference
  196. default:
  197. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  198. }
  199. my_termios.c_cflag |= CS8;
  200. my_termios.c_cflag |= CREAD;
  201. my_termios.c_cflag |= CLOCAL;
  202. my_termios.c_cflag &= ~(CSIZE | PARENB);
  203. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  204. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  205. my_termios.c_oflag &= ~OPOST;
  206. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  207. // Code must specify a valid timeout value (0 means don't timeout)
  208. my_termios.c_cc[VTIME] = (cc_t)timeout;
  209. my_termios.c_cc[VMIN] = 0;
  210. tcsetattr(fdDev, TCSANOW, &my_termios);
  211. if (purge)
  212. tcflush(fdDev, TCIOFLUSH);
  213. return fdDev;
  214. #endif
  215. }
  216. ssize_t
  217. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  218. {
  219. ssize_t len, tlen = 0;
  220. while (bufsiz) {
  221. len = read(fd, buf, eol ? 1 : bufsiz);
  222. if (len < 1)
  223. break;
  224. tlen += len;
  225. if (eol && *eol == buf[0])
  226. break;
  227. buf += len;
  228. bufsiz -= len;
  229. }
  230. return tlen;
  231. }
  232. static FILE*
  233. _open_bitstream(const char*path, const char*subdir, const char*filename)
  234. {
  235. char fullpath[PATH_MAX];
  236. strcpy(fullpath, path);
  237. strcat(fullpath, "/");
  238. if (subdir) {
  239. strcat(fullpath, subdir);
  240. strcat(fullpath, "/");
  241. }
  242. strcat(fullpath, filename);
  243. return fopen(fullpath, "rb");
  244. }
  245. #define _open_bitstream(path, subdir) do { \
  246. f = _open_bitstream(path, subdir, filename); \
  247. if (f) \
  248. return f; \
  249. } while(0)
  250. #define _open_bitstream3(path) do { \
  251. _open_bitstream(path, dname); \
  252. _open_bitstream(path, "bitstreams"); \
  253. _open_bitstream(path, NULL); \
  254. } while(0)
  255. FILE*
  256. open_bitstream(const char*dname, const char*filename)
  257. {
  258. FILE *f;
  259. _open_bitstream3(opt_kernel_path);
  260. _open_bitstream3(cgminer_path);
  261. _open_bitstream3(".");
  262. return NULL;
  263. }