blob: e9b7327ce5b34db3b9ce667a2d545d61249fe0ed [file] [log] [blame]
Marc Schink16e76ba2013-06-07 20:34:40 +02001/*
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
Bert Vermeulenf254bc42014-09-16 22:11:39 +020022static const uint32_t devopts[] = {
Marc Schinke52e7122013-06-10 10:10:13 +020023 SR_CONF_LOGIC_ANALYZER,
Bert Vermeulen5827f612014-09-17 15:28:29 +020024 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,
Marc Schinke52e7122013-06-10 10:10:13 +020028};
29
Bert Vermeulen02d5c0d2014-05-28 00:21:23 +020030static const int32_t trigger_matches[] = {
31 SR_TRIGGER_RISING,
32 SR_TRIGGER_FALLING,
33 SR_TRIGGER_EDGE,
34};
35
Uwe Hermann79914b32013-06-11 18:55:47 +020036SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
Marc Schinke52e7122013-06-10 10:10:13 +020037 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),
Uwe Hermannc824eb62013-06-11 18:55:47 +020047 SR_MHZ(20),
Marc Schinke52e7122013-06-10 10:10:13 +020048};
49
Uwe Hermann3f239f02014-03-24 22:39:42 +010050static const char *channel_names[NUM_CHANNELS + 1] = {
Marc Schinke52e7122013-06-10 10:10:13 +020051 "0", "1", "2", "3",
Uwe Hermannc824eb62013-06-11 18:55:47 +020052 NULL,
Marc Schinke52e7122013-06-10 10:10:13 +020053};
54
Marc Schink16e76ba2013-06-07 20:34:40 +020055SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
56static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
57
Marc Schink16e76ba2013-06-07 20:34:40 +020058static int init(struct sr_context *sr_ctx)
59{
Marc Schinke52e7122013-06-10 10:10:13 +020060 return std_init(sr_ctx, di, LOG_PREFIX);
Marc Schink16e76ba2013-06-07 20:34:40 +020061}
62
63static GSList *scan(GSList *options)
64{
Marc Schinke52e7122013-06-10 10:10:13 +020065 GSList *usb_devices, *devices, *l;
Marc Schink16e76ba2013-06-07 20:34:40 +020066 struct drv_context *drvc;
Marc Schinke52e7122013-06-10 10:10:13 +020067 struct sr_dev_inst *sdi;
Uwe Hermannba7dd8b2014-03-24 21:34:20 +010068 struct sr_channel *ch;
Marc Schinke52e7122013-06-10 10:10:13 +020069 struct dev_context *devc;
70 struct sr_usb_dev_inst *usb;
71 struct device_info dev_info;
Soeren Apel65340dd2014-09-27 23:03:52 +020072 int ret, i;
Marc Schinke52e7122013-06-10 10:10:13 +020073 char *fw_ver_str;
Soeren Apel65340dd2014-09-27 23:03:52 +020074 char connection_id[64];
Marc Schink16e76ba2013-06-07 20:34:40 +020075
76 (void)options;
77
78 devices = NULL;
79 drvc = di->priv;
80 drvc->instances = NULL;
81
Marc Schinke52e7122013-06-10 10:10:13 +020082 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
83
84 if (usb_devices == NULL)
85 return NULL;
86
Uwe Hermannc824eb62013-06-11 18:55:47 +020087 for (l = usb_devices; l; l = l->next) {
Marc Schinke52e7122013-06-10 10:10:13 +020088 usb = l->data;
89
Uwe Hermann79914b32013-06-11 18:55:47 +020090 if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
91 sr_warn("Failed to get device information: %d.", ret);
Marc Schinke52e7122013-06-10 10:10:13 +020092 sr_usb_dev_inst_free(usb);
93 continue;
94 }
95
Uwe Hermannc824eb62013-06-11 18:55:47 +020096 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
Marc Schinke52e7122013-06-10 10:10:13 +020097 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);
Marc Schinke52e7122013-06-10 10:10:13 +0200119 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
Soeren Apel65340dd2014-09-27 23:03:52 +0200128 sdi = sr_dev_inst_new(SR_ST_INACTIVE, VENDOR_NAME,
Marc Schinke52e7122013-06-10 10:10:13 +0200129 MODEL_NAME, fw_ver_str);
Marc Schinke52e7122013-06-10 10:10:13 +0200130 g_free(fw_ver_str);
Marc Schinke52e7122013-06-10 10:10:13 +0200131 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
Soeren Apel65340dd2014-09-27 23:03:52 +0200140 usb_get_port_path(libusb_get_device(usb->devhdl),
141 connection_id, sizeof(connection_id));
142
Marc Schinke52e7122013-06-10 10:10:13 +0200143 sdi->priv = devc;
144 sdi->driver = di;
145 sdi->inst_type = SR_INST_USB;
146 sdi->conn = usb;
Soeren Apel65340dd2014-09-27 23:03:52 +0200147 sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
148 sdi->connection_id = g_strdup(connection_id);
Marc Schinke52e7122013-06-10 10:10:13 +0200149
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100150 for (i = 0; channel_names[i]; i++) {
Uwe Hermann3f239f02014-03-24 22:39:42 +0100151 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100152 channel_names[i]);
153 sdi->channels = g_slist_append(sdi->channels, ch);
154 devc->channels[i] = ch;
Marc Schinke52e7122013-06-10 10:10:13 +0200155 }
156
157 devc->state = STATE_IDLE;
158 devc->next_state = STATE_IDLE;
159
160 /* Set default samplerate. */
Uwe Hermann79914b32013-06-11 18:55:47 +0200161 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
Marc Schinke52e7122013-06-10 10:10:13 +0200162
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,
Marc Schinkfb8d5932013-07-02 21:52:23 +0200175 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
Marc Schinke52e7122013-06-10 10:10:13 +0200176 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);
Marc Schinke52e7122013-06-10 10:10:13 +0200190 }
191
192 g_slist_free(usb_devices);
Marc Schink16e76ba2013-06-07 20:34:40 +0200193
194 return devices;
195}
196
197static GSList *dev_list(void)
198{
Uwe Hermannc824eb62013-06-11 18:55:47 +0200199 return ((struct drv_context *)(di->priv))->instances;
Marc Schink16e76ba2013-06-07 20:34:40 +0200200}
201
Marc Schinke52e7122013-06-10 10:10:13 +0200202static void clear_dev_context(void *priv)
203{
Uwe Hermannc824eb62013-06-11 18:55:47 +0200204 struct dev_context *devc;
Marc Schinke52e7122013-06-10 10:10:13 +0200205
Uwe Hermannc824eb62013-06-11 18:55:47 +0200206 devc = priv;
207
208 sr_dbg("Device context cleared.");
Marc Schinke52e7122013-06-10 10:10:13 +0200209
210 libusb_free_transfer(devc->xfer_in);
211 libusb_free_transfer(devc->xfer_out);
212 g_free(devc);
213}
214
Marc Schink16e76ba2013-06-07 20:34:40 +0200215static int dev_clear(void)
216{
Marc Schinke52e7122013-06-10 10:10:13 +0200217 return std_dev_clear(di, &clear_dev_context);
Marc Schink16e76ba2013-06-07 20:34:40 +0200218}
219
220static int dev_open(struct sr_dev_inst *sdi)
221{
Marc Schinke52e7122013-06-10 10:10:13 +0200222 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;
Marc Schink16e76ba2013-06-07 20:34:40 +0200227
Marc Schinke52e7122013-06-10 10:10:13 +0200228 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);
Marc Schinke52e7122013-06-10 10:10:13 +0200245 if (ret < 0) {
Uwe Hermann9526f2d2013-06-11 18:55:47 +0200246 sr_err("Failed to detach kernel driver: %s.",
Marc Schinke52e7122013-06-10 10:10:13 +0200247 libusb_error_name(ret));
248 return SR_ERR;
249 }
250 }
251
Uwe Hermann79914b32013-06-11 18:55:47 +0200252 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
Marc Schinke52e7122013-06-10 10:10:13 +0200253 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,
Uwe Hermann79914b32013-06-11 18:55:47 +0200259 devc->xfer_buf_in, sl2_receive_transfer_in,
Marc Schinke52e7122013-06-10 10:10:13 +0200260 sdi, USB_TIMEOUT);
261
262 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
Uwe Hermann79914b32013-06-11 18:55:47 +0200263 devc->xfer_buf_out, sl2_receive_transfer_out,
Marc Schinke52e7122013-06-10 10:10:13 +0200264 sdi, USB_TIMEOUT);
265
266 memset(buffer, 0, sizeof(buffer));
267
268 buffer[0] = CMD_RESET;
Uwe Hermann79914b32013-06-11 18:55:47 +0200269 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
Marc Schinke52e7122013-06-10 10:10:13 +0200270 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
Uwe Hermannc824eb62013-06-11 18:55:47 +0200276 * possibly will reset itself after a few seconds without being used
277 * and thereby close the connection.
Marc Schinke52e7122013-06-10 10:10:13 +0200278 */
279 buffer[0] = CMD_IDLE;
Uwe Hermann79914b32013-06-11 18:55:47 +0200280 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
Marc Schinke52e7122013-06-10 10:10:13 +0200281 sr_err("Failed to set device in idle state: %s.",
282 libusb_error_name(ret));
283 return SR_ERR;
284 }
Marc Schink16e76ba2013-06-07 20:34:40 +0200285
286 sdi->status = SR_ST_ACTIVE;
287
288 return SR_OK;
289}
290
291static int dev_close(struct sr_dev_inst *sdi)
292{
Marc Schinke52e7122013-06-10 10:10:13 +0200293 struct sr_usb_dev_inst *usb;
Marc Schink16e76ba2013-06-07 20:34:40 +0200294
Marc Schinke52e7122013-06-10 10:10:13 +0200295 if (!di->priv) {
296 sr_err("Driver was not initialized.");
297 return SR_ERR;
298 }
Marc Schink16e76ba2013-06-07 20:34:40 +0200299
Marc Schinke52e7122013-06-10 10:10:13 +0200300 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;
Marc Schink16e76ba2013-06-07 20:34:40 +0200309 sdi->status = SR_ST_INACTIVE;
310
311 return SR_OK;
312}
313
314static int cleanup(void)
315{
Uwe Hermannc824eb62013-06-11 18:55:47 +0200316 return dev_clear();
Marc Schink16e76ba2013-06-07 20:34:40 +0200317}
318
Bert Vermeulen584560f2014-09-16 17:49:20 +0200319static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100320 const struct sr_channel_group *cg)
Marc Schink16e76ba2013-06-07 20:34:40 +0200321{
Marc Schinke52e7122013-06-10 10:10:13 +0200322 struct dev_context *devc;
Marc Schink16e76ba2013-06-07 20:34:40 +0200323 int ret;
324
Uwe Hermann53b46802014-03-20 21:58:01 +0100325 (void)cg;
Bert Vermeulend3c74a62013-10-31 23:58:33 +0100326
Marc Schink16e76ba2013-06-07 20:34:40 +0200327 ret = SR_OK;
Marc Schinke52e7122013-06-10 10:10:13 +0200328 devc = sdi->priv;
329
Marc Schink16e76ba2013-06-07 20:34:40 +0200330 switch (key) {
Marc Schinke52e7122013-06-10 10:10:13 +0200331 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;
Marc Schink16e76ba2013-06-07 20:34:40 +0200337 default:
338 return SR_ERR_NA;
339 }
340
341 return ret;
342}
343
Bert Vermeulen584560f2014-09-16 17:49:20 +0200344static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100345 const struct sr_channel_group *cg)
Marc Schink16e76ba2013-06-07 20:34:40 +0200346{
Marc Schinke52e7122013-06-10 10:10:13 +0200347 uint64_t samplerate, limit_samples, capture_ratio;
Marc Schink16e76ba2013-06-07 20:34:40 +0200348 int ret;
349
Uwe Hermann53b46802014-03-20 21:58:01 +0100350 (void)cg;
Bert Vermeulend3c74a62013-10-31 23:58:33 +0100351
Marc Schink16e76ba2013-06-07 20:34:40 +0200352 if (sdi->status != SR_ST_ACTIVE)
353 return SR_ERR_DEV_CLOSED;
354
355 ret = SR_OK;
Marc Schinke52e7122013-06-10 10:10:13 +0200356
Marc Schink16e76ba2013-06-07 20:34:40 +0200357 switch (key) {
Marc Schinke52e7122013-06-10 10:10:13 +0200358 case SR_CONF_LIMIT_SAMPLES:
359 limit_samples = g_variant_get_uint64(data);
Uwe Hermann79914b32013-06-11 18:55:47 +0200360 ret = sl2_set_limit_samples(sdi, limit_samples);
Marc Schinke52e7122013-06-10 10:10:13 +0200361 break;
362 case SR_CONF_SAMPLERATE:
363 samplerate = g_variant_get_uint64(data);
Uwe Hermann79914b32013-06-11 18:55:47 +0200364 ret = sl2_set_samplerate(sdi, samplerate);
Marc Schinke52e7122013-06-10 10:10:13 +0200365 break;
366 case SR_CONF_CAPTURE_RATIO:
367 capture_ratio = g_variant_get_uint64(data);
Uwe Hermann79914b32013-06-11 18:55:47 +0200368 ret = sl2_set_capture_ratio(sdi, capture_ratio);
Marc Schinke52e7122013-06-10 10:10:13 +0200369 break;
Marc Schink16e76ba2013-06-07 20:34:40 +0200370 default:
371 return SR_ERR_NA;
372 }
373
374 return ret;
375}
376
Bert Vermeulen584560f2014-09-16 17:49:20 +0200377static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100378 const struct sr_channel_group *cg)
Marc Schink16e76ba2013-06-07 20:34:40 +0200379{
Bert Vermeulenf0de2dd2014-01-19 17:18:59 +0100380 GVariant *gvar, *grange[2];
Marc Schinke52e7122013-06-10 10:10:13 +0200381 GVariantBuilder gvb;
382 int ret;
383
Marc Schink16e76ba2013-06-07 20:34:40 +0200384 (void)sdi;
Uwe Hermann53b46802014-03-20 21:58:01 +0100385 (void)cg;
Marc Schinke52e7122013-06-10 10:10:13 +0200386
387 ret = SR_OK;
388
389 switch (key) {
390 case SR_CONF_DEVICE_OPTIONS:
Bert Vermeulen584560f2014-09-16 17:49:20 +0200391 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
Bert Vermeulenf254bc42014-09-16 22:11:39 +0200392 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
Marc Schinke52e7122013-06-10 10:10:13 +0200393 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"),
Uwe Hermann79914b32013-06-11 18:55:47 +0200397 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
Marc Schinke52e7122013-06-10 10:10:13 +0200398 sizeof(uint64_t));
399 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
400 *data = g_variant_builder_end(&gvb);
401 break;
Bert Vermeulen02d5c0d2014-05-28 00:21:23 +0200402 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));
Marc Schinke52e7122013-06-10 10:10:13 +0200406 break;
Bert Vermeulenf0de2dd2014-01-19 17:18:59 +0100407 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;
Marc Schinke52e7122013-06-10 10:10:13 +0200412 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{
Marc Schinke52e7122013-06-10 10:10:13 +0200421 struct drv_context *drvc;
422 struct dev_context *devc;
423 uint16_t trigger_bytes, tmp;
424 unsigned int i, j;
425 int ret;
Marc Schink16e76ba2013-06-07 20:34:40 +0200426
427 if (sdi->status != SR_ST_ACTIVE)
428 return SR_ERR_DEV_CLOSED;
429
Marc Schinke52e7122013-06-10 10:10:13 +0200430 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 */
Bert Vermeulen02d5c0d2014-05-28 00:21:23 +0200445 sl2_convert_trigger(sdi);
Uwe Hermann79914b32013-06-11 18:55:47 +0200446 sl2_calculate_trigger_samples(sdi);
Marc Schinke52e7122013-06-10 10:10:13 +0200447
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
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100457 devc->num_enabled_channels = 0;
Marc Schinke52e7122013-06-10 10:10:13 +0200458
459 /*
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100460 * Count the number of enabled channels and number them for a sequential
Marc Schinke52e7122013-06-10 10:10:13 +0200461 * access.
462 */
Uwe Hermann3f239f02014-03-24 22:39:42 +0100463 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100464 if (devc->channels[i]->enabled) {
465 devc->num_enabled_channels++;
466 devc->channel_map[j] = i;
Marc Schinke52e7122013-06-10 10:10:13 +0200467 j++;
468 }
469 }
470
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100471 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
Marc Schinke52e7122013-06-10 10:10:13 +0200472
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
Marc Schinke52e7122013-06-10 10:10:13 +0200491 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
Uwe Hermannc824eb62013-06-11 18:55:47 +0200492 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
Marc Schinke52e7122013-06-10 10:10:13 +0200493 return SR_ERR;
494 }
495
Bert Vermeulen102f1232014-07-21 14:35:27 +0200496 usb_source_add(sdi->session, drvc->sr_ctx, 100,
497 ikalogic_scanalogic2_receive_data, (void *)sdi);
Marc Schinke52e7122013-06-10 10:10:13 +0200498
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;
Marc Schink16e76ba2013-06-07 20:34:40 +0200505
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
Marc Schinke52e7122013-06-10 10:10:13 +0200516 sr_dbg("Stopping acquisition.");
517
518 sdi->status = SR_ST_STOPPING;
Marc Schink16e76ba2013-06-07 20:34:40 +0200519
520 return SR_OK;
521}
522
523SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
524 .name = "ikalogic-scanalogic2",
Marc Schinke52e7122013-06-10 10:10:13 +0200525 .longname = "IKALOGIC Scanalogic-2",
Marc Schink16e76ba2013-06-07 20:34:40 +0200526 .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};