fpgautils.c 14 KB

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