lowl-vcom.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2013 Con Kolivas
  4. * Copyright 2012 Andrew Smith
  5. * Copyright 2013 Xiangfu
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <ctype.h>
  14. #include <stdarg.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. #include <string.h>
  21. #ifdef HAVE_SYS_FILE_H
  22. #include <sys/file.h>
  23. #endif
  24. #ifdef HAVE_LIBUSB
  25. #include <libusb.h>
  26. #endif
  27. #include "miner.h"
  28. #ifndef WIN32
  29. #include <errno.h>
  30. #include <termios.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #ifndef O_CLOEXEC
  36. #define O_CLOEXEC 0
  37. #endif
  38. #else /* WIN32 */
  39. #include <windows.h>
  40. #ifdef HAVE_WIN_DDKUSB
  41. #include <setupapi.h>
  42. #include <usbioctl.h>
  43. #include <usbiodef.h>
  44. #endif
  45. #include <io.h>
  46. #include <utlist.h>
  47. #define dlsym (void*)GetProcAddress
  48. #define dlclose FreeLibrary
  49. typedef unsigned long FT_STATUS;
  50. typedef PVOID FT_HANDLE;
  51. __stdcall FT_STATUS (*FT_ListDevices)(PVOID pArg1, PVOID pArg2, DWORD Flags);
  52. __stdcall FT_STATUS (*FT_Open)(int idx, FT_HANDLE*);
  53. __stdcall FT_STATUS (*FT_GetComPortNumber)(FT_HANDLE, LPLONG lplComPortNumber);
  54. __stdcall FT_STATUS (*FT_Close)(FT_HANDLE);
  55. const uint32_t FT_OPEN_BY_SERIAL_NUMBER = 1;
  56. const uint32_t FT_OPEN_BY_DESCRIPTION = 2;
  57. const uint32_t FT_LIST_ALL = 0x20000000;
  58. const uint32_t FT_LIST_BY_INDEX = 0x40000000;
  59. const uint32_t FT_LIST_NUMBER_ONLY = 0x80000000;
  60. enum {
  61. FT_OK,
  62. };
  63. #endif /* WIN32 */
  64. #ifdef HAVE_LIBUDEV
  65. #include <libudev.h>
  66. #include <sys/ioctl.h>
  67. #endif
  68. #include "logging.h"
  69. #include "lowlevel.h"
  70. #include "miner.h"
  71. #include "util.h"
  72. #include "lowl-vcom.h"
  73. struct lowlevel_driver lowl_vcom;
  74. struct detectone_meta_info_t detectone_meta_info;
  75. void clear_detectone_meta_info(void)
  76. {
  77. detectone_meta_info = (struct detectone_meta_info_t){
  78. .manufacturer = NULL,
  79. };
  80. }
  81. #define _vcom_unique_id(devpath) devpath_to_devid(devpath)
  82. struct lowlevel_device_info *_vcom_devinfo_findorcreate(struct lowlevel_device_info ** const devinfo_list, const char * const devpath)
  83. {
  84. struct lowlevel_device_info *devinfo;
  85. char * const devid = _vcom_unique_id(devpath);
  86. HASH_FIND_STR(*devinfo_list, devid, devinfo);
  87. if (!devinfo)
  88. {
  89. devinfo = malloc(sizeof(*devinfo));
  90. *devinfo = (struct lowlevel_device_info){
  91. .lowl = &lowl_vcom,
  92. .path = strdup(devpath),
  93. .devid = devid,
  94. };
  95. HASH_ADD_KEYPTR(hh, *devinfo_list, devinfo->devid, strlen(devid), devinfo);
  96. }
  97. else
  98. free(devid);
  99. return devinfo;
  100. }
  101. #ifdef HAVE_LIBUDEV
  102. static
  103. void _decode_udev_enc(char *o, const char *s)
  104. {
  105. while(s[0])
  106. {
  107. if (s[0] == '\\' && s[1] == 'x' && s[2] && s[3])
  108. {
  109. hex2bin((void*)(o++), &s[2], 1);
  110. s += 4;
  111. }
  112. else
  113. (o++)[0] = (s++)[0];
  114. }
  115. o[0] = '\0';
  116. }
  117. static
  118. char *_decode_udev_enc_dup(const char *s)
  119. {
  120. if (!s)
  121. return NULL;
  122. char *o = malloc(strlen(s) + 1);
  123. if (!o)
  124. {
  125. applog(LOG_ERR, "Failed to malloc in _decode_udev_enc_dup");
  126. return NULL;
  127. }
  128. _decode_udev_enc(o, s);
  129. return o;
  130. }
  131. static
  132. void _vcom_devinfo_scan_udev(struct lowlevel_device_info ** const devinfo_list)
  133. {
  134. struct udev *udev = udev_new();
  135. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  136. struct udev_list_entry *list_entry;
  137. struct lowlevel_device_info *devinfo;
  138. udev_enumerate_add_match_subsystem(enumerate, "tty");
  139. udev_enumerate_add_match_property(enumerate, "ID_SERIAL", "*");
  140. udev_enumerate_scan_devices(enumerate);
  141. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  142. struct udev_device *device = udev_device_new_from_syspath(
  143. udev_enumerate_get_udev(enumerate),
  144. udev_list_entry_get_name(list_entry)
  145. );
  146. if (!device)
  147. continue;
  148. const char * const devpath = udev_device_get_devnode(device);
  149. devinfo = _vcom_devinfo_findorcreate(devinfo_list, devpath);
  150. BFGINIT(devinfo->manufacturer, _decode_udev_enc_dup(udev_device_get_property_value(device, "ID_VENDOR_ENC")));
  151. BFGINIT(devinfo->product, _decode_udev_enc_dup(udev_device_get_property_value(device, "ID_MODEL_ENC")));
  152. BFGINIT(devinfo->serial, _decode_udev_enc_dup(udev_device_get_property_value(device, "ID_SERIAL_SHORT")));
  153. udev_device_unref(device);
  154. }
  155. udev_enumerate_unref(enumerate);
  156. udev_unref(udev);
  157. }
  158. #endif
  159. #ifndef WIN32
  160. static
  161. void _vcom_devinfo_scan_devserial(struct lowlevel_device_info ** const devinfo_list)
  162. {
  163. DIR *D;
  164. struct dirent *de;
  165. const char udevdir[] = "/dev/serial/by-id";
  166. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  167. char *devfile = devpath + sizeof(udevdir);
  168. struct lowlevel_device_info *devinfo;
  169. D = opendir(udevdir);
  170. if (!D)
  171. return;
  172. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  173. devpath[sizeof(udevdir) - 1] = '/';
  174. while ( (de = readdir(D)) ) {
  175. if (strncmp(de->d_name, "usb-", 4))
  176. continue;
  177. strcpy(devfile, de->d_name);
  178. devinfo = _vcom_devinfo_findorcreate(devinfo_list, devpath);
  179. if (!(devinfo->manufacturer || devinfo->product || devinfo->serial))
  180. devinfo->product = strdup(devfile);
  181. }
  182. closedir(D);
  183. }
  184. #endif
  185. #ifndef WIN32
  186. static
  187. char *_sysfs_do_read(const char *devpath, char *devfile, const char *append)
  188. {
  189. char buf[0x40];
  190. FILE *F;
  191. strcpy(devfile, append);
  192. F = fopen(devpath, "r");
  193. if (F)
  194. {
  195. if (fgets(buf, sizeof(buf), F))
  196. {
  197. size_t L = strlen(buf);
  198. while (isCspace(buf[--L]))
  199. buf[L] = '\0';
  200. }
  201. else
  202. buf[0] = '\0';
  203. fclose(F);
  204. }
  205. else
  206. buf[0] = '\0';
  207. return buf[0] ? strdup(buf) : NULL;
  208. }
  209. static
  210. void _sysfs_find_tty(char *devpath, char *devfile, struct lowlevel_device_info ** const devinfo_list)
  211. {
  212. struct lowlevel_device_info *devinfo;
  213. DIR *DT;
  214. struct dirent *de;
  215. char ttybuf[0x10] = "/dev/";
  216. char *mydevfile = strdup(devfile);
  217. DT = opendir(devpath);
  218. if (!DT)
  219. goto out;
  220. while ( (de = readdir(DT)) )
  221. {
  222. if (strncmp(de->d_name, "tty", 3))
  223. continue;
  224. if (!de->d_name[3])
  225. {
  226. // "tty" directory: recurse (needed for ttyACM)
  227. sprintf(devfile, "%s/tty", mydevfile);
  228. _sysfs_find_tty(devpath, devfile, devinfo_list);
  229. continue;
  230. }
  231. if (strncmp(&de->d_name[3], "USB", 3) && strncmp(&de->d_name[3], "ACM", 3))
  232. continue;
  233. strcpy(&ttybuf[5], de->d_name);
  234. devinfo = _vcom_devinfo_findorcreate(devinfo_list, ttybuf);
  235. BFGINIT(devinfo->manufacturer, _sysfs_do_read(devpath, devfile, "/manufacturer"));
  236. BFGINIT(devinfo->product, _sysfs_do_read(devpath, devfile, "/product"));
  237. BFGINIT(devinfo->serial, _sysfs_do_read(devpath, devfile, "/serial"));
  238. }
  239. closedir(DT);
  240. out:
  241. free(mydevfile);
  242. }
  243. static
  244. void _vcom_devinfo_scan_sysfs(struct lowlevel_device_info ** const devinfo_list)
  245. {
  246. DIR *D, *DS;
  247. struct dirent *de;
  248. const char devroot[] = "/sys/bus/usb/devices";
  249. const size_t devrootlen = sizeof(devroot) - 1;
  250. char devpath[sizeof(devroot) + (NAME_MAX * 3)];
  251. char *devfile, *upfile;
  252. size_t len, len2;
  253. D = opendir(devroot);
  254. if (!D)
  255. return;
  256. memcpy(devpath, devroot, devrootlen);
  257. devpath[devrootlen] = '/';
  258. while ( (de = readdir(D)) )
  259. {
  260. len = strlen(de->d_name);
  261. upfile = &devpath[devrootlen + 1];
  262. memcpy(upfile, de->d_name, len);
  263. devfile = upfile + len;
  264. devfile[0] = '\0';
  265. DS = opendir(devpath);
  266. if (!DS)
  267. continue;
  268. devfile[0] = '/';
  269. ++devfile;
  270. while ( (de = readdir(DS)) )
  271. {
  272. if (strncmp(de->d_name, upfile, len))
  273. continue;
  274. len2 = strlen(de->d_name);
  275. memcpy(devfile, de->d_name, len2 + 1);
  276. _sysfs_find_tty(devpath, devfile, devinfo_list);
  277. }
  278. closedir(DS);
  279. }
  280. closedir(D);
  281. }
  282. #endif
  283. #ifdef HAVE_WIN_DDKUSB
  284. static const GUID WIN_GUID_DEVINTERFACE_USB_HOST_CONTROLLER = { 0x3ABF6F2D, 0x71C4, 0x462A, {0x8A, 0x92, 0x1E, 0x68, 0x61, 0xE6, 0xAF, 0x27} };
  285. static
  286. char *windows_usb_get_port_path(HANDLE hubh, const int portno)
  287. {
  288. size_t namesz;
  289. ULONG rsz;
  290. {
  291. USB_NODE_CONNECTION_NAME pathinfo = {
  292. .ConnectionIndex = portno,
  293. };
  294. if (!(DeviceIoControl(hubh, IOCTL_USB_GET_NODE_CONNECTION_NAME, &pathinfo, sizeof(pathinfo), &pathinfo, sizeof(pathinfo), &rsz, NULL) && rsz >= sizeof(pathinfo)))
  295. applogfailinfor(NULL, LOG_ERR, "ioctl (1)", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  296. namesz = pathinfo.ActualLength;
  297. }
  298. const size_t bufsz = sizeof(USB_NODE_CONNECTION_NAME) + namesz;
  299. uint8_t buf[bufsz];
  300. USB_NODE_CONNECTION_NAME *path = (USB_NODE_CONNECTION_NAME *)buf;
  301. *path = (USB_NODE_CONNECTION_NAME){
  302. .ConnectionIndex = portno,
  303. };
  304. if (!(DeviceIoControl(hubh, IOCTL_USB_GET_NODE_CONNECTION_NAME, path, bufsz, path, bufsz, &rsz, NULL) && rsz >= sizeof(*path)))
  305. applogfailinfor(NULL, LOG_ERR, "ioctl (2)", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  306. return ucs2_to_utf8_dup(path->NodeName, path->ActualLength);
  307. }
  308. static
  309. char *windows_usb_get_string(HANDLE hubh, const int portno, const uint8_t descid)
  310. {
  311. if (!descid)
  312. return NULL;
  313. const size_t descsz_max = sizeof(USB_STRING_DESCRIPTOR) + MAXIMUM_USB_STRING_LENGTH;
  314. const size_t reqsz = sizeof(USB_DESCRIPTOR_REQUEST) + descsz_max;
  315. uint8_t buf[reqsz];
  316. USB_DESCRIPTOR_REQUEST * const req = (USB_DESCRIPTOR_REQUEST *)buf;
  317. USB_STRING_DESCRIPTOR * const desc = (USB_STRING_DESCRIPTOR *)&req[1];
  318. *req = (USB_DESCRIPTOR_REQUEST){
  319. .ConnectionIndex = portno,
  320. .SetupPacket = {
  321. .wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | descid,
  322. .wIndex = 0,
  323. .wLength = descsz_max,
  324. },
  325. };
  326. // Need to explicitly zero the output memory
  327. memset(desc, '\0', descsz_max);
  328. ULONG descsz;
  329. if (!DeviceIoControl(hubh, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, req, reqsz, req, reqsz, &descsz, NULL))
  330. applogfailinfor(NULL, LOG_DEBUG, "ioctl", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  331. if (descsz < 2 || desc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE || desc->bLength > descsz - sizeof(USB_DESCRIPTOR_REQUEST) || desc->bLength % 2)
  332. applogfailr(NULL, LOG_ERR, "sanity check");
  333. return ucs2_to_utf8_dup(desc->bString, desc->bLength);
  334. }
  335. static void _vcom_devinfo_scan_windows__hub(struct lowlevel_device_info **, const char *);
  336. static
  337. void _vcom_devinfo_scan_windows__hubport(struct lowlevel_device_info ** const devinfo_list, HANDLE hubh, const int portno)
  338. {
  339. struct lowlevel_device_info *devinfo;
  340. const size_t conninfosz = sizeof(USB_NODE_CONNECTION_INFORMATION) + (sizeof(USB_PIPE_INFO) * 30);
  341. uint8_t buf[conninfosz];
  342. USB_NODE_CONNECTION_INFORMATION * const conninfo = (USB_NODE_CONNECTION_INFORMATION *)buf;
  343. conninfo->ConnectionIndex = portno;
  344. ULONG respsz;
  345. if (!DeviceIoControl(hubh, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION, conninfo, conninfosz, conninfo, conninfosz, &respsz, NULL))
  346. applogfailinfor(, LOG_ERR, "ioctl", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  347. if (conninfo->ConnectionStatus != DeviceConnected)
  348. return;
  349. if (conninfo->DeviceIsHub)
  350. {
  351. const char * const hubpath = windows_usb_get_port_path(hubh, portno);
  352. if (hubpath)
  353. _vcom_devinfo_scan_windows__hub(devinfo_list, hubpath);
  354. return;
  355. }
  356. const USB_DEVICE_DESCRIPTOR * const devdesc = &conninfo->DeviceDescriptor;
  357. char * const serial = windows_usb_get_string(hubh, portno, devdesc->iSerialNumber);
  358. if (!serial)
  359. {
  360. out:
  361. free(serial);
  362. return;
  363. }
  364. const size_t slen = strlen(serial);
  365. char subkey[52 + slen + 18 + 1];
  366. sprintf(subkey, "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%04x&PID_%04x\\%s\\Device Parameters",
  367. (unsigned)devdesc->idVendor, (unsigned)devdesc->idProduct, serial);
  368. HKEY hkey;
  369. int e;
  370. if (ERROR_SUCCESS != (e = RegOpenKey(HKEY_LOCAL_MACHINE, subkey, &hkey)))
  371. {
  372. applogfailinfo(LOG_ERR, "open Device Parameters registry key", "%s", bfg_strerror(e, BST_SYSTEM));
  373. goto out;
  374. }
  375. char devpath[0x10] = "\\\\.\\";
  376. DWORD type, sz = sizeof(devpath) - 4;
  377. if (ERROR_SUCCESS != (e = RegQueryValueExA(hkey, "PortName", NULL, &type, (LPBYTE)&devpath[4], &sz)))
  378. {
  379. applogfailinfo(LOG_ERR, "get PortName registry key value", "%s", bfg_strerror(e, BST_SYSTEM));
  380. RegCloseKey(hkey);
  381. goto out;
  382. }
  383. RegCloseKey(hkey);
  384. if (type != REG_SZ)
  385. {
  386. applogfailinfor(, LOG_ERR, "get expected type for PortName registry key value", "%ld", (long)type);
  387. goto out;
  388. }
  389. devinfo = _vcom_devinfo_findorcreate(devinfo_list, devpath);
  390. BFGINIT(devinfo->manufacturer, windows_usb_get_string(hubh, portno, devdesc->iManufacturer));
  391. BFGINIT(devinfo->product, windows_usb_get_string(hubh, portno, devdesc->iProduct));
  392. if (devinfo->serial)
  393. free(serial);
  394. else
  395. devinfo->serial = serial;
  396. }
  397. static
  398. void _vcom_devinfo_scan_windows__hub(struct lowlevel_device_info ** const devinfo_list, const char * const hubpath)
  399. {
  400. HANDLE hubh;
  401. USB_NODE_INFORMATION nodeinfo;
  402. {
  403. char deviceName[4 + strlen(hubpath) + 1];
  404. sprintf(deviceName, "\\\\.\\%s", hubpath);
  405. hubh = CreateFile(deviceName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  406. if (hubh == INVALID_HANDLE_VALUE)
  407. applogr(, LOG_ERR, "Error opening USB hub device %s for autodetect: %s", deviceName, bfg_strerror(GetLastError(), BST_SYSTEM));
  408. }
  409. ULONG nBytes;
  410. if (!DeviceIoControl(hubh, IOCTL_USB_GET_NODE_INFORMATION, &nodeinfo, sizeof(nodeinfo), &nodeinfo, sizeof(nodeinfo), &nBytes, NULL))
  411. applogfailinfor(, LOG_ERR, "ioctl", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  412. const int portcount = nodeinfo.u.HubInformation.HubDescriptor.bNumberOfPorts;
  413. for (int i = 1; i <= portcount; ++i)
  414. _vcom_devinfo_scan_windows__hubport(devinfo_list, hubh, i);
  415. CloseHandle(hubh);
  416. }
  417. static
  418. char *windows_usb_get_root_hub_path(HANDLE hcntlrh)
  419. {
  420. size_t namesz;
  421. ULONG rsz;
  422. {
  423. USB_ROOT_HUB_NAME pathinfo;
  424. if (!(DeviceIoControl(hcntlrh, IOCTL_USB_GET_ROOT_HUB_NAME, 0, 0, &pathinfo, sizeof(pathinfo), &rsz, NULL) && rsz >= sizeof(pathinfo)))
  425. applogfailinfor(NULL, LOG_ERR, "ioctl (1)", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  426. namesz = pathinfo.ActualLength;
  427. }
  428. const size_t bufsz = sizeof(USB_ROOT_HUB_NAME) + namesz;
  429. uint8_t buf[bufsz];
  430. USB_ROOT_HUB_NAME *hubpath = (USB_ROOT_HUB_NAME *)buf;
  431. if (!(DeviceIoControl(hcntlrh, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0, hubpath, bufsz, &rsz, NULL) && rsz >= sizeof(*hubpath)))
  432. applogfailinfor(NULL, LOG_ERR, "ioctl (2)", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  433. return ucs2_to_utf8_dup(hubpath->RootHubName, hubpath->ActualLength);
  434. }
  435. static
  436. void _vcom_devinfo_scan_windows__hcntlr(struct lowlevel_device_info ** const devinfo_list, HDEVINFO *devinfo, const int i)
  437. {
  438. SP_DEVICE_INTERFACE_DATA devifacedata = {
  439. .cbSize = sizeof(devifacedata),
  440. };
  441. if (!SetupDiEnumDeviceInterfaces(*devinfo, 0, (LPGUID)&WIN_GUID_DEVINTERFACE_USB_HOST_CONTROLLER, i, &devifacedata))
  442. applogfailinfor(, LOG_ERR, "SetupDiEnumDeviceInterfaces", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  443. DWORD detailsz;
  444. if (!(!SetupDiGetDeviceInterfaceDetail(*devinfo, &devifacedata, NULL, 0, &detailsz, NULL) && GetLastError() == ERROR_INSUFFICIENT_BUFFER))
  445. applogfailinfor(, LOG_ERR, "SetupDiEnumDeviceInterfaceDetail (1)", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  446. PSP_DEVICE_INTERFACE_DETAIL_DATA detail = alloca(detailsz);
  447. detail->cbSize = sizeof(*detail);
  448. if (!SetupDiGetDeviceInterfaceDetail(*devinfo, &devifacedata, detail, detailsz, &detailsz, NULL))
  449. applogfailinfor(, LOG_ERR, "SetupDiEnumDeviceInterfaceDetail (2)", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  450. HANDLE hcntlrh = CreateFile(detail->DevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  451. if (hcntlrh == INVALID_HANDLE_VALUE)
  452. applogfailinfor(, LOG_DEBUG, "open USB host controller device", "%s", bfg_strerror(GetLastError(), BST_SYSTEM));
  453. char * const hubpath = windows_usb_get_root_hub_path(hcntlrh);
  454. CloseHandle(hcntlrh);
  455. if (unlikely(!hubpath))
  456. return;
  457. _vcom_devinfo_scan_windows__hub(devinfo_list, hubpath);
  458. free(hubpath);
  459. }
  460. static
  461. void _vcom_devinfo_scan_windows(struct lowlevel_device_info ** const devinfo_list)
  462. {
  463. HDEVINFO devinfo;
  464. devinfo = SetupDiGetClassDevs(&WIN_GUID_DEVINTERFACE_USB_HOST_CONTROLLER, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
  465. SP_DEVINFO_DATA devinfodata = {
  466. .cbSize = sizeof(devinfodata),
  467. };
  468. for (int i = 0; SetupDiEnumDeviceInfo(devinfo, i, &devinfodata); ++i)
  469. _vcom_devinfo_scan_windows__hcntlr(devinfo_list, &devinfo, i);
  470. SetupDiDestroyDeviceInfoList(devinfo);
  471. }
  472. #endif
  473. #ifdef WIN32
  474. #define LOAD_SYM(sym) do { \
  475. if (!(sym = dlsym(dll, #sym))) { \
  476. applog(LOG_DEBUG, "Failed to load " #sym ", not using FTDI autodetect"); \
  477. goto out; \
  478. } \
  479. } while(0)
  480. static
  481. char *_ftdi_get_string(char *buf, intptr_t i, DWORD flags)
  482. {
  483. if (FT_OK != FT_ListDevices((PVOID)i, buf, FT_LIST_BY_INDEX | flags))
  484. return NULL;
  485. return buf[0] ? buf : NULL;
  486. }
  487. static
  488. void _vcom_devinfo_scan_ftdi(struct lowlevel_device_info ** const devinfo_list)
  489. {
  490. char devpath[] = "\\\\.\\COMnnnnn";
  491. char *devpathnum = &devpath[7];
  492. char **bufptrs;
  493. char *buf;
  494. char serial[64];
  495. struct lowlevel_device_info *devinfo;
  496. DWORD i;
  497. FT_STATUS ftStatus;
  498. DWORD numDevs;
  499. HMODULE dll = LoadLibrary("FTD2XX.DLL");
  500. if (!dll) {
  501. applog(LOG_DEBUG, "FTD2XX.DLL failed to load, not using FTDI autodetect");
  502. return;
  503. }
  504. LOAD_SYM(FT_ListDevices);
  505. LOAD_SYM(FT_Open);
  506. LOAD_SYM(FT_GetComPortNumber);
  507. LOAD_SYM(FT_Close);
  508. ftStatus = FT_ListDevices(&numDevs, NULL, FT_LIST_NUMBER_ONLY);
  509. if (ftStatus != FT_OK) {
  510. applog(LOG_DEBUG, "FTDI device count failed, not using FTDI autodetect");
  511. goto out;
  512. }
  513. applog(LOG_DEBUG, "FTDI reports %u devices", (unsigned)numDevs);
  514. buf = alloca(65 * numDevs);
  515. bufptrs = alloca(sizeof(*bufptrs) * (numDevs + 1));
  516. for (i = 0; i < numDevs; ++i)
  517. bufptrs[i] = &buf[i * 65];
  518. bufptrs[numDevs] = NULL;
  519. ftStatus = FT_ListDevices(bufptrs, &numDevs, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
  520. if (ftStatus != FT_OK) {
  521. applog(LOG_DEBUG, "FTDI device list failed, not using FTDI autodetect");
  522. goto out;
  523. }
  524. for (i = numDevs; i > 0; ) {
  525. --i;
  526. bufptrs[i][64] = '\0';
  527. FT_HANDLE ftHandle;
  528. if (FT_OK != FT_Open(i, &ftHandle))
  529. continue;
  530. LONG lComPortNumber;
  531. ftStatus = FT_GetComPortNumber(ftHandle, &lComPortNumber);
  532. FT_Close(ftHandle);
  533. if (FT_OK != ftStatus || lComPortNumber < 0)
  534. continue;
  535. applog(LOG_ERR, "FT_GetComPortNumber(%p (%ld), %ld)", ftHandle, (long)i, (long)lComPortNumber);
  536. sprintf(devpathnum, "%d", (int)lComPortNumber);
  537. devinfo = _vcom_devinfo_findorcreate(devinfo_list, devpath);
  538. BFGINIT(devinfo->product, (bufptrs[i] && bufptrs[i][0]) ? strdup(bufptrs[i]) : NULL);
  539. BFGINIT(devinfo->serial, maybe_strdup(_ftdi_get_string(serial, i, FT_OPEN_BY_SERIAL_NUMBER)));
  540. }
  541. out:
  542. dlclose(dll);
  543. }
  544. #endif
  545. #ifdef WIN32
  546. extern void _vcom_devinfo_scan_querydosdevice(struct lowlevel_device_info **);
  547. #else
  548. extern void _vcom_devinfo_scan_lsdev(struct lowlevel_device_info **);
  549. #endif
  550. extern bool lowl_usb_attach_kernel_driver(const struct lowlevel_device_info *);
  551. bool vcom_lowl_probe_wrapper(const struct lowlevel_device_info * const info, detectone_func_t detectone)
  552. {
  553. if (info->lowl != &lowl_vcom)
  554. {
  555. #ifdef HAVE_LIBUSB
  556. if (info->lowl == &lowl_usb)
  557. {
  558. if (lowl_usb_attach_kernel_driver(info))
  559. bfg_need_detect_rescan = true;
  560. }
  561. #endif
  562. return false;
  563. }
  564. detectone_meta_info = (struct detectone_meta_info_t){
  565. .manufacturer = info->manufacturer,
  566. .product = info->product,
  567. .serial = info->serial,
  568. };
  569. const bool rv = detectone(info->path);
  570. clear_detectone_meta_info();
  571. return rv;
  572. }
  573. bool _serial_autodetect_found_cb(struct lowlevel_device_info * const devinfo, void *userp)
  574. {
  575. detectone_func_t detectone = userp;
  576. if (bfg_claim_any(NULL, devinfo->path, devinfo->devid))
  577. {
  578. applog(LOG_DEBUG, "%s (%s) is already claimed, skipping probe", devinfo->path, devinfo->devid);
  579. return false;
  580. }
  581. if (devinfo->lowl != &lowl_vcom)
  582. {
  583. #ifdef HAVE_LIBUSB
  584. if (devinfo->lowl == &lowl_usb)
  585. {
  586. if (lowl_usb_attach_kernel_driver(devinfo))
  587. bfg_need_detect_rescan = true;
  588. }
  589. else
  590. #endif
  591. applog(LOG_WARNING, "Non-VCOM %s (%s) matched", devinfo->path, devinfo->devid);
  592. return false;
  593. }
  594. detectone_meta_info = (struct detectone_meta_info_t){
  595. .manufacturer = devinfo->manufacturer,
  596. .product = devinfo->product,
  597. .serial = devinfo->serial,
  598. };
  599. const bool rv = detectone(devinfo->path);
  600. clear_detectone_meta_info();
  601. return rv;
  602. }
  603. int _serial_autodetect(detectone_func_t detectone, ...)
  604. {
  605. va_list needles;
  606. char *needles_array[0x10];
  607. int needlecount = 0;
  608. va_start(needles, detectone);
  609. while ( (needles_array[needlecount++] = va_arg(needles, void *)) )
  610. {}
  611. va_end(needles);
  612. return _lowlevel_detect(_serial_autodetect_found_cb, NULL, (const char **)needles_array, detectone);
  613. }
  614. static
  615. struct lowlevel_device_info *vcom_devinfo_scan()
  616. {
  617. struct lowlevel_device_info *devinfo_hash = NULL;
  618. struct lowlevel_device_info *devinfo_list = NULL;
  619. struct lowlevel_device_info *devinfo, *tmp;
  620. // All 3 USB Strings available:
  621. #ifndef WIN32
  622. _vcom_devinfo_scan_sysfs(&devinfo_hash);
  623. #endif
  624. #ifdef HAVE_WIN_DDKUSB
  625. _vcom_devinfo_scan_windows(&devinfo_hash);
  626. #endif
  627. #ifdef HAVE_LIBUDEV
  628. _vcom_devinfo_scan_udev(&devinfo_hash);
  629. #endif
  630. // Missing Manufacturer:
  631. #ifdef WIN32
  632. _vcom_devinfo_scan_ftdi(&devinfo_hash);
  633. #endif
  634. // All blobbed together:
  635. #ifndef WIN32
  636. _vcom_devinfo_scan_devserial(&devinfo_hash);
  637. #endif
  638. // No info:
  639. #ifdef WIN32
  640. _vcom_devinfo_scan_querydosdevice(&devinfo_hash);
  641. #else
  642. _vcom_devinfo_scan_lsdev(&devinfo_hash);
  643. #endif
  644. // Convert hash to simple list
  645. HASH_ITER(hh, devinfo_hash, devinfo, tmp)
  646. {
  647. LL_PREPEND(devinfo_list, devinfo);
  648. }
  649. HASH_CLEAR(hh, devinfo_hash);
  650. return devinfo_list;
  651. }
  652. struct device_drv *bfg_claim_serial(struct device_drv * const api, const bool verbose, const char * const devpath)
  653. {
  654. char * const devs = _vcom_unique_id(devpath);
  655. if (!devs)
  656. return false;
  657. struct device_drv * const rv = bfg_claim_any(api, (verbose ? devpath : NULL), devs);
  658. free(devs);
  659. return rv;
  660. }
  661. // This code is purely for debugging but is very useful for that
  662. // It also took quite a bit of effort so I left it in
  663. // #define TERMIOS_DEBUG 1
  664. // Here to include it at compile time
  665. // It's off by default
  666. #ifndef WIN32
  667. #ifdef TERMIOS_DEBUG
  668. #define BITSSET "Y"
  669. #define BITSNOTSET "N"
  670. int tiospeed(speed_t speed)
  671. {
  672. switch (speed) {
  673. #define IOSPEED(baud) \
  674. case B ## baud: \
  675. return baud; \
  676. // END
  677. #include "iospeeds_local.h"
  678. #undef IOSPEED
  679. default:
  680. return -1;
  681. }
  682. }
  683. void termios_debug(const char *devpath, struct termios *my_termios, const char *msg)
  684. {
  685. applog(LOG_DEBUG, "TIOS: Open %s attributes %s: ispeed=%d ospeed=%d",
  686. devpath, msg, tiospeed(cfgetispeed(my_termios)), tiospeed(cfgetispeed(my_termios)));
  687. #define ISSETI(b) ((my_termios->c_iflag | (b)) ? BITSSET : BITSNOTSET)
  688. 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",
  689. ISSETI(IGNBRK), ISSETI(BRKINT), ISSETI(IGNPAR), ISSETI(PARMRK),
  690. ISSETI(INPCK), ISSETI(ISTRIP), ISSETI(INLCR), ISSETI(IGNCR),
  691. ISSETI(ICRNL), ISSETI(IUCLC), ISSETI(IXON), ISSETI(IXANY),
  692. ISSETI(IXOFF), ISSETI(IMAXBEL), ISSETI(IUTF8));
  693. #define ISSETO(b) ((my_termios->c_oflag | (b)) ? BITSSET : BITSNOTSET)
  694. #define VALO(b) (my_termios->c_oflag | (b))
  695. 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",
  696. ISSETO(OPOST), ISSETO(OLCUC), ISSETO(ONLCR), ISSETO(OCRNL),
  697. ISSETO(ONOCR), ISSETO(ONLRET), ISSETO(OFILL), ISSETO(OFDEL),
  698. VALO(NLDLY), VALO(CRDLY), VALO(TABDLY), VALO(BSDLY),
  699. VALO(VTDLY), VALO(FFDLY));
  700. #define ISSETC(b) ((my_termios->c_cflag | (b)) ? BITSSET : BITSNOTSET)
  701. #define VALC(b) (my_termios->c_cflag | (b))
  702. applog(LOG_DEBUG, "TIOS: c_cflag: CBAUDEX=%s CSIZE=%d CSTOPB=%s CREAD=%s PARENB=%s PARODD=%s HUPCL=%s CLOCAL=%s"
  703. #ifdef LOBLK
  704. " LOBLK=%s"
  705. #endif
  706. " CMSPAR=%s CRTSCTS=%s",
  707. ISSETC(CBAUDEX), VALC(CSIZE), ISSETC(CSTOPB), ISSETC(CREAD),
  708. ISSETC(PARENB), ISSETC(PARODD), ISSETC(HUPCL), ISSETC(CLOCAL),
  709. #ifdef LOBLK
  710. ISSETC(LOBLK),
  711. #endif
  712. ISSETC(CMSPAR), ISSETC(CRTSCTS));
  713. #define ISSETL(b) ((my_termios->c_lflag | (b)) ? BITSSET : BITSNOTSET)
  714. 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"
  715. #ifdef DEFECHO
  716. " DEFECHO=%s"
  717. #endif
  718. " FLUSHO=%s NOFLSH=%s TOSTOP=%s PENDIN=%s IEXTEN=%s",
  719. ISSETL(ISIG), ISSETL(ICANON), ISSETL(XCASE), ISSETL(ECHO),
  720. ISSETL(ECHOE), ISSETL(ECHOK), ISSETL(ECHONL), ISSETL(ECHOCTL),
  721. ISSETL(ECHOPRT), ISSETL(ECHOKE),
  722. #ifdef DEFECHO
  723. ISSETL(DEFECHO),
  724. #endif
  725. ISSETL(FLUSHO), ISSETL(NOFLSH), ISSETL(TOSTOP), ISSETL(PENDIN),
  726. ISSETL(IEXTEN));
  727. #define VALCC(b) (my_termios->c_cc[b])
  728. 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"
  729. #ifdef VSWTCH
  730. " VSWTCH=0x%02x"
  731. #endif
  732. " VSTART=0x%02x VSTOP=0x%02x VSUSP=0x%02x"
  733. #ifdef VDSUSP
  734. " VDSUSP=0x%02x"
  735. #endif
  736. " VLNEXT=0x%02x VWERASE=0x%02x VREPRINT=0x%02x VDISCARD=0x%02x"
  737. #ifdef VSTATUS
  738. " VSTATUS=0x%02x"
  739. #endif
  740. ,
  741. VALCC(VINTR), VALCC(VQUIT), VALCC(VERASE), VALCC(VKILL),
  742. VALCC(VEOF), VALCC(VMIN), VALCC(VEOL), VALCC(VTIME),
  743. VALCC(VEOL2),
  744. #ifdef VSWTCH
  745. VALCC(VSWTCH),
  746. #endif
  747. VALCC(VSTART), VALCC(VSTOP), VALCC(VSUSP),
  748. #ifdef VDSUSP
  749. VALCC(VDSUSP),
  750. #endif
  751. VALCC(VLNEXT), VALCC(VWERASE),
  752. VALCC(VREPRINT), VALCC(VDISCARD)
  753. #ifdef VSTATUS
  754. ,VALCC(VSTATUS)
  755. #endif
  756. );
  757. }
  758. #endif /* TERMIOS_DEBUG */
  759. speed_t tiospeed_t(int baud)
  760. {
  761. switch (baud) {
  762. #define IOSPEED(baud) \
  763. case baud: \
  764. return B ## baud; \
  765. // END
  766. #include "iospeeds_local.h"
  767. #undef IOSPEED
  768. default:
  769. return B0;
  770. }
  771. }
  772. #endif /* WIN32 */
  773. bool valid_baud(int baud)
  774. {
  775. switch (baud) {
  776. #define IOSPEED(baud) \
  777. case baud: \
  778. return true; \
  779. // END
  780. #include "iospeeds_local.h"
  781. #undef IOSPEED
  782. default:
  783. return false;
  784. }
  785. }
  786. /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  787. * this interface buys us warnings when bad constants are passed in.
  788. */
  789. int serial_open(const char *devpath, unsigned long baud, uint8_t timeout, bool purge)
  790. {
  791. #ifdef WIN32
  792. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  793. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  794. {
  795. DWORD e = GetLastError();
  796. switch (e) {
  797. case ERROR_ACCESS_DENIED:
  798. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  799. break;
  800. case ERROR_SHARING_VIOLATION:
  801. applog(LOG_ERR, "%s is already in use by another process", devpath);
  802. break;
  803. default:
  804. applog(LOG_DEBUG, "Open %s failed, GetLastError:%u", devpath, (unsigned)e);
  805. break;
  806. }
  807. return -1;
  808. }
  809. if (baud)
  810. {
  811. COMMCONFIG comCfg = {0};
  812. comCfg.dwSize = sizeof(COMMCONFIG);
  813. comCfg.wVersion = 1;
  814. comCfg.dcb.DCBlength = sizeof(DCB);
  815. comCfg.dcb.BaudRate = baud;
  816. comCfg.dcb.fBinary = 1;
  817. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  818. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  819. comCfg.dcb.ByteSize = 8;
  820. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  821. }
  822. // Code must specify a valid timeout value (0 means don't timeout)
  823. const DWORD ctoms = ((DWORD)timeout * 100);
  824. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  825. SetCommTimeouts(hSerial, &cto);
  826. if (purge) {
  827. PurgeComm(hSerial, PURGE_RXABORT);
  828. PurgeComm(hSerial, PURGE_TXABORT);
  829. PurgeComm(hSerial, PURGE_RXCLEAR);
  830. PurgeComm(hSerial, PURGE_TXCLEAR);
  831. }
  832. return _open_osfhandle((intptr_t)hSerial, 0);
  833. #else
  834. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  835. if (unlikely(fdDev == -1))
  836. {
  837. if (errno == EACCES)
  838. applog(LOG_ERR, "Do not have user privileges required to open %s", devpath);
  839. else
  840. applog(LOG_DEBUG, "Open %s failed: %s", devpath, bfg_strerror(errno, BST_ERRNO));
  841. return -1;
  842. }
  843. #if defined(LOCK_EX) && defined(LOCK_NB)
  844. if (likely(!flock(fdDev, LOCK_EX | LOCK_NB)))
  845. applog(LOG_DEBUG, "Acquired exclusive advisory lock on %s", devpath);
  846. else
  847. if (errno == EWOULDBLOCK)
  848. {
  849. applog(LOG_ERR, "%s is already in use by another process", devpath);
  850. close(fdDev);
  851. return -1;
  852. }
  853. else
  854. applog(LOG_WARNING, "Failed to acquire exclusive lock on %s: %s (ignoring)", devpath, bfg_strerror(errno, BST_ERRNO));
  855. #endif
  856. struct termios my_termios;
  857. tcgetattr(fdDev, &my_termios);
  858. #ifdef TERMIOS_DEBUG
  859. termios_debug(devpath, &my_termios, "before");
  860. #endif
  861. if (baud)
  862. {
  863. speed_t speed = tiospeed_t(baud);
  864. if (speed == B0)
  865. applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud);
  866. else
  867. {
  868. cfsetispeed(&my_termios, speed);
  869. cfsetospeed(&my_termios, speed);
  870. }
  871. }
  872. my_termios.c_cflag &= ~(CSIZE | PARENB);
  873. my_termios.c_cflag |= CS8;
  874. my_termios.c_cflag |= CREAD;
  875. #ifdef USE_AVALON
  876. // my_termios.c_cflag |= CRTSCTS;
  877. #endif
  878. my_termios.c_cflag |= CLOCAL;
  879. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  880. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  881. my_termios.c_oflag &= ~OPOST;
  882. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  883. // Code must specify a valid timeout value (0 means don't timeout)
  884. my_termios.c_cc[VTIME] = (cc_t)timeout;
  885. my_termios.c_cc[VMIN] = 0;
  886. #ifdef TERMIOS_DEBUG
  887. termios_debug(devpath, &my_termios, "settings");
  888. #endif
  889. tcsetattr(fdDev, TCSANOW, &my_termios);
  890. #ifdef TERMIOS_DEBUG
  891. tcgetattr(fdDev, &my_termios);
  892. termios_debug(devpath, &my_termios, "after");
  893. #endif
  894. if (purge)
  895. tcflush(fdDev, TCIOFLUSH);
  896. return fdDev;
  897. #endif
  898. }
  899. int serial_close(const int fd)
  900. {
  901. #if defined(LOCK_EX) && defined(LOCK_NB) && defined(LOCK_UN)
  902. flock(fd, LOCK_UN);
  903. #endif
  904. return close(fd);
  905. }
  906. ssize_t _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
  907. {
  908. ssize_t len, tlen = 0;
  909. while (bufsiz) {
  910. len = read(fd, buf, eol ? 1 : bufsiz);
  911. if (len < 1)
  912. break;
  913. tlen += len;
  914. if (eol && *eol == buf[0])
  915. break;
  916. buf += len;
  917. bufsiz -= len;
  918. }
  919. return tlen;
  920. }
  921. #ifndef WIN32
  922. int get_serial_cts(int fd)
  923. {
  924. int flags;
  925. if (!fd)
  926. return -1;
  927. ioctl(fd, TIOCMGET, &flags);
  928. return (flags & TIOCM_CTS) ? 1 : 0;
  929. }
  930. int set_serial_rts(int fd, int rts)
  931. {
  932. int flags;
  933. if (!fd)
  934. return -1;
  935. ioctl(fd, TIOCMGET, &flags);
  936. if (rts)
  937. flags |= TIOCM_RTS;
  938. else
  939. flags &= ~TIOCM_RTS;
  940. ioctl(fd, TIOCMSET, &flags);
  941. return flags & TIOCM_CTS;
  942. }
  943. #else
  944. int get_serial_cts(const int fd)
  945. {
  946. if (!fd)
  947. return -1;
  948. const HANDLE fh = (HANDLE)_get_osfhandle(fd);
  949. if (!fh)
  950. return -1;
  951. DWORD flags;
  952. if (!GetCommModemStatus(fh, &flags))
  953. return -1;
  954. return (flags & MS_CTS_ON) ? 1 : 0;
  955. }
  956. #endif // ! WIN32
  957. struct lowlevel_driver lowl_vcom = {
  958. .dname = "vcom",
  959. .devinfo_scan = vcom_devinfo_scan,
  960. };