]> sigrok.org Git - libsigrok.git/blob - src/hardware/fx2lafw/api.c
drivers: Factor out std_*_idx*().
[libsigrok.git] / src / hardware / fx2lafw / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 "protocol.h"
23 #include <math.h>
24
25 static const struct fx2lafw_profile supported_fx2[] = {
26         /*
27          * CWAV USBee AX
28          * EE Electronics ESLA201A
29          * ARMFLY AX-Pro
30          */
31         { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
32                 "fx2lafw-cwav-usbeeax.fw",
33                 DEV_CAPS_AX_ANALOG, NULL, NULL},
34         /*
35          * CWAV USBee DX
36          * XZL-Studio DX
37          */
38         { 0x08a9, 0x0015, "CWAV", "USBee DX", NULL,
39                 "fx2lafw-cwav-usbeedx.fw",
40                 DEV_CAPS_16BIT, NULL, NULL },
41
42         /*
43          * CWAV USBee SX
44          */
45         { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
46                 "fx2lafw-cwav-usbeesx.fw",
47                 0, NULL, NULL},
48
49         /*
50          * CWAV USBee ZX
51          */
52         { 0x08a9, 0x0005, "CWAV", "USBee ZX", NULL,
53                 "fx2lafw-cwav-usbeezx.fw",
54                 0, NULL, NULL},
55
56         /*
57          * Saleae Logic
58          * EE Electronics ESLA100
59          * Robomotic MiniLogic
60          * Robomotic BugLogic 3
61          */
62         { 0x0925, 0x3881, "Saleae", "Logic", NULL,
63                 "fx2lafw-saleae-logic.fw",
64                 0, NULL, NULL},
65
66         /*
67          * Default Cypress FX2 without EEPROM, e.g.:
68          * Lcsoft Mini Board
69          * Braintechnology USB Interface V2.x
70          */
71         { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
72                 "fx2lafw-cypress-fx2.fw",
73                 DEV_CAPS_16BIT, NULL, NULL },
74
75         /*
76          * Braintechnology USB-LPS
77          */
78         { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
79                 "fx2lafw-braintechnology-usb-lps.fw",
80                 DEV_CAPS_16BIT, NULL, NULL },
81
82         /*
83          * sigrok FX2 based 8-channel logic analyzer
84          */
85         { 0x1d50, 0x608c, "sigrok", "FX2 LA (8ch)", NULL,
86                 "fx2lafw-sigrok-fx2-8ch.fw",
87                 0, NULL, NULL},
88
89         /*
90          * sigrok FX2 based 16-channel logic analyzer
91          */
92         { 0x1d50, 0x608d, "sigrok", "FX2 LA (16ch)", NULL,
93                 "fx2lafw-sigrok-fx2-16ch.fw",
94                 DEV_CAPS_16BIT, NULL, NULL },
95
96         ALL_ZERO
97 };
98
99 static const uint32_t scanopts[] = {
100         SR_CONF_CONN,
101 };
102
103 static const uint32_t drvopts[] = {
104         SR_CONF_LOGIC_ANALYZER,
105 };
106
107 static const uint32_t devopts[] = {
108         SR_CONF_CONTINUOUS,
109         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
110         SR_CONF_CONN | SR_CONF_GET,
111         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
112         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
113         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
114 };
115
116 static const int32_t trigger_matches[] = {
117         SR_TRIGGER_ZERO,
118         SR_TRIGGER_ONE,
119         SR_TRIGGER_RISING,
120         SR_TRIGGER_FALLING,
121         SR_TRIGGER_EDGE,
122 };
123
124 static const uint64_t samplerates[] = {
125         SR_KHZ(20),
126         SR_KHZ(25),
127         SR_KHZ(50),
128         SR_KHZ(100),
129         SR_KHZ(200),
130         SR_KHZ(250),
131         SR_KHZ(500),
132         SR_MHZ(1),
133         SR_MHZ(2),
134         SR_MHZ(3),
135         SR_MHZ(4),
136         SR_MHZ(6),
137         SR_MHZ(8),
138         SR_MHZ(12),
139         SR_MHZ(16),
140         SR_MHZ(24),
141 };
142
143 static gboolean is_plausible(const struct libusb_device_descriptor *des)
144 {
145         int i;
146
147         for (i = 0; supported_fx2[i].vid; i++) {
148                 if (des->idVendor != supported_fx2[i].vid)
149                         continue;
150                 if (des->idProduct == supported_fx2[i].pid)
151                         return TRUE;
152         }
153
154         return FALSE;
155 }
156
157 static GSList *scan(struct sr_dev_driver *di, GSList *options)
158 {
159         struct drv_context *drvc;
160         struct dev_context *devc;
161         struct sr_dev_inst *sdi;
162         struct sr_usb_dev_inst *usb;
163         struct sr_channel *ch;
164         struct sr_channel_group *cg;
165         struct sr_config *src;
166         const struct fx2lafw_profile *prof;
167         GSList *l, *devices, *conn_devices;
168         gboolean has_firmware;
169         struct libusb_device_descriptor des;
170         libusb_device **devlist;
171         struct libusb_device_handle *hdl;
172         int ret, i, j;
173         int num_logic_channels = 0, num_analog_channels = 0;
174         const char *conn;
175         char manufacturer[64], product[64], serial_num[64], connection_id[64];
176         char channel_name[16];
177
178         drvc = di->context;
179
180         conn = NULL;
181         for (l = options; l; l = l->next) {
182                 src = l->data;
183                 switch (src->key) {
184                 case SR_CONF_CONN:
185                         conn = g_variant_get_string(src->data, NULL);
186                         break;
187                 }
188         }
189         if (conn)
190                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
191         else
192                 conn_devices = NULL;
193
194         /* Find all fx2lafw compatible devices and upload firmware to them. */
195         devices = NULL;
196         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
197         for (i = 0; devlist[i]; i++) {
198                 if (conn) {
199                         usb = NULL;
200                         for (l = conn_devices; l; l = l->next) {
201                                 usb = l->data;
202                                 if (usb->bus == libusb_get_bus_number(devlist[i])
203                                         && usb->address == libusb_get_device_address(devlist[i]))
204                                         break;
205                         }
206                         if (!l)
207                                 /* This device matched none of the ones that
208                                  * matched the conn specification. */
209                                 continue;
210                 }
211
212                 libusb_get_device_descriptor( devlist[i], &des);
213
214                 if (!is_plausible(&des))
215                         continue;
216
217                 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
218                         sr_warn("Failed to open potential device with "
219                                 "VID:PID %04x:%04x: %s.", des.idVendor,
220                                 des.idProduct, libusb_error_name(ret));
221                         continue;
222                 }
223
224                 if (des.iManufacturer == 0) {
225                         manufacturer[0] = '\0';
226                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
227                                 des.iManufacturer, (unsigned char *) manufacturer,
228                                 sizeof(manufacturer))) < 0) {
229                         sr_warn("Failed to get manufacturer string descriptor: %s.",
230                                 libusb_error_name(ret));
231                         continue;
232                 }
233
234                 if (des.iProduct == 0) {
235                         product[0] = '\0';
236                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
237                                 des.iProduct, (unsigned char *) product,
238                                 sizeof(product))) < 0) {
239                         sr_warn("Failed to get product string descriptor: %s.",
240                                 libusb_error_name(ret));
241                         continue;
242                 }
243
244                 if (des.iSerialNumber == 0) {
245                         serial_num[0] = '\0';
246                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
247                                 des.iSerialNumber, (unsigned char *) serial_num,
248                                 sizeof(serial_num))) < 0) {
249                         sr_warn("Failed to get serial number string descriptor: %s.",
250                                 libusb_error_name(ret));
251                         continue;
252                 }
253
254                 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
255
256                 libusb_close(hdl);
257
258                 prof = NULL;
259                 for (j = 0; supported_fx2[j].vid; j++) {
260                         if (des.idVendor == supported_fx2[j].vid &&
261                                         des.idProduct == supported_fx2[j].pid &&
262                                         (!supported_fx2[j].usb_manufacturer ||
263                                          !strcmp(manufacturer, supported_fx2[j].usb_manufacturer)) &&
264                                         (!supported_fx2[j].usb_product ||
265                                          !strcmp(product, supported_fx2[j].usb_product))) {
266                                 prof = &supported_fx2[j];
267                                 break;
268                         }
269                 }
270
271                 if (!prof)
272                         continue;
273
274                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
275                 sdi->status = SR_ST_INITIALIZING;
276                 sdi->vendor = g_strdup(prof->vendor);
277                 sdi->model = g_strdup(prof->model);
278                 sdi->version = g_strdup(prof->model_version);
279                 sdi->serial_num = g_strdup(serial_num);
280                 sdi->connection_id = g_strdup(connection_id);
281
282                 /* Fill in channellist according to this device's profile. */
283                 num_logic_channels = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
284                 num_analog_channels = prof->dev_caps & DEV_CAPS_AX_ANALOG ? 1 : 0;
285
286                 /* Logic channels, all in one channel group. */
287                 cg = g_malloc0(sizeof(struct sr_channel_group));
288                 cg->name = g_strdup("Logic");
289                 for (j = 0; j < num_logic_channels; j++) {
290                         sprintf(channel_name, "D%d", j);
291                         ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
292                                                 TRUE, channel_name);
293                         cg->channels = g_slist_append(cg->channels, ch);
294                 }
295                 sdi->channel_groups = g_slist_append(NULL, cg);
296
297                 for (j = 0; j < num_analog_channels; j++) {
298                         snprintf(channel_name, 16, "A%d", j);
299                         ch = sr_channel_new(sdi, j + num_logic_channels,
300                                         SR_CHANNEL_ANALOG, TRUE, channel_name);
301
302                         /* Every analog channel gets its own channel group. */
303                         cg = g_malloc0(sizeof(struct sr_channel_group));
304                         cg->name = g_strdup(channel_name);
305                         cg->channels = g_slist_append(NULL, ch);
306                         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
307                 }
308
309                 devc = fx2lafw_dev_new();
310                 devc->profile = prof;
311                 sdi->priv = devc;
312                 devices = g_slist_append(devices, sdi);
313
314                 devc->samplerates = samplerates;
315                 devc->num_samplerates = ARRAY_SIZE(samplerates);
316                 has_firmware = usb_match_manuf_prod(devlist[i],
317                                 "sigrok", "fx2lafw");
318
319                 if (has_firmware) {
320                         /* Already has the firmware, so fix the new address. */
321                         sr_dbg("Found an fx2lafw device.");
322                         sdi->status = SR_ST_INACTIVE;
323                         sdi->inst_type = SR_INST_USB;
324                         sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
325                                         libusb_get_device_address(devlist[i]), NULL);
326                 } else {
327                         if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
328                                         USB_CONFIGURATION, prof->firmware) == SR_OK)
329                                 /* Store when this device's FW was updated. */
330                                 devc->fw_updated = g_get_monotonic_time();
331                         else
332                                 sr_err("Firmware upload failed for "
333                                        "device %d.%d (logical).",
334                                        libusb_get_bus_number(devlist[i]),
335                                        libusb_get_device_address(devlist[i]));
336                         sdi->inst_type = SR_INST_USB;
337                         sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
338                                         0xff, NULL);
339                 }
340         }
341         libusb_free_device_list(devlist, 1);
342         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
343
344         return std_scan_complete(di, devices);
345 }
346
347 static void clear_helper(struct dev_context *devc)
348 {
349         g_slist_free(devc->enabled_analog_channels);
350 }
351
352 static int dev_clear(const struct sr_dev_driver *di)
353 {
354         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
355 }
356
357 static int dev_open(struct sr_dev_inst *sdi)
358 {
359         struct sr_dev_driver *di = sdi->driver;
360         struct sr_usb_dev_inst *usb;
361         struct dev_context *devc;
362         int ret;
363         int64_t timediff_us, timediff_ms;
364
365         devc = sdi->priv;
366         usb = sdi->conn;
367
368         /*
369          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
370          * milliseconds for the FX2 to renumerate.
371          */
372         ret = SR_ERR;
373         if (devc->fw_updated > 0) {
374                 sr_info("Waiting for device to reset.");
375                 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
376                 g_usleep(300 * 1000);
377                 timediff_ms = 0;
378                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
379                         if ((ret = fx2lafw_dev_open(sdi, di)) == SR_OK)
380                                 break;
381                         g_usleep(100 * 1000);
382
383                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
384                         timediff_ms = timediff_us / 1000;
385                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
386                 }
387                 if (ret != SR_OK) {
388                         sr_err("Device failed to renumerate.");
389                         return SR_ERR;
390                 }
391                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
392         } else {
393                 sr_info("Firmware upload was not needed.");
394                 ret = fx2lafw_dev_open(sdi, di);
395         }
396
397         if (ret != SR_OK) {
398                 sr_err("Unable to open device.");
399                 return SR_ERR;
400         }
401
402         ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
403         if (ret != 0) {
404                 switch (ret) {
405                 case LIBUSB_ERROR_BUSY:
406                         sr_err("Unable to claim USB interface. Another "
407                                "program or driver has already claimed it.");
408                         break;
409                 case LIBUSB_ERROR_NO_DEVICE:
410                         sr_err("Device has been disconnected.");
411                         break;
412                 default:
413                         sr_err("Unable to claim interface: %s.",
414                                libusb_error_name(ret));
415                         break;
416                 }
417
418                 return SR_ERR;
419         }
420
421         if (devc->cur_samplerate == 0) {
422                 /* Samplerate hasn't been set; default to the slowest one. */
423                 devc->cur_samplerate = devc->samplerates[0];
424         }
425
426         return SR_OK;
427 }
428
429 static int dev_close(struct sr_dev_inst *sdi)
430 {
431         struct sr_usb_dev_inst *usb;
432
433         usb = sdi->conn;
434
435         if (!usb->devhdl)
436                 return SR_ERR_BUG;
437
438         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
439                 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
440         libusb_release_interface(usb->devhdl, USB_INTERFACE);
441         libusb_close(usb->devhdl);
442         usb->devhdl = NULL;
443
444         return SR_OK;
445 }
446
447 static int config_get(uint32_t key, GVariant **data,
448         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
449 {
450         struct dev_context *devc;
451         struct sr_usb_dev_inst *usb;
452
453         (void)cg;
454
455         if (!sdi)
456                 return SR_ERR_ARG;
457
458         devc = sdi->priv;
459
460         switch (key) {
461         case SR_CONF_CONN:
462                 if (!sdi->conn)
463                         return SR_ERR_ARG;
464                 usb = sdi->conn;
465                 if (usb->address == 255)
466                         /* Device still needs to re-enumerate after firmware
467                          * upload, so we don't know its (future) address. */
468                         return SR_ERR;
469                 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
470                 break;
471         case SR_CONF_LIMIT_SAMPLES:
472                 *data = g_variant_new_uint64(devc->limit_samples);
473                 break;
474         case SR_CONF_SAMPLERATE:
475                 *data = g_variant_new_uint64(devc->cur_samplerate);
476                 break;
477         case SR_CONF_CAPTURE_RATIO:
478                 *data = g_variant_new_uint64(devc->capture_ratio);
479                 break;
480         default:
481                 return SR_ERR_NA;
482         }
483
484         return SR_OK;
485 }
486
487 static int config_set(uint32_t key, GVariant *data,
488         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
489 {
490         struct dev_context *devc;
491         int idx, ret;
492
493         (void)cg;
494
495         if (!sdi)
496                 return SR_ERR_ARG;
497
498         devc = sdi->priv;
499
500         ret = SR_OK;
501
502         switch (key) {
503         case SR_CONF_SAMPLERATE:
504                 if ((idx = std_u64_idx(data, devc->samplerates, devc->num_samplerates)) < 0)
505                         return SR_ERR_ARG;
506                 devc->cur_samplerate = devc->samplerates[idx];
507                 break;
508         case SR_CONF_LIMIT_SAMPLES:
509                 devc->limit_samples = g_variant_get_uint64(data);
510                 break;
511         case SR_CONF_CAPTURE_RATIO:
512                 devc->capture_ratio = g_variant_get_uint64(data);
513                 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
514                 break;
515         default:
516                 ret = SR_ERR_NA;
517         }
518
519         return ret;
520 }
521
522 static int config_list(uint32_t key, GVariant **data,
523         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
524 {
525         struct dev_context *devc;
526
527         devc = (sdi) ? sdi->priv : NULL;
528
529         switch (key) {
530         case SR_CONF_SCAN_OPTIONS:
531         case SR_CONF_DEVICE_OPTIONS:
532                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
533         case SR_CONF_SAMPLERATE:
534                 *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
535                 break;
536         case SR_CONF_TRIGGER_MATCH:
537                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
538                 break;
539         default:
540                 return SR_ERR_NA;
541         }
542
543         return SR_OK;
544 }
545
546 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
547 {
548         fx2lafw_abort_acquisition(sdi->priv);
549
550         return SR_OK;
551 }
552
553 static struct sr_dev_driver fx2lafw_driver_info = {
554         .name = "fx2lafw",
555         .longname = "fx2lafw (generic driver for FX2 based LAs)",
556         .api_version = 1,
557         .init = std_init,
558         .cleanup = std_cleanup,
559         .scan = scan,
560         .dev_list = std_dev_list,
561         .dev_clear = dev_clear,
562         .config_get = config_get,
563         .config_set = config_set,
564         .config_list = config_list,
565         .dev_open = dev_open,
566         .dev_close = dev_close,
567         .dev_acquisition_start = fx2lafw_start_acquisition,
568         .dev_acquisition_stop = dev_acquisition_stop,
569         .context = NULL,
570 };
571 SR_REGISTER_DEV_DRIVER(fx2lafw_driver_info);