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