]>
Commit | Line | Data |
---|---|---|
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 | 27 | #include "libsigrok-internal.h" |
caeb8d7a | 28 | #include "protocol.h" |
3b533202 | 29 | |
c118080b | 30 | static int send_begin(const struct sr_dev_inst *sdi) |
3b533202 | 31 | { |
c118080b | 32 | struct sr_usb_dev_inst *usb; |
3b533202 BV |
33 | int ret; |
34 | unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe, | |
35 | 0x00, 0x01, 0x00}; | |
36 | ||
e98b7f1b | 37 | sr_dbg("Sending CTRL_BEGINCOMMAND."); |
3b533202 | 38 | |
c118080b BV |
39 | usb = sdi->conn; |
40 | if ((ret = libusb_control_transfer(usb->devhdl, | |
3b533202 BV |
41 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND, |
42 | 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) { | |
d4928d71 PS |
43 | sr_err("Failed to send begincommand: %s.", |
44 | libusb_error_name(ret)); | |
3b533202 BV |
45 | return SR_ERR; |
46 | } | |
47 | ||
48 | return SR_OK; | |
49 | } | |
50 | ||
c118080b | 51 | static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen) |
3b533202 | 52 | { |
c118080b | 53 | struct sr_usb_dev_inst *usb; |
3b533202 BV |
54 | int ret, tmp; |
55 | ||
c118080b BV |
56 | usb = sdi->conn; |
57 | ||
58 | if (send_begin(sdi) != SR_OK) | |
3b533202 BV |
59 | return SR_ERR; |
60 | ||
c118080b BV |
61 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring, |
62 | cmdlen, &tmp, 200)) != 0) | |
3b533202 BV |
63 | return SR_ERR; |
64 | ||
65 | return SR_OK; | |
66 | } | |
67 | ||
d87c1766 | 68 | static int dso_getmps(libusb_device *dev) |
3b533202 BV |
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 | ||
2a8f2d41 | 75 | libusb_get_device_descriptor(dev, &des); |
3b533202 BV |
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 | ||
25a0f108 | 107 | SR_PRIV int dso_open(struct sr_dev_inst *sdi) |
3b533202 | 108 | { |
269971dd | 109 | struct dev_context *devc; |
e32862eb | 110 | struct drv_context *drvc = sdi->driver->context; |
c118080b BV |
111 | struct sr_usb_dev_inst *usb; |
112 | struct libusb_device_descriptor des; | |
113 | libusb_device **devlist; | |
395206f4 SA |
114 | int err, i; |
115 | char connection_id[64]; | |
3b533202 | 116 | |
269971dd | 117 | devc = sdi->priv; |
c118080b | 118 | usb = sdi->conn; |
3b533202 | 119 | |
d4abb463 | 120 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
3b533202 | 121 | for (i = 0; devlist[i]; i++) { |
2a8f2d41 | 122 | libusb_get_device_descriptor(devlist[i], &des); |
3b533202 | 123 | |
269971dd BV |
124 | if (des.idVendor != devc->profile->fw_vid |
125 | || des.idProduct != devc->profile->fw_pid) | |
3b533202 BV |
126 | continue; |
127 | ||
395206f4 SA |
128 | if ((sdi->status == SR_ST_INITIALIZING) || |
129 | (sdi->status == SR_ST_INACTIVE)) { | |
3b533202 | 130 | /* |
395206f4 | 131 | * Check device by its physical USB bus/port address. |
3b533202 | 132 | */ |
395206f4 SA |
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. */ | |
3b533202 BV |
136 | continue; |
137 | } | |
138 | ||
c118080b BV |
139 | if (!(err = libusb_open(devlist[i], &usb->devhdl))) { |
140 | if (usb->address == 0xff) | |
3b533202 BV |
141 | /* |
142 | * first time we touch this device after firmware upload, | |
143 | * so we don't know the address yet. | |
144 | */ | |
c118080b | 145 | usb->address = libusb_get_device_address(devlist[i]); |
3b533202 | 146 | |
e98b7f1b UH |
147 | if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i]))) |
148 | sr_err("Wrong endpoint profile."); | |
3b533202 BV |
149 | else { |
150 | sdi->status = SR_ST_ACTIVE; | |
395206f4 SA |
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); | |
3b533202 BV |
155 | } |
156 | } else { | |
d4928d71 PS |
157 | sr_err("Failed to open device: %s.", |
158 | libusb_error_name(err)); | |
3b533202 BV |
159 | } |
160 | ||
e98b7f1b | 161 | /* If we made it here, we handled the device (somehow). */ |
3b533202 BV |
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 | { | |
c118080b | 174 | struct sr_usb_dev_inst *usb; |
3b533202 | 175 | |
c118080b | 176 | usb = sdi->conn; |
3b533202 | 177 | |
98fec29e | 178 | if (!usb->devhdl) |
3b533202 BV |
179 | return; |
180 | ||
395206f4 SA |
181 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", |
182 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
c118080b BV |
183 | libusb_release_interface(usb->devhdl, USB_INTERFACE); |
184 | libusb_close(usb->devhdl); | |
185 | usb->devhdl = NULL; | |
3b533202 | 186 | sdi->status = SR_ST_INACTIVE; |
3b533202 BV |
187 | } |
188 | ||
c118080b | 189 | static int get_channel_offsets(const struct sr_dev_inst *sdi) |
3b533202 | 190 | { |
c118080b BV |
191 | struct dev_context *devc; |
192 | struct sr_usb_dev_inst *usb; | |
2715c0b8 BV |
193 | GString *gs; |
194 | int chan, v, ret; | |
3b533202 | 195 | |
e98b7f1b | 196 | sr_dbg("Getting channel offsets."); |
3b533202 | 197 | |
c118080b BV |
198 | devc = sdi->priv; |
199 | usb = sdi->conn; | |
200 | ||
201 | ret = libusb_control_transfer(usb->devhdl, | |
3b533202 BV |
202 | LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, |
203 | CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0, | |
269971dd BV |
204 | (unsigned char *)&devc->channel_levels, |
205 | sizeof(devc->channel_levels), 200); | |
206 | if (ret != sizeof(devc->channel_levels)) { | |
d4928d71 PS |
207 | sr_err("Failed to get channel offsets: %s.", |
208 | libusb_error_name(ret)); | |
3b533202 BV |
209 | return SR_ERR; |
210 | } | |
211 | ||
2715c0b8 BV |
212 | /* Comes in as 16-bit numbers with the second byte always 0 on |
213 | * the DSO-2090. Guessing this is supposed to be big-endian, | |
214 | * since that's how voltage offsets are submitted back to the DSO. | |
215 | * Convert to host order now, so we can use them natively. | |
216 | */ | |
07ffa5b3 | 217 | for (chan = 0; chan < NUM_CHANNELS; chan++) { |
2715c0b8 | 218 | for (v = 0; v < 9; v++) { |
e98b7f1b UH |
219 | devc->channel_levels[chan][v][0] = |
220 | g_ntohs(devc->channel_levels[chan][v][0]); | |
221 | devc->channel_levels[chan][v][1] = | |
222 | g_ntohs(devc->channel_levels[chan][v][1]); | |
2715c0b8 BV |
223 | } |
224 | } | |
225 | ||
226 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { | |
227 | gs = g_string_sized_new(128); | |
07ffa5b3 | 228 | for (chan = 0; chan < NUM_CHANNELS; chan++) { |
e98b7f1b | 229 | g_string_printf(gs, "CH%d:", chan + 1); |
2715c0b8 BV |
230 | for (v = 0; v < 9; v++) { |
231 | g_string_append_printf(gs, " %.4x-%.4x", | |
e98b7f1b UH |
232 | devc->channel_levels[chan][v][0], |
233 | devc->channel_levels[chan][v][1]); | |
2715c0b8 | 234 | } |
cbc6f3b2 | 235 | sr_dbg("%s", gs->str); |
2715c0b8 BV |
236 | } |
237 | g_string_free(gs, TRUE); | |
238 | } | |
239 | ||
3b533202 BV |
240 | return SR_OK; |
241 | } | |
242 | ||
95983cc3 PM |
243 | static void dso2250_set_triggerpos(int value, int long_buffer, uint8_t dest[], int offset) |
244 | { | |
245 | uint32_t min, max; | |
246 | uint32_t tmp, diff; | |
247 | ||
248 | min = long_buffer ? 0 : 0x7d7ff; | |
249 | max = 0x7ffff; | |
250 | ||
251 | diff = max - min; | |
252 | tmp = min + diff * value / 100; | |
253 | sr_dbg("2250 trigger pos: %3d%% * [0x%x,0x%x] == 0x%x", value, min, max, tmp); | |
254 | ||
255 | dest[offset + 0] = tmp & 0xff; | |
256 | dest[offset + 1] = (tmp >> 8) & 0xff; | |
257 | dest[offset + 2] = (tmp >> 16) & 0x7; | |
258 | } | |
259 | ||
87f56d01 PM |
260 | /* See http://openhantek.sourceforge.net/doc/namespaceHantek.html#ac1cd181814cf3da74771c29800b39028 */ |
261 | static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi) | |
262 | { | |
263 | struct dev_context *devc; | |
264 | struct sr_usb_dev_inst *usb; | |
265 | int ret, tmp; | |
6deb361b | 266 | uint64_t base; |
87f56d01 | 267 | uint8_t cmdstring[12]; |
95983cc3 | 268 | int trig; |
87f56d01 | 269 | |
87f56d01 PM |
270 | devc = sdi->priv; |
271 | usb = sdi->conn; | |
272 | ||
273 | memset(cmdstring, 0, sizeof(cmdstring)); | |
274 | /* Command */ | |
275 | cmdstring[0] = CMD_2250_SET_TRIGGERSOURCE; | |
276 | sr_dbg("Trigger source %s.", devc->triggersource); | |
277 | if (!strcmp("CH2", devc->triggersource)) | |
278 | tmp = 3; | |
279 | else if (!strcmp("CH1", devc->triggersource)) | |
280 | tmp = 2; | |
281 | else if (!strcmp("EXT", devc->triggersource)) | |
282 | tmp = 0; | |
283 | else { | |
284 | sr_err("Invalid trigger source: '%s'.", devc->triggersource); | |
285 | return SR_ERR_ARG; | |
286 | } | |
287 | cmdstring[2] = tmp; | |
288 | ||
87f56d01 PM |
289 | sr_dbg("Trigger slope: %d.", devc->triggerslope); |
290 | cmdstring[2] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; | |
291 | ||
292 | if (send_begin(sdi) != SR_OK) | |
293 | return SR_ERR; | |
294 | ||
295 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
296 | cmdstring, 8, &tmp, 100)) != 0) { | |
297 | sr_err("Failed to set trigger/samplerate: %s.", | |
298 | libusb_error_name(ret)); | |
299 | return SR_ERR; | |
300 | } | |
301 | ||
87f56d01 PM |
302 | /* Frame size */ |
303 | sr_dbg("Frame size: %d.", devc->framesize); | |
304 | cmdstring[0] = CMD_2250_SET_RECORD_LENGTH; | |
ab8df2b1 | 305 | cmdstring[2] = (devc->framesize == FRAMESIZE_SMALL) ? 0x01 : 0x02; |
87f56d01 PM |
306 | |
307 | if (send_begin(sdi) != SR_OK) | |
308 | return SR_ERR; | |
309 | ||
310 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
311 | cmdstring, 4, &tmp, 100)) != 0) { | |
312 | sr_err("Failed to set record length: %s.", | |
313 | libusb_error_name(ret)); | |
314 | return SR_ERR; | |
315 | } | |
316 | ||
87f56d01 PM |
317 | memset(cmdstring, 0, sizeof(cmdstring)); |
318 | cmdstring[0] = CMD_2250_SET_SAMPLERATE; | |
87f56d01 | 319 | base = 100e6; |
11e33196 PM |
320 | if (devc->samplerate > base) { |
321 | /* Timebase fast */ | |
322 | sr_err("Sample rate > 100MHz not yet supported."); | |
323 | return SR_ERR_ARG; | |
87f56d01 PM |
324 | } |
325 | ||
11e33196 | 326 | tmp = base / devc->samplerate; |
11e33196 PM |
327 | if (tmp) { |
328 | /* Downsampling on */ | |
329 | cmdstring[2] |= 2; | |
330 | /* Downsampler = 1comp((Base / Samplerate) - 2) | |
331 | * Base == 100Msa resp. 200MSa | |
332 | * | |
333 | * Example for 500kSa/s: | |
334 | * 100e6 / 500e3 => 200 | |
335 | * 200 - 2 => 198 | |
336 | * 1comp(198) => ff39 */ | |
337 | tmp -= 2; | |
338 | tmp = ~tmp; | |
ab8df2b1 | 339 | sr_dbg("Down sampler value: 0x%x.", tmp & 0xffff); |
11e33196 PM |
340 | cmdstring[4] = (tmp >> 0) & 0xff; |
341 | cmdstring[5] = (tmp >> 8) & 0xff; | |
342 | } | |
87f56d01 PM |
343 | |
344 | if (send_begin(sdi) != SR_OK) | |
345 | return SR_ERR; | |
346 | ||
347 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
348 | cmdstring, 8, &tmp, 100)) != 0) { | |
349 | sr_err("Failed to set sample rate: %s.", | |
350 | libusb_error_name(ret)); | |
351 | return SR_ERR; | |
352 | } | |
353 | sr_dbg("Sent CMD_2250_SET_SAMPLERATE."); | |
354 | ||
ab8df2b1 | 355 | /* Enabled channels: 00=CH1, 01=CH2, 10=both. */ |
87f56d01 PM |
356 | memset(cmdstring, 0, sizeof(cmdstring)); |
357 | cmdstring[0] = CMD_2250_SET_CHANNELS; | |
ab8df2b1 | 358 | sr_dbg("Channels: CH1=%d, CH2=%d.", devc->ch_enabled[0], devc->ch_enabled[1]); |
87f56d01 PM |
359 | cmdstring[2] = (devc->ch_enabled[0] ? 0 : 1) + (devc->ch_enabled[1] ? 2 : 0); |
360 | ||
361 | if (send_begin(sdi) != SR_OK) | |
362 | return SR_ERR; | |
363 | ||
364 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
365 | cmdstring, 4, &tmp, 100)) != 0) { | |
366 | sr_err("Failed to set channels: %s.", | |
367 | libusb_error_name(ret)); | |
368 | return SR_ERR; | |
369 | } | |
370 | sr_dbg("Sent CMD_2250_SET_CHANNELS."); | |
371 | ||
ab8df2b1 | 372 | /* Trigger slope: 0=positive, 1=negative. */ |
87f56d01 PM |
373 | memset(cmdstring, 0, sizeof(cmdstring)); |
374 | cmdstring[0] = CMD_2250_SET_TRIGGERPOS_AND_BUFFER; | |
375 | ||
d3f14af7 | 376 | sr_dbg("Capture ratio: %" PRIu64 ".", devc->capture_ratio); |
95983cc3 PM |
377 | trig = devc->capture_ratio; |
378 | dso2250_set_triggerpos(trig, | |
379 | devc->framesize != FRAMESIZE_SMALL, cmdstring, 2); | |
380 | dso2250_set_triggerpos(100 - trig, | |
381 | devc->framesize != FRAMESIZE_SMALL, cmdstring, 6); | |
87f56d01 PM |
382 | |
383 | if (send_begin(sdi) != SR_OK) | |
384 | return SR_ERR; | |
385 | ||
386 | /* TODO: 12 bytes according to documentation? */ | |
387 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, | |
388 | cmdstring, 10, &tmp, 100)) != 0) { | |
389 | sr_err("Failed to set trigger position: %s.", | |
390 | libusb_error_name(ret)); | |
391 | return SR_ERR; | |
392 | } | |
393 | ||
394 | return SR_OK; | |
395 | } | |
396 | ||
be702d16 | 397 | SR_PRIV int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi) |
3b533202 | 398 | { |
c118080b BV |
399 | struct dev_context *devc; |
400 | struct sr_usb_dev_inst *usb; | |
3b533202 BV |
401 | int ret, tmp; |
402 | uint8_t cmdstring[12]; | |
3b533202 | 403 | uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce, |
e98b7f1b | 404 | 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 }; |
3b533202 | 405 | uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, |
e98b7f1b | 406 | 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; |
3b533202 | 407 | |
87f56d01 PM |
408 | devc = sdi->priv; |
409 | if (devc->profile->fw_pid == 0x2250) | |
410 | return dso2250_set_trigger_samplerate(sdi); | |
411 | ||
e98b7f1b | 412 | sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE."); |
3b533202 | 413 | |
c118080b BV |
414 | usb = sdi->conn; |
415 | ||
3b533202 BV |
416 | memset(cmdstring, 0, sizeof(cmdstring)); |
417 | /* Command */ | |
418 | cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; | |
419 | ||
420 | /* Trigger source */ | |
e98b7f1b | 421 | sr_dbg("Trigger source %s.", devc->triggersource); |
269971dd | 422 | if (!strcmp("CH2", devc->triggersource)) |
a370ef19 | 423 | tmp = 0; |
269971dd | 424 | else if (!strcmp("CH1", devc->triggersource)) |
a370ef19 | 425 | tmp = 1; |
269971dd | 426 | else if (!strcmp("EXT", devc->triggersource)) |
a370ef19 BV |
427 | tmp = 2; |
428 | else { | |
e98b7f1b | 429 | sr_err("Invalid trigger source: '%s'.", devc->triggersource); |
a370ef19 BV |
430 | return SR_ERR_ARG; |
431 | } | |
432 | cmdstring[2] = tmp; | |
3b533202 BV |
433 | |
434 | /* Frame size */ | |
e98b7f1b | 435 | sr_dbg("Frame size: %d.", devc->framesize); |
269971dd | 436 | cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; |
bc79e906 BV |
437 | |
438 | /* Timebase fast */ | |
e98b7f1b | 439 | sr_dbg("Time base index: %d.", devc->timebase); |
034accb5 | 440 | if (devc->framesize == FRAMESIZE_SMALL) { |
269971dd | 441 | if (devc->timebase < TIME_20us) |
bc79e906 | 442 | tmp = 0; |
269971dd | 443 | else if (devc->timebase == TIME_20us) |
bc79e906 | 444 | tmp = 1; |
269971dd | 445 | else if (devc->timebase == TIME_40us) |
bc79e906 | 446 | tmp = 2; |
269971dd | 447 | else if (devc->timebase == TIME_100us) |
bc79e906 | 448 | tmp = 3; |
269971dd | 449 | else if (devc->timebase >= TIME_200us) |
bc79e906 | 450 | tmp = 4; |
034accb5 | 451 | } else { |
269971dd | 452 | if (devc->timebase < TIME_40us) { |
e98b7f1b | 453 | sr_err("Timebase < 40us only supported with 10K buffer."); |
bc79e906 BV |
454 | return SR_ERR_ARG; |
455 | } | |
269971dd | 456 | else if (devc->timebase == TIME_40us) |
bc79e906 | 457 | tmp = 0; |
269971dd | 458 | else if (devc->timebase == TIME_100us) |
bc79e906 | 459 | tmp = 2; |
269971dd | 460 | else if (devc->timebase == TIME_200us) |
bc79e906 | 461 | tmp = 3; |
269971dd | 462 | else if (devc->timebase >= TIME_400us) |
bc79e906 | 463 | tmp = 4; |
3b533202 | 464 | } |
bc79e906 BV |
465 | cmdstring[2] |= (tmp & 0x07) << 5; |
466 | ||
467 | /* Enabled channels: 00=CH1 01=CH2 10=both */ | |
417412c8 AJ |
468 | sr_dbg("Channels CH1=%d CH2=%d", devc->ch_enabled[0], devc->ch_enabled[1]); |
469 | tmp = (((devc->ch_enabled[1] ? 1 : 0) << 1) + (devc->ch_enabled[0] ? 1 : 0)) - 1; | |
6e71ef3b | 470 | cmdstring[3] = tmp; |
3b533202 | 471 | |
bc79e906 | 472 | /* Fast rates channel */ |
e98b7f1b | 473 | /* TODO: Is this right? */ |
269971dd | 474 | tmp = devc->timebase < TIME_10us ? 1 : 0; |
bc79e906 | 475 | cmdstring[3] |= tmp << 2; |
3b533202 | 476 | |
bc79e906 | 477 | /* Trigger slope: 0=positive 1=negative */ |
e98b7f1b UH |
478 | /* TODO: Does this work? */ |
479 | sr_dbg("Trigger slope: %d.", devc->triggerslope); | |
269971dd | 480 | cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; |
3b533202 | 481 | |
a370ef19 | 482 | /* Timebase slow */ |
269971dd | 483 | if (devc->timebase < TIME_100us) |
3b533202 | 484 | tmp = 0; |
269971dd | 485 | else if (devc->timebase > TIME_400ms) |
3b533202 BV |
486 | tmp = 0xffed; |
487 | else { | |
269971dd BV |
488 | if (devc->framesize == FRAMESIZE_SMALL) |
489 | tmp = timebase_small[devc->timebase - 3]; | |
3b533202 | 490 | else |
269971dd | 491 | tmp = timebase_large[devc->timebase - 3]; |
3b533202 | 492 | } |
bc79e906 BV |
493 | cmdstring[4] = tmp & 0xff; |
494 | cmdstring[5] = (tmp >> 8) & 0xff; | |
495 | ||
496 | /* Horizontal trigger position */ | |
d3f14af7 | 497 | sr_dbg("Capture ratio: %" PRIu64 ".", devc->capture_ratio); |
95983cc3 | 498 | tmp = 0x77fff + 0x8000 * devc->capture_ratio / 100; |
3b533202 BV |
499 | cmdstring[6] = tmp & 0xff; |
500 | cmdstring[7] = (tmp >> 8) & 0xff; | |
3b533202 BV |
501 | cmdstring[10] = (tmp >> 16) & 0xff; |
502 | ||
c118080b | 503 | if (send_begin(sdi) != SR_OK) |
3b533202 BV |
504 | return SR_ERR; |
505 | ||
c118080b BV |
506 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, |
507 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
d4928d71 PS |
508 | sr_err("Failed to set trigger/samplerate: %s.", |
509 | libusb_error_name(ret)); | |
3b533202 BV |
510 | return SR_ERR; |
511 | } | |
e98b7f1b | 512 | sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE."); |
3b533202 BV |
513 | |
514 | return SR_OK; | |
515 | } | |
516 | ||
d87c1766 | 517 | static int dso_set_filters(const struct sr_dev_inst *sdi) |
3b533202 | 518 | { |
c118080b BV |
519 | struct dev_context *devc; |
520 | struct sr_usb_dev_inst *usb; | |
3b533202 BV |
521 | int ret, tmp; |
522 | uint8_t cmdstring[8]; | |
523 | ||
e98b7f1b | 524 | sr_dbg("Preparing CMD_SET_FILTERS."); |
3b533202 | 525 | |
c118080b BV |
526 | devc = sdi->priv; |
527 | usb = sdi->conn; | |
528 | ||
3b533202 BV |
529 | memset(cmdstring, 0, sizeof(cmdstring)); |
530 | cmdstring[0] = CMD_SET_FILTERS; | |
531 | cmdstring[1] = 0x0f; | |
933defaa | 532 | if (devc->filter[0]) { |
e98b7f1b | 533 | sr_dbg("Turning on CH1 filter."); |
3b533202 | 534 | cmdstring[2] |= 0x80; |
ebb781a6 | 535 | } |
933defaa | 536 | if (devc->filter[1]) { |
e98b7f1b | 537 | sr_dbg("Turning on CH2 filter."); |
3b533202 | 538 | cmdstring[2] |= 0x40; |
ebb781a6 | 539 | } |
933defaa BV |
540 | /* |
541 | * Not supported: filtering on the trigger | |
542 | * cmdstring[2] |= 0x20; | |
543 | */ | |
3b533202 | 544 | |
c118080b | 545 | if (send_begin(sdi) != SR_OK) |
3b533202 BV |
546 | return SR_ERR; |
547 | ||
c118080b BV |
548 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, |
549 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
1740429d | 550 | sr_err("Failed to set filters: %s.", libusb_error_name(ret)); |
3b533202 BV |
551 | return SR_ERR; |
552 | } | |
e98b7f1b | 553 | sr_dbg("Sent CMD_SET_FILTERS."); |
3b533202 BV |
554 | |
555 | return SR_OK; | |
556 | } | |
557 | ||
d87c1766 | 558 | static int dso_set_voltage(const struct sr_dev_inst *sdi) |
3b533202 | 559 | { |
c118080b BV |
560 | struct dev_context *devc; |
561 | struct sr_usb_dev_inst *usb; | |
3b533202 BV |
562 | int ret, tmp; |
563 | uint8_t cmdstring[8]; | |
564 | ||
e98b7f1b | 565 | sr_dbg("Preparing CMD_SET_VOLTAGE."); |
3b533202 | 566 | |
c118080b BV |
567 | devc = sdi->priv; |
568 | usb = sdi->conn; | |
569 | ||
3b533202 BV |
570 | memset(cmdstring, 0, sizeof(cmdstring)); |
571 | cmdstring[0] = CMD_SET_VOLTAGE; | |
87f56d01 PM |
572 | |
573 | if (devc->profile->fw_pid == 0x2250) { | |
ab8df2b1 | 574 | cmdstring[1] = 0x00; |
87f56d01 PM |
575 | cmdstring[2] = 0x08; |
576 | } else { | |
577 | cmdstring[1] = 0x0f; | |
578 | cmdstring[2] = 0x30; | |
579 | } | |
313deed2 | 580 | |
ab8df2b1 | 581 | /* CH1 volts/div is encoded in bits 0-1. */ |
933defaa BV |
582 | sr_dbg("CH1 vdiv index: %d.", devc->voltage[0]); |
583 | switch (devc->voltage[0]) { | |
313deed2 BV |
584 | case VDIV_1V: |
585 | case VDIV_100MV: | |
586 | case VDIV_10MV: | |
587 | cmdstring[2] |= 0x00; | |
588 | break; | |
589 | case VDIV_2V: | |
590 | case VDIV_200MV: | |
591 | case VDIV_20MV: | |
592 | cmdstring[2] |= 0x01; | |
593 | break; | |
594 | case VDIV_5V: | |
595 | case VDIV_500MV: | |
596 | case VDIV_50MV: | |
597 | cmdstring[2] |= 0x02; | |
598 | break; | |
599 | } | |
600 | ||
ab8df2b1 | 601 | /* CH2 volts/div is encoded in bits 2-3. */ |
933defaa BV |
602 | sr_dbg("CH2 vdiv index: %d.", devc->voltage[1]); |
603 | switch (devc->voltage[1]) { | |
313deed2 BV |
604 | case VDIV_1V: |
605 | case VDIV_100MV: | |
606 | case VDIV_10MV: | |
607 | cmdstring[2] |= 0x00; | |
608 | break; | |
609 | case VDIV_2V: | |
610 | case VDIV_200MV: | |
611 | case VDIV_20MV: | |
1d97091e | 612 | cmdstring[2] |= 0x04; |
313deed2 BV |
613 | break; |
614 | case VDIV_5V: | |
615 | case VDIV_500MV: | |
616 | case VDIV_50MV: | |
1d97091e | 617 | cmdstring[2] |= 0x08; |
313deed2 BV |
618 | break; |
619 | } | |
3b533202 | 620 | |
c118080b | 621 | if (send_begin(sdi) != SR_OK) |
3b533202 BV |
622 | return SR_ERR; |
623 | ||
c118080b BV |
624 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, |
625 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
1740429d | 626 | sr_err("Failed to set voltage: %s.", libusb_error_name(ret)); |
3b533202 BV |
627 | return SR_ERR; |
628 | } | |
e98b7f1b | 629 | sr_dbg("Sent CMD_SET_VOLTAGE."); |
3b533202 BV |
630 | |
631 | return SR_OK; | |
632 | } | |
633 | ||
d87c1766 | 634 | static int dso_set_relays(const struct sr_dev_inst *sdi) |
3b533202 | 635 | { |
c118080b BV |
636 | struct dev_context *devc; |
637 | struct sr_usb_dev_inst *usb; | |
a217bcdf BV |
638 | GString *gs; |
639 | int ret, i; | |
640 | uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01, | |
3b533202 BV |
641 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
642 | ||
e98b7f1b | 643 | sr_dbg("Preparing CTRL_SETRELAYS."); |
3b533202 | 644 | |
c118080b BV |
645 | devc = sdi->priv; |
646 | usb = sdi->conn; | |
647 | ||
933defaa | 648 | if (devc->voltage[0] < VDIV_1V) |
3b533202 BV |
649 | relays[1] = ~relays[1]; |
650 | ||
933defaa | 651 | if (devc->voltage[0] < VDIV_100MV) |
3b533202 BV |
652 | relays[2] = ~relays[2]; |
653 | ||
933defaa BV |
654 | sr_dbg("CH1 coupling: %d.", devc->coupling[0]); |
655 | if (devc->coupling[0] != COUPLING_AC) | |
3b533202 BV |
656 | relays[3] = ~relays[3]; |
657 | ||
933defaa | 658 | if (devc->voltage[1] < VDIV_1V) |
3b533202 BV |
659 | relays[4] = ~relays[4]; |
660 | ||
933defaa | 661 | if (devc->voltage[1] < VDIV_100MV) |
3b533202 BV |
662 | relays[5] = ~relays[5]; |
663 | ||
933defaa BV |
664 | sr_dbg("CH2 coupling: %d.", devc->coupling[1]); |
665 | if (devc->coupling[1] != COUPLING_AC) | |
3b533202 BV |
666 | relays[6] = ~relays[6]; |
667 | ||
269971dd | 668 | if (!strcmp(devc->triggersource, "EXT")) |
3b533202 BV |
669 | relays[7] = ~relays[7]; |
670 | ||
a217bcdf BV |
671 | if (sr_log_loglevel_get() >= SR_LOG_DBG) { |
672 | gs = g_string_sized_new(128); | |
e98b7f1b | 673 | g_string_printf(gs, "Relays:"); |
a217bcdf BV |
674 | for (i = 0; i < 17; i++) |
675 | g_string_append_printf(gs, " %.2x", relays[i]); | |
cbc6f3b2 | 676 | sr_dbg("%s", gs->str); |
a217bcdf BV |
677 | g_string_free(gs, TRUE); |
678 | } | |
679 | ||
c118080b | 680 | if ((ret = libusb_control_transfer(usb->devhdl, |
3b533202 | 681 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS, |
a217bcdf | 682 | 0, 0, relays, 17, 100)) != sizeof(relays)) { |
1740429d | 683 | sr_err("Failed to set relays: %s.", libusb_error_name(ret)); |
3b533202 BV |
684 | return SR_ERR; |
685 | } | |
e98b7f1b | 686 | sr_dbg("Sent CTRL_SETRELAYS."); |
3b533202 BV |
687 | |
688 | return SR_OK; | |
689 | } | |
690 | ||
be702d16 | 691 | SR_PRIV int dso_set_voffsets(const struct sr_dev_inst *sdi) |
3b533202 | 692 | { |
c118080b BV |
693 | struct dev_context *devc; |
694 | struct sr_usb_dev_inst *usb; | |
3b533202 BV |
695 | int offset, ret; |
696 | uint16_t *ch_levels; | |
2715c0b8 BV |
697 | uint8_t offsets[17]; |
698 | ||
e98b7f1b | 699 | sr_dbg("Preparing CTRL_SETOFFSET."); |
2715c0b8 | 700 | |
c118080b BV |
701 | devc = sdi->priv; |
702 | usb = sdi->conn; | |
703 | ||
2715c0b8 BV |
704 | memset(offsets, 0, sizeof(offsets)); |
705 | /* Channel 1 */ | |
933defaa | 706 | ch_levels = devc->channel_levels[0][devc->voltage[0]]; |
269971dd | 707 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0]; |
2715c0b8 BV |
708 | offsets[0] = (offset >> 8) | 0x20; |
709 | offsets[1] = offset & 0xff; | |
e98b7f1b UH |
710 | sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1, |
711 | offsets[0], offsets[1]); | |
2715c0b8 BV |
712 | |
713 | /* Channel 2 */ | |
933defaa | 714 | ch_levels = devc->channel_levels[1][devc->voltage[1]]; |
269971dd | 715 | offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0]; |
2715c0b8 BV |
716 | offsets[2] = (offset >> 8) | 0x20; |
717 | offsets[3] = offset & 0xff; | |
e98b7f1b UH |
718 | sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2, |
719 | offsets[2], offsets[3]); | |
2715c0b8 BV |
720 | |
721 | /* Trigger */ | |
269971dd | 722 | offset = MAX_VERT_TRIGGER * devc->voffset_trigger; |
2715c0b8 BV |
723 | offsets[4] = (offset >> 8) | 0x20; |
724 | offsets[5] = offset & 0xff; | |
e98b7f1b | 725 | sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger, |
2715c0b8 | 726 | offsets[4], offsets[5]); |
3b533202 | 727 | |
c118080b | 728 | if ((ret = libusb_control_transfer(usb->devhdl, |
3b533202 BV |
729 | LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET, |
730 | 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) { | |
1740429d | 731 | sr_err("Failed to set offsets: %s.", libusb_error_name(ret)); |
3b533202 BV |
732 | return SR_ERR; |
733 | } | |
e98b7f1b | 734 | sr_dbg("Sent CTRL_SETOFFSET."); |
3b533202 BV |
735 | |
736 | return SR_OK; | |
737 | } | |
738 | ||
c118080b | 739 | SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi) |
3b533202 | 740 | { |
c118080b | 741 | struct sr_usb_dev_inst *usb; |
3b533202 BV |
742 | int ret, tmp; |
743 | uint8_t cmdstring[2]; | |
744 | ||
e98b7f1b | 745 | sr_dbg("Sending CMD_ENABLE_TRIGGER."); |
3b533202 | 746 | |
c118080b BV |
747 | usb = sdi->conn; |
748 | ||
3b533202 BV |
749 | memset(cmdstring, 0, sizeof(cmdstring)); |
750 | cmdstring[0] = CMD_ENABLE_TRIGGER; | |
751 | cmdstring[1] = 0x00; | |
752 | ||
c118080b | 753 | if (send_begin(sdi) != SR_OK) |
3b533202 BV |
754 | return SR_ERR; |
755 | ||
c118080b BV |
756 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, |
757 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
1740429d | 758 | sr_err("Failed to enable trigger: %s.", libusb_error_name(ret)); |
3b533202 BV |
759 | return SR_ERR; |
760 | } | |
761 | ||
762 | return SR_OK; | |
763 | } | |
764 | ||
c118080b | 765 | SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi) |
3b533202 | 766 | { |
c118080b | 767 | struct sr_usb_dev_inst *usb; |
3b533202 BV |
768 | int ret, tmp; |
769 | uint8_t cmdstring[2]; | |
770 | ||
e98b7f1b | 771 | sr_dbg("Sending CMD_FORCE_TRIGGER."); |
3b533202 | 772 | |
c118080b BV |
773 | usb = sdi->conn; |
774 | ||
3b533202 BV |
775 | memset(cmdstring, 0, sizeof(cmdstring)); |
776 | cmdstring[0] = CMD_FORCE_TRIGGER; | |
777 | cmdstring[1] = 0x00; | |
778 | ||
c118080b | 779 | if (send_begin(sdi) != SR_OK) |
3b533202 BV |
780 | return SR_ERR; |
781 | ||
c118080b BV |
782 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, |
783 | cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) { | |
1740429d | 784 | sr_err("Failed to force trigger: %s.", libusb_error_name(ret)); |
3b533202 BV |
785 | return SR_ERR; |
786 | } | |
787 | ||
788 | return SR_OK; | |
789 | } | |
790 | ||
c118080b | 791 | SR_PRIV int dso_init(const struct sr_dev_inst *sdi) |
3b533202 | 792 | { |
e98b7f1b | 793 | sr_dbg("Initializing DSO."); |
3b533202 | 794 | |
c118080b | 795 | if (get_channel_offsets(sdi) != SR_OK) |
3b533202 BV |
796 | return SR_ERR; |
797 | ||
c118080b | 798 | if (dso_set_trigger_samplerate(sdi) != SR_OK) |
3b533202 BV |
799 | return SR_ERR; |
800 | ||
c118080b | 801 | if (dso_set_filters(sdi) != SR_OK) |
3b533202 BV |
802 | return SR_ERR; |
803 | ||
c118080b | 804 | if (dso_set_voltage(sdi) != SR_OK) |
3b533202 BV |
805 | return SR_ERR; |
806 | ||
c118080b | 807 | if (dso_set_relays(sdi) != SR_OK) |
3b533202 BV |
808 | return SR_ERR; |
809 | ||
c118080b | 810 | if (dso_set_voffsets(sdi) != SR_OK) |
3b533202 BV |
811 | return SR_ERR; |
812 | ||
c118080b | 813 | if (dso_enable_trigger(sdi) != SR_OK) |
3b533202 BV |
814 | return SR_ERR; |
815 | ||
3b533202 BV |
816 | return SR_OK; |
817 | } | |
818 | ||
c118080b BV |
819 | SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi, |
820 | uint8_t *capturestate, uint32_t *trigger_offset) | |
3b533202 | 821 | { |
c118080b | 822 | struct sr_usb_dev_inst *usb; |
e05a174b BV |
823 | int ret, tmp, i; |
824 | unsigned int bitvalue, toff; | |
3b533202 BV |
825 | uint8_t cmdstring[2], inbuf[512]; |
826 | ||
e98b7f1b | 827 | sr_dbg("Sending CMD_GET_CAPTURESTATE."); |
3b533202 | 828 | |
c118080b BV |
829 | usb = sdi->conn; |
830 | ||
3b533202 BV |
831 | cmdstring[0] = CMD_GET_CAPTURESTATE; |
832 | cmdstring[1] = 0; | |
833 | ||
c118080b | 834 | if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) { |
d4928d71 PS |
835 | sr_dbg("Failed to send get_capturestate command: %s.", |
836 | libusb_error_name(ret)); | |
6e6eeff4 | 837 | return SR_ERR; |
3b533202 BV |
838 | } |
839 | ||
c118080b | 840 | if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN, |
3b533202 | 841 | inbuf, 512, &tmp, 100)) != 0) { |
d4928d71 PS |
842 | sr_dbg("Failed to get capturestate: %s.", |
843 | libusb_error_name(ret)); | |
6e6eeff4 | 844 | return SR_ERR; |
3b533202 | 845 | } |
6e6eeff4 | 846 | *capturestate = inbuf[0]; |
e05a174b BV |
847 | toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2]; |
848 | ||
e98b7f1b UH |
849 | /* |
850 | * This conversion comes from the openhantek project. | |
e05a174b BV |
851 | * Each set bit in the 24-bit value inverts all bits with a lower |
852 | * value. No idea why the device reports the trigger point this way. | |
853 | */ | |
854 | bitvalue = 1; | |
855 | for (i = 0; i < 24; i++) { | |
e749a8cb | 856 | /* Each set bit inverts all bits with a lower value. */ |
0c5f2abc | 857 | if (toff & bitvalue) |
e05a174b BV |
858 | toff ^= bitvalue - 1; |
859 | bitvalue <<= 1; | |
860 | } | |
861 | *trigger_offset = toff; | |
3b533202 | 862 | |
6e6eeff4 | 863 | return SR_OK; |
3b533202 BV |
864 | } |
865 | ||
c118080b | 866 | SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi) |
3b533202 BV |
867 | { |
868 | int ret; | |
869 | uint8_t cmdstring[2]; | |
870 | ||
e98b7f1b | 871 | sr_dbg("Sending CMD_CAPTURE_START."); |
3b533202 BV |
872 | |
873 | cmdstring[0] = CMD_CAPTURE_START; | |
874 | cmdstring[1] = 0; | |
875 | ||
c118080b | 876 | if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) { |
d4928d71 PS |
877 | sr_err("Failed to send capture_start command: %s.", |
878 | libusb_error_name(ret)); | |
3b533202 BV |
879 | return SR_ERR; |
880 | } | |
881 | ||
882 | return SR_OK; | |
883 | } | |
884 | ||
69e19dd7 BV |
885 | SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi, |
886 | libusb_transfer_cb_fn cb) | |
3b533202 | 887 | { |
69e19dd7 | 888 | struct dev_context *devc; |
c118080b | 889 | struct sr_usb_dev_inst *usb; |
3b533202 BV |
890 | struct libusb_transfer *transfer; |
891 | int num_transfers, ret, i; | |
892 | uint8_t cmdstring[2]; | |
893 | unsigned char *buf; | |
894 | ||
e98b7f1b | 895 | sr_dbg("Sending CMD_GET_CHANNELDATA."); |
3b533202 | 896 | |
c118080b BV |
897 | devc = sdi->priv; |
898 | usb = sdi->conn; | |
899 | ||
3b533202 BV |
900 | cmdstring[0] = CMD_GET_CHANNELDATA; |
901 | cmdstring[1] = 0; | |
902 | ||
c118080b | 903 | if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) { |
d4928d71 PS |
904 | sr_err("Failed to get channel data: %s.", |
905 | libusb_error_name(ret)); | |
3b533202 BV |
906 | return SR_ERR; |
907 | } | |
908 | ||
e98b7f1b UH |
909 | /* TODO: DSO-2xxx only. */ |
910 | num_transfers = devc->framesize * | |
911 | sizeof(unsigned short) / devc->epin_maxpacketsize; | |
912 | sr_dbg("Queueing up %d transfers.", num_transfers); | |
3b533202 | 913 | for (i = 0; i < num_transfers; i++) { |
269971dd | 914 | if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) { |
e98b7f1b | 915 | sr_err("Failed to malloc USB endpoint buffer."); |
3b533202 BV |
916 | return SR_ERR_MALLOC; |
917 | } | |
918 | transfer = libusb_alloc_transfer(0); | |
c118080b | 919 | libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf, |
69e19dd7 | 920 | devc->epin_maxpacketsize, cb, (void *)sdi, 40); |
3b533202 | 921 | if ((ret = libusb_submit_transfer(transfer)) != 0) { |
d4928d71 PS |
922 | sr_err("Failed to submit transfer: %s.", |
923 | libusb_error_name(ret)); | |
3b533202 BV |
924 | /* TODO: Free them all. */ |
925 | libusb_free_transfer(transfer); | |
926 | g_free(buf); | |
927 | return SR_ERR; | |
928 | } | |
929 | } | |
930 | ||
931 | return SR_OK; | |
932 | } |