]> sigrok.org Git - libsigrok.git/blame - src/hardware/lecroy-logicstudio/api.c
Ensure device is closed before usb_get_port_path()
[libsigrok.git] / src / hardware / lecroy-logicstudio / api.c
CommitLineData
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
b3fd0993
UH
38#define NUM_CHANNELS 16
39
05199c0a 40static const uint32_t drvopts[] = {
c7b17bcb 41 SR_CONF_LOGIC_ANALYZER,
05199c0a
UH
42};
43
44static const uint32_t devopts[] = {
c7b17bcb
TS
45 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
47 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
48};
49
50static const int32_t trigger_matches[] = {
51 SR_TRIGGER_ZERO,
52 SR_TRIGGER_ONE,
53 SR_TRIGGER_RISING,
54 SR_TRIGGER_FALLING,
55 SR_TRIGGER_EDGE,
56};
57
58static const uint64_t samplerates[] = {
59 SR_HZ(1000),
60 SR_HZ(2500),
61 SR_KHZ(5),
62 SR_KHZ(10),
63 SR_KHZ(25),
64 SR_KHZ(50),
65 SR_KHZ(100),
66 SR_KHZ(250),
67 SR_KHZ(500),
68 SR_KHZ(1000),
69 SR_KHZ(2500),
70 SR_MHZ(5),
71 SR_MHZ(10),
72 SR_MHZ(25),
73 SR_MHZ(50),
74 SR_MHZ(100),
75 SR_MHZ(250),
76 SR_MHZ(500),
77};
78
15a5bfe4
LPC
79static struct sr_dev_inst *create_device(struct sr_usb_dev_inst *usb,
80 enum sr_dev_inst_status status, int64_t fw_updated)
c7b17bcb
TS
81{
82 struct sr_dev_inst *sdi;
83 struct dev_context *devc;
84 char channel_name[8];
85 int i;
86
87 sdi = g_malloc0(sizeof(struct sr_dev_inst));
88 sdi->status = status;
89 sdi->vendor = g_strdup("LeCroy");
90 sdi->model = g_strdup("LogicStudio16");
c7b17bcb
TS
91 sdi->inst_type = SR_INST_USB;
92 sdi->conn = usb;
93
b3fd0993 94 for (i = 0; i < NUM_CHANNELS; i++) {
c7b17bcb
TS
95 snprintf(channel_name, sizeof(channel_name), "D%i", i);
96 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
97 }
98
99 devc = g_malloc0(sizeof(struct dev_context));
100
101 sdi->priv = devc;
102
103 devc->fw_updated = fw_updated;
104 devc->capture_ratio = 50;
105
106 lls_set_samplerate(sdi, SR_MHZ(500));
107
108 return sdi;
109}
110
111static GSList *scan(struct sr_dev_driver *di, GSList *options)
112{
113 struct sr_dev_inst *sdi;
114 struct drv_context *drvc;
115 struct sr_usb_dev_inst *usb;
116 struct libusb_device_descriptor des;
117 libusb_device **devlist;
118 GSList *devices;
119 char connection_id[64];
120 size_t i;
121 int r;
122
123 (void)options;
124
125 drvc = di->context;
c7b17bcb
TS
126
127 devices = NULL;
128
129 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
130
131 for (i = 0; devlist[i]; i++) {
132 libusb_get_device_descriptor(devlist[i], &des);
133
134 if (des.idVendor != LOGICSTUDIO16_VID)
135 continue;
136
137 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
138
139 usb = NULL;
140
141 switch (des.idProduct) {
142 case LOGICSTUDIO16_PID_HAVE_FIRMWARE:
143 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
144 libusb_get_device_address(devlist[i]), NULL);
145
15a5bfe4 146 sdi = create_device(usb, SR_ST_INACTIVE, 0);
c7b17bcb
TS
147 break;
148 case LOGICSTUDIO16_PID_LACK_FIRMWARE:
149 r = ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
150 USB_CONFIGURATION, FX2_FIRMWARE);
151 if (r != SR_OK) {
152 /*
153 * An error message has already been logged by
154 * ezusb_upload_firmware().
155 */
156 continue;
157 }
158
159 /*
160 * Put unknown as the address so that we know we still
161 * need to get the proper address after the device
162 * renumerates.
163 */
164 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
165 UNKNOWN_ADDRESS, NULL);
166
15a5bfe4 167 sdi = create_device(usb, SR_ST_INITIALIZING,
c7b17bcb
TS
168 g_get_monotonic_time());
169 break;
170 default:
171 break;
172 }
173
174 /* Cannot handle this device? */
175 if (!usb)
176 continue;
177
178 sdi->connection_id = g_strdup(connection_id);
179
c7b17bcb
TS
180 devices = g_slist_append(devices, sdi);
181 }
182
183 libusb_free_device_list(devlist, 1);
184
15a5bfe4 185 return std_scan_complete(di, devices);
c7b17bcb
TS
186}
187
c7b17bcb
TS
188static int open_device(struct sr_dev_inst *sdi)
189{
190 struct drv_context *drvc;
191 struct sr_usb_dev_inst *usb;
192 struct libusb_device_descriptor des;
193 libusb_device **devlist;
194 char connection_id[64];
195 bool is_opened;
196 size_t i;
197 int r;
198
c7b17bcb
TS
199 drvc = sdi->driver->context;
200 usb = sdi->conn;
201
202 is_opened = FALSE;
203
204 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
205
206 for (i = 0; devlist[i]; i++) {
207 libusb_get_device_descriptor(devlist[i], &des);
208
209 if (des.idVendor != LOGICSTUDIO16_VID ||
210 des.idProduct != LOGICSTUDIO16_PID_HAVE_FIRMWARE)
211 continue;
212
213 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
214
215 /*
216 * Check if this device is the same one that we associated
217 * with this sdi in scan() and bail if it isn't.
218 */
219 if (strcmp(sdi->connection_id, connection_id))
220 continue;
221
222 r = libusb_open(devlist[i], &usb->devhdl);
223
224 if (r) {
225 sr_err("Failed to open device: %s.",
226 libusb_error_name(r));
227 break;
228 }
229
230 /* Fix up address after firmware upload. */
231 if (usb->address == UNKNOWN_ADDRESS)
232 usb->address = libusb_get_device_address(devlist[i]);
233
234 is_opened = TRUE;
235
236 break;
237 }
238
239 libusb_free_device_list(devlist, 1);
240
241 if (!is_opened)
242 return SR_ERR;
243
244 if ((r = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
245 sr_err("Failed to claim interface: %s.",
246 libusb_error_name(r));
247 return SR_ERR;
248 }
249
250 sdi->status = SR_ST_ACTIVE;
251
252 return SR_OK;
253}
254
255static int dev_open(struct sr_dev_inst *sdi)
256{
c7b17bcb
TS
257 struct dev_context *devc;
258 int64_t timediff_us, timediff_ms;
259 int ret;
260
c7b17bcb
TS
261 devc = sdi->priv;
262
c7b17bcb
TS
263 /*
264 * If we didn't need to upload FX2 firmware in scan(), open the device
265 * right away. Otherwise, wait up to MAX_RENUM_DELAY_MS ms for the
266 * FX2 to renumerate.
267 */
268 if (!devc->fw_updated) {
269 ret = open_device(sdi);
270 } else {
271 sr_info("Waiting for device to reset.");
272
273 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
274 g_usleep(300 * 1000);
275 timediff_ms = 0;
276
277 while (timediff_ms < MAX_RENUM_DELAY_MS) {
278 ret = open_device(sdi);
279
280 if (ret == SR_OK)
281 break;
282
283 g_usleep(100 * 1000);
284
285 timediff_us = g_get_monotonic_time() - devc->fw_updated;
286 timediff_ms = timediff_us / 1000;
287
288 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
289 }
290
291 if (ret != SR_OK) {
292 sr_err("Device failed to renumerate.");
293 return SR_ERR;
294 }
295
296 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
297 }
298
299 if (ret != SR_OK) {
300 sr_err("Unable to open device.");
301 return ret;
302 }
303
304 /*
305 * Only allocate the sample buffer now since it's rather large.
306 * Don't want to allocate it before we know we are going to use it.
307 */
308 devc->fetched_samples = g_malloc(SAMPLE_BUF_SIZE);
309
310 devc->conv8to16 = g_malloc(CONV_8TO16_BUF_SIZE);
311
312 devc->intr_xfer = libusb_alloc_transfer(0);
313 devc->bulk_xfer = libusb_alloc_transfer(0);
314
315 return SR_OK;
316}
317
318static int dev_close(struct sr_dev_inst *sdi)
319{
320 struct sr_usb_dev_inst *usb;
321 struct dev_context *devc;
322
323 usb = sdi->conn;
324 devc = sdi->priv;
325
326 g_free(devc->fetched_samples);
327 devc->fetched_samples = NULL;
328
329 g_free(devc->conv8to16);
330 devc->conv8to16 = NULL;
331
332 if (devc->intr_xfer) {
333 devc->intr_xfer->buffer = NULL; /* Points into devc. */
334 libusb_free_transfer(devc->intr_xfer);
335 devc->intr_xfer = NULL;
336 }
337
338 if (devc->bulk_xfer) {
339 devc->bulk_xfer->buffer = NULL; /* Points into devc. */
340 libusb_free_transfer(devc->bulk_xfer);
341 devc->bulk_xfer = NULL;
342 }
343
344 if (!usb->devhdl)
f1ba6b4b 345 return SR_ERR_BUG;
c7b17bcb
TS
346
347 libusb_release_interface(usb->devhdl, 0);
348
349 libusb_close(usb->devhdl);
350 usb->devhdl = NULL;
351
c7b17bcb
TS
352 return SR_OK;
353}
354
c7b17bcb
TS
355static int config_get(uint32_t key, GVariant **data,
356 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
357{
358 struct dev_context *devc;
359
360 (void)cg;
361
362 if (!sdi)
363 return SR_ERR_ARG;
364
365 devc = sdi->priv;
366
367 switch (key) {
368 case SR_CONF_SAMPLERATE:
369 *data = g_variant_new_uint64(lls_get_samplerate(sdi));
370 break;
371 case SR_CONF_CAPTURE_RATIO:
372 *data = g_variant_new_uint64(devc->capture_ratio);
373 break;
374 default:
375 return SR_ERR_NA;
376 }
377
378 return SR_OK;
379}
380
381static int config_set(uint32_t key, GVariant *data,
382 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
383{
384 struct dev_context *devc;
385
386 (void)cg;
387
388 if (!sdi)
389 return SR_ERR_ARG;
390
391 devc = sdi->priv;
392
c7b17bcb
TS
393 switch (key) {
394 case SR_CONF_SAMPLERATE:
395 return lls_set_samplerate(sdi, g_variant_get_uint64(data));
396 case SR_CONF_CAPTURE_RATIO:
397 devc->capture_ratio = g_variant_get_uint64(data);
c7b17bcb
TS
398 break;
399 default:
400 return SR_ERR_NA;
401 }
402
403 return SR_OK;
404}
405
406static int config_list(uint32_t key, GVariant **data,
407 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
408{
c7b17bcb
TS
409 switch (key) {
410 case SR_CONF_DEVICE_OPTIONS:
05199c0a 411 return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
c7b17bcb 412 case SR_CONF_SAMPLERATE:
53012da6 413 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
c7b17bcb
TS
414 break;
415 case SR_CONF_TRIGGER_MATCH:
53012da6 416 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
c7b17bcb
TS
417 break;
418 default:
419 return SR_ERR_NA;
420 }
421
422 return SR_OK;
423}
424
425static int config_commit(const struct sr_dev_inst *sdi)
426{
427 return lls_setup_acquisition(sdi);
428}
429
430static int receive_usb_data(int fd, int revents, void *cb_data)
431{
432 struct drv_context *drvc;
433 struct timeval tv;
434
435 (void)fd;
436 (void)revents;
437
695dc859 438 drvc = (struct drv_context *)cb_data;
c7b17bcb
TS
439
440 tv.tv_sec = 0;
441 tv.tv_usec = 0;
442
443 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
444 &tv, NULL);
445
446 return TRUE;
447}
448
695dc859 449static int dev_acquisition_start(const struct sr_dev_inst *sdi)
c7b17bcb
TS
450{
451 struct drv_context *drvc;
452 int ret;
453
c7b17bcb
TS
454 drvc = sdi->driver->context;
455
456 if ((ret = lls_start_acquisition(sdi)) < 0)
457 return ret;
458
bee2b016 459 std_session_send_df_header(sdi);
c7b17bcb
TS
460
461 return usb_source_add(sdi->session, drvc->sr_ctx, 100,
462 receive_usb_data, drvc);
463}
464
695dc859 465static int dev_acquisition_stop(struct sr_dev_inst *sdi)
c7b17bcb 466{
c7b17bcb
TS
467 return lls_stop_acquisition(sdi);
468}
469
dd5c48a6 470static struct sr_dev_driver lecroy_logicstudio_driver_info = {
c7b17bcb
TS
471 .name = "lecroy-logicstudio",
472 .longname = "LeCroy LogicStudio",
473 .api_version = 1,
c2fdcc25 474 .init = std_init,
700d6b64 475 .cleanup = std_cleanup,
c7b17bcb 476 .scan = scan,
c01bf34c 477 .dev_list = std_dev_list,
f778bf02 478 .dev_clear = std_dev_clear,
c7b17bcb
TS
479 .config_get = config_get,
480 .config_set = config_set,
481 .config_list = config_list,
482 .config_commit = config_commit,
483 .dev_open = dev_open,
484 .dev_close = dev_close,
485 .dev_acquisition_start = dev_acquisition_start,
486 .dev_acquisition_stop = dev_acquisition_stop,
487 .context = NULL,
488};
dd5c48a6 489SR_REGISTER_DEV_DRIVER(lecroy_logicstudio_driver_info);