]> sigrok.org Git - libsigrok.git/blob - src/hardware/hantek-dso/protocol.c
drivers: Use NUM_CHANNELS in favor of hardcoded values.
[libsigrok.git] / src / hardware / hantek-dso / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  * With protocol information from the hantekdso project,
6  * Copyright (C) 2008 Oleg Khudyakov <prcoder@gmail.com>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <libusb.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 static int send_begin(const struct sr_dev_inst *sdi)
31 {
32         struct sr_usb_dev_inst *usb;
33         int ret;
34         unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
35         0x00, 0x01, 0x00};
36
37         sr_dbg("Sending CTRL_BEGINCOMMAND.");
38
39         usb = sdi->conn;
40         if ((ret = libusb_control_transfer(usb->devhdl,
41                         LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
42                         0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
43                 sr_err("Failed to send begincommand: %s.",
44                        libusb_error_name(ret));
45                 return SR_ERR;
46         }
47
48         return SR_OK;
49 }
50
51 static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen)
52 {
53         struct sr_usb_dev_inst *usb;
54         int ret, tmp;
55
56         usb = sdi->conn;
57
58         if (send_begin(sdi) != SR_OK)
59                 return SR_ERR;
60
61         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring,
62                         cmdlen, &tmp, 200)) != 0)
63                 return SR_ERR;
64
65         return SR_OK;
66 }
67
68 static int dso_getmps(libusb_device *dev)
69 {
70         struct libusb_device_descriptor des;
71         struct libusb_config_descriptor *conf_dsc;
72         const struct libusb_interface_descriptor *intf_dsc;
73         int mps;
74
75         libusb_get_device_descriptor(dev, &des);
76
77         if (des.bNumConfigurations != 1)
78                 return 0;
79
80         if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
81                 return 0;
82
83         mps = 0;
84         intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
85         if (intf_dsc->bNumEndpoints != 2)
86                 goto err;
87
88         if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
89             (2 | LIBUSB_ENDPOINT_OUT))
90                 /* The first endpoint should be 2 (outbound). */
91                 goto err;
92
93         if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
94             (6 | LIBUSB_ENDPOINT_IN))
95                 /* The second endpoint should be 6 (inbound). */
96                 goto err;
97
98         mps = intf_dsc->endpoint[1].wMaxPacketSize;
99
100 err:
101         if (conf_dsc)
102                 libusb_free_config_descriptor(conf_dsc);
103
104         return mps;
105 }
106
107 SR_PRIV int dso_open(struct sr_dev_inst *sdi)
108 {
109         struct dev_context *devc;
110         struct drv_context *drvc = sdi->driver->context;
111         struct sr_usb_dev_inst *usb;
112         struct libusb_device_descriptor des;
113         libusb_device **devlist;
114         int err, i;
115         char connection_id[64];
116
117         devc = sdi->priv;
118         usb = sdi->conn;
119
120         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
121         for (i = 0; devlist[i]; i++) {
122                 libusb_get_device_descriptor(devlist[i], &des);
123
124                 if (des.idVendor != devc->profile->fw_vid
125                     || des.idProduct != devc->profile->fw_pid)
126                         continue;
127
128                 if ((sdi->status == SR_ST_INITIALIZING) ||
129                                 (sdi->status == SR_ST_INACTIVE)) {
130                         /*
131                          * Check device by its physical USB bus/port address.
132                          */
133                         usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
134                         if (strcmp(sdi->connection_id, connection_id))
135                                 /* This is not the one. */
136                                 continue;
137                 }
138
139                 if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
140                         if (usb->address == 0xff)
141                                 /*
142                                  * first time we touch this device after firmware upload,
143                                  * so we don't know the address yet.
144                                  */
145                                 usb->address = libusb_get_device_address(devlist[i]);
146
147                         if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
148                                 sr_err("Wrong endpoint profile.");
149                         else {
150                                 sdi->status = SR_ST_ACTIVE;
151                                 sr_info("Opened device on %d.%d (logical) / "
152                                                 "%s (physical) interface %d.",
153                                         usb->bus, usb->address,
154                                         sdi->connection_id, USB_INTERFACE);
155                         }
156                 } else {
157                         sr_err("Failed to open device: %s.",
158                                libusb_error_name(err));
159                 }
160
161                 /* If we made it here, we handled the device (somehow). */
162                 break;
163         }
164         libusb_free_device_list(devlist, 1);
165
166         if (sdi->status != SR_ST_ACTIVE)
167                 return SR_ERR;
168
169         return SR_OK;
170 }
171
172 SR_PRIV void dso_close(struct sr_dev_inst *sdi)
173 {
174         struct sr_usb_dev_inst *usb;
175
176         usb = sdi->conn;
177
178         if (!usb->devhdl)
179                 return;
180
181         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
182                         usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
183         libusb_release_interface(usb->devhdl, USB_INTERFACE);
184         libusb_close(usb->devhdl);
185         usb->devhdl = NULL;
186         sdi->status = SR_ST_INACTIVE;
187
188 }
189
190 static int get_channel_offsets(const struct sr_dev_inst *sdi)
191 {
192         struct dev_context *devc;
193         struct sr_usb_dev_inst *usb;
194         GString *gs;
195         int chan, v, ret;
196
197         sr_dbg("Getting channel offsets.");
198
199         devc = sdi->priv;
200         usb = sdi->conn;
201
202         ret = libusb_control_transfer(usb->devhdl,
203                         LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
204                         CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
205                         (unsigned char *)&devc->channel_levels,
206                         sizeof(devc->channel_levels), 200);
207         if (ret != sizeof(devc->channel_levels)) {
208                 sr_err("Failed to get channel offsets: %s.",
209                        libusb_error_name(ret));
210                 return SR_ERR;
211         }
212
213         /* Comes in as 16-bit numbers with the second byte always 0 on
214          * the DSO-2090. Guessing this is supposed to be big-endian,
215          * since that's how voltage offsets are submitted back to the DSO.
216          * Convert to host order now, so we can use them natively.
217          */
218         for (chan = 0; chan < NUM_CHANNELS; chan++) {
219                 for (v = 0; v < 9; v++) {
220                         devc->channel_levels[chan][v][0] =
221                                 g_ntohs(devc->channel_levels[chan][v][0]);
222                         devc->channel_levels[chan][v][1] =
223                                 g_ntohs(devc->channel_levels[chan][v][1]);
224                 }
225         }
226
227         if (sr_log_loglevel_get() >= SR_LOG_DBG) {
228                 gs = g_string_sized_new(128);
229                 for (chan = 0; chan < NUM_CHANNELS; chan++) {
230                         g_string_printf(gs, "CH%d:", chan + 1);
231                         for (v = 0; v < 9; v++) {
232                                 g_string_append_printf(gs, " %.4x-%.4x",
233                                         devc->channel_levels[chan][v][0],
234                                         devc->channel_levels[chan][v][1]);
235                         }
236                         sr_dbg("%s", gs->str);
237                 }
238                 g_string_free(gs, TRUE);
239         }
240
241         return SR_OK;
242 }
243
244 static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
245 {
246         struct dev_context *devc;
247         struct sr_usb_dev_inst *usb;
248         int ret, tmp;
249         uint8_t cmdstring[12];
250         uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
251                 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
252         uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
253                 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
254
255         sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
256
257         devc = sdi->priv;
258         usb = sdi->conn;
259
260         memset(cmdstring, 0, sizeof(cmdstring));
261         /* Command */
262         cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
263
264         /* Trigger source */
265         sr_dbg("Trigger source %s.", devc->triggersource);
266         if (!strcmp("CH2", devc->triggersource))
267                 tmp = 0;
268         else if (!strcmp("CH1", devc->triggersource))
269                 tmp = 1;
270         else if (!strcmp("EXT", devc->triggersource))
271                 tmp = 2;
272         else {
273                 sr_err("Invalid trigger source: '%s'.", devc->triggersource);
274                 return SR_ERR_ARG;
275         }
276         cmdstring[2] = tmp;
277
278         /* Frame size */
279         sr_dbg("Frame size: %d.", devc->framesize);
280         cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
281
282         /* Timebase fast */
283         sr_dbg("Time base index: %d.", devc->timebase);
284         if (devc->framesize == FRAMESIZE_SMALL) {
285                 if (devc->timebase < TIME_20us)
286                         tmp = 0;
287                 else if (devc->timebase == TIME_20us)
288                         tmp = 1;
289                 else if (devc->timebase == TIME_40us)
290                         tmp = 2;
291                 else if (devc->timebase == TIME_100us)
292                         tmp = 3;
293                 else if (devc->timebase >= TIME_200us)
294                         tmp = 4;
295         } else {
296                 if (devc->timebase < TIME_40us) {
297                         sr_err("Timebase < 40us only supported with 10K buffer.");
298                         return SR_ERR_ARG;
299                 }
300                 else if (devc->timebase == TIME_40us)
301                         tmp = 0;
302                 else if (devc->timebase == TIME_100us)
303                         tmp = 2;
304                 else if (devc->timebase == TIME_200us)
305                         tmp = 3;
306                 else if (devc->timebase >= TIME_400us)
307                         tmp = 4;
308         }
309         cmdstring[2] |= (tmp & 0x07) << 5;
310
311         /* Enabled channels: 00=CH1 01=CH2 10=both */
312         sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]);
313         tmp = (((devc->ch_enabled[1] ? 1 : 0) << 1) + (devc->ch_enabled[0] ? 1 : 0)) - 1;
314         cmdstring[3] = tmp;
315
316         /* Fast rates channel */
317         /* TODO: Is this right? */
318         tmp = devc->timebase < TIME_10us ? 1 : 0;
319         cmdstring[3] |= tmp << 2;
320
321         /* Trigger slope: 0=positive 1=negative */
322         /* TODO: Does this work? */
323         sr_dbg("Trigger slope: %d.", devc->triggerslope);
324         cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
325
326         /* Timebase slow */
327         if (devc->timebase < TIME_100us)
328                 tmp = 0;
329         else if (devc->timebase > TIME_400ms)
330                 tmp = 0xffed;
331         else {
332                 if (devc->framesize == FRAMESIZE_SMALL)
333                         tmp = timebase_small[devc->timebase - 3];
334                 else
335                         tmp = timebase_large[devc->timebase - 3];
336         }
337         cmdstring[4] = tmp & 0xff;
338         cmdstring[5] = (tmp >> 8) & 0xff;
339
340         /* Horizontal trigger position */
341         sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
342         tmp = 0x77fff + 0x8000 * devc->triggerposition;
343         cmdstring[6] = tmp & 0xff;
344         cmdstring[7] = (tmp >> 8) & 0xff;
345         cmdstring[10] = (tmp >> 16) & 0xff;
346
347         if (send_begin(sdi) != SR_OK)
348                 return SR_ERR;
349
350         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
351                         cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
352                 sr_err("Failed to set trigger/samplerate: %s.",
353                        libusb_error_name(ret));
354                 return SR_ERR;
355         }
356         sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
357
358         return SR_OK;
359 }
360
361 static int dso_set_filters(const struct sr_dev_inst *sdi)
362 {
363         struct dev_context *devc;
364         struct sr_usb_dev_inst *usb;
365         int ret, tmp;
366         uint8_t cmdstring[8];
367
368         sr_dbg("Preparing CMD_SET_FILTERS.");
369
370         devc = sdi->priv;
371         usb = sdi->conn;
372
373         memset(cmdstring, 0, sizeof(cmdstring));
374         cmdstring[0] = CMD_SET_FILTERS;
375         cmdstring[1] = 0x0f;
376         if (devc->filter[0]) {
377                 sr_dbg("Turning on CH1 filter.");
378                 cmdstring[2] |= 0x80;
379         }
380         if (devc->filter[1]) {
381                 sr_dbg("Turning on CH2 filter.");
382                 cmdstring[2] |= 0x40;
383         }
384         /*
385          * Not supported: filtering on the trigger
386          * cmdstring[2] |= 0x20;
387          */
388
389         if (send_begin(sdi) != SR_OK)
390                 return SR_ERR;
391
392         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
393                         cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
394                 sr_err("Failed to set filters: %s.", libusb_error_name(ret));
395                 return SR_ERR;
396         }
397         sr_dbg("Sent CMD_SET_FILTERS.");
398
399         return SR_OK;
400 }
401
402 static int dso_set_voltage(const struct sr_dev_inst *sdi)
403 {
404         struct dev_context *devc;
405         struct sr_usb_dev_inst *usb;
406         int ret, tmp;
407         uint8_t cmdstring[8];
408
409         sr_dbg("Preparing CMD_SET_VOLTAGE.");
410
411         devc = sdi->priv;
412         usb = sdi->conn;
413
414         memset(cmdstring, 0, sizeof(cmdstring));
415         cmdstring[0] = CMD_SET_VOLTAGE;
416         cmdstring[1] = 0x0f;
417         cmdstring[2] = 0x30;
418
419         /* CH1 volts/div is encoded in bits 0-1 */
420         sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]);
421         switch (devc->voltage[0]) {
422         case VDIV_1V:
423         case VDIV_100MV:
424         case VDIV_10MV:
425                 cmdstring[2] |= 0x00;
426                 break;
427         case VDIV_2V:
428         case VDIV_200MV:
429         case VDIV_20MV:
430                 cmdstring[2] |= 0x01;
431                 break;
432         case VDIV_5V:
433         case VDIV_500MV:
434         case VDIV_50MV:
435                 cmdstring[2] |= 0x02;
436                 break;
437         }
438
439         /* CH2 volts/div is encoded in bits 2-3 */
440         sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]);
441         switch (devc->voltage[1]) {
442         case VDIV_1V:
443         case VDIV_100MV:
444         case VDIV_10MV:
445                 cmdstring[2] |= 0x00;
446                 break;
447         case VDIV_2V:
448         case VDIV_200MV:
449         case VDIV_20MV:
450                 cmdstring[2] |= 0x04;
451                 break;
452         case VDIV_5V:
453         case VDIV_500MV:
454         case VDIV_50MV:
455                 cmdstring[2] |= 0x08;
456                 break;
457         }
458
459         if (send_begin(sdi) != SR_OK)
460                 return SR_ERR;
461
462         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
463                         cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
464                 sr_err("Failed to set voltage: %s.", libusb_error_name(ret));
465                 return SR_ERR;
466         }
467         sr_dbg("Sent CMD_SET_VOLTAGE.");
468
469         return SR_OK;
470 }
471
472 static int dso_set_relays(const struct sr_dev_inst *sdi)
473 {
474         struct dev_context *devc;
475         struct sr_usb_dev_inst *usb;
476         GString *gs;
477         int ret, i;
478         uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
479                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
480
481         sr_dbg("Preparing CTRL_SETRELAYS.");
482
483         devc = sdi->priv;
484         usb = sdi->conn;
485
486         if (devc->voltage[0] < VDIV_1V)
487                 relays[1] = ~relays[1];
488
489         if (devc->voltage[0] < VDIV_100MV)
490                 relays[2] = ~relays[2];
491
492         sr_dbg("CH1 coupling: %d.", devc->coupling[0]);
493         if (devc->coupling[0] != COUPLING_AC)
494                 relays[3] = ~relays[3];
495
496         if (devc->voltage[1] < VDIV_1V)
497                 relays[4] = ~relays[4];
498
499         if (devc->voltage[1] < VDIV_100MV)
500                 relays[5] = ~relays[5];
501
502         sr_dbg("CH2 coupling: %d.", devc->coupling[1]);
503         if (devc->coupling[1] != COUPLING_AC)
504                 relays[6] = ~relays[6];
505
506         if (!strcmp(devc->triggersource, "EXT"))
507                 relays[7] = ~relays[7];
508
509         if (sr_log_loglevel_get() >= SR_LOG_DBG) {
510                 gs = g_string_sized_new(128);
511                 g_string_printf(gs, "Relays:");
512                 for (i = 0; i < 17; i++)
513                         g_string_append_printf(gs, " %.2x", relays[i]);
514                 sr_dbg("%s", gs->str);
515                 g_string_free(gs, TRUE);
516         }
517
518         if ((ret = libusb_control_transfer(usb->devhdl,
519                         LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
520                         0, 0, relays, 17, 100)) != sizeof(relays)) {
521                 sr_err("Failed to set relays: %s.", libusb_error_name(ret));
522                 return SR_ERR;
523         }
524         sr_dbg("Sent CTRL_SETRELAYS.");
525
526         return SR_OK;
527 }
528
529 static int dso_set_voffsets(const struct sr_dev_inst *sdi)
530 {
531         struct dev_context *devc;
532         struct sr_usb_dev_inst *usb;
533         int offset, ret;
534         uint16_t *ch_levels;
535         uint8_t offsets[17];
536
537         sr_dbg("Preparing CTRL_SETOFFSET.");
538
539         devc = sdi->priv;
540         usb = sdi->conn;
541
542         memset(offsets, 0, sizeof(offsets));
543         /* Channel 1 */
544         ch_levels = devc->channel_levels[0][devc->voltage[0]];
545         offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
546         offsets[0] = (offset >> 8) | 0x20;
547         offsets[1] = offset & 0xff;
548         sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
549                offsets[0], offsets[1]);
550
551         /* Channel 2 */
552         ch_levels = devc->channel_levels[1][devc->voltage[1]];
553         offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
554         offsets[2] = (offset >> 8) | 0x20;
555         offsets[3] = offset & 0xff;
556         sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
557                offsets[2], offsets[3]);
558
559         /* Trigger */
560         offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
561         offsets[4] = (offset >> 8) | 0x20;
562         offsets[5] = offset & 0xff;
563         sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
564                         offsets[4], offsets[5]);
565
566         if ((ret = libusb_control_transfer(usb->devhdl,
567                         LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
568                         0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
569                 sr_err("Failed to set offsets: %s.", libusb_error_name(ret));
570                 return SR_ERR;
571         }
572         sr_dbg("Sent CTRL_SETOFFSET.");
573
574         return SR_OK;
575 }
576
577 SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi)
578 {
579         struct sr_usb_dev_inst *usb;
580         int ret, tmp;
581         uint8_t cmdstring[2];
582
583         sr_dbg("Sending CMD_ENABLE_TRIGGER.");
584
585         usb = sdi->conn;
586
587         memset(cmdstring, 0, sizeof(cmdstring));
588         cmdstring[0] = CMD_ENABLE_TRIGGER;
589         cmdstring[1] = 0x00;
590
591         if (send_begin(sdi) != SR_OK)
592                 return SR_ERR;
593
594         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
595                         cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
596                 sr_err("Failed to enable trigger: %s.", libusb_error_name(ret));
597                 return SR_ERR;
598         }
599
600         return SR_OK;
601 }
602
603 SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
604 {
605         struct sr_usb_dev_inst *usb;
606         int ret, tmp;
607         uint8_t cmdstring[2];
608
609         sr_dbg("Sending CMD_FORCE_TRIGGER.");
610
611         usb = sdi->conn;
612
613         memset(cmdstring, 0, sizeof(cmdstring));
614         cmdstring[0] = CMD_FORCE_TRIGGER;
615         cmdstring[1] = 0x00;
616
617         if (send_begin(sdi) != SR_OK)
618                 return SR_ERR;
619
620         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
621                         cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
622                 sr_err("Failed to force trigger: %s.", libusb_error_name(ret));
623                 return SR_ERR;
624         }
625
626         return SR_OK;
627 }
628
629 SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
630 {
631
632         sr_dbg("Initializing DSO.");
633
634         if (get_channel_offsets(sdi) != SR_OK)
635                 return SR_ERR;
636
637         if (dso_set_trigger_samplerate(sdi) != SR_OK)
638                 return SR_ERR;
639
640         if (dso_set_filters(sdi) != SR_OK)
641                 return SR_ERR;
642
643         if (dso_set_voltage(sdi) != SR_OK)
644                 return SR_ERR;
645
646         if (dso_set_relays(sdi) != SR_OK)
647                 return SR_ERR;
648
649         if (dso_set_voffsets(sdi) != SR_OK)
650                 return SR_ERR;
651
652         if (dso_enable_trigger(sdi) != SR_OK)
653                 return SR_ERR;
654
655         return SR_OK;
656 }
657
658 SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
659                 uint8_t *capturestate, uint32_t *trigger_offset)
660 {
661         struct sr_usb_dev_inst *usb;
662         int ret, tmp, i;
663         unsigned int bitvalue, toff;
664         uint8_t cmdstring[2], inbuf[512];
665
666         sr_dbg("Sending CMD_GET_CAPTURESTATE.");
667
668         usb = sdi->conn;
669
670         cmdstring[0] = CMD_GET_CAPTURESTATE;
671         cmdstring[1] = 0;
672
673         if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
674                 sr_dbg("Failed to send get_capturestate command: %s.",
675                        libusb_error_name(ret));
676                 return SR_ERR;
677         }
678
679         if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN,
680                         inbuf, 512, &tmp, 100)) != 0) {
681                 sr_dbg("Failed to get capturestate: %s.",
682                        libusb_error_name(ret));
683                 return SR_ERR;
684         }
685         *capturestate = inbuf[0];
686         toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
687
688         /*
689          * This conversion comes from the openhantek project.
690          * Each set bit in the 24-bit value inverts all bits with a lower
691          * value. No idea why the device reports the trigger point this way.
692          */
693         bitvalue = 1;
694         for (i = 0; i < 24; i++) {
695                 /* Each set bit inverts all bits with a lower value. */
696                 if (toff & bitvalue)
697                         toff ^= bitvalue - 1;
698                 bitvalue <<= 1;
699         }
700         *trigger_offset = toff;
701
702         return SR_OK;
703 }
704
705 SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi)
706 {
707         int ret;
708         uint8_t cmdstring[2];
709
710         sr_dbg("Sending CMD_CAPTURE_START.");
711
712         cmdstring[0] = CMD_CAPTURE_START;
713         cmdstring[1] = 0;
714
715         if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
716                 sr_err("Failed to send capture_start command: %s.",
717                        libusb_error_name(ret));
718                 return SR_ERR;
719         }
720
721         return SR_OK;
722 }
723
724 SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
725                 libusb_transfer_cb_fn cb)
726 {
727         struct dev_context *devc;
728         struct sr_usb_dev_inst *usb;
729         struct libusb_transfer *transfer;
730         int num_transfers, ret, i;
731         uint8_t cmdstring[2];
732         unsigned char *buf;
733
734         sr_dbg("Sending CMD_GET_CHANNELDATA.");
735
736         devc = sdi->priv;
737         usb = sdi->conn;
738
739         cmdstring[0] = CMD_GET_CHANNELDATA;
740         cmdstring[1] = 0;
741
742         if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
743                 sr_err("Failed to get channel data: %s.",
744                        libusb_error_name(ret));
745                 return SR_ERR;
746         }
747
748         /* TODO: DSO-2xxx only. */
749         num_transfers = devc->framesize *
750                         sizeof(unsigned short) / devc->epin_maxpacketsize;
751         sr_dbg("Queueing up %d transfers.", num_transfers);
752         for (i = 0; i < num_transfers; i++) {
753                 if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
754                         sr_err("Failed to malloc USB endpoint buffer.");
755                         return SR_ERR_MALLOC;
756                 }
757                 transfer = libusb_alloc_transfer(0);
758                 libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf,
759                                 devc->epin_maxpacketsize, cb, (void *)sdi, 40);
760                 if ((ret = libusb_submit_transfer(transfer)) != 0) {
761                         sr_err("Failed to submit transfer: %s.",
762                                libusb_error_name(ret));
763                         /* TODO: Free them all. */
764                         libusb_free_transfer(transfer);
765                         g_free(buf);
766                         return SR_ERR;
767                 }
768         }
769
770         return SR_OK;
771 }