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