blob: 44eb47552a4a3e75190b0f0a67675f7a85e9c233 [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,
24 SR_CONF_SAMPLERATE,
25 SR_CONF_LIMIT_SAMPLES,
Bert Vermeulen02d5c0d2014-05-28 00:21:23 +020026 SR_CONF_TRIGGER_MATCH,
Uwe Hermannc824eb62013-06-11 18:55:47 +020027 SR_CONF_CAPTURE_RATIO,
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;
72 int ret, device_index, i;
73 char *fw_ver_str;
Marc Schink16e76ba2013-06-07 20:34:40 +020074
75 (void)options;
76
77 devices = NULL;
78 drvc = di->priv;
79 drvc->instances = NULL;
Marc Schinke52e7122013-06-10 10:10:13 +020080 device_index = 0;
Marc Schink16e76ba2013-06-07 20:34:40 +020081
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
128 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE, VENDOR_NAME,
129 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
140 sdi->priv = devc;
141 sdi->driver = di;
142 sdi->inst_type = SR_INST_USB;
143 sdi->conn = usb;
144
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100145 for (i = 0; channel_names[i]; i++) {
Uwe Hermann3f239f02014-03-24 22:39:42 +0100146 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100147 channel_names[i]);
148 sdi->channels = g_slist_append(sdi->channels, ch);
149 devc->channels[i] = ch;
Marc Schinke52e7122013-06-10 10:10:13 +0200150 }
151
152 devc->state = STATE_IDLE;
153 devc->next_state = STATE_IDLE;
154
155 /* Set default samplerate. */
Uwe Hermann79914b32013-06-11 18:55:47 +0200156 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
Marc Schinke52e7122013-06-10 10:10:13 +0200157
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,
Marc Schinkfb8d5932013-07-02 21:52:23 +0200170 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
Marc Schinke52e7122013-06-10 10:10:13 +0200171 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 device_index++;
187 }
188
189 g_slist_free(usb_devices);
Marc Schink16e76ba2013-06-07 20:34:40 +0200190
191 return devices;
192}
193
194static GSList *dev_list(void)
195{
Uwe Hermannc824eb62013-06-11 18:55:47 +0200196 return ((struct drv_context *)(di->priv))->instances;
Marc Schink16e76ba2013-06-07 20:34:40 +0200197}
198
Marc Schinke52e7122013-06-10 10:10:13 +0200199static void clear_dev_context(void *priv)
200{
Uwe Hermannc824eb62013-06-11 18:55:47 +0200201 struct dev_context *devc;
Marc Schinke52e7122013-06-10 10:10:13 +0200202
Uwe Hermannc824eb62013-06-11 18:55:47 +0200203 devc = priv;
204
205 sr_dbg("Device context cleared.");
Marc Schinke52e7122013-06-10 10:10:13 +0200206
207 libusb_free_transfer(devc->xfer_in);
208 libusb_free_transfer(devc->xfer_out);
209 g_free(devc);
210}
211
Marc Schink16e76ba2013-06-07 20:34:40 +0200212static int dev_clear(void)
213{
Marc Schinke52e7122013-06-10 10:10:13 +0200214 return std_dev_clear(di, &clear_dev_context);
Marc Schink16e76ba2013-06-07 20:34:40 +0200215}
216
217static int dev_open(struct sr_dev_inst *sdi)
218{
Marc Schinke52e7122013-06-10 10:10:13 +0200219 struct drv_context *drvc;
220 struct dev_context *devc;
221 struct sr_usb_dev_inst *usb;
222 uint8_t buffer[PACKET_LENGTH];
223 int ret;
Marc Schink16e76ba2013-06-07 20:34:40 +0200224
Marc Schinke52e7122013-06-10 10:10:13 +0200225 if (!(drvc = di->priv)) {
226 sr_err("Driver was not initialized.");
227 return SR_ERR;
228 }
229
230 usb = sdi->conn;
231 devc = sdi->priv;
232
233 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
234 return SR_ERR;
235
236 /*
237 * Determine if a kernel driver is active on this interface and, if so,
238 * detach it.
239 */
240 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
241 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
Marc Schinke52e7122013-06-10 10:10:13 +0200242 if (ret < 0) {
Uwe Hermann9526f2d2013-06-11 18:55:47 +0200243 sr_err("Failed to detach kernel driver: %s.",
Marc Schinke52e7122013-06-10 10:10:13 +0200244 libusb_error_name(ret));
245 return SR_ERR;
246 }
247 }
248
Uwe Hermann79914b32013-06-11 18:55:47 +0200249 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
Marc Schinke52e7122013-06-10 10:10:13 +0200250 sr_err("Failed to claim interface: %s.",
251 libusb_error_name(ret));
252 return SR_ERR;
253 }
254
255 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
Uwe Hermann79914b32013-06-11 18:55:47 +0200256 devc->xfer_buf_in, sl2_receive_transfer_in,
Marc Schinke52e7122013-06-10 10:10:13 +0200257 sdi, USB_TIMEOUT);
258
259 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
Uwe Hermann79914b32013-06-11 18:55:47 +0200260 devc->xfer_buf_out, sl2_receive_transfer_out,
Marc Schinke52e7122013-06-10 10:10:13 +0200261 sdi, USB_TIMEOUT);
262
263 memset(buffer, 0, sizeof(buffer));
264
265 buffer[0] = CMD_RESET;
Uwe Hermann79914b32013-06-11 18:55:47 +0200266 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
Marc Schinke52e7122013-06-10 10:10:13 +0200267 sr_err("Device reset failed: %s.", libusb_error_name(ret));
268 return SR_ERR;
269 }
270
271 /*
272 * Set the device to idle state. If the device is not in idle state it
Uwe Hermannc824eb62013-06-11 18:55:47 +0200273 * possibly will reset itself after a few seconds without being used
274 * and thereby close the connection.
Marc Schinke52e7122013-06-10 10:10:13 +0200275 */
276 buffer[0] = CMD_IDLE;
Uwe Hermann79914b32013-06-11 18:55:47 +0200277 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
Marc Schinke52e7122013-06-10 10:10:13 +0200278 sr_err("Failed to set device in idle state: %s.",
279 libusb_error_name(ret));
280 return SR_ERR;
281 }
Marc Schink16e76ba2013-06-07 20:34:40 +0200282
283 sdi->status = SR_ST_ACTIVE;
284
285 return SR_OK;
286}
287
288static int dev_close(struct sr_dev_inst *sdi)
289{
Marc Schinke52e7122013-06-10 10:10:13 +0200290 struct sr_usb_dev_inst *usb;
Marc Schink16e76ba2013-06-07 20:34:40 +0200291
Marc Schinke52e7122013-06-10 10:10:13 +0200292 if (!di->priv) {
293 sr_err("Driver was not initialized.");
294 return SR_ERR;
295 }
Marc Schink16e76ba2013-06-07 20:34:40 +0200296
Marc Schinke52e7122013-06-10 10:10:13 +0200297 usb = sdi->conn;
298
299 if (!usb->devhdl)
300 return SR_OK;
301
302 libusb_release_interface(usb->devhdl, USB_INTERFACE);
303 libusb_close(usb->devhdl);
304
305 usb->devhdl = NULL;
Marc Schink16e76ba2013-06-07 20:34:40 +0200306 sdi->status = SR_ST_INACTIVE;
307
308 return SR_OK;
309}
310
311static int cleanup(void)
312{
Uwe Hermannc824eb62013-06-11 18:55:47 +0200313 return dev_clear();
Marc Schink16e76ba2013-06-07 20:34:40 +0200314}
315
Bert Vermeulen584560f2014-09-16 17:49:20 +0200316static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100317 const struct sr_channel_group *cg)
Marc Schink16e76ba2013-06-07 20:34:40 +0200318{
Marc Schinke52e7122013-06-10 10:10:13 +0200319 struct dev_context *devc;
Marc Schink16e76ba2013-06-07 20:34:40 +0200320 int ret;
321
Uwe Hermann53b46802014-03-20 21:58:01 +0100322 (void)cg;
Bert Vermeulend3c74a62013-10-31 23:58:33 +0100323
Marc Schink16e76ba2013-06-07 20:34:40 +0200324 ret = SR_OK;
Marc Schinke52e7122013-06-10 10:10:13 +0200325 devc = sdi->priv;
326
Marc Schink16e76ba2013-06-07 20:34:40 +0200327 switch (key) {
Marc Schinke52e7122013-06-10 10:10:13 +0200328 case SR_CONF_SAMPLERATE:
329 *data = g_variant_new_uint64(devc->samplerate);
330 break;
331 case SR_CONF_CAPTURE_RATIO:
332 *data = g_variant_new_uint64(devc->capture_ratio);
333 break;
Marc Schink16e76ba2013-06-07 20:34:40 +0200334 default:
335 return SR_ERR_NA;
336 }
337
338 return ret;
339}
340
Bert Vermeulen584560f2014-09-16 17:49:20 +0200341static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100342 const struct sr_channel_group *cg)
Marc Schink16e76ba2013-06-07 20:34:40 +0200343{
Marc Schinke52e7122013-06-10 10:10:13 +0200344 uint64_t samplerate, limit_samples, capture_ratio;
Marc Schink16e76ba2013-06-07 20:34:40 +0200345 int ret;
346
Uwe Hermann53b46802014-03-20 21:58:01 +0100347 (void)cg;
Bert Vermeulend3c74a62013-10-31 23:58:33 +0100348
Marc Schink16e76ba2013-06-07 20:34:40 +0200349 if (sdi->status != SR_ST_ACTIVE)
350 return SR_ERR_DEV_CLOSED;
351
352 ret = SR_OK;
Marc Schinke52e7122013-06-10 10:10:13 +0200353
Marc Schink16e76ba2013-06-07 20:34:40 +0200354 switch (key) {
Marc Schinke52e7122013-06-10 10:10:13 +0200355 case SR_CONF_LIMIT_SAMPLES:
356 limit_samples = g_variant_get_uint64(data);
Uwe Hermann79914b32013-06-11 18:55:47 +0200357 ret = sl2_set_limit_samples(sdi, limit_samples);
Marc Schinke52e7122013-06-10 10:10:13 +0200358 break;
359 case SR_CONF_SAMPLERATE:
360 samplerate = g_variant_get_uint64(data);
Uwe Hermann79914b32013-06-11 18:55:47 +0200361 ret = sl2_set_samplerate(sdi, samplerate);
Marc Schinke52e7122013-06-10 10:10:13 +0200362 break;
363 case SR_CONF_CAPTURE_RATIO:
364 capture_ratio = g_variant_get_uint64(data);
Uwe Hermann79914b32013-06-11 18:55:47 +0200365 ret = sl2_set_capture_ratio(sdi, capture_ratio);
Marc Schinke52e7122013-06-10 10:10:13 +0200366 break;
Marc Schink16e76ba2013-06-07 20:34:40 +0200367 default:
368 return SR_ERR_NA;
369 }
370
371 return ret;
372}
373
Bert Vermeulen584560f2014-09-16 17:49:20 +0200374static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100375 const struct sr_channel_group *cg)
Marc Schink16e76ba2013-06-07 20:34:40 +0200376{
Bert Vermeulenf0de2dd2014-01-19 17:18:59 +0100377 GVariant *gvar, *grange[2];
Marc Schinke52e7122013-06-10 10:10:13 +0200378 GVariantBuilder gvb;
379 int ret;
380
Marc Schink16e76ba2013-06-07 20:34:40 +0200381 (void)sdi;
Uwe Hermann53b46802014-03-20 21:58:01 +0100382 (void)cg;
Marc Schinke52e7122013-06-10 10:10:13 +0200383
384 ret = SR_OK;
385
386 switch (key) {
387 case SR_CONF_DEVICE_OPTIONS:
Bert Vermeulen584560f2014-09-16 17:49:20 +0200388 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
Bert Vermeulenf254bc42014-09-16 22:11:39 +0200389 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
Marc Schinke52e7122013-06-10 10:10:13 +0200390 break;
391 case SR_CONF_SAMPLERATE:
392 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
393 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
Uwe Hermann79914b32013-06-11 18:55:47 +0200394 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
Marc Schinke52e7122013-06-10 10:10:13 +0200395 sizeof(uint64_t));
396 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
397 *data = g_variant_builder_end(&gvb);
398 break;
Bert Vermeulen02d5c0d2014-05-28 00:21:23 +0200399 case SR_CONF_TRIGGER_MATCH:
400 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
401 trigger_matches, ARRAY_SIZE(trigger_matches),
402 sizeof(int32_t));
Marc Schinke52e7122013-06-10 10:10:13 +0200403 break;
Bert Vermeulenf0de2dd2014-01-19 17:18:59 +0100404 case SR_CONF_LIMIT_SAMPLES:
405 grange[0] = g_variant_new_uint64(0);
406 grange[1] = g_variant_new_uint64(MAX_SAMPLES);
407 *data = g_variant_new_tuple(grange, 2);
408 break;
Marc Schinke52e7122013-06-10 10:10:13 +0200409 default:
410 return SR_ERR_NA;
411 }
412
413 return ret;
414}
415
416static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
417{
Marc Schinke52e7122013-06-10 10:10:13 +0200418 struct drv_context *drvc;
419 struct dev_context *devc;
420 uint16_t trigger_bytes, tmp;
421 unsigned int i, j;
422 int ret;
Marc Schink16e76ba2013-06-07 20:34:40 +0200423
424 if (sdi->status != SR_ST_ACTIVE)
425 return SR_ERR_DEV_CLOSED;
426
Marc Schinke52e7122013-06-10 10:10:13 +0200427 devc = sdi->priv;
428 drvc = di->priv;
429
430 devc->cb_data = cb_data;
431 devc->wait_data_ready_locked = TRUE;
432 devc->stopping_in_progress = FALSE;
433 devc->transfer_error = FALSE;
434 devc->samples_processed = 0;
435 devc->channel = 0;
436 devc->sample_packet = 0;
437
438 /*
439 * The trigger must be configured first because the calculation of the
440 * pre and post trigger samples depends on a configured trigger.
441 */
Bert Vermeulen02d5c0d2014-05-28 00:21:23 +0200442 sl2_convert_trigger(sdi);
Uwe Hermann79914b32013-06-11 18:55:47 +0200443 sl2_calculate_trigger_samples(sdi);
Marc Schinke52e7122013-06-10 10:10:13 +0200444
445 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
446
447 /* Calculate the number of expected sample packets. */
448 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
449
450 /* Round up the number of expected sample packets. */
451 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
452 devc->num_sample_packets++;
453
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100454 devc->num_enabled_channels = 0;
Marc Schinke52e7122013-06-10 10:10:13 +0200455
456 /*
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100457 * Count the number of enabled channels and number them for a sequential
Marc Schinke52e7122013-06-10 10:10:13 +0200458 * access.
459 */
Uwe Hermann3f239f02014-03-24 22:39:42 +0100460 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100461 if (devc->channels[i]->enabled) {
462 devc->num_enabled_channels++;
463 devc->channel_map[j] = i;
Marc Schinke52e7122013-06-10 10:10:13 +0200464 j++;
465 }
466 }
467
Uwe Hermannba7dd8b2014-03-24 21:34:20 +0100468 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
Marc Schinke52e7122013-06-10 10:10:13 +0200469
470 /* Set up the transfer buffer for the acquisition. */
471 devc->xfer_data_out[0] = CMD_SAMPLE;
472 devc->xfer_data_out[1] = 0x00;
473
474 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
475 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
476
477 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
478 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
479
480 devc->xfer_data_out[6] = devc->samplerate_id;
481 devc->xfer_data_out[7] = devc->trigger_type;
482 devc->xfer_data_out[8] = devc->trigger_channel;
483 devc->xfer_data_out[9] = 0x00;
484
485 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
486 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
487
Marc Schinke52e7122013-06-10 10:10:13 +0200488 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
Uwe Hermannc824eb62013-06-11 18:55:47 +0200489 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
Marc Schinke52e7122013-06-10 10:10:13 +0200490 return SR_ERR;
491 }
492
Bert Vermeulen102f1232014-07-21 14:35:27 +0200493 usb_source_add(sdi->session, drvc->sr_ctx, 100,
494 ikalogic_scanalogic2_receive_data, (void *)sdi);
Marc Schinke52e7122013-06-10 10:10:13 +0200495
496 sr_dbg("Acquisition started successfully.");
497
498 /* Send header packet to the session bus. */
499 std_session_send_df_header(cb_data, LOG_PREFIX);
500
501 devc->next_state = STATE_SAMPLE;
Marc Schink16e76ba2013-06-07 20:34:40 +0200502
503 return SR_OK;
504}
505
506static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
507{
508 (void)cb_data;
509
510 if (sdi->status != SR_ST_ACTIVE)
511 return SR_ERR_DEV_CLOSED;
512
Marc Schinke52e7122013-06-10 10:10:13 +0200513 sr_dbg("Stopping acquisition.");
514
515 sdi->status = SR_ST_STOPPING;
Marc Schink16e76ba2013-06-07 20:34:40 +0200516
517 return SR_OK;
518}
519
520SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
521 .name = "ikalogic-scanalogic2",
Marc Schinke52e7122013-06-10 10:10:13 +0200522 .longname = "IKALOGIC Scanalogic-2",
Marc Schink16e76ba2013-06-07 20:34:40 +0200523 .api_version = 1,
524 .init = init,
525 .cleanup = cleanup,
526 .scan = scan,
527 .dev_list = dev_list,
528 .dev_clear = dev_clear,
529 .config_get = config_get,
530 .config_set = config_set,
531 .config_list = config_list,
532 .dev_open = dev_open,
533 .dev_close = dev_close,
534 .dev_acquisition_start = dev_acquisition_start,
535 .dev_acquisition_stop = dev_acquisition_stop,
536 .priv = NULL,
537};