]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
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 | ||
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <sys/time.h> | |
23 | #include <inttypes.h> | |
24 | #include <glib.h> | |
25 | #include <libusb.h> | |
408e7199 | 26 | #include <sigrok.h> |
a1bb33af | 27 | #include "config.h" |
a1bb33af | 28 | |
6f5f21f9 | 29 | #define USB_VENDOR 0x0925 |
a1bb33af | 30 | #define USB_PRODUCT 0x3881 |
6f5f21f9 | 31 | #define USB_VENDOR_NAME "Saleae" |
a1bb33af UH |
32 | #define USB_MODEL_NAME "Logic" |
33 | #define USB_MODEL_VERSION "" | |
34 | ||
35 | #define USB_INTERFACE 0 | |
36 | #define USB_CONFIGURATION 1 | |
6f5f21f9 | 37 | #define NUM_PROBES 8 |
a1bb33af UH |
38 | #define NUM_TRIGGER_STAGES 4 |
39 | #define TRIGGER_TYPES "01" | |
e65679b1 | 40 | #define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw" |
a1bb33af UH |
41 | |
42 | /* delay in ms */ | |
6f5f21f9 UH |
43 | #define FIRMWARE_RENUM_DELAY 2000 |
44 | #define NUM_SIMUL_TRANSFERS 10 | |
45 | #define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2) | |
a1bb33af | 46 | |
fed16f06 | 47 | /* Software trigger implementation: positive values indicate trigger stage. */ |
a1bb33af UH |
48 | #define TRIGGER_FIRED -1 |
49 | ||
6f5f21f9 | 50 | /* There is only one model Saleae Logic, and this is what it supports: */ |
a1bb33af UH |
51 | static int capabilities[] = { |
52 | HWCAP_LOGIC_ANALYZER, | |
53 | HWCAP_SAMPLERATE, | |
54 | ||
6f5f21f9 | 55 | /* These are really implemented in the driver, not the hardware. */ |
a1bb33af | 56 | HWCAP_LIMIT_SAMPLES, |
6f5f21f9 | 57 | 0, |
a1bb33af UH |
58 | }; |
59 | ||
6f5f21f9 | 60 | /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */ |
a1bb33af UH |
61 | static GSList *device_instances = NULL; |
62 | ||
6f5f21f9 UH |
63 | /* |
64 | * Since we can't keep track of a Saleae Logic device after upgrading the | |
a1bb33af UH |
65 | * firmware -- it re-enumerates into a different device address after the |
66 | * upgrade -- this is like a global lock. No device will open until a proper | |
67 | * delay after the last device was upgraded. | |
68 | */ | |
afc8e4de | 69 | GTimeVal firmware_updated = { 0, 0 }; |
a1bb33af UH |
70 | |
71 | static libusb_context *usb_context = NULL; | |
72 | ||
73 | static uint64_t supported_samplerates[] = { | |
74 | KHZ(200), | |
75 | KHZ(250), | |
76 | KHZ(500), | |
77 | MHZ(1), | |
78 | MHZ(2), | |
79 | MHZ(4), | |
80 | MHZ(8), | |
81 | MHZ(12), | |
82 | MHZ(16), | |
83 | MHZ(24), | |
6f5f21f9 | 84 | 0, |
a1bb33af UH |
85 | }; |
86 | ||
87 | static struct samplerates samplerates = { | |
88 | KHZ(200), | |
89 | MHZ(24), | |
90 | 0, | |
6f5f21f9 | 91 | supported_samplerates, |
a1bb33af UH |
92 | }; |
93 | ||
6f5f21f9 | 94 | /* TODO: All of these should go in a device-specific struct. */ |
4c100f32 | 95 | static uint64_t cur_samplerate = 0; |
a1bb33af | 96 | static uint64_t limit_samples = 0; |
6f5f21f9 UH |
97 | static uint8_t probe_mask = 0; |
98 | static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 }; | |
99 | static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 }; | |
100 | static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 }; | |
fed16f06 | 101 | |
a1bb33af UH |
102 | int trigger_stage = TRIGGER_FIRED; |
103 | ||
a1bb33af | 104 | static int hw_set_configuration(int device_index, int capability, void *value); |
9c48090a | 105 | static void hw_stop_acquisition(int device_index, gpointer session_device_id); |
a1bb33af | 106 | |
fed16f06 UH |
107 | /* |
108 | * Returns 1 if the device's configuration profile match the Logic firmware's | |
109 | * configuration, 0 otherwise. | |
a1bb33af UH |
110 | */ |
111 | int check_conf_profile(libusb_device *dev) | |
112 | { | |
113 | struct libusb_device_descriptor des; | |
6f5f21f9 | 114 | struct libusb_config_descriptor *conf_dsc = NULL; |
a1bb33af | 115 | const struct libusb_interface_descriptor *intf_dsc; |
6f5f21f9 | 116 | int ret = -1; |
a1bb33af | 117 | |
6f5f21f9 UH |
118 | while (ret == -1) { |
119 | /* Assume it's not a Saleae Logic unless proven wrong. */ | |
a1bb33af UH |
120 | ret = 0; |
121 | ||
6f5f21f9 | 122 | if (libusb_get_device_descriptor(dev, &des) != 0) |
a1bb33af UH |
123 | break; |
124 | ||
6f5f21f9 UH |
125 | if (des.bNumConfigurations != 1) |
126 | /* Need exactly 1 configuration. */ | |
a1bb33af UH |
127 | break; |
128 | ||
6f5f21f9 | 129 | if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0) |
a1bb33af UH |
130 | break; |
131 | ||
6f5f21f9 UH |
132 | if (conf_dsc->bNumInterfaces != 1) |
133 | /* Need exactly 1 interface. */ | |
a1bb33af UH |
134 | break; |
135 | ||
6f5f21f9 UH |
136 | if (conf_dsc->interface[0].num_altsetting != 1) |
137 | /* Need just one alternate setting. */ | |
a1bb33af UH |
138 | break; |
139 | ||
140 | intf_dsc = &(conf_dsc->interface[0].altsetting[0]); | |
6f5f21f9 UH |
141 | if (intf_dsc->bNumEndpoints != 2) |
142 | /* Need 2 endpoints. */ | |
a1bb33af UH |
143 | break; |
144 | ||
6f5f21f9 UH |
145 | if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != |
146 | (1 | LIBUSB_ENDPOINT_OUT)) | |
147 | /* First endpoint should be 1 (outbound). */ | |
a1bb33af UH |
148 | break; |
149 | ||
6f5f21f9 UH |
150 | if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) != |
151 | (2 | LIBUSB_ENDPOINT_IN)) | |
152 | /* First endpoint should be 2 (inbound). */ | |
a1bb33af UH |
153 | break; |
154 | ||
6f5f21f9 | 155 | /* If we made it here, it must be a Saleae Logic. */ |
a1bb33af UH |
156 | ret = 1; |
157 | } | |
6f5f21f9 UH |
158 | |
159 | if (conf_dsc) | |
a1bb33af UH |
160 | libusb_free_config_descriptor(conf_dsc); |
161 | ||
162 | return ret; | |
163 | } | |
164 | ||
90429916 UH |
165 | static int opendev2(int device_index, struct sigrok_device_instance **sdi, |
166 | libusb_device *dev, struct libusb_device_descriptor *des, | |
167 | int *skip) | |
168 | { | |
169 | int err; | |
170 | ||
171 | if ((err = libusb_get_device_descriptor(dev, des))) { | |
172 | g_warning("failed to get device descriptor: %d", err); | |
173 | return -1; | |
174 | } | |
175 | ||
176 | if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT) | |
177 | return 0; | |
178 | ||
179 | if (*skip != device_index) { | |
180 | /* Skip devices of this type that aren't the one we want. */ | |
181 | *skip += 1; | |
182 | return 0; | |
183 | } | |
184 | ||
185 | /* | |
186 | * Should check the bus here, since we know that already. But what are | |
187 | * we going to do if it doesn't match after the right number of skips? | |
188 | */ | |
189 | if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) { | |
190 | (*sdi)->usb->address = libusb_get_device_address(dev); | |
191 | (*sdi)->status = ST_ACTIVE; | |
192 | g_message("opened device %d on %d.%d interface %d", | |
193 | (*sdi)->index, (*sdi)->usb->bus, | |
194 | (*sdi)->usb->address, USB_INTERFACE); | |
195 | } else { | |
196 | g_warning("failed to open device: %d", err); | |
197 | *sdi = NULL; | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
afc8e4de UH |
203 | static int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev, |
204 | struct libusb_device_descriptor *des) | |
90429916 UH |
205 | { |
206 | int err; | |
207 | ||
208 | if ((err = libusb_get_device_descriptor(dev, des))) { | |
209 | g_warning("failed to get device descriptor: %d", err); | |
210 | return -1; | |
211 | } | |
212 | ||
213 | if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT) | |
214 | return 0; | |
215 | ||
216 | if (libusb_get_bus_number(dev) == (*sdi)->usb->bus | |
217 | && libusb_get_device_address(dev) == (*sdi)->usb->address) { | |
218 | /* Found it. */ | |
219 | if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) { | |
220 | (*sdi)->status = ST_ACTIVE; | |
221 | g_message("opened device %d on %d.%d interface %d", | |
222 | (*sdi)->index, (*sdi)->usb->bus, | |
223 | (*sdi)->usb->address, USB_INTERFACE); | |
224 | } else { | |
225 | g_warning("failed to open device: %d", err); | |
226 | *sdi = NULL; | |
227 | } | |
228 | } | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
a1bb33af UH |
233 | struct sigrok_device_instance *sl_open_device(int device_index) |
234 | { | |
235 | struct sigrok_device_instance *sdi; | |
236 | libusb_device **devlist; | |
237 | struct libusb_device_descriptor des; | |
238 | int err, skip, i; | |
239 | ||
6f5f21f9 | 240 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) |
a1bb33af UH |
241 | return NULL; |
242 | ||
243 | libusb_get_device_list(usb_context, &devlist); | |
6f5f21f9 UH |
244 | if (sdi->status == ST_INITIALIZING) { |
245 | /* | |
246 | * This device was renumerating last time we touched it. | |
247 | * opendev() guarantees we've waited long enough for it to | |
248 | * have booted properly, so now we need to find it on | |
a1bb33af UH |
249 | * the bus and record its new address. |
250 | */ | |
251 | skip = 0; | |
6f5f21f9 | 252 | for (i = 0; devlist[i]; i++) { |
90429916 UH |
253 | /* TODO: Error handling. */ |
254 | err = opendev2(device_index, &sdi, devlist[i], &des, | |
255 | &skip); | |
a1bb33af | 256 | } |
6f5f21f9 UH |
257 | } else if (sdi->status == ST_INACTIVE) { |
258 | /* | |
259 | * This device is fully enumerated, so we need to find this | |
260 | * device by vendor, product, bus and address. | |
261 | */ | |
a1bb33af | 262 | libusb_get_device_list(usb_context, &devlist); |
6f5f21f9 | 263 | for (i = 0; devlist[i]; i++) { |
90429916 | 264 | /* TODO: Error handling. */ |
afc8e4de | 265 | err = opendev3(&sdi, devlist[i], &des); |
a1bb33af | 266 | } |
6f5f21f9 UH |
267 | } else { |
268 | /* Status must be ST_ACTIVE, i.e. already in use... */ | |
a1bb33af UH |
269 | sdi = NULL; |
270 | } | |
271 | libusb_free_device_list(devlist, 1); | |
272 | ||
6f5f21f9 | 273 | if (sdi && sdi->status != ST_ACTIVE) |
a1bb33af UH |
274 | sdi = NULL; |
275 | ||
276 | return sdi; | |
277 | } | |
278 | ||
a1bb33af UH |
279 | int upload_firmware(libusb_device *dev) |
280 | { | |
edf60d05 | 281 | int ret; |
a1bb33af | 282 | |
08cfe6a2 UH |
283 | ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE); |
284 | if (ret != 0) | |
a1bb33af UH |
285 | return 1; |
286 | ||
6f5f21f9 | 287 | /* Remember when the last firmware update was done. */ |
a1bb33af UH |
288 | g_get_current_time(&firmware_updated); |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
a1bb33af UH |
293 | static void close_device(struct sigrok_device_instance *sdi) |
294 | { | |
f6958dab UH |
295 | if (sdi->usb->devhdl == NULL) |
296 | return; | |
297 | ||
298 | g_message("closing device %d on %d.%d interface %d", sdi->index, | |
299 | sdi->usb->bus, sdi->usb->address, USB_INTERFACE); | |
300 | libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE); | |
301 | libusb_close(sdi->usb->devhdl); | |
302 | sdi->usb->devhdl = NULL; | |
303 | sdi->status = ST_INACTIVE; | |
a1bb33af UH |
304 | } |
305 | ||
fed16f06 | 306 | static int configure_probes(GSList *probes) |
a1bb33af UH |
307 | { |
308 | struct probe *probe; | |
309 | GSList *l; | |
310 | int probe_bit, stage, i; | |
311 | char *tc; | |
312 | ||
313 | probe_mask = 0; | |
6f5f21f9 | 314 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { |
a1bb33af UH |
315 | trigger_mask[i] = 0; |
316 | trigger_value[i] = 0; | |
317 | } | |
318 | ||
319 | stage = -1; | |
6f5f21f9 UH |
320 | for (l = probes; l; l = l->next) { |
321 | probe = (struct probe *)l->data; | |
322 | if (probe->enabled == FALSE) | |
a1bb33af UH |
323 | continue; |
324 | probe_bit = 1 << (probe->index - 1); | |
325 | probe_mask |= probe_bit; | |
6f5f21f9 | 326 | if (probe->trigger) { |
a1bb33af | 327 | stage = 0; |
6f5f21f9 | 328 | for (tc = probe->trigger; *tc; tc++) { |
a1bb33af | 329 | trigger_mask[stage] |= probe_bit; |
6f5f21f9 | 330 | if (*tc == '1') |
a1bb33af UH |
331 | trigger_value[stage] |= probe_bit; |
332 | stage++; | |
6f5f21f9 | 333 | if (stage > NUM_TRIGGER_STAGES) |
e31b636d | 334 | return SIGROK_ERR; |
a1bb33af UH |
335 | } |
336 | } | |
337 | } | |
338 | ||
6f5f21f9 UH |
339 | if (stage == -1) |
340 | /* | |
341 | * We didn't configure any triggers, make sure acquisition | |
342 | * doesn't wait for any. | |
343 | */ | |
a1bb33af UH |
344 | trigger_stage = TRIGGER_FIRED; |
345 | else | |
346 | trigger_stage = 0; | |
347 | ||
348 | return SIGROK_OK; | |
349 | } | |
350 | ||
a1bb33af UH |
351 | /* |
352 | * API callbacks | |
353 | */ | |
354 | ||
355 | static int hw_init(char *deviceinfo) | |
356 | { | |
357 | struct sigrok_device_instance *sdi; | |
358 | struct libusb_device_descriptor des; | |
359 | libusb_device **devlist; | |
360 | int err, devcnt, i; | |
361 | ||
afc8e4de UH |
362 | /* QUICK HACK */ |
363 | deviceinfo = deviceinfo; | |
364 | ||
6f5f21f9 | 365 | if (libusb_init(&usb_context) != 0) { |
a1bb33af UH |
366 | g_warning("Failed to initialize USB."); |
367 | return 0; | |
368 | } | |
369 | libusb_set_debug(usb_context, 3); | |
370 | ||
6f5f21f9 | 371 | /* Find all Saleae Logic devices and upload firmware to all of them. */ |
a1bb33af UH |
372 | devcnt = 0; |
373 | libusb_get_device_list(usb_context, &devlist); | |
6f5f21f9 | 374 | for (i = 0; devlist[i]; i++) { |
a1bb33af | 375 | err = libusb_get_device_descriptor(devlist[i], &des); |
6f5f21f9 | 376 | if (err != 0) { |
a1bb33af UH |
377 | g_warning("failed to get device descriptor: %d", err); |
378 | continue; | |
379 | } | |
380 | ||
f6958dab UH |
381 | if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT) |
382 | continue; /* Not a Saleae Logic... */ | |
a1bb33af | 383 | |
f6958dab UH |
384 | sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING, |
385 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
386 | if (!sdi) | |
387 | return 0; | |
388 | device_instances = g_slist_append(device_instances, sdi); | |
6f5f21f9 | 389 | |
f6958dab UH |
390 | if (check_conf_profile(devlist[i]) == 0) { |
391 | /* | |
392 | * Continue on the off chance that the device is in a | |
393 | * working state. TODO: Could maybe try a USB reset, | |
394 | * or uploading the firmware again. | |
395 | */ | |
396 | if (upload_firmware(devlist[i]) > 0) | |
fed16f06 UH |
397 | g_warning("firmware upload failed for " |
398 | "device %d", devcnt); | |
a1bb33af | 399 | |
f6958dab | 400 | sdi->usb = usb_device_instance_new |
fed16f06 | 401 | (libusb_get_bus_number(devlist[i]), 0, NULL); |
f6958dab UH |
402 | } else { |
403 | /* Already has the firmware, so fix the new address. */ | |
404 | sdi->usb = usb_device_instance_new | |
405 | (libusb_get_bus_number(devlist[i]), | |
fed16f06 | 406 | libusb_get_device_address(devlist[i]), NULL); |
a1bb33af | 407 | } |
f6958dab | 408 | devcnt++; |
a1bb33af UH |
409 | } |
410 | libusb_free_device_list(devlist, 1); | |
411 | ||
412 | return devcnt; | |
413 | } | |
414 | ||
a1bb33af UH |
415 | static int hw_opendev(int device_index) |
416 | { | |
417 | GTimeVal cur_time; | |
418 | struct sigrok_device_instance *sdi; | |
419 | int timediff, err; | |
420 | unsigned int cur, upd; | |
421 | ||
6f5f21f9 UH |
422 | if (firmware_updated.tv_sec > 0) { |
423 | /* Firmware was recently uploaded. */ | |
a1bb33af UH |
424 | g_get_current_time(&cur_time); |
425 | cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000; | |
6f5f21f9 UH |
426 | upd = firmware_updated.tv_sec * 1000 + |
427 | firmware_updated.tv_usec / 1000; | |
a1bb33af | 428 | timediff = cur - upd; |
6f5f21f9 | 429 | if (timediff < FIRMWARE_RENUM_DELAY) { |
a1bb33af | 430 | timediff = FIRMWARE_RENUM_DELAY - timediff; |
6f5f21f9 UH |
431 | g_message("waiting %d ms for device to reset", |
432 | timediff); | |
a1bb33af UH |
433 | g_usleep(timediff * 1000); |
434 | firmware_updated.tv_sec = 0; | |
435 | } | |
436 | } | |
437 | ||
6f5f21f9 | 438 | if (!(sdi = sl_open_device(device_index))) { |
a1bb33af | 439 | g_warning("unable to open device"); |
e31b636d | 440 | return SIGROK_ERR; |
a1bb33af UH |
441 | } |
442 | ||
443 | err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE); | |
6f5f21f9 | 444 | if (err != 0) { |
a1bb33af | 445 | g_warning("Unable to claim interface: %d", err); |
e31b636d | 446 | return SIGROK_ERR; |
a1bb33af UH |
447 | } |
448 | ||
6f5f21f9 UH |
449 | if (cur_samplerate == 0) { |
450 | /* Samplerate hasn't been set; default to the slowest one. */ | |
451 | if (hw_set_configuration(device_index, HWCAP_SAMPLERATE, | |
fed16f06 | 452 | &supported_samplerates[0]) == SIGROK_ERR) |
e31b636d | 453 | return SIGROK_ERR; |
a1bb33af UH |
454 | } |
455 | ||
456 | return SIGROK_OK; | |
457 | } | |
458 | ||
a1bb33af UH |
459 | static void hw_closedev(int device_index) |
460 | { | |
461 | struct sigrok_device_instance *sdi; | |
462 | ||
6f5f21f9 | 463 | if ((sdi = get_sigrok_device_instance(device_instances, device_index))) |
a1bb33af | 464 | close_device(sdi); |
a1bb33af UH |
465 | } |
466 | ||
a1bb33af UH |
467 | static void hw_cleanup(void) |
468 | { | |
469 | GSList *l; | |
470 | ||
6f5f21f9 UH |
471 | /* Properly close all devices... */ |
472 | for (l = device_instances; l; l = l->next) | |
473 | close_device((struct sigrok_device_instance *)l->data); | |
a1bb33af | 474 | |
6f5f21f9 UH |
475 | /* ...and free all their memory. */ |
476 | for (l = device_instances; l; l = l->next) | |
a1bb33af UH |
477 | g_free(l->data); |
478 | g_slist_free(device_instances); | |
479 | device_instances = NULL; | |
480 | ||
6f5f21f9 | 481 | if (usb_context) |
a1bb33af UH |
482 | libusb_exit(usb_context); |
483 | usb_context = NULL; | |
a1bb33af UH |
484 | } |
485 | ||
a1bb33af UH |
486 | static void *hw_get_device_info(int device_index, int device_info_id) |
487 | { | |
488 | struct sigrok_device_instance *sdi; | |
6f5f21f9 | 489 | void *info = NULL; |
a1bb33af | 490 | |
6f5f21f9 | 491 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) |
a1bb33af UH |
492 | return NULL; |
493 | ||
6f5f21f9 | 494 | switch (device_info_id) { |
a1bb33af UH |
495 | case DI_INSTANCE: |
496 | info = sdi; | |
497 | break; | |
498 | case DI_NUM_PROBES: | |
499 | info = GINT_TO_POINTER(NUM_PROBES); | |
500 | break; | |
501 | case DI_SAMPLERATES: | |
502 | info = &samplerates; | |
503 | break; | |
504 | case DI_TRIGGER_TYPES: | |
505 | info = TRIGGER_TYPES; | |
506 | break; | |
4c100f32 UH |
507 | case DI_CUR_SAMPLERATE: |
508 | info = &cur_samplerate; | |
a1bb33af UH |
509 | break; |
510 | } | |
511 | ||
512 | return info; | |
513 | } | |
514 | ||
a1bb33af UH |
515 | static int hw_get_status(int device_index) |
516 | { | |
517 | struct sigrok_device_instance *sdi; | |
518 | ||
519 | sdi = get_sigrok_device_instance(device_instances, device_index); | |
6f5f21f9 | 520 | if (sdi) |
a1bb33af UH |
521 | return sdi->status; |
522 | else | |
523 | return ST_NOT_FOUND; | |
524 | } | |
525 | ||
a1bb33af UH |
526 | static int *hw_get_capabilities(void) |
527 | { | |
a1bb33af UH |
528 | return capabilities; |
529 | } | |
530 | ||
6f5f21f9 UH |
531 | static int set_configuration_samplerate(struct sigrok_device_instance *sdi, |
532 | uint64_t samplerate) | |
a1bb33af UH |
533 | { |
534 | uint8_t divider; | |
535 | int ret, result, i; | |
536 | unsigned char buf[2]; | |
537 | ||
6f5f21f9 UH |
538 | for (i = 0; supported_samplerates[i]; i++) { |
539 | if (supported_samplerates[i] == samplerate) | |
a1bb33af UH |
540 | break; |
541 | } | |
6f5f21f9 | 542 | if (supported_samplerates[i] == 0) |
e31b636d | 543 | return SIGROK_ERR_SAMPLERATE; |
a1bb33af | 544 | |
eee4890f | 545 | divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1; |
a1bb33af | 546 | |
6f5f21f9 UH |
547 | g_message("setting samplerate to %" PRIu64 " Hz (divider %d)", |
548 | samplerate, divider); | |
a1bb33af UH |
549 | buf[0] = 0x01; |
550 | buf[1] = divider; | |
6f5f21f9 UH |
551 | ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT, |
552 | buf, 2, &result, 500); | |
553 | if (ret != 0) { | |
a1bb33af | 554 | g_warning("failed to set samplerate: %d", ret); |
e31b636d | 555 | return SIGROK_ERR; |
a1bb33af | 556 | } |
4c100f32 | 557 | cur_samplerate = samplerate; |
a1bb33af UH |
558 | |
559 | return SIGROK_OK; | |
560 | } | |
561 | ||
a1bb33af UH |
562 | static int hw_set_configuration(int device_index, int capability, void *value) |
563 | { | |
564 | struct sigrok_device_instance *sdi; | |
565 | int ret; | |
566 | uint64_t *tmp_u64; | |
567 | ||
6f5f21f9 | 568 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) |
e31b636d | 569 | return SIGROK_ERR; |
a1bb33af | 570 | |
6f5f21f9 | 571 | if (capability == HWCAP_SAMPLERATE) { |
a1bb33af UH |
572 | tmp_u64 = value; |
573 | ret = set_configuration_samplerate(sdi, *tmp_u64); | |
6f5f21f9 UH |
574 | } else if (capability == HWCAP_PROBECONFIG) { |
575 | ret = configure_probes((GSList *) value); | |
576 | } else if (capability == HWCAP_LIMIT_SAMPLES) { | |
a1bb33af UH |
577 | limit_samples = strtoull(value, NULL, 10); |
578 | ret = SIGROK_OK; | |
6f5f21f9 | 579 | } else { |
e31b636d | 580 | ret = SIGROK_ERR; |
6f5f21f9 | 581 | } |
a1bb33af UH |
582 | |
583 | return ret; | |
584 | } | |
585 | ||
a1bb33af UH |
586 | static int receive_data(int fd, int revents, void *user_data) |
587 | { | |
588 | struct timeval tv; | |
589 | ||
afc8e4de UH |
590 | /* QUICK HACK */ |
591 | fd = fd; | |
592 | revents = revents; | |
593 | user_data = user_data; | |
594 | ||
a1bb33af UH |
595 | tv.tv_sec = tv.tv_usec = 0; |
596 | libusb_handle_events_timeout(usb_context, &tv); | |
597 | ||
598 | return TRUE; | |
599 | } | |
600 | ||
afc8e4de | 601 | static void trigger_helper(int i, unsigned char *cur_buf, |
08cfe6a2 UH |
602 | struct datafeed_packet *packet, void *user_data, |
603 | int *trigger_offset) | |
604 | { | |
605 | if ((cur_buf[i] & trigger_mask[trigger_stage]) | |
606 | == trigger_value[trigger_stage]) { | |
607 | /* Match on this trigger stage. */ | |
608 | trigger_buffer[trigger_stage] = cur_buf[i]; | |
609 | trigger_stage++; | |
610 | if (trigger_stage == NUM_TRIGGER_STAGES | |
611 | || trigger_mask[trigger_stage] == 0) { | |
612 | /* Match on all trigger stages, we're done. */ | |
613 | *trigger_offset = i + 1; | |
614 | ||
615 | /* | |
616 | * TODO: Send pre-trigger buffer to session bus. | |
617 | * Tell the frontend we hit the trigger here. | |
618 | */ | |
619 | packet->type = DF_TRIGGER; | |
620 | packet->length = 0; | |
621 | session_bus(user_data, packet); | |
622 | ||
623 | /* | |
624 | * Send the samples that triggered it, since we're | |
625 | * skipping past them. | |
626 | */ | |
627 | packet->type = DF_LOGIC8; | |
628 | packet->length = trigger_stage; | |
629 | packet->payload = trigger_buffer; | |
630 | session_bus(user_data, packet); | |
631 | // break; // FIXME??? | |
632 | ||
633 | trigger_stage = TRIGGER_FIRED; | |
634 | } | |
635 | return; | |
636 | } | |
637 | ||
638 | /* | |
639 | * We had a match before, but not in the next sample. However, we may | |
640 | * have a match on this stage in the next bit -- trigger on 0001 will | |
641 | * fail on seeing 00001, so we need to go back to stage 0 -- but at | |
642 | * the next sample from the one that matched originally, which the | |
643 | * counter increment at the end of the loop takes care of. | |
644 | */ | |
645 | if (trigger_stage > 0) { | |
646 | i -= trigger_stage; | |
647 | if (i < -1) | |
648 | i = -1; /* Oops, went back past this buffer. */ | |
649 | /* Reset trigger stage. */ | |
650 | trigger_stage = 0; | |
651 | } | |
652 | } | |
653 | ||
a1bb33af UH |
654 | void receive_transfer(struct libusb_transfer *transfer) |
655 | { | |
656 | static int num_samples = 0; | |
657 | static int empty_transfer_count = 0; | |
a1bb33af UH |
658 | struct datafeed_packet packet; |
659 | void *user_data; | |
660 | int cur_buflen, trigger_offset, i; | |
661 | unsigned char *cur_buf, *new_buf; | |
662 | ||
f6958dab UH |
663 | /* hw_stop_acquisition() is telling us to stop. */ |
664 | if (transfer == NULL) | |
a1bb33af | 665 | num_samples = -1; |
a1bb33af | 666 | |
f6958dab UH |
667 | /* |
668 | * If acquisition has already ended, just free any queued up | |
669 | * transfer that come in. | |
670 | */ | |
6f5f21f9 | 671 | if (num_samples == -1) { |
9c48090a BV |
672 | if (transfer) |
673 | libusb_free_transfer(transfer); | |
f6958dab UH |
674 | return; |
675 | } | |
a1bb33af | 676 | |
f6958dab UH |
677 | g_message("receive_transfer(): status %d received %d bytes", |
678 | transfer->status, transfer->actual_length); | |
679 | ||
680 | /* Save incoming transfer before reusing the transfer struct. */ | |
681 | cur_buf = transfer->buffer; | |
682 | cur_buflen = transfer->actual_length; | |
683 | user_data = transfer->user_data; | |
684 | ||
685 | /* Fire off a new request. */ | |
686 | new_buf = g_malloc(4096); | |
687 | transfer->buffer = new_buf; | |
688 | transfer->length = 4096; | |
689 | if (libusb_submit_transfer(transfer) != 0) { | |
690 | /* TODO: Stop session? */ | |
691 | g_warning("eek"); | |
692 | } | |
693 | ||
694 | if (cur_buflen == 0) { | |
695 | empty_transfer_count++; | |
696 | if (empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
697 | /* | |
698 | * The FX2 gave up. End the acquisition, the frontend | |
699 | * will work out that the samplecount is short. | |
700 | */ | |
9c48090a | 701 | hw_stop_acquisition(-1, user_data); |
6f5f21f9 | 702 | } |
f6958dab UH |
703 | return; |
704 | } else { | |
705 | empty_transfer_count = 0; | |
706 | } | |
a1bb33af | 707 | |
f6958dab UH |
708 | trigger_offset = 0; |
709 | if (trigger_stage >= 0) { | |
710 | for (i = 0; i < cur_buflen; i++) { | |
afc8e4de UH |
711 | trigger_helper(i, cur_buf, &packet, user_data, |
712 | &trigger_offset); | |
a1bb33af | 713 | } |
f6958dab | 714 | } |
a1bb33af | 715 | |
f6958dab UH |
716 | if (trigger_stage == TRIGGER_FIRED) { |
717 | /* Send the incoming transfer to the session bus. */ | |
718 | packet.type = DF_LOGIC8; | |
719 | packet.length = cur_buflen - trigger_offset; | |
720 | packet.payload = cur_buf + trigger_offset; | |
721 | session_bus(user_data, &packet); | |
722 | g_free(cur_buf); | |
723 | ||
724 | num_samples += cur_buflen; | |
afc8e4de | 725 | if ((unsigned int)num_samples > limit_samples) { |
9c48090a | 726 | hw_stop_acquisition(-1, user_data); |
a1bb33af | 727 | } |
f6958dab UH |
728 | } else { |
729 | /* | |
730 | * TODO: Buffer pre-trigger data in capture | |
731 | * ratio-sized buffer. | |
732 | */ | |
a1bb33af | 733 | } |
a1bb33af UH |
734 | } |
735 | ||
a1bb33af UH |
736 | static int hw_start_acquisition(int device_index, gpointer session_device_id) |
737 | { | |
738 | struct sigrok_device_instance *sdi; | |
739 | struct datafeed_packet *packet; | |
740 | struct datafeed_header *header; | |
741 | struct libusb_transfer *transfer; | |
742 | const struct libusb_pollfd **lupfd; | |
743 | int size, i; | |
744 | unsigned char *buf; | |
745 | ||
6f5f21f9 | 746 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) |
e31b636d | 747 | return SIGROK_ERR; |
a1bb33af UH |
748 | |
749 | packet = g_malloc(sizeof(struct datafeed_packet)); | |
750 | header = g_malloc(sizeof(struct datafeed_header)); | |
6f5f21f9 | 751 | if (!packet || !header) |
e31b636d | 752 | return SIGROK_ERR; |
a1bb33af | 753 | |
6f5f21f9 | 754 | /* Start with 2K transfer, subsequently increased to 4K. */ |
a1bb33af | 755 | size = 2048; |
6f5f21f9 | 756 | for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) { |
a1bb33af UH |
757 | buf = g_malloc(size); |
758 | transfer = libusb_alloc_transfer(0); | |
6f5f21f9 UH |
759 | libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl, |
760 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
a1bb33af | 761 | receive_transfer, session_device_id, 40); |
6f5f21f9 UH |
762 | if (libusb_submit_transfer(transfer) != 0) { |
763 | /* TODO: Free them all. */ | |
a1bb33af UH |
764 | libusb_free_transfer(transfer); |
765 | g_free(buf); | |
e31b636d | 766 | return SIGROK_ERR; |
a1bb33af UH |
767 | } |
768 | size = 4096; | |
769 | } | |
770 | ||
771 | lupfd = libusb_get_pollfds(usb_context); | |
6f5f21f9 | 772 | for (i = 0; lupfd[i]; i++) |
9c48090a | 773 | source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data, |
6f5f21f9 | 774 | NULL); |
a1bb33af UH |
775 | free(lupfd); |
776 | ||
777 | packet->type = DF_HEADER; | |
778 | packet->length = sizeof(struct datafeed_header); | |
6f5f21f9 | 779 | packet->payload = (unsigned char *)header; |
a1bb33af UH |
780 | header->feed_version = 1; |
781 | gettimeofday(&header->starttime, NULL); | |
4c100f32 | 782 | header->samplerate = cur_samplerate; |
a1bb33af UH |
783 | header->protocol_id = PROTO_RAW; |
784 | header->num_probes = NUM_PROBES; | |
785 | session_bus(session_device_id, packet); | |
786 | g_free(header); | |
787 | g_free(packet); | |
788 | ||
789 | return SIGROK_OK; | |
790 | } | |
791 | ||
6f5f21f9 | 792 | /* This stops acquisition on ALL devices, ignoring device_index. */ |
a1bb33af UH |
793 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) |
794 | { | |
795 | struct datafeed_packet packet; | |
796 | ||
afc8e4de UH |
797 | /* QUICK HACK */ |
798 | device_index = device_index; | |
799 | ||
a1bb33af UH |
800 | packet.type = DF_END; |
801 | session_bus(session_device_id, &packet); | |
802 | ||
803 | receive_transfer(NULL); | |
804 | ||
6f5f21f9 | 805 | /* TODO: Need to cancel and free any queued up transfers. */ |
a1bb33af UH |
806 | } |
807 | ||
a1bb33af UH |
808 | struct device_plugin saleae_logic_plugin_info = { |
809 | "saleae-logic", | |
810 | 1, | |
811 | hw_init, | |
812 | hw_cleanup, | |
813 | ||
814 | hw_opendev, | |
815 | hw_closedev, | |
816 | hw_get_device_info, | |
817 | hw_get_status, | |
818 | hw_get_capabilities, | |
819 | hw_set_configuration, | |
820 | hw_start_acquisition, | |
6f5f21f9 | 821 | hw_stop_acquisition, |
a1bb33af | 822 | }; |