]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-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 | ||
ffedd0bf | 39 | static int hwcaps[] = { |
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 | ||
e7eb703f | 90 | /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */ |
d68e2d1a | 91 | static GSList *dev_insts = NULL; |
6d754b6d | 92 | static libusb_context *usb_context = NULL; |
a1bb33af | 93 | |
003f9beb UH |
94 | static int new_saleae_logic_firmware = 0; |
95 | ||
a9a245b4 | 96 | static int hw_dev_config_set(int dev_index, int hwcap, void *value); |
69040b7c | 97 | static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_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 | ||
bb7ef793 | 168 | static int sl_open_dev(int dev_index) |
a1bb33af | 169 | { |
a1bb33af UH |
170 | libusb_device **devlist; |
171 | struct libusb_device_descriptor des; | |
d68e2d1a | 172 | struct sr_dev_inst *sdi; |
ea9cfed7 | 173 | struct context *ctx; |
a1bb33af UH |
174 | int err, skip, i; |
175 | ||
bb7ef793 | 176 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
6d754b6d | 177 | return SR_ERR; |
ea9cfed7 | 178 | ctx = 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))) { | |
7b48d6e1 | 188 | sr_err("logic: failed to get device descriptor: %d", err); |
e10d6e32 | 189 | continue; |
a1bb33af | 190 | } |
e10d6e32 | 191 | |
ea9cfed7 UH |
192 | if (des.idVendor != ctx->profile->fw_vid |
193 | || des.idProduct != ctx->profile->fw_pid) | |
e10d6e32 BV |
194 | continue; |
195 | ||
196 | if (sdi->status == SR_ST_INITIALIZING) { | |
bb7ef793 | 197 | if (skip != dev_index) { |
e10d6e32 BV |
198 | /* Skip devices of this type that aren't the one we want. */ |
199 | skip += 1; | |
200 | continue; | |
201 | } | |
202 | } else if (sdi->status == SR_ST_INACTIVE) { | |
203 | /* | |
7b48d6e1 UH |
204 | * This device is fully enumerated, so we need to find |
205 | * this device by vendor, product, bus and address. | |
e10d6e32 | 206 | */ |
ea9cfed7 UH |
207 | if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus |
208 | || libusb_get_device_address(devlist[i]) != ctx->usb->address) | |
e10d6e32 BV |
209 | /* this is not the one */ |
210 | continue; | |
211 | } | |
212 | ||
ea9cfed7 UH |
213 | if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) { |
214 | if (ctx->usb->address == 0xff) | |
e10d6e32 BV |
215 | /* |
216 | * first time we touch this device after firmware upload, | |
217 | * so we don't know the address yet. | |
218 | */ | |
ea9cfed7 | 219 | ctx->usb->address = libusb_get_device_address(devlist[i]); |
e10d6e32 BV |
220 | |
221 | sdi->status = SR_ST_ACTIVE; | |
7b48d6e1 | 222 | sr_info("logic: opened device %d on %d.%d interface %d", |
ea9cfed7 UH |
223 | sdi->index, ctx->usb->bus, |
224 | ctx->usb->address, USB_INTERFACE); | |
e10d6e32 | 225 | } else { |
7b48d6e1 | 226 | sr_err("logic: failed to open device: %d", err); |
a1bb33af | 227 | } |
6d754b6d BV |
228 | |
229 | /* if we made it here, we handled the device one way or another */ | |
230 | break; | |
a1bb33af UH |
231 | } |
232 | libusb_free_device_list(devlist, 1); | |
233 | ||
6d754b6d BV |
234 | if (sdi->status != SR_ST_ACTIVE) |
235 | return SR_ERR; | |
a1bb33af | 236 | |
6d754b6d | 237 | return SR_OK; |
a1bb33af UH |
238 | } |
239 | ||
bb7ef793 | 240 | static void close_dev(struct sr_dev_inst *sdi) |
a1bb33af | 241 | { |
ea9cfed7 | 242 | struct context *ctx; |
69890f73 | 243 | |
ea9cfed7 | 244 | ctx = sdi->priv; |
69890f73 | 245 | |
ea9cfed7 | 246 | if (ctx->usb->devhdl == NULL) |
f6958dab UH |
247 | return; |
248 | ||
7b48d6e1 | 249 | sr_info("logic: closing device %d on %d.%d interface %d", sdi->index, |
ea9cfed7 UH |
250 | ctx->usb->bus, ctx->usb->address, USB_INTERFACE); |
251 | libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE); | |
252 | libusb_close(ctx->usb->devhdl); | |
253 | ctx->usb->devhdl = NULL; | |
5a2326a7 | 254 | sdi->status = SR_ST_INACTIVE; |
a1bb33af UH |
255 | } |
256 | ||
ea9cfed7 | 257 | static int configure_probes(struct context *ctx, GSList *probes) |
a1bb33af | 258 | { |
1afe8989 | 259 | struct sr_probe *probe; |
a1bb33af UH |
260 | GSList *l; |
261 | int probe_bit, stage, i; | |
262 | char *tc; | |
263 | ||
ea9cfed7 | 264 | ctx->probe_mask = 0; |
6f5f21f9 | 265 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { |
ea9cfed7 UH |
266 | ctx->trigger_mask[i] = 0; |
267 | ctx->trigger_value[i] = 0; | |
a1bb33af UH |
268 | } |
269 | ||
270 | stage = -1; | |
6f5f21f9 | 271 | for (l = probes; l; l = l->next) { |
1afe8989 | 272 | probe = (struct sr_probe *)l->data; |
6f5f21f9 | 273 | if (probe->enabled == FALSE) |
a1bb33af UH |
274 | continue; |
275 | probe_bit = 1 << (probe->index - 1); | |
ea9cfed7 | 276 | ctx->probe_mask |= probe_bit; |
989938f6 UH |
277 | if (!(probe->trigger)) |
278 | continue; | |
279 | ||
280 | stage = 0; | |
281 | for (tc = probe->trigger; *tc; tc++) { | |
ea9cfed7 | 282 | ctx->trigger_mask[stage] |= probe_bit; |
989938f6 | 283 | if (*tc == '1') |
ea9cfed7 | 284 | ctx->trigger_value[stage] |= probe_bit; |
989938f6 UH |
285 | stage++; |
286 | if (stage > NUM_TRIGGER_STAGES) | |
e46b8fb1 | 287 | return SR_ERR; |
a1bb33af UH |
288 | } |
289 | } | |
290 | ||
6f5f21f9 UH |
291 | if (stage == -1) |
292 | /* | |
293 | * We didn't configure any triggers, make sure acquisition | |
294 | * doesn't wait for any. | |
295 | */ | |
ea9cfed7 | 296 | ctx->trigger_stage = TRIGGER_FIRED; |
a1bb33af | 297 | else |
ea9cfed7 | 298 | ctx->trigger_stage = 0; |
a1bb33af | 299 | |
e46b8fb1 | 300 | return SR_OK; |
a1bb33af UH |
301 | } |
302 | ||
ea9cfed7 | 303 | static struct context *fx2_dev_new(void) |
6d754b6d | 304 | { |
ea9cfed7 | 305 | struct context *ctx; |
6d754b6d | 306 | |
ea9cfed7 UH |
307 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
308 | sr_err("logic: %s: ctx malloc failed", __func__); | |
6d754b6d BV |
309 | return NULL; |
310 | } | |
ea9cfed7 UH |
311 | ctx->trigger_stage = TRIGGER_FIRED; |
312 | ctx->usb = NULL; | |
6d754b6d | 313 | |
ea9cfed7 | 314 | return ctx; |
6d754b6d BV |
315 | } |
316 | ||
317 | ||
a1bb33af UH |
318 | /* |
319 | * API callbacks | |
320 | */ | |
321 | ||
bb7ef793 | 322 | static int hw_init(const char *devinfo) |
a1bb33af | 323 | { |
d68e2d1a | 324 | struct sr_dev_inst *sdi; |
a1bb33af | 325 | struct libusb_device_descriptor des; |
6d754b6d | 326 | struct fx2_profile *fx2_prof; |
ea9cfed7 | 327 | struct context *ctx; |
a1bb33af | 328 | libusb_device **devlist; |
e10d6e32 | 329 | int err, devcnt, i, j; |
a1bb33af | 330 | |
17e1afcb | 331 | /* Avoid compiler warnings. */ |
bb7ef793 | 332 | (void)devinfo; |
afc8e4de | 333 | |
6f5f21f9 | 334 | if (libusb_init(&usb_context) != 0) { |
7b48d6e1 | 335 | sr_err("logic: Failed to initialize USB."); |
a1bb33af UH |
336 | return 0; |
337 | } | |
a1bb33af | 338 | |
6f5f21f9 | 339 | /* Find all Saleae Logic devices and upload firmware to all of them. */ |
a1bb33af UH |
340 | devcnt = 0; |
341 | libusb_get_device_list(usb_context, &devlist); | |
6f5f21f9 | 342 | for (i = 0; devlist[i]; i++) { |
6d754b6d | 343 | fx2_prof = NULL; |
a1bb33af | 344 | err = libusb_get_device_descriptor(devlist[i], &des); |
6f5f21f9 | 345 | if (err != 0) { |
7b48d6e1 UH |
346 | sr_err("logic: failed to get device descriptor: %d", |
347 | err); | |
a1bb33af UH |
348 | continue; |
349 | } | |
350 | ||
e10d6e32 BV |
351 | for (j = 0; supported_fx2[j].orig_vid; j++) { |
352 | if (des.idVendor == supported_fx2[j].orig_vid | |
353 | && des.idProduct == supported_fx2[j].orig_pid) { | |
6d754b6d | 354 | fx2_prof = &supported_fx2[j]; |
e10d6e32 BV |
355 | break; |
356 | } | |
357 | } | |
6d754b6d | 358 | if (!fx2_prof) |
e10d6e32 BV |
359 | /* not a supported VID/PID */ |
360 | continue; | |
a1bb33af | 361 | |
d3683c42 | 362 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, |
6d754b6d | 363 | fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version); |
f6958dab UH |
364 | if (!sdi) |
365 | return 0; | |
ea9cfed7 UH |
366 | ctx = fx2_dev_new(); |
367 | ctx->profile = fx2_prof; | |
368 | sdi->priv = ctx; | |
d68e2d1a | 369 | dev_insts = g_slist_append(dev_insts, sdi); |
6f5f21f9 | 370 | |
6d754b6d | 371 | if (check_conf_profile(devlist[i])) { |
f6958dab | 372 | /* Already has the firmware, so fix the new address. */ |
7b48d6e1 | 373 | sr_dbg("logic: Found a Saleae Logic with %s firmware.", |
003f9beb | 374 | new_saleae_logic_firmware ? "new" : "old"); |
e10d6e32 | 375 | sdi->status = SR_ST_INACTIVE; |
ea9cfed7 | 376 | ctx->usb = sr_usb_dev_inst_new |
f6958dab | 377 | (libusb_get_bus_number(devlist[i]), |
fed16f06 | 378 | libusb_get_device_address(devlist[i]), NULL); |
6d754b6d BV |
379 | } else { |
380 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK) | |
381 | /* Remember when the firmware on this device was updated */ | |
ea9cfed7 | 382 | g_get_current_time(&ctx->fw_updated); |
6d754b6d | 383 | else |
7b48d6e1 UH |
384 | sr_err("logic: firmware upload failed for " |
385 | "device %d", devcnt); | |
ea9cfed7 | 386 | ctx->usb = sr_usb_dev_inst_new |
6d754b6d | 387 | (libusb_get_bus_number(devlist[i]), 0xff, NULL); |
a1bb33af | 388 | } |
f6958dab | 389 | devcnt++; |
a1bb33af UH |
390 | } |
391 | libusb_free_device_list(devlist, 1); | |
392 | ||
393 | return devcnt; | |
394 | } | |
395 | ||
e7eb703f | 396 | static int hw_dev_open(int dev_index) |
a1bb33af UH |
397 | { |
398 | GTimeVal cur_time; | |
d68e2d1a | 399 | struct sr_dev_inst *sdi; |
ea9cfed7 | 400 | struct context *ctx; |
a1bb33af | 401 | int timediff, err; |
e10d6e32 | 402 | |
bb7ef793 | 403 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
6d754b6d | 404 | return SR_ERR; |
ea9cfed7 | 405 | ctx = sdi->priv; |
6d754b6d | 406 | |
e10d6e32 BV |
407 | /* |
408 | * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms | |
409 | * for the FX2 to renumerate | |
410 | */ | |
6d754b6d | 411 | err = 0; |
ea9cfed7 | 412 | if (GTV_TO_MSEC(ctx->fw_updated) > 0) { |
7b48d6e1 | 413 | sr_info("logic: waiting for device to reset"); |
e10d6e32 | 414 | /* takes at least 300ms for the FX2 to be gone from the USB bus */ |
7b48d6e1 | 415 | g_usleep(300 * 1000); |
e10d6e32 BV |
416 | timediff = 0; |
417 | while (timediff < MAX_RENUM_DELAY) { | |
bb7ef793 | 418 | if ((err = sl_open_dev(dev_index)) == SR_OK) |
e10d6e32 | 419 | break; |
7b48d6e1 | 420 | g_usleep(100 * 1000); |
e10d6e32 | 421 | g_get_current_time(&cur_time); |
ea9cfed7 | 422 | timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated); |
a1bb33af | 423 | } |
7b48d6e1 | 424 | sr_info("logic: device came back after %d ms", timediff); |
e10d6e32 | 425 | } else { |
bb7ef793 | 426 | err = sl_open_dev(dev_index); |
a1bb33af UH |
427 | } |
428 | ||
6d754b6d | 429 | if (err != SR_OK) { |
7b48d6e1 | 430 | sr_err("logic: unable to open device"); |
e46b8fb1 | 431 | return SR_ERR; |
a1bb33af | 432 | } |
ea9cfed7 | 433 | ctx = sdi->priv; |
a1bb33af | 434 | |
ea9cfed7 | 435 | err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE); |
6f5f21f9 | 436 | if (err != 0) { |
7b48d6e1 | 437 | sr_err("logic: Unable to claim interface: %d", err); |
e46b8fb1 | 438 | return SR_ERR; |
a1bb33af UH |
439 | } |
440 | ||
ea9cfed7 | 441 | if (ctx->cur_samplerate == 0) { |
6f5f21f9 | 442 | /* Samplerate hasn't been set; default to the slowest one. */ |
a9a245b4 | 443 | if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE, |
e46b8fb1 UH |
444 | &supported_samplerates[0]) == SR_ERR) |
445 | return SR_ERR; | |
a1bb33af UH |
446 | } |
447 | ||
e46b8fb1 | 448 | return SR_OK; |
a1bb33af UH |
449 | } |
450 | ||
e7eb703f | 451 | static int hw_dev_close(int dev_index) |
a1bb33af | 452 | { |
d68e2d1a | 453 | struct sr_dev_inst *sdi; |
a1bb33af | 454 | |
bb7ef793 | 455 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { |
697785d1 UH |
456 | sr_err("logic: %s: sdi was NULL", __func__); |
457 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
458 | } | |
459 | ||
460 | /* TODO */ | |
bb7ef793 | 461 | close_dev(sdi); |
697785d1 UH |
462 | |
463 | return SR_OK; | |
a1bb33af UH |
464 | } |
465 | ||
57ab7d9f | 466 | static int hw_cleanup(void) |
a1bb33af UH |
467 | { |
468 | GSList *l; | |
d68e2d1a | 469 | struct sr_dev_inst *sdi; |
ea9cfed7 | 470 | struct context *ctx; |
57ab7d9f | 471 | int ret = SR_OK; |
a1bb33af | 472 | |
69890f73 | 473 | /* Properly close and free all devices. */ |
d68e2d1a | 474 | for (l = dev_insts; l; l = l->next) { |
57ab7d9f UH |
475 | if (!(sdi = l->data)) { |
476 | /* Log error, but continue cleaning up the rest. */ | |
7b48d6e1 | 477 | sr_err("logic: %s: sdi was NULL, continuing", __func__); |
57ab7d9f UH |
478 | ret = SR_ERR_BUG; |
479 | continue; | |
480 | } | |
ea9cfed7 | 481 | if (!(ctx = sdi->priv)) { |
57ab7d9f | 482 | /* Log error, but continue cleaning up the rest. */ |
7b48d6e1 | 483 | sr_err("logic: %s: sdi->priv was NULL, continuing", |
57ab7d9f UH |
484 | __func__); |
485 | ret = SR_ERR_BUG; | |
486 | continue; | |
487 | } | |
bb7ef793 | 488 | close_dev(sdi); |
ea9cfed7 | 489 | sr_usb_dev_inst_free(ctx->usb); |
d3683c42 | 490 | sr_dev_inst_free(sdi); |
69890f73 | 491 | } |
a1bb33af | 492 | |
d68e2d1a UH |
493 | g_slist_free(dev_insts); |
494 | dev_insts = NULL; | |
a1bb33af | 495 | |
6f5f21f9 | 496 | if (usb_context) |
a1bb33af UH |
497 | libusb_exit(usb_context); |
498 | usb_context = NULL; | |
57ab7d9f UH |
499 | |
500 | return ret; | |
a1bb33af UH |
501 | } |
502 | ||
5097b0d0 | 503 | static void *hw_dev_info_get(int dev_index, int dev_info_id) |
a1bb33af | 504 | { |
d68e2d1a | 505 | struct sr_dev_inst *sdi; |
ea9cfed7 | 506 | struct context *ctx; |
6f5f21f9 | 507 | void *info = NULL; |
a1bb33af | 508 | |
bb7ef793 | 509 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
a1bb33af | 510 | return NULL; |
ea9cfed7 | 511 | ctx = sdi->priv; |
a1bb33af | 512 | |
bb7ef793 | 513 | switch (dev_info_id) { |
1d9a8a5f | 514 | case SR_DI_INST: |
a1bb33af UH |
515 | info = sdi; |
516 | break; | |
5a2326a7 | 517 | case SR_DI_NUM_PROBES: |
ea9cfed7 | 518 | info = GINT_TO_POINTER(ctx->profile->num_probes); |
a1bb33af | 519 | break; |
464d12c7 KS |
520 | case SR_DI_PROBE_NAMES: |
521 | info = probe_names; | |
522 | break; | |
5a2326a7 | 523 | case SR_DI_SAMPLERATES: |
a1bb33af UH |
524 | info = &samplerates; |
525 | break; | |
5a2326a7 | 526 | case SR_DI_TRIGGER_TYPES: |
a1bb33af UH |
527 | info = TRIGGER_TYPES; |
528 | break; | |
5a2326a7 | 529 | case SR_DI_CUR_SAMPLERATE: |
ea9cfed7 | 530 | info = &ctx->cur_samplerate; |
a1bb33af UH |
531 | break; |
532 | } | |
533 | ||
534 | return info; | |
535 | } | |
536 | ||
e7eb703f | 537 | static int hw_dev_status_get(int dev_index) |
a1bb33af | 538 | { |
d68e2d1a | 539 | struct sr_dev_inst *sdi; |
a1bb33af | 540 | |
bb7ef793 | 541 | sdi = sr_dev_inst_get(dev_insts, dev_index); |
6f5f21f9 | 542 | if (sdi) |
a1bb33af UH |
543 | return sdi->status; |
544 | else | |
5a2326a7 | 545 | return SR_ST_NOT_FOUND; |
a1bb33af UH |
546 | } |
547 | ||
ffedd0bf | 548 | static int *hw_hwcap_get_all(void) |
a1bb33af | 549 | { |
ffedd0bf | 550 | return hwcaps; |
a1bb33af UH |
551 | } |
552 | ||
003f9beb UH |
553 | static uint8_t new_firmware_divider_value(uint64_t samplerate) |
554 | { | |
555 | switch (samplerate) { | |
556 | case SR_MHZ(24): | |
557 | return 0xe0; | |
558 | break; | |
559 | case SR_MHZ(16): | |
560 | return 0xd5; | |
561 | break; | |
562 | case SR_MHZ(12): | |
563 | return 0xe2; | |
564 | break; | |
565 | case SR_MHZ(8): | |
566 | return 0xd4; | |
567 | break; | |
568 | case SR_MHZ(4): | |
569 | return 0xda; | |
570 | break; | |
571 | case SR_MHZ(2): | |
572 | return 0xe6; | |
573 | break; | |
574 | case SR_MHZ(1): | |
575 | return 0x8e; | |
576 | break; | |
577 | case SR_KHZ(500): | |
578 | return 0xfe; | |
579 | break; | |
580 | case SR_KHZ(250): | |
581 | return 0x9e; | |
582 | break; | |
583 | case SR_KHZ(200): | |
584 | return 0x4e; | |
585 | break; | |
586 | } | |
587 | ||
588 | /* Shouldn't happen. */ | |
7b48d6e1 | 589 | sr_err("logic: %s: Invalid samplerate %" PRIu64 "", |
003f9beb UH |
590 | __func__, samplerate); |
591 | return 0; | |
592 | } | |
593 | ||
a9a245b4 | 594 | static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate) |
a1bb33af | 595 | { |
ea9cfed7 | 596 | struct context *ctx; |
a1bb33af UH |
597 | uint8_t divider; |
598 | int ret, result, i; | |
599 | unsigned char buf[2]; | |
600 | ||
ea9cfed7 | 601 | ctx = sdi->priv; |
6f5f21f9 UH |
602 | for (i = 0; supported_samplerates[i]; i++) { |
603 | if (supported_samplerates[i] == samplerate) | |
a1bb33af UH |
604 | break; |
605 | } | |
6f5f21f9 | 606 | if (supported_samplerates[i] == 0) |
e46b8fb1 | 607 | return SR_ERR_SAMPLERATE; |
a1bb33af | 608 | |
003f9beb UH |
609 | if (new_saleae_logic_firmware) |
610 | divider = new_firmware_divider_value(samplerate); | |
611 | else | |
612 | divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1; | |
a1bb33af | 613 | |
7b48d6e1 | 614 | sr_info("logic: setting samplerate to %" PRIu64 " Hz (divider %d)", |
b08024a8 | 615 | samplerate, divider); |
003f9beb UH |
616 | |
617 | buf[0] = (new_saleae_logic_firmware) ? 0xd5 : 0x01; | |
a1bb33af | 618 | buf[1] = divider; |
ea9cfed7 | 619 | ret = libusb_bulk_transfer(ctx->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT, |
6f5f21f9 UH |
620 | buf, 2, &result, 500); |
621 | if (ret != 0) { | |
7b48d6e1 | 622 | sr_err("logic: failed to set samplerate: %d", ret); |
e46b8fb1 | 623 | return SR_ERR; |
a1bb33af | 624 | } |
ea9cfed7 | 625 | ctx->cur_samplerate = samplerate; |
a1bb33af | 626 | |
e46b8fb1 | 627 | return SR_OK; |
a1bb33af UH |
628 | } |
629 | ||
a9a245b4 | 630 | static int hw_dev_config_set(int dev_index, int hwcap, void *value) |
a1bb33af | 631 | { |
d68e2d1a | 632 | struct sr_dev_inst *sdi; |
ea9cfed7 | 633 | struct context *ctx; |
a1bb33af | 634 | int ret; |
a1bb33af | 635 | |
bb7ef793 | 636 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
e46b8fb1 | 637 | return SR_ERR; |
ea9cfed7 | 638 | ctx = sdi->priv; |
a1bb33af | 639 | |
ffedd0bf | 640 | if (hwcap == SR_HWCAP_SAMPLERATE) { |
ee61b340 | 641 | ret = set_samplerate(sdi, *(uint64_t *)value); |
ffedd0bf | 642 | } else if (hwcap == SR_HWCAP_PROBECONFIG) { |
ea9cfed7 | 643 | ret = configure_probes(ctx, (GSList *) value); |
ffedd0bf | 644 | } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) { |
ea9cfed7 | 645 | ctx->limit_samples = *(uint64_t *)value; |
e46b8fb1 | 646 | ret = SR_OK; |
6f5f21f9 | 647 | } else { |
e46b8fb1 | 648 | ret = SR_ERR; |
6f5f21f9 | 649 | } |
a1bb33af UH |
650 | |
651 | return ret; | |
652 | } | |
653 | ||
a1bb33af UH |
654 | static int receive_data(int fd, int revents, void *user_data) |
655 | { | |
656 | struct timeval tv; | |
657 | ||
17e1afcb | 658 | /* Avoid compiler warnings. */ |
cb93f8a9 UH |
659 | (void)fd; |
660 | (void)revents; | |
661 | (void)user_data; | |
afc8e4de | 662 | |
a1bb33af UH |
663 | tv.tv_sec = tv.tv_usec = 0; |
664 | libusb_handle_events_timeout(usb_context, &tv); | |
665 | ||
666 | return TRUE; | |
667 | } | |
668 | ||
a0ecd83b | 669 | static void receive_transfer(struct libusb_transfer *transfer) |
a1bb33af | 670 | { |
ea9cfed7 | 671 | /* TODO: These statics have to move to the ctx struct. */ |
a1bb33af UH |
672 | static int num_samples = 0; |
673 | static int empty_transfer_count = 0; | |
b9c735a2 | 674 | struct sr_datafeed_packet packet; |
9c939c51 | 675 | struct sr_datafeed_logic logic; |
ea9cfed7 | 676 | struct context *ctx; |
a1bb33af UH |
677 | int cur_buflen, trigger_offset, i; |
678 | unsigned char *cur_buf, *new_buf; | |
679 | ||
69040b7c | 680 | /* hw_dev_acquisition_stop() is telling us to stop. */ |
f6958dab | 681 | if (transfer == NULL) |
a1bb33af | 682 | num_samples = -1; |
a1bb33af | 683 | |
f6958dab UH |
684 | /* |
685 | * If acquisition has already ended, just free any queued up | |
686 | * transfer that come in. | |
687 | */ | |
6f5f21f9 | 688 | if (num_samples == -1) { |
9c48090a BV |
689 | if (transfer) |
690 | libusb_free_transfer(transfer); | |
f6958dab UH |
691 | return; |
692 | } | |
a1bb33af | 693 | |
7b48d6e1 | 694 | sr_info("logic: receive_transfer(): status %d received %d bytes", |
b08024a8 | 695 | transfer->status, transfer->actual_length); |
f6958dab UH |
696 | |
697 | /* Save incoming transfer before reusing the transfer struct. */ | |
698 | cur_buf = transfer->buffer; | |
699 | cur_buflen = transfer->actual_length; | |
ea9cfed7 | 700 | ctx = transfer->user_data; |
f6958dab UH |
701 | |
702 | /* Fire off a new request. */ | |
b53738ba | 703 | if (!(new_buf = g_try_malloc(4096))) { |
7b48d6e1 | 704 | sr_err("logic: %s: new_buf malloc failed", __func__); |
133a37bf | 705 | return; /* TODO: SR_ERR_MALLOC */ |
b53738ba UH |
706 | } |
707 | ||
f6958dab UH |
708 | transfer->buffer = new_buf; |
709 | transfer->length = 4096; | |
710 | if (libusb_submit_transfer(transfer) != 0) { | |
711 | /* TODO: Stop session? */ | |
7b48d6e1 UH |
712 | /* TODO: Better error message. */ |
713 | sr_err("logic: %s: libusb_submit_transfer error", __func__); | |
f6958dab UH |
714 | } |
715 | ||
716 | if (cur_buflen == 0) { | |
717 | empty_transfer_count++; | |
718 | if (empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
719 | /* | |
720 | * The FX2 gave up. End the acquisition, the frontend | |
721 | * will work out that the samplecount is short. | |
722 | */ | |
ea9cfed7 | 723 | hw_dev_acquisition_stop(-1, ctx->session_data); |
6f5f21f9 | 724 | } |
f6958dab UH |
725 | return; |
726 | } else { | |
727 | empty_transfer_count = 0; | |
728 | } | |
a1bb33af | 729 | |
f6958dab | 730 | trigger_offset = 0; |
ea9cfed7 | 731 | if (ctx->trigger_stage >= 0) { |
f6958dab | 732 | for (i = 0; i < cur_buflen; i++) { |
b5698bd7 | 733 | |
ea9cfed7 | 734 | if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) { |
b5698bd7 | 735 | /* Match on this trigger stage. */ |
ea9cfed7 UH |
736 | ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i]; |
737 | ctx->trigger_stage++; | |
a634574e | 738 | |
ea9cfed7 | 739 | if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) { |
b5698bd7 BV |
740 | /* Match on all trigger stages, we're done. */ |
741 | trigger_offset = i + 1; | |
742 | ||
743 | /* | |
744 | * TODO: Send pre-trigger buffer to session bus. | |
745 | * Tell the frontend we hit the trigger here. | |
746 | */ | |
5a2326a7 | 747 | packet.type = SR_DF_TRIGGER; |
9c939c51 | 748 | packet.payload = NULL; |
ea9cfed7 | 749 | sr_session_bus(ctx->session_data, &packet); |
b5698bd7 BV |
750 | |
751 | /* | |
752 | * Send the samples that triggered it, since we're | |
753 | * skipping past them. | |
754 | */ | |
5a2326a7 | 755 | packet.type = SR_DF_LOGIC; |
9c939c51 | 756 | packet.payload = &logic; |
ea9cfed7 | 757 | logic.length = ctx->trigger_stage; |
9c939c51 | 758 | logic.unitsize = 1; |
ea9cfed7 UH |
759 | logic.data = ctx->trigger_buffer; |
760 | sr_session_bus(ctx->session_data, &packet); | |
b5698bd7 | 761 | |
ea9cfed7 | 762 | ctx->trigger_stage = TRIGGER_FIRED; |
b5698bd7 BV |
763 | break; |
764 | } | |
765 | return; | |
766 | } | |
767 | ||
768 | /* | |
769 | * We had a match before, but not in the next sample. However, we may | |
770 | * have a match on this stage in the next bit -- trigger on 0001 will | |
771 | * fail on seeing 00001, so we need to go back to stage 0 -- but at | |
772 | * the next sample from the one that matched originally, which the | |
773 | * counter increment at the end of the loop takes care of. | |
774 | */ | |
ea9cfed7 UH |
775 | if (ctx->trigger_stage > 0) { |
776 | i -= ctx->trigger_stage; | |
b5698bd7 BV |
777 | if (i < -1) |
778 | i = -1; /* Oops, went back past this buffer. */ | |
779 | /* Reset trigger stage. */ | |
ea9cfed7 | 780 | ctx->trigger_stage = 0; |
b5698bd7 | 781 | } |
a1bb33af | 782 | } |
f6958dab | 783 | } |
a1bb33af | 784 | |
ea9cfed7 | 785 | if (ctx->trigger_stage == TRIGGER_FIRED) { |
f6958dab | 786 | /* Send the incoming transfer to the session bus. */ |
5a2326a7 | 787 | packet.type = SR_DF_LOGIC; |
9c939c51 BV |
788 | packet.payload = &logic; |
789 | logic.length = cur_buflen - trigger_offset; | |
790 | logic.unitsize = 1; | |
791 | logic.data = cur_buf + trigger_offset; | |
ea9cfed7 | 792 | sr_session_bus(ctx->session_data, &packet); |
f6958dab UH |
793 | g_free(cur_buf); |
794 | ||
795 | num_samples += cur_buflen; | |
ea9cfed7 UH |
796 | if (ctx->limit_samples && (unsigned int) num_samples > ctx->limit_samples) { |
797 | hw_dev_acquisition_stop(-1, ctx->session_data); | |
a1bb33af | 798 | } |
f6958dab UH |
799 | } else { |
800 | /* | |
801 | * TODO: Buffer pre-trigger data in capture | |
802 | * ratio-sized buffer. | |
803 | */ | |
a1bb33af | 804 | } |
a1bb33af UH |
805 | } |
806 | ||
69040b7c | 807 | static int hw_dev_acquisition_start(int dev_index, gpointer session_data) |
a1bb33af | 808 | { |
d68e2d1a | 809 | struct sr_dev_inst *sdi; |
b9c735a2 UH |
810 | struct sr_datafeed_packet *packet; |
811 | struct sr_datafeed_header *header; | |
ea9cfed7 | 812 | struct context *ctx; |
a1bb33af UH |
813 | struct libusb_transfer *transfer; |
814 | const struct libusb_pollfd **lupfd; | |
815 | int size, i; | |
816 | unsigned char *buf; | |
817 | ||
bb7ef793 | 818 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
e46b8fb1 | 819 | return SR_ERR; |
ea9cfed7 UH |
820 | ctx = sdi->priv; |
821 | ctx->session_data = session_data; | |
a1bb33af | 822 | |
b53738ba | 823 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { |
7b48d6e1 | 824 | sr_err("logic: %s: packet malloc failed", __func__); |
b53738ba UH |
825 | return SR_ERR_MALLOC; |
826 | } | |
9c939c51 | 827 | |
b53738ba | 828 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { |
7b48d6e1 | 829 | sr_err("logic: %s: header malloc failed", __func__); |
b53738ba UH |
830 | return SR_ERR_MALLOC; |
831 | } | |
a1bb33af | 832 | |
6f5f21f9 | 833 | /* Start with 2K transfer, subsequently increased to 4K. */ |
a1bb33af | 834 | size = 2048; |
6f5f21f9 | 835 | for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) { |
b53738ba | 836 | if (!(buf = g_try_malloc(size))) { |
7b48d6e1 | 837 | sr_err("logic: %s: buf malloc failed", __func__); |
b53738ba UH |
838 | return SR_ERR_MALLOC; |
839 | } | |
a1bb33af | 840 | transfer = libusb_alloc_transfer(0); |
ea9cfed7 | 841 | libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl, |
6f5f21f9 | 842 | 2 | LIBUSB_ENDPOINT_IN, buf, size, |
ea9cfed7 | 843 | receive_transfer, ctx, 40); |
6f5f21f9 UH |
844 | if (libusb_submit_transfer(transfer) != 0) { |
845 | /* TODO: Free them all. */ | |
a1bb33af UH |
846 | libusb_free_transfer(transfer); |
847 | g_free(buf); | |
e46b8fb1 | 848 | return SR_ERR; |
a1bb33af UH |
849 | } |
850 | size = 4096; | |
851 | } | |
852 | ||
853 | lupfd = libusb_get_pollfds(usb_context); | |
6f5f21f9 | 854 | for (i = 0; lupfd[i]; i++) |
6f1be0a2 UH |
855 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data, |
856 | NULL); | |
133a37bf | 857 | free(lupfd); /* NOT g_free()! */ |
a1bb33af | 858 | |
5a2326a7 | 859 | packet->type = SR_DF_HEADER; |
9c939c51 | 860 | packet->payload = header; |
a1bb33af UH |
861 | header->feed_version = 1; |
862 | gettimeofday(&header->starttime, NULL); | |
ea9cfed7 UH |
863 | header->samplerate = ctx->cur_samplerate; |
864 | header->num_logic_probes = ctx->profile->num_probes; | |
6d754b6d | 865 | sr_session_bus(session_data, packet); |
a1bb33af UH |
866 | g_free(header); |
867 | g_free(packet); | |
868 | ||
e46b8fb1 | 869 | return SR_OK; |
a1bb33af UH |
870 | } |
871 | ||
bb7ef793 | 872 | /* This stops acquisition on ALL devices, ignoring dev_index. */ |
69040b7c | 873 | static int hw_dev_acquisition_stop(int dev_index, gpointer session_data) |
a1bb33af | 874 | { |
b9c735a2 | 875 | struct sr_datafeed_packet packet; |
a1bb33af | 876 | |
17e1afcb | 877 | /* Avoid compiler warnings. */ |
bb7ef793 | 878 | (void)dev_index; |
afc8e4de | 879 | |
5a2326a7 | 880 | packet.type = SR_DF_END; |
6d754b6d | 881 | sr_session_bus(session_data, &packet); |
a1bb33af UH |
882 | |
883 | receive_transfer(NULL); | |
884 | ||
6f5f21f9 | 885 | /* TODO: Need to cancel and free any queued up transfers. */ |
3010f21c UH |
886 | |
887 | return SR_OK; | |
a1bb33af UH |
888 | } |
889 | ||
bb7ef793 | 890 | SR_PRIV struct sr_dev_plugin saleae_logic_plugin_info = { |
e519ba86 UH |
891 | .name = "saleae-logic", |
892 | .longname = "Saleae Logic", | |
893 | .api_version = 1, | |
894 | .init = hw_init, | |
895 | .cleanup = hw_cleanup, | |
e7eb703f UH |
896 | .dev_open = hw_dev_open, |
897 | .dev_close = hw_dev_close, | |
5097b0d0 | 898 | .dev_info_get = hw_dev_info_get, |
e7eb703f | 899 | .dev_status_get = hw_dev_status_get, |
ffedd0bf | 900 | .hwcap_get_all = hw_hwcap_get_all, |
a9a245b4 | 901 | .dev_config_set = hw_dev_config_set, |
69040b7c UH |
902 | .dev_acquisition_start = hw_dev_acquisition_start, |
903 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
a1bb33af | 904 | }; |