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 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
27 static struct sr_dev_driver *di = &hameg_hmo_driver_info;
29 static const char *manufacturers[] = {
33 static const int32_t hwopts[] = {
43 static struct usb_id_info ho_models[] = {
44 { 0x0403, 0xed72 }, /* HO720 */
45 { 0x0403, 0xed73 }, /* HO730 */
55 static int init(struct sr_context *sr_ctx)
57 return std_init(sr_ctx, di, LOG_PREFIX);
61 * Find USB serial devices via the USB vendor ID and product ID.
63 * @param vendor_id Vendor ID of the USB device.
64 * @param product_id Product ID of the USB device.
66 * @return A GSList of strings containing the path of the serial device or
67 * NULL if no serial device is found. The returned list must be freed
70 static GSList *auto_find_usb(uint16_t vendor_id, uint16_t product_id)
74 const char device_tree[] = "/sys/bus/usb/devices/";
75 GDir *devices_dir, *device_dir;
78 GSList *matched_paths;
81 gchar *vendor_path, *product_path, *path_copy;
82 gchar *prefix, *subdir_path, *device_path, *tty_path;
83 unsigned long read_vendor_id, read_product_id;
90 if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
94 * Find potential candidates using the vendor ID and product ID
95 * and store them in matched_paths.
97 while ((usb_dev = g_dir_read_name(devices_dir))) {
98 vendor_path = g_strconcat(device_tree,
99 usb_dev, "/idVendor", NULL);
100 product_path = g_strconcat(device_tree,
101 usb_dev, "/idProduct", NULL);
103 if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
104 !g_file_test(product_path, G_FILE_TEST_EXISTS))
107 if ((fd = g_fopen(vendor_path, "r")) == NULL)
110 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
114 read_vendor_id = strtoul(tmp, NULL, 16);
118 if ((fd = g_fopen(product_path, "r")) == NULL)
121 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
125 read_product_id = strtoul(tmp, NULL, 16);
129 if (vendor_id == read_vendor_id &&
130 product_id == read_product_id) {
131 path_copy = g_strdup(usb_dev);
132 matched_paths = g_slist_prepend(matched_paths,
138 g_free(product_path);
140 g_dir_close(devices_dir);
142 /* For every matched device try to find a ttyUSBX subfolder. */
143 for (l = matched_paths; l; l = l->next) {
146 device_path = g_strconcat(device_tree, l->data, NULL);
148 if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
153 prefix = g_strconcat(l->data, ":", NULL);
155 while ((file = g_dir_read_name(device_dir))) {
156 if (g_str_has_prefix(file, prefix)) {
157 subdir_path = g_strconcat(device_path,
162 g_dir_close(device_dir);
168 if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
174 while ((file = g_dir_read_name(device_dir))) {
175 if (g_str_has_prefix(file, "ttyUSB")) {
176 tty_path = g_strconcat("/dev/",
178 sr_dbg("Found USB device %04x:%04x attached to %s.",
179 vendor_id, product_id, tty_path);
180 tty_devs = g_slist_prepend(tty_devs,
185 g_dir_close(device_dir);
188 g_slist_free_full(matched_paths, g_free);
196 static int check_manufacturer(const char *manufacturer)
200 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
201 if (!strcmp(manufacturer, manufacturers[i]))
207 static struct sr_dev_inst *hmo_probe_serial_device(const char *serial_device,
208 const char *serial_options)
210 struct sr_dev_inst *sdi;
211 struct dev_context *devc;
212 struct sr_scpi_hw_info *hw_info;
213 struct sr_scpi_dev_inst *scpi;
220 if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
223 sr_info("Probing %s.", serial_device);
224 if (sr_scpi_open(scpi) != SR_OK)
227 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
228 sr_info("Couldn't get IDN response.");
232 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
235 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
236 hw_info->manufacturer, hw_info->model,
237 hw_info->firmware_version))) {
240 sr_scpi_hw_info_free(hw_info);
243 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
248 sdi->inst_type = SR_INST_SCPI;
251 if (hmo_init_device(sdi) != SR_OK)
254 sr_scpi_close(sdi->conn);
256 sdi->status = SR_ST_INACTIVE;
262 sr_scpi_hw_info_free(hw_info);
266 sr_dev_inst_free(sdi);
273 static GSList *scan(GSList *options)
276 struct drv_context *drvc;
277 struct sr_dev_inst *sdi;
278 const char *serial_device, *serial_options;
279 GSList *l, *tty_devs;
282 serial_device = NULL;
283 serial_options = SERIALCOMM;
287 drvc->instances = NULL;
289 if (sr_serial_extract_options(options, &serial_device,
290 &serial_options) == SR_OK) {
291 sdi = hmo_probe_serial_device(serial_device, serial_options);
293 devices = g_slist_append(devices, sdi);
294 drvc->instances = g_slist_append(drvc->instances, sdi);
299 for (i = 0; i < ARRAY_SIZE(ho_models); i++) {
300 if ((l = auto_find_usb(ho_models[i].vendor_id,
301 ho_models[i].product_id)) == NULL)
303 tty_devs = g_slist_concat(tty_devs, l);
306 for (l = tty_devs; l; l = l->next) {
307 sdi = hmo_probe_serial_device(l->data, serial_options);
309 devices = g_slist_append(devices, sdi);
310 drvc->instances = g_slist_append(drvc->instances, sdi);
314 g_slist_free_full(tty_devs, g_free);
320 static GSList *dev_list(void)
322 return ((struct drv_context *)(di->priv))->instances;
325 static void clear_helper(void *priv)
328 struct dev_context *devc;
329 struct scope_config *model;
332 model = devc->model_config;
334 hmo_scope_state_free(devc->model_state);
336 for (i = 0; i < model->analog_channels; ++i)
337 g_slist_free(devc->analog_groups[i].probes);
339 for (i = 0; i < model->digital_pods; ++i) {
340 g_slist_free(devc->digital_groups[i].probes);
341 g_free(devc->digital_groups[i].name);
344 g_free(devc->analog_groups);
345 g_free(devc->digital_groups);
350 static int dev_clear(void)
352 return std_dev_clear(di, clear_helper);
355 static int dev_open(struct sr_dev_inst *sdi)
357 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
360 if (hmo_scope_state_get(sdi) != SR_OK)
363 sdi->status = SR_ST_ACTIVE;
368 static int dev_close(struct sr_dev_inst *sdi)
370 if (sdi->status == SR_ST_INACTIVE)
373 sr_scpi_close(sdi->conn);
375 sdi->status = SR_ST_INACTIVE;
380 static int cleanup(void)
387 static int check_probe_group(struct dev_context *devc,
388 const struct sr_probe_group *probe_group)
391 struct scope_config *model;
393 model = devc->model_config;
398 for (i = 0; i < model->analog_channels; ++i)
399 if (probe_group == &devc->analog_groups[i])
402 for (i = 0; i < model->digital_pods; ++i)
403 if (probe_group == &devc->digital_groups[i])
406 sr_err("Invalid probe group specified.");
411 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
412 const struct sr_probe_group *probe_group)
416 struct dev_context *devc;
417 struct scope_config *model;
418 struct scope_state *state;
420 if (!sdi || !(devc = sdi->priv))
423 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
427 model = devc->model_config;
428 state = devc->model_state;
431 case SR_CONF_NUM_TIMEBASE:
432 *data = g_variant_new_int32(model->num_xdivs);
435 case SR_CONF_NUM_VDIV:
436 if (pg_type == PG_NONE) {
437 sr_err("No probe group specified.");
438 return SR_ERR_PROBE_GROUP;
439 } else if (pg_type == PG_ANALOG) {
440 for (i = 0; i < model->analog_channels; ++i) {
441 if (probe_group != &devc->analog_groups[i])
443 *data = g_variant_new_int32(model->num_ydivs);
452 case SR_CONF_SAMPLERATE:
453 *data = g_variant_new_uint64(state->sample_rate);
463 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
466 GVariant *rational[2];
469 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
471 for (i = 0; i < n; i++) {
472 rational[0] = g_variant_new_uint64((*array)[i][0]);
473 rational[1] = g_variant_new_uint64((*array)[i][1]);
475 /* FIXME: Valgrind reports a memory leak here. */
476 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
479 return g_variant_builder_end(&gvb);
482 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
483 const struct sr_probe_group *probe_group)
487 char command[MAX_COMMAND_SIZE], float_str[30];
488 struct dev_context *devc;
489 struct scope_config *model;
490 struct scope_state *state;
492 uint64_t p, q, tmp_u64;
494 gboolean update_sample_rate;
496 if (!sdi || !(devc = sdi->priv))
499 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
502 model = devc->model_config;
503 state = devc->model_state;
504 update_sample_rate = FALSE;
509 case SR_CONF_LIMIT_FRAMES:
510 devc->frame_limit = g_variant_get_uint64(data);
513 case SR_CONF_TRIGGER_SOURCE:
514 tmp = g_variant_get_string(data, NULL);
515 for (i = 0; (*model->trigger_sources)[i]; i++) {
516 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
518 state->trigger_source = i;
519 g_snprintf(command, sizeof(command),
520 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
521 (*model->trigger_sources)[i]);
523 ret = sr_scpi_send(sdi->conn, command);
528 if (pg_type == PG_NONE) {
529 sr_err("No probe group specified.");
530 return SR_ERR_PROBE_GROUP;
533 g_variant_get(data, "(tt)", &p, &q);
535 for (i = 0; i < model->num_vdivs; i++) {
536 if (p != (*model->vdivs)[i][0] ||
537 q != (*model->vdivs)[i][1])
539 for (j = 1; j <= model->analog_channels; ++j) {
540 if (probe_group != &devc->analog_groups[j - 1])
542 state->analog_channels[j - 1].vdiv = i;
543 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
544 g_snprintf(command, sizeof(command),
545 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
548 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
549 sr_scpi_get_opc(sdi->conn) != SR_OK)
559 case SR_CONF_TIMEBASE:
560 g_variant_get(data, "(tt)", &p, &q);
562 for (i = 0; i < model->num_timebases; i++) {
563 if (p != (*model->timebases)[i][0] ||
564 q != (*model->timebases)[i][1])
567 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
568 g_snprintf(command, sizeof(command),
569 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
572 ret = sr_scpi_send(sdi->conn, command);
573 update_sample_rate = TRUE;
577 case SR_CONF_HORIZ_TRIGGERPOS:
578 tmp_d = g_variant_get_double(data);
580 if (tmp_d < 0.0 || tmp_d > 1.0)
583 state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs;
584 g_snprintf(command, sizeof(command),
585 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
586 state->horiz_triggerpos);
588 ret = sr_scpi_send(sdi->conn, command);
590 case SR_CONF_TRIGGER_SLOPE:
591 tmp_u64 = g_variant_get_uint64(data);
593 if (tmp_u64 != 0 && tmp_u64 != 1)
596 state->trigger_slope = tmp_u64;
598 g_snprintf(command, sizeof(command),
599 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
600 tmp_u64 ? "POS" : "NEG");
602 ret = sr_scpi_send(sdi->conn, command);
604 case SR_CONF_COUPLING:
605 if (pg_type == PG_NONE) {
606 sr_err("No probe group specified.");
607 return SR_ERR_PROBE_GROUP;
610 tmp = g_variant_get_string(data, NULL);
612 for (i = 0; (*model->coupling_options)[i]; i++) {
613 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
615 for (j = 1; j <= model->analog_channels; ++j) {
616 if (probe_group != &devc->analog_groups[j - 1])
618 state->analog_channels[j-1].coupling = i;
620 g_snprintf(command, sizeof(command),
621 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
624 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
625 sr_scpi_get_opc(sdi->conn) != SR_OK)
640 ret = sr_scpi_get_opc(sdi->conn);
642 if (ret == SR_OK && update_sample_rate)
643 ret = hmo_update_sample_rate(sdi);
648 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
649 const struct sr_probe_group *probe_group)
652 struct dev_context *devc;
653 struct scope_config *model;
655 if (!sdi || !(devc = sdi->priv))
658 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
661 model = devc->model_config;
664 case SR_CONF_DEVICE_OPTIONS:
665 if (pg_type == PG_NONE) {
666 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
667 model->hw_caps, model->num_hwcaps,
669 } else if (pg_type == PG_ANALOG) {
670 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
671 model->analog_hwcaps, model->num_analog_hwcaps,
674 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
675 NULL, 0, sizeof(int32_t));
678 case SR_CONF_COUPLING:
679 if (pg_type == PG_NONE)
680 return SR_ERR_PROBE_GROUP;
681 *data = g_variant_new_strv(*model->coupling_options,
682 g_strv_length((char **)*model->coupling_options));
684 case SR_CONF_TRIGGER_SOURCE:
685 *data = g_variant_new_strv(*model->trigger_sources,
686 g_strv_length((char **)*model->trigger_sources));
688 case SR_CONF_TIMEBASE:
689 *data = build_tuples(model->timebases, model->num_timebases);
692 if (pg_type == PG_NONE)
693 return SR_ERR_PROBE_GROUP;
694 *data = build_tuples(model->vdivs, model->num_vdivs);
703 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
705 char command[MAX_COMMAND_SIZE];
706 struct sr_probe *probe;
707 struct dev_context *devc;
708 struct scope_config *model;
711 model = devc->model_config;
713 probe = devc->current_probe->data;
715 switch (probe->type) {
716 case SR_PROBE_ANALOG:
717 g_snprintf(command, sizeof(command),
718 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
722 g_snprintf(command, sizeof(command),
723 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
724 probe->index < 8 ? 1 : 2);
727 sr_err("Invalid probe type.");
731 return sr_scpi_send(sdi->conn, command);
734 static int hmo_check_probes(GSList *probes)
737 struct sr_probe *probe;
738 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
740 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
742 for (l = probes; l; l = l->next) {
744 switch (probe->type) {
745 case SR_PROBE_ANALOG:
746 if (probe->index == 2)
747 enabled_chan3 = TRUE;
748 else if (probe->index == 3)
749 enabled_chan4 = TRUE;
752 if (probe->index < 8)
762 if ((enabled_pod1 && enabled_chan3) ||
763 (enabled_pod2 && enabled_chan4))
769 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
773 gboolean *pod_enabled, setup_changed;
774 char command[MAX_COMMAND_SIZE];
775 struct scope_state *state;
776 struct scope_config *model;
777 struct sr_probe *probe;
778 struct dev_context *devc;
779 struct sr_scpi_dev_inst *scpi;
783 state = devc->model_state;
784 model = devc->model_config;
785 setup_changed = FALSE;
787 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
789 for (l = sdi->probes; l; l = l->next) {
791 switch (probe->type) {
792 case SR_PROBE_ANALOG:
793 if (probe->enabled == state->analog_channels[probe->index].state)
795 g_snprintf(command, sizeof(command),
796 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
797 probe->index + 1, probe->enabled);
799 if (sr_scpi_send(scpi, command) != SR_OK)
801 state->analog_channels[probe->index].state = probe->enabled;
802 setup_changed = TRUE;
806 * A digital POD needs to be enabled for every group of
810 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
812 if (probe->enabled == state->digital_channels[probe->index])
814 g_snprintf(command, sizeof(command),
815 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
816 probe->index, probe->enabled);
818 if (sr_scpi_send(scpi, command) != SR_OK)
821 state->digital_channels[probe->index] = probe->enabled;
822 setup_changed = TRUE;
829 for (i = 1; i <= model->digital_pods; ++i) {
830 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
832 g_snprintf(command, sizeof(command),
833 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
834 i, pod_enabled[i - 1]);
835 if (sr_scpi_send(scpi, command) != SR_OK)
837 state->digital_pods[i - 1] = pod_enabled[i - 1];
838 setup_changed = TRUE;
843 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
849 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
852 gboolean digital_added;
853 struct sr_probe *probe;
854 struct dev_context *devc;
855 struct sr_scpi_dev_inst *scpi;
857 if (sdi->status != SR_ST_ACTIVE)
858 return SR_ERR_DEV_CLOSED;
862 digital_added = FALSE;
864 for (l = sdi->probes; l; l = l->next) {
868 /* Only add a single digital probe. */
869 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
870 devc->enabled_probes = g_slist_append(
871 devc->enabled_probes, probe);
872 if (probe->type == SR_PROBE_LOGIC)
873 digital_added = TRUE;
877 if (!devc->enabled_probes)
880 if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
881 sr_err("Invalid probe configuration specified!");
885 if (hmo_setup_probes(sdi) != SR_OK) {
886 sr_err("Failed to setup probe configuration!");
890 sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
892 /* Send header packet to the session bus. */
893 std_session_send_df_header(cb_data, LOG_PREFIX);
895 devc->current_probe = devc->enabled_probes;
897 return hmo_request_data(sdi);
900 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
902 struct dev_context *devc;
903 struct sr_scpi_dev_inst *scpi;
904 struct sr_datafeed_packet packet;
908 packet.type = SR_DF_END;
909 packet.payload = NULL;
910 sr_session_send(sdi, &packet);
912 if (sdi->status != SR_ST_ACTIVE)
913 return SR_ERR_DEV_CLOSED;
917 g_slist_free(devc->enabled_probes);
918 devc->enabled_probes = NULL;
920 sr_scpi_source_remove(scpi);
925 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
927 .longname = "Hameg HMO",
932 .dev_list = dev_list,
933 .dev_clear = dev_clear,
934 .config_get = config_get,
935 .config_set = config_set,
936 .config_list = config_list,
937 .dev_open = dev_open,
938 .dev_close = dev_close,
939 .dev_acquisition_start = dev_acquisition_start,
940 .dev_acquisition_stop = dev_acquisition_stop,