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