fpgautils.c 6.0 KB

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