]> sigrok.org Git - libsigrok.git/blame - hardware/hameg-hmo/api.c
hwdriver: Change TRIGGER_SLOPE setting to string type.
[libsigrok.git] / hardware / hameg-hmo / api.c
CommitLineData
06a3e78a
DJ
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
13f2b9d7
DJ
20#include <stdlib.h>
21#include <glib/gstdio.h>
06a3e78a
DJ
22#include "protocol.h"
23
13f2b9d7
DJ
24#define SERIALCOMM "115200/8n1/flow=1"
25
e9a62139
DJ
26SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
27static struct sr_dev_driver *di = &hameg_hmo_driver_info;
28
29static const char *manufacturers[] = {
30 "HAMEG",
31};
32
13f2b9d7
DJ
33static const int32_t hwopts[] = {
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
36};
37
38struct usb_id_info {
39 uint16_t vendor_id;
40 uint16_t product_id;
89280b1a 41};
13f2b9d7
DJ
42
43static struct usb_id_info ho_models[] = {
89280b1a
UH
44 { 0x0403, 0xed72 }, /* HO720 */
45 { 0x0403, 0xed73 }, /* HO730 */
13f2b9d7 46};
06a3e78a 47
719eff68
UH
48enum {
49 PG_INVALID = -1,
50 PG_NONE,
51 PG_ANALOG,
52 PG_DIGITAL,
53};
54
06a3e78a
DJ
55static int init(struct sr_context *sr_ctx)
56{
57 return std_init(sr_ctx, di, LOG_PREFIX);
58}
59
13f2b9d7
DJ
60/**
61 * Find USB serial devices via the USB vendor ID and product ID.
62 *
89280b1a
UH
63 * @param vendor_id Vendor ID of the USB device.
64 * @param product_id Product ID of the USB device.
13f2b9d7 65 *
89280b1a
UH
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.
13f2b9d7 69 */
89280b1a 70static GSList *auto_find_usb(uint16_t vendor_id, uint16_t product_id)
13f2b9d7
DJ
71{
72#ifdef __linux__
73 const gchar *usb_dev;
74 const char device_tree[] = "/sys/bus/usb/devices/";
89280b1a 75 GDir *devices_dir, *device_dir;
13f2b9d7 76 GSList *l = NULL;
89280b1a 77 GSList *tty_devs;
13f2b9d7 78 GSList *matched_paths;
89280b1a
UH
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;
13f2b9d7
DJ
85
86 l = NULL;
89280b1a 87 tty_devs = NULL;
13f2b9d7
DJ
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
89280b1a 95 * and store them in matched_paths.
13f2b9d7
DJ
96 */
97 while ((usb_dev = g_dir_read_name(devices_dir))) {
13f2b9d7
DJ
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) {
13f2b9d7
DJ
131 path_copy = g_strdup(usb_dev);
132 matched_paths = g_slist_prepend(matched_paths,
133 path_copy);
134 }
135
89280b1a 136skip_device:
13f2b9d7
DJ
137 g_free(vendor_path);
138 g_free(product_path);
139 }
140 g_dir_close(devices_dir);
141
89280b1a 142 /* For every matched device try to find a ttyUSBX subfolder. */
13f2b9d7 143 for (l = matched_paths; l; l = l->next) {
13f2b9d7
DJ
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,
89280b1a 158 "/", file, NULL);
13f2b9d7
DJ
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")) {
13f2b9d7
DJ
176 tty_path = g_strconcat("/dev/",
177 file, NULL);
89280b1a 178 sr_dbg("Found USB device %04x:%04x attached to %s.",
13f2b9d7 179 vendor_id, product_id, tty_path);
89280b1a
UH
180 tty_devs = g_slist_prepend(tty_devs,
181 tty_path);
13f2b9d7
DJ
182 break;
183 }
184 }
185 g_dir_close(device_dir);
186 }
187 }
188 g_slist_free_full(matched_paths, g_free);
189
89280b1a 190 return tty_devs;
13f2b9d7 191#else
55eb33db
UH
192 (void)vendor_id;
193 (void)product_id;
194
13f2b9d7
DJ
195 return NULL;
196#endif
197}
198
e9a62139
DJ
199static 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
210static 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
c3515cea 223 if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
e9a62139
DJ
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
68e3d070
DJ
257 sr_scpi_close(sdi->conn);
258
259 sdi->status = SR_ST_INACTIVE;
260
e9a62139
DJ
261 return sdi;
262
263fail:
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
06a3e78a
DJ
276static GSList *scan(GSList *options)
277{
06a3e78a 278 GSList *devices;
13f2b9d7
DJ
279 struct drv_context *drvc;
280 struct sr_dev_inst *sdi;
89280b1a
UH
281 const char *serial_device, *serial_options;
282 GSList *l, *tty_devs;
283 unsigned int i;
06a3e78a 284
13f2b9d7
DJ
285 serial_device = NULL;
286 serial_options = SERIALCOMM;
13f2b9d7 287 sdi = NULL;
06a3e78a
DJ
288 devices = NULL;
289 drvc = di->priv;
290 drvc->instances = NULL;
291
13f2b9d7
DJ
292 if (sr_serial_extract_options(options, &serial_device,
293 &serial_options) == SR_OK) {
719eff68 294 sdi = hmo_probe_serial_device(serial_device, serial_options);
13f2b9d7
DJ
295 if (sdi != NULL) {
296 devices = g_slist_append(devices, sdi);
297 drvc->instances = g_slist_append(drvc->instances, sdi);
298 }
13f2b9d7 299 } else {
89280b1a 300 tty_devs = NULL;
13f2b9d7
DJ
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;
89280b1a 306 tty_devs = g_slist_concat(tty_devs, l);
13f2b9d7
DJ
307 }
308
89280b1a 309 for (l = tty_devs; l; l = l->next) {
719eff68 310 sdi = hmo_probe_serial_device(l->data, serial_options);
13f2b9d7
DJ
311 if (sdi != NULL) {
312 devices = g_slist_append(devices, sdi);
313 drvc->instances = g_slist_append(drvc->instances, sdi);
314 }
315 }
316
89280b1a 317 g_slist_free_full(tty_devs, g_free);
13f2b9d7 318 }
06a3e78a
DJ
319
320 return devices;
321}
322
323static GSList *dev_list(void)
324{
325 return ((struct drv_context *)(di->priv))->instances;
326}
327
13f2b9d7
DJ
328static void clear_helper(void *priv)
329{
330 unsigned int i;
13f2b9d7
DJ
331 struct dev_context *devc;
332 struct scope_config *model;
333
334 devc = priv;
335 model = devc->model_config;
336
719eff68 337 hmo_scope_state_free(devc->model_state);
13f2b9d7 338
89280b1a 339 for (i = 0; i < model->analog_channels; ++i)
13f2b9d7 340 g_slist_free(devc->analog_groups[i].probes);
13f2b9d7
DJ
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
06a3e78a
DJ
353static int dev_clear(void)
354{
13f2b9d7 355 return std_dev_clear(di, clear_helper);
06a3e78a
DJ
356}
357
358static int dev_open(struct sr_dev_inst *sdi)
359{
23f43dff 360 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
13f2b9d7 361 return SR_ERR;
06a3e78a 362
719eff68 363 if (hmo_scope_state_get(sdi) != SR_OK)
13f2b9d7 364 return SR_ERR;
06a3e78a
DJ
365
366 sdi->status = SR_ST_ACTIVE;
367
368 return SR_OK;
369}
370
371static int dev_close(struct sr_dev_inst *sdi)
372{
13f2b9d7
DJ
373 if (sdi->status == SR_ST_INACTIVE)
374 return SR_OK;
06a3e78a 375
23f43dff 376 sr_scpi_close(sdi->conn);
06a3e78a
DJ
377
378 sdi->status = SR_ST_INACTIVE;
379
380 return SR_OK;
381}
382
383static int cleanup(void)
384{
385 dev_clear();
386
06a3e78a
DJ
387 return SR_OK;
388}
389
13f2b9d7
DJ
390static 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.");
89280b1a 410
13f2b9d7
DJ
411 return PG_INVALID;
412}
413
414static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
415 const struct sr_probe_group *probe_group)
06a3e78a 416{
89280b1a 417 int ret, pg_type;
13f2b9d7 418 unsigned int i;
13f2b9d7
DJ
419 struct dev_context *devc;
420 struct scope_config *model;
14a2f74d 421 struct scope_state *state;
13f2b9d7
DJ
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;
06a3e78a 428
13f2b9d7
DJ
429 ret = SR_ERR_NA;
430 model = devc->model_config;
14a2f74d 431 state = devc->model_state;
06a3e78a 432
06a3e78a 433 switch (key) {
13f2b9d7
DJ
434 case SR_CONF_NUM_TIMEBASE:
435 *data = g_variant_new_int32(model->num_xdivs);
436 ret = SR_OK;
437 break;
13f2b9d7
DJ
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;
13f2b9d7
DJ
442 } else if (pg_type == PG_ANALOG) {
443 for (i = 0; i < model->analog_channels; ++i) {
082972e8
UH
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;
13f2b9d7
DJ
449 }
450
ccf14618
DJ
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
13f2b9d7
DJ
472 } else {
473 ret = SR_ERR_NA;
474 }
475 break;
14a2f74d
DJ
476 case SR_CONF_SAMPLERATE:
477 *data = g_variant_new_uint64(state->sample_rate);
478 ret = SR_OK;
479 break;
06a3e78a 480 default:
13f2b9d7 481 ret = SR_ERR_NA;
06a3e78a
DJ
482 }
483
484 return ret;
485}
486
13f2b9d7
DJ
487static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
488{
489 unsigned int i;
13f2b9d7
DJ
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
89280b1a 499 /* FIXME: Valgrind reports a memory leak here. */
13f2b9d7
DJ
500 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
501 }
502
503 return g_variant_builder_end(&gvb);
504}
505
506static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
507 const struct sr_probe_group *probe_group)
06a3e78a 508{
89280b1a
UH
509 int ret, pg_type;
510 unsigned int i, j;
965b463d 511 char command[MAX_COMMAND_SIZE], float_str[30];
13f2b9d7
DJ
512 struct dev_context *devc;
513 struct scope_config *model;
514 struct scope_state *state;
89280b1a 515 const char *tmp;
ca9b9f48 516 uint64_t p, q;
89280b1a 517 double tmp_d;
23e1ea7a 518 gboolean update_sample_rate;
06a3e78a 519
13f2b9d7
DJ
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;
23e1ea7a 528 update_sample_rate = FALSE;
13f2b9d7
DJ
529
530 ret = SR_ERR_NA;
06a3e78a 531
06a3e78a 532 switch (key) {
13f2b9d7
DJ
533 case SR_CONF_LIMIT_FRAMES:
534 devc->frame_limit = g_variant_get_uint64(data);
535 ret = SR_OK;
536 break;
13f2b9d7 537 case SR_CONF_TRIGGER_SOURCE:
13f2b9d7 538 tmp = g_variant_get_string(data, NULL);
13f2b9d7 539 for (i = 0; (*model->trigger_sources)[i]; i++) {
082972e8
UH
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]);
13f2b9d7 546
082972e8
UH
547 ret = sr_scpi_send(sdi->conn, command);
548 break;
13f2b9d7 549 }
89280b1a 550 break;
13f2b9d7 551 case SR_CONF_VDIV:
13f2b9d7
DJ
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++) {
082972e8
UH
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;
8de2dc3b 566 state->analog_channels[j - 1].vdiv = i;
965b463d 567 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
568 g_snprintf(command, sizeof(command),
569 (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
965b463d 570 j, float_str);
082972e8
UH
571
572 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
573 sr_scpi_get_opc(sdi->conn) != SR_OK)
574 return SR_ERR;
13f2b9d7 575
13f2b9d7
DJ
576 break;
577 }
082972e8
UH
578
579 ret = SR_OK;
580 break;
13f2b9d7 581 }
89280b1a 582 break;
13f2b9d7 583 case SR_CONF_TIMEBASE:
13f2b9d7
DJ
584 g_variant_get(data, "(tt)", &p, &q);
585
586 for (i = 0; i < model->num_timebases; i++) {
082972e8
UH
587 if (p != (*model->timebases)[i][0] ||
588 q != (*model->timebases)[i][1])
589 continue;
8de2dc3b 590 state->timebase = i;
965b463d 591 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
082972e8
UH
592 g_snprintf(command, sizeof(command),
593 (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
965b463d 594 float_str);
13f2b9d7 595
082972e8 596 ret = sr_scpi_send(sdi->conn, command);
23e1ea7a 597 update_sample_rate = TRUE;
082972e8 598 break;
13f2b9d7 599 }
89280b1a 600 break;
13f2b9d7 601 case SR_CONF_HORIZ_TRIGGERPOS:
89280b1a 602 tmp_d = g_variant_get_double(data);
13f2b9d7 603
89280b1a 604 if (tmp_d < 0.0 || tmp_d > 1.0)
13f2b9d7
DJ
605 return SR_ERR;
606
89280b1a 607 state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs;
13f2b9d7
DJ
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);
89280b1a 613 break;
13f2b9d7 614 case SR_CONF_TRIGGER_SLOPE:
ca9b9f48 615 tmp = g_variant_get_string(data, NULL);
13f2b9d7 616
ca9b9f48
DE
617 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
618 return SR_ERR_ARG;
13f2b9d7 619
ca9b9f48 620 state->trigger_slope = (tmp[0] == 'r') ? 0 : 1;
13f2b9d7
DJ
621
622 g_snprintf(command, sizeof(command),
623 (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
ca9b9f48 624 (state->trigger_slope == 0) ? "POS" : "NEG");
13f2b9d7
DJ
625
626 ret = sr_scpi_send(sdi->conn, command);
89280b1a 627 break;
13f2b9d7 628 case SR_CONF_COUPLING:
13f2b9d7
DJ
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++) {
082972e8
UH
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;
13f2b9d7 643
082972e8
UH
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;
13f2b9d7
DJ
651 break;
652 }
082972e8
UH
653
654 ret = SR_OK;
655 break;
13f2b9d7 656 }
89280b1a 657 break;
06a3e78a
DJ
658 default:
659 ret = SR_ERR_NA;
13f2b9d7 660 break;
06a3e78a
DJ
661 }
662
13f2b9d7
DJ
663 if (ret == SR_OK)
664 ret = sr_scpi_get_opc(sdi->conn);
665
23e1ea7a
DJ
666 if (ret == SR_OK && update_sample_rate)
667 ret = hmo_update_sample_rate(sdi);
668
06a3e78a
DJ
669 return ret;
670}
671
13f2b9d7 672static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
89280b1a 673 const struct sr_probe_group *probe_group)
06a3e78a 674{
13f2b9d7 675 int pg_type;
13f2b9d7
DJ
676 struct dev_context *devc;
677 struct scope_config *model;
678
679 if (!sdi || !(devc = sdi->priv))
680 return SR_ERR_ARG;
06a3e78a 681
13f2b9d7
DJ
682 if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
683 return SR_ERR;
684
685 model = devc->model_config;
06a3e78a 686
06a3e78a 687 switch (key) {
13f2b9d7
DJ
688 case SR_CONF_DEVICE_OPTIONS:
689 if (pg_type == PG_NONE) {
690 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
89280b1a
UH
691 model->hw_caps, model->num_hwcaps,
692 sizeof(int32_t));
13f2b9d7
DJ
693 } else if (pg_type == PG_ANALOG) {
694 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
89280b1a
UH
695 model->analog_hwcaps, model->num_analog_hwcaps,
696 sizeof(int32_t));
13f2b9d7
DJ
697 } else {
698 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
89280b1a 699 NULL, 0, sizeof(int32_t));
13f2b9d7
DJ
700 }
701 break;
13f2b9d7
DJ
702 case SR_CONF_COUPLING:
703 if (pg_type == PG_NONE)
704 return SR_ERR_PROBE_GROUP;
13f2b9d7 705 *data = g_variant_new_strv(*model->coupling_options,
89280b1a 706 g_strv_length((char **)*model->coupling_options));
13f2b9d7 707 break;
13f2b9d7
DJ
708 case SR_CONF_TRIGGER_SOURCE:
709 *data = g_variant_new_strv(*model->trigger_sources,
89280b1a 710 g_strv_length((char **)*model->trigger_sources));
13f2b9d7 711 break;
13f2b9d7
DJ
712 case SR_CONF_TIMEBASE:
713 *data = build_tuples(model->timebases, model->num_timebases);
714 break;
13f2b9d7
DJ
715 case SR_CONF_VDIV:
716 if (pg_type == PG_NONE)
717 return SR_ERR_PROBE_GROUP;
13f2b9d7
DJ
718 *data = build_tuples(model->vdivs, model->num_vdivs);
719 break;
06a3e78a
DJ
720 default:
721 return SR_ERR_NA;
722 }
723
13f2b9d7 724 return SR_OK;
06a3e78a
DJ
725}
726
13f2b9d7 727SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
06a3e78a 728{
13f2b9d7 729 char command[MAX_COMMAND_SIZE];
13f2b9d7
DJ
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:
89280b1a 751 sr_err("Invalid probe type.");
13f2b9d7
DJ
752 break;
753 }
754
755 return sr_scpi_send(sdi->conn, command);
756}
757
758static int hmo_check_probes(GSList *probes)
759{
760 GSList *l;
13f2b9d7 761 struct sr_probe *probe;
89280b1a 762 gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4;
13f2b9d7 763
89280b1a 764 enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE;
13f2b9d7
DJ
765
766 for (l = probes; l; l = l->next) {
767 probe = l->data;
13f2b9d7
DJ
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;
13f2b9d7
DJ
775 case SR_PROBE_LOGIC:
776 if (probe->index < 8)
777 enabled_pod1 = TRUE;
778 else
779 enabled_pod2 = TRUE;
780 break;
13f2b9d7
DJ
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
793static int hmo_setup_probes(const struct sr_dev_inst *sdi)
794{
795 GSList *l;
796 unsigned int i;
23e1ea7a 797 gboolean *pod_enabled, setup_changed;
13f2b9d7 798 char command[MAX_COMMAND_SIZE];
13f2b9d7
DJ
799 struct scope_state *state;
800 struct scope_config *model;
13f2b9d7
DJ
801 struct sr_probe *probe;
802 struct dev_context *devc;
23f43dff 803 struct sr_scpi_dev_inst *scpi;
13f2b9d7
DJ
804
805 devc = sdi->priv;
23f43dff 806 scpi = sdi->conn;
13f2b9d7
DJ
807 state = devc->model_state;
808 model = devc->model_config;
23e1ea7a 809 setup_changed = FALSE;
13f2b9d7
DJ
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;
13f2b9d7
DJ
815 switch (probe->type) {
816 case SR_PROBE_ANALOG:
082972e8
UH
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);
13f2b9d7 822
23f43dff 823 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
824 return SR_ERR;
825 state->analog_channels[probe->index].state = probe->enabled;
23e1ea7a 826 setup_changed = TRUE;
89280b1a 827 break;
13f2b9d7 828 case SR_PROBE_LOGIC:
13f2b9d7
DJ
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
082972e8
UH
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);
13f2b9d7 841
23f43dff 842 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8 843 return SR_ERR;
13f2b9d7 844
082972e8 845 state->digital_channels[probe->index] = probe->enabled;
23e1ea7a 846 setup_changed = TRUE;
89280b1a 847 break;
13f2b9d7
DJ
848 default:
849 return SR_ERR;
850 }
851 }
852
853 for (i = 1; i <= model->digital_pods; ++i) {
082972e8
UH
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]);
23f43dff 859 if (sr_scpi_send(scpi, command) != SR_OK)
082972e8
UH
860 return SR_ERR;
861 state->digital_pods[i - 1] = pod_enabled[i - 1];
23e1ea7a 862 setup_changed = TRUE;
13f2b9d7
DJ
863 }
864
865 g_free(pod_enabled);
866
23e1ea7a
DJ
867 if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
868 return SR_ERR;
869
13f2b9d7
DJ
870 return SR_OK;
871}
872
873static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
874{
875 GSList *l;
876 gboolean digital_added;
13f2b9d7
DJ
877 struct sr_probe *probe;
878 struct dev_context *devc;
23f43dff 879 struct sr_scpi_dev_inst *scpi;
06a3e78a
DJ
880
881 if (sdi->status != SR_ST_ACTIVE)
882 return SR_ERR_DEV_CLOSED;
883
23f43dff 884 scpi = sdi->conn;
13f2b9d7
DJ
885 devc = sdi->priv;
886 digital_added = FALSE;
887
888 for (l = sdi->probes; l; l = l->next) {
889 probe = l->data;
082972e8
UH
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;
13f2b9d7
DJ
898 }
899 }
06a3e78a 900
13f2b9d7
DJ
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
23f43dff 914 sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi);
13f2b9d7
DJ
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);
06a3e78a
DJ
922}
923
924static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
925{
13f2b9d7 926 struct dev_context *devc;
23f43dff 927 struct sr_scpi_dev_inst *scpi;
66e3219d 928 struct sr_datafeed_packet packet;
13f2b9d7 929
06a3e78a
DJ
930 (void)cb_data;
931
66e3219d
DJ
932 packet.type = SR_DF_END;
933 packet.payload = NULL;
934 sr_session_send(sdi, &packet);
935
06a3e78a
DJ
936 if (sdi->status != SR_ST_ACTIVE)
937 return SR_ERR_DEV_CLOSED;
938
13f2b9d7
DJ
939 devc = sdi->priv;
940
ef1a346b 941 devc->num_frames = 0;
13f2b9d7
DJ
942 g_slist_free(devc->enabled_probes);
943 devc->enabled_probes = NULL;
23f43dff
ML
944 scpi = sdi->conn;
945 sr_scpi_source_remove(scpi);
06a3e78a
DJ
946
947 return SR_OK;
948}
949
950SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = {
951 .name = "hameg-hmo",
89280b1a 952 .longname = "Hameg HMO",
06a3e78a
DJ
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};