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