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