fpgautils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <stdint.h>
  12. #include <sys/types.h>
  13. #include <dirent.h>
  14. #include <string.h>
  15. #ifndef WIN32
  16. #include <errno.h>
  17. #include <termios.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #ifndef O_CLOEXEC
  22. #define O_CLOEXEC 0
  23. #endif
  24. #else
  25. #include <windows.h>
  26. #include <io.h>
  27. #endif
  28. #ifdef HAVE_LIBUDEV
  29. #include <libudev.h>
  30. #endif
  31. #include "elist.h"
  32. #include "fpgautils.h"
  33. #include "logging.h"
  34. #include "miner.h"
  35. #ifdef HAVE_LIBUDEV
  36. int
  37. serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  38. {
  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. }
  58. udev_enumerate_unref(enumerate);
  59. udev_unref(udev);
  60. return found;
  61. }
  62. #else
  63. int
  64. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  65. {
  66. return 0;
  67. }
  68. #endif
  69. int
  70. serial_autodetect_devserial(detectone_func_t detectone, const char*prodname)
  71. {
  72. #ifndef WIN32
  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. }
  91. closedir(D);
  92. return found;
  93. #else
  94. return 0;
  95. #endif
  96. }
  97. int
  98. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  99. {
  100. struct string_elist *iter, *tmp;
  101. const char*s, *p;
  102. bool inhibitauto = false;
  103. char found = 0;
  104. size_t dnamel = strlen(dname);
  105. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  106. s = iter->string;
  107. if ((p = strchr(s, ':')) && p[1] != '\0') {
  108. size_t plen = p - s;
  109. if (plen != dnamel || strncasecmp(s, dname, plen))
  110. continue;
  111. s = p + 1;
  112. }
  113. if (!strcmp(s, "auto"))
  114. forceauto = true;
  115. else
  116. if (!strcmp(s, "noauto"))
  117. inhibitauto = true;
  118. else
  119. if (!detectone)
  120. {} // do nothing
  121. else
  122. if (detectone(s)) {
  123. string_elist_del(iter);
  124. inhibitauto = true;
  125. ++found;
  126. }
  127. }
  128. if ((forceauto || !inhibitauto) && autoscan)
  129. found += autoscan();
  130. return found;
  131. }
  132. // This code is purely for debugging but is very useful for that
  133. // It also took quite a bit of effort so I left it in
  134. // #define TERMIOS_DEBUG 1
  135. // Here to include it at compile time
  136. // It's off by default
  137. #ifndef WIN32
  138. #ifdef TERMIOS_DEBUG
  139. #define BITSSET "Y"
  140. #define BITSNOTSET "N"
  141. int tiospeed(speed_t speed)
  142. {
  143. switch (speed) {
  144. case B0:
  145. return 0;
  146. case B50:
  147. return 50;
  148. case B75:
  149. return 75;
  150. case B110:
  151. return 110;
  152. case B134:
  153. return 134;
  154. case B150:
  155. return 150;
  156. case B200:
  157. return 200;
  158. case B300:
  159. return 300;
  160. case B600:
  161. return 600;
  162. case B1200:
  163. return 1200;
  164. case B1800:
  165. return 1800;
  166. case B2400:
  167. return 2400;
  168. case B4800:
  169. return 4800;
  170. case B9600:
  171. return 9600;
  172. case B19200:
  173. return 19200;
  174. case B38400:
  175. return 38400;
  176. case B57600:
  177. return 57600;
  178. case B115200:
  179. return 115200;
  180. case B230400:
  181. return 230400;
  182. case B460800:
  183. return 460800;
  184. case B500000:
  185. return 500000;
  186. case B576000:
  187. return 576000;
  188. case B921600:
  189. return 921600;
  190. case B1000000:
  191. return 1000000;
  192. case B1152000:
  193. return 1152000;
  194. case B1500000:
  195. return 1500000;
  196. case B2000000:
  197. return 2000000;
  198. case B2500000:
  199. return 2500000;
  200. case B3000000:
  201. return 3000000;
  202. case B3500000:
  203. return 3500000;
  204. case B4000000:
  205. return 4000000;
  206. default:
  207. return -1;
  208. }
  209. }
  210. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  211. {
  212. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  213. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  214. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  215. applog(LOG_DEBUG, "TIOS: c_iflag: IGNBRK=%s BRKINT=%s IGNPAR=%s PARMRK=%s INPCK=%s ISTRIP=%s INLCR=%s IGNCR=%s ICRNL=%s IUCLC=%s IXON=%s IXANY=%s IOFF=%s IMAXBEL=%s IUTF8=%s",
  216. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  217. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  218. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  219. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  220. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  221. #define VALO(b) (my_termios->c_oflag | (b))
  222. applog(LOG_DEBUG, "TIOS: c_oflag: OPOST=%s OLCUC=%s ONLCR=%s OCRNL=%s ONOCR=%s ONLRET=%s OFILL=%s OFDEL=%s NLDLY=%d CRDLY=%d TABDLY=%d BSDLY=%d VTDLY=%d FFDLY=%d",
  223. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  224. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  225. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  226. VALO(VTDLY), VALO(FFDLY));
  227. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  228. #define VALC(b) (my_termios->c_cflag | (b))
  229. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  230. #ifdef LOBLK
  231. " LOBLK=%s"
  232. #endif
  233. " CMSPAR=%s CRTSCTS=%s",
  234. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  235. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  236. #ifdef LOBLK
  237. ISSETC(LOBLK),
  238. #endif
  239. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  240. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  241. applog(LOG_DEBUG, "TIOS: c_lflag: ISIG=%s ICANON=%s XCASE=%s ECHO=%s ECHOE=%s ECHOK=%s ECHONL=%s ECHOCTL=%s ECHOPRT=%s ECHOKE=%s"
  242. #ifdef DEFECHO
  243. " DEFECHO=%s"
  244. #endif
  245. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  246. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  247. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  248. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  249. #ifdef DEFECHO
  250. ISSETL(DEFECHO),
  251. #endif
  252. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  253. ISSETL(IEXTEN));
  254. #define VALCC(b) (my_termios->c_cc[b])
  255. applog(LOG_DEBUG, "TIOS: c_cc: VINTR=0x%02x VQUIT=0x%02x VERASE=0x%02x VKILL=0x%02x VEOF=0x%02x VMIN=%u VEOL=0x%02x VTIME=%u VEOL2=0x%02x"
  256. #ifdef VSWTCH
  257. " VSWTCH=0x%02x"
  258. #endif
  259. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  260. #ifdef VDSUSP
  261. " VDSUSP=0x%02x"
  262. #endif
  263. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  264. #ifdef VSTATUS
  265. " VSTATUS=0x%02x"
  266. #endif
  267. ,
  268. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  269. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  270. VALCC(VEOL2),
  271. #ifdef VSWTCH
  272. VALCC(VSWTCH),
  273. #endif
  274. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  275. #ifdef VDSUSP
  276. VALCC(VDSUSP),
  277. #endif
  278. VALCC(VLNEXT), VALCC(VWERASE),
  279. VALCC(VREPRINT), VALCC(VDISCARD)
  280. #ifdef VSTATUS
  281. ,VALCC(VSTATUS)
  282. #endif
  283. );
  284. }
  285. #endif
  286. #endif
  287. /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  288. * this interface buys us warnings when bad constants are passed in.
  289. */
  290. int
  291. serial_open(const char*devpath, unsigned long baud, uint8_t timeout, bool purge)
  292. {
  293. #ifdef WIN32
  294. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  295. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  296. {
  297. DWORD e = GetLastError();
  298. switch (e) {
  299. case ERROR_ACCESS_DENIED:
  300. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  301. break;
  302. case ERROR_SHARING_VIOLATION:
  303. applog(LOG_ERR, "%s is already in use by another process", devpath);
  304. break;
  305. default:
  306. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  307. break;
  308. }
  309. return -1;
  310. }
  311. // thanks to af_newbie for pointers about this
  312. COMMCONFIG comCfg = {0};
  313. comCfg.dwSize = sizeof(COMMCONFIG);
  314. comCfg.wVersion = 1;
  315. comCfg.dcb.DCBlength = sizeof(DCB);
  316. comCfg.dcb.BaudRate = baud;
  317. comCfg.dcb.fBinary = 1;
  318. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  319. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  320. comCfg.dcb.ByteSize = 8;
  321. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  322. // Code must specify a valid timeout value (0 means don't timeout)
  323. const DWORD ctoms = ((DWORD)timeout * 100);
  324. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  325. SetCommTimeouts(hSerial, &cto);
  326. if (purge) {
  327. PurgeComm(hSerial, PURGE_RXABORT);
  328. PurgeComm(hSerial, PURGE_TXABORT);
  329. PurgeComm(hSerial, PURGE_RXCLEAR);
  330. PurgeComm(hSerial, PURGE_TXCLEAR);
  331. }
  332. return _open_osfhandle((LONG)hSerial, 0);
  333. #else
  334. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  335. if (unlikely(fdDev == -1))
  336. {
  337. if (errno == EACCES)
  338. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  339. else
  340. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  341. return -1;
  342. }
  343. struct termios my_termios;
  344. tcgetattr(fdDev, &my_termios);
  345. #ifdef TERMIOS_DEBUG
  346. termios_debug(devpath, &my_termios, "before");
  347. #endif
  348. switch (baud) {
  349. case 0:
  350. break;
  351. case 57600:
  352. cfsetispeed(&my_termios, B57600);
  353. cfsetospeed(&my_termios, B57600);
  354. break;
  355. case 115200:
  356. cfsetispeed(&my_termios, B115200);
  357. cfsetospeed(&my_termios, B115200);
  358. break;
  359. // TODO: try some higher speeds with the Icarus and BFL to see
  360. // if they support them and if setting them makes any difference
  361. // N.B. B3000000 doesn't work on Icarus
  362. default:
  363. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  364. }
  365. my_termios.c_cflag |= CS8;
  366. my_termios.c_cflag |= CREAD;
  367. my_termios.c_cflag |= CLOCAL;
  368. my_termios.c_cflag &= ~(CSIZE | PARENB);
  369. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  370. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  371. my_termios.c_oflag &= ~OPOST;
  372. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  373. // Code must specify a valid timeout value (0 means don't timeout)
  374. my_termios.c_cc[VTIME] = (cc_t)timeout;
  375. my_termios.c_cc[VMIN] = 0;
  376. #ifdef TERMIOS_DEBUG
  377. termios_debug(devpath, &my_termios, "settings");
  378. #endif
  379. tcsetattr(fdDev, TCSANOW, &my_termios);
  380. #ifdef TERMIOS_DEBUG
  381. tcgetattr(fdDev, &my_termios);
  382. termios_debug(devpath, &my_termios, "after");
  383. #endif
  384. if (purge)
  385. tcflush(fdDev, TCIOFLUSH);
  386. return fdDev;
  387. #endif
  388. }
  389. ssize_t
  390. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  391. {
  392. ssize_t len, tlen = 0;
  393. while (bufsiz) {
  394. len = read(fd, buf, eol ? 1 : bufsiz);
  395. if (len < 1)
  396. break;
  397. tlen += len;
  398. if (eol && *eol == buf[0])
  399. break;
  400. buf += len;
  401. bufsiz -= len;
  402. }
  403. return tlen;
  404. }
  405. static FILE*
  406. _open_bitstream(const char*path, const char*subdir, const char*filename)
  407. {
  408. char fullpath[PATH_MAX];
  409. strcpy(fullpath, path);
  410. strcat(fullpath, "/");
  411. if (subdir) {
  412. strcat(fullpath, subdir);
  413. strcat(fullpath, "/");
  414. }
  415. strcat(fullpath, filename);
  416. return fopen(fullpath, "rb");
  417. }
  418. #define _open_bitstream(path, subdir) do { \
  419. f = _open_bitstream(path, subdir, filename); \
  420. if (f) \
  421. return f; \
  422. } while(0)
  423. #define _open_bitstream3(path) do { \
  424. _open_bitstream(path, dname); \
  425. _open_bitstream(path, "bitstreams"); \
  426. _open_bitstream(path, NULL); \
  427. } while(0)
  428. FILE*
  429. open_bitstream(const char*dname, const char*filename)
  430. {
  431. FILE *f;
  432. _open_bitstream3(opt_kernel_path);
  433. _open_bitstream3(cgminer_path);
  434. _open_bitstream3(".");
  435. return NULL;
  436. }