]>
Commit | Line | Data |
---|---|---|
c463dcf0 MC |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
f6a21fa5 MC |
20 | #include <glib.h> |
21 | #include <libusb.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include "libsigrok.h" | |
25 | #include "libsigrok-internal.h" | |
c463dcf0 MC |
26 | #include "protocol.h" |
27 | ||
f6a21fa5 MC |
28 | #define LOGIC16_VID 0x21a9 |
29 | #define LOGIC16_PID 0x1001 | |
30 | #define NUM_PROBES 16 | |
31 | ||
5eea4305 MC |
32 | #define USB_INTERFACE 0 |
33 | #define USB_CONFIGURATION 1 | |
34 | #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw" | |
35 | ||
36 | #define MAX_RENUM_DELAY_MS 3000 | |
7b5daad4 | 37 | #define NUM_SIMUL_TRANSFERS 32 |
5eea4305 MC |
38 | |
39 | ||
c463dcf0 MC |
40 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info; |
41 | static struct sr_dev_driver *di = &saleae_logic16_driver_info; | |
42 | ||
f6a21fa5 MC |
43 | static const char *probe_names[NUM_PROBES + 1] = { |
44 | "0", "1", "2", "3", "4", "5", "6", "7", "8", | |
45 | "9", "10", "11", "12", "13", "14", "15", | |
46 | NULL, | |
47 | }; | |
c463dcf0 | 48 | |
7b5daad4 MC |
49 | static const uint64_t samplerates[] = { |
50 | SR_KHZ(500), | |
51 | SR_MHZ(1), | |
52 | SR_MHZ(2), | |
53 | SR_MHZ(4), | |
54 | SR_MHZ(5), | |
55 | SR_MHZ(8), | |
56 | SR_MHZ(10), | |
57 | SR_KHZ(12500), | |
58 | SR_MHZ(16), | |
59 | SR_MHZ(25), | |
60 | SR_MHZ(32), | |
61 | SR_MHZ(40), | |
62 | SR_MHZ(80), | |
63 | SR_MHZ(100), | |
64 | }; | |
65 | ||
c463dcf0 MC |
66 | static int init(struct sr_context *sr_ctx) |
67 | { | |
68 | return std_init(sr_ctx, di, LOG_PREFIX); | |
69 | } | |
70 | ||
5eea4305 MC |
71 | static gboolean check_conf_profile(libusb_device *dev) |
72 | { | |
73 | struct libusb_device_descriptor des; | |
74 | struct libusb_device_handle *hdl; | |
75 | gboolean ret; | |
76 | unsigned char strdesc[64]; | |
77 | ||
78 | hdl = NULL; | |
79 | ret = FALSE; | |
80 | while (!ret) { | |
81 | /* Assume the FW has not been loaded, unless proven wrong. */ | |
82 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
83 | break; | |
84 | ||
85 | if (libusb_open(dev, &hdl) != 0) | |
86 | break; | |
87 | ||
88 | if (libusb_get_string_descriptor_ascii(hdl, | |
89 | des.iManufacturer, strdesc, sizeof(strdesc)) < 0) | |
90 | break; | |
91 | if (strcmp((const char *)strdesc, "Saleae LLC")) | |
92 | break; | |
93 | ||
94 | if (libusb_get_string_descriptor_ascii(hdl, | |
95 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
96 | break; | |
97 | if (strcmp((const char *)strdesc, "Logic S/16")) | |
98 | break; | |
99 | ||
100 | /* If we made it here, it must be a configured Logic16. */ | |
101 | ret = TRUE; | |
102 | } | |
103 | if (hdl) | |
104 | libusb_close(hdl); | |
105 | ||
106 | return ret; | |
107 | } | |
108 | ||
c463dcf0 MC |
109 | static GSList *scan(GSList *options) |
110 | { | |
111 | struct drv_context *drvc; | |
f6a21fa5 MC |
112 | struct dev_context *devc; |
113 | struct sr_dev_inst *sdi; | |
5eea4305 | 114 | struct sr_usb_dev_inst *usb; |
f6a21fa5 | 115 | struct sr_probe *probe; |
5eea4305 MC |
116 | struct sr_config *src; |
117 | GSList *l, *devices, *conn_devices; | |
f6a21fa5 MC |
118 | struct libusb_device_descriptor des; |
119 | libusb_device **devlist; | |
5eea4305 MC |
120 | int devcnt, ret, i, j; |
121 | const char *conn; | |
c463dcf0 | 122 | |
c463dcf0 | 123 | drvc = di->priv; |
c463dcf0 | 124 | |
5eea4305 MC |
125 | conn = NULL; |
126 | for (l = options; l; l = l->next) { | |
127 | src = l->data; | |
128 | switch (src->key) { | |
129 | case SR_CONF_CONN: | |
130 | conn = g_variant_get_string(src->data, NULL); | |
131 | break; | |
132 | } | |
133 | } | |
134 | if (conn) | |
135 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
136 | else | |
137 | conn_devices = NULL; | |
138 | ||
139 | /* Find all Logic16 devices and upload firmware to them. */ | |
f6a21fa5 MC |
140 | devices = NULL; |
141 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
142 | for (i = 0; devlist[i]; i++) { | |
5eea4305 MC |
143 | if (conn) { |
144 | usb = NULL; | |
145 | for (l = conn_devices; l; l = l->next) { | |
146 | usb = l->data; | |
147 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
148 | && usb->address == libusb_get_device_address(devlist[i])) | |
149 | break; | |
150 | } | |
151 | if (!l) | |
152 | /* This device matched none of the ones that | |
153 | * matched the conn specification. */ | |
154 | continue; | |
155 | } | |
156 | ||
157 | if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) { | |
158 | sr_warn("Failed to get device descriptor: %s.", | |
159 | libusb_error_name(ret)); | |
f6a21fa5 MC |
160 | continue; |
161 | } | |
162 | ||
163 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) | |
164 | continue; | |
165 | ||
166 | devcnt = g_slist_length(drvc->instances); | |
5eea4305 MC |
167 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, |
168 | "Saleae", "Logic16", NULL); | |
169 | if (!sdi) | |
f6a21fa5 MC |
170 | return NULL; |
171 | sdi->driver = di; | |
172 | ||
f6a21fa5 MC |
173 | for (j = 0; probe_names[j]; j++) { |
174 | if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE, | |
175 | probe_names[j]))) | |
176 | return NULL; | |
177 | sdi->probes = g_slist_append(sdi->probes, probe); | |
178 | } | |
179 | ||
5eea4305 | 180 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) |
f6a21fa5 | 181 | return NULL; |
5eea4305 | 182 | sdi->priv = devc; |
f6a21fa5 MC |
183 | drvc->instances = g_slist_append(drvc->instances, sdi); |
184 | devices = g_slist_append(devices, sdi); | |
5eea4305 MC |
185 | |
186 | if (check_conf_profile(devlist[i])) { | |
187 | /* Already has the firmware, so fix the new address. */ | |
188 | sr_dbg("Found a Logic16 device."); | |
189 | sdi->status = SR_ST_INACTIVE; | |
190 | sdi->inst_type = SR_INST_USB; | |
191 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
192 | libusb_get_device_address(devlist[i]), NULL); | |
193 | } else { | |
194 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, | |
195 | FX2_FIRMWARE) == SR_OK) | |
196 | /* Store when this device's FW was updated. */ | |
197 | devc->fw_updated = g_get_monotonic_time(); | |
198 | else | |
199 | sr_err("Firmware upload failed for " | |
200 | "device %d.", devcnt); | |
201 | sdi->inst_type = SR_INST_USB; | |
202 | sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]), | |
203 | 0xff, NULL); | |
204 | } | |
f6a21fa5 MC |
205 | } |
206 | libusb_free_device_list(devlist, 1); | |
5eea4305 | 207 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); |
c463dcf0 MC |
208 | |
209 | return devices; | |
210 | } | |
211 | ||
212 | static GSList *dev_list(void) | |
213 | { | |
214 | struct drv_context *drvc; | |
215 | ||
216 | drvc = di->priv; | |
217 | ||
218 | return drvc->instances; | |
219 | } | |
220 | ||
221 | static int dev_clear(void) | |
222 | { | |
223 | return std_dev_clear(di, NULL); | |
224 | } | |
225 | ||
5eea4305 MC |
226 | static int logic16_dev_open(struct sr_dev_inst *sdi) |
227 | { | |
228 | libusb_device **devlist; | |
229 | struct sr_usb_dev_inst *usb; | |
230 | struct libusb_device_descriptor des; | |
231 | struct dev_context *devc; | |
232 | struct drv_context *drvc; | |
233 | int ret, skip, i, device_count; | |
234 | ||
235 | drvc = di->priv; | |
236 | devc = sdi->priv; | |
237 | usb = sdi->conn; | |
238 | ||
239 | if (sdi->status == SR_ST_ACTIVE) | |
240 | /* Device is already in use. */ | |
241 | return SR_ERR; | |
242 | ||
243 | skip = 0; | |
244 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
245 | if (device_count < 0) { | |
246 | sr_err("Failed to get device list: %s.", | |
247 | libusb_error_name(device_count)); | |
248 | return SR_ERR; | |
249 | } | |
250 | ||
251 | for (i = 0; i < device_count; i++) { | |
252 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
253 | sr_err("Failed to get device descriptor: %s.", | |
254 | libusb_error_name(ret)); | |
255 | continue; | |
256 | } | |
257 | ||
258 | if (des.idVendor != LOGIC16_VID | |
259 | || des.idProduct != LOGIC16_PID) | |
260 | continue; | |
261 | ||
262 | if (sdi->status == SR_ST_INITIALIZING) { | |
263 | if (skip != sdi->index) { | |
264 | /* Skip devices of this type that aren't the one we want. */ | |
265 | skip += 1; | |
266 | continue; | |
267 | } | |
268 | } else if (sdi->status == SR_ST_INACTIVE) { | |
269 | /* | |
270 | * This device is fully enumerated, so we need to find | |
271 | * this device by vendor, product, bus and address. | |
272 | */ | |
273 | if (libusb_get_bus_number(devlist[i]) != usb->bus | |
274 | || libusb_get_device_address(devlist[i]) != usb->address) | |
275 | /* This is not the one. */ | |
276 | continue; | |
277 | } | |
278 | ||
279 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
280 | if (usb->address == 0xff) | |
281 | /* | |
282 | * First time we touch this device after FW | |
283 | * upload, so we don't know the address yet. | |
284 | */ | |
285 | usb->address = libusb_get_device_address(devlist[i]); | |
286 | } else { | |
287 | sr_err("Failed to open device: %s.", | |
288 | libusb_error_name(ret)); | |
289 | break; | |
290 | } | |
291 | ||
15abcf0f MC |
292 | if ((ret = saleae_logic16_init_device(sdi)) != SR_OK) { |
293 | sr_err("Failed to init device."); | |
294 | break; | |
295 | } | |
296 | ||
5eea4305 MC |
297 | sdi->status = SR_ST_ACTIVE; |
298 | sr_info("Opened device %d on %d.%d, " | |
299 | "interface %d.", | |
300 | sdi->index, usb->bus, usb->address, | |
301 | USB_INTERFACE); | |
302 | ||
303 | break; | |
304 | } | |
305 | libusb_free_device_list(devlist, 1); | |
306 | ||
307 | if (sdi->status != SR_ST_ACTIVE) | |
308 | return SR_ERR; | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
c463dcf0 MC |
313 | static int dev_open(struct sr_dev_inst *sdi) |
314 | { | |
5eea4305 MC |
315 | struct sr_usb_dev_inst *usb; |
316 | struct dev_context *devc; | |
317 | int ret; | |
318 | int64_t timediff_us, timediff_ms; | |
319 | ||
320 | devc = sdi->priv; | |
321 | usb = sdi->conn; | |
322 | ||
323 | /* | |
324 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
325 | * milliseconds for the FX2 to renumerate. | |
326 | */ | |
327 | ret = SR_ERR; | |
328 | if (devc->fw_updated > 0) { | |
329 | sr_info("Waiting for device to reset."); | |
330 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
331 | g_usleep(300 * 1000); | |
332 | timediff_ms = 0; | |
333 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
334 | if ((ret = logic16_dev_open(sdi)) == SR_OK) | |
335 | break; | |
336 | g_usleep(100 * 1000); | |
337 | ||
338 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
339 | timediff_ms = timediff_us / 1000; | |
340 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
341 | } | |
342 | if (ret != SR_OK) { | |
343 | sr_err("Device failed to renumerate."); | |
344 | return SR_ERR; | |
345 | } | |
346 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
347 | } else { | |
348 | sr_info("Firmware upload was not needed."); | |
349 | ret = logic16_dev_open(sdi); | |
350 | } | |
351 | ||
352 | if (ret != SR_OK) { | |
353 | sr_err("Unable to open device."); | |
354 | return SR_ERR; | |
355 | } | |
356 | ||
357 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
358 | if (ret != 0) { | |
359 | switch(ret) { | |
360 | case LIBUSB_ERROR_BUSY: | |
361 | sr_err("Unable to claim USB interface. Another " | |
362 | "program or driver has already claimed it."); | |
363 | break; | |
364 | case LIBUSB_ERROR_NO_DEVICE: | |
365 | sr_err("Device has been disconnected."); | |
366 | break; | |
367 | default: | |
368 | sr_err("Unable to claim interface: %s.", | |
369 | libusb_error_name(ret)); | |
370 | break; | |
371 | } | |
c463dcf0 | 372 | |
5eea4305 MC |
373 | return SR_ERR; |
374 | } | |
c463dcf0 | 375 | |
5eea4305 MC |
376 | if (devc->cur_samplerate == 0) { |
377 | /* Samplerate hasn't been set; default to the slowest one. */ | |
7b5daad4 | 378 | devc->cur_samplerate = samplerates[0]; |
5eea4305 | 379 | } |
c463dcf0 MC |
380 | |
381 | return SR_OK; | |
382 | } | |
383 | ||
384 | static int dev_close(struct sr_dev_inst *sdi) | |
385 | { | |
5eea4305 | 386 | struct sr_usb_dev_inst *usb; |
c463dcf0 | 387 | |
5eea4305 MC |
388 | usb = sdi->conn; |
389 | if (usb->devhdl == NULL) | |
390 | return SR_ERR; | |
c463dcf0 | 391 | |
5eea4305 MC |
392 | sr_info("Closing device %d on %d.%d interface %d.", |
393 | sdi->index, usb->bus, usb->address, USB_INTERFACE); | |
394 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
395 | libusb_close(usb->devhdl); | |
396 | usb->devhdl = NULL; | |
c463dcf0 MC |
397 | sdi->status = SR_ST_INACTIVE; |
398 | ||
399 | return SR_OK; | |
400 | } | |
401 | ||
402 | static int cleanup(void) | |
403 | { | |
f6a21fa5 MC |
404 | int ret; |
405 | struct drv_context *drvc; | |
c463dcf0 | 406 | |
f6a21fa5 MC |
407 | if (!(drvc = di->priv)) |
408 | /* Can get called on an unused driver, doesn't matter. */ | |
409 | return SR_OK; | |
c463dcf0 | 410 | |
f6a21fa5 MC |
411 | ret = dev_clear(); |
412 | g_free(drvc); | |
413 | di->priv = NULL; | |
414 | ||
415 | return ret; | |
c463dcf0 MC |
416 | } |
417 | ||
418 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
419 | { | |
7b5daad4 | 420 | struct dev_context *devc; |
c463dcf0 MC |
421 | int ret; |
422 | ||
423 | (void)sdi; | |
424 | (void)data; | |
425 | ||
426 | ret = SR_OK; | |
427 | switch (key) { | |
7b5daad4 MC |
428 | case SR_CONF_SAMPLERATE: |
429 | if (!sdi) | |
430 | return SR_ERR; | |
431 | devc = sdi->priv; | |
432 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
433 | break; | |
c463dcf0 MC |
434 | default: |
435 | return SR_ERR_NA; | |
436 | } | |
437 | ||
438 | return ret; | |
439 | } | |
440 | ||
441 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi) | |
442 | { | |
7b5daad4 | 443 | struct dev_context *devc; |
c463dcf0 MC |
444 | int ret; |
445 | ||
c463dcf0 MC |
446 | if (sdi->status != SR_ST_ACTIVE) |
447 | return SR_ERR_DEV_CLOSED; | |
448 | ||
7b5daad4 MC |
449 | devc = sdi->priv; |
450 | ||
c463dcf0 MC |
451 | ret = SR_OK; |
452 | switch (key) { | |
7b5daad4 MC |
453 | case SR_CONF_SAMPLERATE: |
454 | devc->cur_samplerate = g_variant_get_uint64(data); | |
455 | break; | |
456 | case SR_CONF_LIMIT_SAMPLES: | |
457 | devc->limit_samples = g_variant_get_uint64(data); | |
458 | break; | |
c463dcf0 MC |
459 | default: |
460 | ret = SR_ERR_NA; | |
461 | } | |
462 | ||
463 | return ret; | |
464 | } | |
465 | ||
466 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
467 | { | |
7b5daad4 MC |
468 | GVariant *gvar; |
469 | GVariantBuilder gvb; | |
c463dcf0 MC |
470 | int ret; |
471 | ||
472 | (void)sdi; | |
c463dcf0 MC |
473 | |
474 | ret = SR_OK; | |
475 | switch (key) { | |
7b5daad4 MC |
476 | case SR_CONF_SAMPLERATE: |
477 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
478 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, | |
479 | ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
480 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
481 | *data = g_variant_builder_end(&gvb); | |
482 | break; | |
c463dcf0 MC |
483 | default: |
484 | return SR_ERR_NA; | |
485 | } | |
486 | ||
487 | return ret; | |
488 | } | |
489 | ||
7b5daad4 MC |
490 | static void abort_acquisition(struct dev_context *devc) |
491 | { | |
492 | int i; | |
493 | ||
494 | devc->num_samples = -1; | |
495 | ||
496 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
497 | if (devc->transfers[i]) | |
498 | libusb_cancel_transfer(devc->transfers[i]); | |
499 | } | |
500 | } | |
501 | ||
502 | static unsigned int bytes_per_ms(struct dev_context *devc) | |
503 | { | |
504 | return devc->cur_samplerate * devc->num_channels / 8000; | |
505 | } | |
506 | ||
507 | static size_t get_buffer_size(struct dev_context *devc) | |
508 | { | |
509 | size_t s; | |
510 | ||
511 | /* | |
512 | * The buffer should be large enough to hold 10ms of data and | |
513 | * a multiple of 512. | |
514 | */ | |
515 | s = 10 * bytes_per_ms(devc); | |
516 | return (s + 511) & ~511; | |
517 | } | |
518 | ||
519 | static unsigned int get_number_of_transfers(struct dev_context *devc) | |
520 | { | |
521 | unsigned int n; | |
522 | ||
523 | /* Total buffer size should be able to hold about 500ms of data. */ | |
524 | n = 500 * bytes_per_ms(devc) / get_buffer_size(devc); | |
525 | ||
526 | if (n > NUM_SIMUL_TRANSFERS) | |
527 | return NUM_SIMUL_TRANSFERS; | |
528 | ||
529 | return n; | |
530 | } | |
531 | ||
532 | static unsigned int get_timeout(struct dev_context *devc) | |
533 | { | |
534 | size_t total_size; | |
535 | unsigned int timeout; | |
536 | ||
537 | total_size = get_buffer_size(devc) * get_number_of_transfers(devc); | |
538 | timeout = total_size / bytes_per_ms(devc); | |
539 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
540 | } | |
541 | ||
542 | static int configure_probes(const struct sr_dev_inst *sdi) | |
543 | { | |
544 | struct dev_context *devc; | |
545 | struct sr_probe *probe; | |
546 | GSList *l; | |
547 | uint16_t probe_bit; | |
548 | ||
549 | devc = sdi->priv; | |
550 | ||
551 | devc->cur_channels = 0; | |
552 | devc->num_channels = 0; | |
553 | for (l = sdi->probes; l; l = l->next) { | |
554 | probe = (struct sr_probe *)l->data; | |
555 | if (probe->enabled == FALSE) | |
556 | continue; | |
557 | ||
558 | probe_bit = 1 << (probe->index); | |
559 | ||
560 | devc->cur_channels |= probe_bit; | |
561 | ||
562 | #ifdef WORDS_BIGENDIAN | |
563 | /* Output logic data should be stored in little endian | |
564 | format. To speed things up during conversion, do the | |
565 | switcharoo here instead. */ | |
566 | ||
567 | probe_bit = 1 << (probe->index ^ 8); | |
568 | #endif | |
569 | ||
570 | devc->channel_masks[devc->num_channels ++] = probe_bit; | |
571 | } | |
572 | ||
573 | return SR_OK; | |
574 | } | |
575 | ||
576 | static int receive_data(int fd, int revents, void *cb_data) | |
577 | { | |
578 | struct timeval tv; | |
579 | struct dev_context *devc; | |
580 | struct drv_context *drvc; | |
581 | const struct sr_dev_inst *sdi; | |
582 | ||
583 | (void)fd; | |
584 | (void)revents; | |
585 | ||
586 | sdi = cb_data; | |
587 | drvc = di->priv; | |
588 | devc = sdi->priv; | |
589 | ||
590 | tv.tv_sec = tv.tv_usec = 0; | |
591 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
592 | ||
593 | if (devc->num_samples == -2) { | |
594 | saleae_logic16_abort_acquisition(sdi); | |
595 | abort_acquisition(devc); | |
596 | } | |
597 | ||
598 | return TRUE; | |
599 | } | |
600 | ||
c463dcf0 | 601 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, |
7b5daad4 | 602 | void *cb_data) |
c463dcf0 | 603 | { |
7b5daad4 MC |
604 | struct dev_context *devc; |
605 | struct drv_context *drvc; | |
606 | struct sr_usb_dev_inst *usb; | |
607 | struct libusb_transfer *transfer; | |
608 | const struct libusb_pollfd **lupfd; | |
609 | unsigned int i, timeout, num_transfers; | |
610 | int ret; | |
611 | unsigned char *buf; | |
612 | size_t size, convsize; | |
c463dcf0 MC |
613 | |
614 | if (sdi->status != SR_ST_ACTIVE) | |
615 | return SR_ERR_DEV_CLOSED; | |
616 | ||
7b5daad4 MC |
617 | drvc = di->priv; |
618 | devc = sdi->priv; | |
619 | usb = sdi->conn; | |
620 | ||
621 | /* Configures devc->cur_channels */ | |
622 | if (configure_probes(sdi) != SR_OK) { | |
623 | sr_err("Failed to configure probes."); | |
624 | return SR_ERR; | |
625 | } | |
626 | ||
627 | devc->cb_data = cb_data; | |
628 | devc->num_samples = 0; | |
629 | devc->empty_transfer_count = 0; | |
630 | devc->cur_channel = 0; | |
631 | memset(devc->channel_data, 0, sizeof(devc->channel_data)); | |
632 | ||
633 | timeout = get_timeout(devc); | |
634 | num_transfers = get_number_of_transfers(devc); | |
635 | size = get_buffer_size(devc); | |
636 | convsize = (size / devc->num_channels + 2) * 16; | |
637 | devc->submitted_transfers = 0; | |
638 | devc->usbfd = NULL; | |
639 | ||
640 | devc->convbuffer_size = convsize; | |
641 | if (!(devc->convbuffer = g_try_malloc(convsize))) { | |
642 | sr_err("Conversion buffer malloc failed."); | |
643 | return SR_ERR_MALLOC; | |
644 | } | |
645 | ||
646 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
647 | if (!devc->transfers) { | |
648 | sr_err("USB transfers malloc failed."); | |
649 | g_free(devc->convbuffer); | |
650 | return SR_ERR_MALLOC; | |
651 | } | |
652 | ||
653 | if ((ret = saleae_logic16_setup_acquisition(sdi, devc->cur_samplerate, | |
654 | devc->cur_channels)) != SR_OK) { | |
655 | g_free(devc->transfers); | |
656 | g_free(devc->convbuffer); | |
657 | return ret; | |
658 | } | |
659 | ||
660 | devc->num_transfers = num_transfers; | |
661 | for (i = 0; i < num_transfers; i++) { | |
662 | if (!(buf = g_try_malloc(size))) { | |
663 | sr_err("USB transfer buffer malloc failed."); | |
664 | if (devc->submitted_transfers) | |
665 | abort_acquisition(devc); | |
666 | else { | |
667 | g_free(devc->transfers); | |
668 | g_free(devc->convbuffer); | |
669 | } | |
670 | return SR_ERR_MALLOC; | |
671 | } | |
672 | transfer = libusb_alloc_transfer(0); | |
673 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
674 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
675 | saleae_logic16_receive_transfer, devc, timeout); | |
676 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
677 | sr_err("Failed to submit transfer: %s.", | |
678 | libusb_error_name(ret)); | |
679 | libusb_free_transfer(transfer); | |
680 | g_free(buf); | |
681 | abort_acquisition(devc); | |
682 | return SR_ERR; | |
683 | } | |
684 | devc->transfers[i] = transfer; | |
685 | devc->submitted_transfers++; | |
686 | } | |
687 | ||
688 | lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx); | |
689 | for (i = 0; lupfd[i]; i++); | |
690 | if (!(devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1)))) { | |
691 | abort_acquisition(devc); | |
692 | free(lupfd); | |
693 | return SR_ERR; | |
694 | } | |
695 | for (i = 0; lupfd[i]; i++) { | |
696 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, | |
697 | timeout, receive_data, (void *)sdi); | |
698 | devc->usbfd[i] = lupfd[i]->fd; | |
699 | } | |
700 | devc->usbfd[i] = -1; | |
701 | free(lupfd); | |
702 | ||
703 | /* Send header packet to the session bus. */ | |
704 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
705 | ||
706 | if ((ret = saleae_logic16_start_acquisition(sdi)) != SR_OK) { | |
707 | abort_acquisition(devc); | |
708 | return ret; | |
709 | } | |
c463dcf0 MC |
710 | |
711 | return SR_OK; | |
712 | } | |
713 | ||
714 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
715 | { | |
7b5daad4 MC |
716 | int ret; |
717 | ||
c463dcf0 MC |
718 | (void)cb_data; |
719 | ||
720 | if (sdi->status != SR_ST_ACTIVE) | |
721 | return SR_ERR_DEV_CLOSED; | |
722 | ||
7b5daad4 | 723 | ret = saleae_logic16_abort_acquisition(sdi); |
c463dcf0 | 724 | |
7b5daad4 MC |
725 | abort_acquisition(sdi->priv); |
726 | ||
727 | return ret; | |
c463dcf0 MC |
728 | } |
729 | ||
730 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = { | |
731 | .name = "saleae-logic16", | |
732 | .longname = "Saleae Logic16", | |
733 | .api_version = 1, | |
734 | .init = init, | |
735 | .cleanup = cleanup, | |
736 | .scan = scan, | |
737 | .dev_list = dev_list, | |
738 | .dev_clear = dev_clear, | |
739 | .config_get = config_get, | |
740 | .config_set = config_set, | |
741 | .config_list = config_list, | |
742 | .dev_open = dev_open, | |
743 | .dev_close = dev_close, | |
744 | .dev_acquisition_start = dev_acquisition_start, | |
745 | .dev_acquisition_stop = dev_acquisition_stop, | |
746 | .priv = NULL, | |
747 | }; |