]> sigrok.org Git - libsigrok.git/blame - src/hardware/ikalogic-scanalogic2/api.c
Removal of sdi->index, step 5: fix ikalogic-scanalogic2
[libsigrok.git] / src / hardware / ikalogic-scanalogic2 / api.c
CommitLineData
16e76bae
MS
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
f254bc4b 22static const uint32_t devopts[] = {
e52e712d 23 SR_CONF_LOGIC_ANALYZER,
5827f61b
BV
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,
e52e712d
MS
28};
29
02d5c0d8
BV
30static const int32_t trigger_matches[] = {
31 SR_TRIGGER_RISING,
32 SR_TRIGGER_FALLING,
33 SR_TRIGGER_EDGE,
34};
35
79914b3a 36SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
e52e712d
MS
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),
c824eb63 47 SR_MHZ(20),
e52e712d
MS
48};
49
3f239f08 50static const char *channel_names[NUM_CHANNELS + 1] = {
e52e712d 51 "0", "1", "2", "3",
c824eb63 52 NULL,
e52e712d
MS
53};
54
16e76bae
MS
55SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
56static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
57
16e76bae
MS
58static int init(struct sr_context *sr_ctx)
59{
e52e712d 60 return std_init(sr_ctx, di, LOG_PREFIX);
16e76bae
MS
61}
62
63static GSList *scan(GSList *options)
64{
e52e712d 65 GSList *usb_devices, *devices, *l;
16e76bae 66 struct drv_context *drvc;
e52e712d 67 struct sr_dev_inst *sdi;
ba7dd8bb 68 struct sr_channel *ch;
e52e712d
MS
69 struct dev_context *devc;
70 struct sr_usb_dev_inst *usb;
71 struct device_info dev_info;
65340dd5 72 int ret, i;
e52e712d 73 char *fw_ver_str;
65340dd5 74 char connection_id[64];
16e76bae
MS
75
76 (void)options;
77
78 devices = NULL;
79 drvc = di->priv;
80 drvc->instances = NULL;
e52e712d
MS
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
c824eb63 87 for (l = usb_devices; l; l = l->next) {
e52e712d
MS
88 usb = l->data;
89
79914b3a
UH
90 if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
91 sr_warn("Failed to get device information: %d.", ret);
e52e712d
MS
92 sr_usb_dev_inst_free(usb);
93 continue;
94 }
95
c824eb63 96 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
e52e712d
MS
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);
e52e712d
MS
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
65340dd5 128 sdi = sr_dev_inst_new(SR_ST_INACTIVE, VENDOR_NAME,
e52e712d 129 MODEL_NAME, fw_ver_str);
e52e712d 130 g_free(fw_ver_str);
e52e712d
MS
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
65340dd5
SA
140 usb_get_port_path(libusb_get_device(usb->devhdl),
141 connection_id, sizeof(connection_id));
142
e52e712d
MS
143 sdi->priv = devc;
144 sdi->driver = di;
145 sdi->inst_type = SR_INST_USB;
146 sdi->conn = usb;
65340dd5
SA
147 sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
148 sdi->connection_id = g_strdup(connection_id);
e52e712d 149
ba7dd8bb 150 for (i = 0; channel_names[i]; i++) {
3f239f08 151 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
ba7dd8bb
UH
152 channel_names[i]);
153 sdi->channels = g_slist_append(sdi->channels, ch);
154 devc->channels[i] = ch;
e52e712d
MS
155 }
156
157 devc->state = STATE_IDLE;
158 devc->next_state = STATE_IDLE;
159
160 /* Set default samplerate. */
79914b3a 161 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
e52e712d
MS
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,
fb8d593c 175 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
e52e712d
MS
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);
e52e712d 190 }
16e76bae 191
e52e712d 192 g_slist_free(usb_devices);
16e76bae
MS
193
194 return devices;
195}
196
197static GSList *dev_list(void)
198{
c824eb63 199 return ((struct drv_context *)(di->priv))->instances;
16e76bae
MS
200}
201
e52e712d
MS
202static void clear_dev_context(void *priv)
203{
c824eb63
UH
204 struct dev_context *devc;
205
206 devc = priv;
e52e712d 207
c824eb63 208 sr_dbg("Device context cleared.");
e52e712d
MS
209
210 libusb_free_transfer(devc->xfer_in);
211 libusb_free_transfer(devc->xfer_out);
212 g_free(devc);
213}
214
16e76bae
MS
215static int dev_clear(void)
216{
e52e712d 217 return std_dev_clear(di, &clear_dev_context);
16e76bae
MS
218}
219
220static int dev_open(struct sr_dev_inst *sdi)
221{
e52e712d
MS
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;
16e76bae 235
e52e712d
MS
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);
e52e712d 245 if (ret < 0) {
9526f2d4 246 sr_err("Failed to detach kernel driver: %s.",
e52e712d
MS
247 libusb_error_name(ret));
248 return SR_ERR;
249 }
250 }
251
79914b3a 252 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
e52e712d
MS
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,
79914b3a 259 devc->xfer_buf_in, sl2_receive_transfer_in,
e52e712d
MS
260 sdi, USB_TIMEOUT);
261
262 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
79914b3a 263 devc->xfer_buf_out, sl2_receive_transfer_out,
e52e712d
MS
264 sdi, USB_TIMEOUT);
265
266 memset(buffer, 0, sizeof(buffer));
267
268 buffer[0] = CMD_RESET;
79914b3a 269 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
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
c824eb63
UH
276 * possibly will reset itself after a few seconds without being used
277 * and thereby close the connection.
e52e712d
MS
278 */
279 buffer[0] = CMD_IDLE;
79914b3a 280 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
281 sr_err("Failed to set device in idle state: %s.",
282 libusb_error_name(ret));
283 return SR_ERR;
284 }
16e76bae
MS
285
286 sdi->status = SR_ST_ACTIVE;
287
288 return SR_OK;
289}
290
291static int dev_close(struct sr_dev_inst *sdi)
292{
e52e712d
MS
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;
16e76bae 304
e52e712d
MS
305 libusb_release_interface(usb->devhdl, USB_INTERFACE);
306 libusb_close(usb->devhdl);
16e76bae 307
e52e712d 308 usb->devhdl = NULL;
16e76bae
MS
309 sdi->status = SR_ST_INACTIVE;
310
311 return SR_OK;
312}
313
314static int cleanup(void)
315{
c824eb63 316 return dev_clear();
16e76bae
MS
317}
318
584560f1 319static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 320 const struct sr_channel_group *cg)
16e76bae 321{
e52e712d 322 struct dev_context *devc;
16e76bae
MS
323 int ret;
324
53b4680f 325 (void)cg;
d3c74a6f 326
16e76bae 327 ret = SR_OK;
e52e712d
MS
328 devc = sdi->priv;
329
16e76bae 330 switch (key) {
e52e712d
MS
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;
16e76bae
MS
337 default:
338 return SR_ERR_NA;
339 }
340
341 return ret;
342}
343
584560f1 344static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 345 const struct sr_channel_group *cg)
16e76bae 346{
e52e712d 347 uint64_t samplerate, limit_samples, capture_ratio;
16e76bae
MS
348 int ret;
349
53b4680f 350 (void)cg;
d3c74a6f 351
16e76bae
MS
352 if (sdi->status != SR_ST_ACTIVE)
353 return SR_ERR_DEV_CLOSED;
354
355 ret = SR_OK;
e52e712d 356
16e76bae 357 switch (key) {
e52e712d
MS
358 case SR_CONF_LIMIT_SAMPLES:
359 limit_samples = g_variant_get_uint64(data);
79914b3a 360 ret = sl2_set_limit_samples(sdi, limit_samples);
e52e712d
MS
361 break;
362 case SR_CONF_SAMPLERATE:
363 samplerate = g_variant_get_uint64(data);
79914b3a 364 ret = sl2_set_samplerate(sdi, samplerate);
e52e712d
MS
365 break;
366 case SR_CONF_CAPTURE_RATIO:
367 capture_ratio = g_variant_get_uint64(data);
79914b3a 368 ret = sl2_set_capture_ratio(sdi, capture_ratio);
e52e712d 369 break;
16e76bae 370 default:
e52e712d 371 return SR_ERR_NA;
16e76bae
MS
372 }
373
374 return ret;
375}
376
584560f1 377static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 378 const struct sr_channel_group *cg)
16e76bae 379{
f0de2dd0 380 GVariant *gvar, *grange[2];
e52e712d 381 GVariantBuilder gvb;
16e76bae
MS
382 int ret;
383
384 (void)sdi;
53b4680f 385 (void)cg;
16e76bae
MS
386
387 ret = SR_OK;
e52e712d 388
16e76bae 389 switch (key) {
e52e712d 390 case SR_CONF_DEVICE_OPTIONS:
584560f1 391 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 392 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
e52e712d
MS
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"),
79914b3a 397 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
e52e712d
MS
398 sizeof(uint64_t));
399 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
400 *data = g_variant_builder_end(&gvb);
401 break;
02d5c0d8
BV
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));
e52e712d 406 break;
f0de2dd0
BV
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;
16e76bae
MS
412 default:
413 return SR_ERR_NA;
414 }
415
416 return ret;
417}
418
e52e712d 419static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
16e76bae 420{
e52e712d
MS
421 struct drv_context *drvc;
422 struct dev_context *devc;
423 uint16_t trigger_bytes, tmp;
424 unsigned int i, j;
425 int ret;
16e76bae
MS
426
427 if (sdi->status != SR_ST_ACTIVE)
428 return SR_ERR_DEV_CLOSED;
429
e52e712d
MS
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 */
02d5c0d8 445 sl2_convert_trigger(sdi);
79914b3a 446 sl2_calculate_trigger_samples(sdi);
e52e712d
MS
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
ba7dd8bb 457 devc->num_enabled_channels = 0;
e52e712d
MS
458
459 /*
ba7dd8bb 460 * Count the number of enabled channels and number them for a sequential
e52e712d
MS
461 * access.
462 */
3f239f08 463 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
ba7dd8bb
UH
464 if (devc->channels[i]->enabled) {
465 devc->num_enabled_channels++;
466 devc->channel_map[j] = i;
e52e712d
MS
467 j++;
468 }
469 }
470
ba7dd8bb 471 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
e52e712d
MS
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
e52e712d 491 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
c824eb63 492 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
e52e712d
MS
493 return SR_ERR;
494 }
495
102f1239
BV
496 usb_source_add(sdi->session, drvc->sr_ctx, 100,
497 ikalogic_scanalogic2_receive_data, (void *)sdi);
e52e712d
MS
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;
16e76bae
MS
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
e52e712d
MS
516 sr_dbg("Stopping acquisition.");
517
518 sdi->status = SR_ST_STOPPING;
16e76bae
MS
519
520 return SR_OK;
521}
522
523SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
524 .name = "ikalogic-scanalogic2",
e52e712d 525 .longname = "IKALOGIC Scanalogic-2",
16e76bae
MS
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};