fpgautils.c 6.1 KB

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