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