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