]> sigrok.org Git - libsigrok.git/blame_incremental - api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.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 3 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 "protocol.h"
21
22static const uint32_t devopts[] = {
23 SR_CONF_LOGIC_ANALYZER,
24 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
25 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
26 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
27 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
28};
29
30static const int32_t trigger_matches[] = {
31 SR_TRIGGER_RISING,
32 SR_TRIGGER_FALLING,
33 SR_TRIGGER_EDGE,
34};
35
36SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
37 SR_KHZ(1.25),
38 SR_KHZ(10),
39 SR_KHZ(50),
40 SR_KHZ(100),
41 SR_KHZ(250),
42 SR_KHZ(500),
43 SR_MHZ(1),
44 SR_MHZ(2.5),
45 SR_MHZ(5),
46 SR_MHZ(10),
47 SR_MHZ(20),
48};
49
50static const char *channel_names[NUM_CHANNELS + 1] = {
51 "0", "1", "2", "3",
52 NULL,
53};
54
55SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
56static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
57
58static int init(struct sr_context *sr_ctx)
59{
60 return std_init(sr_ctx, di, LOG_PREFIX);
61}
62
63static GSList *scan(GSList *options)
64{
65 GSList *usb_devices, *devices, *l;
66 struct drv_context *drvc;
67 struct sr_dev_inst *sdi;
68 struct sr_channel *ch;
69 struct dev_context *devc;
70 struct sr_usb_dev_inst *usb;
71 struct device_info dev_info;
72 int ret, i;
73 char *fw_ver_str;
74 char connection_id[64];
75
76 (void)options;
77
78 devices = NULL;
79 drvc = di->priv;
80 drvc->instances = NULL;
81
82 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
83
84 if (usb_devices == NULL)
85 return NULL;
86
87 for (l = usb_devices; l; l = l->next) {
88 usb = l->data;
89
90 if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
91 sr_warn("Failed to get device information: %d.", ret);
92 sr_usb_dev_inst_free(usb);
93 continue;
94 }
95
96 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
97 sr_err("Device instance malloc failed.");
98 sr_usb_dev_inst_free(usb);
99 continue;
100 }
101
102 if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
103 sr_err("Transfer malloc failed.");
104 sr_usb_dev_inst_free(usb);
105 g_free(devc);
106 continue;
107 }
108
109 if (!(devc->xfer_out = libusb_alloc_transfer(0))) {
110 sr_err("Transfer malloc failed.");
111 sr_usb_dev_inst_free(usb);
112 libusb_free_transfer(devc->xfer_in);
113 g_free(devc);
114 continue;
115 }
116
117 fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major,
118 dev_info.fw_ver_minor);
119 if (!fw_ver_str) {
120 sr_err("Firmware string malloc failed.");
121 sr_usb_dev_inst_free(usb);
122 libusb_free_transfer(devc->xfer_in);
123 libusb_free_transfer(devc->xfer_out);
124 g_free(devc);
125 continue;
126 }
127
128 sdi = sr_dev_inst_new(SR_ST_INACTIVE, VENDOR_NAME,
129 MODEL_NAME, fw_ver_str);
130 g_free(fw_ver_str);
131 if (!sdi) {
132 sr_err("sr_dev_inst_new failed.");
133 sr_usb_dev_inst_free(usb);
134 libusb_free_transfer(devc->xfer_in);
135 libusb_free_transfer(devc->xfer_out);
136 g_free(devc);
137 continue;
138 }
139
140 usb_get_port_path(libusb_get_device(usb->devhdl),
141 connection_id, sizeof(connection_id));
142
143 sdi->priv = devc;
144 sdi->driver = di;
145 sdi->inst_type = SR_INST_USB;
146 sdi->conn = usb;
147 sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
148 sdi->connection_id = g_strdup(connection_id);
149
150 for (i = 0; channel_names[i]; i++) {
151 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
152 channel_names[i]);
153 sdi->channels = g_slist_append(sdi->channels, ch);
154 devc->channels[i] = ch;
155 }
156
157 devc->state = STATE_IDLE;
158 devc->next_state = STATE_IDLE;
159
160 /* Set default samplerate. */
161 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
162
163 /* Set default capture ratio. */
164 devc->capture_ratio = 0;
165
166 /* Set default after trigger delay. */
167 devc->after_trigger_delay = 0;
168
169 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
170 PACKET_LENGTH);
171 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
172 PACKET_LENGTH);
173
174 libusb_fill_control_setup(devc->xfer_buf_in,
175 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
176 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
177 PACKET_LENGTH);
178 libusb_fill_control_setup(devc->xfer_buf_out,
179 USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
180 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
181 PACKET_LENGTH);
182
183 devc->xfer_data_in = devc->xfer_buf_in +
184 LIBUSB_CONTROL_SETUP_SIZE;
185 devc->xfer_data_out = devc->xfer_buf_out +
186 LIBUSB_CONTROL_SETUP_SIZE;
187
188 drvc->instances = g_slist_append(drvc->instances, sdi);
189 devices = g_slist_append(devices, sdi);
190 }
191
192 g_slist_free(usb_devices);
193
194 return devices;
195}
196
197static GSList *dev_list(void)
198{
199 return ((struct drv_context *)(di->priv))->instances;
200}
201
202static void clear_dev_context(void *priv)
203{
204 struct dev_context *devc;
205
206 devc = priv;
207
208 sr_dbg("Device context cleared.");
209
210 libusb_free_transfer(devc->xfer_in);
211 libusb_free_transfer(devc->xfer_out);
212 g_free(devc);
213}
214
215static int dev_clear(void)
216{
217 return std_dev_clear(di, &clear_dev_context);
218}
219
220static int dev_open(struct sr_dev_inst *sdi)
221{
222 struct drv_context *drvc;
223 struct dev_context *devc;
224 struct sr_usb_dev_inst *usb;
225 uint8_t buffer[PACKET_LENGTH];
226 int ret;
227
228 if (!(drvc = di->priv)) {
229 sr_err("Driver was not initialized.");
230 return SR_ERR;
231 }
232
233 usb = sdi->conn;
234 devc = sdi->priv;
235
236 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
237 return SR_ERR;
238
239 /*
240 * Determine if a kernel driver is active on this interface and, if so,
241 * detach it.
242 */
243 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
244 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
245 if (ret < 0) {
246 sr_err("Failed to detach kernel driver: %s.",
247 libusb_error_name(ret));
248 return SR_ERR;
249 }
250 }
251
252 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
253 sr_err("Failed to claim interface: %s.",
254 libusb_error_name(ret));
255 return SR_ERR;
256 }
257
258 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
259 devc->xfer_buf_in, sl2_receive_transfer_in,
260 sdi, USB_TIMEOUT);
261
262 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
263 devc->xfer_buf_out, sl2_receive_transfer_out,
264 sdi, USB_TIMEOUT);
265
266 memset(buffer, 0, sizeof(buffer));
267
268 buffer[0] = CMD_RESET;
269 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
270 sr_err("Device reset failed: %s.", libusb_error_name(ret));
271 return SR_ERR;
272 }
273
274 /*
275 * Set the device to idle state. If the device is not in idle state it
276 * possibly will reset itself after a few seconds without being used
277 * and thereby close the connection.
278 */
279 buffer[0] = CMD_IDLE;
280 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
281 sr_err("Failed to set device in idle state: %s.",
282 libusb_error_name(ret));
283 return SR_ERR;
284 }
285
286 sdi->status = SR_ST_ACTIVE;
287
288 return SR_OK;
289}
290
291static int dev_close(struct sr_dev_inst *sdi)
292{
293 struct sr_usb_dev_inst *usb;
294
295 if (!di->priv) {
296 sr_err("Driver was not initialized.");
297 return SR_ERR;
298 }
299
300 usb = sdi->conn;
301
302 if (!usb->devhdl)
303 return SR_OK;
304
305 libusb_release_interface(usb->devhdl, USB_INTERFACE);
306 libusb_close(usb->devhdl);
307
308 usb->devhdl = NULL;
309 sdi->status = SR_ST_INACTIVE;
310
311 return SR_OK;
312}
313
314static int cleanup(void)
315{
316 return dev_clear();
317}
318
319static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
320 const struct sr_channel_group *cg)
321{
322 struct dev_context *devc;
323 int ret;
324
325 (void)cg;
326
327 ret = SR_OK;
328 devc = sdi->priv;
329
330 switch (key) {
331 case SR_CONF_SAMPLERATE:
332 *data = g_variant_new_uint64(devc->samplerate);
333 break;
334 case SR_CONF_CAPTURE_RATIO:
335 *data = g_variant_new_uint64(devc->capture_ratio);
336 break;
337 default:
338 return SR_ERR_NA;
339 }
340
341 return ret;
342}
343
344static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
345 const struct sr_channel_group *cg)
346{
347 uint64_t samplerate, limit_samples, capture_ratio;
348 int ret;
349
350 (void)cg;
351
352 if (sdi->status != SR_ST_ACTIVE)
353 return SR_ERR_DEV_CLOSED;
354
355 ret = SR_OK;
356
357 switch (key) {
358 case SR_CONF_LIMIT_SAMPLES:
359 limit_samples = g_variant_get_uint64(data);
360 ret = sl2_set_limit_samples(sdi, limit_samples);
361 break;
362 case SR_CONF_SAMPLERATE:
363 samplerate = g_variant_get_uint64(data);
364 ret = sl2_set_samplerate(sdi, samplerate);
365 break;
366 case SR_CONF_CAPTURE_RATIO:
367 capture_ratio = g_variant_get_uint64(data);
368 ret = sl2_set_capture_ratio(sdi, capture_ratio);
369 break;
370 default:
371 return SR_ERR_NA;
372 }
373
374 return ret;
375}
376
377static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
378 const struct sr_channel_group *cg)
379{
380 GVariant *gvar, *grange[2];
381 GVariantBuilder gvb;
382 int ret;
383
384 (void)sdi;
385 (void)cg;
386
387 ret = SR_OK;
388
389 switch (key) {
390 case SR_CONF_DEVICE_OPTIONS:
391 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
392 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
393 break;
394 case SR_CONF_SAMPLERATE:
395 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
396 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
397 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
398 sizeof(uint64_t));
399 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
400 *data = g_variant_builder_end(&gvb);
401 break;
402 case SR_CONF_TRIGGER_MATCH:
403 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
404 trigger_matches, ARRAY_SIZE(trigger_matches),
405 sizeof(int32_t));
406 break;
407 case SR_CONF_LIMIT_SAMPLES:
408 grange[0] = g_variant_new_uint64(0);
409 grange[1] = g_variant_new_uint64(MAX_SAMPLES);
410 *data = g_variant_new_tuple(grange, 2);
411 break;
412 default:
413 return SR_ERR_NA;
414 }
415
416 return ret;
417}
418
419static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
420{
421 struct drv_context *drvc;
422 struct dev_context *devc;
423 uint16_t trigger_bytes, tmp;
424 unsigned int i, j;
425 int ret;
426
427 if (sdi->status != SR_ST_ACTIVE)
428 return SR_ERR_DEV_CLOSED;
429
430 devc = sdi->priv;
431 drvc = di->priv;
432
433 devc->cb_data = cb_data;
434 devc->wait_data_ready_locked = TRUE;
435 devc->stopping_in_progress = FALSE;
436 devc->transfer_error = FALSE;
437 devc->samples_processed = 0;
438 devc->channel = 0;
439 devc->sample_packet = 0;
440
441 /*
442 * The trigger must be configured first because the calculation of the
443 * pre and post trigger samples depends on a configured trigger.
444 */
445 sl2_convert_trigger(sdi);
446 sl2_calculate_trigger_samples(sdi);
447
448 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
449
450 /* Calculate the number of expected sample packets. */
451 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
452
453 /* Round up the number of expected sample packets. */
454 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
455 devc->num_sample_packets++;
456
457 devc->num_enabled_channels = 0;
458
459 /*
460 * Count the number of enabled channels and number them for a sequential
461 * access.
462 */
463 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
464 if (devc->channels[i]->enabled) {
465 devc->num_enabled_channels++;
466 devc->channel_map[j] = i;
467 j++;
468 }
469 }
470
471 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
472
473 /* Set up the transfer buffer for the acquisition. */
474 devc->xfer_data_out[0] = CMD_SAMPLE;
475 devc->xfer_data_out[1] = 0x00;
476
477 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
478 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
479
480 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
481 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
482
483 devc->xfer_data_out[6] = devc->samplerate_id;
484 devc->xfer_data_out[7] = devc->trigger_type;
485 devc->xfer_data_out[8] = devc->trigger_channel;
486 devc->xfer_data_out[9] = 0x00;
487
488 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
489 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
490
491 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
492 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
493 return SR_ERR;
494 }
495
496 usb_source_add(sdi->session, drvc->sr_ctx, 100,
497 ikalogic_scanalogic2_receive_data, (void *)sdi);
498
499 sr_dbg("Acquisition started successfully.");
500
501 /* Send header packet to the session bus. */
502 std_session_send_df_header(cb_data, LOG_PREFIX);
503
504 devc->next_state = STATE_SAMPLE;
505
506 return SR_OK;
507}
508
509static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
510{
511 (void)cb_data;
512
513 if (sdi->status != SR_ST_ACTIVE)
514 return SR_ERR_DEV_CLOSED;
515
516 sr_dbg("Stopping acquisition.");
517
518 sdi->status = SR_ST_STOPPING;
519
520 return SR_OK;
521}
522
523SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
524 .name = "ikalogic-scanalogic2",
525 .longname = "IKALOGIC Scanalogic-2",
526 .api_version = 1,
527 .init = init,
528 .cleanup = cleanup,
529 .scan = scan,
530 .dev_list = dev_list,
531 .dev_clear = dev_clear,
532 .config_get = config_get,
533 .config_set = config_set,
534 .config_list = config_list,
535 .dev_open = dev_open,
536 .dev_close = dev_close,
537 .dev_acquisition_start = dev_acquisition_start,
538 .dev_acquisition_stop = dev_acquisition_stop,
539 .priv = NULL,
540};