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