]>
Commit | Line | Data |
---|---|---|
c7b17bcb TS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Tilman Sauerbeck <tilman@code-monkey.de> | |
5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <stdbool.h> | |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include "protocol.h" | |
26 | ||
27 | #define LOGICSTUDIO16_VID 0x05ff | |
28 | #define LOGICSTUDIO16_PID_LACK_FIRMWARE 0xa001 | |
29 | #define LOGICSTUDIO16_PID_HAVE_FIRMWARE 0xa002 | |
30 | ||
31 | #define USB_INTERFACE 0 | |
32 | #define USB_CONFIGURATION 0 | |
33 | #define FX2_FIRMWARE "lecroy-logicstudio16-fx2lp.fw" | |
34 | ||
35 | #define UNKNOWN_ADDRESS 0xff | |
36 | #define MAX_RENUM_DELAY_MS 3000 | |
37 | ||
38 | static const uint32_t devopts[] = { | |
39 | SR_CONF_LOGIC_ANALYZER, | |
40 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
42 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
43 | }; | |
44 | ||
45 | static const int32_t trigger_matches[] = { | |
46 | SR_TRIGGER_ZERO, | |
47 | SR_TRIGGER_ONE, | |
48 | SR_TRIGGER_RISING, | |
49 | SR_TRIGGER_FALLING, | |
50 | SR_TRIGGER_EDGE, | |
51 | }; | |
52 | ||
53 | static const uint64_t samplerates[] = { | |
54 | SR_HZ(1000), | |
55 | SR_HZ(2500), | |
56 | SR_KHZ(5), | |
57 | SR_KHZ(10), | |
58 | SR_KHZ(25), | |
59 | SR_KHZ(50), | |
60 | SR_KHZ(100), | |
61 | SR_KHZ(250), | |
62 | SR_KHZ(500), | |
63 | SR_KHZ(1000), | |
64 | SR_KHZ(2500), | |
65 | SR_MHZ(5), | |
66 | SR_MHZ(10), | |
67 | SR_MHZ(25), | |
68 | SR_MHZ(50), | |
69 | SR_MHZ(100), | |
70 | SR_MHZ(250), | |
71 | SR_MHZ(500), | |
72 | }; | |
73 | ||
c7b17bcb TS |
74 | static struct sr_dev_inst *create_device(struct sr_dev_driver *di, |
75 | struct sr_usb_dev_inst *usb, enum sr_dev_inst_status status, | |
76 | int64_t fw_updated) | |
77 | { | |
78 | struct sr_dev_inst *sdi; | |
79 | struct dev_context *devc; | |
80 | char channel_name[8]; | |
81 | int i; | |
82 | ||
83 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
84 | sdi->status = status; | |
85 | sdi->vendor = g_strdup("LeCroy"); | |
86 | sdi->model = g_strdup("LogicStudio16"); | |
87 | sdi->driver = di; | |
88 | sdi->inst_type = SR_INST_USB; | |
89 | sdi->conn = usb; | |
90 | ||
91 | for (i = 0; i < 16; i++) { | |
92 | snprintf(channel_name, sizeof(channel_name), "D%i", i); | |
93 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name); | |
94 | } | |
95 | ||
96 | devc = g_malloc0(sizeof(struct dev_context)); | |
97 | ||
98 | sdi->priv = devc; | |
99 | ||
100 | devc->fw_updated = fw_updated; | |
101 | devc->capture_ratio = 50; | |
102 | ||
103 | lls_set_samplerate(sdi, SR_MHZ(500)); | |
104 | ||
105 | return sdi; | |
106 | } | |
107 | ||
108 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
109 | { | |
110 | struct sr_dev_inst *sdi; | |
111 | struct drv_context *drvc; | |
112 | struct sr_usb_dev_inst *usb; | |
113 | struct libusb_device_descriptor des; | |
114 | libusb_device **devlist; | |
115 | GSList *devices; | |
116 | char connection_id[64]; | |
117 | size_t i; | |
118 | int r; | |
119 | ||
120 | (void)options; | |
121 | ||
122 | drvc = di->context; | |
c7b17bcb TS |
123 | |
124 | devices = NULL; | |
125 | ||
126 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
127 | ||
128 | for (i = 0; devlist[i]; i++) { | |
129 | libusb_get_device_descriptor(devlist[i], &des); | |
130 | ||
131 | if (des.idVendor != LOGICSTUDIO16_VID) | |
132 | continue; | |
133 | ||
134 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
135 | ||
136 | usb = NULL; | |
137 | ||
138 | switch (des.idProduct) { | |
139 | case LOGICSTUDIO16_PID_HAVE_FIRMWARE: | |
140 | usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
141 | libusb_get_device_address(devlist[i]), NULL); | |
142 | ||
143 | sdi = create_device(di, usb, SR_ST_INACTIVE, 0); | |
144 | break; | |
145 | case LOGICSTUDIO16_PID_LACK_FIRMWARE: | |
146 | r = ezusb_upload_firmware(drvc->sr_ctx, devlist[i], | |
147 | USB_CONFIGURATION, FX2_FIRMWARE); | |
148 | if (r != SR_OK) { | |
149 | /* | |
150 | * An error message has already been logged by | |
151 | * ezusb_upload_firmware(). | |
152 | */ | |
153 | continue; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Put unknown as the address so that we know we still | |
158 | * need to get the proper address after the device | |
159 | * renumerates. | |
160 | */ | |
161 | usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
162 | UNKNOWN_ADDRESS, NULL); | |
163 | ||
164 | sdi = create_device(di, usb, SR_ST_INITIALIZING, | |
165 | g_get_monotonic_time()); | |
166 | break; | |
167 | default: | |
168 | break; | |
169 | } | |
170 | ||
171 | /* Cannot handle this device? */ | |
172 | if (!usb) | |
173 | continue; | |
174 | ||
175 | sdi->connection_id = g_strdup(connection_id); | |
176 | ||
177 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
178 | devices = g_slist_append(devices, sdi); | |
179 | } | |
180 | ||
181 | libusb_free_device_list(devlist, 1); | |
182 | ||
183 | return devices; | |
184 | } | |
185 | ||
c7b17bcb TS |
186 | static int open_device(struct sr_dev_inst *sdi) |
187 | { | |
188 | struct drv_context *drvc; | |
189 | struct sr_usb_dev_inst *usb; | |
190 | struct libusb_device_descriptor des; | |
191 | libusb_device **devlist; | |
192 | char connection_id[64]; | |
193 | bool is_opened; | |
194 | size_t i; | |
195 | int r; | |
196 | ||
197 | if (sdi->status == SR_ST_ACTIVE) | |
198 | return SR_ERR; | |
199 | ||
200 | drvc = sdi->driver->context; | |
201 | usb = sdi->conn; | |
202 | ||
203 | is_opened = FALSE; | |
204 | ||
205 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
206 | ||
207 | for (i = 0; devlist[i]; i++) { | |
208 | libusb_get_device_descriptor(devlist[i], &des); | |
209 | ||
210 | if (des.idVendor != LOGICSTUDIO16_VID || | |
211 | des.idProduct != LOGICSTUDIO16_PID_HAVE_FIRMWARE) | |
212 | continue; | |
213 | ||
214 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
215 | ||
216 | /* | |
217 | * Check if this device is the same one that we associated | |
218 | * with this sdi in scan() and bail if it isn't. | |
219 | */ | |
220 | if (strcmp(sdi->connection_id, connection_id)) | |
221 | continue; | |
222 | ||
223 | r = libusb_open(devlist[i], &usb->devhdl); | |
224 | ||
225 | if (r) { | |
226 | sr_err("Failed to open device: %s.", | |
227 | libusb_error_name(r)); | |
228 | break; | |
229 | } | |
230 | ||
231 | /* Fix up address after firmware upload. */ | |
232 | if (usb->address == UNKNOWN_ADDRESS) | |
233 | usb->address = libusb_get_device_address(devlist[i]); | |
234 | ||
235 | is_opened = TRUE; | |
236 | ||
237 | break; | |
238 | } | |
239 | ||
240 | libusb_free_device_list(devlist, 1); | |
241 | ||
242 | if (!is_opened) | |
243 | return SR_ERR; | |
244 | ||
245 | if ((r = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) { | |
246 | sr_err("Failed to claim interface: %s.", | |
247 | libusb_error_name(r)); | |
248 | return SR_ERR; | |
249 | } | |
250 | ||
251 | sdi->status = SR_ST_ACTIVE; | |
252 | ||
253 | return SR_OK; | |
254 | } | |
255 | ||
256 | static int dev_open(struct sr_dev_inst *sdi) | |
257 | { | |
c7b17bcb TS |
258 | struct dev_context *devc; |
259 | int64_t timediff_us, timediff_ms; | |
260 | int ret; | |
261 | ||
c7b17bcb TS |
262 | devc = sdi->priv; |
263 | ||
c7b17bcb TS |
264 | /* |
265 | * If we didn't need to upload FX2 firmware in scan(), open the device | |
266 | * right away. Otherwise, wait up to MAX_RENUM_DELAY_MS ms for the | |
267 | * FX2 to renumerate. | |
268 | */ | |
269 | if (!devc->fw_updated) { | |
270 | ret = open_device(sdi); | |
271 | } else { | |
272 | sr_info("Waiting for device to reset."); | |
273 | ||
274 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
275 | g_usleep(300 * 1000); | |
276 | timediff_ms = 0; | |
277 | ||
278 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
279 | ret = open_device(sdi); | |
280 | ||
281 | if (ret == SR_OK) | |
282 | break; | |
283 | ||
284 | g_usleep(100 * 1000); | |
285 | ||
286 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
287 | timediff_ms = timediff_us / 1000; | |
288 | ||
289 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
290 | } | |
291 | ||
292 | if (ret != SR_OK) { | |
293 | sr_err("Device failed to renumerate."); | |
294 | return SR_ERR; | |
295 | } | |
296 | ||
297 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
298 | } | |
299 | ||
300 | if (ret != SR_OK) { | |
301 | sr_err("Unable to open device."); | |
302 | return ret; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Only allocate the sample buffer now since it's rather large. | |
307 | * Don't want to allocate it before we know we are going to use it. | |
308 | */ | |
309 | devc->fetched_samples = g_malloc(SAMPLE_BUF_SIZE); | |
310 | ||
311 | devc->conv8to16 = g_malloc(CONV_8TO16_BUF_SIZE); | |
312 | ||
313 | devc->intr_xfer = libusb_alloc_transfer(0); | |
314 | devc->bulk_xfer = libusb_alloc_transfer(0); | |
315 | ||
316 | return SR_OK; | |
317 | } | |
318 | ||
319 | static int dev_close(struct sr_dev_inst *sdi) | |
320 | { | |
321 | struct sr_usb_dev_inst *usb; | |
322 | struct dev_context *devc; | |
323 | ||
324 | usb = sdi->conn; | |
325 | devc = sdi->priv; | |
326 | ||
327 | g_free(devc->fetched_samples); | |
328 | devc->fetched_samples = NULL; | |
329 | ||
330 | g_free(devc->conv8to16); | |
331 | devc->conv8to16 = NULL; | |
332 | ||
333 | if (devc->intr_xfer) { | |
334 | devc->intr_xfer->buffer = NULL; /* Points into devc. */ | |
335 | libusb_free_transfer(devc->intr_xfer); | |
336 | devc->intr_xfer = NULL; | |
337 | } | |
338 | ||
339 | if (devc->bulk_xfer) { | |
340 | devc->bulk_xfer->buffer = NULL; /* Points into devc. */ | |
341 | libusb_free_transfer(devc->bulk_xfer); | |
342 | devc->bulk_xfer = NULL; | |
343 | } | |
344 | ||
345 | if (!usb->devhdl) | |
346 | return SR_ERR; | |
347 | ||
348 | libusb_release_interface(usb->devhdl, 0); | |
349 | ||
350 | libusb_close(usb->devhdl); | |
351 | usb->devhdl = NULL; | |
352 | ||
353 | sdi->status = SR_ST_INACTIVE; | |
354 | ||
355 | return SR_OK; | |
356 | } | |
357 | ||
c7b17bcb TS |
358 | static int config_get(uint32_t key, GVariant **data, |
359 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
360 | { | |
361 | struct dev_context *devc; | |
362 | ||
363 | (void)cg; | |
364 | ||
365 | if (!sdi) | |
366 | return SR_ERR_ARG; | |
367 | ||
368 | devc = sdi->priv; | |
369 | ||
370 | switch (key) { | |
371 | case SR_CONF_SAMPLERATE: | |
372 | *data = g_variant_new_uint64(lls_get_samplerate(sdi)); | |
373 | break; | |
374 | case SR_CONF_CAPTURE_RATIO: | |
375 | *data = g_variant_new_uint64(devc->capture_ratio); | |
376 | break; | |
377 | default: | |
378 | return SR_ERR_NA; | |
379 | } | |
380 | ||
381 | return SR_OK; | |
382 | } | |
383 | ||
384 | static int config_set(uint32_t key, GVariant *data, | |
385 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
386 | { | |
387 | struct dev_context *devc; | |
388 | ||
389 | (void)cg; | |
390 | ||
391 | if (!sdi) | |
392 | return SR_ERR_ARG; | |
393 | ||
394 | devc = sdi->priv; | |
395 | ||
396 | if (sdi->status != SR_ST_ACTIVE) | |
397 | return SR_ERR_DEV_CLOSED; | |
398 | ||
399 | switch (key) { | |
400 | case SR_CONF_SAMPLERATE: | |
401 | return lls_set_samplerate(sdi, g_variant_get_uint64(data)); | |
402 | case SR_CONF_CAPTURE_RATIO: | |
403 | devc->capture_ratio = g_variant_get_uint64(data); | |
404 | if (devc->capture_ratio > 100) | |
405 | return SR_ERR; | |
406 | break; | |
407 | default: | |
408 | return SR_ERR_NA; | |
409 | } | |
410 | ||
411 | return SR_OK; | |
412 | } | |
413 | ||
414 | static int config_list(uint32_t key, GVariant **data, | |
415 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
416 | { | |
417 | GVariantBuilder vb; | |
418 | GVariant *var; | |
419 | ||
420 | (void)sdi; | |
421 | (void)cg; | |
422 | ||
423 | switch (key) { | |
424 | case SR_CONF_DEVICE_OPTIONS: | |
425 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
426 | devopts, ARRAY_SIZE(devopts), | |
427 | sizeof(uint32_t)); | |
428 | break; | |
429 | case SR_CONF_SAMPLERATE: | |
430 | g_variant_builder_init(&vb, G_VARIANT_TYPE("a{sv}")); | |
431 | var = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
432 | samplerates, ARRAY_SIZE(samplerates), | |
433 | sizeof(uint64_t)); | |
434 | g_variant_builder_add(&vb, "{sv}", "samplerates", var); | |
435 | *data = g_variant_builder_end(&vb); | |
436 | break; | |
437 | case SR_CONF_TRIGGER_MATCH: | |
438 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
439 | trigger_matches, ARRAY_SIZE(trigger_matches), | |
440 | sizeof(int32_t)); | |
441 | break; | |
442 | default: | |
443 | return SR_ERR_NA; | |
444 | } | |
445 | ||
446 | return SR_OK; | |
447 | } | |
448 | ||
449 | static int config_commit(const struct sr_dev_inst *sdi) | |
450 | { | |
451 | return lls_setup_acquisition(sdi); | |
452 | } | |
453 | ||
454 | static int receive_usb_data(int fd, int revents, void *cb_data) | |
455 | { | |
456 | struct drv_context *drvc; | |
457 | struct timeval tv; | |
458 | ||
459 | (void)fd; | |
460 | (void)revents; | |
461 | ||
695dc859 | 462 | drvc = (struct drv_context *)cb_data; |
c7b17bcb TS |
463 | |
464 | tv.tv_sec = 0; | |
465 | tv.tv_usec = 0; | |
466 | ||
467 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, | |
468 | &tv, NULL); | |
469 | ||
470 | return TRUE; | |
471 | } | |
472 | ||
695dc859 | 473 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
c7b17bcb TS |
474 | { |
475 | struct drv_context *drvc; | |
476 | int ret; | |
477 | ||
478 | if (sdi->status != SR_ST_ACTIVE) | |
479 | return SR_ERR_DEV_CLOSED; | |
480 | ||
481 | drvc = sdi->driver->context; | |
482 | ||
483 | if ((ret = lls_start_acquisition(sdi)) < 0) | |
484 | return ret; | |
485 | ||
695dc859 | 486 | std_session_send_df_header(sdi, LOG_PREFIX); |
c7b17bcb TS |
487 | |
488 | return usb_source_add(sdi->session, drvc->sr_ctx, 100, | |
489 | receive_usb_data, drvc); | |
490 | } | |
491 | ||
695dc859 | 492 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
c7b17bcb | 493 | { |
c7b17bcb TS |
494 | if (sdi->status != SR_ST_ACTIVE) |
495 | return SR_ERR_DEV_CLOSED; | |
496 | ||
497 | return lls_stop_acquisition(sdi); | |
498 | } | |
499 | ||
dd5c48a6 | 500 | static struct sr_dev_driver lecroy_logicstudio_driver_info = { |
c7b17bcb TS |
501 | .name = "lecroy-logicstudio", |
502 | .longname = "LeCroy LogicStudio", | |
503 | .api_version = 1, | |
c2fdcc25 | 504 | .init = std_init, |
700d6b64 | 505 | .cleanup = std_cleanup, |
c7b17bcb | 506 | .scan = scan, |
c01bf34c | 507 | .dev_list = std_dev_list, |
c7b17bcb TS |
508 | .config_get = config_get, |
509 | .config_set = config_set, | |
510 | .config_list = config_list, | |
511 | .config_commit = config_commit, | |
512 | .dev_open = dev_open, | |
513 | .dev_close = dev_close, | |
514 | .dev_acquisition_start = dev_acquisition_start, | |
515 | .dev_acquisition_stop = dev_acquisition_stop, | |
516 | .context = NULL, | |
517 | }; | |
dd5c48a6 | 518 | SR_REGISTER_DEV_DRIVER(lecroy_logicstudio_driver_info); |