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