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