]> sigrok.org Git - libsigrok.git/blob - src/hardware/kingst-la2016/api.c
kingst-la2016: sort include directives
[libsigrok.git] / src / hardware / kingst-la2016 / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de>
5  * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
6  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 /* mostly stolen from src/hardware/saleae-logic16/ */
24
25 #include <config.h>
26
27 #include <glib.h>
28 #include <libsigrok/libsigrok.h>
29 #include <libusb.h>
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "libsigrok-internal.h"
35 #include "protocol.h"
36
37 static const uint32_t scanopts[] = {
38         SR_CONF_CONN,
39 };
40
41 static const uint32_t drvopts[] = {
42         SR_CONF_LOGIC_ANALYZER,
43 };
44
45 static const uint32_t devopts[] = {
46         /* TODO: SR_CONF_CONTINUOUS, */
47         SR_CONF_CONN | SR_CONF_GET,
48         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST,
50         SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51         SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52         SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
53         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
54         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
55 };
56
57 static const int32_t trigger_matches[] = {
58         SR_TRIGGER_ZERO,
59         SR_TRIGGER_ONE,
60         SR_TRIGGER_RISING,
61         SR_TRIGGER_FALLING,
62 };
63
64 static const char *channel_names[] = {
65         "0", "1", "2", "3", "4", "5", "6", "7",
66         "8", "9", "10", "11", "12", "13", "14", "15",
67 };
68
69 static const uint64_t samplerates_la2016[] = {
70         SR_KHZ(20),
71         SR_KHZ(50),
72         SR_KHZ(100),
73         SR_KHZ(200),
74         SR_KHZ(500),
75         SR_MHZ(1),
76         SR_MHZ(2),
77         SR_MHZ(4),
78         SR_MHZ(5),
79         SR_MHZ(8),
80         SR_MHZ(10),
81         SR_MHZ(20),
82         SR_MHZ(50),
83         SR_MHZ(100),
84         SR_MHZ(200),
85 };
86
87 static const uint64_t samplerates_la1016[] = {
88         SR_KHZ(20),
89         SR_KHZ(50),
90         SR_KHZ(100),
91         SR_KHZ(200),
92         SR_KHZ(500),
93         SR_MHZ(1),
94         SR_MHZ(2),
95         SR_MHZ(4),
96         SR_MHZ(5),
97         SR_MHZ(8),
98         SR_MHZ(10),
99         SR_MHZ(20),
100         SR_MHZ(50),
101         SR_MHZ(100),
102 };
103
104 static const float logic_threshold_value[] = {
105         1.58,
106         2.5,
107         1.165,
108         1.5,
109         1.25,
110         0.9,
111         0.75,
112         0.60,
113         0.45,
114 };
115
116 static const char *logic_threshold[] = {
117         "TTL 5V",
118         "CMOS 5V",
119         "CMOS 3.3V",
120         "CMOS 3.0V",
121         "CMOS 2.5V",
122         "CMOS 1.8V",
123         "CMOS 1.5V",
124         "CMOS 1.2V",
125         "CMOS 0.9V",
126         "USER",
127 };
128
129 #define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold)
130
131 static GSList *scan(struct sr_dev_driver *di, GSList *options)
132 {
133         struct drv_context *drvc;
134         struct dev_context *devc;
135         struct sr_dev_inst *sdi;
136         struct sr_usb_dev_inst *usb;
137         struct sr_config *src;
138         GSList *l;
139         GSList *devices;
140         GSList *conn_devices;
141         struct libusb_device_descriptor des;
142         libusb_device **devlist;
143         unsigned int i, j;
144         const char *conn;
145         char connection_id[64];
146         int64_t fw_updated;
147         unsigned int dev_addr;
148
149         drvc = di->context;
150
151         conn = NULL;
152         for (l = options; l; l = l->next) {
153                 src = l->data;
154                 switch (src->key) {
155                 case SR_CONF_CONN:
156                         conn = g_variant_get_string(src->data, NULL);
157                         break;
158                 }
159         }
160         if (conn)
161                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
162         else
163                 conn_devices = NULL;
164
165         /* Find all LA2016 devices and upload firmware to them. */
166         devices = NULL;
167         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
168         for (i = 0; devlist[i]; i++) {
169                 if (conn) {
170                         usb = NULL;
171                         for (l = conn_devices; l; l = l->next) {
172                                 usb = l->data;
173                                 if (usb->bus == libusb_get_bus_number(devlist[i]) &&
174                                     usb->address == libusb_get_device_address(devlist[i]))
175                                         break;
176                         }
177                         if (!l) {
178                                 /* This device matched none of the ones that
179                                  * matched the conn specification. */
180                                 continue;
181                         }
182                 }
183
184                 libusb_get_device_descriptor(devlist[i], &des);
185
186                 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
187                         continue;
188
189                 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
190                         continue;
191
192                 /* Already has the firmware */
193                 sr_dbg("Found a LA2016 device.");
194                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
195                 sdi->status = SR_ST_INITIALIZING;
196                 sdi->connection_id = g_strdup(connection_id);
197
198                 fw_updated = 0;
199                 dev_addr = libusb_get_device_address(devlist[i]);
200                 if (des.iProduct != 2) {
201                         sr_info("device at '%s' has no firmware loaded!", connection_id);
202
203                         if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) {
204                                 sr_err("uC firmware upload failed!");
205                                 g_free(sdi->connection_id);
206                                 g_free(sdi);
207                                 continue;
208                         }
209                         fw_updated = g_get_monotonic_time();
210                         dev_addr = 0xff; /* to mark that we don't know address yet... ugly */
211                 }
212
213                 sdi->vendor = g_strdup("Kingst");
214                 sdi->model = g_strdup("LA2016");
215
216                 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
217                         sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
218
219                 devices = g_slist_append(devices, sdi);
220
221                 devc = g_malloc0(sizeof(struct dev_context));
222                 sdi->priv = devc;
223                 devc->fw_updated = fw_updated;
224                 devc->threshold_voltage_idx = 0;
225                 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
226
227                 sdi->status = SR_ST_INACTIVE;
228                 sdi->inst_type = SR_INST_USB;
229
230                 sdi->conn = sr_usb_dev_inst_new(
231                         libusb_get_bus_number(devlist[i]),
232                         dev_addr, NULL);
233         }
234         libusb_free_device_list(devlist, 1);
235         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
236
237         return std_scan_complete(di, devices);
238 }
239
240 static int la2016_dev_open(struct sr_dev_inst *sdi)
241 {
242         struct sr_dev_driver *di;
243         libusb_device **devlist;
244         struct sr_usb_dev_inst *usb;
245         struct libusb_device_descriptor des;
246         struct drv_context *drvc;
247         int ret, i, device_count;
248         char connection_id[64];
249
250         di = sdi->driver;
251         drvc = di->context;
252         usb = sdi->conn;
253         ret = SR_ERR;
254
255         device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
256         if (device_count < 0) {
257                 sr_err("Failed to get device list: %s.", libusb_error_name(device_count));
258                 return SR_ERR;
259         }
260
261         for (i = 0; i < device_count; i++) {
262                 libusb_get_device_descriptor(devlist[i], &des);
263
264                 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2)
265                         continue;
266
267                 if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
268                         /*
269                          * Check device by its physical USB bus/port address.
270                          */
271                         if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
272                                 continue;
273
274                         if (strcmp(sdi->connection_id, connection_id))
275                                 /* This is not the one. */
276                                 continue;
277                 }
278
279                 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
280                         if (usb->address == 0xff)
281                                 /*
282                                  * First time we touch this device after FW
283                                  * upload, so we don't know the address yet.
284                                  */
285                                 usb->address = libusb_get_device_address(devlist[i]);
286                 } else {
287                         sr_err("Failed to open device: %s.", libusb_error_name(ret));
288                         ret = SR_ERR;
289                         break;
290                 }
291
292                 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
293                 if (ret == LIBUSB_ERROR_BUSY) {
294                         sr_err("Unable to claim USB interface. Another "
295                                "program or driver has already claimed it.");
296                         ret = SR_ERR;
297                         break;
298                 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
299                         sr_err("Device has been disconnected.");
300                         ret = SR_ERR;
301                         break;
302                 } else if (ret != 0) {
303                         sr_err("Unable to claim interface: %s.", libusb_error_name(ret));
304                         ret = SR_ERR;
305                         break;
306                 }
307
308                 if ((ret = la2016_init_device(sdi)) != SR_OK) {
309                         sr_err("Failed to init device.");
310                         break;
311                 }
312
313                 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
314                         usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
315
316                 ret = SR_OK;
317
318                 break;
319         }
320
321         libusb_free_device_list(devlist, 1);
322
323         if (ret != SR_OK) {
324                 if (usb->devhdl) {
325                         libusb_release_interface(usb->devhdl, USB_INTERFACE);
326                         libusb_close(usb->devhdl);
327                         usb->devhdl = NULL;
328                 }
329                 return SR_ERR;
330         }
331
332         return SR_OK;
333 }
334
335 static int dev_open(struct sr_dev_inst *sdi)
336 {
337         struct dev_context *devc;
338         int64_t timediff_us, timediff_ms;
339         uint64_t reset_done;
340         uint64_t now;
341         int ret;
342
343         devc = sdi->priv;
344
345         /*
346          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
347          * milliseconds for the FX2 to renumerate.
348          */
349         ret = SR_ERR;
350         if (devc->fw_updated > 0) {
351                 sr_info("Waiting for device to reset after firmware upload.");
352                 /* Takes >= 2000ms for the uC to be gone from the USB bus. */
353                 reset_done = devc->fw_updated + 18 * (uint64_t)1e5; /* 1.8 seconds */
354                 now = g_get_monotonic_time();
355                 if (reset_done > now)
356                         g_usleep(reset_done - now);
357                 timediff_ms = 0;
358                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
359                         g_usleep(200 * 1000);
360
361                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
362                         timediff_ms = timediff_us / 1000;
363
364                         if ((ret = la2016_dev_open(sdi)) == SR_OK)
365                                 break;
366                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
367                 }
368                 if (ret != SR_OK) {
369                         sr_err("Device failed to re-enumerate.");
370                         return SR_ERR;
371                 }
372                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
373         } else {
374                 ret = la2016_dev_open(sdi);
375         }
376
377         if (ret != SR_OK) {
378                 sr_err("Unable to open device.");
379                 return SR_ERR;
380         }
381
382         return SR_OK;
383 }
384
385 static int dev_close(struct sr_dev_inst *sdi)
386 {
387         struct sr_usb_dev_inst *usb;
388
389         usb = sdi->conn;
390
391         if (!usb->devhdl)
392                 return SR_ERR_BUG;
393
394         la2016_deinit_device(sdi);
395
396         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
397                 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
398         libusb_release_interface(usb->devhdl, USB_INTERFACE);
399         libusb_close(usb->devhdl);
400         usb->devhdl = NULL;
401
402         return SR_OK;
403 }
404
405 static int config_get(uint32_t key, GVariant **data,
406         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
407 {
408         struct dev_context *devc;
409         struct sr_usb_dev_inst *usb;
410         double rounded;
411
412         (void)cg;
413
414         if (!sdi)
415                 return SR_ERR_ARG;
416         devc = sdi->priv;
417
418         switch (key) {
419         case SR_CONF_CONN:
420                 if (!sdi->conn)
421                         return SR_ERR_ARG;
422                 usb = sdi->conn;
423                 if (usb->address == 255) {
424                         /* Device still needs to re-enumerate after firmware
425                          * upload, so we don't know its (future) address. */
426                         return SR_ERR;
427                 }
428                 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
429                 break;
430         case SR_CONF_SAMPLERATE:
431                 *data = g_variant_new_uint64(devc->cur_samplerate);
432                 break;
433         case SR_CONF_LIMIT_SAMPLES:
434                 *data = g_variant_new_uint64(devc->limit_samples);
435                 break;
436         case SR_CONF_CAPTURE_RATIO:
437                 *data = g_variant_new_uint64(devc->capture_ratio);
438                 break;
439         case SR_CONF_VOLTAGE_THRESHOLD:
440                 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
441                 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
442                 return SR_OK;
443         case SR_CONF_LOGIC_THRESHOLD:
444                 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
445                 break;
446         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
447                 *data = g_variant_new_double(devc->threshold_voltage);
448                 break;
449
450         default:
451                 return SR_ERR_NA;
452         }
453
454         return SR_OK;
455 }
456
457 static int config_set(uint32_t key, GVariant *data,
458         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
459 {
460         struct dev_context *devc;
461         double low, high;
462         int idx;
463
464         (void)cg;
465
466         devc = sdi->priv;
467
468         switch (key) {
469         case SR_CONF_SAMPLERATE:
470                 devc->cur_samplerate = g_variant_get_uint64(data);
471                 break;
472         case SR_CONF_LIMIT_SAMPLES:
473                 devc->limit_samples = g_variant_get_uint64(data);
474                 break;
475         case SR_CONF_CAPTURE_RATIO:
476                 devc->capture_ratio = g_variant_get_uint64(data);
477                 break;
478         case SR_CONF_VOLTAGE_THRESHOLD:
479                 g_variant_get(data, "(dd)", &low, &high);
480                 devc->threshold_voltage = (low + high) / 2.0;
481                 devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */
482                 break;
483         case SR_CONF_LOGIC_THRESHOLD: {
484                 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
485                         return SR_ERR_ARG;
486                 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
487                         /* user threshold */
488                 } else {
489                         devc->threshold_voltage = logic_threshold_value[idx];
490                 }
491                 devc->threshold_voltage_idx = idx;
492                 break;
493         }
494         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
495                 devc->threshold_voltage = g_variant_get_double(data);
496                 break;
497         default:
498                 return SR_ERR_NA;
499         }
500
501         return SR_OK;
502 }
503
504 static int config_list(uint32_t key, GVariant **data,
505         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
506 {
507         struct dev_context *devc;
508
509         switch (key) {
510         case SR_CONF_SCAN_OPTIONS:
511         case SR_CONF_DEVICE_OPTIONS:
512                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
513         case SR_CONF_SAMPLERATE:
514                 if (!sdi)
515                         return SR_ERR_ARG;
516                 devc = sdi->priv;
517                 if (devc->max_samplerate == SR_MHZ(200)) {
518                         *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
519                 }
520                 else {
521                         *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
522                 }
523                 break;
524         case SR_CONF_LIMIT_SAMPLES:
525                 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
526                 break;
527         case SR_CONF_VOLTAGE_THRESHOLD:
528                 *data = std_gvar_min_max_step_thresholds(
529                         LA2016_THR_VOLTAGE_MIN,
530                         LA2016_THR_VOLTAGE_MAX, 0.1);
531                 break;
532         case SR_CONF_TRIGGER_MATCH:
533                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
534                 break;
535         case SR_CONF_LOGIC_THRESHOLD:
536                 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
537                 break;
538         default:
539                 return SR_ERR_NA;
540         }
541
542         return SR_OK;
543 }
544
545 static int configure_channels(const struct sr_dev_inst *sdi)
546 {
547         struct dev_context *devc;
548
549         devc = sdi->priv;
550         devc->cur_channels = 0;
551         devc->num_channels = 0;
552
553         for (GSList *l = sdi->channels; l; l = l->next) {
554                 struct sr_channel *ch = (struct sr_channel*)l->data;
555                 if (ch->enabled == FALSE)
556                         continue;
557                 devc->cur_channels |= 1 << ch->index;
558                 devc->num_channels++;
559         }
560
561         return SR_OK;
562 }
563
564 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
565 {
566         struct sr_dev_driver *di;
567         struct drv_context *drvc;
568         struct dev_context *devc;
569         int ret;
570
571         di = sdi->driver;
572         drvc = di->context;
573         devc = sdi->priv;
574
575         if (configure_channels(sdi) != SR_OK) {
576                 sr_err("Failed to configure channels.");
577                 return SR_ERR;
578         }
579
580         devc->convbuffer_size = 4 * 1024 * 1024;
581         if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
582                 sr_err("Conversion buffer malloc failed.");
583                 return SR_ERR_MALLOC;
584         }
585
586         if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
587                 g_free(devc->convbuffer);
588                 devc->convbuffer = NULL;
589                 return ret;
590         }
591
592         devc->ctx = drvc->sr_ctx;
593
594         if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
595                 la2016_abort_acquisition(sdi);
596                 return ret;
597         }
598
599         devc->have_trigger = 0;
600         usb_source_add(sdi->session, drvc->sr_ctx, 50,
601                 la2016_receive_data, (void *)sdi);
602
603         std_session_send_df_header(sdi);
604
605         return SR_OK;
606 }
607
608 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
609 {
610         int ret;
611
612         ret = la2016_abort_acquisition(sdi);
613
614         return ret;
615 }
616
617 static struct sr_dev_driver kingst_la2016_driver_info = {
618         .name = "kingst-la2016",
619         .longname = "Kingst LA2016",
620         .api_version = 1,
621         .init = std_init,
622         .cleanup = std_cleanup,
623         .scan = scan,
624         .dev_list = std_dev_list,
625         .dev_clear = std_dev_clear,
626         .config_get = config_get,
627         .config_set = config_set,
628         .config_list = config_list,
629         .dev_open = dev_open,
630         .dev_close = dev_close,
631         .dev_acquisition_start = dev_acquisition_start,
632         .dev_acquisition_stop = dev_acquisition_stop,
633         .context = NULL,
634 };
635 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);