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