fpgautils.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. if (total_devices == MAX_DEVICES)
  38. return 0;
  39. struct udev *udev = udev_new();
  40. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  41. struct udev_list_entry *list_entry;
  42. char found = 0;
  43. udev_enumerate_add_match_subsystem(enumerate, "tty");
  44. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  45. udev_enumerate_scan_devices(enumerate);
  46. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  47. struct udev_device *device = udev_device_new_from_syspath(
  48. udev_enumerate_get_udev(enumerate),
  49. udev_list_entry_get_name(list_entry)
  50. );
  51. if (!device)
  52. continue;
  53. const char *devpath = udev_device_get_devnode(device);
  54. if (devpath && detectone(devpath))
  55. ++found;
  56. udev_device_unref(device);
  57. if (total_devices == MAX_DEVICES)
  58. break;
  59. }
  60. udev_enumerate_unref(enumerate);
  61. udev_unref(udev);
  62. return found;
  63. }
  64. #else
  65. char
  66. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  67. {
  68. return 0;
  69. }
  70. #endif
  71. char
  72. serial_autodetect_devserial(detectone_func_t detectone, const char*prodname)
  73. {
  74. #ifndef WIN32
  75. if (total_devices == MAX_DEVICES)
  76. return 0;
  77. DIR *D;
  78. struct dirent *de;
  79. const char udevdir[] = "/dev/serial/by-id";
  80. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  81. char *devfile = devpath + sizeof(udevdir);
  82. char found = 0;
  83. D = opendir(udevdir);
  84. if (!D)
  85. return 0;
  86. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  87. devpath[sizeof(udevdir) - 1] = '/';
  88. while ( (de = readdir(D)) ) {
  89. if (!strstr(de->d_name, prodname))
  90. continue;
  91. strcpy(devfile, de->d_name);
  92. if (detectone(devpath)) {
  93. ++found;
  94. if (total_devices == MAX_DEVICES)
  95. break;
  96. }
  97. }
  98. closedir(D);
  99. return found;
  100. #else
  101. return 0;
  102. #endif
  103. }
  104. char
  105. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  106. {
  107. if (total_devices == MAX_DEVICES)
  108. return 0;
  109. struct string_elist *iter, *tmp;
  110. const char*s, *p;
  111. bool inhibitauto = false;
  112. char found = 0;
  113. size_t dnamel = strlen(dname);
  114. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  115. s = iter->string;
  116. if ((p = strchr(s, ':')) && p[1] != '\0') {
  117. size_t plen = p - s;
  118. if (plen != dnamel || strncasecmp(s, dname, plen))
  119. continue;
  120. s = p + 1;
  121. }
  122. if (!strcmp(s, "auto"))
  123. forceauto = true;
  124. else
  125. if (!strcmp(s, "noauto"))
  126. inhibitauto = true;
  127. else
  128. if (detectone(s)) {
  129. string_elist_del(iter);
  130. inhibitauto = true;
  131. ++found;
  132. if (total_devices == MAX_DEVICES)
  133. break;
  134. }
  135. }
  136. if ((forceauto || !inhibitauto) && autoscan && total_devices < MAX_DEVICES)
  137. found += autoscan();
  138. return found;
  139. }
  140. int
  141. serial_open(const char*devpath, unsigned long baud, signed short timeout, bool purge)
  142. {
  143. #ifdef WIN32
  144. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  145. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  146. return -1;
  147. // thanks to af_newbie for pointers about this
  148. COMMCONFIG comCfg = {0};
  149. comCfg.dwSize = sizeof(COMMCONFIG);
  150. comCfg.wVersion = 1;
  151. comCfg.dcb.DCBlength = sizeof(DCB);
  152. comCfg.dcb.BaudRate = baud;
  153. comCfg.dcb.fBinary = 1;
  154. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  155. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  156. comCfg.dcb.ByteSize = 8;
  157. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  158. const DWORD ctoms = (timeout == -1) ? 30000 : (timeout * 100);
  159. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  160. SetCommTimeouts(hSerial, &cto);
  161. if (purge) {
  162. PurgeComm(hSerial, PURGE_RXABORT);
  163. PurgeComm(hSerial, PURGE_TXABORT);
  164. PurgeComm(hSerial, PURGE_RXCLEAR);
  165. PurgeComm(hSerial, PURGE_TXCLEAR);
  166. }
  167. return _open_osfhandle((LONG)hSerial, 0);
  168. #else
  169. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  170. if (unlikely(fdDev == -1))
  171. return -1;
  172. struct termios pattr;
  173. tcgetattr(fdDev, &pattr);
  174. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  175. pattr.c_oflag &= ~OPOST;
  176. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  177. pattr.c_cflag &= ~(CSIZE | PARENB);
  178. pattr.c_cflag |= CS8;
  179. switch (baud) {
  180. case 0: break;
  181. case 115200: pattr.c_cflag = B115200; break;
  182. default:
  183. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  184. }
  185. pattr.c_cflag |= CREAD | CLOCAL;
  186. if (timeout >= 0) {
  187. pattr.c_cc[VTIME] = (cc_t)timeout;
  188. pattr.c_cc[VMIN] = 0;
  189. }
  190. tcsetattr(fdDev, TCSANOW, &pattr);
  191. if (purge)
  192. tcflush(fdDev, TCIOFLUSH);
  193. return fdDev;
  194. #endif
  195. }
  196. ssize_t
  197. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  198. {
  199. ssize_t len, tlen = 0;
  200. while (bufsiz) {
  201. len = read(fd, buf, eol ? 1 : bufsiz);
  202. if (len < 1)
  203. break;
  204. tlen += len;
  205. if (eol && *eol == buf[0])
  206. break;
  207. buf += len;
  208. bufsiz -= len;
  209. }
  210. return tlen;
  211. }
  212. static FILE*
  213. _open_bitstream(const char*path, const char*subdir, const char*filename)
  214. {
  215. char fullpath[PATH_MAX];
  216. strcpy(fullpath, path);
  217. strcat(fullpath, "/");
  218. if (subdir) {
  219. strcat(fullpath, subdir);
  220. strcat(fullpath, "/");
  221. }
  222. strcat(fullpath, filename);
  223. return fopen(fullpath, "rb");
  224. }
  225. #define _open_bitstream(path, subdir) do { \
  226. f = _open_bitstream(path, subdir, filename); \
  227. if (f) \
  228. return f; \
  229. } while(0)
  230. #define _open_bitstream3(path) do { \
  231. _open_bitstream(path, dname); \
  232. _open_bitstream(path, "bitstreams"); \
  233. _open_bitstream(path, NULL); \
  234. } while(0)
  235. FILE*
  236. open_bitstream(const char*dname, const char*filename)
  237. {
  238. FILE *f;
  239. _open_bitstream3(opt_kernel_path);
  240. _open_bitstream3(cgminer_path);
  241. _open_bitstream3(".");
  242. return NULL;
  243. }