]>
Commit | Line | Data |
---|---|---|
adcb9951 JH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "protocol.h" | |
23 | #include "dslogic.h" | |
24 | #include <math.h> | |
25 | ||
26 | static const struct dslogic_profile supported_device[] = { | |
6c317a8d | 27 | /* DreamSourceLab DSLogic */ |
adcb9951 JH |
28 | { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL, |
29 | "dreamsourcelab-dslogic-fx2.fw", | |
30 | 0, "DreamSourceLab", "DSLogic"}, | |
6c317a8d | 31 | /* DreamSourceLab DSCope */ |
adcb9951 JH |
32 | { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL, |
33 | "dreamsourcelab-dscope-fx2.fw", | |
34 | 0, "DreamSourceLab", "DSCope"}, | |
6c317a8d | 35 | /* DreamSourceLab DSLogic Pro */ |
adcb9951 JH |
36 | { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL, |
37 | "dreamsourcelab-dslogic-pro-fx2.fw", | |
38 | 0, "DreamSourceLab", "DSLogic"}, | |
6c317a8d | 39 | /* DreamSourceLab DSLogic Plus */ |
adcb9951 JH |
40 | { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL, |
41 | "dreamsourcelab-dslogic-plus-fx2.fw", | |
42 | 0, "DreamSourceLab", "DSLogic"}, | |
6c317a8d | 43 | /* DreamSourceLab DSLogic Basic */ |
adcb9951 JH |
44 | { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL, |
45 | "dreamsourcelab-dslogic-basic-fx2.fw", | |
46 | 0, "DreamSourceLab", "DSLogic"}, | |
47 | ||
48 | ALL_ZERO | |
49 | }; | |
50 | ||
51 | static const uint32_t drvopts[] = { | |
52 | SR_CONF_LOGIC_ANALYZER, | |
53 | }; | |
54 | ||
55 | static const uint32_t scanopts[] = { | |
56 | SR_CONF_CONN, | |
57 | }; | |
58 | ||
59 | static const uint32_t devopts[] = { | |
60 | SR_CONF_CONTINUOUS | SR_CONF_SET | SR_CONF_GET, | |
61 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
62 | SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
63 | SR_CONF_CONN | SR_CONF_GET, | |
64 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
65 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
66 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
67 | SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET, | |
68 | SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
69 | }; | |
70 | ||
71 | /* Names assigned to available edge slope choices. */ | |
72 | static const char *const signal_edge_names[] = { | |
73 | [DS_EDGE_RISING] = "rising", | |
74 | [DS_EDGE_FALLING] = "falling", | |
75 | }; | |
76 | ||
77 | static const struct { | |
adcb9951 JH |
78 | gdouble low; |
79 | gdouble high; | |
1ee70746 JH |
80 | } dslogic_voltage_thresholds[] = { |
81 | { 0.7, 1.4 }, | |
82 | { 1.4, 3.6 }, | |
adcb9951 JH |
83 | }; |
84 | ||
85 | static const uint64_t samplerates[] = { | |
86 | SR_KHZ(10), | |
87 | SR_KHZ(20), | |
88 | SR_KHZ(50), | |
89 | SR_KHZ(100), | |
90 | SR_KHZ(200), | |
91 | SR_KHZ(500), | |
92 | SR_MHZ(1), | |
93 | SR_MHZ(2), | |
94 | SR_MHZ(5), | |
95 | SR_MHZ(10), | |
96 | SR_MHZ(20), | |
97 | SR_MHZ(25), | |
98 | SR_MHZ(50), | |
99 | SR_MHZ(100), | |
100 | SR_MHZ(200), | |
101 | SR_MHZ(400), | |
102 | }; | |
103 | ||
104 | static gboolean is_plausible(const struct libusb_device_descriptor *des) | |
105 | { | |
106 | int i; | |
107 | ||
108 | for (i = 0; supported_device[i].vid; i++) { | |
109 | if (des->idVendor != supported_device[i].vid) | |
110 | continue; | |
111 | if (des->idProduct == supported_device[i].pid) | |
112 | return TRUE; | |
113 | } | |
114 | ||
115 | return FALSE; | |
116 | } | |
117 | ||
118 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
119 | { | |
120 | struct drv_context *drvc; | |
121 | struct dev_context *devc; | |
122 | struct sr_dev_inst *sdi; | |
123 | struct sr_usb_dev_inst *usb; | |
124 | struct sr_channel *ch; | |
125 | struct sr_channel_group *cg; | |
126 | struct sr_config *src; | |
127 | const struct dslogic_profile *prof; | |
128 | GSList *l, *devices, *conn_devices; | |
129 | gboolean has_firmware; | |
130 | struct libusb_device_descriptor des; | |
131 | libusb_device **devlist; | |
132 | struct libusb_device_handle *hdl; | |
133 | int ret, i, j; | |
134 | const char *conn; | |
135 | char manufacturer[64], product[64], serial_num[64], connection_id[64]; | |
136 | char channel_name[16]; | |
137 | ||
138 | drvc = di->context; | |
139 | ||
140 | conn = NULL; | |
141 | for (l = options; l; l = l->next) { | |
142 | src = l->data; | |
143 | switch (src->key) { | |
144 | case SR_CONF_CONN: | |
145 | conn = g_variant_get_string(src->data, NULL); | |
146 | break; | |
147 | } | |
148 | } | |
149 | if (conn) | |
150 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
151 | else | |
152 | conn_devices = NULL; | |
153 | ||
154 | /* Find all dslogic compatible devices and upload firmware to them. */ | |
155 | devices = NULL; | |
156 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
157 | for (i = 0; devlist[i]; i++) { | |
158 | if (conn) { | |
159 | usb = NULL; | |
160 | for (l = conn_devices; l; l = l->next) { | |
161 | usb = l->data; | |
162 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
163 | && usb->address == libusb_get_device_address(devlist[i])) | |
164 | break; | |
165 | } | |
166 | if (!l) | |
167 | /* This device matched none of the ones that | |
168 | * matched the conn specification. */ | |
169 | continue; | |
170 | } | |
171 | ||
172 | libusb_get_device_descriptor( devlist[i], &des); | |
173 | ||
174 | if (!is_plausible(&des)) | |
175 | continue; | |
176 | ||
177 | if ((ret = libusb_open(devlist[i], &hdl)) < 0) { | |
178 | sr_warn("Failed to open potential device with " | |
179 | "VID:PID %04x:%04x: %s.", des.idVendor, | |
180 | des.idProduct, libusb_error_name(ret)); | |
181 | continue; | |
182 | } | |
183 | ||
184 | if (des.iManufacturer == 0) { | |
185 | manufacturer[0] = '\0'; | |
186 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
187 | des.iManufacturer, (unsigned char *) manufacturer, | |
188 | sizeof(manufacturer))) < 0) { | |
189 | sr_warn("Failed to get manufacturer string descriptor: %s.", | |
190 | libusb_error_name(ret)); | |
191 | continue; | |
192 | } | |
193 | ||
194 | if (des.iProduct == 0) { | |
195 | product[0] = '\0'; | |
196 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
197 | des.iProduct, (unsigned char *) product, | |
198 | sizeof(product))) < 0) { | |
199 | sr_warn("Failed to get product string descriptor: %s.", | |
200 | libusb_error_name(ret)); | |
201 | continue; | |
202 | } | |
203 | ||
204 | if (des.iSerialNumber == 0) { | |
205 | serial_num[0] = '\0'; | |
206 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
207 | des.iSerialNumber, (unsigned char *) serial_num, | |
208 | sizeof(serial_num))) < 0) { | |
209 | sr_warn("Failed to get serial number string descriptor: %s.", | |
210 | libusb_error_name(ret)); | |
211 | continue; | |
212 | } | |
213 | ||
214 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
215 | ||
216 | libusb_close(hdl); | |
217 | ||
218 | prof = NULL; | |
219 | for (j = 0; supported_device[j].vid; j++) { | |
220 | if (des.idVendor == supported_device[j].vid && | |
221 | des.idProduct == supported_device[j].pid && | |
6c317a8d JH |
222 | (!strcmp(manufacturer, supported_device[j].usb_manufacturer)) && |
223 | (!strcmp(product, "USB-based Instrument") || | |
224 | !strcmp(product, supported_device[j].usb_product))) { | |
adcb9951 JH |
225 | prof = &supported_device[j]; |
226 | break; | |
227 | } | |
228 | } | |
229 | ||
230 | /* Skip if the device was not found. */ | |
231 | if (!prof) | |
232 | continue; | |
233 | ||
234 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
235 | sdi->status = SR_ST_INITIALIZING; | |
236 | sdi->vendor = g_strdup(prof->vendor); | |
237 | sdi->model = g_strdup(prof->model); | |
238 | sdi->version = g_strdup(prof->model_version); | |
239 | sdi->serial_num = g_strdup(serial_num); | |
240 | sdi->connection_id = g_strdup(connection_id); | |
241 | ||
242 | /* Logic channels, all in one channel group. */ | |
243 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
244 | cg->name = g_strdup("Logic"); | |
245 | for (j = 0; j < 16; j++) { | |
84f6d40a | 246 | sprintf(channel_name, "%d", j); |
adcb9951 JH |
247 | ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, |
248 | TRUE, channel_name); | |
249 | cg->channels = g_slist_append(cg->channels, ch); | |
250 | } | |
251 | sdi->channel_groups = g_slist_append(NULL, cg); | |
252 | ||
253 | devc = dslogic_dev_new(); | |
254 | devc->profile = prof; | |
255 | sdi->priv = devc; | |
256 | devices = g_slist_append(devices, sdi); | |
257 | ||
258 | devc->samplerates = samplerates; | |
259 | devc->num_samplerates = ARRAY_SIZE(samplerates); | |
3c749da1 | 260 | has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument"); |
adcb9951 JH |
261 | |
262 | if (has_firmware) { | |
263 | /* Already has the firmware, so fix the new address. */ | |
264 | sr_dbg("Found an dslogic device."); | |
265 | sdi->status = SR_ST_INACTIVE; | |
266 | sdi->inst_type = SR_INST_USB; | |
267 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
268 | libusb_get_device_address(devlist[i]), NULL); | |
269 | } else { | |
270 | if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i], | |
271 | USB_CONFIGURATION, prof->firmware) == SR_OK) | |
272 | /* Store when this device's FW was updated. */ | |
273 | devc->fw_updated = g_get_monotonic_time(); | |
274 | else | |
275 | sr_err("Firmware upload failed for " | |
276 | "device %d.%d (logical).", | |
277 | libusb_get_bus_number(devlist[i]), | |
278 | libusb_get_device_address(devlist[i])); | |
279 | sdi->inst_type = SR_INST_USB; | |
280 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
281 | 0xff, NULL); | |
282 | } | |
283 | } | |
284 | libusb_free_device_list(devlist, 1); | |
285 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
286 | ||
287 | return std_scan_complete(di, devices); | |
288 | } | |
289 | ||
290 | static void clear_dev_context(void *priv) | |
291 | { | |
292 | struct dev_context *devc; | |
293 | ||
294 | devc = priv; | |
295 | g_free(devc); | |
296 | } | |
297 | ||
298 | static int dev_clear(const struct sr_dev_driver *di) | |
299 | { | |
300 | return std_dev_clear(di, clear_dev_context); | |
301 | } | |
302 | ||
303 | static int dev_open(struct sr_dev_inst *sdi) | |
304 | { | |
305 | struct sr_dev_driver *di = sdi->driver; | |
306 | struct sr_usb_dev_inst *usb; | |
307 | struct dev_context *devc; | |
adcb9951 JH |
308 | int ret; |
309 | int64_t timediff_us, timediff_ms; | |
310 | ||
311 | devc = sdi->priv; | |
312 | usb = sdi->conn; | |
313 | ||
314 | /* | |
315 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
316 | * milliseconds for the FX2 to renumerate. | |
317 | */ | |
318 | ret = SR_ERR; | |
319 | if (devc->fw_updated > 0) { | |
320 | sr_info("Waiting for device to reset."); | |
321 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
322 | g_usleep(300 * 1000); | |
323 | timediff_ms = 0; | |
324 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
325 | if ((ret = dslogic_dev_open(sdi, di)) == SR_OK) | |
326 | break; | |
327 | g_usleep(100 * 1000); | |
328 | ||
329 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
330 | timediff_ms = timediff_us / 1000; | |
331 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
332 | } | |
333 | if (ret != SR_OK) { | |
334 | sr_err("Device failed to renumerate."); | |
335 | return SR_ERR; | |
336 | } | |
337 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
338 | } else { | |
339 | sr_info("Firmware upload was not needed."); | |
340 | ret = dslogic_dev_open(sdi, di); | |
341 | } | |
342 | ||
343 | if (ret != SR_OK) { | |
344 | sr_err("Unable to open device."); | |
345 | return SR_ERR; | |
346 | } | |
347 | ||
348 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
349 | if (ret != 0) { | |
350 | switch (ret) { | |
351 | case LIBUSB_ERROR_BUSY: | |
352 | sr_err("Unable to claim USB interface. Another " | |
353 | "program or driver has already claimed it."); | |
354 | break; | |
355 | case LIBUSB_ERROR_NO_DEVICE: | |
356 | sr_err("Device has been disconnected."); | |
357 | break; | |
358 | default: | |
359 | sr_err("Unable to claim interface: %s.", | |
360 | libusb_error_name(ret)); | |
361 | break; | |
362 | } | |
363 | ||
364 | return SR_ERR; | |
365 | } | |
366 | ||
adcb9951 | 367 | |
3566348b | 368 | if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK) |
adcb9951 JH |
369 | return ret; |
370 | ||
371 | if (devc->cur_samplerate == 0) { | |
372 | /* Samplerate hasn't been set; default to the slowest one. */ | |
373 | devc->cur_samplerate = devc->samplerates[0]; | |
374 | } | |
375 | ||
1ee70746 JH |
376 | if (devc->cur_threshold == 0.0) |
377 | devc->cur_threshold = 1.5; | |
378 | ||
adcb9951 JH |
379 | return SR_OK; |
380 | } | |
381 | ||
382 | static int dev_close(struct sr_dev_inst *sdi) | |
383 | { | |
384 | struct sr_usb_dev_inst *usb; | |
385 | ||
386 | usb = sdi->conn; | |
387 | ||
388 | if (!usb->devhdl) | |
389 | return SR_ERR; | |
390 | ||
391 | sr_info("dslogic: Closing device on %d.%d (logical) / %s (physical) interface %d.", | |
392 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
393 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
394 | libusb_close(usb->devhdl); | |
395 | usb->devhdl = NULL; | |
396 | sdi->status = SR_ST_INACTIVE; | |
397 | ||
398 | return SR_OK; | |
399 | } | |
400 | ||
401 | static int config_get(uint32_t key, GVariant **data, | |
402 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
403 | { | |
404 | struct dev_context *devc; | |
405 | struct sr_usb_dev_inst *usb; | |
406 | GVariant *range[2]; | |
1ee70746 | 407 | unsigned int i, voltage_range; |
adcb9951 JH |
408 | char str[128]; |
409 | ||
410 | (void)cg; | |
411 | ||
412 | if (!sdi) | |
413 | return SR_ERR_ARG; | |
414 | ||
415 | devc = sdi->priv; | |
416 | ||
417 | switch (key) { | |
418 | case SR_CONF_CONN: | |
419 | if (!sdi->conn) | |
420 | return SR_ERR_ARG; | |
421 | usb = sdi->conn; | |
422 | if (usb->address == 255) | |
423 | /* Device still needs to re-enumerate after firmware | |
424 | * upload, so we don't know its (future) address. */ | |
425 | return SR_ERR; | |
426 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
427 | *data = g_variant_new_string(str); | |
428 | break; | |
429 | case SR_CONF_VOLTAGE_THRESHOLD: | |
1ee70746 JH |
430 | if (!strcmp(devc->profile->model, "DSLogic")) { |
431 | voltage_range = 0; | |
432 | ||
433 | for (i = 0; i < ARRAY_SIZE(dslogic_voltage_thresholds); i++) | |
434 | if (dslogic_voltage_thresholds[i].low == | |
435 | devc->cur_threshold) { | |
436 | voltage_range = i; | |
437 | break; | |
438 | } | |
439 | ||
440 | range[0] = g_variant_new_double( | |
441 | dslogic_voltage_thresholds[voltage_range].low); | |
442 | range[1] = g_variant_new_double( | |
443 | dslogic_voltage_thresholds[voltage_range].high); | |
444 | } else { | |
445 | range[0] = g_variant_new_double(devc->cur_threshold); | |
446 | range[1] = g_variant_new_double(devc->cur_threshold); | |
adcb9951 | 447 | } |
1ee70746 | 448 | *data = g_variant_new_tuple(range, 2); |
adcb9951 JH |
449 | break; |
450 | case SR_CONF_LIMIT_SAMPLES: | |
451 | *data = g_variant_new_uint64(devc->limit_samples); | |
452 | break; | |
453 | case SR_CONF_SAMPLERATE: | |
454 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
455 | break; | |
456 | case SR_CONF_CAPTURE_RATIO: | |
457 | *data = g_variant_new_uint64(devc->capture_ratio); | |
458 | break; | |
459 | case SR_CONF_EXTERNAL_CLOCK: | |
460 | *data = g_variant_new_boolean(devc->external_clock); | |
461 | break; | |
462 | case SR_CONF_CONTINUOUS: | |
463 | *data = g_variant_new_boolean(devc->continuous_mode); | |
464 | break; | |
465 | case SR_CONF_CLOCK_EDGE: | |
466 | i = devc->clock_edge; | |
467 | if (i >= ARRAY_SIZE(signal_edge_names)) | |
468 | return SR_ERR_BUG; | |
469 | *data = g_variant_new_string(signal_edge_names[0]); | |
470 | break; | |
471 | default: | |
472 | return SR_ERR_NA; | |
473 | } | |
474 | ||
475 | return SR_OK; | |
476 | } | |
477 | ||
478 | /* | |
479 | * Helper for mapping a string-typed configuration value to an index | |
480 | * within a table of possible values. | |
481 | */ | |
482 | static int lookup_index(GVariant *value, const char *const *table, int len) | |
483 | { | |
484 | const char *entry; | |
485 | int i; | |
486 | ||
487 | entry = g_variant_get_string(value, NULL); | |
488 | if (!entry) | |
489 | return -1; | |
490 | ||
491 | /* Linear search is fine for very small tables. */ | |
492 | for (i = 0; i < len; i++) { | |
493 | if (strcmp(entry, table[i]) == 0) | |
494 | return i; | |
495 | } | |
496 | ||
497 | return -1; | |
498 | } | |
499 | ||
500 | static int config_set(uint32_t key, GVariant *data, | |
501 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
502 | { | |
503 | struct dev_context *devc; | |
504 | uint64_t arg; | |
505 | int i, ret; | |
506 | gdouble low, high; | |
507 | ||
508 | (void)cg; | |
509 | ||
510 | if (!sdi) | |
511 | return SR_ERR_ARG; | |
512 | ||
513 | if (sdi->status != SR_ST_ACTIVE) | |
514 | return SR_ERR; | |
515 | ||
516 | devc = sdi->priv; | |
517 | ||
518 | ret = SR_OK; | |
519 | ||
520 | switch (key) { | |
521 | case SR_CONF_SAMPLERATE: | |
522 | arg = g_variant_get_uint64(data); | |
523 | for (i = 0; i < devc->num_samplerates; i++) { | |
524 | if (devc->samplerates[i] == arg) { | |
525 | devc->cur_samplerate = arg; | |
526 | break; | |
527 | } | |
528 | } | |
529 | if (i == devc->num_samplerates) | |
530 | ret = SR_ERR_ARG; | |
531 | break; | |
532 | case SR_CONF_LIMIT_SAMPLES: | |
533 | devc->limit_samples = g_variant_get_uint64(data); | |
534 | break; | |
535 | case SR_CONF_CAPTURE_RATIO: | |
536 | devc->capture_ratio = g_variant_get_uint64(data); | |
537 | ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK; | |
538 | break; | |
539 | case SR_CONF_VOLTAGE_THRESHOLD: | |
540 | g_variant_get(data, "(dd)", &low, &high); | |
1ee70746 JH |
541 | if (!strcmp(devc->profile->model, "DSLogic")) { |
542 | for (i = 0; (unsigned int)i < | |
543 | ARRAY_SIZE(dslogic_voltage_thresholds); i++) { | |
544 | if (fabs(dslogic_voltage_thresholds[i].low - low) < 0.1 && | |
545 | fabs(dslogic_voltage_thresholds[i].high - high) < 0.1) { | |
546 | devc->cur_threshold = | |
547 | dslogic_voltage_thresholds[i].low; | |
548 | break; | |
549 | } | |
adcb9951 | 550 | } |
1ee70746 JH |
551 | ret = dslogic_fpga_firmware_upload(sdi); |
552 | } else { | |
553 | ret = dslogic_set_voltage_threshold(sdi, (low + high) / 2.0); | |
adcb9951 | 554 | } |
adcb9951 JH |
555 | break; |
556 | case SR_CONF_EXTERNAL_CLOCK: | |
557 | devc->external_clock = g_variant_get_boolean(data); | |
558 | break; | |
559 | case SR_CONF_CONTINUOUS: | |
560 | devc->continuous_mode = g_variant_get_boolean(data); | |
561 | break; | |
562 | case SR_CONF_CLOCK_EDGE: | |
563 | i = lookup_index(data, signal_edge_names, | |
564 | ARRAY_SIZE(signal_edge_names)); | |
565 | if (i < 0) | |
566 | return SR_ERR_ARG; | |
567 | devc->clock_edge = i; | |
568 | break; | |
569 | default: | |
570 | ret = SR_ERR_NA; | |
571 | } | |
572 | ||
573 | return ret; | |
574 | } | |
575 | ||
576 | static int config_list(uint32_t key, GVariant **data, | |
577 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
578 | { | |
1ee70746 | 579 | struct dev_context *devc = NULL; |
adcb9951 JH |
580 | GVariant *gvar, *range[2]; |
581 | GVariantBuilder gvb; | |
582 | unsigned int i; | |
1ee70746 | 583 | double v; |
adcb9951 JH |
584 | |
585 | (void)cg; | |
586 | ||
587 | switch (key) { | |
588 | case SR_CONF_SCAN_OPTIONS: | |
589 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
590 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
591 | break; | |
592 | case SR_CONF_DEVICE_OPTIONS: | |
593 | if (!sdi) { | |
594 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
595 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
596 | } else { | |
597 | devc = sdi->priv; | |
598 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
599 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
600 | } | |
601 | break; | |
602 | case SR_CONF_VOLTAGE_THRESHOLD: | |
1ee70746 JH |
603 | if (sdi->priv) |
604 | devc = sdi->priv; | |
adcb9951 | 605 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); |
1ee70746 JH |
606 | if (devc && !strcmp(devc->profile->model, "DSLogic")) { |
607 | for (i = 0; i < ARRAY_SIZE(dslogic_voltage_thresholds); i++) { | |
608 | range[0] = g_variant_new_double(dslogic_voltage_thresholds[i].low); | |
609 | range[1] = g_variant_new_double(dslogic_voltage_thresholds[i].high); | |
610 | gvar = g_variant_new_tuple(range, 2); | |
611 | g_variant_builder_add_value(&gvb, gvar); | |
612 | } | |
613 | } else { | |
614 | for (v = 0.0; v <= 5.0; v += 0.1) { | |
615 | range[0] = g_variant_new_double(v); | |
616 | range[1] = g_variant_new_double(v); | |
617 | gvar = g_variant_new_tuple(range, 2); | |
618 | g_variant_builder_add_value(&gvb, gvar); | |
619 | } | |
adcb9951 JH |
620 | } |
621 | *data = g_variant_builder_end(&gvb); | |
622 | break; | |
623 | case SR_CONF_SAMPLERATE: | |
624 | devc = sdi->priv; | |
625 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
626 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), devc->samplerates, | |
627 | devc->num_samplerates, sizeof(uint64_t)); | |
628 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
629 | *data = g_variant_builder_end(&gvb); | |
630 | break; | |
631 | case SR_CONF_CLOCK_EDGE: | |
632 | *data = g_variant_new_strv(signal_edge_names, | |
633 | ARRAY_SIZE(signal_edge_names)); | |
634 | break; | |
635 | default: | |
636 | return SR_ERR_NA; | |
637 | } | |
638 | ||
639 | return SR_OK; | |
640 | } | |
641 | ||
642 | static int receive_data(int fd, int revents, void *cb_data) | |
643 | { | |
644 | struct timeval tv; | |
645 | struct drv_context *drvc; | |
646 | ||
647 | (void)fd; | |
648 | (void)revents; | |
649 | ||
650 | drvc = (struct drv_context *)cb_data; | |
651 | ||
652 | tv.tv_sec = tv.tv_usec = 0; | |
653 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
654 | ||
655 | return TRUE; | |
656 | } | |
657 | ||
658 | static int start_transfers(const struct sr_dev_inst *sdi) | |
659 | { | |
660 | struct dev_context *devc; | |
661 | struct sr_usb_dev_inst *usb; | |
662 | struct libusb_transfer *transfer; | |
663 | unsigned int i, num_transfers; | |
664 | int timeout, ret; | |
665 | unsigned char *buf; | |
666 | size_t size; | |
667 | ||
668 | devc = sdi->priv; | |
669 | usb = sdi->conn; | |
670 | ||
671 | devc->sent_samples = 0; | |
672 | devc->acq_aborted = FALSE; | |
673 | devc->empty_transfer_count = 0; | |
674 | devc->trigger_fired = TRUE; | |
675 | ||
676 | num_transfers = dslogic_get_number_of_transfers(devc); | |
677 | ||
678 | if (devc->cur_samplerate == SR_MHZ(100)) | |
679 | num_transfers = 16; | |
680 | else if (devc->cur_samplerate == SR_MHZ(200)) | |
681 | num_transfers = 8; | |
682 | else if (devc->cur_samplerate == SR_MHZ(400)) | |
683 | num_transfers = 4; | |
684 | ||
685 | size = dslogic_get_buffer_size(devc); | |
686 | devc->submitted_transfers = 0; | |
687 | ||
688 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
689 | if (!devc->transfers) { | |
690 | sr_err("USB transfers malloc failed."); | |
691 | return SR_ERR_MALLOC; | |
692 | } | |
693 | ||
694 | timeout = dslogic_get_timeout(devc); | |
695 | devc->num_transfers = num_transfers; | |
696 | for (i = 0; i < num_transfers; i++) { | |
697 | if (!(buf = g_try_malloc(size))) { | |
698 | sr_err("USB transfer buffer malloc failed."); | |
699 | return SR_ERR_MALLOC; | |
700 | } | |
701 | transfer = libusb_alloc_transfer(0); | |
702 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
703 | 6 | LIBUSB_ENDPOINT_IN, buf, size, | |
704 | dslogic_receive_transfer, (void *)sdi, timeout); | |
705 | sr_info("submitting transfer: %d", i); | |
706 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
707 | sr_err("Failed to submit transfer: %s.", | |
708 | libusb_error_name(ret)); | |
709 | libusb_free_transfer(transfer); | |
710 | g_free(buf); | |
711 | dslogic_abort_acquisition(devc); | |
712 | return SR_ERR; | |
713 | } | |
714 | devc->transfers[i] = transfer; | |
715 | devc->submitted_transfers++; | |
716 | } | |
717 | ||
718 | std_session_send_df_header(sdi); | |
719 | ||
720 | return SR_OK; | |
721 | } | |
722 | ||
723 | static void LIBUSB_CALL trigger_receive(struct libusb_transfer *transfer) | |
724 | { | |
725 | const struct sr_dev_inst *sdi; | |
726 | struct dslogic_trigger_pos *tpos; | |
727 | struct dev_context *devc; | |
728 | ||
729 | sdi = transfer->user_data; | |
730 | devc = sdi->priv; | |
731 | if (transfer->status == LIBUSB_TRANSFER_CANCELLED) { | |
732 | sr_dbg("Trigger transfer canceled."); | |
733 | /* Terminate session. */ | |
734 | std_session_send_df_end(sdi); | |
735 | usb_source_remove(sdi->session, devc->ctx); | |
736 | devc->num_transfers = 0; | |
737 | g_free(devc->transfers); | |
738 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED | |
739 | && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) { | |
740 | tpos = (struct dslogic_trigger_pos *)transfer->buffer; | |
741 | sr_info("tpos real_pos %d ram_saddr %d cnt %d", tpos->real_pos, | |
742 | tpos->ram_saddr, tpos->remain_cnt); | |
743 | devc->trigger_pos = tpos->real_pos; | |
744 | g_free(tpos); | |
745 | start_transfers(sdi); | |
746 | } | |
747 | libusb_free_transfer(transfer); | |
748 | } | |
749 | ||
750 | static int trigger_request(const struct sr_dev_inst *sdi) | |
751 | { | |
752 | struct sr_usb_dev_inst *usb; | |
753 | struct libusb_transfer *transfer; | |
754 | struct dslogic_trigger_pos *tpos; | |
755 | struct dev_context *devc; | |
756 | int ret; | |
757 | ||
758 | usb = sdi->conn; | |
759 | devc = sdi->priv; | |
760 | ||
761 | if ((ret = dslogic_stop_acquisition(sdi)) != SR_OK) | |
762 | return ret; | |
763 | ||
764 | if ((ret = dslogic_fpga_configure(sdi)) != SR_OK) | |
765 | return ret; | |
766 | ||
adcb9951 JH |
767 | if ((ret = dslogic_start_acquisition(sdi)) != SR_OK) |
768 | return ret; | |
769 | ||
770 | sr_dbg("Getting trigger."); | |
771 | tpos = g_malloc(sizeof(struct dslogic_trigger_pos)); | |
772 | transfer = libusb_alloc_transfer(0); | |
773 | libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN, | |
774 | (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos), | |
775 | trigger_receive, (void *)sdi, 0); | |
776 | if ((ret = libusb_submit_transfer(transfer)) < 0) { | |
777 | sr_err("Failed to request trigger: %s.", libusb_error_name(ret)); | |
778 | libusb_free_transfer(transfer); | |
779 | g_free(tpos); | |
780 | return SR_ERR; | |
781 | } | |
782 | ||
783 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers)); | |
784 | if (!devc->transfers) { | |
785 | sr_err("USB trigger_pos transfer malloc failed."); | |
786 | return SR_ERR_MALLOC; | |
787 | } | |
788 | devc->num_transfers = 1; | |
789 | devc->submitted_transfers++; | |
790 | devc->transfers[0] = transfer; | |
791 | ||
792 | return ret; | |
793 | } | |
794 | ||
795 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
796 | { | |
797 | struct sr_dev_driver *di; | |
798 | struct drv_context *drvc; | |
799 | struct dev_context *devc; | |
800 | int timeout; | |
801 | ||
802 | if (sdi->status != SR_ST_ACTIVE) | |
803 | return SR_ERR_DEV_CLOSED; | |
804 | ||
805 | di = sdi->driver; | |
806 | drvc = di->context; | |
807 | devc = sdi->priv; | |
808 | ||
809 | devc->ctx = drvc->sr_ctx; | |
810 | devc->sent_samples = 0; | |
811 | devc->empty_transfer_count = 0; | |
812 | devc->acq_aborted = FALSE; | |
813 | ||
814 | timeout = dslogic_get_timeout(devc); | |
815 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc); | |
816 | ||
817 | trigger_request(sdi); | |
818 | ||
819 | return SR_OK; | |
820 | } | |
821 | ||
822 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
823 | { | |
824 | dslogic_stop_acquisition(sdi); | |
825 | ||
826 | dslogic_abort_acquisition(sdi->priv); | |
827 | ||
828 | return SR_OK; | |
829 | } | |
830 | ||
831 | static struct sr_dev_driver dslogic_driver_info = { | |
832 | .name = "dslogic", | |
833 | .longname = "DreamSourceLabs DSLogic", | |
834 | .api_version = 1, | |
835 | .init = std_init, | |
836 | .cleanup = std_cleanup, | |
837 | .scan = scan, | |
838 | .dev_list = std_dev_list, | |
839 | .dev_clear = dev_clear, | |
840 | .config_get = config_get, | |
841 | .config_set = config_set, | |
842 | .config_list = config_list, | |
843 | .dev_open = dev_open, | |
844 | .dev_close = dev_close, | |
845 | .dev_acquisition_start = dev_acquisition_start, | |
846 | .dev_acquisition_stop = dev_acquisition_stop, | |
847 | .context = NULL, | |
848 | }; | |
849 | SR_REGISTER_DEV_DRIVER(dslogic_driver_info); |