]> sigrok.org Git - libsigrok.git/blob - hardware/hameg-hmo/api.c
hwdriver: Change TRIGGER_SLOPE setting to string type.
[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         (void)vendor_id;
193         (void)product_id;
194
195         return NULL;
196 #endif
197 }
198
199 static int check_manufacturer(const char *manufacturer)
200 {
201         unsigned int i;
202
203         for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
204                 if (!strcmp(manufacturer, manufacturers[i]))
205                         return SR_OK;
206
207         return SR_ERR;
208 }
209
210 static struct sr_dev_inst *hmo_probe_serial_device(const char *serial_device,
211                                                     const char *serial_options)
212 {
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;
217
218         sdi = NULL;
219         devc = NULL;
220         scpi = NULL;
221         hw_info = NULL;
222
223         if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
224                 goto fail;
225
226         sr_info("Probing %s.", serial_device);
227         if (sr_scpi_open(scpi) != SR_OK)
228                 goto fail;
229
230         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
231                 sr_info("Couldn't get IDN response.");
232                 goto fail;
233         }
234
235         if (check_manufacturer(hw_info->manufacturer) != SR_OK)
236                 goto fail;
237
238         if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
239                                     hw_info->manufacturer, hw_info->model,
240                                     hw_info->firmware_version))) {
241                 goto fail;
242         }
243         sr_scpi_hw_info_free(hw_info);
244         hw_info = NULL;
245
246         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
247                 goto fail;
248
249         sdi->driver = di;
250         sdi->priv = devc;
251         sdi->inst_type = SR_INST_SCPI;
252         sdi->conn = scpi;
253
254         if (hmo_init_device(sdi) != SR_OK)
255                 goto fail;
256
257         sr_scpi_close(sdi->conn);
258
259         sdi->status = SR_ST_INACTIVE;
260
261         return sdi;
262
263 fail:
264         if (hw_info)
265                 sr_scpi_hw_info_free(hw_info);
266         if (scpi)
267                 sr_scpi_free(scpi);
268         if (sdi)
269                 sr_dev_inst_free(sdi);
270         if (devc)
271                 g_free(devc);
272
273         return NULL;
274 }
275
276 static GSList *scan(GSList *options)
277 {
278         GSList *devices;
279         struct drv_context *drvc;
280         struct sr_dev_inst *sdi;
281         const char *serial_device, *serial_options;
282         GSList *l, *tty_devs;
283         unsigned int i;
284
285         serial_device = NULL;
286         serial_options = SERIALCOMM;
287         sdi = NULL;
288         devices = NULL;
289         drvc = di->priv;
290         drvc->instances = NULL;
291
292         if (sr_serial_extract_options(options, &serial_device,
293                                       &serial_options) == SR_OK) {
294                 sdi = hmo_probe_serial_device(serial_device, serial_options);
295                 if (sdi != NULL) {
296                         devices = g_slist_append(devices, sdi);
297                         drvc->instances = g_slist_append(drvc->instances, sdi);
298                 }
299         } else {
300                 tty_devs = NULL;
301
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)
305                                 continue;
306                         tty_devs = g_slist_concat(tty_devs, l);
307                 }
308
309                 for (l = tty_devs; l; l = l->next) {
310                         sdi = hmo_probe_serial_device(l->data, serial_options);
311                         if (sdi != NULL) {
312                                 devices = g_slist_append(devices, sdi);
313                                 drvc->instances = g_slist_append(drvc->instances, sdi);
314                         }
315                 }
316
317                 g_slist_free_full(tty_devs, g_free);
318         }
319
320         return devices;
321 }
322
323 static GSList *dev_list(void)
324 {
325         return ((struct drv_context *)(di->priv))->instances;
326 }
327
328 static void clear_helper(void *priv)
329 {
330         unsigned int i;
331         struct dev_context *devc;
332         struct scope_config *model;
333
334         devc = priv;
335         model = devc->model_config;
336
337         hmo_scope_state_free(devc->model_state);
338
339         for (i = 0; i < model->analog_channels; ++i)
340                 g_slist_free(devc->analog_groups[i].probes);
341
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);
345         }
346
347         g_free(devc->analog_groups);
348         g_free(devc->digital_groups);
349
350         g_free(devc);
351 }
352
353 static int dev_clear(void)
354 {
355         return std_dev_clear(di, clear_helper);
356 }
357
358 static int dev_open(struct sr_dev_inst *sdi)
359 {
360         if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
361                 return SR_ERR;
362
363         if (hmo_scope_state_get(sdi) != SR_OK)
364                 return SR_ERR;
365
366         sdi->status = SR_ST_ACTIVE;
367
368         return SR_OK;
369 }
370
371 static int dev_close(struct sr_dev_inst *sdi)
372 {
373         if (sdi->status == SR_ST_INACTIVE)
374                 return SR_OK;
375
376         sr_scpi_close(sdi->conn);
377
378         sdi->status = SR_ST_INACTIVE;
379
380         return SR_OK;
381 }
382
383 static int cleanup(void)
384 {
385         dev_clear();
386
387         return SR_OK;
388 }
389
390 static int check_probe_group(struct dev_context *devc,
391                              const struct sr_probe_group *probe_group)
392 {
393         unsigned int i;
394         struct scope_config *model;
395
396         model = devc->model_config;
397
398         if (!probe_group)
399                 return PG_NONE;
400
401         for (i = 0; i < model->analog_channels; ++i)
402                 if (probe_group == &devc->analog_groups[i])
403                         return PG_ANALOG;
404
405         for (i = 0; i < model->digital_pods; ++i)
406                 if (probe_group == &devc->digital_groups[i])
407                         return PG_DIGITAL;
408
409         sr_err("Invalid probe group specified.");
410
411         return PG_INVALID;
412 }
413
414 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
415                       const struct sr_probe_group *probe_group)
416 {
417         int ret, pg_type;
418         unsigned int i;
419         struct dev_context *devc;
420         struct scope_config *model;
421         struct scope_state *state;
422
423         if (!sdi || !(devc = sdi->priv))
424                 return SR_ERR_ARG;
425
426         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
427                 return SR_ERR;
428
429         ret = SR_ERR_NA;
430         model = devc->model_config;
431         state = devc->model_state;
432
433         switch (key) {
434         case SR_CONF_NUM_TIMEBASE:
435                 *data = g_variant_new_int32(model->num_xdivs);
436                 ret = SR_OK;
437                 break;
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])
445                                         continue;
446                                 *data = g_variant_new_int32(model->num_ydivs);
447                                 ret = SR_OK;
448                                 break;
449                         }
450
451                 } else {
452                         ret = SR_ERR_NA;
453                 }
454                 break;
455         case SR_CONF_TRIGGER_SOURCE:
456                 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
457                 ret = SR_OK;
458                 break;
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])
466                                         continue;
467                                 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
468                                 ret = SR_OK;
469                                 break;
470                         }
471
472                 } else {
473                         ret = SR_ERR_NA;
474                 }
475                 break;
476         case SR_CONF_SAMPLERATE:
477                 *data = g_variant_new_uint64(state->sample_rate);
478                 ret = SR_OK;
479                 break;
480         default:
481                 ret = SR_ERR_NA;
482         }
483
484         return ret;
485 }
486
487 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
488 {
489         unsigned int i;
490         GVariant *rational[2];
491         GVariantBuilder gvb;
492
493         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
494
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]);
498
499                 /* FIXME: Valgrind reports a memory leak here. */
500                 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
501         }
502
503         return g_variant_builder_end(&gvb);
504 }
505
506 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
507                       const struct sr_probe_group *probe_group)
508 {
509         int ret, pg_type;
510         unsigned int i, j;
511         char command[MAX_COMMAND_SIZE], float_str[30];
512         struct dev_context *devc;
513         struct scope_config *model;
514         struct scope_state *state;
515         const char *tmp;
516         uint64_t p, q;
517         double tmp_d;
518         gboolean update_sample_rate;
519
520         if (!sdi || !(devc = sdi->priv))
521                 return SR_ERR_ARG;
522
523         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
524                 return SR_ERR;
525
526         model = devc->model_config;
527         state = devc->model_state;
528         update_sample_rate = FALSE;
529
530         ret = SR_ERR_NA;
531
532         switch (key) {
533         case SR_CONF_LIMIT_FRAMES:
534                 devc->frame_limit = g_variant_get_uint64(data);
535                 ret = SR_OK;
536                 break;
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)
541                                 continue;
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]);
546
547                         ret = sr_scpi_send(sdi->conn, command);
548                         break;
549                 }
550                 break;
551         case SR_CONF_VDIV:
552                 if (pg_type == PG_NONE) {
553                         sr_err("No probe group specified.");
554                         return SR_ERR_PROBE_GROUP;
555                 }
556
557                 g_variant_get(data, "(tt)", &p, &q);
558
559                 for (i = 0; i < model->num_vdivs; i++) {
560                         if (p != (*model->vdivs)[i][0] ||
561                             q != (*model->vdivs)[i][1])
562                                 continue;
563                         for (j = 1; j <= model->analog_channels; ++j) {
564                                 if (probe_group != &devc->analog_groups[j - 1])
565                                         continue;
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],
570                                            j, float_str);
571
572                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
573                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
574                                         return SR_ERR;
575
576                                 break;
577                         }
578
579                         ret = SR_OK;
580                         break;
581                 }
582                 break;
583         case SR_CONF_TIMEBASE:
584                 g_variant_get(data, "(tt)", &p, &q);
585
586                 for (i = 0; i < model->num_timebases; i++) {
587                         if (p != (*model->timebases)[i][0] ||
588                             q != (*model->timebases)[i][1])
589                                 continue;
590                         state->timebase = i;
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],
594                                    float_str);
595
596                         ret = sr_scpi_send(sdi->conn, command);
597                         update_sample_rate = TRUE;
598                         break;
599                 }
600                 break;
601         case SR_CONF_HORIZ_TRIGGERPOS:
602                 tmp_d = g_variant_get_double(data);
603
604                 if (tmp_d < 0.0 || tmp_d > 1.0)
605                         return SR_ERR;
606
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);
611
612                 ret = sr_scpi_send(sdi->conn, command);
613                 break;
614         case SR_CONF_TRIGGER_SLOPE:
615                 tmp = g_variant_get_string(data, NULL);
616
617                 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
618                         return SR_ERR_ARG;
619
620                 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
621
622                 g_snprintf(command, sizeof(command),
623                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
624                            (state->trigger_slope == 0) ? "POS" : "NEG");
625
626                 ret = sr_scpi_send(sdi->conn, command);
627                 break;
628         case SR_CONF_COUPLING:
629                 if (pg_type == PG_NONE) {
630                         sr_err("No probe group specified.");
631                         return SR_ERR_PROBE_GROUP;
632                 }
633
634                 tmp = g_variant_get_string(data, NULL);
635
636                 for (i = 0; (*model->coupling_options)[i]; i++) {
637                         if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
638                                 continue;
639                         for (j = 1; j <= model->analog_channels; ++j) {
640                                 if (probe_group != &devc->analog_groups[j - 1])
641                                         continue;
642                                 state->analog_channels[j-1].coupling = i;
643
644                                 g_snprintf(command, sizeof(command),
645                                            (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
646                                            j, tmp);
647
648                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
649                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
650                                         return SR_ERR;
651                                 break;
652                         }
653
654                         ret = SR_OK;
655                         break;
656                 }
657                 break;
658         default:
659                 ret = SR_ERR_NA;
660                 break;
661         }
662
663         if (ret == SR_OK)
664                 ret = sr_scpi_get_opc(sdi->conn);
665
666         if (ret == SR_OK && update_sample_rate)
667                 ret = hmo_update_sample_rate(sdi);
668
669         return ret;
670 }
671
672 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
673                        const struct sr_probe_group *probe_group)
674 {
675         int pg_type;
676         struct dev_context *devc;
677         struct scope_config *model;
678
679         if (!sdi || !(devc = sdi->priv))
680                 return SR_ERR_ARG;
681
682         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
683                 return SR_ERR;
684
685         model = devc->model_config;
686
687         switch (key) {
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,
692                                 sizeof(int32_t));
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,
696                                 sizeof(int32_t));
697                 } else {
698                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
699                                 NULL, 0, sizeof(int32_t));
700                 }
701                 break;
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));
707                 break;
708         case SR_CONF_TRIGGER_SOURCE:
709                 *data = g_variant_new_strv(*model->trigger_sources,
710                            g_strv_length((char **)*model->trigger_sources));
711                 break;
712         case SR_CONF_TIMEBASE:
713                 *data = build_tuples(model->timebases, model->num_timebases);
714                 break;
715         case SR_CONF_VDIV:
716                 if (pg_type == PG_NONE)
717                         return SR_ERR_PROBE_GROUP;
718                 *data = build_tuples(model->vdivs, model->num_vdivs);
719                 break;
720         default:
721                 return SR_ERR_NA;
722         }
723
724         return SR_OK;
725 }
726
727 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
728 {
729         char command[MAX_COMMAND_SIZE];
730         struct sr_probe *probe;
731         struct dev_context *devc;
732         struct scope_config *model;
733
734         devc = sdi->priv;
735         model = devc->model_config;
736
737         probe = devc->current_probe->data;
738
739         switch (probe->type) {
740         case SR_PROBE_ANALOG:
741                 g_snprintf(command, sizeof(command),
742                            (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
743                            probe->index + 1);
744                 break;
745         case SR_PROBE_LOGIC:
746                 g_snprintf(command, sizeof(command),
747                            (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
748                            probe->index < 8 ? 1 : 2);
749                 break;
750         default:
751                 sr_err("Invalid probe type.");
752                 break;
753         }
754
755         return sr_scpi_send(sdi->conn, command);
756 }
757
758 static int hmo_check_probes(GSList *probes)
759 {
760         GSList *l;
761         struct sr_probe *probe;
762         gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
763
764         enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
765
766         for (l = probes; l; l = l->next) {
767                 probe = l->data;
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;
774                         break;
775                 case SR_PROBE_LOGIC:
776                         if (probe->index < 8)
777                                 enabled_pod1 = TRUE;
778                         else
779                                 enabled_pod2 = TRUE;
780                         break;
781                 default:
782                         return SR_ERR;
783                 }
784         }
785
786         if ((enabled_pod1 && enabled_chan3) ||
787             (enabled_pod2 && enabled_chan4))
788                 return SR_ERR;
789
790         return SR_OK;
791 }
792
793 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
794 {
795         GSList *l;
796         unsigned int i;
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;
804
805         devc = sdi->priv;
806         scpi = sdi->conn;
807         state = devc->model_state;
808         model = devc->model_config;
809         setup_changed = FALSE;
810
811         pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
812
813         for (l = sdi->probes; l; l = l->next) {
814                 probe = l->data;
815                 switch (probe->type) {
816                 case SR_PROBE_ANALOG:
817                         if (probe->enabled == state->analog_channels[probe->index].state)
818                                 break;
819                         g_snprintf(command, sizeof(command),
820                                    (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
821                                    probe->index + 1, probe->enabled);
822
823                         if (sr_scpi_send(scpi, command) != SR_OK)
824                                 return SR_ERR;
825                         state->analog_channels[probe->index].state = probe->enabled;
826                         setup_changed = TRUE;
827                         break;
828                 case SR_PROBE_LOGIC:
829                         /*
830                          * A digital POD needs to be enabled for every group of
831                          * 8 probes.
832                          */
833                         if (probe->enabled)
834                                 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
835
836                         if (probe->enabled == state->digital_channels[probe->index])
837                                 break;
838                         g_snprintf(command, sizeof(command),
839                                    (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
840                                    probe->index, probe->enabled);
841
842                         if (sr_scpi_send(scpi, command) != SR_OK)
843                                 return SR_ERR;
844
845                         state->digital_channels[probe->index] = probe->enabled;
846                         setup_changed = TRUE;
847                         break;
848                 default:
849                         return SR_ERR;
850                 }
851         }
852
853         for (i = 1; i <= model->digital_pods; ++i) {
854                 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
855                         continue;
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)
860                         return SR_ERR;
861                 state->digital_pods[i - 1] = pod_enabled[i - 1];
862                 setup_changed = TRUE;
863         }
864
865         g_free(pod_enabled);
866
867         if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
868                 return SR_ERR;
869
870         return SR_OK;
871 }
872
873 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
874 {
875         GSList *l;
876         gboolean digital_added;
877         struct sr_probe *probe;
878         struct dev_context *devc;
879         struct sr_scpi_dev_inst *scpi;
880
881         if (sdi->status != SR_ST_ACTIVE)
882                 return SR_ERR_DEV_CLOSED;
883
884         scpi = sdi->conn;
885         devc = sdi->priv;
886         digital_added = FALSE;
887
888         for (l = sdi->probes; l; l = l->next) {
889                 probe = l->data;
890                 if (!probe->enabled)
891                         continue;
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;
898                 }
899         }
900
901         if (!devc->enabled_probes)
902                 return SR_ERR;
903
904         if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
905                 sr_err("Invalid probe configuration specified!");
906                 return SR_ERR_NA;
907         }
908
909         if (hmo_setup_probes(sdi) != SR_OK) {
910                 sr_err("Failed to setup probe configuration!");
911                 return SR_ERR;
912         }
913
914         sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
915
916         /* Send header packet to the session bus. */
917         std_session_send_df_header(cb_data, LOG_PREFIX);
918
919         devc->current_probe = devc->enabled_probes;
920
921         return hmo_request_data(sdi);
922 }
923
924 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
925 {
926         struct dev_context *devc;
927         struct sr_scpi_dev_inst *scpi;
928         struct sr_datafeed_packet packet;
929
930         (void)cb_data;
931
932         packet.type = SR_DF_END;
933         packet.payload = NULL;
934         sr_session_send(sdi, &packet);
935
936         if (sdi->status != SR_ST_ACTIVE)
937                 return SR_ERR_DEV_CLOSED;
938
939         devc = sdi->priv;
940
941         devc->num_frames = 0;
942         g_slist_free(devc->enabled_probes);
943         devc->enabled_probes = NULL;
944         scpi = sdi->conn;
945         sr_scpi_source_remove(scpi);
946
947         return SR_OK;
948 }
949
950 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
951         .name = "hameg-hmo",
952         .longname = "Hameg HMO",
953         .api_version = 1,
954         .init = init,
955         .cleanup = cleanup,
956         .scan = scan,
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,
966         .priv = NULL,
967 };