]> sigrok.org Git - libsigrok.git/blob - src/hardware/chronovu-la/api.c
chronovu-la: Add checking of VendorId
[libsigrok.git] / src / hardware / chronovu-la / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2011-2015 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include "protocol.h"
22
23 #define CHRONOVU_VENDOR (0x0403)
24
25 static const uint32_t scanopts[] = {
26         SR_CONF_CONN,
27 };
28
29 static const uint32_t drvopts[] = {
30         SR_CONF_LOGIC_ANALYZER,
31 };
32
33 static const uint32_t devopts[] = {
34         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
35         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
36         SR_CONF_CONN | SR_CONF_GET,
37         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
39 };
40
41 static const int32_t trigger_matches[] = {
42         SR_TRIGGER_ZERO,
43         SR_TRIGGER_ONE,
44         SR_TRIGGER_RISING,
45         SR_TRIGGER_FALLING,
46 };
47
48 static void clear_helper(struct dev_context *devc)
49 {
50         ftdi_free(devc->ftdic);
51         g_free(devc->final_buf);
52 }
53
54 static int dev_clear(const struct sr_dev_driver *di)
55 {
56         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
57 }
58
59 static int add_device(int model, struct libusb_device_descriptor *des,
60         const char *serial_num, const char *connection_id, libusb_device *usbdev,
61         GSList **devices)
62 {
63         int ret;
64         unsigned int i;
65         struct sr_dev_inst *sdi;
66         struct dev_context *devc;
67
68         ret = SR_OK;
69
70         devc = g_malloc0(sizeof(struct dev_context));
71
72         /* Set some sane defaults. */
73         devc->prof = &cv_profiles[model];
74         devc->ftdic = NULL; /* Will be set in the open() API call. */
75         devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
76         devc->limit_msec = 0;
77         devc->limit_samples = 0;
78         memset(devc->mangled_buf, 0, BS);
79         devc->final_buf = NULL;
80         devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
81         devc->trigger_mask = 0x0000; /* All channels: "don't care". */
82         devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
83         devc->trigger_found = 0;
84         devc->done = 0;
85         devc->block_counter = 0;
86         devc->divcount = 0;
87         devc->usb_vid = des->idVendor;
88         devc->usb_pid = des->idProduct;
89         memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
90
91         /* Allocate memory where we'll store the de-mangled data. */
92         if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
93                 sr_err("Failed to allocate memory for sample buffer.");
94                 ret = SR_ERR_MALLOC;
95                 goto err_free_devc;
96         }
97
98         /* We now know the device, set its max. samplerate as default. */
99         devc->cur_samplerate = devc->prof->max_samplerate;
100
101         sdi = g_malloc0(sizeof(struct sr_dev_inst));
102         sdi->status = SR_ST_INACTIVE;
103         sdi->vendor = g_strdup("ChronoVu");
104         sdi->model = g_strdup(devc->prof->modelname);
105         sdi->serial_num = g_strdup(serial_num);
106         sdi->connection_id = g_strdup(connection_id);
107         sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
108                 libusb_get_device_address(usbdev), NULL);
109         sdi->priv = devc;
110
111         for (i = 0; i < devc->prof->num_channels; i++)
112                 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
113                                 cv_channel_names[i]);
114
115         *devices = g_slist_append(*devices, sdi);
116
117         if (ret == SR_OK)
118                 return SR_OK;
119
120 err_free_devc:
121         g_free(devc);
122
123         return ret;
124 }
125
126 static GSList *scan(struct sr_dev_driver *di, GSList *options)
127 {
128         int i, ret, model;
129         struct drv_context *drvc;
130         GSList *devices, *conn_devices, *l;
131         struct sr_usb_dev_inst *usb;
132         struct sr_config *src;
133         struct libusb_device_descriptor des;
134         libusb_device **devlist;
135         struct libusb_device_handle *hdl;
136         const char *conn;
137         char product[64], serial_num[64], connection_id[64];
138
139         drvc = di->context;
140
141         conn = NULL;
142         for (l = options; l; l = l->next) {
143                 src = l->data;
144                 switch (src->key) {
145                 case SR_CONF_CONN:
146                         conn = g_variant_get_string(src->data, NULL);
147                         break;
148                 }
149         }
150         if (conn)
151                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
152         else
153                 conn_devices = NULL;
154
155         devices = NULL;
156         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
157
158         for (i = 0; devlist[i]; i++) {
159                 if (conn) {
160                         for (l = conn_devices; l; l = l->next) {
161                                 usb = l->data;
162                                 if (usb->bus == libusb_get_bus_number(devlist[i])
163                                         && usb->address == libusb_get_device_address(devlist[i]))
164                                         break;
165                         }
166                         if (!l)
167                                 /* This device matched none of the ones that
168                                  * matched the conn specification. */
169                                 continue;
170                 }
171
172                 libusb_get_device_descriptor(devlist[i], &des);
173
174                 /* See https://sigrok.org/bugzilla/show_bug.cgi?id=1115 and
175                  * https://github.com/sigrokproject/libsigrok/pull/166 */
176                 if (des.idVendor != CHRONOVU_VENDOR)
177                         continue;
178
179                 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
180                         continue;
181
182                 if (des.iProduct == 0) {
183                         product[0] = '\0';
184                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
185                                 des.iProduct, (unsigned char *)product,
186                                 sizeof(product))) < 0) {
187                         sr_warn("Failed to get product string descriptor: %s.",
188                                 libusb_error_name(ret));
189                         continue;
190                 }
191
192                 if (des.iSerialNumber == 0) {
193                         serial_num[0] = '\0';
194                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
195                                 des.iSerialNumber, (unsigned char *)serial_num,
196                                 sizeof(serial_num))) < 0) {
197                         sr_warn("Failed to get serial number string descriptor: %s.",
198                                 libusb_error_name(ret));
199                         continue;
200                 }
201
202                 libusb_close(hdl);
203
204                 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
205                         continue;
206
207                 if (!strcmp(product, "ChronoVu LA8"))
208                         model = 0;
209                 else if (!strcmp(product, "ChronoVu LA16"))
210                         model = 1;
211                 else
212                         continue; /* Unknown iProduct string, ignore. */
213
214                 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
215                        product, des.idVendor, des.idProduct,
216                        libusb_get_bus_number(devlist[i]),
217                        libusb_get_device_address(devlist[i]), connection_id);
218
219                 if ((ret = add_device(model, &des, serial_num, connection_id,
220                                         devlist[i], &devices)) < 0) {
221                         sr_dbg("Failed to add device: %d.", ret);
222                 }
223         }
224
225         libusb_free_device_list(devlist, 1);
226         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
227
228         return std_scan_complete(di, devices);
229 }
230
231 static int dev_open(struct sr_dev_inst *sdi)
232 {
233         struct dev_context *devc;
234         int ret;
235
236         devc = sdi->priv;
237
238         if (!(devc->ftdic = ftdi_new())) {
239                 sr_err("Failed to initialize libftdi.");
240                 return SR_ERR;
241         }
242
243         sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
244                devc->usb_vid, devc->usb_pid);
245
246         if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
247                         devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
248                 sr_err("Failed to open FTDI device (%d): %s.",
249                        ret, ftdi_get_error_string(devc->ftdic));
250                 goto err_ftdi_free;
251         }
252
253         if ((ret = PURGE_FTDI_BOTH(devc->ftdic)) < 0) {
254                 sr_err("Failed to purge FTDI buffers (%d): %s.",
255                        ret, ftdi_get_error_string(devc->ftdic));
256                 goto err_ftdi_free;
257         }
258
259         if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
260                 sr_err("Failed to enable FTDI flow control (%d): %s.",
261                        ret, ftdi_get_error_string(devc->ftdic));
262                 goto err_ftdi_free;
263         }
264
265         g_usleep(100 * 1000);
266
267         return SR_OK;
268
269 err_ftdi_free:
270         ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
271         devc->ftdic = NULL;
272         return SR_ERR;
273 }
274
275 static int dev_close(struct sr_dev_inst *sdi)
276 {
277         int ret;
278         struct dev_context *devc;
279
280         devc = sdi->priv;
281
282         if (!devc->ftdic)
283                 return SR_ERR_BUG;
284
285         if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
286                 sr_err("Failed to close FTDI device (%d): %s.",
287                        ret, ftdi_get_error_string(devc->ftdic));
288
289         return (ret == 0) ? SR_OK : SR_ERR;
290 }
291
292 static int config_get(uint32_t key, GVariant **data,
293         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
294 {
295         struct dev_context *devc;
296         struct sr_usb_dev_inst *usb;
297
298         (void)cg;
299
300         switch (key) {
301         case SR_CONF_CONN:
302                 if (!sdi || !(usb = sdi->conn))
303                         return SR_ERR_ARG;
304                 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
305                 break;
306         case SR_CONF_SAMPLERATE:
307                 if (!sdi)
308                         return SR_ERR_BUG;
309                 devc = sdi->priv;
310                 *data = g_variant_new_uint64(devc->cur_samplerate);
311                 break;
312         default:
313                 return SR_ERR_NA;
314         }
315
316         return SR_OK;
317 }
318
319 static int config_set(uint32_t key, GVariant *data,
320         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
321 {
322         struct dev_context *devc;
323
324         (void)cg;
325
326         devc = sdi->priv;
327
328         switch (key) {
329         case SR_CONF_SAMPLERATE:
330                 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
331                         return SR_ERR;
332                 break;
333         case SR_CONF_LIMIT_MSEC:
334                 devc->limit_msec = g_variant_get_uint64(data);
335                 break;
336         case SR_CONF_LIMIT_SAMPLES:
337                 devc->limit_samples = g_variant_get_uint64(data);
338                 break;
339         default:
340                 return SR_ERR_NA;
341         }
342
343         return SR_OK;
344 }
345
346 static int config_list(uint32_t key, GVariant **data,
347         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
348 {
349         struct dev_context *devc;
350
351         devc = (sdi) ? sdi->priv : NULL;
352
353         switch (key) {
354         case SR_CONF_SCAN_OPTIONS:
355         case SR_CONF_DEVICE_OPTIONS:
356                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
357         case SR_CONF_SAMPLERATE:
358                 cv_fill_samplerates_if_needed(sdi);
359                 *data = std_gvar_samplerates(ARRAY_AND_SIZE(devc->samplerates));
360                 break;
361         case SR_CONF_LIMIT_SAMPLES:
362                 if (!devc || !devc->prof)
363                         return SR_ERR_BUG;
364                 *data = std_gvar_tuple_u64(0, (devc->prof->model == CHRONOVU_LA8) ? MAX_NUM_SAMPLES : MAX_NUM_SAMPLES / 2);
365                 break;
366         case SR_CONF_TRIGGER_MATCH:
367                 if (!devc || !devc->prof)
368                         return SR_ERR_BUG;
369                 *data = std_gvar_array_i32(trigger_matches, devc->prof->num_trigger_matches);
370                 break;
371         default:
372                 return SR_ERR_NA;
373         }
374
375         return SR_OK;
376 }
377
378 static int receive_data(int fd, int revents, void *cb_data)
379 {
380         int i, ret;
381         struct sr_dev_inst *sdi;
382         struct dev_context *devc;
383
384         (void)fd;
385         (void)revents;
386
387         if (!(sdi = cb_data)) {
388                 sr_err("cb_data was NULL.");
389                 return FALSE;
390         }
391
392         if (!(devc = sdi->priv)) {
393                 sr_err("sdi->priv was NULL.");
394                 return FALSE;
395         }
396
397         if (!devc->ftdic) {
398                 sr_err("devc->ftdic was NULL.");
399                 return FALSE;
400         }
401
402         /* Get one block of data. */
403         if ((ret = cv_read_block(devc)) < 0) {
404                 sr_err("Failed to read data block: %d.", ret);
405                 sr_dev_acquisition_stop(sdi);
406                 return FALSE;
407         }
408
409         /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
410         if (devc->block_counter != (NUM_BLOCKS - 1)) {
411                 devc->block_counter++;
412                 return TRUE;
413         }
414
415         sr_dbg("Sampling finished, sending data to session bus now.");
416
417         /*
418          * All data was received and demangled, send it to the session bus.
419          *
420          * Note: Due to the method how data is spread across the 8MByte of
421          * SDRAM, we can _not_ send it to the session bus in a streaming
422          * manner while we receive it. We have to receive and de-mangle the
423          * full 8MByte first, only then the whole buffer contains valid data.
424          */
425         for (i = 0; i < NUM_BLOCKS; i++)
426                 cv_send_block_to_session_bus(sdi, i);
427
428         sr_dev_acquisition_stop(sdi);
429
430         return TRUE;
431 }
432
433 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
434 {
435         struct dev_context *devc;
436         uint8_t buf[8];
437         int bytes_to_write, bytes_written;
438
439         devc = sdi->priv;
440
441         if (!devc->ftdic) {
442                 sr_err("devc->ftdic was NULL.");
443                 return SR_ERR_BUG;
444         }
445
446         devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
447         if (devc->divcount == 0xff) {
448                 sr_err("Invalid divcount/samplerate.");
449                 return SR_ERR;
450         }
451
452         if (cv_convert_trigger(sdi) != SR_OK) {
453                 sr_err("Failed to configure trigger.");
454                 return SR_ERR;
455         }
456
457         /* Fill acquisition parameters into buf[]. */
458         if (devc->prof->model == CHRONOVU_LA8) {
459                 buf[0] = devc->divcount;
460                 buf[1] = 0xff; /* This byte must always be 0xff. */
461                 buf[2] = devc->trigger_pattern & 0xff;
462                 buf[3] = devc->trigger_mask & 0xff;
463                 bytes_to_write = 4;
464         } else {
465                 buf[0] = devc->divcount;
466                 buf[1] = 0xff; /* This byte must always be 0xff. */
467                 buf[2] = (devc->trigger_pattern & 0xff00) >> 8;  /* LSB */
468                 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0;  /* MSB */
469                 buf[4] = (devc->trigger_mask & 0xff00) >> 8;     /* LSB */
470                 buf[5] = (devc->trigger_mask & 0x00ff) >> 0;     /* MSB */
471                 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
472                 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
473                 bytes_to_write = 8;
474         }
475
476         /* Start acquisition. */
477         bytes_written = cv_write(devc, buf, bytes_to_write);
478
479         if (bytes_written < 0 || bytes_written != bytes_to_write) {
480                 sr_err("Acquisition failed to start.");
481                 return SR_ERR;
482         }
483
484         std_session_send_df_header(sdi);
485
486         /* Time when we should be done (for detecting trigger timeouts). */
487         devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
488                         g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
489         devc->block_counter = 0;
490         devc->trigger_found = 0;
491
492         /* Hook up a dummy handler to receive data from the device. */
493         sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
494
495         return SR_OK;
496 }
497
498 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
499 {
500         sr_session_source_remove(sdi->session, -1);
501         std_session_send_df_end(sdi);
502
503         return SR_OK;
504 }
505
506 static struct sr_dev_driver chronovu_la_driver_info = {
507         .name = "chronovu-la",
508         .longname = "ChronoVu LA8/LA16",
509         .api_version = 1,
510         .init = std_init,
511         .cleanup = std_cleanup,
512         .scan = scan,
513         .dev_list = std_dev_list,
514         .dev_clear = dev_clear,
515         .config_get = config_get,
516         .config_set = config_set,
517         .config_list = config_list,
518         .dev_open = dev_open,
519         .dev_close = dev_close,
520         .dev_acquisition_start = dev_acquisition_start,
521         .dev_acquisition_stop = dev_acquisition_stop,
522         .context = NULL,
523 };
524 SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);