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