fpgautils.c 15 KB

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