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);
199 static int check_manufacturer(const char *manufacturer)
203 for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
204 if (!strcmp(manufacturer, manufacturers[i]))
210 static struct sr_dev_inst *hmo_probe_serial_device(const char *serial_device,
211 const char *serial_options)
213 struct sr_dev_inst *sdi;
214 struct dev_context *devc;
215 struct sr_scpi_hw_info *hw_info;
216 struct sr_scpi_dev_inst *scpi;
223 if (!(scpi = scpi_dev_inst_new(di->priv, serial_device, serial_options)))
226 sr_info("Probing %s.", serial_device);
227 if (sr_scpi_open(scpi) != SR_OK)
230 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
231 sr_info("Couldn't get IDN response.");
235 if (check_manufacturer(hw_info->manufacturer) != SR_OK)
238 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
239 hw_info->manufacturer, hw_info->model,
240 hw_info->firmware_version))) {
243 sr_scpi_hw_info_free(hw_info);
246 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
251 sdi->inst_type = SR_INST_SCPI;
254 if (hmo_init_device(sdi) != SR_OK)
257 sr_scpi_close(sdi->conn);
259 sdi->status = SR_ST_INACTIVE;
265 sr_scpi_hw_info_free(hw_info);
269 sr_dev_inst_free(sdi);
276 static GSList *scan(GSList *options)
279 struct drv_context *drvc;
280 struct sr_dev_inst *sdi;
281 const char *serial_device, *serial_options;
282 GSList *l, *tty_devs;
285 serial_device = NULL;
286 serial_options = SERIALCOMM;
290 drvc->instances = NULL;
292 if (sr_serial_extract_options(options, &serial_device,
293 &serial_options) == SR_OK) {
294 sdi = hmo_probe_serial_device(serial_device, serial_options);
296 devices = g_slist_append(devices, sdi);
297 drvc->instances = g_slist_append(drvc->instances, sdi);
302 for (i = 0; i < ARRAY_SIZE(ho_models); i++) {
303 if ((l = auto_find_usb(ho_models[i].vendor_id,
304 ho_models[i].product_id)) == NULL)
306 tty_devs = g_slist_concat(tty_devs, l);
309 for (l = tty_devs; l; l = l->next) {
310 sdi = hmo_probe_serial_device(l->data, serial_options);
312 devices = g_slist_append(devices, sdi);
313 drvc->instances = g_slist_append(drvc->instances, sdi);
317 g_slist_free_full(tty_devs, g_free);
323 static GSList *dev_list(void)
325 return ((struct drv_context *)(di->priv))->instances;
328 static void clear_helper(void *priv)
331 struct dev_context *devc;
332 struct scope_config *model;
335 model = devc->model_config;
337 hmo_scope_state_free(devc->model_state);
339 for (i = 0; i < model->analog_channels; ++i)
340 g_slist_free(devc->analog_groups[i].probes);
342 for (i = 0; i < model->digital_pods; ++i) {
343 g_slist_free(devc->digital_groups[i].probes);
344 g_free(devc->digital_groups[i].name);
347 g_free(devc->analog_groups);
348 g_free(devc->digital_groups);
353 static int dev_clear(void)
355 return std_dev_clear(di, clear_helper);
358 static int dev_open(struct sr_dev_inst *sdi)
360 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
363 if (hmo_scope_state_get(sdi) != SR_OK)
366 sdi->status = SR_ST_ACTIVE;
371 static int dev_close(struct sr_dev_inst *sdi)
373 if (sdi->status == SR_ST_INACTIVE)
376 sr_scpi_close(sdi->conn);
378 sdi->status = SR_ST_INACTIVE;
383 static int cleanup(void)
390 static int check_probe_group(struct dev_context *devc,
391 const struct sr_probe_group *probe_group)
394 struct scope_config *model;
396 model = devc->model_config;
401 for (i = 0; i < model->analog_channels; ++i)
402 if (probe_group == &devc->analog_groups[i])
405 for (i = 0; i < model->digital_pods; ++i)
406 if (probe_group == &devc->digital_groups[i])
409 sr_err("Invalid probe group specified.");
414 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
415 const struct sr_probe_group *probe_group)
419 struct dev_context *devc;
420 struct scope_config *model;
421 struct scope_state *state;
423 if (!sdi || !(devc = sdi->priv))
426 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
430 model = devc->model_config;
431 state = devc->model_state;
434 case SR_CONF_NUM_TIMEBASE:
435 *data = g_variant_new_int32(model->num_xdivs);
438 case SR_CONF_NUM_VDIV:
439 if (pg_type == PG_NONE) {
440 sr_err("No probe group specified.");
441 return SR_ERR_PROBE_GROUP;
442 } else if (pg_type == PG_ANALOG) {
443 for (i = 0; i < model->analog_channels; ++i) {
444 if (probe_group != &devc->analog_groups[i])
446 *data = g_variant_new_int32(model->num_ydivs);
455 case SR_CONF_TRIGGER_SOURCE:
456 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
459 case SR_CONF_COUPLING:
460 if (pg_type == PG_NONE) {
461 sr_err("No probe group specified.");
462 return SR_ERR_PROBE_GROUP;
463 } else if (pg_type == PG_ANALOG) {
464 for (i = 0; i < model->analog_channels; ++i) {
465 if (probe_group != &devc->analog_groups[i])
467 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
476 case SR_CONF_SAMPLERATE:
477 *data = g_variant_new_uint64(state->sample_rate);
487 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
490 GVariant *rational[2];
493 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
495 for (i = 0; i < n; i++) {
496 rational[0] = g_variant_new_uint64((*array)[i][0]);
497 rational[1] = g_variant_new_uint64((*array)[i][1]);
499 /* FIXME: Valgrind reports a memory leak here. */
500 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
503 return g_variant_builder_end(&gvb);
506 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
507 const struct sr_probe_group *probe_group)
511 char command[MAX_COMMAND_SIZE], float_str[30];
512 struct dev_context *devc;
513 struct scope_config *model;
514 struct scope_state *state;
518 gboolean update_sample_rate;
520 if (!sdi || !(devc = sdi->priv))
523 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
526 model = devc->model_config;
527 state = devc->model_state;
528 update_sample_rate = FALSE;
533 case SR_CONF_LIMIT_FRAMES:
534 devc->frame_limit = g_variant_get_uint64(data);
537 case SR_CONF_TRIGGER_SOURCE:
538 tmp = g_variant_get_string(data, NULL);
539 for (i = 0; (*model->trigger_sources)[i]; i++) {
540 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
542 state->trigger_source = i;
543 g_snprintf(command, sizeof(command),
544 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
545 (*model->trigger_sources)[i]);
547 ret = sr_scpi_send(sdi->conn, command);
552 if (pg_type == PG_NONE) {
553 sr_err("No probe group specified.");
554 return SR_ERR_PROBE_GROUP;
557 g_variant_get(data, "(tt)", &p, &q);
559 for (i = 0; i < model->num_vdivs; i++) {
560 if (p != (*model->vdivs)[i][0] ||
561 q != (*model->vdivs)[i][1])
563 for (j = 1; j <= model->analog_channels; ++j) {
564 if (probe_group != &devc->analog_groups[j - 1])
566 state->analog_channels[j - 1].vdiv = i;
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_VERTICAL_DIV],
572 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
573 sr_scpi_get_opc(sdi->conn) != SR_OK)
583 case SR_CONF_TIMEBASE:
584 g_variant_get(data, "(tt)", &p, &q);
586 for (i = 0; i < model->num_timebases; i++) {
587 if (p != (*model->timebases)[i][0] ||
588 q != (*model->timebases)[i][1])
591 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
592 g_snprintf(command, sizeof(command),
593 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
596 ret = sr_scpi_send(sdi->conn, command);
597 update_sample_rate = TRUE;
601 case SR_CONF_HORIZ_TRIGGERPOS:
602 tmp_d = g_variant_get_double(data);
604 if (tmp_d < 0.0 || tmp_d > 1.0)
607 state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs;
608 g_snprintf(command, sizeof(command),
609 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
610 state->horiz_triggerpos);
612 ret = sr_scpi_send(sdi->conn, command);
614 case SR_CONF_TRIGGER_SLOPE:
615 tmp = g_variant_get_string(data, NULL);
617 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
620 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
622 g_snprintf(command, sizeof(command),
623 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
624 (state->trigger_slope == 0) ? "POS" : "NEG");
626 ret = sr_scpi_send(sdi->conn, command);
628 case SR_CONF_COUPLING:
629 if (pg_type == PG_NONE) {
630 sr_err("No probe group specified.");
631 return SR_ERR_PROBE_GROUP;
634 tmp = g_variant_get_string(data, NULL);
636 for (i = 0; (*model->coupling_options)[i]; i++) {
637 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
639 for (j = 1; j <= model->analog_channels; ++j) {
640 if (probe_group != &devc->analog_groups[j - 1])
642 state->analog_channels[j-1].coupling = i;
644 g_snprintf(command, sizeof(command),
645 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
648 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
649 sr_scpi_get_opc(sdi->conn) != SR_OK)
664 ret = sr_scpi_get_opc(sdi->conn);
666 if (ret == SR_OK && update_sample_rate)
667 ret = hmo_update_sample_rate(sdi);
672 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
673 const struct sr_probe_group *probe_group)
676 struct dev_context *devc;
677 struct scope_config *model;
679 if (!sdi || !(devc = sdi->priv))
682 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
685 model = devc->model_config;
688 case SR_CONF_DEVICE_OPTIONS:
689 if (pg_type == PG_NONE) {
690 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
691 model->hw_caps, model->num_hwcaps,
693 } else if (pg_type == PG_ANALOG) {
694 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
695 model->analog_hwcaps, model->num_analog_hwcaps,
698 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
699 NULL, 0, sizeof(int32_t));
702 case SR_CONF_COUPLING:
703 if (pg_type == PG_NONE)
704 return SR_ERR_PROBE_GROUP;
705 *data = g_variant_new_strv(*model->coupling_options,
706 g_strv_length((char **)*model->coupling_options));
708 case SR_CONF_TRIGGER_SOURCE:
709 *data = g_variant_new_strv(*model->trigger_sources,
710 g_strv_length((char **)*model->trigger_sources));
712 case SR_CONF_TIMEBASE:
713 *data = build_tuples(model->timebases, model->num_timebases);
716 if (pg_type == PG_NONE)
717 return SR_ERR_PROBE_GROUP;
718 *data = build_tuples(model->vdivs, model->num_vdivs);
727 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
729 char command[MAX_COMMAND_SIZE];
730 struct sr_probe *probe;
731 struct dev_context *devc;
732 struct scope_config *model;
735 model = devc->model_config;
737 probe = devc->current_probe->data;
739 switch (probe->type) {
740 case SR_PROBE_ANALOG:
741 g_snprintf(command, sizeof(command),
742 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
746 g_snprintf(command, sizeof(command),
747 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
748 probe->index < 8 ? 1 : 2);
751 sr_err("Invalid probe type.");
755 return sr_scpi_send(sdi->conn, command);
758 static int hmo_check_probes(GSList *probes)
761 struct sr_probe *probe;
762 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
764 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
766 for (l = probes; l; l = l->next) {
768 switch (probe->type) {
769 case SR_PROBE_ANALOG:
770 if (probe->index == 2)
771 enabled_chan3 = TRUE;
772 else if (probe->index == 3)
773 enabled_chan4 = TRUE;
776 if (probe->index < 8)
786 if ((enabled_pod1 && enabled_chan3) ||
787 (enabled_pod2 && enabled_chan4))
793 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
797 gboolean *pod_enabled, setup_changed;
798 char command[MAX_COMMAND_SIZE];
799 struct scope_state *state;
800 struct scope_config *model;
801 struct sr_probe *probe;
802 struct dev_context *devc;
803 struct sr_scpi_dev_inst *scpi;
807 state = devc->model_state;
808 model = devc->model_config;
809 setup_changed = FALSE;
811 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
813 for (l = sdi->probes; l; l = l->next) {
815 switch (probe->type) {
816 case SR_PROBE_ANALOG:
817 if (probe->enabled == state->analog_channels[probe->index].state)
819 g_snprintf(command, sizeof(command),
820 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
821 probe->index + 1, probe->enabled);
823 if (sr_scpi_send(scpi, command) != SR_OK)
825 state->analog_channels[probe->index].state = probe->enabled;
826 setup_changed = TRUE;
830 * A digital POD needs to be enabled for every group of
834 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
836 if (probe->enabled == state->digital_channels[probe->index])
838 g_snprintf(command, sizeof(command),
839 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
840 probe->index, probe->enabled);
842 if (sr_scpi_send(scpi, command) != SR_OK)
845 state->digital_channels[probe->index] = probe->enabled;
846 setup_changed = TRUE;
853 for (i = 1; i <= model->digital_pods; ++i) {
854 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
856 g_snprintf(command, sizeof(command),
857 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
858 i, pod_enabled[i - 1]);
859 if (sr_scpi_send(scpi, command) != SR_OK)
861 state->digital_pods[i - 1] = pod_enabled[i - 1];
862 setup_changed = TRUE;
867 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
873 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
876 gboolean digital_added;
877 struct sr_probe *probe;
878 struct dev_context *devc;
879 struct sr_scpi_dev_inst *scpi;
881 if (sdi->status != SR_ST_ACTIVE)
882 return SR_ERR_DEV_CLOSED;
886 digital_added = FALSE;
888 for (l = sdi->probes; l; l = l->next) {
892 /* Only add a single digital probe. */
893 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
894 devc->enabled_probes = g_slist_append(
895 devc->enabled_probes, probe);
896 if (probe->type == SR_PROBE_LOGIC)
897 digital_added = TRUE;
901 if (!devc->enabled_probes)
904 if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
905 sr_err("Invalid probe configuration specified!");
909 if (hmo_setup_probes(sdi) != SR_OK) {
910 sr_err("Failed to setup probe configuration!");
914 sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
916 /* Send header packet to the session bus. */
917 std_session_send_df_header(cb_data, LOG_PREFIX);
919 devc->current_probe = devc->enabled_probes;
921 return hmo_request_data(sdi);
924 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
926 struct dev_context *devc;
927 struct sr_scpi_dev_inst *scpi;
928 struct sr_datafeed_packet packet;
932 packet.type = SR_DF_END;
933 packet.payload = NULL;
934 sr_session_send(sdi, &packet);
936 if (sdi->status != SR_ST_ACTIVE)
937 return SR_ERR_DEV_CLOSED;
941 devc->num_frames = 0;
942 g_slist_free(devc->enabled_probes);
943 devc->enabled_probes = NULL;
945 sr_scpi_source_remove(scpi);
950 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
952 .longname = "Hameg HMO",
957 .dev_list = dev_list,
958 .dev_clear = dev_clear,
959 .config_get = config_get,
960 .config_set = config_set,
961 .config_list = config_list,
962 .dev_open = dev_open,
963 .dev_close = dev_close,
964 .dev_acquisition_start = dev_acquisition_start,
965 .dev_acquisition_stop = dev_acquisition_stop,