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