]> sigrok.org Git - libsigrok.git/blob - hardware/hameg-hmo/api.c
hameg-hmo: Add initial working driver version.
[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
23 #include "protocol.h"
24
25 #define SERIALCOMM "115200/8n1/flow=1"
26
27 static const int32_t hwopts[] = {
28         SR_CONF_CONN,
29         SR_CONF_SERIALCOMM,
30 };
31
32 struct usb_id_info {
33         uint16_t vendor_id;
34         uint16_t product_id;
35 } usb_id_info;
36
37 static struct usb_id_info ho_models[] = {
38         {
39                 .vendor_id  = 0x0403,
40                 .product_id = 0xed72, /* HO720 */
41         },
42         {
43                 .vendor_id  = 0x0403,
44                 .product_id = 0xed73, /* HO730 */
45         },
46 };
47
48 static int init(struct sr_context *sr_ctx)
49 {
50         return std_init(sr_ctx, di, LOG_PREFIX);
51 }
52
53 /**
54  * Find USB serial devices via the USB vendor ID and product ID.
55  *
56  * @param vendor_id vendor ID of the USB device.
57  * @param product_id product ID of the USB device.
58  *
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.
61  */
62 static GSList *auto_find_usb(unsigned long vendor_id,
63                              unsigned long product_id)
64 {
65 #ifdef __linux__
66         const gchar *usb_dev;
67         const char device_tree[] = "/sys/bus/usb/devices/";
68
69         GDir *devices_dir;
70
71         GSList *l = NULL;
72         GSList *tty_devices;
73         GSList *matched_paths;
74
75         l = NULL;
76         tty_devices = NULL;
77         matched_paths = NULL;
78
79         if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
80                 return NULL;
81
82         /*
83          * Find potential candidates using the vendor ID and product ID
84          * and store them in matched_paths
85          */
86         while ((usb_dev = g_dir_read_name(devices_dir))) {
87                 FILE *fd;
88                 char tmp[5];
89
90                 gchar *vendor_path;
91                 gchar *product_path;
92
93                 unsigned long read_vendor_id;
94                 unsigned long read_product_id;
95
96                 vendor_path = g_strconcat(device_tree,
97                                           usb_dev, "/idVendor", NULL);
98                 product_path = g_strconcat(device_tree,
99                                            usb_dev, "/idProduct", NULL);
100
101                 if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
102                     !g_file_test(product_path, G_FILE_TEST_EXISTS))
103                         goto skip_device;
104
105                 if ((fd = g_fopen(vendor_path, "r")) == NULL)
106                         goto skip_device;
107
108                 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
109                         fclose(fd);
110                         goto skip_device;
111                 }
112                 read_vendor_id = strtoul(tmp, NULL, 16);
113
114                 fclose(fd);
115
116                 if ((fd = g_fopen(product_path, "r")) == NULL)
117                         goto skip_device;
118
119                 if (fgets(tmp, sizeof(tmp), fd) == NULL) {
120                         fclose(fd);
121                         goto skip_device;
122                 }
123                 read_product_id = strtoul(tmp, NULL, 16);
124
125                 fclose(fd);
126
127                 if (vendor_id == read_vendor_id &&
128                     product_id == read_product_id) {
129                         gchar *path_copy;
130
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                 const char *file;
145
146                 GDir *device_dir;
147
148                 gchar *prefix;
149                 gchar *subdir_path;
150                 gchar *device_path;
151
152                 subdir_path = NULL;
153
154                 device_path = g_strconcat(device_tree, l->data, NULL);
155
156                 if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
157                         g_free(device_path);
158                         continue;
159                 }
160
161                 prefix = g_strconcat(l->data, ":", NULL);
162
163                 while ((file = g_dir_read_name(device_dir))) {
164                         if (g_str_has_prefix(file, prefix)) {
165                                 subdir_path = g_strconcat(device_path,
166                                                           "/", file,
167                                                           NULL);
168                                 break;
169                         }
170                 }
171                 g_dir_close(device_dir);
172
173                 g_free(prefix);
174                 g_free(device_path);
175
176                 if (subdir_path) {
177                         if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
178                                 g_free(subdir_path);
179                                 continue;
180                         }
181                         g_free(subdir_path);
182
183                         while ((file = g_dir_read_name(device_dir))) {
184                                 if (g_str_has_prefix(file, "ttyUSB")) {
185                                         gchar *tty_path;
186
187                                         tty_path = g_strconcat("/dev/",
188                                                                file, NULL);
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,
192                                                                       tty_path);
193                                         break;
194                                 }
195                         }
196                         g_dir_close(device_dir);
197                 }
198         }
199         g_slist_free_full(matched_paths, g_free);
200
201         return tty_devices;
202 #else
203         return NULL;
204 #endif
205 }
206
207 static GSList *scan(GSList *options)
208 {
209         GSList *devices;
210
211         struct drv_context *drvc;
212         struct sr_dev_inst *sdi;
213
214         const char *serial_device;
215         const char *serial_options;
216
217         serial_device = NULL;
218         serial_options = SERIALCOMM;
219
220         sdi = NULL;
221         devices = NULL;
222         drvc = di->priv;
223         drvc->instances = NULL;
224
225         if (sr_serial_extract_options(options, &serial_device,
226                                       &serial_options) == SR_OK) {
227                 sdi = hameg_probe_serial_device(serial_device, serial_options);
228
229                 if (sdi != NULL) {
230                         devices = g_slist_append(devices, sdi);
231                         drvc->instances = g_slist_append(drvc->instances, sdi);
232                 }
233
234         } else {
235                 GSList *l;
236                 GSList *tty_devices;
237
238                 unsigned int i;
239
240                 tty_devices = NULL;
241
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)
245                                 continue;
246
247                         tty_devices = g_slist_concat(tty_devices, l);
248                 }
249
250                 for (l = tty_devices; l; l = l->next) {
251                         sdi = hameg_probe_serial_device(l->data, serial_options);
252                         if (sdi != NULL) {
253                                 devices = g_slist_append(devices, sdi);
254                                 drvc->instances = g_slist_append(drvc->instances, sdi);
255                         }
256                 }
257
258                 g_slist_free_full(tty_devices, g_free);
259         }
260
261         return devices;
262 }
263
264 static GSList *dev_list(void)
265 {
266         return ((struct drv_context *)(di->priv))->instances;
267 }
268
269 static void clear_helper(void *priv)
270 {
271         unsigned int i;
272
273         struct dev_context *devc;
274         struct scope_config *model;
275
276         devc = priv;
277         model = devc->model_config;
278
279         scope_state_free(devc->model_state);
280
281         for (i = 0; i < model->analog_channels; ++i) {
282                 g_slist_free(devc->analog_groups[i].probes);
283         }
284
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);
288         }
289
290         g_free(devc->analog_groups);
291         g_free(devc->digital_groups);
292
293         g_free(devc);
294 }
295
296 static int dev_clear(void)
297 {
298         return std_dev_clear(di, clear_helper);
299 }
300
301 static int dev_open(struct sr_dev_inst *sdi)
302 {
303         if (sdi->status != SR_ST_ACTIVE &&
304             serial_open(sdi->conn, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
305                 return SR_ERR;
306
307         if (scope_state_get(sdi) != SR_OK)
308                 return SR_ERR;
309
310         sdi->status = SR_ST_ACTIVE;
311
312         return SR_OK;
313 }
314
315 static int dev_close(struct sr_dev_inst *sdi)
316 {
317         if (sdi->status == SR_ST_INACTIVE)
318                 return SR_OK;
319
320         serial_close(sdi->conn);
321
322         sdi->status = SR_ST_INACTIVE;
323
324         return SR_OK;
325 }
326
327 static int cleanup(void)
328 {
329         dev_clear();
330
331         return SR_OK;
332 }
333
334 static int check_probe_group(struct dev_context *devc,
335                              const struct sr_probe_group *probe_group)
336 {
337         unsigned int i;
338         struct scope_config *model;
339
340         model = devc->model_config;
341
342         if (!probe_group)
343                 return PG_NONE;
344
345         for (i = 0; i < model->analog_channels; ++i)
346                 if (probe_group == &devc->analog_groups[i])
347                         return PG_ANALOG;
348
349         for (i = 0; i < model->digital_pods; ++i)
350                 if (probe_group == &devc->digital_groups[i])
351                         return PG_DIGITAL;
352
353         sr_err("Invalid probe group specified.");
354         return PG_INVALID;
355 }
356
357 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
358                       const struct sr_probe_group *probe_group)
359 {
360         int ret;
361         int pg_type;
362         unsigned int i;
363
364         struct dev_context *devc;
365         struct scope_config *model;
366
367         if (!sdi || !(devc = sdi->priv))
368                 return SR_ERR_ARG;
369
370         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
371                 return SR_ERR;
372
373         ret = SR_ERR_NA;
374         model = devc->model_config;
375
376         switch (key) {
377         case SR_CONF_NUM_TIMEBASE:
378                 *data = g_variant_new_int32(model->num_xdivs);
379                 ret = SR_OK;
380                 break;
381
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;
386
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);
391                                         ret = SR_OK;
392                                         break;
393                                 }
394                         }
395
396                 } else {
397                         ret = SR_ERR_NA;
398                 }
399                 break;
400
401         default:
402                 ret = SR_ERR_NA;
403         }
404
405         return ret;
406 }
407
408 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
409 {
410         unsigned int i;
411
412         GVariant *rational[2];
413         GVariantBuilder gvb;
414
415         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
416
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]);
420
421                 /* FIXME valgrind reports a memory leak here */
422                 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
423         }
424
425         return g_variant_builder_end(&gvb);
426 }
427
428 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
429                       const struct sr_probe_group *probe_group)
430 {
431         int ret;
432         int pg_type;
433         unsigned int i;
434         char command[MAX_COMMAND_SIZE];
435
436         struct dev_context *devc;
437         struct scope_config *model;
438         struct scope_state *state;
439
440         if (!sdi || !(devc = sdi->priv))
441                 return SR_ERR_ARG;
442
443         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
444                 return SR_ERR;
445
446         model = devc->model_config;
447         state = devc->model_state;
448
449         ret = SR_ERR_NA;
450
451         switch (key) {
452         case SR_CONF_LIMIT_FRAMES:
453                 devc->frame_limit = g_variant_get_uint64(data);
454                 ret = SR_OK;
455                 break;
456
457         case SR_CONF_TRIGGER_SOURCE:
458         {
459                 const char *tmp;
460
461                 tmp = g_variant_get_string(data, NULL);
462
463                 for (i = 0; (*model->trigger_sources)[i]; i++) {
464                         if (!g_strcmp0(tmp, (*model->trigger_sources)[i])) {
465                                 state->trigger_source = i;
466
467                                 g_snprintf(command, sizeof(command),
468                                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
469                                            (*model->trigger_sources)[i]);
470
471                                 ret = sr_scpi_send(sdi->conn, command);
472                                 break;
473                         }
474                 }
475         }
476         break;
477
478         case SR_CONF_VDIV:
479         {
480                 unsigned int j;
481                 uint64_t p, q;
482
483                 if (pg_type == PG_NONE) {
484                         sr_err("No probe group specified.");
485                         return SR_ERR_PROBE_GROUP;
486                 }
487
488                 g_variant_get(data, "(tt)", &p, &q);
489
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);
499
500                                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
501                                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
502                                                         return SR_ERR;
503
504                                                 break;
505                                         }
506                                 }
507
508                                 ret = SR_OK;
509                                 break;
510                         }
511                 }
512         }
513         break;
514
515         case SR_CONF_TIMEBASE:
516         {
517                 uint64_t p, q;
518
519                 g_variant_get(data, "(tt)", &p, &q);
520
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],
527                                            state->timebase);
528
529                                 ret = sr_scpi_send(sdi->conn, command);
530                                 break;
531                         }
532                 }
533         }
534         break;
535
536         case SR_CONF_HORIZ_TRIGGERPOS:
537         {
538                 double tmp;
539
540                 tmp = g_variant_get_double(data);
541
542                 if (tmp < 0.0 || tmp > 1.0)
543                         return SR_ERR;
544
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);
549
550                 ret = sr_scpi_send(sdi->conn, command);
551         }
552         break;
553
554         case SR_CONF_TRIGGER_SLOPE:
555         {
556                 uint64_t tmp;
557
558                 tmp = g_variant_get_uint64(data);
559
560                 if (tmp != 0 && tmp != 1)
561                         return SR_ERR;
562
563                 state->trigger_slope = tmp;
564
565                 g_snprintf(command, sizeof(command),
566                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
567                            tmp ? "POS" : "NEG");
568
569                 ret = sr_scpi_send(sdi->conn, command);
570         }
571         break;
572
573         case SR_CONF_COUPLING:
574         {
575                 unsigned int j;
576                 const char *tmp;
577
578                 if (pg_type == PG_NONE) {
579                         sr_err("No probe group specified.");
580                         return SR_ERR_PROBE_GROUP;
581                 }
582
583                 tmp = g_variant_get_string(data, NULL);
584
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;
590
591                                                 g_snprintf(command, sizeof(command),
592                                                            (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
593                                                            j, tmp);
594
595                                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
596                                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
597                                                         return SR_ERR;
598                                                 break;
599                                         }
600                                 }
601
602                                 ret = SR_OK;
603                                 break;
604                         }
605                 }
606         }
607         break;
608
609         default:
610                 ret = SR_ERR_NA;
611                 break;
612         }
613
614         if (ret == SR_OK)
615                 ret = sr_scpi_get_opc(sdi->conn);
616
617         return ret;
618 }
619
620 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
621                       const struct sr_probe_group *probe_group)
622 {
623         int pg_type;
624
625         struct dev_context *devc;
626         struct scope_config *model;
627
628         if (!sdi || !(devc = sdi->priv))
629                 return SR_ERR_ARG;
630
631         if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
632                 return SR_ERR;
633
634         model = devc->model_config;
635
636         switch (key) {
637         case SR_CONF_DEVICE_OPTIONS:
638                 if (pg_type == PG_NONE) {
639                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
640                                                           model->hw_caps,
641                                                           model->num_hwcaps,
642                                                           sizeof(int32_t));
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,
647                                                           sizeof(int32_t));
648                 } else {
649                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
650                                                           NULL, 0, sizeof(int32_t));
651                 }
652                 break;
653
654         case SR_CONF_COUPLING:
655                 if (pg_type == PG_NONE)
656                         return SR_ERR_PROBE_GROUP;
657
658                 *data = g_variant_new_strv(*model->coupling_options,
659                                            g_strv_length((char **) *model->coupling_options));
660                 break;
661
662         case SR_CONF_TRIGGER_SOURCE:
663                 *data = g_variant_new_strv(*model->trigger_sources,
664                                            g_strv_length((char **) *model->trigger_sources));
665                 break;
666
667         case SR_CONF_TIMEBASE:
668                 *data = build_tuples(model->timebases, model->num_timebases);
669                 break;
670
671         case SR_CONF_VDIV:
672                 if (pg_type == PG_NONE)
673                         return SR_ERR_PROBE_GROUP;
674
675                 *data = build_tuples(model->vdivs, model->num_vdivs);
676                 break;
677
678         default:
679                 return SR_ERR_NA;
680         }
681
682         return SR_OK;
683 }
684
685 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
686 {
687         char command[MAX_COMMAND_SIZE];
688
689         struct sr_probe *probe;
690         struct dev_context *devc;
691         struct scope_config *model;
692
693         devc = sdi->priv;
694         model = devc->model_config;
695
696         probe = devc->current_probe->data;
697
698         switch (probe->type) {
699         case SR_PROBE_ANALOG:
700                 g_snprintf(command, sizeof(command),
701                            (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
702                            probe->index + 1);
703                 break;
704         case SR_PROBE_LOGIC:
705                 g_snprintf(command, sizeof(command),
706                            (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
707                            probe->index < 8 ? 1 : 2);
708                 break;
709         default:
710                 sr_err("Invalid probe type");
711                 break;
712         }
713
714         return sr_scpi_send(sdi->conn, command);
715 }
716
717 static int hmo_check_probes(GSList *probes)
718 {
719         GSList *l;
720         gboolean enabled_pod1;
721         gboolean enabled_pod2;
722         gboolean enabled_chan3;
723         gboolean enabled_chan4;
724
725         struct sr_probe *probe;
726
727         enabled_pod1 = FALSE;
728         enabled_pod2 = FALSE;
729         enabled_chan3 = FALSE;
730         enabled_chan4 = FALSE;
731
732         for (l = probes; l; l = l->next) {
733                 probe = l->data;
734
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;
741                         break;
742
743                 case SR_PROBE_LOGIC:
744                         if (probe->index < 8)
745                                 enabled_pod1 = TRUE;
746                         else
747                                 enabled_pod2 = TRUE;
748                         break;
749
750                 default:
751                         return SR_ERR;
752                 }
753         }
754
755         if ((enabled_pod1 && enabled_chan3) ||
756             (enabled_pod2 && enabled_chan4))
757                 return SR_ERR;
758
759         return SR_OK;
760 }
761
762 static int hmo_setup_probes(const struct sr_dev_inst *sdi)
763 {
764         GSList *l;
765         unsigned int i;
766         gboolean *pod_enabled;
767         char command[MAX_COMMAND_SIZE];
768
769         struct scope_state *state;
770         struct scope_config *model;
771
772         struct sr_probe *probe;
773         struct dev_context *devc;
774         struct sr_serial_dev_inst *serial;
775
776         devc = sdi->priv;
777         serial = sdi->conn;
778         state = devc->model_state;
779         model = devc->model_config;
780
781         pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
782
783         for (l = sdi->probes; l; l = l->next) {
784                 probe = l->data;
785
786                 switch (probe->type) {
787                 case SR_PROBE_ANALOG:
788                 {
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);
793
794                                 if (sr_scpi_send(serial, command) != SR_OK)
795                                         return SR_ERR;
796                                 state->analog_channels[probe->index].state = probe->enabled;
797                         }
798                 }
799                 break;
800
801                 case SR_PROBE_LOGIC:
802                 {
803                         /*
804                          * A digital POD needs to be enabled for every group of
805                          * 8 probes.
806                          */
807                         if (probe->enabled)
808                                 pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
809
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);
814
815                                 if (sr_scpi_send(serial, command) != SR_OK)
816                                         return SR_ERR;
817
818                                 state->digital_channels[probe->index] = probe->enabled;
819                         }
820                 }
821                 break;
822
823                 default:
824                         return SR_ERR;
825                 }
826         }
827
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]);
833
834                         if (sr_scpi_send(serial, command) != SR_OK)
835                                 return SR_ERR;
836
837                         state->digital_pods[i-1] = pod_enabled[i-1];
838                 }
839         }
840
841         g_free(pod_enabled);
842
843         return SR_OK;
844 }
845
846 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
847 {
848         GSList *l;
849         gboolean digital_added;
850
851         struct sr_probe *probe;
852         struct dev_context *devc;
853         struct sr_serial_dev_inst *serial;
854
855         if (sdi->status != SR_ST_ACTIVE)
856                 return SR_ERR_DEV_CLOSED;
857
858         serial = sdi->conn;
859         devc = sdi->priv;
860         digital_added = FALSE;
861
862         for (l = sdi->probes; l; l = l->next) {
863                 probe = l->data;
864
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,
869                                                                       probe);
870                                 if (probe->type == SR_PROBE_LOGIC)
871                                         digital_added = TRUE;
872                         }
873                 }
874         }
875
876         if (!devc->enabled_probes)
877                 return SR_ERR;
878
879         if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
880                 sr_err("Invalid probe configuration specified!");
881                 return SR_ERR_NA;
882         }
883
884         if (hmo_setup_probes(sdi) != SR_OK) {
885                 sr_err("Failed to setup probe configuration!");
886                 return SR_ERR;
887         }
888
889         sr_source_add(serial->fd, G_IO_IN, 50, hameg_hmo_receive_data, (void *)sdi);
890
891         /* Send header packet to the session bus. */
892         std_session_send_df_header(cb_data, LOG_PREFIX);
893
894         devc->current_probe = devc->enabled_probes;
895
896         return hmo_request_data(sdi);
897 }
898
899 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
900 {
901         struct dev_context *devc;
902         struct sr_serial_dev_inst *serial;
903
904         (void)cb_data;
905
906         if (sdi->status != SR_ST_ACTIVE)
907                 return SR_ERR_DEV_CLOSED;
908
909         devc = sdi->priv;
910
911         g_slist_free(devc->enabled_probes);
912         devc->enabled_probes = NULL;
913         serial = sdi->conn;
914         sr_source_remove(serial->fd);
915
916         return SR_OK;
917 }
918
919 SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
920         .name = "hameg-hmo",
921         .longname = "Hameg HMO scope driver",
922         .api_version = 1,
923         .init = init,
924         .cleanup = cleanup,
925         .scan = scan,
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,
935         .priv = NULL,
936 };