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