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