2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
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/>.
21 #include <glib/gstdio.h>
24 #define SERIALCOMM "115200/8n1/flow=1"
26 static const int32_t hwopts[] = {
36 static struct usb_id_info ho_models[] = {
37 { 0x0403, 0xed72 }, /* HO720 */
38 { 0x0403, 0xed73 }, /* HO730 */
48 static int init(struct sr_context *sr_ctx)
50 return std_init(sr_ctx, di, LOG_PREFIX);
54 * Find USB serial devices via the USB vendor ID and product ID.
56 * @param vendor_id Vendor ID of the USB device.
57 * @param product_id Product ID of the USB device.
59 * @return A GSList of strings containing the path of the serial device or
60 * NULL if no serial device is found. The returned list must be freed
63 static GSList *auto_find_usb(uint16_t vendor_id, uint16_t product_id)
67 const char device_tree[] = "/sys/bus/usb/devices/";
68 GDir *devices_dir, *device_dir;
71 GSList *matched_paths;
74 gchar *vendor_path, *product_path, *path_copy;
75 gchar *prefix, *subdir_path, *device_path, *tty_path;
76 unsigned long read_vendor_id, read_product_id;
83 if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
87 * Find potential candidates using the vendor ID and product ID
88 * and store them in matched_paths.
90 while ((usb_dev = g_dir_read_name(devices_dir))) {
91 vendor_path = g_strconcat(device_tree,
92 usb_dev, "/idVendor", NULL);
93 product_path = g_strconcat(device_tree,
94 usb_dev, "/idProduct", NULL);
96 if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
97 !g_file_test(product_path, G_FILE_TEST_EXISTS))
100 if ((fd = g_fopen(vendor_path, "r")) == NULL)
103 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
107 read_vendor_id = strtoul(tmp, NULL, 16);
111 if ((fd = g_fopen(product_path, "r")) == NULL)
114 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
118 read_product_id = strtoul(tmp, NULL, 16);
122 if (vendor_id == read_vendor_id &&
123 product_id == read_product_id) {
124 path_copy = g_strdup(usb_dev);
125 matched_paths = g_slist_prepend(matched_paths,
131 g_free(product_path);
133 g_dir_close(devices_dir);
135 /* For every matched device try to find a ttyUSBX subfolder. */
136 for (l = matched_paths; l; l = l->next) {
139 device_path = g_strconcat(device_tree, l->data, NULL);
141 if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
146 prefix = g_strconcat(l->data, ":", NULL);
148 while ((file = g_dir_read_name(device_dir))) {
149 if (g_str_has_prefix(file, prefix)) {
150 subdir_path = g_strconcat(device_path,
155 g_dir_close(device_dir);
161 if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
167 while ((file = g_dir_read_name(device_dir))) {
168 if (g_str_has_prefix(file, "ttyUSB")) {
169 tty_path = g_strconcat("/dev/",
171 sr_dbg("Found USB device %04x:%04x attached to %s.",
172 vendor_id, product_id, tty_path);
173 tty_devs = g_slist_prepend(tty_devs,
178 g_dir_close(device_dir);
181 g_slist_free_full(matched_paths, g_free);
189 static GSList *scan(GSList *options)
192 struct drv_context *drvc;
193 struct sr_dev_inst *sdi;
194 const char *serial_device, *serial_options;
195 GSList *l, *tty_devs;
198 serial_device = NULL;
199 serial_options = SERIALCOMM;
203 drvc->instances = NULL;
205 if (sr_serial_extract_options(options, &serial_device,
206 &serial_options) == SR_OK) {
207 sdi = hmo_probe_serial_device(serial_device, serial_options);
209 devices = g_slist_append(devices, sdi);
210 drvc->instances = g_slist_append(drvc->instances, sdi);
215 for (i = 0; i < ARRAY_SIZE(ho_models); i++) {
216 if ((l = auto_find_usb(ho_models[i].vendor_id,
217 ho_models[i].product_id)) == NULL)
219 tty_devs = g_slist_concat(tty_devs, l);
222 for (l = tty_devs; l; l = l->next) {
223 sdi = hmo_probe_serial_device(l->data, serial_options);
225 devices = g_slist_append(devices, sdi);
226 drvc->instances = g_slist_append(drvc->instances, sdi);
230 g_slist_free_full(tty_devs, g_free);
236 static GSList *dev_list(void)
238 return ((struct drv_context *)(di->priv))->instances;
241 static void clear_helper(void *priv)
244 struct dev_context *devc;
245 struct scope_config *model;
248 model = devc->model_config;
250 hmo_scope_state_free(devc->model_state);
252 for (i = 0; i < model->analog_channels; ++i)
253 g_slist_free(devc->analog_groups[i].probes);
255 for (i = 0; i < model->digital_pods; ++i) {
256 g_slist_free(devc->digital_groups[i].probes);
257 g_free(devc->digital_groups[i].name);
260 g_free(devc->analog_groups);
261 g_free(devc->digital_groups);
266 static int dev_clear(void)
268 return std_dev_clear(di, clear_helper);
271 static int dev_open(struct sr_dev_inst *sdi)
273 if (sdi->status != SR_ST_ACTIVE &&
274 serial_open(sdi->conn, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
277 if (hmo_scope_state_get(sdi) != SR_OK)
280 sdi->status = SR_ST_ACTIVE;
285 static int dev_close(struct sr_dev_inst *sdi)
287 if (sdi->status == SR_ST_INACTIVE)
290 serial_close(sdi->conn);
292 sdi->status = SR_ST_INACTIVE;
297 static int cleanup(void)
304 static int check_probe_group(struct dev_context *devc,
305 const struct sr_probe_group *probe_group)
308 struct scope_config *model;
310 model = devc->model_config;
315 for (i = 0; i < model->analog_channels; ++i)
316 if (probe_group == &devc->analog_groups[i])
319 for (i = 0; i < model->digital_pods; ++i)
320 if (probe_group == &devc->digital_groups[i])
323 sr_err("Invalid probe group specified.");
328 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
329 const struct sr_probe_group *probe_group)
333 struct dev_context *devc;
334 struct scope_config *model;
336 if (!sdi || !(devc = sdi->priv))
339 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
343 model = devc->model_config;
346 case SR_CONF_NUM_TIMEBASE:
347 *data = g_variant_new_int32(model->num_xdivs);
350 case SR_CONF_NUM_VDIV:
351 if (pg_type == PG_NONE) {
352 sr_err("No probe group specified.");
353 return SR_ERR_PROBE_GROUP;
354 } else if (pg_type == PG_ANALOG) {
355 for (i = 0; i < model->analog_channels; ++i) {
356 if (probe_group != &devc->analog_groups[i])
358 *data = g_variant_new_int32(model->num_ydivs);
374 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
377 GVariant *rational[2];
380 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
382 for (i = 0; i < n; i++) {
383 rational[0] = g_variant_new_uint64((*array)[i][0]);
384 rational[1] = g_variant_new_uint64((*array)[i][1]);
386 /* FIXME: Valgrind reports a memory leak here. */
387 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
390 return g_variant_builder_end(&gvb);
393 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
394 const struct sr_probe_group *probe_group)
398 char command[MAX_COMMAND_SIZE];
399 struct dev_context *devc;
400 struct scope_config *model;
401 struct scope_state *state;
403 uint64_t p, q, tmp_u64;
406 if (!sdi || !(devc = sdi->priv))
409 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
412 model = devc->model_config;
413 state = devc->model_state;
418 case SR_CONF_LIMIT_FRAMES:
419 devc->frame_limit = g_variant_get_uint64(data);
422 case SR_CONF_TRIGGER_SOURCE:
423 tmp = g_variant_get_string(data, NULL);
424 for (i = 0; (*model->trigger_sources)[i]; i++) {
425 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
427 state->trigger_source = i;
428 g_snprintf(command, sizeof(command),
429 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
430 (*model->trigger_sources)[i]);
432 ret = sr_scpi_send(sdi->conn, command);
437 if (pg_type == PG_NONE) {
438 sr_err("No probe group specified.");
439 return SR_ERR_PROBE_GROUP;
442 g_variant_get(data, "(tt)", &p, &q);
444 for (i = 0; i < model->num_vdivs; i++) {
445 if (p != (*model->vdivs)[i][0] ||
446 q != (*model->vdivs)[i][1])
448 for (j = 1; j <= model->analog_channels; ++j) {
449 if (probe_group != &devc->analog_groups[j - 1])
451 state->analog_channels[j - 1].vdiv = (float) p / q;
452 g_snprintf(command, sizeof(command),
453 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
454 j, state->analog_channels[j-1].vdiv);
456 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
457 sr_scpi_get_opc(sdi->conn) != SR_OK)
467 case SR_CONF_TIMEBASE:
468 g_variant_get(data, "(tt)", &p, &q);
470 for (i = 0; i < model->num_timebases; i++) {
471 if (p != (*model->timebases)[i][0] ||
472 q != (*model->timebases)[i][1])
474 state->timebase = (float) p / q;
475 g_snprintf(command, sizeof(command),
476 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
479 ret = sr_scpi_send(sdi->conn, command);
483 case SR_CONF_HORIZ_TRIGGERPOS:
484 tmp_d = g_variant_get_double(data);
486 if (tmp_d < 0.0 || tmp_d > 1.0)
489 state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs;
490 g_snprintf(command, sizeof(command),
491 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
492 state->horiz_triggerpos);
494 ret = sr_scpi_send(sdi->conn, command);
496 case SR_CONF_TRIGGER_SLOPE:
497 tmp_u64 = g_variant_get_uint64(data);
499 if (tmp_u64 != 0 && tmp_u64 != 1)
502 state->trigger_slope = tmp_u64;
504 g_snprintf(command, sizeof(command),
505 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
506 tmp_u64 ? "POS" : "NEG");
508 ret = sr_scpi_send(sdi->conn, command);
510 case SR_CONF_COUPLING:
511 if (pg_type == PG_NONE) {
512 sr_err("No probe group specified.");
513 return SR_ERR_PROBE_GROUP;
516 tmp = g_variant_get_string(data, NULL);
518 for (i = 0; (*model->coupling_options)[i]; i++) {
519 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
521 for (j = 1; j <= model->analog_channels; ++j) {
522 if (probe_group != &devc->analog_groups[j - 1])
524 state->analog_channels[j-1].coupling = i;
526 g_snprintf(command, sizeof(command),
527 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
530 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
531 sr_scpi_get_opc(sdi->conn) != SR_OK)
546 ret = sr_scpi_get_opc(sdi->conn);
551 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
552 const struct sr_probe_group *probe_group)
555 struct dev_context *devc;
556 struct scope_config *model;
558 if (!sdi || !(devc = sdi->priv))
561 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
564 model = devc->model_config;
567 case SR_CONF_DEVICE_OPTIONS:
568 if (pg_type == PG_NONE) {
569 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
570 model->hw_caps, model->num_hwcaps,
572 } else if (pg_type == PG_ANALOG) {
573 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
574 model->analog_hwcaps, model->num_analog_hwcaps,
577 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
578 NULL, 0, sizeof(int32_t));
581 case SR_CONF_COUPLING:
582 if (pg_type == PG_NONE)
583 return SR_ERR_PROBE_GROUP;
584 *data = g_variant_new_strv(*model->coupling_options,
585 g_strv_length((char **)*model->coupling_options));
587 case SR_CONF_TRIGGER_SOURCE:
588 *data = g_variant_new_strv(*model->trigger_sources,
589 g_strv_length((char **)*model->trigger_sources));
591 case SR_CONF_TIMEBASE:
592 *data = build_tuples(model->timebases, model->num_timebases);
595 if (pg_type == PG_NONE)
596 return SR_ERR_PROBE_GROUP;
597 *data = build_tuples(model->vdivs, model->num_vdivs);
606 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
608 char command[MAX_COMMAND_SIZE];
609 struct sr_probe *probe;
610 struct dev_context *devc;
611 struct scope_config *model;
614 model = devc->model_config;
616 probe = devc->current_probe->data;
618 switch (probe->type) {
619 case SR_PROBE_ANALOG:
620 g_snprintf(command, sizeof(command),
621 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
625 g_snprintf(command, sizeof(command),
626 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
627 probe->index < 8 ? 1 : 2);
630 sr_err("Invalid probe type.");
634 return sr_scpi_send(sdi->conn, command);
637 static int hmo_check_probes(GSList *probes)
640 struct sr_probe *probe;
641 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
643 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
645 for (l = probes; l; l = l->next) {
647 switch (probe->type) {
648 case SR_PROBE_ANALOG:
649 if (probe->index == 2)
650 enabled_chan3 = TRUE;
651 else if (probe->index == 3)
652 enabled_chan4 = TRUE;
655 if (probe->index < 8)
665 if ((enabled_pod1 && enabled_chan3) ||
666 (enabled_pod2 && enabled_chan4))
672 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
676 gboolean *pod_enabled;
677 char command[MAX_COMMAND_SIZE];
678 struct scope_state *state;
679 struct scope_config *model;
680 struct sr_probe *probe;
681 struct dev_context *devc;
682 struct sr_serial_dev_inst *serial;
686 state = devc->model_state;
687 model = devc->model_config;
689 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
691 for (l = sdi->probes; l; l = l->next) {
693 switch (probe->type) {
694 case SR_PROBE_ANALOG:
695 if (probe->enabled == state->analog_channels[probe->index].state)
697 g_snprintf(command, sizeof(command),
698 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
699 probe->index + 1, probe->enabled);
701 if (sr_scpi_send(serial, command) != SR_OK)
703 state->analog_channels[probe->index].state = probe->enabled;
707 * A digital POD needs to be enabled for every group of
711 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
713 if (probe->enabled == state->digital_channels[probe->index])
715 g_snprintf(command, sizeof(command),
716 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
717 probe->index, probe->enabled);
719 if (sr_scpi_send(serial, command) != SR_OK)
722 state->digital_channels[probe->index] = probe->enabled;
729 for (i = 1; i <= model->digital_pods; ++i) {
730 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
732 g_snprintf(command, sizeof(command),
733 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
734 i, pod_enabled[i - 1]);
735 if (sr_scpi_send(serial, command) != SR_OK)
737 state->digital_pods[i - 1] = pod_enabled[i - 1];
745 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
748 gboolean digital_added;
749 struct sr_probe *probe;
750 struct dev_context *devc;
751 struct sr_serial_dev_inst *serial;
753 if (sdi->status != SR_ST_ACTIVE)
754 return SR_ERR_DEV_CLOSED;
758 digital_added = FALSE;
760 for (l = sdi->probes; l; l = l->next) {
764 /* Only add a single digital probe. */
765 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
766 devc->enabled_probes = g_slist_append(
767 devc->enabled_probes, probe);
768 if (probe->type == SR_PROBE_LOGIC)
769 digital_added = TRUE;
773 if (!devc->enabled_probes)
776 if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
777 sr_err("Invalid probe configuration specified!");
781 if (hmo_setup_probes(sdi) != SR_OK) {
782 sr_err("Failed to setup probe configuration!");
786 sr_source_add(serial->fd, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
788 /* Send header packet to the session bus. */
789 std_session_send_df_header(cb_data, LOG_PREFIX);
791 devc->current_probe = devc->enabled_probes;
793 return hmo_request_data(sdi);
796 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
798 struct dev_context *devc;
799 struct sr_serial_dev_inst *serial;
803 if (sdi->status != SR_ST_ACTIVE)
804 return SR_ERR_DEV_CLOSED;
808 g_slist_free(devc->enabled_probes);
809 devc->enabled_probes = NULL;
811 sr_source_remove(serial->fd);
816 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
818 .longname = "Hameg HMO",
823 .dev_list = dev_list,
824 .dev_clear = dev_clear,
825 .config_get = config_get,
826 .config_set = config_set,
827 .config_list = config_list,
828 .dev_open = dev_open,
829 .dev_close = dev_close,
830 .dev_acquisition_start = dev_acquisition_start,
831 .dev_acquisition_stop = dev_acquisition_stop,