fpgautils.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 <errno.h>
  16. #include <termios.h>
  17. #include <sys/stat.h>
  18. #include <sys/ioctl.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 "logging.h"
  33. #include "miner.h"
  34. #include "fpgautils.h"
  35. #ifdef HAVE_LIBUDEV
  36. int serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  37. {
  38. struct udev *udev = udev_new();
  39. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  40. struct udev_list_entry *list_entry;
  41. char found = 0;
  42. udev_enumerate_add_match_subsystem(enumerate, "tty");
  43. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  44. udev_enumerate_scan_devices(enumerate);
  45. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  46. struct udev_device *device = udev_device_new_from_syspath(
  47. udev_enumerate_get_udev(enumerate),
  48. udev_list_entry_get_name(list_entry)
  49. );
  50. if (!device)
  51. continue;
  52. const char *devpath = udev_device_get_devnode(device);
  53. if (devpath && detectone(devpath))
  54. ++found;
  55. udev_device_unref(device);
  56. }
  57. udev_enumerate_unref(enumerate);
  58. udev_unref(udev);
  59. return found;
  60. }
  61. #else
  62. int serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  63. {
  64. return 0;
  65. }
  66. #endif
  67. int serial_autodetect_devserial(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  68. {
  69. #ifndef WIN32
  70. DIR *D;
  71. struct dirent *de;
  72. const char udevdir[] = "/dev/serial/by-id";
  73. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  74. char *devfile = devpath + sizeof(udevdir);
  75. char found = 0;
  76. D = opendir(udevdir);
  77. if (!D)
  78. return 0;
  79. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  80. devpath[sizeof(udevdir) - 1] = '/';
  81. while ( (de = readdir(D)) ) {
  82. if (!strstr(de->d_name, prodname))
  83. continue;
  84. strcpy(devfile, de->d_name);
  85. if (detectone(devpath))
  86. ++found;
  87. }
  88. closedir(D);
  89. return found;
  90. #else
  91. return 0;
  92. #endif
  93. }
  94. int _serial_detect(struct device_api *api, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  95. {
  96. struct string_elist *iter, *tmp;
  97. const char *dev, *colon;
  98. bool inhibitauto = false;
  99. char found = 0;
  100. size_t namel = strlen(api->name);
  101. size_t dnamel = strlen(api->dname);
  102. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  103. dev = iter->string;
  104. if ((colon = strchr(dev, ':')) && colon[1] != '\0') {
  105. size_t idlen = colon - dev;
  106. // allow either name:device or dname:device
  107. if ((idlen != namel || strncasecmp(dev, api->name, idlen))
  108. && (idlen != dnamel || strncasecmp(dev, api->dname, idlen)))
  109. continue;
  110. dev = colon + 1;
  111. }
  112. if (!strcmp(dev, "auto"))
  113. forceauto = true;
  114. else if (!strcmp(dev, "noauto"))
  115. inhibitauto = true;
  116. else if (detectone(dev)) {
  117. string_elist_del(iter);
  118. inhibitauto = true;
  119. ++found;
  120. }
  121. }
  122. if ((forceauto || !inhibitauto) && autoscan)
  123. found += autoscan();
  124. return found;
  125. }
  126. // This code is purely for debugging but is very useful for that
  127. // It also took quite a bit of effort so I left it in
  128. // #define TERMIOS_DEBUG 1
  129. // Here to include it at compile time
  130. // It's off by default
  131. #ifndef WIN32
  132. #ifdef TERMIOS_DEBUG
  133. #define BITSSET "Y"
  134. #define BITSNOTSET "N"
  135. int tiospeed(speed_t speed)
  136. {
  137. switch (speed) {
  138. case B0:
  139. return 0;
  140. case B50:
  141. return 50;
  142. case B75:
  143. return 75;
  144. case B110:
  145. return 110;
  146. case B134:
  147. return 134;
  148. case B150:
  149. return 150;
  150. case B200:
  151. return 200;
  152. case B300:
  153. return 300;
  154. case B600:
  155. return 600;
  156. case B1200:
  157. return 1200;
  158. case B1800:
  159. return 1800;
  160. case B2400:
  161. return 2400;
  162. case B4800:
  163. return 4800;
  164. case B9600:
  165. return 9600;
  166. case B19200:
  167. return 19200;
  168. case B38400:
  169. return 38400;
  170. case B57600:
  171. return 57600;
  172. case B115200:
  173. return 115200;
  174. case B230400:
  175. return 230400;
  176. case B460800:
  177. return 460800;
  178. case B500000:
  179. return 500000;
  180. case B576000:
  181. return 576000;
  182. case B921600:
  183. return 921600;
  184. case B1000000:
  185. return 1000000;
  186. case B1152000:
  187. return 1152000;
  188. case B1500000:
  189. return 1500000;
  190. case B2000000:
  191. return 2000000;
  192. case B2500000:
  193. return 2500000;
  194. case B3000000:
  195. return 3000000;
  196. case B3500000:
  197. return 3500000;
  198. case B4000000:
  199. return 4000000;
  200. default:
  201. return -1;
  202. }
  203. }
  204. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  205. {
  206. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  207. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  208. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  209. 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",
  210. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  211. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  212. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  213. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  214. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  215. #define VALO(b) (my_termios->c_oflag | (b))
  216. 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",
  217. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  218. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  219. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  220. VALO(VTDLY), VALO(FFDLY));
  221. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  222. #define VALC(b) (my_termios->c_cflag | (b))
  223. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  224. #ifdef LOBLK
  225. " LOBLK=%s"
  226. #endif
  227. " CMSPAR=%s CRTSCTS=%s",
  228. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  229. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  230. #ifdef LOBLK
  231. ISSETC(LOBLK),
  232. #endif
  233. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  234. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  235. 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"
  236. #ifdef DEFECHO
  237. " DEFECHO=%s"
  238. #endif
  239. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  240. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  241. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  242. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  243. #ifdef DEFECHO
  244. ISSETL(DEFECHO),
  245. #endif
  246. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  247. ISSETL(IEXTEN));
  248. #define VALCC(b) (my_termios->c_cc[b])
  249. 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"
  250. #ifdef VSWTCH
  251. " VSWTCH=0x%02x"
  252. #endif
  253. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  254. #ifdef VDSUSP
  255. " VDSUSP=0x%02x"
  256. #endif
  257. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  258. #ifdef VSTATUS
  259. " VSTATUS=0x%02x"
  260. #endif
  261. ,
  262. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  263. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  264. VALCC(VEOL2),
  265. #ifdef VSWTCH
  266. VALCC(VSWTCH),
  267. #endif
  268. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  269. #ifdef VDSUSP
  270. VALCC(VDSUSP),
  271. #endif
  272. VALCC(VLNEXT), VALCC(VWERASE),
  273. VALCC(VREPRINT), VALCC(VDISCARD)
  274. #ifdef VSTATUS
  275. ,VALCC(VSTATUS)
  276. #endif
  277. );
  278. }
  279. #endif
  280. #endif
  281. int serial_open(const char *devpath, unsigned long baud, signed short timeout, bool purge)
  282. {
  283. #ifdef WIN32
  284. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  285. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  286. {
  287. DWORD e = GetLastError();
  288. switch (e) {
  289. case ERROR_ACCESS_DENIED:
  290. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  291. break;
  292. case ERROR_SHARING_VIOLATION:
  293. applog(LOG_ERR, "%s is already in use by another process", devpath);
  294. break;
  295. default:
  296. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  297. break;
  298. }
  299. return -1;
  300. }
  301. // thanks to af_newbie for pointers about this
  302. COMMCONFIG comCfg = {0};
  303. comCfg.dwSize = sizeof(COMMCONFIG);
  304. comCfg.wVersion = 1;
  305. comCfg.dcb.DCBlength = sizeof(DCB);
  306. comCfg.dcb.BaudRate = baud;
  307. comCfg.dcb.fBinary = 1;
  308. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  309. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  310. comCfg.dcb.ByteSize = 8;
  311. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  312. // Code must specify a valid timeout value (0 means don't timeout)
  313. const DWORD ctoms = (timeout * 100);
  314. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  315. SetCommTimeouts(hSerial, &cto);
  316. if (purge) {
  317. PurgeComm(hSerial, PURGE_RXABORT);
  318. PurgeComm(hSerial, PURGE_TXABORT);
  319. PurgeComm(hSerial, PURGE_RXCLEAR);
  320. PurgeComm(hSerial, PURGE_TXCLEAR);
  321. }
  322. return _open_osfhandle((LONG)hSerial, 0);
  323. #else
  324. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  325. if (unlikely(fdDev == -1))
  326. {
  327. if (errno == EACCES)
  328. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  329. else
  330. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  331. return -1;
  332. }
  333. struct termios my_termios;
  334. tcgetattr(fdDev, &my_termios);
  335. #ifdef TERMIOS_DEBUG
  336. termios_debug(devpath, &my_termios, "before");
  337. #endif
  338. switch (baud) {
  339. case 0:
  340. break;
  341. case 57600:
  342. cfsetispeed(&my_termios, B57600);
  343. cfsetospeed(&my_termios, B57600);
  344. break;
  345. case 115200:
  346. cfsetispeed(&my_termios, B115200);
  347. cfsetospeed(&my_termios, B115200);
  348. break;
  349. // TODO: try some higher speeds with the Icarus and BFL to see
  350. // if they support them and if setting them makes any difference
  351. // N.B. B3000000 doesn't work on Icarus
  352. default:
  353. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  354. }
  355. my_termios.c_cflag |= CS8;
  356. my_termios.c_cflag |= CREAD;
  357. #ifdef USE_AVALON
  358. my_termios.c_cflag |= CRTSCTS;
  359. #endif
  360. my_termios.c_cflag |= CLOCAL;
  361. my_termios.c_cflag &= ~(CSIZE | PARENB);
  362. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  363. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  364. my_termios.c_oflag &= ~OPOST;
  365. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  366. // Code must specify a valid timeout value (0 means don't timeout)
  367. my_termios.c_cc[VTIME] = (cc_t)timeout;
  368. my_termios.c_cc[VMIN] = 0;
  369. #ifdef TERMIOS_DEBUG
  370. termios_debug(devpath, &my_termios, "settings");
  371. #endif
  372. tcsetattr(fdDev, TCSANOW, &my_termios);
  373. #ifdef TERMIOS_DEBUG
  374. tcgetattr(fdDev, &my_termios);
  375. termios_debug(devpath, &my_termios, "after");
  376. #endif
  377. if (purge)
  378. tcflush(fdDev, TCIOFLUSH);
  379. return fdDev;
  380. #endif
  381. }
  382. ssize_t _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  383. {
  384. ssize_t len, tlen = 0;
  385. while (bufsiz) {
  386. len = read(fd, buf, eol ? 1 : bufsiz);
  387. if (unlikely(len == -1))
  388. break;
  389. tlen += len;
  390. if (eol && *eol == buf[0])
  391. break;
  392. buf += len;
  393. bufsiz -= len;
  394. }
  395. return tlen;
  396. }
  397. static FILE *_open_bitstream(const char *path, const char *subdir, const char *filename)
  398. {
  399. char fullpath[PATH_MAX];
  400. strcpy(fullpath, path);
  401. strcat(fullpath, "/");
  402. if (subdir) {
  403. strcat(fullpath, subdir);
  404. strcat(fullpath, "/");
  405. }
  406. strcat(fullpath, filename);
  407. return fopen(fullpath, "rb");
  408. }
  409. #define _open_bitstream(path, subdir) do { \
  410. f = _open_bitstream(path, subdir, filename); \
  411. if (f) \
  412. return f; \
  413. } while(0)
  414. #define _open_bitstream3(path) do { \
  415. _open_bitstream(path, dname); \
  416. _open_bitstream(path, "bitstreams"); \
  417. _open_bitstream(path, NULL); \
  418. } while(0)
  419. FILE *open_bitstream(const char *dname, const char *filename)
  420. {
  421. FILE *f;
  422. _open_bitstream3(opt_kernel_path);
  423. _open_bitstream3(cgminer_path);
  424. _open_bitstream3(".");
  425. return NULL;
  426. }
  427. #ifndef WIN32
  428. static bool _select_wait_read(int fd, struct timeval *timeout)
  429. {
  430. fd_set rfds;
  431. FD_ZERO(&rfds);
  432. FD_SET(fd, &rfds);
  433. if (select(fd+1, &rfds, NULL, NULL, timeout) > 0)
  434. return true;
  435. else
  436. return false;
  437. }
  438. // Default timeout 100ms - only for device initialisation
  439. const struct timeval tv_timeout_default = { 0, 100000 };
  440. // Default inter character timeout = 1ms - only for device initialisation
  441. const struct timeval tv_inter_char_default = { 0, 1000 };
  442. // Device initialisation function - NOT for work processing
  443. size_t _select_read(int fd, char *buf, size_t bufsiz, struct timeval *timeout, struct timeval *char_timeout, int finished)
  444. {
  445. struct timeval tv_time, tv_char;
  446. ssize_t siz, red = 0;
  447. char got;
  448. // timeout is the maximum time to wait for the first character
  449. tv_time.tv_sec = timeout->tv_sec;
  450. tv_time.tv_usec = timeout->tv_usec;
  451. if (!_select_wait_read(fd, &tv_time))
  452. return 0;
  453. while (4242) {
  454. if ((siz = read(fd, buf, 1)) < 0)
  455. return red;
  456. got = *buf;
  457. buf += siz;
  458. red += siz;
  459. bufsiz -= siz;
  460. if (bufsiz < 1 || (finished >= 0 && got == finished))
  461. return red;
  462. // char_timeout is the maximum time to wait for each subsequent character
  463. // this is OK for initialisation, but bad for work processing
  464. // work processing MUST have a fixed size so this doesn't come into play
  465. tv_char.tv_sec = char_timeout->tv_sec;
  466. tv_char.tv_usec = char_timeout->tv_usec;
  467. if (!_select_wait_read(fd, &tv_char))
  468. return red;
  469. }
  470. return red;
  471. }
  472. // Device initialisation function - NOT for work processing
  473. size_t _select_write(int fd, char *buf, size_t siz, struct timeval *timeout)
  474. {
  475. struct timeval tv_time, tv_now, tv_finish;
  476. fd_set rfds;
  477. ssize_t wrote = 0, ret;
  478. gettimeofday(&tv_now, NULL);
  479. timeradd(&tv_now, timeout, &tv_finish);
  480. // timeout is the maximum time to spend trying to write
  481. tv_time.tv_sec = timeout->tv_sec;
  482. tv_time.tv_usec = timeout->tv_usec;
  483. FD_ZERO(&rfds);
  484. FD_SET(fd, &rfds);
  485. while (siz > 0 && (tv_now.tv_sec < tv_finish.tv_sec || (tv_now.tv_sec == tv_finish.tv_sec && tv_now.tv_usec < tv_finish.tv_usec)) && select(fd+1, NULL, &rfds, NULL, &tv_time) > 0) {
  486. if ((ret = write(fd, buf, 1)) > 0) {
  487. buf++;
  488. wrote++;
  489. siz--;
  490. }
  491. else if (ret < 0)
  492. return wrote;
  493. gettimeofday(&tv_now, NULL);
  494. }
  495. return wrote;
  496. }
  497. int get_serial_cts(int fd)
  498. {
  499. int status;
  500. if (!fd)
  501. return -1;
  502. ioctl(fd, TIOCMSET, &status);
  503. return status & TIOCM_CTS;
  504. }
  505. #endif // ! WIN32