fpgautils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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, int flags)
  99. {
  100. struct string_elist *iter, *tmp;
  101. const char*s, *p;
  102. bool inhibitauto = false;
  103. char found = 0;
  104. bool forceauto = flags & 1;
  105. bool hasname;
  106. size_t dnamel = strlen(dname);
  107. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  108. s = iter->string;
  109. if ((p = strchr(s, ':')) && p[1] != '\0') {
  110. size_t plen = p - s;
  111. if (plen != dnamel || strncasecmp(s, dname, plen))
  112. continue;
  113. s = p + 1;
  114. hasname = true;
  115. }
  116. else
  117. hasname = false;
  118. if (!strcmp(s, "auto"))
  119. forceauto = true;
  120. else
  121. if (!strcmp(s, "noauto"))
  122. inhibitauto = true;
  123. else
  124. if ((flags & 2) && !hasname)
  125. continue;
  126. else
  127. if (detectone(s)) {
  128. string_elist_del(iter);
  129. inhibitauto = true;
  130. ++found;
  131. }
  132. }
  133. if ((forceauto || !inhibitauto) && autoscan)
  134. found += autoscan();
  135. return found;
  136. }
  137. // This code is purely for debugging but is very useful for that
  138. // It also took quite a bit of effort so I left it in
  139. // #define TERMIOS_DEBUG 1
  140. // Here to include it at compile time
  141. // It's off by default
  142. #ifndef WIN32
  143. #ifdef TERMIOS_DEBUG
  144. #define BITSSET "Y"
  145. #define BITSNOTSET "N"
  146. int tiospeed(speed_t speed)
  147. {
  148. switch (speed) {
  149. case B0:
  150. return 0;
  151. case B50:
  152. return 50;
  153. case B75:
  154. return 75;
  155. case B110:
  156. return 110;
  157. case B134:
  158. return 134;
  159. case B150:
  160. return 150;
  161. case B200:
  162. return 200;
  163. case B300:
  164. return 300;
  165. case B600:
  166. return 600;
  167. case B1200:
  168. return 1200;
  169. case B1800:
  170. return 1800;
  171. case B2400:
  172. return 2400;
  173. case B4800:
  174. return 4800;
  175. case B9600:
  176. return 9600;
  177. case B19200:
  178. return 19200;
  179. case B38400:
  180. return 38400;
  181. case B57600:
  182. return 57600;
  183. case B115200:
  184. return 115200;
  185. case B230400:
  186. return 230400;
  187. case B460800:
  188. return 460800;
  189. case B500000:
  190. return 500000;
  191. case B576000:
  192. return 576000;
  193. case B921600:
  194. return 921600;
  195. case B1000000:
  196. return 1000000;
  197. case B1152000:
  198. return 1152000;
  199. case B1500000:
  200. return 1500000;
  201. case B2000000:
  202. return 2000000;
  203. case B2500000:
  204. return 2500000;
  205. case B3000000:
  206. return 3000000;
  207. case B3500000:
  208. return 3500000;
  209. case B4000000:
  210. return 4000000;
  211. default:
  212. return -1;
  213. }
  214. }
  215. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  216. {
  217. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  218. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  219. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  220. 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",
  221. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  222. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  223. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  224. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  225. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  226. #define VALO(b) (my_termios->c_oflag | (b))
  227. 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",
  228. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  229. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  230. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  231. VALO(VTDLY), VALO(FFDLY));
  232. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  233. #define VALC(b) (my_termios->c_cflag | (b))
  234. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  235. #ifdef LOBLK
  236. " LOBLK=%s"
  237. #endif
  238. " CMSPAR=%s CRTSCTS=%s",
  239. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  240. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  241. #ifdef LOBLK
  242. ISSETC(LOBLK),
  243. #endif
  244. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  245. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  246. 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"
  247. #ifdef DEFECHO
  248. " DEFECHO=%s"
  249. #endif
  250. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  251. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  252. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  253. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  254. #ifdef DEFECHO
  255. ISSETL(DEFECHO),
  256. #endif
  257. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  258. ISSETL(IEXTEN));
  259. #define VALCC(b) (my_termios->c_cc[b])
  260. 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"
  261. #ifdef VSWTCH
  262. " VSWTCH=0x%02x"
  263. #endif
  264. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  265. #ifdef VDSUSP
  266. " VDSUSP=0x%02x"
  267. #endif
  268. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  269. #ifdef VSTATUS
  270. " VSTATUS=0x%02x"
  271. #endif
  272. ,
  273. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  274. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  275. VALCC(VEOL2),
  276. #ifdef VSWTCH
  277. VALCC(VSWTCH),
  278. #endif
  279. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  280. #ifdef VDSUSP
  281. VALCC(VDSUSP),
  282. #endif
  283. VALCC(VLNEXT), VALCC(VWERASE),
  284. VALCC(VREPRINT), VALCC(VDISCARD)
  285. #ifdef VSTATUS
  286. ,VALCC(VSTATUS)
  287. #endif
  288. );
  289. }
  290. #endif
  291. #endif
  292. /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  293. * this interface buys us warnings when bad constants are passed in.
  294. */
  295. int
  296. serial_open(const char*devpath, unsigned long baud, uint8_t timeout, bool purge)
  297. {
  298. #ifdef WIN32
  299. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  300. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  301. {
  302. DWORD e = GetLastError();
  303. switch (e) {
  304. case ERROR_ACCESS_DENIED:
  305. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  306. break;
  307. case ERROR_SHARING_VIOLATION:
  308. applog(LOG_ERR, "%s is already in use by another process", devpath);
  309. break;
  310. default:
  311. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, e);
  312. break;
  313. }
  314. return -1;
  315. }
  316. // thanks to af_newbie for pointers about this
  317. COMMCONFIG comCfg = {0};
  318. comCfg.dwSize = sizeof(COMMCONFIG);
  319. comCfg.wVersion = 1;
  320. comCfg.dcb.DCBlength = sizeof(DCB);
  321. comCfg.dcb.BaudRate = baud;
  322. comCfg.dcb.fBinary = 1;
  323. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  324. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  325. comCfg.dcb.ByteSize = 8;
  326. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  327. // Code must specify a valid timeout value (0 means don't timeout)
  328. const DWORD ctoms = ((DWORD)timeout * 100);
  329. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  330. SetCommTimeouts(hSerial, &cto);
  331. if (purge) {
  332. PurgeComm(hSerial, PURGE_RXABORT);
  333. PurgeComm(hSerial, PURGE_TXABORT);
  334. PurgeComm(hSerial, PURGE_RXCLEAR);
  335. PurgeComm(hSerial, PURGE_TXCLEAR);
  336. }
  337. return _open_osfhandle((LONG)hSerial, 0);
  338. #else
  339. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  340. if (unlikely(fdDev == -1))
  341. {
  342. if (errno == EACCES)
  343. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  344. else
  345. applog(LOG_DEBUG, "Open %s failed, errno:%d", devpath, errno);
  346. return -1;
  347. }
  348. struct termios my_termios;
  349. tcgetattr(fdDev, &my_termios);
  350. #ifdef TERMIOS_DEBUG
  351. termios_debug(devpath, &my_termios, "before");
  352. #endif
  353. switch (baud) {
  354. case 0:
  355. break;
  356. case 57600:
  357. cfsetispeed(&my_termios, B57600);
  358. cfsetospeed(&my_termios, B57600);
  359. break;
  360. case 115200:
  361. cfsetispeed(&my_termios, B115200);
  362. cfsetospeed(&my_termios, B115200);
  363. break;
  364. // TODO: try some higher speeds with the Icarus and BFL to see
  365. // if they support them and if setting them makes any difference
  366. // N.B. B3000000 doesn't work on Icarus
  367. default:
  368. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  369. }
  370. my_termios.c_cflag |= CS8;
  371. my_termios.c_cflag |= CREAD;
  372. my_termios.c_cflag |= CLOCAL;
  373. my_termios.c_cflag &= ~(CSIZE | PARENB);
  374. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  375. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  376. my_termios.c_oflag &= ~OPOST;
  377. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  378. // Code must specify a valid timeout value (0 means don't timeout)
  379. my_termios.c_cc[VTIME] = (cc_t)timeout;
  380. my_termios.c_cc[VMIN] = 0;
  381. #ifdef TERMIOS_DEBUG
  382. termios_debug(devpath, &my_termios, "settings");
  383. #endif
  384. tcsetattr(fdDev, TCSANOW, &my_termios);
  385. #ifdef TERMIOS_DEBUG
  386. tcgetattr(fdDev, &my_termios);
  387. termios_debug(devpath, &my_termios, "after");
  388. #endif
  389. if (purge)
  390. tcflush(fdDev, TCIOFLUSH);
  391. return fdDev;
  392. #endif
  393. }
  394. ssize_t
  395. _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  396. {
  397. ssize_t len, tlen = 0;
  398. while (bufsiz) {
  399. len = read(fd, buf, eol ? 1 : bufsiz);
  400. if (len < 1)
  401. break;
  402. tlen += len;
  403. if (eol && *eol == buf[0])
  404. break;
  405. buf += len;
  406. bufsiz -= len;
  407. }
  408. return tlen;
  409. }
  410. static FILE*
  411. _open_bitstream(const char*path, const char*subdir, const char*filename)
  412. {
  413. char fullpath[PATH_MAX];
  414. strcpy(fullpath, path);
  415. strcat(fullpath, "/");
  416. if (subdir) {
  417. strcat(fullpath, subdir);
  418. strcat(fullpath, "/");
  419. }
  420. strcat(fullpath, filename);
  421. return fopen(fullpath, "rb");
  422. }
  423. #define _open_bitstream(path, subdir) do { \
  424. f = _open_bitstream(path, subdir, filename); \
  425. if (f) \
  426. return f; \
  427. } while(0)
  428. #define _open_bitstream3(path) do { \
  429. _open_bitstream(path, dname); \
  430. _open_bitstream(path, "bitstreams"); \
  431. _open_bitstream(path, NULL); \
  432. } while(0)
  433. FILE*
  434. open_bitstream(const char*dname, const char*filename)
  435. {
  436. FILE *f;
  437. _open_bitstream3(opt_kernel_path);
  438. _open_bitstream3(cgminer_path);
  439. _open_bitstream3(".");
  440. return NULL;
  441. }