fpgautils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "fpgautils.h"
  32. #include "logging.h"
  33. #include "miner.h"
  34. #ifdef HAVE_LIBUDEV
  35. int
  36. serial_autodetect_udev(detectone_func_t detectone, const char*prodname)
  37. {
  38. struct udev *udev = udev_new();
  39. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  40. struct udev_list_entry *list_entry;
  41. char found = 0;
  42. udev_enumerate_add_match_subsystem(enumerate, "tty");
  43. udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname);
  44. udev_enumerate_scan_devices(enumerate);
  45. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  46. struct udev_device *device = udev_device_new_from_syspath(
  47. udev_enumerate_get_udev(enumerate),
  48. udev_list_entry_get_name(list_entry)
  49. );
  50. if (!device)
  51. continue;
  52. const char *devpath = udev_device_get_devnode(device);
  53. if (devpath && detectone(devpath))
  54. ++found;
  55. udev_device_unref(device);
  56. }
  57. udev_enumerate_unref(enumerate);
  58. udev_unref(udev);
  59. return found;
  60. }
  61. #else
  62. int
  63. serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname)
  64. {
  65. return 0;
  66. }
  67. #endif
  68. int
  69. serial_autodetect_devserial(detectone_func_t detectone, const char*prodname)
  70. {
  71. #ifndef WIN32
  72. DIR *D;
  73. struct dirent *de;
  74. const char udevdir[] = "/dev/serial/by-id";
  75. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  76. char *devfile = devpath + sizeof(udevdir);
  77. char found = 0;
  78. D = opendir(udevdir);
  79. if (!D)
  80. return 0;
  81. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  82. devpath[sizeof(udevdir) - 1] = '/';
  83. while ( (de = readdir(D)) ) {
  84. if (!strstr(de->d_name, prodname))
  85. continue;
  86. strcpy(devfile, de->d_name);
  87. if (detectone(devpath))
  88. ++found;
  89. }
  90. closedir(D);
  91. return found;
  92. #else
  93. return 0;
  94. #endif
  95. }
  96. int
  97. _serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto)
  98. {
  99. struct string_elist *iter, *tmp;
  100. const char*s, *p;
  101. bool inhibitauto = false;
  102. char found = 0;
  103. size_t dnamel = strlen(dname);
  104. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  105. s = iter->string;
  106. if ((p = strchr(s, ':')) && p[1] != '\0') {
  107. size_t plen = p - s;
  108. if (plen != dnamel || strncasecmp(s, dname, plen))
  109. continue;
  110. s = p + 1;
  111. }
  112. if (!strcmp(s, "auto"))
  113. forceauto = true;
  114. else
  115. if (!strcmp(s, "noauto"))
  116. inhibitauto = true;
  117. else
  118. if (detectone(s)) {
  119. string_elist_del(iter);
  120. inhibitauto = true;
  121. ++found;
  122. }
  123. }
  124. if ((forceauto || !inhibitauto) && autoscan)
  125. found += autoscan();
  126. return found;
  127. }
  128. // This code is purely for debugging but is very useful for that
  129. // It also took quite a bit of effort so I left it in
  130. // #define TERMIOS_DEBUG 1
  131. // Here to include it at compile time
  132. // It's off by default
  133. #ifndef WIN32
  134. #ifdef TERMIOS_DEBUG
  135. #define BITSSET "Y"
  136. #define BITSNOTSET "N"
  137. int tiospeed(speed_t speed)
  138. {
  139. switch (speed) {
  140. case B0:
  141. return 0;
  142. case B50:
  143. return 50;
  144. case B75:
  145. return 75;
  146. case B110:
  147. return 110;
  148. case B134:
  149. return 134;
  150. case B150:
  151. return 150;
  152. case B200:
  153. return 200;
  154. case B300:
  155. return 300;
  156. case B600:
  157. return 600;
  158. case B1200:
  159. return 1200;
  160. case B1800:
  161. return 1800;
  162. case B2400:
  163. return 2400;
  164. case B4800:
  165. return 4800;
  166. case B9600:
  167. return 9600;
  168. case B19200:
  169. return 19200;
  170. case B38400:
  171. return 38400;
  172. case B57600:
  173. return 57600;
  174. case B115200:
  175. return 115200;
  176. case B230400:
  177. return 230400;
  178. case B460800:
  179. return 460800;
  180. case B500000:
  181. return 500000;
  182. case B576000:
  183. return 576000;
  184. case B921600:
  185. return 921600;
  186. case B1000000:
  187. return 1000000;
  188. case B1152000:
  189. return 1152000;
  190. case B1500000:
  191. return 1500000;
  192. case B2000000:
  193. return 2000000;
  194. case B2500000:
  195. return 2500000;
  196. case B3000000:
  197. return 3000000;
  198. case B3500000:
  199. return 3500000;
  200. case B4000000:
  201. return 4000000;
  202. default:
  203. return -1;
  204. }
  205. }
  206. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  207. {
  208. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  209. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  210. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  211. 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",
  212. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  213. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  214. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  215. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  216. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  217. #define VALO(b) (my_termios->c_oflag | (b))
  218. 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",
  219. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  220. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  221. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  222. VALO(VTDLY), VALO(FFDLY));
  223. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  224. #define VALC(b) (my_termios->c_cflag | (b))
  225. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  226. #ifdef LOBLK
  227. " LOBLK=%s"
  228. #endif
  229. " CMSPAR=%s CRTSCTS=%s",
  230. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  231. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  232. #ifdef LOBLK
  233. ISSETC(LOBLK),
  234. #endif
  235. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  236. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  237. 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"
  238. #ifdef DEFECHO
  239. " DEFECHO=%s"
  240. #endif
  241. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  242. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  243. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  244. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  245. #ifdef DEFECHO
  246. ISSETL(DEFECHO),
  247. #endif
  248. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  249. ISSETL(IEXTEN));
  250. #define VALCC(b) (my_termios->c_cc[b])
  251. 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"
  252. #ifdef VSWTCH
  253. " VSWTCH=0x%02x"
  254. #endif
  255. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  256. #ifdef VDSUSP
  257. " VDSUSP=0x%02x"
  258. #endif
  259. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  260. #ifdef VSTATUS
  261. " VSTATUS=0x%02x"
  262. #endif
  263. ,
  264. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  265. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  266. VALCC(VEOL2),
  267. #ifdef VSWTCH
  268. VALCC(VSWTCH),
  269. #endif
  270. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  271. #ifdef VDSUSP
  272. VALCC(VDSUSP),
  273. #endif
  274. VALCC(VLNEXT), VALCC(VWERASE),
  275. VALCC(VREPRINT), VALCC(VDISCARD)
  276. #ifdef VSTATUS
  277. ,VALCC(VSTATUS)
  278. #endif
  279. );
  280. }
  281. #endif
  282. #endif
  283. int
  284. 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:%u", devpath, 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((LONG)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 57600:
  345. cfsetispeed(&my_termios, B57600);
  346. cfsetospeed(&my_termios, B57600);
  347. break;
  348. case 115200:
  349. cfsetispeed(&my_termios, B115200);
  350. cfsetospeed(&my_termios, B115200);
  351. break;
  352. // TODO: try some higher speeds with the Icarus and BFL to see
  353. // if they support them and if setting them makes any difference
  354. // N.B. B3000000 doesn't work on Icarus
  355. default:
  356. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  357. }
  358. my_termios.c_cflag |= CS8;
  359. my_termios.c_cflag |= CREAD;
  360. my_termios.c_cflag |= CLOCAL;
  361. my_termios.c_cflag &= ~(CSIZE | PARENB);
  362. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  363. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  364. my_termios.c_oflag &= ~OPOST;
  365. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  366. // Code must specify a valid timeout value (0 means don't timeout)
  367. my_termios.c_cc[VTIME] = (cc_t)timeout;
  368. my_termios.c_cc[VMIN] = 0;
  369. #ifdef TERMIOS_DEBUG
  370. termios_debug(devpath, &my_termios, "settings");
  371. #endif
  372. tcsetattr(fdDev, TCSANOW, &my_termios);
  373. #ifdef TERMIOS_DEBUG
  374. tcgetattr(fdDev, &my_termios);
  375. termios_debug(devpath, &my_termios, "after");
  376. #endif
  377. if (purge)
  378. tcflush(fdDev, TCIOFLUSH);
  379. return fdDev;
  380. #endif
  381. }
  382. ssize_t
  383. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  384. {
  385. ssize_t len, tlen = 0;
  386. while (bufsiz) {
  387. len = read(fd, buf, eol ? 1 : bufsiz);
  388. if (unlikely(len == -1))
  389. break;
  390. tlen += len;
  391. if (eol && *eol == buf[0])
  392. break;
  393. buf += len;
  394. bufsiz -= len;
  395. }
  396. return tlen;
  397. }
  398. static FILE*
  399. _open_bitstream(const char*path, const char*subdir, const char*filename)
  400. {
  401. char fullpath[PATH_MAX];
  402. strcpy(fullpath, path);
  403. strcat(fullpath, "/");
  404. if (subdir) {
  405. strcat(fullpath, subdir);
  406. strcat(fullpath, "/");
  407. }
  408. strcat(fullpath, filename);
  409. return fopen(fullpath, "rb");
  410. }
  411. #define _open_bitstream(path, subdir) do { \
  412. f = _open_bitstream(path, subdir, filename); \
  413. if (f) \
  414. return f; \
  415. } while(0)
  416. #define _open_bitstream3(path) do { \
  417. _open_bitstream(path, dname); \
  418. _open_bitstream(path, "bitstreams"); \
  419. _open_bitstream(path, NULL); \
  420. } while(0)
  421. FILE*
  422. open_bitstream(const char*dname, const char*filename)
  423. {
  424. FILE *f;
  425. _open_bitstream3(opt_kernel_path);
  426. _open_bitstream3(cgminer_path);
  427. _open_bitstream3(".");
  428. return NULL;
  429. }