fpgautils.c 6.0 KB

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