fpgautils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 (!detectone)
  213. {} // do nothing
  214. else
  215. if (serial_claim(s, NULL))
  216. {
  217. applog(LOG_DEBUG, "%s is already claimed... skipping probes", s);
  218. string_elist_del(iter);
  219. }
  220. else
  221. if (detectone(s)) {
  222. string_elist_del(iter);
  223. inhibitauto = true;
  224. ++found;
  225. }
  226. }
  227. if ((forceauto || !inhibitauto) && autoscan)
  228. found += autoscan();
  229. return found;
  230. }
  231. #ifndef WIN32
  232. typedef dev_t my_dev_t;
  233. #else
  234. typedef int my_dev_t;
  235. #endif
  236. struct _device_claim {
  237. struct device_api *api;
  238. my_dev_t dev;
  239. UT_hash_handle hh;
  240. };
  241. struct device_api *serial_claim(const char *devpath, struct device_api *api)
  242. {
  243. static struct _device_claim *claims = NULL;
  244. struct _device_claim *c;
  245. my_dev_t dev;
  246. #ifndef WIN32
  247. {
  248. struct stat my_stat;
  249. if (stat(devpath, &my_stat))
  250. return NULL;
  251. dev = my_stat.st_rdev;
  252. }
  253. #else
  254. {
  255. char *p = strstr(devpath, "COM"), *p2;
  256. if (!p)
  257. return NULL;
  258. dev = strtol(&p[3], &p2, 10);
  259. if (p2 == p)
  260. return NULL;
  261. }
  262. #endif
  263. HASH_FIND(hh, claims, &dev, sizeof(dev), c);
  264. if (c)
  265. return c->api;
  266. if (!api)
  267. return NULL;
  268. c = malloc(sizeof(*c));
  269. c->dev = dev;
  270. c->api = api;
  271. HASH_ADD(hh, claims, dev, sizeof(dev), c);
  272. return NULL;
  273. }
  274. // This code is purely for debugging but is very useful for that
  275. // It also took quite a bit of effort so I left it in
  276. // #define TERMIOS_DEBUG 1
  277. // Here to include it at compile time
  278. // It's off by default
  279. #ifndef WIN32
  280. #ifdef TERMIOS_DEBUG
  281. #define BITSSET "Y"
  282. #define BITSNOTSET "N"
  283. int tiospeed(speed_t speed)
  284. {
  285. switch (speed) {
  286. case B0:
  287. return 0;
  288. case B50:
  289. return 50;
  290. case B75:
  291. return 75;
  292. case B110:
  293. return 110;
  294. case B134:
  295. return 134;
  296. case B150:
  297. return 150;
  298. case B200:
  299. return 200;
  300. case B300:
  301. return 300;
  302. case B600:
  303. return 600;
  304. case B1200:
  305. return 1200;
  306. case B1800:
  307. return 1800;
  308. case B2400:
  309. return 2400;
  310. case B4800:
  311. return 4800;
  312. case B9600:
  313. return 9600;
  314. case B19200:
  315. return 19200;
  316. case B38400:
  317. return 38400;
  318. case B57600:
  319. return 57600;
  320. case B115200:
  321. return 115200;
  322. case B230400:
  323. return 230400;
  324. case B460800:
  325. return 460800;
  326. case B500000:
  327. return 500000;
  328. case B576000:
  329. return 576000;
  330. case B921600:
  331. return 921600;
  332. case B1000000:
  333. return 1000000;
  334. case B1152000:
  335. return 1152000;
  336. case B1500000:
  337. return 1500000;
  338. case B2000000:
  339. return 2000000;
  340. case B2500000:
  341. return 2500000;
  342. case B3000000:
  343. return 3000000;
  344. case B3500000:
  345. return 3500000;
  346. case B4000000:
  347. return 4000000;
  348. default:
  349. return -1;
  350. }
  351. }
  352. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  353. {
  354. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  355. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  356. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  357. 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",
  358. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  359. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  360. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  361. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  362. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  363. #define VALO(b) (my_termios->c_oflag | (b))
  364. 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",
  365. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  366. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  367. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  368. VALO(VTDLY), VALO(FFDLY));
  369. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  370. #define VALC(b) (my_termios->c_cflag | (b))
  371. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  372. #ifdef LOBLK
  373. " LOBLK=%s"
  374. #endif
  375. " CMSPAR=%s CRTSCTS=%s",
  376. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  377. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  378. #ifdef LOBLK
  379. ISSETC(LOBLK),
  380. #endif
  381. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  382. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  383. 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"
  384. #ifdef DEFECHO
  385. " DEFECHO=%s"
  386. #endif
  387. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  388. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  389. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  390. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  391. #ifdef DEFECHO
  392. ISSETL(DEFECHO),
  393. #endif
  394. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  395. ISSETL(IEXTEN));
  396. #define VALCC(b) (my_termios->c_cc[b])
  397. 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"
  398. #ifdef VSWTCH
  399. " VSWTCH=0x%02x"
  400. #endif
  401. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  402. #ifdef VDSUSP
  403. " VDSUSP=0x%02x"
  404. #endif
  405. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  406. #ifdef VSTATUS
  407. " VSTATUS=0x%02x"
  408. #endif
  409. ,
  410. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  411. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  412. VALCC(VEOL2),
  413. #ifdef VSWTCH
  414. VALCC(VSWTCH),
  415. #endif
  416. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  417. #ifdef VDSUSP
  418. VALCC(VDSUSP),
  419. #endif
  420. VALCC(VLNEXT), VALCC(VWERASE),
  421. VALCC(VREPRINT), VALCC(VDISCARD)
  422. #ifdef VSTATUS
  423. ,VALCC(VSTATUS)
  424. #endif
  425. );
  426. }
  427. #endif
  428. #endif
  429. /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  430. * this interface buys us warnings when bad constants are passed in.
  431. */
  432. int
  433. serial_open(const char*devpath, unsigned long baud, uint8_t timeout, bool purge)
  434. {
  435. #ifdef WIN32
  436. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  437. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  438. {
  439. DWORD e = GetLastError();
  440. switch (e) {
  441. case ERROR_ACCESS_DENIED:
  442. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  443. break;
  444. case ERROR_SHARING_VIOLATION:
  445. applog(LOG_ERR, "%s is already in use by another process", devpath);
  446. break;
  447. default:
  448. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  449. break;
  450. }
  451. return -1;
  452. }
  453. // thanks to af_newbie for pointers about this
  454. COMMCONFIG comCfg = {0};
  455. comCfg.dwSize = sizeof(COMMCONFIG);
  456. comCfg.wVersion = 1;
  457. comCfg.dcb.DCBlength = sizeof(DCB);
  458. comCfg.dcb.BaudRate = baud;
  459. comCfg.dcb.fBinary = 1;
  460. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  461. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  462. comCfg.dcb.ByteSize = 8;
  463. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  464. // Code must specify a valid timeout value (0 means don't timeout)
  465. const DWORD ctoms = ((DWORD)timeout * 100);
  466. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  467. SetCommTimeouts(hSerial, &cto);
  468. if (purge) {
  469. PurgeComm(hSerial, PURGE_RXABORT);
  470. PurgeComm(hSerial, PURGE_TXABORT);
  471. PurgeComm(hSerial, PURGE_RXCLEAR);
  472. PurgeComm(hSerial, PURGE_TXCLEAR);
  473. }
  474. return _open_osfhandle((LONG)hSerial, 0);
  475. #else
  476. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  477. if (unlikely(fdDev == -1))
  478. {
  479. if (errno == EACCES)
  480. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  481. else
  482. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  483. return -1;
  484. }
  485. struct termios my_termios;
  486. tcgetattr(fdDev, &my_termios);
  487. #ifdef TERMIOS_DEBUG
  488. termios_debug(devpath, &my_termios, "before");
  489. #endif
  490. switch (baud) {
  491. case 0:
  492. break;
  493. case 57600:
  494. cfsetispeed(&my_termios, B57600);
  495. cfsetospeed(&my_termios, B57600);
  496. break;
  497. case 115200:
  498. cfsetispeed(&my_termios, B115200);
  499. cfsetospeed(&my_termios, B115200);
  500. break;
  501. // TODO: try some higher speeds with the Icarus and BFL to see
  502. // if they support them and if setting them makes any difference
  503. // N.B. B3000000 doesn't work on Icarus
  504. default:
  505. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  506. }
  507. my_termios.c_cflag |= CS8;
  508. my_termios.c_cflag |= CREAD;
  509. my_termios.c_cflag |= CLOCAL;
  510. my_termios.c_cflag &= ~(CSIZE | PARENB);
  511. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  512. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  513. my_termios.c_oflag &= ~OPOST;
  514. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  515. // Code must specify a valid timeout value (0 means don't timeout)
  516. my_termios.c_cc[VTIME] = (cc_t)timeout;
  517. my_termios.c_cc[VMIN] = 0;
  518. #ifdef TERMIOS_DEBUG
  519. termios_debug(devpath, &my_termios, "settings");
  520. #endif
  521. tcsetattr(fdDev, TCSANOW, &my_termios);
  522. #ifdef TERMIOS_DEBUG
  523. tcgetattr(fdDev, &my_termios);
  524. termios_debug(devpath, &my_termios, "after");
  525. #endif
  526. if (purge)
  527. tcflush(fdDev, TCIOFLUSH);
  528. return fdDev;
  529. #endif
  530. }
  531. ssize_t
  532. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  533. {
  534. ssize_t len, tlen = 0;
  535. while (bufsiz) {
  536. len = read(fd, buf, eol ? 1 : bufsiz);
  537. if (len < 1)
  538. break;
  539. tlen += len;
  540. if (eol && *eol == buf[0])
  541. break;
  542. buf += len;
  543. bufsiz -= len;
  544. }
  545. return tlen;
  546. }
  547. static FILE*
  548. _open_bitstream(const char*path, const char*subdir, const char*filename)
  549. {
  550. char fullpath[PATH_MAX];
  551. strcpy(fullpath, path);
  552. strcat(fullpath, "/");
  553. if (subdir) {
  554. strcat(fullpath, subdir);
  555. strcat(fullpath, "/");
  556. }
  557. strcat(fullpath, filename);
  558. return fopen(fullpath, "rb");
  559. }
  560. #define _open_bitstream(path, subdir) do { \
  561. f = _open_bitstream(path, subdir, filename); \
  562. if (f) \
  563. return f; \
  564. } while(0)
  565. #define _open_bitstream3(path) do { \
  566. _open_bitstream(path, dname); \
  567. _open_bitstream(path, "bitstreams"); \
  568. _open_bitstream(path, NULL); \
  569. } while(0)
  570. FILE*
  571. open_bitstream(const char*dname, const char*filename)
  572. {
  573. FILE *f;
  574. _open_bitstream3(opt_kernel_path);
  575. _open_bitstream3(cgminer_path);
  576. _open_bitstream3(".");
  577. return NULL;
  578. }