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, const char *want_serno,
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 /* Optionally filter by serial number. */
132 if (want_serno && *want_serno && strcmp(serno, want_serno) != 0) {
133 sr_dbg("Serial number does not match user spec. Skipping.");
137 /* Create a device instance. */
138 sdi = g_malloc0(sizeof(*sdi));
139 sdi->vendor = g_strdup_printf("%ls", vendor);
140 sdi->model = g_strdup_printf("%ls", product);
141 sdi->serial_num = g_strdup(serno);
142 sdi->connection_id = g_strdup(path);
143 sdi->driver = &dcttech_usbrelay_driver_info;
144 sdi->inst_type = SR_INST_USB;
146 /* Create channels (groups). */
147 devc = g_malloc0(sizeof(*devc));
149 devc->hid_path = g_strdup(path);
152 devc->relay_count = relay_count;
153 devc->relay_mask = (1U << relay_count) - 1;
154 for (idx = 0; idx < devc->relay_count; idx++) {
156 cg = g_malloc0(sizeof(*cg));
157 cg->name = g_strdup_printf("R%zu", nr);
158 cgc = g_malloc0(sizeof(*cgc));
161 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
167 static struct sr_dev_inst *probe_device_enum(struct hid_device_info *dev,
168 const char *want_serno)
170 return probe_device_common(dev->path, 0, 0, want_serno,
171 dev->manufacturer_string, dev->product_string);
174 static struct sr_dev_inst *probe_device_conn(const char *path)
184 wchar_t vendor[32], product[32];
187 * The hidapi(3) library's API strives for maximum portability,
188 * thus won't provide ways of getting a path from alternative
189 * presentations like VID:PID pairs, bus.addr specs, etc. The
190 * typical V-USB setup neither provides reliable serial numbers
191 * (that USB enumeration would cover). So this driver's support
192 * for conn= specs beyond Unix style path names is limited, too.
193 * This implementation tries "VID.PID" then assumes "path". The
194 * inability to even get the path for a successfully opened HID
195 * results in redundancy across the places which open devices.
198 /* Check for "<vid>.<pid>" specs. */
201 ret = sr_atoul_base(s, &num, &endp, 16);
202 if (ret == SR_OK && endp && endp == s + 4 && *endp == '.' && num) {
206 ret = sr_atoul_base(s, &num, &endp, 16);
207 if (ret == SR_OK && endp && endp == s + 4 && *endp == '\0' && num) {
212 snprintf(vid_pid, sizeof(vid_pid), "%04x.%04x", vid, pid);
214 sr_dbg("Using VID.PID %s.", path);
217 /* Open the device, get vendor and product strings. */
219 dev = hid_open(vid, pid, NULL);
221 dev = hid_open_path(path);
223 sr_err("Cannot open %s.", path);
227 ret = hid_get_manufacturer_string(dev, vendor, ARRAY_SIZE(vendor));
232 ret = hid_get_product_string(dev, product, ARRAY_SIZE(product));
235 if (!wcslen(product))
241 return probe_device_common(path, vid, pid, NULL, vendor, product);
244 static GSList *scan(struct sr_dev_driver *di, GSList *options)
248 struct drv_context *drvc;
249 char want_serno[SERNO_LENGTH + 1];
250 struct hid_device_info *devs, *curdev;
253 struct sr_dev_inst *sdi;
255 /* Get optional conn= spec when provided. */
257 (void)sr_serial_extract_options(options, &conn, NULL);
263 drvc->instances = NULL;
266 * The firmware is V-USB based. The USB VID:PID identification
267 * is shared across several projects. Need to inspect the vendor
268 * and product _strings_ to actually identify the device.
270 * The USB serial number need not be present nor reliable. The
271 * HID report content will carry the board's serial number.
272 * When users specify "sn=..." connection strings, then run a
273 * regular USB enumation, and filter the result set by serial
274 * numbers which only become available with HID reports.
276 * When other connection strings were specified, then have
277 * HIDAPI open _this_ device and skip the enumeration. Which
278 * allows users to specify paths that need not match the
279 * enumeration's details.
281 memset(want_serno, 0, sizeof(want_serno));
282 if (conn && g_str_has_prefix(conn, "sn=")) {
283 conn += strlen("sn=");
284 snprintf(want_serno, sizeof(want_serno), "%s", conn);
288 sr_info("Checking HID path %s.", conn);
289 sdi = probe_device_conn(conn);
291 sr_warn("Failed to communicate to %s.", conn);
293 devices = g_slist_append(devices, sdi);
295 devs = hid_enumerate(VENDOR_ID, PRODUCT_ID);
296 for (curdev = devs; curdev; curdev = curdev->next) {
299 if (!curdev->vendor_id || !curdev->product_id)
301 if (!curdev->manufacturer_string || !curdev->product_string)
303 if (!*curdev->manufacturer_string || !*curdev->product_string)
305 sr_dbg("Checking %04hx:%04hx, vendor %ls, product %ls.",
306 curdev->vendor_id, curdev->product_id,
307 curdev->manufacturer_string, curdev->product_string);
309 /* Check USB details retrieved by enumeration. */
310 ws = curdev->manufacturer_string;
311 if (!ws || !wcslen(ws))
313 snprintf(nonws, sizeof(nonws), "%ls", ws);
314 if (strcmp(nonws, VENDOR_STRING) != 0)
316 ws = curdev->product_string;
317 if (!ws || !wcslen(ws))
319 snprintf(nonws, sizeof(nonws), "%ls", ws);
320 if (!g_str_has_prefix(nonws, PRODUCT_STRING_PREFIX))
323 /* Identify device by communicating to it. */
324 sr_info("Checking HID path %s.", curdev->path);
325 sdi = probe_device_enum(curdev, want_serno);
328 devices = g_slist_append(devices, sdi);
330 hid_free_enumeration(devs);
335 static int dev_open(struct sr_dev_inst *sdi)
337 struct dev_context *devc;
342 hid_close(devc->hid_dev);
343 devc->hid_dev = NULL;
346 if (devc->usb_vid && devc->usb_pid)
347 devc->hid_dev = hid_open(devc->usb_vid, devc->usb_pid, NULL);
349 devc->hid_dev = hid_open_path(devc->hid_path);
353 (void)dcttech_usbrelay_update_state(sdi);
358 static int dev_close(struct sr_dev_inst *sdi)
360 struct dev_context *devc;
365 hid_close(devc->hid_dev);
366 devc->hid_dev = NULL;
372 static int config_get(uint32_t key, GVariant **data,
373 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
381 if (!sdi->connection_id)
383 *data = g_variant_new_string(sdi->connection_id);
391 case SR_CONF_ENABLED:
392 ret = dcttech_usbrelay_query_cg(sdi, cg, &on);
395 *data = g_variant_new_boolean(on);
402 static int config_set(uint32_t key, GVariant *data,
403 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
409 case SR_CONF_ENABLED:
410 /* Enable/disable all channels at the same time. */
411 on = g_variant_get_boolean(data);
412 return dcttech_usbrelay_switch_cg(sdi, cg, on);
418 case SR_CONF_ENABLED:
419 on = g_variant_get_boolean(data);
420 return dcttech_usbrelay_switch_cg(sdi, cg, on);
429 static int config_list(uint32_t key, GVariant **data,
430 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
435 case SR_CONF_SCAN_OPTIONS:
436 case SR_CONF_DEVICE_OPTIONS:
437 return STD_CONFIG_LIST(key, data, sdi, cg,
438 scanopts, drvopts, devopts);
445 case SR_CONF_DEVICE_OPTIONS:
446 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
453 static struct sr_dev_driver dcttech_usbrelay_driver_info = {
454 .name = "dcttech-usbrelay",
455 .longname = "dcttech usbrelay",
458 .cleanup = std_cleanup,
460 .dev_list = std_dev_list,
461 .dev_clear = std_dev_clear,
462 .config_get = config_get,
463 .config_set = config_set,
464 .config_list = config_list,
465 .dev_open = dev_open,
466 .dev_close = dev_close,
467 .dev_acquisition_start = std_dummy_dev_acquisition_start,
468 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
471 SR_REGISTER_DEV_DRIVER(dcttech_usbrelay_driver_info);