fpgautils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 <stdlib.h>
  13. #include <sys/types.h>
  14. #include <dirent.h>
  15. #include <string.h>
  16. #ifndef WIN32
  17. #include <errno.h>
  18. #include <termios.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #ifndef O_CLOEXEC
  23. #define O_CLOEXEC 0
  24. #endif
  25. #else /* WIN32 */
  26. #include <windows.h>
  27. #include <io.h>
  28. #define dlsym (void*)GetProcAddress
  29. #define dlclose FreeLibrary
  30. typedef unsigned long FT_STATUS;
  31. typedef PVOID FT_HANDLE;
  32. __stdcall FT_STATUS (*FT_ListDevices)(PVOID pArg1, PVOID pArg2, DWORD Flags);
  33. __stdcall FT_STATUS (*FT_Open)(int idx, FT_HANDLE*);
  34. __stdcall FT_STATUS (*FT_GetComPortNumber)(FT_HANDLE, LPLONG lplComPortNumber);
  35. __stdcall FT_STATUS (*FT_Close)(FT_HANDLE);
  36. const uint32_t FT_OPEN_BY_DESCRIPTION = 2;
  37. const uint32_t FT_LIST_ALL = 0x20000000;
  38. const uint32_t FT_LIST_NUMBER_ONLY = 0x80000000;
  39. enum {
  40. FT_OK,
  41. };
  42. #endif /* WIN32 */
  43. #ifdef HAVE_LIBUDEV
  44. #include <libudev.h>
  45. #endif
  46. #include "elist.h"
  47. #include "fpgautils.h"
  48. #include "logging.h"
  49. #include "miner.h"
  50. #ifdef HAVE_LIBUDEV
  51. int
  52. serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  53. {
  54. struct udev *udev = udev_new();
  55. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  56. struct udev_list_entry *list_entry;
  57. char found = 0;
  58. udev_enumerate_add_match_subsystem(enumerate, "tty");
  59. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  60. udev_enumerate_scan_devices(enumerate);
  61. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  62. struct udev_device *device = udev_device_new_from_syspath(
  63. udev_enumerate_get_udev(enumerate),
  64. udev_list_entry_get_name(list_entry)
  65. );
  66. if (!device)
  67. continue;
  68. const char *devpath = udev_device_get_devnode(device);
  69. if (devpath && detectone(devpath))
  70. ++found;
  71. udev_device_unref(device);
  72. }
  73. udev_enumerate_unref(enumerate);
  74. udev_unref(udev);
  75. return found;
  76. }
  77. #else
  78. int
  79. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  80. {
  81. return 0;
  82. }
  83. #endif
  84. int
  85. serial_autodetect_devserial(detectone_func_t detectone, const char*prodname)
  86. {
  87. #ifndef WIN32
  88. DIR *D;
  89. struct dirent *de;
  90. const char udevdir[] = "/dev/serial/by-id";
  91. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  92. char *devfile = devpath + sizeof(udevdir);
  93. char found = 0;
  94. D = opendir(udevdir);
  95. if (!D)
  96. return 0;
  97. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  98. devpath[sizeof(udevdir) - 1] = '/';
  99. while ( (de = readdir(D)) ) {
  100. if (!strstr(de->d_name, prodname))
  101. continue;
  102. strcpy(devfile, de->d_name);
  103. if (detectone(devpath))
  104. ++found;
  105. }
  106. closedir(D);
  107. return found;
  108. #else
  109. return 0;
  110. #endif
  111. }
  112. #ifdef WIN32
  113. #define LOAD_SYM(sym) do { \
  114. if (!(sym = dlsym(dll, #sym))) { \
  115. applog(LOG_DEBUG, "Failed to load " #sym ", not using FTDI autodetect"); \
  116. goto out; \
  117. } \
  118. } while(0)
  119. int serial_autodetect_ftdi(detectone_func_t detectone, const char *needle, const char *needle2)
  120. {
  121. char devpath[] = "\\\\.\\COMnnnnn";
  122. char *devpathnum = &devpath[7];
  123. char **bufptrs;
  124. char *buf;
  125. int found = 0;
  126. int i;
  127. FT_STATUS ftStatus;
  128. DWORD numDevs;
  129. HMODULE dll = LoadLibrary("FTD2XX.DLL");
  130. if (!dll) {
  131. applog(LOG_DEBUG, "FTD2XX.DLL failed to load, not using FTDI autodetect");
  132. return 0;
  133. }
  134. LOAD_SYM(FT_ListDevices);
  135. LOAD_SYM(FT_Open);
  136. LOAD_SYM(FT_GetComPortNumber);
  137. LOAD_SYM(FT_Close);
  138. ftStatus = FT_ListDevices(&numDevs, NULL, FT_LIST_NUMBER_ONLY);
  139. if (ftStatus != FT_OK) {
  140. applog(LOG_DEBUG, "FTDI device count failed, not using FTDI autodetect");
  141. goto out;
  142. }
  143. applog(LOG_DEBUG, "FTDI reports %u devices", (unsigned)numDevs);
  144. buf = alloca(65 * numDevs);
  145. bufptrs = alloca(sizeof(*bufptrs) * (numDevs + 1));
  146. for (i = 0; i < numDevs; ++i)
  147. bufptrs[i] = &buf[i * 65];
  148. bufptrs[numDevs] = NULL;
  149. ftStatus = FT_ListDevices(bufptrs, &numDevs, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
  150. if (ftStatus != FT_OK) {
  151. applog(LOG_DEBUG, "FTDI device list failed, not using FTDI autodetect");
  152. goto out;
  153. }
  154. for (i = numDevs; i > 0; ) {
  155. --i;
  156. bufptrs[i][64] = '\0';
  157. if (!(strstr(bufptrs[i], needle) && (!needle2 || strstr(bufptrs[i], needle2))))
  158. continue;
  159. FT_HANDLE ftHandle;
  160. if (FT_OK != FT_Open(i, &ftHandle))
  161. continue;
  162. LONG lComPortNumber;
  163. ftStatus = FT_GetComPortNumber(ftHandle, &lComPortNumber);
  164. FT_Close(ftHandle);
  165. if (FT_OK != ftStatus || lComPortNumber < 0)
  166. continue;
  167. sprintf(devpathnum, "%d", (int)lComPortNumber);
  168. if (detectone(devpath))
  169. ++found;
  170. }
  171. out:
  172. dlclose(dll);
  173. return found;
  174. }
  175. #else
  176. int serial_autodetect_ftdi(__maybe_unused detectone_func_t detectone, __maybe_unused const char *needle, __maybe_unused const char *needle2)
  177. {
  178. return 0;
  179. }
  180. #endif
  181. struct device_api *serial_claim(const char *devpath, struct device_api *api);
  182. int
  183. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, int flags)
  184. {
  185. struct string_elist *iter, *tmp;
  186. const char*s, *p;
  187. bool inhibitauto = false;
  188. char found = 0;
  189. bool forceauto = flags & 1;
  190. bool hasname;
  191. size_t dnamel = strlen(dname);
  192. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  193. s = iter->string;
  194. if ((p = strchr(s, ':')) && p[1] != '\0') {
  195. size_t plen = p - s;
  196. if (plen != dnamel || strncasecmp(s, dname, plen))
  197. continue;
  198. s = p + 1;
  199. hasname = true;
  200. }
  201. else
  202. hasname = false;
  203. if (!strcmp(s, "auto"))
  204. forceauto = true;
  205. else
  206. if (!strcmp(s, "noauto"))
  207. inhibitauto = true;
  208. else
  209. if ((flags & 2) && !hasname)
  210. continue;
  211. else
  212. if (serial_claim(s, NULL))
  213. {
  214. applog(LOG_DEBUG, "%s is already claimed... skipping probes", s);
  215. string_elist_del(iter);
  216. }
  217. else
  218. if (detectone(s)) {
  219. string_elist_del(iter);
  220. inhibitauto = true;
  221. ++found;
  222. }
  223. }
  224. if ((forceauto || !inhibitauto) && autoscan)
  225. found += autoscan();
  226. return found;
  227. }
  228. #ifndef WIN32
  229. typedef dev_t my_dev_t;
  230. #else
  231. typedef int my_dev_t;
  232. #endif
  233. struct _device_claim {
  234. struct device_api *api;
  235. my_dev_t dev;
  236. UT_hash_handle hh;
  237. };
  238. struct device_api *serial_claim(const char *devpath, struct device_api *api)
  239. {
  240. static struct _device_claim *claims = NULL;
  241. struct _device_claim *c;
  242. my_dev_t dev;
  243. #ifndef WIN32
  244. {
  245. struct stat my_stat;
  246. if (stat(devpath, &my_stat))
  247. return NULL;
  248. dev = my_stat.st_rdev;
  249. }
  250. #else
  251. {
  252. char *p = strstr(devpath, "COM"), *p2;
  253. if (!p)
  254. return NULL;
  255. dev = strtol(&p[3], &p2, 10);
  256. if (p2 == p)
  257. return NULL;
  258. }
  259. #endif
  260. HASH_FIND(hh, claims, &dev, sizeof(dev), c);
  261. if (c)
  262. return c->api;
  263. if (!api)
  264. return NULL;
  265. c = malloc(sizeof(*c));
  266. c->dev = dev;
  267. c->api = api;
  268. HASH_ADD(hh, claims, dev, sizeof(dev), c);
  269. return NULL;
  270. }
  271. // This code is purely for debugging but is very useful for that
  272. // It also took quite a bit of effort so I left it in
  273. // #define TERMIOS_DEBUG 1
  274. // Here to include it at compile time
  275. // It's off by default
  276. #ifndef WIN32
  277. #ifdef TERMIOS_DEBUG
  278. #define BITSSET "Y"
  279. #define BITSNOTSET "N"
  280. int tiospeed(speed_t speed)
  281. {
  282. switch (speed) {
  283. case B0:
  284. return 0;
  285. case B50:
  286. return 50;
  287. case B75:
  288. return 75;
  289. case B110:
  290. return 110;
  291. case B134:
  292. return 134;
  293. case B150:
  294. return 150;
  295. case B200:
  296. return 200;
  297. case B300:
  298. return 300;
  299. case B600:
  300. return 600;
  301. case B1200:
  302. return 1200;
  303. case B1800:
  304. return 1800;
  305. case B2400:
  306. return 2400;
  307. case B4800:
  308. return 4800;
  309. case B9600:
  310. return 9600;
  311. case B19200:
  312. return 19200;
  313. case B38400:
  314. return 38400;
  315. case B57600:
  316. return 57600;
  317. case B115200:
  318. return 115200;
  319. case B230400:
  320. return 230400;
  321. case B460800:
  322. return 460800;
  323. case B500000:
  324. return 500000;
  325. case B576000:
  326. return 576000;
  327. case B921600:
  328. return 921600;
  329. case B1000000:
  330. return 1000000;
  331. case B1152000:
  332. return 1152000;
  333. case B1500000:
  334. return 1500000;
  335. case B2000000:
  336. return 2000000;
  337. case B2500000:
  338. return 2500000;
  339. case B3000000:
  340. return 3000000;
  341. case B3500000:
  342. return 3500000;
  343. case B4000000:
  344. return 4000000;
  345. default:
  346. return -1;
  347. }
  348. }
  349. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  350. {
  351. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  352. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  353. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  354. 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",
  355. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  356. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  357. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  358. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  359. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  360. #define VALO(b) (my_termios->c_oflag | (b))
  361. 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",
  362. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  363. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  364. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  365. VALO(VTDLY), VALO(FFDLY));
  366. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  367. #define VALC(b) (my_termios->c_cflag | (b))
  368. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  369. #ifdef LOBLK
  370. " LOBLK=%s"
  371. #endif
  372. " CMSPAR=%s CRTSCTS=%s",
  373. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  374. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  375. #ifdef LOBLK
  376. ISSETC(LOBLK),
  377. #endif
  378. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  379. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  380. 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"
  381. #ifdef DEFECHO
  382. " DEFECHO=%s"
  383. #endif
  384. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  385. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  386. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  387. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  388. #ifdef DEFECHO
  389. ISSETL(DEFECHO),
  390. #endif
  391. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  392. ISSETL(IEXTEN));
  393. #define VALCC(b) (my_termios->c_cc[b])
  394. 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"
  395. #ifdef VSWTCH
  396. " VSWTCH=0x%02x"
  397. #endif
  398. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  399. #ifdef VDSUSP
  400. " VDSUSP=0x%02x"
  401. #endif
  402. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  403. #ifdef VSTATUS
  404. " VSTATUS=0x%02x"
  405. #endif
  406. ,
  407. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  408. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  409. VALCC(VEOL2),
  410. #ifdef VSWTCH
  411. VALCC(VSWTCH),
  412. #endif
  413. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  414. #ifdef VDSUSP
  415. VALCC(VDSUSP),
  416. #endif
  417. VALCC(VLNEXT), VALCC(VWERASE),
  418. VALCC(VREPRINT), VALCC(VDISCARD)
  419. #ifdef VSTATUS
  420. ,VALCC(VSTATUS)
  421. #endif
  422. );
  423. }
  424. #endif
  425. #endif
  426. /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  427. * this interface buys us warnings when bad constants are passed in.
  428. */
  429. int
  430. serial_open(const char*devpath, unsigned long baud, uint8_t timeout, bool purge)
  431. {
  432. #ifdef WIN32
  433. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  434. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  435. {
  436. DWORD e = GetLastError();
  437. switch (e) {
  438. case ERROR_ACCESS_DENIED:
  439. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  440. break;
  441. case ERROR_SHARING_VIOLATION:
  442. applog(LOG_ERR, "%s is already in use by another process", devpath);
  443. break;
  444. default:
  445. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  446. break;
  447. }
  448. return -1;
  449. }
  450. // thanks to af_newbie for pointers about this
  451. COMMCONFIG comCfg = {0};
  452. comCfg.dwSize = sizeof(COMMCONFIG);
  453. comCfg.wVersion = 1;
  454. comCfg.dcb.DCBlength = sizeof(DCB);
  455. comCfg.dcb.BaudRate = baud;
  456. comCfg.dcb.fBinary = 1;
  457. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  458. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  459. comCfg.dcb.ByteSize = 8;
  460. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  461. // Code must specify a valid timeout value (0 means don't timeout)
  462. const DWORD ctoms = ((DWORD)timeout * 100);
  463. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  464. SetCommTimeouts(hSerial, &cto);
  465. if (purge) {
  466. PurgeComm(hSerial, PURGE_RXABORT);
  467. PurgeComm(hSerial, PURGE_TXABORT);
  468. PurgeComm(hSerial, PURGE_RXCLEAR);
  469. PurgeComm(hSerial, PURGE_TXCLEAR);
  470. }
  471. return _open_osfhandle((LONG)hSerial, 0);
  472. #else
  473. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  474. if (unlikely(fdDev == -1))
  475. {
  476. if (errno == EACCES)
  477. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  478. else
  479. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  480. return -1;
  481. }
  482. struct termios my_termios;
  483. tcgetattr(fdDev, &my_termios);
  484. #ifdef TERMIOS_DEBUG
  485. termios_debug(devpath, &my_termios, "before");
  486. #endif
  487. switch (baud) {
  488. case 0:
  489. break;
  490. case 57600:
  491. cfsetispeed(&my_termios, B57600);
  492. cfsetospeed(&my_termios, B57600);
  493. break;
  494. case 115200:
  495. cfsetispeed(&my_termios, B115200);
  496. cfsetospeed(&my_termios, B115200);
  497. break;
  498. // TODO: try some higher speeds with the Icarus and BFL to see
  499. // if they support them and if setting them makes any difference
  500. // N.B. B3000000 doesn't work on Icarus
  501. default:
  502. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  503. }
  504. my_termios.c_cflag |= CS8;
  505. my_termios.c_cflag |= CREAD;
  506. my_termios.c_cflag |= CLOCAL;
  507. my_termios.c_cflag &= ~(CSIZE | PARENB);
  508. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  509. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  510. my_termios.c_oflag &= ~OPOST;
  511. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  512. // Code must specify a valid timeout value (0 means don't timeout)
  513. my_termios.c_cc[VTIME] = (cc_t)timeout;
  514. my_termios.c_cc[VMIN] = 0;
  515. #ifdef TERMIOS_DEBUG
  516. termios_debug(devpath, &my_termios, "settings");
  517. #endif
  518. tcsetattr(fdDev, TCSANOW, &my_termios);
  519. #ifdef TERMIOS_DEBUG
  520. tcgetattr(fdDev, &my_termios);
  521. termios_debug(devpath, &my_termios, "after");
  522. #endif
  523. if (purge)
  524. tcflush(fdDev, TCIOFLUSH);
  525. return fdDev;
  526. #endif
  527. }
  528. ssize_t
  529. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  530. {
  531. ssize_t len, tlen = 0;
  532. while (bufsiz) {
  533. len = read(fd, buf, eol ? 1 : bufsiz);
  534. if (len < 1)
  535. break;
  536. tlen += len;
  537. if (eol && *eol == buf[0])
  538. break;
  539. buf += len;
  540. bufsiz -= len;
  541. }
  542. return tlen;
  543. }
  544. static FILE*
  545. _open_bitstream(const char*path, const char*subdir, const char*filename)
  546. {
  547. char fullpath[PATH_MAX];
  548. strcpy(fullpath, path);
  549. strcat(fullpath, "/");
  550. if (subdir) {
  551. strcat(fullpath, subdir);
  552. strcat(fullpath, "/");
  553. }
  554. strcat(fullpath, filename);
  555. return fopen(fullpath, "rb");
  556. }
  557. #define _open_bitstream(path, subdir) do { \
  558. f = _open_bitstream(path, subdir, filename); \
  559. if (f) \
  560. return f; \
  561. } while(0)
  562. #define _open_bitstream3(path) do { \
  563. _open_bitstream(path, dname); \
  564. _open_bitstream(path, "bitstreams"); \
  565. _open_bitstream(path, NULL); \
  566. } while(0)
  567. FILE*
  568. open_bitstream(const char*dname, const char*filename)
  569. {
  570. FILE *f;
  571. _open_bitstream3(opt_kernel_path);
  572. _open_bitstream3(cgminer_path);
  573. _open_bitstream3(".");
  574. return NULL;
  575. }