]> sigrok.org Git - libsigrok.git/blame - src/hardware/hung-chang-dso-2100/api.c
Put driver pointers into special section
[libsigrok.git] / src / hardware / hung-chang-dso-2100 / api.c
CommitLineData
05ac4a98
DG
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Daniel Glöckner <daniel-gl@gmx.net>
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
6ec6c43b 20#include <config.h>
c9404469
DG
21#include <ieee1284.h>
22#include <string.h>
05ac4a98
DG
23#include "protocol.h"
24
c9404469
DG
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_OSCILLOSCOPE,
31};
32
33static const uint32_t devopts[] = {
34 SR_CONF_CONN | SR_CONF_GET,
35 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_BUFFERSIZE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40};
41
42static const uint32_t cgopts[] = {
43 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
46};
47
48static const uint64_t samplerates[] = {
49 SR_MHZ(100), SR_MHZ(50), SR_MHZ(25), SR_MHZ(20),
50 SR_MHZ(10), SR_MHZ(5), SR_KHZ(2500), SR_MHZ(2),
51 SR_MHZ(1), SR_KHZ(500), SR_KHZ(250), SR_KHZ(200),
52 SR_KHZ(100), SR_KHZ(50), SR_KHZ(25), SR_KHZ(20),
53 SR_KHZ(10), SR_KHZ(5), SR_HZ(2500), SR_KHZ(2),
54 SR_KHZ(1), SR_HZ(500), SR_HZ(250), SR_HZ(200),
55 SR_HZ(100), SR_HZ(50), SR_HZ(25), SR_HZ(20)
56};
57
58/* must be in sync with readout_steps[] in protocol.c */
59static const uint64_t buffersizes[] = {
60 2 * 500, 3 * 500, 4 * 500, 5 * 500,
61 6 * 500, 7 * 500, 8 * 500, 9 * 500, 10 * 500,
62 12 * 500 - 2, 14 * 500 - 2, 16 * 500 - 2,
63 18 * 500 - 2, 20 * 500 - 2, 10240 - 2
64};
65
66static const uint64_t vdivs[][2] = {
67 { 10, 1000 },
68 { 20, 1000 },
69 { 50, 1000 },
70 { 100, 1000 },
71 { 200, 1000 },
72 { 500, 1000 },
73 { 1, 1 },
74 { 2, 1 },
75 { 5, 1 },
76};
77
78/* Bits 4 and 5 enable relays that add /10 filters to the chain
79 * Bits 0 and 1 select an output from a resistor array */
80static const uint8_t vdivs_map[] = {
81 0x01, 0x02, 0x03, 0x21, 0x22, 0x23, 0x31, 0x32, 0x33
82};
83
84
85static const char *trigger_sources[] = {
86 "A", "B", "EXT"
87};
88
89static const uint8_t trigger_sources_map[] = {
90 0x00, 0x80, 0x40
91};
92
93static const char *trigger_slopes[] = {
94 "f", "r"
95};
96
97static const char *coupling[] = {
98 "DC", "AC", "GND"
99};
100
101static const uint8_t coupling_map[] = {
102 0x00, 0x08, 0x04
103};
104
c9404469
DG
105static GSList *scan_port(GSList *devices, struct sr_dev_driver *di,
106 struct parport *port)
05ac4a98 107{
c9404469
DG
108 struct sr_dev_inst *sdi;
109 struct sr_channel *ch;
110 struct sr_channel_group *cg;
111 struct dev_context *devc;
05ac4a98 112 struct drv_context *drvc;
c9404469 113 int i;
05ac4a98 114
c9404469
DG
115 if (ieee1284_open(port, 0, &i) != E1284_OK) {
116 sr_err("Can't open parallel port %s", port->name);
117 goto fail1;
118 }
05ac4a98 119
c9404469
DG
120 if ((i & (CAP1284_RAW | CAP1284_BYTE)) != (CAP1284_RAW | CAP1284_BYTE)) {
121 sr_err("Parallel port %s does not provide low-level bidirection access",
122 port->name);
123 goto fail2;
124 }
125
126 if (ieee1284_claim(port) != E1284_OK) {
127 sr_err("Parallel port %s already in use", port->name);
128 goto fail2;
129 }
130
131 if (!hung_chang_dso_2100_check_id(port))
132 goto fail3;
133
134 sdi = g_malloc0(sizeof(struct sr_dev_inst));
135 sdi->status = SR_ST_INACTIVE;
136 sdi->vendor = g_strdup("Hung-Chang");
137 sdi->model = g_strdup("DSO-2100");
138 sdi->driver = di;
05ac4a98 139 drvc = di->context;
c9404469
DG
140 sdi->inst_type = 0; /* FIXME */
141 sdi->conn = port;
142 ieee1284_ref(port);
143
144 for (i = 0; i < NUM_CHANNELS; i++) {
145 cg = g_malloc0(sizeof(struct sr_channel_group));
146 cg->name = g_strdup(trigger_sources[i]);
147 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, FALSE, trigger_sources[i]);
148 cg->channels = g_slist_append(cg->channels, ch);
149 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
150 }
151
152 devc = g_malloc0(sizeof(struct dev_context));
153 devc->enabled_channel = g_slist_append(NULL, NULL);
154 devc->channel = 0;
155 devc->rate = 0;
156 devc->probe[0] = 10;
157 devc->probe[1] = 10;
158 devc->cctl[0] = 0x31; /* 1V/div, DC coupling, trigger on channel A*/
159 devc->cctl[1] = 0x31; /* 1V/div, DC coupling, no tv sync trigger */
160 devc->edge = 0;
161 devc->tlevel = 0x80;
162 devc->pos[0] = 0x80;
163 devc->pos[1] = 0x80;
164 devc->offset[0] = 0x80;
165 devc->offset[1] = 0x80;
166 devc->gain[0] = 0x80;
167 devc->gain[1] = 0x80;
168 devc->frame_limit = 0;
169 devc->last_step = 0; /* buffersize = 1000 */
170 sdi->priv = devc;
171
172 drvc->instances = g_slist_append(drvc->instances, sdi);
173 devices = g_slist_append(devices, sdi);
174
175fail3:
176 ieee1284_release(port);
177fail2:
178 ieee1284_close(port);
179fail1:
180 return devices;
181}
182
183static GSList *scan(struct sr_dev_driver *di, GSList *options)
184{
185 struct parport_list ports;
186 struct sr_config *src;
187 const char *conn = NULL;
188 GSList *devices, *option;
189 gboolean port_found;
190 int i;
191
192
193 for (option = options; option; option = option->next) {
194 src = option->data;
195 if (src->key == SR_CONF_CONN) {
196 conn = g_variant_get_string(src->data, NULL);
197 break;
198 }
199 }
200
201 if (!conn)
202 return NULL;
05ac4a98 203
c9404469
DG
204 if (ieee1284_find_ports(&ports, 0) != E1284_OK)
205 return NULL;
206
207 devices = NULL;
208 port_found = FALSE;
209 for (i = 0; i < ports.portc; i++)
210 if (!strcmp(ports.portv[i]->name, conn)) {
211 port_found = TRUE;
212 devices = scan_port(devices, di, ports.portv[i]);
213 }
214
215 if (!port_found) {
216 sr_err("Parallel port %s not found. Valid names are:", conn);
217 for (i = 0; i < ports.portc; i++)
218 sr_err("\t%s", ports.portv[i]->name);
219 }
220
221 ieee1284_free_ports(&ports);
05ac4a98
DG
222
223 return devices;
224}
225
c9404469
DG
226static void clear_private(void *priv)
227{
228 struct dev_context *devc = priv;
229
230 g_slist_free(devc->enabled_channel);
231}
232
05ac4a98
DG
233static int dev_clear(const struct sr_dev_driver *di)
234{
c9404469
DG
235 struct drv_context *drvc = di->context;
236 struct sr_dev_inst *sdi;
237 GSList *l;
238
239 if (drvc) {
240 for (l = drvc->instances; l; l = l->next) {
241 sdi = l->data;
242 ieee1284_unref(sdi->conn);
243 }
244 }
245
246 return std_dev_clear(di, clear_private);
05ac4a98
DG
247}
248
249static int dev_open(struct sr_dev_inst *sdi)
250{
c9404469
DG
251 struct dev_context *devc = sdi->priv;
252 int i;
253
254 if (sdi->status != SR_ST_INACTIVE)
255 goto fail1;
256
257 if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK)
258 goto fail1;
05ac4a98 259
c9404469
DG
260 if (ieee1284_claim(sdi->conn) != E1284_OK)
261 goto fail2;
262
263 if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK)
264 goto fail3;
265
266 if (hung_chang_dso_2100_move_to(sdi, 1))
267 goto fail3;
268
269 devc->samples = g_try_malloc(1000 * sizeof(*devc->samples));
270 if (!devc->samples)
271 goto fail3;
05ac4a98
DG
272
273 sdi->status = SR_ST_ACTIVE;
274
275 return SR_OK;
c9404469
DG
276
277fail3:
278 hung_chang_dso_2100_reset_port(sdi->conn);
279 ieee1284_release(sdi->conn);
280fail2:
281 ieee1284_close(sdi->conn);
282fail1:
283 return SR_ERR;
05ac4a98
DG
284}
285
286static int dev_close(struct sr_dev_inst *sdi)
287{
c9404469 288 struct dev_context *devc = sdi->priv;
05ac4a98 289
c9404469
DG
290 if (sdi->status != SR_ST_ACTIVE)
291 return SR_OK;
05ac4a98 292
c9404469
DG
293 g_free(devc->samples);
294 hung_chang_dso_2100_reset_port(sdi->conn);
295 ieee1284_release(sdi->conn);
296 ieee1284_close(sdi->conn);
05ac4a98
DG
297 sdi->status = SR_ST_INACTIVE;
298
299 return SR_OK;
300}
301
c9404469
DG
302static int find_in_array(GVariant *data, const GVariantType *type,
303 const void *arr, int n)
304{
305 const char * const *sarr;
306 const char *s;
307 const uint64_t *u64arr;
308 const uint8_t *u8arr;
309 uint64_t u64;
310 uint8_t u8;
311 int i;
312
313 if (!g_variant_is_of_type(data, type))
314 return -1;
315
316 switch (g_variant_classify(data)) {
317 case G_VARIANT_CLASS_STRING:
318 s = g_variant_get_string(data, NULL);
319 sarr = arr;
320
321 for (i = 0; i < n; i++)
322 if (!strcmp(s, sarr[i]))
323 return i;
324 break;
325 case G_VARIANT_CLASS_UINT64:
326 u64 = g_variant_get_uint64(data);
327 u64arr = arr;
328
329 for (i = 0; i < n; i++)
330 if (u64 == u64arr[i])
331 return i;
332 break;
333 case G_VARIANT_CLASS_BYTE:
334 u8 = g_variant_get_byte(data);
335 u8arr = arr;
336
337 for (i = 0; i < n; i++)
338 if (u8 == u8arr[i])
339 return i;
340 default:
341 break;
342 }
343
344 return -1;
345}
346
347static int reverse_map(uint8_t u, const uint8_t *arr, int n)
348{
349 GVariant *v = g_variant_new_byte(u);
350 int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
351 g_variant_unref(v);
352 return i;
05ac4a98
DG
353}
354
355static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
356 const struct sr_channel_group *cg)
357{
c9404469
DG
358 struct dev_context *devc = sdi->priv;
359 struct parport *port;
360 int ret, i, ch = -1;
05ac4a98 361
c9404469
DG
362 if (cg) /* sr_config_get will validate cg using config_list */
363 ch = ((struct sr_channel *)cg->channels->data)->index;
05ac4a98
DG
364
365 ret = SR_OK;
366 switch (key) {
c9404469
DG
367 case SR_CONF_CONN:
368 port = sdi->conn;
369 *data = g_variant_new_string(port->name);
370 break;
371 case SR_CONF_LIMIT_FRAMES:
372 *data = g_variant_new_uint64(devc->frame_limit);
373 break;
374 case SR_CONF_SAMPLERATE:
375 *data = g_variant_new_uint64(samplerates[devc->rate]);
376 break;
377 case SR_CONF_TRIGGER_SOURCE:
378 i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map,
379 ARRAY_SIZE(trigger_sources_map));
380 if (i == -1)
381 ret = SR_ERR;
382 else
383 *data = g_variant_new_string(trigger_sources[i]);
384 break;
385 case SR_CONF_TRIGGER_SLOPE:
386 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
387 ret = SR_ERR;
388 else
389 *data = g_variant_new_string(trigger_slopes[devc->edge]);
390 break;
391 case SR_CONF_BUFFERSIZE:
392 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
393 break;
394 case SR_CONF_VDIV:
395 if (ch == -1) {
396 ret = SR_ERR_CHANNEL_GROUP;
397 } else {
398 i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map,
399 ARRAY_SIZE(vdivs_map));
400 if (i == -1)
401 ret = SR_ERR;
402 else
403 *data = g_variant_new("(tt)", vdivs[i][0],
404 vdivs[i][1]);
405 }
406 break;
407 case SR_CONF_COUPLING:
408 if (ch == -1) {
409 ret = SR_ERR_CHANNEL_GROUP;
410 } else {
411 i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map,
412 ARRAY_SIZE(coupling_map));
413 if (i == -1)
414 ret = SR_ERR;
415 else
416 *data = g_variant_new_string(coupling[i]);
417 }
418 break;
419 case SR_CONF_PROBE_FACTOR:
420 if (ch == -1)
421 ret = SR_ERR_CHANNEL_GROUP;
422 else
423 *data = g_variant_new_uint64(devc->probe[ch]);
424 break;
05ac4a98 425 default:
c9404469 426 ret = SR_ERR_NA;
05ac4a98
DG
427 }
428
429 return ret;
430}
431
432static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
433 const struct sr_channel_group *cg)
434{
c9404469
DG
435 struct dev_context *devc = sdi->priv;
436 int ret, i, ch = -1;
437 uint64_t u, v;
05ac4a98 438
c9404469
DG
439 if (cg) /* sr_config_set will validate cg using config_list */
440 ch = ((struct sr_channel *)cg->channels->data)->index;
05ac4a98
DG
441
442 if (sdi->status != SR_ST_ACTIVE)
443 return SR_ERR_DEV_CLOSED;
444
445 ret = SR_OK;
446 switch (key) {
c9404469
DG
447 case SR_CONF_LIMIT_FRAMES:
448 devc->frame_limit = g_variant_get_uint64(data);
449 break;
450 case SR_CONF_SAMPLERATE:
451 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
452 samplerates, ARRAY_SIZE(samplerates));
453 if (i == -1)
454 ret = SR_ERR_ARG;
455 else
456 devc->rate = i;
457 break;
458 case SR_CONF_TRIGGER_SOURCE:
459 i = find_in_array(data, G_VARIANT_TYPE_STRING,
460 trigger_sources, ARRAY_SIZE(trigger_sources));
461 if (i == -1)
462 ret = SR_ERR_ARG;
463 else
464 devc->cctl[0] = (devc->cctl[0] & 0x3F)
465 | trigger_sources_map[i];
466 break;
467 case SR_CONF_TRIGGER_SLOPE:
468 i = find_in_array(data, G_VARIANT_TYPE_STRING,
469 trigger_slopes, ARRAY_SIZE(trigger_slopes));
470 if (i == -1)
471 ret = SR_ERR_ARG;
472 else
473 devc->edge = i;
474 break;
475 case SR_CONF_BUFFERSIZE:
476 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
477 buffersizes, ARRAY_SIZE(buffersizes));
478 if (i == -1)
479 ret = SR_ERR_ARG;
480 else
481 devc->last_step = i;
482 break;
483 case SR_CONF_VDIV:
484 if (ch == -1) {
485 ret = SR_ERR_CHANNEL_GROUP;
486 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
487 ret = SR_ERR_ARG;
488 } else {
489 g_variant_get(data, "(tt)", &u, &v);
490 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
491 if (vdivs[i][0] == u && vdivs[i][1] == v)
492 break;
493 if (i == ARRAY_SIZE(vdivs))
494 ret = SR_ERR_ARG;
495 else
496 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
497 | vdivs_map[i];
498 }
499 break;
500 case SR_CONF_COUPLING:
501 if (ch == -1) {
502 ret = SR_ERR_CHANNEL_GROUP;
503 } else {
504 i = find_in_array(data, G_VARIANT_TYPE_STRING,
505 coupling, ARRAY_SIZE(coupling));
506 if (i == -1)
507 ret = SR_ERR_ARG;
508 else
509 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
510 | coupling_map[i];
511 }
512 break;
513 case SR_CONF_PROBE_FACTOR:
514 if (ch == -1) {
515 ret = SR_ERR_CHANNEL_GROUP;
516 } else {
517 u = g_variant_get_uint64(data);
518 if (!u)
519 ret = SR_ERR_ARG;
520 else
521 devc->probe[ch] = u;
522 }
523 break;
05ac4a98
DG
524 default:
525 ret = SR_ERR_NA;
526 }
527
528 return ret;
529}
530
c9404469
DG
531static int config_channel_set(const struct sr_dev_inst *sdi,
532 struct sr_channel *ch,
533 unsigned int changes)
534{
535 struct dev_context *devc = sdi->priv;
536 uint8_t v;
537
538 if (changes & SR_CHANNEL_SET_ENABLED) {
539 if (ch->enabled) {
540 v = devc->channel | (1 << ch->index);
541 if (v & (v - 1))
542 return SR_ERR;
543 devc->channel = v;
544 devc->enabled_channel->data = ch;
545 } else {
546 devc->channel &= ~(1 << ch->index);
547 }
548 }
549 return SR_OK;
550}
551
552static int config_commit(const struct sr_dev_inst *sdi)
05ac4a98 553{
c9404469 554 uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
05ac4a98
DG
555 int ret;
556
c9404469
DG
557 if (sdi->status != SR_ST_ACTIVE)
558 return SR_ERR_DEV_CLOSED;
559
560 switch (state) {
561 case 0x03:
562 case 0x14:
563 case 0x21:
564 /* we will travel the complete config path on our way to state 1 */
565 break;
566 case 0x00:
567 state = 0x01;
568 default:
569 ret = hung_chang_dso_2100_move_to(sdi, 1);
570 if (ret != SR_OK)
571 return ret;
572 case 0x01:
573 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
574 }
575 ret = hung_chang_dso_2100_move_to(sdi, 1);
576 if (ret != SR_OK)
577 return ret;
578 return hung_chang_dso_2100_move_to(sdi, state);
579}
580
581static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
582 const struct sr_channel_group *cg)
583{
584 GVariantBuilder gvb;
585 GVariant *gvar, *rational[2];
586 GSList *l;
587 int i;
05ac4a98 588
05ac4a98 589 switch (key) {
c9404469
DG
590 case SR_CONF_SCAN_OPTIONS:
591 case SR_CONF_DEVICE_OPTIONS:
592 break;
593 case SR_CONF_SAMPLERATE:
594 case SR_CONF_TRIGGER_SOURCE:
595 case SR_CONF_TRIGGER_SLOPE:
596 case SR_CONF_BUFFERSIZE:
597 if (!sdi || cg)
598 return SR_ERR_NA;
599 break;
600 case SR_CONF_VDIV:
601 case SR_CONF_COUPLING:
602 if (!sdi)
603 return SR_ERR_NA;
604 if (!cg)
605 return SR_ERR_CHANNEL_GROUP;
606 l = g_slist_find(sdi->channel_groups, cg);
607 if (!l)
608 return SR_ERR_ARG;
609 break;
05ac4a98
DG
610 default:
611 return SR_ERR_NA;
612 }
613
c9404469
DG
614 switch (key) {
615 case SR_CONF_SCAN_OPTIONS:
616 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
617 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
618 break;
619 case SR_CONF_DEVICE_OPTIONS:
620 if (!sdi)
621 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
622 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
623 else if (!cg)
624 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
625 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
626 else
627 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
628 cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t));
629 break;
630 case SR_CONF_SAMPLERATE:
631 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
632 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
633 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
634 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
635 *data = g_variant_builder_end(&gvb);
636 break;
637 case SR_CONF_TRIGGER_SOURCE:
638 *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources));
639 break;
640 case SR_CONF_TRIGGER_SLOPE:
641 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
642 break;
643 case SR_CONF_BUFFERSIZE:
644 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
645 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
646 break;
647 case SR_CONF_VDIV:
648 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
649 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) {
650 rational[0] = g_variant_new_uint64(vdivs[i][0]);
651 rational[1] = g_variant_new_uint64(vdivs[i][1]);
652 gvar = g_variant_new_tuple(rational, 2);
653 g_variant_builder_add_value(&gvb, gvar);
654 }
655 *data = g_variant_builder_end(&gvb);
656 break;
657 case SR_CONF_COUPLING:
658 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
659 break;
660 }
661
662 return SR_OK;
05ac4a98
DG
663}
664
695dc859 665static int dev_acquisition_start(const struct sr_dev_inst *sdi)
05ac4a98 666{
c9404469
DG
667 struct dev_context *devc = sdi->priv;
668 int ret;
05ac4a98
DG
669
670 if (sdi->status != SR_ST_ACTIVE)
671 return SR_ERR_DEV_CLOSED;
672
c9404469
DG
673 if (devc->channel) {
674 static const float res_array[] = {0.5, 1, 2, 5};
675 static const uint8_t relays[] = {100, 10, 10, 1};
676 devc->factor = devc->probe[devc->channel - 1] / 32.0;
677 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
678 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
679 }
680 devc->frame = 0;
c9404469
DG
681 devc->state_known = TRUE;
682 devc->step = 0;
683 devc->adc2 = FALSE;
684 devc->retries = MAX_RETRIES;
685
686 ret = hung_chang_dso_2100_move_to(sdi, 0x21);
687 if (ret != SR_OK)
688 return ret;
689
695dc859 690 std_session_send_df_header(sdi, LOG_PREFIX);
c9404469 691
b165a242 692 sr_session_source_add(sdi->session, -1, 0, 8,
c9404469 693 hung_chang_dso_2100_poll, (void *)sdi);
05ac4a98
DG
694
695 return SR_OK;
696}
697
695dc859 698SR_PRIV int hung_chang_dso_2100_dev_acquisition_stop(const struct sr_dev_inst *sdi)
05ac4a98 699{
05ac4a98
DG
700 if (sdi->status != SR_ST_ACTIVE)
701 return SR_ERR_DEV_CLOSED;
702
695dc859 703 std_session_send_df_end(sdi, LOG_PREFIX);
b165a242 704 sr_session_source_remove(sdi->session, -1);
c9404469 705 hung_chang_dso_2100_move_to(sdi, 1);
05ac4a98
DG
706
707 return SR_OK;
708}
709
695dc859 710static int dev_acquisition_stop(struct sr_dev_inst *sdi)
c9404469 711{
695dc859 712 return hung_chang_dso_2100_dev_acquisition_stop(sdi);
c9404469
DG
713}
714
dd5c48a6 715static struct sr_dev_driver hung_chang_dso_2100_driver_info = {
05ac4a98
DG
716 .name = "hung-chang-dso-2100",
717 .longname = "Hung-Chang DSO-2100",
718 .api_version = 1,
c2fdcc25 719 .init = std_init,
700d6b64 720 .cleanup = std_cleanup,
05ac4a98 721 .scan = scan,
c01bf34c 722 .dev_list = std_dev_list,
05ac4a98
DG
723 .dev_clear = dev_clear,
724 .config_get = config_get,
725 .config_set = config_set,
c9404469
DG
726 .config_channel_set = config_channel_set,
727 .config_commit = config_commit,
05ac4a98
DG
728 .config_list = config_list,
729 .dev_open = dev_open,
730 .dev_close = dev_close,
731 .dev_acquisition_start = dev_acquisition_start,
732 .dev_acquisition_stop = dev_acquisition_stop,
733 .context = NULL,
734};
dd5c48a6 735SR_REGISTER_DEV_DRIVER(hung_chang_dso_2100_driver_info);