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>
25 #define SERIALCOMM "115200/8n1/flow=1"
27 static const int32_t hwopts[] = {
37 static struct usb_id_info ho_models[] = {
40 .product_id = 0xed72, /* HO720 */
44 .product_id = 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 null
60 * if no serial device is found. The returned list must be freed by the caller.
62 static GSList *auto_find_usb(unsigned long vendor_id,
63 unsigned long product_id)
67 const char device_tree[] = "/sys/bus/usb/devices/";
73 GSList *matched_paths;
79 if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
83 * Find potential candidates using the vendor ID and product ID
84 * and store them in matched_paths
86 while ((usb_dev = g_dir_read_name(devices_dir))) {
93 unsigned long read_vendor_id;
94 unsigned long read_product_id;
96 vendor_path = g_strconcat(device_tree,
97 usb_dev, "/idVendor", NULL);
98 product_path = g_strconcat(device_tree,
99 usb_dev, "/idProduct", NULL);
101 if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
102 !g_file_test(product_path, G_FILE_TEST_EXISTS))
105 if ((fd = g_fopen(vendor_path, "r")) == NULL)
108 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
112 read_vendor_id = strtoul(tmp, NULL, 16);
116 if ((fd = g_fopen(product_path, "r")) == NULL)
119 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
123 read_product_id = strtoul(tmp, NULL, 16);
127 if (vendor_id == read_vendor_id &&
128 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) {
154 device_path = g_strconcat(device_tree, l->data, NULL);
156 if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
161 prefix = g_strconcat(l->data, ":", NULL);
163 while ((file = g_dir_read_name(device_dir))) {
164 if (g_str_has_prefix(file, prefix)) {
165 subdir_path = g_strconcat(device_path,
171 g_dir_close(device_dir);
177 if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
183 while ((file = g_dir_read_name(device_dir))) {
184 if (g_str_has_prefix(file, "ttyUSB")) {
187 tty_path = g_strconcat("/dev/",
189 sr_dbg("Found USB device %04x:%04x attached to %s",
190 vendor_id, product_id, tty_path);
191 tty_devices = g_slist_prepend(tty_devices,
196 g_dir_close(device_dir);
199 g_slist_free_full(matched_paths, g_free);
207 static GSList *scan(GSList *options)
211 struct drv_context *drvc;
212 struct sr_dev_inst *sdi;
214 const char *serial_device;
215 const char *serial_options;
217 serial_device = NULL;
218 serial_options = SERIALCOMM;
223 drvc->instances = NULL;
225 if (sr_serial_extract_options(options, &serial_device,
226 &serial_options) == SR_OK) {
227 sdi = hameg_probe_serial_device(serial_device, serial_options);
230 devices = g_slist_append(devices, sdi);
231 drvc->instances = g_slist_append(drvc->instances, sdi);
242 for (i = 0; i < ARRAY_SIZE(ho_models); i++) {
243 if ((l = auto_find_usb(ho_models[i].vendor_id,
244 ho_models[i].product_id)) == NULL)
247 tty_devices = g_slist_concat(tty_devices, l);
250 for (l = tty_devices; l; l = l->next) {
251 sdi = hameg_probe_serial_device(l->data, serial_options);
253 devices = g_slist_append(devices, sdi);
254 drvc->instances = g_slist_append(drvc->instances, sdi);
258 g_slist_free_full(tty_devices, g_free);
264 static GSList *dev_list(void)
266 return ((struct drv_context *)(di->priv))->instances;
269 static void clear_helper(void *priv)
273 struct dev_context *devc;
274 struct scope_config *model;
277 model = devc->model_config;
279 scope_state_free(devc->model_state);
281 for (i = 0; i < model->analog_channels; ++i) {
282 g_slist_free(devc->analog_groups[i].probes);
285 for (i = 0; i < model->digital_pods; ++i) {
286 g_slist_free(devc->digital_groups[i].probes);
287 g_free(devc->digital_groups[i].name);
290 g_free(devc->analog_groups);
291 g_free(devc->digital_groups);
296 static int dev_clear(void)
298 return std_dev_clear(di, clear_helper);
301 static int dev_open(struct sr_dev_inst *sdi)
303 if (sdi->status != SR_ST_ACTIVE &&
304 serial_open(sdi->conn, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
307 if (scope_state_get(sdi) != SR_OK)
310 sdi->status = SR_ST_ACTIVE;
315 static int dev_close(struct sr_dev_inst *sdi)
317 if (sdi->status == SR_ST_INACTIVE)
320 serial_close(sdi->conn);
322 sdi->status = SR_ST_INACTIVE;
327 static int cleanup(void)
334 static int check_probe_group(struct dev_context *devc,
335 const struct sr_probe_group *probe_group)
338 struct scope_config *model;
340 model = devc->model_config;
345 for (i = 0; i < model->analog_channels; ++i)
346 if (probe_group == &devc->analog_groups[i])
349 for (i = 0; i < model->digital_pods; ++i)
350 if (probe_group == &devc->digital_groups[i])
353 sr_err("Invalid probe group specified.");
357 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
358 const struct sr_probe_group *probe_group)
364 struct dev_context *devc;
365 struct scope_config *model;
367 if (!sdi || !(devc = sdi->priv))
370 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
374 model = devc->model_config;
377 case SR_CONF_NUM_TIMEBASE:
378 *data = g_variant_new_int32(model->num_xdivs);
382 case SR_CONF_NUM_VDIV:
383 if (pg_type == PG_NONE) {
384 sr_err("No probe group specified.");
385 return SR_ERR_PROBE_GROUP;
387 } else if (pg_type == PG_ANALOG) {
388 for (i = 0; i < model->analog_channels; ++i) {
389 if (probe_group == &devc->analog_groups[i]) {
390 *data = g_variant_new_int32(model->num_ydivs);
408 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
412 GVariant *rational[2];
415 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
417 for (i = 0; i < n; i++) {
418 rational[0] = g_variant_new_uint64((*array)[i][0]);
419 rational[1] = g_variant_new_uint64((*array)[i][1]);
421 /* FIXME valgrind reports a memory leak here */
422 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
425 return g_variant_builder_end(&gvb);
428 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
429 const struct sr_probe_group *probe_group)
434 char command[MAX_COMMAND_SIZE];
436 struct dev_context *devc;
437 struct scope_config *model;
438 struct scope_state *state;
440 if (!sdi || !(devc = sdi->priv))
443 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
446 model = devc->model_config;
447 state = devc->model_state;
452 case SR_CONF_LIMIT_FRAMES:
453 devc->frame_limit = g_variant_get_uint64(data);
457 case SR_CONF_TRIGGER_SOURCE:
461 tmp = g_variant_get_string(data, NULL);
463 for (i = 0; (*model->trigger_sources)[i]; i++) {
464 if (!g_strcmp0(tmp, (*model->trigger_sources)[i])) {
465 state->trigger_source = i;
467 g_snprintf(command, sizeof(command),
468 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
469 (*model->trigger_sources)[i]);
471 ret = sr_scpi_send(sdi->conn, command);
483 if (pg_type == PG_NONE) {
484 sr_err("No probe group specified.");
485 return SR_ERR_PROBE_GROUP;
488 g_variant_get(data, "(tt)", &p, &q);
490 for (i = 0; i < model->num_vdivs; i++) {
491 if (p == (*model->vdivs)[i][0] &&
492 q == (*model->vdivs)[i][1]){
493 for (j = 1; j <= model->analog_channels; ++j) {
494 if (probe_group == &devc->analog_groups[j - 1]) {
495 state->analog_channels[j-1].vdiv = (float) p / q;
496 g_snprintf(command, sizeof(command),
497 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
498 j, state->analog_channels[j-1].vdiv);
500 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
501 sr_scpi_get_opc(sdi->conn) != SR_OK)
515 case SR_CONF_TIMEBASE:
519 g_variant_get(data, "(tt)", &p, &q);
521 for (i = 0; i < model->num_timebases; i++) {
522 if (p == (*model->timebases)[i][0] &&
523 q == (*model->timebases)[i][1]){
524 state->timebase = (float) p / q;
525 g_snprintf(command, sizeof(command),
526 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
529 ret = sr_scpi_send(sdi->conn, command);
536 case SR_CONF_HORIZ_TRIGGERPOS:
540 tmp = g_variant_get_double(data);
542 if (tmp < 0.0 || tmp > 1.0)
545 state->horiz_triggerpos = -(tmp - 0.5) * state->timebase * model->num_xdivs;
546 g_snprintf(command, sizeof(command),
547 (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
548 state->horiz_triggerpos);
550 ret = sr_scpi_send(sdi->conn, command);
554 case SR_CONF_TRIGGER_SLOPE:
558 tmp = g_variant_get_uint64(data);
560 if (tmp != 0 && tmp != 1)
563 state->trigger_slope = tmp;
565 g_snprintf(command, sizeof(command),
566 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
567 tmp ? "POS" : "NEG");
569 ret = sr_scpi_send(sdi->conn, command);
573 case SR_CONF_COUPLING:
578 if (pg_type == PG_NONE) {
579 sr_err("No probe group specified.");
580 return SR_ERR_PROBE_GROUP;
583 tmp = g_variant_get_string(data, NULL);
585 for (i = 0; (*model->coupling_options)[i]; i++) {
586 if (!strcmp(tmp, (*model->coupling_options)[i])) {
587 for (j = 1; j <= model->analog_channels; ++j) {
588 if (probe_group == &devc->analog_groups[j - 1]) {
589 state->analog_channels[j-1].coupling = i;
591 g_snprintf(command, sizeof(command),
592 (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
595 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
596 sr_scpi_get_opc(sdi->conn) != SR_OK)
615 ret = sr_scpi_get_opc(sdi->conn);
620 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
621 const struct sr_probe_group *probe_group)
625 struct dev_context *devc;
626 struct scope_config *model;
628 if (!sdi || !(devc = sdi->priv))
631 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
634 model = devc->model_config;
637 case SR_CONF_DEVICE_OPTIONS:
638 if (pg_type == PG_NONE) {
639 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
643 } else if (pg_type == PG_ANALOG) {
644 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
645 model->analog_hwcaps,
646 model->num_analog_hwcaps,
649 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
650 NULL, 0, sizeof(int32_t));
654 case SR_CONF_COUPLING:
655 if (pg_type == PG_NONE)
656 return SR_ERR_PROBE_GROUP;
658 *data = g_variant_new_strv(*model->coupling_options,
659 g_strv_length((char **) *model->coupling_options));
662 case SR_CONF_TRIGGER_SOURCE:
663 *data = g_variant_new_strv(*model->trigger_sources,
664 g_strv_length((char **) *model->trigger_sources));
667 case SR_CONF_TIMEBASE:
668 *data = build_tuples(model->timebases, model->num_timebases);
672 if (pg_type == PG_NONE)
673 return SR_ERR_PROBE_GROUP;
675 *data = build_tuples(model->vdivs, model->num_vdivs);
685 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
687 char command[MAX_COMMAND_SIZE];
689 struct sr_probe *probe;
690 struct dev_context *devc;
691 struct scope_config *model;
694 model = devc->model_config;
696 probe = devc->current_probe->data;
698 switch (probe->type) {
699 case SR_PROBE_ANALOG:
700 g_snprintf(command, sizeof(command),
701 (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
705 g_snprintf(command, sizeof(command),
706 (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
707 probe->index < 8 ? 1 : 2);
710 sr_err("Invalid probe type");
714 return sr_scpi_send(sdi->conn, command);
717 static int hmo_check_probes(GSList *probes)
720 gboolean enabled_pod1;
721 gboolean enabled_pod2;
722 gboolean enabled_chan3;
723 gboolean enabled_chan4;
725 struct sr_probe *probe;
727 enabled_pod1 = FALSE;
728 enabled_pod2 = FALSE;
729 enabled_chan3 = FALSE;
730 enabled_chan4 = FALSE;
732 for (l = probes; l; l = l->next) {
735 switch (probe->type) {
736 case SR_PROBE_ANALOG:
737 if (probe->index == 2)
738 enabled_chan3 = TRUE;
739 else if (probe->index == 3)
740 enabled_chan4 = TRUE;
744 if (probe->index < 8)
755 if ((enabled_pod1 && enabled_chan3) ||
756 (enabled_pod2 && enabled_chan4))
762 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
766 gboolean *pod_enabled;
767 char command[MAX_COMMAND_SIZE];
769 struct scope_state *state;
770 struct scope_config *model;
772 struct sr_probe *probe;
773 struct dev_context *devc;
774 struct sr_serial_dev_inst *serial;
778 state = devc->model_state;
779 model = devc->model_config;
781 pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
783 for (l = sdi->probes; l; l = l->next) {
786 switch (probe->type) {
787 case SR_PROBE_ANALOG:
789 if (probe->enabled != state->analog_channels[probe->index].state) {
790 g_snprintf(command, sizeof(command),
791 (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
792 probe->index + 1, probe->enabled);
794 if (sr_scpi_send(serial, command) != SR_OK)
796 state->analog_channels[probe->index].state = probe->enabled;
804 * A digital POD needs to be enabled for every group of
808 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
810 if (probe->enabled != state->digital_channels[probe->index]) {
811 g_snprintf(command, sizeof(command),
812 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
813 probe->index, probe->enabled);
815 if (sr_scpi_send(serial, command) != SR_OK)
818 state->digital_channels[probe->index] = probe->enabled;
828 for (i = 1; i <= model->digital_pods; ++i) {
829 if (state->digital_pods[i-1] != pod_enabled[i-1]) {
830 g_snprintf(command, sizeof(command),
831 (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
832 i, pod_enabled[i-1]);
834 if (sr_scpi_send(serial, command) != SR_OK)
837 state->digital_pods[i-1] = pod_enabled[i-1];
846 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
849 gboolean digital_added;
851 struct sr_probe *probe;
852 struct dev_context *devc;
853 struct sr_serial_dev_inst *serial;
855 if (sdi->status != SR_ST_ACTIVE)
856 return SR_ERR_DEV_CLOSED;
860 digital_added = FALSE;
862 for (l = sdi->probes; l; l = l->next) {
865 if (probe->enabled) {
866 /* Only add a single digital probe */
867 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
868 devc->enabled_probes = g_slist_append(devc->enabled_probes,
870 if (probe->type == SR_PROBE_LOGIC)
871 digital_added = TRUE;
876 if (!devc->enabled_probes)
879 if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
880 sr_err("Invalid probe configuration specified!");
884 if (hmo_setup_probes(sdi) != SR_OK) {
885 sr_err("Failed to setup probe configuration!");
889 sr_source_add(serial->fd, G_IO_IN, 50, hameg_hmo_receive_data, (void *)sdi);
891 /* Send header packet to the session bus. */
892 std_session_send_df_header(cb_data, LOG_PREFIX);
894 devc->current_probe = devc->enabled_probes;
896 return hmo_request_data(sdi);
899 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
901 struct dev_context *devc;
902 struct sr_serial_dev_inst *serial;
906 if (sdi->status != SR_ST_ACTIVE)
907 return SR_ERR_DEV_CLOSED;
911 g_slist_free(devc->enabled_probes);
912 devc->enabled_probes = NULL;
914 sr_source_remove(serial->fd);
919 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
921 .longname = "Hameg HMO scope driver",
926 .dev_list = dev_list,
927 .dev_clear = dev_clear,
928 .config_get = config_get,
929 .config_set = config_set,
930 .config_list = config_list,
931 .dev_open = dev_open,
932 .dev_close = dev_close,
933 .dev_acquisition_start = dev_acquisition_start,
934 .dev_acquisition_stop = dev_acquisition_stop,