fpgautils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 <sys/types.h>
  13. #include <dirent.h>
  14. #include <string.h>
  15. #ifndef WIN32
  16. #include <errno.h>
  17. #include <termios.h>
  18. #include <sys/stat.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 "fpgautils.h"
  33. #include "logging.h"
  34. #include "miner.h"
  35. #ifdef HAVE_LIBUDEV
  36. int
  37. serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  38. {
  39. struct udev *udev = udev_new();
  40. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  41. struct udev_list_entry *list_entry;
  42. char found = 0;
  43. udev_enumerate_add_match_subsystem(enumerate, "tty");
  44. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  45. udev_enumerate_scan_devices(enumerate);
  46. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  47. struct udev_device *device = udev_device_new_from_syspath(
  48. udev_enumerate_get_udev(enumerate),
  49. udev_list_entry_get_name(list_entry)
  50. );
  51. if (!device)
  52. continue;
  53. const char *devpath = udev_device_get_devnode(device);
  54. if (devpath && detectone(devpath))
  55. ++found;
  56. udev_device_unref(device);
  57. }
  58. udev_enumerate_unref(enumerate);
  59. udev_unref(udev);
  60. return found;
  61. }
  62. #else
  63. int
  64. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  65. {
  66. return 0;
  67. }
  68. #endif
  69. int
  70. serial_autodetect_devserial(detectone_func_t detectone, 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
  98. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  99. {
  100. struct string_elist *iter, *tmp;
  101. const char*s, *p;
  102. bool inhibitauto = false;
  103. char found = 0;
  104. size_t dnamel = strlen(dname);
  105. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  106. s = iter->string;
  107. if ((p = strchr(s, ':')) && p[1] != '\0') {
  108. size_t plen = p - s;
  109. if (plen != dnamel || strncasecmp(s, dname, plen))
  110. continue;
  111. s = p + 1;
  112. }
  113. if (!strcmp(s, "auto"))
  114. forceauto = true;
  115. else
  116. if (!strcmp(s, "noauto"))
  117. inhibitauto = true;
  118. else
  119. if (detectone(s)) {
  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. /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  285. * this interface buys us warnings when bad constants are passed in.
  286. */
  287. int
  288. serial_open(const char*devpath, unsigned long baud, uint8_t timeout, bool purge)
  289. {
  290. #ifdef WIN32
  291. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  292. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  293. {
  294. DWORD e = GetLastError();
  295. switch (e) {
  296. case ERROR_ACCESS_DENIED:
  297. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  298. break;
  299. case ERROR_SHARING_VIOLATION:
  300. applog(LOG_ERR, "%s is already in use by another process", devpath);
  301. break;
  302. default:
  303. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  304. break;
  305. }
  306. return -1;
  307. }
  308. // thanks to af_newbie for pointers about this
  309. COMMCONFIG comCfg = {0};
  310. comCfg.dwSize = sizeof(COMMCONFIG);
  311. comCfg.wVersion = 1;
  312. comCfg.dcb.DCBlength = sizeof(DCB);
  313. comCfg.dcb.BaudRate = baud;
  314. comCfg.dcb.fBinary = 1;
  315. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  316. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  317. comCfg.dcb.ByteSize = 8;
  318. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  319. // Code must specify a valid timeout value (0 means don't timeout)
  320. const DWORD ctoms = ((DWORD)timeout * 100);
  321. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  322. SetCommTimeouts(hSerial, &cto);
  323. if (purge) {
  324. PurgeComm(hSerial, PURGE_RXABORT);
  325. PurgeComm(hSerial, PURGE_TXABORT);
  326. PurgeComm(hSerial, PURGE_RXCLEAR);
  327. PurgeComm(hSerial, PURGE_TXCLEAR);
  328. }
  329. return _open_osfhandle((LONG)hSerial, 0);
  330. #else
  331. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  332. if (unlikely(fdDev == -1))
  333. {
  334. if (errno == EACCES)
  335. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  336. else
  337. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  338. return -1;
  339. }
  340. struct termios my_termios;
  341. tcgetattr(fdDev, &my_termios);
  342. #ifdef TERMIOS_DEBUG
  343. termios_debug(devpath, &my_termios, "before");
  344. #endif
  345. switch (baud) {
  346. case 0:
  347. break;
  348. case 57600:
  349. cfsetispeed(&my_termios, B57600);
  350. cfsetospeed(&my_termios, B57600);
  351. break;
  352. case 115200:
  353. cfsetispeed(&my_termios, B115200);
  354. cfsetospeed(&my_termios, B115200);
  355. break;
  356. // TODO: try some higher speeds with the Icarus and BFL to see
  357. // if they support them and if setting them makes any difference
  358. // N.B. B3000000 doesn't work on Icarus
  359. default:
  360. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  361. }
  362. my_termios.c_cflag |= CS8;
  363. my_termios.c_cflag |= CREAD;
  364. my_termios.c_cflag |= CLOCAL;
  365. my_termios.c_cflag &= ~(CSIZE | PARENB);
  366. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  367. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  368. my_termios.c_oflag &= ~OPOST;
  369. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  370. // Code must specify a valid timeout value (0 means don't timeout)
  371. my_termios.c_cc[VTIME] = (cc_t)timeout;
  372. my_termios.c_cc[VMIN] = 0;
  373. #ifdef TERMIOS_DEBUG
  374. termios_debug(devpath, &my_termios, "settings");
  375. #endif
  376. tcsetattr(fdDev, TCSANOW, &my_termios);
  377. #ifdef TERMIOS_DEBUG
  378. tcgetattr(fdDev, &my_termios);
  379. termios_debug(devpath, &my_termios, "after");
  380. #endif
  381. if (purge)
  382. tcflush(fdDev, TCIOFLUSH);
  383. return fdDev;
  384. #endif
  385. }
  386. ssize_t
  387. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  388. {
  389. ssize_t len, tlen = 0;
  390. while (bufsiz) {
  391. len = read(fd, buf, eol ? 1 : bufsiz);
  392. if (len < 1)
  393. break;
  394. tlen += len;
  395. if (eol && *eol == buf[0])
  396. break;
  397. buf += len;
  398. bufsiz -= len;
  399. }
  400. return tlen;
  401. }
  402. static FILE*
  403. _open_bitstream(const char*path, const char*subdir, const char*filename)
  404. {
  405. char fullpath[PATH_MAX];
  406. strcpy(fullpath, path);
  407. strcat(fullpath, "/");
  408. if (subdir) {
  409. strcat(fullpath, subdir);
  410. strcat(fullpath, "/");
  411. }
  412. strcat(fullpath, filename);
  413. return fopen(fullpath, "rb");
  414. }
  415. #define _open_bitstream(path, subdir) do { \
  416. f = _open_bitstream(path, subdir, filename); \
  417. if (f) \
  418. return f; \
  419. } while(0)
  420. #define _open_bitstream3(path) do { \
  421. _open_bitstream(path, dname); \
  422. _open_bitstream(path, "bitstreams"); \
  423. _open_bitstream(path, NULL); \
  424. } while(0)
  425. FILE*
  426. open_bitstream(const char*dname, const char*filename)
  427. {
  428. FILE *f;
  429. _open_bitstream3(opt_kernel_path);
  430. _open_bitstream3(cgminer_path);
  431. _open_bitstream3(".");
  432. return NULL;
  433. }