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