2 * This file is part of the libsigrok project.
4 * Copyright (C) 2021 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 static const uint32_t scanopts[] = {
32 static const uint32_t drvopts[] = {
36 static const uint32_t devopts[] = {
37 SR_CONF_CONN | SR_CONF_GET,
38 SR_CONF_ENABLED | SR_CONF_SET, /* Enable/disable all relays at once. */
41 static const uint32_t devopts_cg[] = {
42 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
45 static struct sr_dev_driver dcttech_usbrelay_driver_info;
47 static struct sr_dev_inst *probe_device_common(const char *path,
48 uint16_t vid, uint16_t pid,
49 const wchar_t *vendor, const wchar_t *product)
51 char nonws[16], *s, *endp;
52 unsigned long relay_count;
55 char serno[SERNO_LENGTH + 1];
57 uint8_t report[1 + REPORT_BYTECOUNT];
61 struct sr_dev_inst *sdi;
62 struct dev_context *devc;
63 struct channel_group_context *cgc;
65 struct sr_channel_group *cg;
68 * Get relay count from product string. Weak condition,
69 * accept any trailing number regardless of preceeding text.
71 snprintf(nonws, sizeof(nonws), "%ls", product);
74 while (s > nonws && isdigit((int)s[-1]))
76 ret = sr_atoul_base(s, &relay_count, &endp, 10);
77 if (ret != SR_OK || !endp || *endp)
81 sr_info("Relay count %lu from product string %s.", relay_count, nonws);
83 /* Open device, need to communicate to identify. */
85 hid = hid_open(vid, pid, NULL);
87 hid = hid_open_path(path);
89 sr_err("Cannot open %s.", path);
93 /* Get an HID report. */
94 hid_set_nonblocking(hid, 0);
95 memset(&report, 0, sizeof(report));
96 report[0] = REPORT_NUMBER;
97 ret = hid_get_feature_report(hid, report, sizeof(report));
99 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
100 txt = sr_hexdump_new(report, sizeof(report));
101 sr_spew("Got report bytes: %s, rc %d.", txt->str, ret);
102 sr_hexdump_free(txt);
105 sr_err("Cannot read %s: %ls.", path, hid_error(NULL));
108 if (ret != sizeof(report)) {
109 sr_err("Unexpected HID report length %d from %s.", ret, path);
114 * Serial number must be all printable characters. Relay state
115 * is for information only, gets re-retrieved before configure
116 * API calls (get/set).
118 memset(serno, 0, sizeof(serno));
119 for (snr_pos = 0; snr_pos < SERNO_LENGTH; snr_pos++) {
120 c = report[1 + snr_pos];
122 if (c < 0x20 || c > 0x7e) {
123 sr_warn("Skipping %s, non-printable serial.", path);
127 curr_state = report[1 + STATE_INDEX];
128 sr_info("HID report data: serial number %s, relay state 0x%02x.",
131 /* Create a device instance. */
132 sdi = g_malloc0(sizeof(*sdi));
133 sdi->vendor = g_strdup_printf("%ls", vendor);
134 sdi->model = g_strdup_printf("%ls", product);
135 sdi->serial_num = g_strdup(serno);
136 sdi->connection_id = g_strdup(path);
137 sdi->driver = &dcttech_usbrelay_driver_info;
138 sdi->inst_type = SR_INST_USB;
140 /* Create channels (groups). */
141 devc = g_malloc0(sizeof(*devc));
143 devc->hid_path = g_strdup(path);
146 devc->relay_count = relay_count;
147 devc->relay_mask = (1U << relay_count) - 1;
148 for (idx = 0; idx < devc->relay_count; idx++) {
150 cg = g_malloc0(sizeof(*cg));
151 cg->name = g_strdup_printf("R%zu", nr);
152 cgc = g_malloc0(sizeof(*cgc));
155 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
161 static struct sr_dev_inst *probe_device_enum(struct hid_device_info *dev)
163 return probe_device_common(dev->path, 0, 0,
164 dev->manufacturer_string, dev->product_string);
167 static struct sr_dev_inst *probe_device_conn(const char *path)
177 wchar_t vendor[32], product[32];
180 * The hidapi(3) library's API strives for maximum portability,
181 * thus won't provide ways of getting a path from alternative
182 * presentations like VID:PID pairs, bus.addr specs, etc. The
183 * typical V-USB setup neither provides reliable serial numbers
184 * (that USB enumeration would cover). So this driver's support
185 * for conn= specs beyond Unix style path names is limited, too.
186 * This implementation tries "VID.PID" then assumes "path". The
187 * inability to even get the path for a successfully opened HID
188 * results in redundancy across the places which open devices.
191 /* Check for "<vid>.<pid>" specs. */
194 ret = sr_atoul_base(s, &num, &endp, 16);
195 if (ret == SR_OK && endp && endp == s + 4 && *endp == '.' && num) {
199 ret = sr_atoul_base(s, &num, &endp, 16);
200 if (ret == SR_OK && endp && endp == s + 4 && *endp == '\0' && num) {
205 snprintf(vid_pid, sizeof(vid_pid), "%04x.%04x", vid, pid);
207 sr_dbg("Using VID.PID %s.", path);
210 /* Open the device, get vendor and product strings. */
212 dev = hid_open(vid, pid, NULL);
214 dev = hid_open_path(path);
216 sr_err("Cannot open %s.", path);
220 ret = hid_get_manufacturer_string(dev, vendor, ARRAY_SIZE(vendor));
225 ret = hid_get_product_string(dev, product, ARRAY_SIZE(product));
228 if (!wcslen(product))
234 return probe_device_common(path, vid, pid, vendor, product);
237 static GSList *scan(struct sr_dev_driver *di, GSList *options)
241 struct drv_context *drvc;
242 struct hid_device_info *devs, *curdev;
245 struct sr_dev_inst *sdi;
247 /* Get optional conn= spec when provided. */
249 (void)sr_serial_extract_options(options, &conn, NULL);
255 drvc->instances = NULL;
258 * The firmware is V-USB based. The USB VID:PID identification
259 * is shared across several projects. Need to inspect the vendor
260 * and product _strings_ to actually identify the device. The
261 * USB serial number need not be present nor reliable. The HID
262 * report content will carry the board's serial number.
264 * When conn= was specified, then have HIDAPI open _this_ device
265 * and skip the enumeration. Which allows users to specify paths
266 * that need not match the enumeration's details.
269 sr_info("Checking HID path %s.", conn);
270 sdi = probe_device_conn(conn);
272 sr_warn("Failed to communicate to %s.", conn);
274 devices = g_slist_append(devices, sdi);
276 devs = hid_enumerate(VENDOR_ID, PRODUCT_ID);
277 for (curdev = devs; curdev; curdev = curdev->next) {
280 if (!curdev->vendor_id || !curdev->product_id)
282 if (!curdev->manufacturer_string || !curdev->product_string)
284 if (!*curdev->manufacturer_string || !*curdev->product_string)
286 sr_dbg("Checking %04hx:%04hx, vendor %ls, product %ls.",
287 curdev->vendor_id, curdev->product_id,
288 curdev->manufacturer_string, curdev->product_string);
290 /* Check USB details retrieved by enumeration. */
291 ws = curdev->manufacturer_string;
292 if (!ws || !wcslen(ws))
294 snprintf(nonws, sizeof(nonws), "%ls", ws);
295 if (strcmp(nonws, VENDOR_STRING) != 0)
297 ws = curdev->product_string;
298 if (!ws || !wcslen(ws))
300 snprintf(nonws, sizeof(nonws), "%ls", ws);
301 if (!g_str_has_prefix(nonws, PRODUCT_STRING_PREFIX))
304 /* Identify device by communicating to it. */
305 sr_info("Checking HID path %s.", curdev->path);
306 sdi = probe_device_enum(curdev);
308 sr_warn("Failed to communicate to %s.", curdev->path);
311 devices = g_slist_append(devices, sdi);
313 hid_free_enumeration(devs);
318 static int dev_open(struct sr_dev_inst *sdi)
320 struct dev_context *devc;
325 hid_close(devc->hid_dev);
326 devc->hid_dev = NULL;
329 if (devc->usb_vid && devc->usb_pid)
330 devc->hid_dev = hid_open(devc->usb_vid, devc->usb_pid, NULL);
332 devc->hid_dev = hid_open_path(devc->hid_path);
336 (void)dcttech_usbrelay_update_state(sdi);
341 static int dev_close(struct sr_dev_inst *sdi)
343 struct dev_context *devc;
348 hid_close(devc->hid_dev);
349 devc->hid_dev = NULL;
355 static int config_get(uint32_t key, GVariant **data,
356 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
364 if (!sdi->connection_id)
366 *data = g_variant_new_string(sdi->connection_id);
374 case SR_CONF_ENABLED:
375 ret = dcttech_usbrelay_query_cg(sdi, cg, &on);
378 *data = g_variant_new_boolean(on);
385 static int config_set(uint32_t key, GVariant *data,
386 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
392 case SR_CONF_ENABLED:
393 /* Enable/disable all channels at the same time. */
394 on = g_variant_get_boolean(data);
395 return dcttech_usbrelay_switch_cg(sdi, cg, on);
401 case SR_CONF_ENABLED:
402 on = g_variant_get_boolean(data);
403 return dcttech_usbrelay_switch_cg(sdi, cg, on);
412 static int config_list(uint32_t key, GVariant **data,
413 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
418 case SR_CONF_SCAN_OPTIONS:
419 case SR_CONF_DEVICE_OPTIONS:
420 return STD_CONFIG_LIST(key, data, sdi, cg,
421 scanopts, drvopts, devopts);
428 case SR_CONF_DEVICE_OPTIONS:
429 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
436 static struct sr_dev_driver dcttech_usbrelay_driver_info = {
437 .name = "dcttech-usbrelay",
438 .longname = "dcttech usbrelay",
441 .cleanup = std_cleanup,
443 .dev_list = std_dev_list,
444 .dev_clear = std_dev_clear,
445 .config_get = config_get,
446 .config_set = config_set,
447 .config_list = config_list,
448 .dev_open = dev_open,
449 .dev_close = dev_close,
450 .dev_acquisition_start = std_dummy_dev_acquisition_start,
451 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
454 SR_REGISTER_DEV_DRIVER(dcttech_usbrelay_driver_info);