]> sigrok.org Git - libsigrok.git/blob - src/hardware/hung-chang-dso-2100/api.c
9f7526172d7c0ed95c650bd17766ebb962ffc5a2
[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 (ieee1284_open(sdi->conn, 0, &i) != E1284_OK)
250                 goto fail1;
251
252         if (ieee1284_claim(sdi->conn) != E1284_OK)
253                 goto fail2;
254
255         if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK)
256                 goto fail3;
257
258         if (hung_chang_dso_2100_move_to(sdi, 1))
259                 goto fail3;
260
261         devc->samples = g_try_malloc(1000 * sizeof(*devc->samples));
262         if (!devc->samples)
263                 goto fail3;
264
265         sdi->status = SR_ST_ACTIVE;
266
267         return SR_OK;
268
269 fail3:
270         hung_chang_dso_2100_reset_port(sdi->conn);
271         ieee1284_release(sdi->conn);
272 fail2:
273         ieee1284_close(sdi->conn);
274 fail1:
275         return SR_ERR;
276 }
277
278 static int dev_close(struct sr_dev_inst *sdi)
279 {
280         struct dev_context *devc = sdi->priv;
281
282         g_free(devc->samples);
283         hung_chang_dso_2100_reset_port(sdi->conn);
284         ieee1284_release(sdi->conn);
285         ieee1284_close(sdi->conn);
286
287         sdi->status = SR_ST_INACTIVE;
288
289         return SR_OK;
290 }
291
292 static int find_in_array(GVariant *data, const GVariantType *type,
293                          const void *arr, int n)
294 {
295         const char * const *sarr;
296         const char *s;
297         const uint64_t *u64arr;
298         const uint8_t *u8arr;
299         uint64_t u64;
300         uint8_t u8;
301         int i;
302
303         if (!g_variant_is_of_type(data, type))
304                 return -1;
305
306         switch (g_variant_classify(data)) {
307         case G_VARIANT_CLASS_STRING:
308                 s = g_variant_get_string(data, NULL);
309                 sarr = arr;
310
311                 for (i = 0; i < n; i++)
312                         if (!strcmp(s, sarr[i]))
313                                 return i;
314                 break;
315         case G_VARIANT_CLASS_UINT64:
316                 u64 = g_variant_get_uint64(data);
317                 u64arr = arr;
318
319                 for (i = 0; i < n; i++)
320                         if (u64 == u64arr[i])
321                                 return i;
322                 break;
323         case G_VARIANT_CLASS_BYTE:
324                 u8 = g_variant_get_byte(data);
325                 u8arr = arr;
326
327                 for (i = 0; i < n; i++)
328                         if (u8 == u8arr[i])
329                                 return i;
330         default:
331                 break;
332         }
333
334         return -1;
335 }
336
337 static int reverse_map(uint8_t u, const uint8_t *arr, int n)
338 {
339         GVariant *v = g_variant_new_byte(u);
340         int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
341         g_variant_unref(v);
342         return i;
343 }
344
345 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
346                 const struct sr_channel_group *cg)
347 {
348         struct dev_context *devc = sdi->priv;
349         struct parport *port;
350         int ret, i, ch = -1;
351
352         if (cg) /* sr_config_get will validate cg using config_list */
353                 ch = ((struct sr_channel *)cg->channels->data)->index;
354
355         ret = SR_OK;
356         switch (key) {
357         case SR_CONF_CONN:
358                 port = sdi->conn;
359                 *data = g_variant_new_string(port->name);
360                 break;
361         case SR_CONF_LIMIT_FRAMES:
362                 *data = g_variant_new_uint64(devc->frame_limit);
363                 break;
364         case SR_CONF_SAMPLERATE:
365                 *data = g_variant_new_uint64(samplerates[devc->rate]);
366                 break;
367         case SR_CONF_TRIGGER_SOURCE:
368                 i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map,
369                                 ARRAY_SIZE(trigger_sources_map));
370                 if (i == -1)
371                         ret = SR_ERR;
372                 else
373                         *data = g_variant_new_string(trigger_sources[i]);
374                 break;
375         case SR_CONF_TRIGGER_SLOPE:
376                 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
377                         ret = SR_ERR;
378                 else
379                         *data = g_variant_new_string(trigger_slopes[devc->edge]);
380                 break;
381         case SR_CONF_BUFFERSIZE:
382                 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
383                 break;
384         case SR_CONF_VDIV:
385                 if (ch == -1) {
386                         ret = SR_ERR_CHANNEL_GROUP;
387                 } else {
388                         i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map,
389                                         ARRAY_SIZE(vdivs_map));
390                         if (i == -1)
391                                 ret = SR_ERR;
392                         else
393                                 *data = g_variant_new("(tt)", vdivs[i][0],
394                                                       vdivs[i][1]);
395                 }
396                 break;
397         case SR_CONF_COUPLING:
398                 if (ch == -1) {
399                         ret = SR_ERR_CHANNEL_GROUP;
400                 } else {
401                         i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map,
402                                         ARRAY_SIZE(coupling_map));
403                         if (i == -1)
404                                 ret = SR_ERR;
405                         else
406                                 *data = g_variant_new_string(coupling[i]);
407                 }
408                 break;
409         case SR_CONF_PROBE_FACTOR:
410                 if (ch == -1)
411                         ret = SR_ERR_CHANNEL_GROUP;
412                 else
413                         *data = g_variant_new_uint64(devc->probe[ch]);
414                 break;
415         default:
416                 ret = SR_ERR_NA;
417         }
418
419         return ret;
420 }
421
422 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
423                 const struct sr_channel_group *cg)
424 {
425         struct dev_context *devc = sdi->priv;
426         int ret, i, ch = -1;
427         uint64_t u, v;
428
429         if (cg) /* sr_config_set will validate cg using config_list */
430                 ch = ((struct sr_channel *)cg->channels->data)->index;
431
432         ret = SR_OK;
433         switch (key) {
434         case SR_CONF_LIMIT_FRAMES:
435                 devc->frame_limit = g_variant_get_uint64(data);
436                 break;
437         case SR_CONF_SAMPLERATE:
438                 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
439                                   samplerates, ARRAY_SIZE(samplerates));
440                 if (i == -1)
441                         ret = SR_ERR_ARG;
442                 else
443                         devc->rate = i;
444                 break;
445         case SR_CONF_TRIGGER_SOURCE:
446                 i = find_in_array(data, G_VARIANT_TYPE_STRING,
447                                   trigger_sources, ARRAY_SIZE(trigger_sources));
448                 if (i == -1)
449                         ret = SR_ERR_ARG;
450                 else
451                         devc->cctl[0] = (devc->cctl[0] & 0x3F)
452                                       | trigger_sources_map[i];
453                 break;
454         case SR_CONF_TRIGGER_SLOPE:
455                 i = find_in_array(data, G_VARIANT_TYPE_STRING,
456                                   trigger_slopes, ARRAY_SIZE(trigger_slopes));
457                 if (i == -1)
458                         ret = SR_ERR_ARG;
459                 else
460                         devc->edge = i;
461                 break;
462         case SR_CONF_BUFFERSIZE:
463                 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
464                                   buffersizes, ARRAY_SIZE(buffersizes));
465                 if (i == -1)
466                         ret = SR_ERR_ARG;
467                 else
468                         devc->last_step = i;
469                 break;
470         case SR_CONF_VDIV:
471                 if (ch == -1) {
472                         ret = SR_ERR_CHANNEL_GROUP;
473                 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
474                         ret = SR_ERR_ARG;
475                 } else {
476                         g_variant_get(data, "(tt)", &u, &v);
477                         for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
478                                 if (vdivs[i][0] == u && vdivs[i][1] == v)
479                                         break;
480                         if (i == ARRAY_SIZE(vdivs))
481                                 ret = SR_ERR_ARG;
482                         else
483                                 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
484                                                | vdivs_map[i];
485                 }
486                 break;
487         case SR_CONF_COUPLING:
488                 if (ch == -1) {
489                         ret = SR_ERR_CHANNEL_GROUP;
490                 } else {
491                         i = find_in_array(data, G_VARIANT_TYPE_STRING,
492                                           coupling, ARRAY_SIZE(coupling));
493                         if (i == -1)
494                                 ret = SR_ERR_ARG;
495                         else
496                                 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
497                                                | coupling_map[i];
498                 }
499                 break;
500         case SR_CONF_PROBE_FACTOR:
501                 if (ch == -1) {
502                         ret = SR_ERR_CHANNEL_GROUP;
503                 } else {
504                         u = g_variant_get_uint64(data);
505                         if (!u)
506                                 ret = SR_ERR_ARG;
507                         else
508                                 devc->probe[ch] = u;
509                 }
510                 break;
511         default:
512                 ret = SR_ERR_NA;
513         }
514
515         return ret;
516 }
517
518 static int config_channel_set(const struct sr_dev_inst *sdi,
519                               struct sr_channel *ch,
520                               unsigned int changes)
521 {
522         struct dev_context *devc = sdi->priv;
523         uint8_t v;
524
525         if (changes & SR_CHANNEL_SET_ENABLED) {
526                 if (ch->enabled) {
527                         v = devc->channel | (1 << ch->index);
528                         if (v & (v - 1))
529                                 return SR_ERR;
530                         devc->channel = v;
531                         devc->enabled_channel->data = ch;
532                 } else {
533                         devc->channel &= ~(1 << ch->index);
534                 }
535         }
536         return SR_OK;
537 }
538
539 static int config_commit(const struct sr_dev_inst *sdi)
540 {
541         uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
542         int ret;
543
544         switch (state) {
545         case 0x03:
546         case 0x14:
547         case 0x21:
548                 /* we will travel the complete config path on our way to state 1 */
549                 break;
550         case 0x00:
551                 state = 0x01;
552                 /* Fallthrough */
553         default:
554                 ret = hung_chang_dso_2100_move_to(sdi, 1);
555                 if (ret != SR_OK)
556                         return ret;
557                 /* Fallthrough */
558         case 0x01:
559                 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
560         }
561         ret = hung_chang_dso_2100_move_to(sdi, 1);
562         if (ret != SR_OK)
563                 return ret;
564         return hung_chang_dso_2100_move_to(sdi, state);
565 }
566
567 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
568                 const struct sr_channel_group *cg)
569 {
570         GVariantBuilder gvb;
571         GVariant *gvar, *rational[2];
572         GSList *l;
573         int i;
574
575         switch (key) {
576                 case SR_CONF_SCAN_OPTIONS:
577         case SR_CONF_DEVICE_OPTIONS:
578                 break;
579         case SR_CONF_SAMPLERATE:
580         case SR_CONF_TRIGGER_SOURCE:
581         case SR_CONF_TRIGGER_SLOPE:
582         case SR_CONF_BUFFERSIZE:
583                 if (!sdi || cg)
584                         return SR_ERR_NA;
585                 break;
586         case SR_CONF_VDIV:
587         case SR_CONF_COUPLING:
588                 if (!sdi)
589                         return SR_ERR_NA;
590                 if (!cg)
591                         return SR_ERR_CHANNEL_GROUP;
592                 l = g_slist_find(sdi->channel_groups, cg);
593                 if (!l)
594                         return SR_ERR_ARG;
595                 break;
596         default:
597                 return SR_ERR_NA;
598         }
599
600         switch (key) {
601         case SR_CONF_SCAN_OPTIONS:
602                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
603                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
604                 break;
605         case SR_CONF_DEVICE_OPTIONS:
606                 if (!sdi)
607                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
608                                         drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
609                 else if (!cg)
610                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
611                                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
612                 else
613                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
614                                         cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t));
615                 break;
616         case SR_CONF_SAMPLERATE:
617                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
618                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
619                                 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
620                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
621                 *data = g_variant_builder_end(&gvb);
622                 break;
623         case SR_CONF_TRIGGER_SOURCE:
624                 *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources));
625                 break;
626         case SR_CONF_TRIGGER_SLOPE:
627                 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
628                 break;
629         case SR_CONF_BUFFERSIZE:
630                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
631                                 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
632                 break;
633         case SR_CONF_VDIV:
634                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
635                 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) {
636                         rational[0] = g_variant_new_uint64(vdivs[i][0]);
637                         rational[1] = g_variant_new_uint64(vdivs[i][1]);
638                         gvar = g_variant_new_tuple(rational, 2);
639                         g_variant_builder_add_value(&gvb, gvar);
640                 }
641                 *data = g_variant_builder_end(&gvb);
642                 break;
643         case SR_CONF_COUPLING:
644                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
645                 break;
646         }
647
648         return SR_OK;
649 }
650
651 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
652 {
653         struct dev_context *devc = sdi->priv;
654         int ret;
655
656         if (devc->channel) {
657                 static const float res_array[] = {0.5, 1, 2, 5};
658                 static const uint8_t relays[] = {100, 10, 10, 1};
659                 devc->factor = devc->probe[devc->channel - 1] / 32.0;
660                 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
661                 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
662         }
663         devc->frame = 0;
664         devc->state_known = TRUE;
665         devc->step = 0;
666         devc->adc2 = FALSE;
667         devc->retries = MAX_RETRIES;
668
669         ret = hung_chang_dso_2100_move_to(sdi, 0x21);
670         if (ret != SR_OK)
671                 return ret;
672
673         std_session_send_df_header(sdi);
674
675         sr_session_source_add(sdi->session, -1, 0, 8,
676                               hung_chang_dso_2100_poll, (void *)sdi);
677
678         return SR_OK;
679 }
680
681 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
682 {
683         std_session_send_df_end(sdi);
684         sr_session_source_remove(sdi->session, -1);
685         hung_chang_dso_2100_move_to(sdi, 1);
686
687         return SR_OK;
688 }
689
690 static struct sr_dev_driver hung_chang_dso_2100_driver_info = {
691         .name = "hung-chang-dso-2100",
692         .longname = "Hung-Chang DSO-2100",
693         .api_version = 1,
694         .init = std_init,
695         .cleanup = std_cleanup,
696         .scan = scan,
697         .dev_list = std_dev_list,
698         .dev_clear = dev_clear,
699         .config_get = config_get,
700         .config_set = config_set,
701         .config_channel_set = config_channel_set,
702         .config_commit = config_commit,
703         .config_list = config_list,
704         .dev_open = dev_open,
705         .dev_close = dev_close,
706         .dev_acquisition_start = dev_acquisition_start,
707         .dev_acquisition_stop = dev_acquisition_stop,
708         .context = NULL,
709 };
710 SR_REGISTER_DEV_DRIVER(hung_chang_dso_2100_driver_info);