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