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