fpgautils.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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*dnamec, size_t dnamel, 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;
  111. bool inhibitauto = false;
  112. char found = 0;
  113. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  114. s = iter->string;
  115. if (!strncmp(dnamec, iter->string, dnamel))
  116. s += dnamel;
  117. if (!strcmp(s, "auto"))
  118. forceauto = true;
  119. else
  120. if (!strcmp(s, "noauto"))
  121. inhibitauto = true;
  122. else
  123. if (detectone(s)) {
  124. string_elist_del(iter);
  125. inhibitauto = true;
  126. ++found;
  127. if (total_devices == MAX_DEVICES)
  128. break;
  129. }
  130. }
  131. if ((forceauto || !inhibitauto) && autoscan && total_devices < MAX_DEVICES)
  132. found += autoscan();
  133. return found;
  134. }
  135. int
  136. serial_open(const char*devpath, unsigned long baud, signed short timeout, bool purge)
  137. {
  138. #ifdef WIN32
  139. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  140. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  141. return -1;
  142. // thanks to af_newbie for pointers about this
  143. COMMCONFIG comCfg = {0};
  144. comCfg.dwSize = sizeof(COMMCONFIG);
  145. comCfg.wVersion = 1;
  146. comCfg.dcb.DCBlength = sizeof(DCB);
  147. comCfg.dcb.BaudRate = baud;
  148. comCfg.dcb.fBinary = 1;
  149. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  150. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  151. comCfg.dcb.ByteSize = 8;
  152. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  153. const DWORD ctoms = (timeout == -1) ? 30000 : (timeout * 100);
  154. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  155. SetCommTimeouts(hSerial, &cto);
  156. if (purge) {
  157. PurgeComm(hSerial, PURGE_RXABORT);
  158. PurgeComm(hSerial, PURGE_TXABORT);
  159. PurgeComm(hSerial, PURGE_RXCLEAR);
  160. PurgeComm(hSerial, PURGE_TXCLEAR);
  161. }
  162. return _open_osfhandle((LONG)hSerial, 0);
  163. #else
  164. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  165. if (unlikely(fdDev == -1))
  166. return -1;
  167. struct termios pattr;
  168. tcgetattr(fdDev, &pattr);
  169. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  170. pattr.c_oflag &= ~OPOST;
  171. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  172. pattr.c_cflag &= ~(CSIZE | PARENB);
  173. pattr.c_cflag |= CS8;
  174. switch (baud) {
  175. case 0: break;
  176. case 115200: pattr.c_cflag = B115200; break;
  177. default:
  178. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  179. }
  180. pattr.c_cflag |= CREAD | CLOCAL;
  181. if (timeout >= 0) {
  182. pattr.c_cc[VTIME] = (cc_t)timeout;
  183. pattr.c_cc[VMIN] = 0;
  184. }
  185. tcsetattr(fdDev, TCSANOW, &pattr);
  186. if (purge)
  187. tcflush(fdDev, TCIOFLUSH);
  188. return fdDev;
  189. #endif
  190. }
  191. ssize_t
  192. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  193. {
  194. ssize_t len, tlen = 0;
  195. while (bufsiz) {
  196. len = read(fd, buf, eol ? 1 : bufsiz);
  197. if (len < 1)
  198. break;
  199. tlen += len;
  200. if (eol && *eol == buf[0])
  201. break;
  202. buf += len;
  203. bufsiz -= len;
  204. }
  205. return tlen;
  206. }
  207. static FILE*
  208. _open_bitstream(const char*path, const char*subdir, const char*filename)
  209. {
  210. char fullpath[PATH_MAX];
  211. strcpy(fullpath, path);
  212. strcat(fullpath, "/");
  213. if (subdir) {
  214. strcat(fullpath, subdir);
  215. strcat(fullpath, "/");
  216. }
  217. strcat(fullpath, filename);
  218. return fopen(fullpath, "rb");
  219. }
  220. #define _open_bitstream(path, subdir) do { \
  221. f = _open_bitstream(path, subdir, filename); \
  222. if (f) \
  223. return f; \
  224. } while(0)
  225. #define _open_bitstream3(path) do { \
  226. _open_bitstream(path, dname); \
  227. _open_bitstream(path, "bitstreams"); \
  228. _open_bitstream(path, NULL); \
  229. } while(0)
  230. FILE*
  231. open_bitstream(const char*dname, const char*filename)
  232. {
  233. FILE *f;
  234. _open_bitstream3(opt_kernel_path);
  235. _open_bitstream3(cgminer_path);
  236. _open_bitstream3(".");
  237. return NULL;
  238. }