]> sigrok.org Git - libsigrok.git/blob - hardware/hameg-hmo/api.c
hameg-hmo: Reset the number of acquired frames in acquisition stop.
[libsigrok.git] / hardware / hameg-hmo / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdlib.h>
21 #include <glib/gstdio.h>
22 #include "protocol.h"
23
24 #define SERIALCOMM "115200/8n1/flow=1"
25
26 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
27 static struct sr_dev_driver *di = &hameg_hmo_driver_info;
28
29 static const char *manufacturers[] = {
30         "HAMEG",
31 };
32
33 static const int32_t hwopts[] = {
34         SR_CONF_CONN,
35         SR_CONF_SERIALCOMM,
36 };
37
38 struct usb_id_info {
39         uint16_t vendor_id;
40         uint16_t product_id;
41 };
42
43 static struct usb_id_info ho_models[] = {
44         { 0x0403, 0xed72 }, /* HO720 */
45         { 0x0403, 0xed73 }, /* HO730 */
46 };
47
48 enum {
49         PG_INVALID = -1,
50         PG_NONE,
51         PG_ANALOG,
52         PG_DIGITAL,
53 };
54
55 static int init(struct sr_context *sr_ctx)
56 {
57         return std_init(sr_ctx, di, LOG_PREFIX);
58 }
59
60 /**
61  * Find USB serial devices via the USB vendor ID and product ID.
62  *
63  * @param vendor_id Vendor ID of the USB device.
64  * @param product_id Product ID of the USB device.
65  *
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
68  *         by the caller.
69  */
70 static GSList *auto_find_usb(uint16_t vendor_id, uint16_t product_id)
71 {
72 #ifdef __linux__
73         const gchar *usb_dev;
74         const char device_tree[] = "/sys/bus/usb/devices/";
75         GDir *devices_dir, *device_dir;
76         GSList *l = NULL;
77         GSList *tty_devs;
78         GSList *matched_paths;
79         FILE *fd;
80         char tmp[5];
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;
84         const char *file;
85
86         l = NULL;
87         tty_devs = NULL;
88         matched_paths = NULL;
89
90         if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
91                 return NULL;
92
93         /*
94          * Find potential candidates using the vendor ID and product ID
95          * and store them in matched_paths.
96          */
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);
102
103                 if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
104                     !g_file_test(product_path, G_FILE_TEST_EXISTS))
105                         goto skip_device;
106
107                 if ((fd = g_fopen(vendor_path, "r")) == NULL)
108                         goto skip_device;
109
110                 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
111                         fclose(fd);
112                         goto skip_device;
113                 }
114                 read_vendor_id = strtoul(tmp, NULL, 16);
115
116                 fclose(fd);
117
118                 if ((fd = g_fopen(product_path, "r")) == NULL)
119                         goto skip_device;
120
121                 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
122                         fclose(fd);
123                         goto skip_device;
124                 }
125                 read_product_id = strtoul(tmp, NULL, 16);
126
127                 fclose(fd);
128
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,
133                                                         path_copy);
134                 }
135
136 skip_device:
137                 g_free(vendor_path);
138                 g_free(product_path);
139         }
140         g_dir_close(devices_dir);
141
142         /* For every matched device try to find a ttyUSBX subfolder. */
143         for (l = matched_paths; l; l = l->next) {
144                 subdir_path = NULL;
145
146                 device_path = g_strconcat(device_tree, l->data, NULL);
147
148                 if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
149                         g_free(device_path);
150                         continue;
151                 }
152
153                 prefix = g_strconcat(l->data, ":", NULL);
154
155                 while ((file = g_dir_read_name(device_dir))) {
156                         if (g_str_has_prefix(file, prefix)) {
157                                 subdir_path = g_strconcat(device_path,
158                                                 "/", file, NULL);
159                                 break;
160                         }
161                 }
162                 g_dir_close(device_dir);
163
164                 g_free(prefix);
165                 g_free(device_path);
166
167                 if (subdir_path) {
168                         if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
169                                 g_free(subdir_path);
170                                 continue;
171                         }
172                         g_free(subdir_path);
173
174                         while ((file = g_dir_read_name(device_dir))) {
175                                 if (g_str_has_prefix(file, "ttyUSB")) {
176                                         tty_path = g_strconcat("/dev/",
177                                                                file, NULL);
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,
181                                                         tty_path);
182                                         break;
183                                 }
184                         }
185                         g_dir_close(device_dir);
186                 }
187         }
188         g_slist_free_full(matched_paths, g_free);
189
190         return tty_devs;
191 #else
192         return NULL;
193 #endif
194 }
195
196 static int check_manufacturer(const char *manufacturer)
197 {
198         unsigned int i;
199
200         for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
201                 if (!strcmp(manufacturer, manufacturers[i]))
202                         return SR_OK;
203
204         return SR_ERR;
205 }
206
207 static struct sr_dev_inst *hmo_probe_serial_device(const char *serial_device,
208                                                     const char *serial_options)
209 {
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;
214
215         sdi = NULL;
216         devc = NULL;
217         scpi = NULL;
218         hw_info = NULL;
219
220         if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
221                 goto fail;
222
223         sr_info("Probing %s.", serial_device);
224         if (sr_scpi_open(scpi) != SR_OK)
225                 goto fail;
226
227         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
228                 sr_info("Couldn't get IDN response.");
229                 goto fail;
230         }
231
232         if (check_manufacturer(hw_info->manufacturer) != SR_OK)
233                 goto fail;
234
235         if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
236                                     hw_info->manufacturer, hw_info->model,
237                                     hw_info->firmware_version))) {
238                 goto fail;
239         }
240         sr_scpi_hw_info_free(hw_info);
241         hw_info = NULL;
242
243         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
244                 goto fail;
245
246         sdi->driver = di;
247         sdi->priv = devc;
248         sdi->inst_type = SR_INST_SCPI;
249         sdi->conn = scpi;
250
251         if (hmo_init_device(sdi) != SR_OK)
252                 goto fail;
253
254         sr_scpi_close(sdi->conn);
255
256         sdi->status = SR_ST_INACTIVE;
257
258         return sdi;
259
260 fail:
261         if (hw_info)
262                 sr_scpi_hw_info_free(hw_info);
263         if (scpi)
264                 sr_scpi_free(scpi);
265         if (sdi)
266                 sr_dev_inst_free(sdi);
267         if (devc)
268                 g_free(devc);
269
270         return NULL;
271 }
272
273 static GSList *scan(GSList *options)
274 {
275         GSList *devices;
276         struct drv_context *drvc;
277         struct sr_dev_inst *sdi;
278         const char *serial_device, *serial_options;
279         GSList *l, *tty_devs;
280         unsigned int i;
281
282         serial_device = NULL;
283         serial_options = SERIALCOMM;
284         sdi = NULL;
285         devices = NULL;
286         drvc = di->priv;
287         drvc->instances = NULL;
288
289         if (sr_serial_extract_options(options, &serial_device,
290                                       &serial_options) == SR_OK) {
291                 sdi = hmo_probe_serial_device(serial_device, serial_options);
292                 if (sdi != NULL) {
293                         devices = g_slist_append(devices, sdi);
294                         drvc->instances = g_slist_append(drvc->instances, sdi);
295                 }
296         } else {
297                 tty_devs = NULL;
298
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)
302                                 continue;
303                         tty_devs = g_slist_concat(tty_devs, l);
304                 }
305
306                 for (l = tty_devs; l; l = l->next) {
307                         sdi = hmo_probe_serial_device(l->data, serial_options);
308                         if (sdi != NULL) {
309                                 devices = g_slist_append(devices, sdi);
310                                 drvc->instances = g_slist_append(drvc->instances, sdi);
311                         }
312                 }
313
314                 g_slist_free_full(tty_devs, g_free);
315         }
316
317         return devices;
318 }
319
320 static GSList *dev_list(void)
321 {
322         return ((struct drv_context *)(di->priv))->instances;
323 }
324
325 static void clear_helper(void *priv)
326 {
327         unsigned int i;
328         struct dev_context *devc;
329         struct scope_config *model;
330
331         devc = priv;
332         model = devc->model_config;
333
334         hmo_scope_state_free(devc->model_state);
335
336         for (i = 0; i < model->analog_channels; ++i)
337                 g_slist_free(devc->analog_groups[i].probes);
338
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);
342         }
343
344         g_free(devc->analog_groups);
345         g_free(devc->digital_groups);
346
347         g_free(devc);
348 }
349
350 static int dev_clear(void)
351 {
352         return std_dev_clear(di, clear_helper);
353 }
354
355 static int dev_open(struct sr_dev_inst *sdi)
356 {
357         if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
358                 return SR_ERR;
359
360         if (hmo_scope_state_get(sdi) != SR_OK)
361                 return SR_ERR;
362
363         sdi->status = SR_ST_ACTIVE;
364
365         return SR_OK;
366 }
367
368 static int dev_close(struct sr_dev_inst *sdi)
369 {
370         if (sdi->status == SR_ST_INACTIVE)
371                 return SR_OK;
372
373         sr_scpi_close(sdi->conn);
374
375         sdi->status = SR_ST_INACTIVE;
376
377         return SR_OK;
378 }
379
380 static int cleanup(void)
381 {
382         dev_clear();
383
384         return SR_OK;
385 }
386
387 static int check_probe_group(struct dev_context *devc,
388                              const struct sr_probe_group *probe_group)
389 {
390         unsigned int i;
391         struct scope_config *model;
392
393         model = devc->model_config;
394
395         if (!probe_group)
396                 return PG_NONE;
397
398         for (i = 0; i < model->analog_channels; ++i)
399                 if (probe_group == &devc->analog_groups[i])
400                         return PG_ANALOG;
401
402         for (i = 0; i < model->digital_pods; ++i)
403                 if (probe_group == &devc->digital_groups[i])
404                         return PG_DIGITAL;
405
406         sr_err("Invalid probe group specified.");
407
408         return PG_INVALID;
409 }
410
411 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
412                       const struct sr_probe_group *probe_group)
413 {
414         int ret, pg_type;
415         unsigned int i;
416         struct dev_context *devc;
417         struct scope_config *model;
418         struct scope_state *state;
419
420         if (!sdi || !(devc = sdi->priv))
421                 return SR_ERR_ARG;
422
423         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
424                 return SR_ERR;
425
426         ret = SR_ERR_NA;
427         model = devc->model_config;
428         state = devc->model_state;
429
430         switch (key) {
431         case SR_CONF_NUM_TIMEBASE:
432                 *data = g_variant_new_int32(model->num_xdivs);
433                 ret = SR_OK;
434                 break;
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])
442                                         continue;
443                                 *data = g_variant_new_int32(model->num_ydivs);
444                                 ret = SR_OK;
445                                 break;
446                         }
447
448                 } else {
449                         ret = SR_ERR_NA;
450                 }
451                 break;
452         case SR_CONF_TRIGGER_SOURCE:
453                 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
454                 ret = SR_OK;
455                 break;
456         case SR_CONF_COUPLING:
457                 if (pg_type == PG_NONE) {
458                         sr_err("No probe group specified.");
459                         return SR_ERR_PROBE_GROUP;
460                 } else if (pg_type == PG_ANALOG) {
461                         for (i = 0; i < model->analog_channels; ++i) {
462                                 if (probe_group != &devc->analog_groups[i])
463                                         continue;
464                                 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
465                                 ret = SR_OK;
466                                 break;
467                         }
468
469                 } else {
470                         ret = SR_ERR_NA;
471                 }
472                 break;
473         case SR_CONF_SAMPLERATE:
474                 *data = g_variant_new_uint64(state->sample_rate);
475                 ret = SR_OK;
476                 break;
477         default:
478                 ret = SR_ERR_NA;
479         }
480
481         return ret;
482 }
483
484 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
485 {
486         unsigned int i;
487         GVariant *rational[2];
488         GVariantBuilder gvb;
489
490         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
491
492         for (i = 0; i < n; i++) {
493                 rational[0] = g_variant_new_uint64((*array)[i][0]);
494                 rational[1] = g_variant_new_uint64((*array)[i][1]);
495
496                 /* FIXME: Valgrind reports a memory leak here. */
497                 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
498         }
499
500         return g_variant_builder_end(&gvb);
501 }
502
503 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
504                       const struct sr_probe_group *probe_group)
505 {
506         int ret, pg_type;
507         unsigned int i, j;
508         char command[MAX_COMMAND_SIZE], float_str[30];
509         struct dev_context *devc;
510         struct scope_config *model;
511         struct scope_state *state;
512         const char *tmp;
513         uint64_t p, q, tmp_u64;
514         double tmp_d;
515         gboolean update_sample_rate;
516
517         if (!sdi || !(devc = sdi->priv))
518                 return SR_ERR_ARG;
519
520         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
521                 return SR_ERR;
522
523         model = devc->model_config;
524         state = devc->model_state;
525         update_sample_rate = FALSE;
526
527         ret = SR_ERR_NA;
528
529         switch (key) {
530         case SR_CONF_LIMIT_FRAMES:
531                 devc->frame_limit = g_variant_get_uint64(data);
532                 ret = SR_OK;
533                 break;
534         case SR_CONF_TRIGGER_SOURCE:
535                 tmp = g_variant_get_string(data, NULL);
536                 for (i = 0; (*model->trigger_sources)[i]; i++) {
537                         if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
538                                 continue;
539                         state->trigger_source = i;
540                         g_snprintf(command, sizeof(command),
541                                    (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
542                                    (*model->trigger_sources)[i]);
543
544                         ret = sr_scpi_send(sdi->conn, command);
545                         break;
546                 }
547                 break;
548         case SR_CONF_VDIV:
549                 if (pg_type == PG_NONE) {
550                         sr_err("No probe group specified.");
551                         return SR_ERR_PROBE_GROUP;
552                 }
553
554                 g_variant_get(data, "(tt)", &p, &q);
555
556                 for (i = 0; i < model->num_vdivs; i++) {
557                         if (p != (*model->vdivs)[i][0] ||
558                             q != (*model->vdivs)[i][1])
559                                 continue;
560                         for (j = 1; j <= model->analog_channels; ++j) {
561                                 if (probe_group != &devc->analog_groups[j - 1])
562                                         continue;
563                                 state->analog_channels[j - 1].vdiv = i;
564                                 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
565                                 g_snprintf(command, sizeof(command),
566                                            (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
567                                            j, float_str);
568
569                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
570                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
571                                         return SR_ERR;
572
573                                 break;
574                         }
575
576                         ret = SR_OK;
577                         break;
578                 }
579                 break;
580         case SR_CONF_TIMEBASE:
581                 g_variant_get(data, "(tt)", &p, &q);
582
583                 for (i = 0; i < model->num_timebases; i++) {
584                         if (p != (*model->timebases)[i][0] ||
585                             q != (*model->timebases)[i][1])
586                                 continue;
587                         state->timebase = i;
588                         g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
589                         g_snprintf(command, sizeof(command),
590                                    (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
591                                    float_str);
592
593                         ret = sr_scpi_send(sdi->conn, command);
594                         update_sample_rate = TRUE;
595                         break;
596                 }
597                 break;
598         case SR_CONF_HORIZ_TRIGGERPOS:
599                 tmp_d = g_variant_get_double(data);
600
601                 if (tmp_d < 0.0 || tmp_d > 1.0)
602                         return SR_ERR;
603
604                 state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs;
605                 g_snprintf(command, sizeof(command),
606                            (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
607                            state->horiz_triggerpos);
608
609                 ret = sr_scpi_send(sdi->conn, command);
610                 break;
611         case SR_CONF_TRIGGER_SLOPE:
612                 tmp_u64 = g_variant_get_uint64(data);
613
614                 if (tmp_u64 != 0 && tmp_u64 != 1)
615                         return SR_ERR;
616
617                 state->trigger_slope = tmp_u64;
618
619                 g_snprintf(command, sizeof(command),
620                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
621                            tmp_u64 ? "POS" : "NEG");
622
623                 ret = sr_scpi_send(sdi->conn, command);
624                 break;
625         case SR_CONF_COUPLING:
626                 if (pg_type == PG_NONE) {
627                         sr_err("No probe group specified.");
628                         return SR_ERR_PROBE_GROUP;
629                 }
630
631                 tmp = g_variant_get_string(data, NULL);
632
633                 for (i = 0; (*model->coupling_options)[i]; i++) {
634                         if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
635                                 continue;
636                         for (j = 1; j <= model->analog_channels; ++j) {
637                                 if (probe_group != &devc->analog_groups[j - 1])
638                                         continue;
639                                 state->analog_channels[j-1].coupling = i;
640
641                                 g_snprintf(command, sizeof(command),
642                                            (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
643                                            j, tmp);
644
645                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
646                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
647                                         return SR_ERR;
648                                 break;
649                         }
650
651                         ret = SR_OK;
652                         break;
653                 }
654                 break;
655         default:
656                 ret = SR_ERR_NA;
657                 break;
658         }
659
660         if (ret == SR_OK)
661                 ret = sr_scpi_get_opc(sdi->conn);
662
663         if (ret == SR_OK && update_sample_rate)
664                 ret = hmo_update_sample_rate(sdi);
665
666         return ret;
667 }
668
669 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
670                        const struct sr_probe_group *probe_group)
671 {
672         int pg_type;
673         struct dev_context *devc;
674         struct scope_config *model;
675
676         if (!sdi || !(devc = sdi->priv))
677                 return SR_ERR_ARG;
678
679         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
680                 return SR_ERR;
681
682         model = devc->model_config;
683
684         switch (key) {
685         case SR_CONF_DEVICE_OPTIONS:
686                 if (pg_type == PG_NONE) {
687                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
688                                 model->hw_caps, model->num_hwcaps,
689                                 sizeof(int32_t));
690                 } else if (pg_type == PG_ANALOG) {
691                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
692                                 model->analog_hwcaps, model->num_analog_hwcaps,
693                                 sizeof(int32_t));
694                 } else {
695                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
696                                 NULL, 0, sizeof(int32_t));
697                 }
698                 break;
699         case SR_CONF_COUPLING:
700                 if (pg_type == PG_NONE)
701                         return SR_ERR_PROBE_GROUP;
702                 *data = g_variant_new_strv(*model->coupling_options,
703                            g_strv_length((char **)*model->coupling_options));
704                 break;
705         case SR_CONF_TRIGGER_SOURCE:
706                 *data = g_variant_new_strv(*model->trigger_sources,
707                            g_strv_length((char **)*model->trigger_sources));
708                 break;
709         case SR_CONF_TIMEBASE:
710                 *data = build_tuples(model->timebases, model->num_timebases);
711                 break;
712         case SR_CONF_VDIV:
713                 if (pg_type == PG_NONE)
714                         return SR_ERR_PROBE_GROUP;
715                 *data = build_tuples(model->vdivs, model->num_vdivs);
716                 break;
717         default:
718                 return SR_ERR_NA;
719         }
720
721         return SR_OK;
722 }
723
724 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
725 {
726         char command[MAX_COMMAND_SIZE];
727         struct sr_probe *probe;
728         struct dev_context *devc;
729         struct scope_config *model;
730
731         devc = sdi->priv;
732         model = devc->model_config;
733
734         probe = devc->current_probe->data;
735
736         switch (probe->type) {
737         case SR_PROBE_ANALOG:
738                 g_snprintf(command, sizeof(command),
739                            (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
740                            probe->index + 1);
741                 break;
742         case SR_PROBE_LOGIC:
743                 g_snprintf(command, sizeof(command),
744                            (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
745                            probe->index < 8 ? 1 : 2);
746                 break;
747         default:
748                 sr_err("Invalid probe type.");
749                 break;
750         }
751
752         return sr_scpi_send(sdi->conn, command);
753 }
754
755 static int hmo_check_probes(GSList *probes)
756 {
757         GSList *l;
758         struct sr_probe *probe;
759         gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
760
761         enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
762
763         for (l = probes; l; l = l->next) {
764                 probe = l->data;
765                 switch (probe->type) {
766                 case SR_PROBE_ANALOG:
767                         if (probe->index == 2)
768                                 enabled_chan3 = TRUE;
769                         else if (probe->index == 3)
770                                 enabled_chan4 = TRUE;
771                         break;
772                 case SR_PROBE_LOGIC:
773                         if (probe->index < 8)
774                                 enabled_pod1 = TRUE;
775                         else
776                                 enabled_pod2 = TRUE;
777                         break;
778                 default:
779                         return SR_ERR;
780                 }
781         }
782
783         if ((enabled_pod1 && enabled_chan3) ||
784             (enabled_pod2 && enabled_chan4))
785                 return SR_ERR;
786
787         return SR_OK;
788 }
789
790 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
791 {
792         GSList *l;
793         unsigned int i;
794         gboolean *pod_enabled, setup_changed;
795         char command[MAX_COMMAND_SIZE];
796         struct scope_state *state;
797         struct scope_config *model;
798         struct sr_probe *probe;
799         struct dev_context *devc;
800         struct sr_scpi_dev_inst *scpi;
801
802         devc = sdi->priv;
803         scpi = sdi->conn;
804         state = devc->model_state;
805         model = devc->model_config;
806         setup_changed = FALSE;
807
808         pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
809
810         for (l = sdi->probes; l; l = l->next) {
811                 probe = l->data;
812                 switch (probe->type) {
813                 case SR_PROBE_ANALOG:
814                         if (probe->enabled == state->analog_channels[probe->index].state)
815                                 break;
816                         g_snprintf(command, sizeof(command),
817                                    (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
818                                    probe->index + 1, probe->enabled);
819
820                         if (sr_scpi_send(scpi, command) != SR_OK)
821                                 return SR_ERR;
822                         state->analog_channels[probe->index].state = probe->enabled;
823                         setup_changed = TRUE;
824                         break;
825                 case SR_PROBE_LOGIC:
826                         /*
827                          * A digital POD needs to be enabled for every group of
828                          * 8 probes.
829                          */
830                         if (probe->enabled)
831                                 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
832
833                         if (probe->enabled == state->digital_channels[probe->index])
834                                 break;
835                         g_snprintf(command, sizeof(command),
836                                    (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
837                                    probe->index, probe->enabled);
838
839                         if (sr_scpi_send(scpi, command) != SR_OK)
840                                 return SR_ERR;
841
842                         state->digital_channels[probe->index] = probe->enabled;
843                         setup_changed = TRUE;
844                         break;
845                 default:
846                         return SR_ERR;
847                 }
848         }
849
850         for (i = 1; i <= model->digital_pods; ++i) {
851                 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
852                         continue;
853                 g_snprintf(command, sizeof(command),
854                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
855                            i, pod_enabled[i - 1]);
856                 if (sr_scpi_send(scpi, command) != SR_OK)
857                         return SR_ERR;
858                 state->digital_pods[i - 1] = pod_enabled[i - 1];
859                 setup_changed = TRUE;
860         }
861
862         g_free(pod_enabled);
863
864         if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
865                 return SR_ERR;
866
867         return SR_OK;
868 }
869
870 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
871 {
872         GSList *l;
873         gboolean digital_added;
874         struct sr_probe *probe;
875         struct dev_context *devc;
876         struct sr_scpi_dev_inst *scpi;
877
878         if (sdi->status != SR_ST_ACTIVE)
879                 return SR_ERR_DEV_CLOSED;
880
881         scpi = sdi->conn;
882         devc = sdi->priv;
883         digital_added = FALSE;
884
885         for (l = sdi->probes; l; l = l->next) {
886                 probe = l->data;
887                 if (!probe->enabled)
888                         continue;
889                 /* Only add a single digital probe. */
890                 if (probe->type != SR_PROBE_LOGIC || !digital_added) {
891                         devc->enabled_probes = g_slist_append(
892                                         devc->enabled_probes, probe);
893                         if (probe->type == SR_PROBE_LOGIC)
894                                 digital_added = TRUE;
895                 }
896         }
897
898         if (!devc->enabled_probes)
899                 return SR_ERR;
900
901         if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
902                 sr_err("Invalid probe configuration specified!");
903                 return SR_ERR_NA;
904         }
905
906         if (hmo_setup_probes(sdi) != SR_OK) {
907                 sr_err("Failed to setup probe configuration!");
908                 return SR_ERR;
909         }
910
911         sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
912
913         /* Send header packet to the session bus. */
914         std_session_send_df_header(cb_data, LOG_PREFIX);
915
916         devc->current_probe = devc->enabled_probes;
917
918         return hmo_request_data(sdi);
919 }
920
921 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
922 {
923         struct dev_context *devc;
924         struct sr_scpi_dev_inst *scpi;
925         struct sr_datafeed_packet packet;
926
927         (void)cb_data;
928
929         packet.type = SR_DF_END;
930         packet.payload = NULL;
931         sr_session_send(sdi, &packet);
932
933         if (sdi->status != SR_ST_ACTIVE)
934                 return SR_ERR_DEV_CLOSED;
935
936         devc = sdi->priv;
937
938         devc->num_frames = 0;
939         g_slist_free(devc->enabled_probes);
940         devc->enabled_probes = NULL;
941         scpi = sdi->conn;
942         sr_scpi_source_remove(scpi);
943
944         return SR_OK;
945 }
946
947 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
948         .name = "hameg-hmo",
949         .longname = "Hameg HMO",
950         .api_version = 1,
951         .init = init,
952         .cleanup = cleanup,
953         .scan = scan,
954         .dev_list = dev_list,
955         .dev_clear = dev_clear,
956         .config_get = config_get,
957         .config_set = config_set,
958         .config_list = config_list,
959         .dev_open = dev_open,
960         .dev_close = dev_close,
961         .dev_acquisition_start = dev_acquisition_start,
962         .dev_acquisition_stop = dev_acquisition_stop,
963         .priv = NULL,
964 };