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