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