2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de>
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.
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.
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/>.
22 static const uint32_t devopts[] = {
23 SR_CONF_LOGIC_ANALYZER,
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,
30 static const int32_t trigger_matches[] = {
36 SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
50 static const char *channel_names[NUM_CHANNELS + 1] = {
55 SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
56 static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
58 static int init(struct sr_context *sr_ctx)
60 return std_init(sr_ctx, di, LOG_PREFIX);
63 static GSList *scan(GSList *options)
65 GSList *usb_devices, *devices, *l;
66 struct drv_context *drvc;
67 struct sr_dev_inst *sdi;
68 struct sr_channel *ch;
69 struct dev_context *devc;
70 struct sr_usb_dev_inst *usb;
71 struct device_info dev_info;
74 char connection_id[64];
80 drvc->instances = NULL;
82 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
84 if (usb_devices == NULL)
87 for (l = usb_devices; l; l = l->next) {
90 if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
91 sr_warn("Failed to get device information: %d.", ret);
92 sr_usb_dev_inst_free(usb);
96 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
97 sr_err("Device instance malloc failed.");
98 sr_usb_dev_inst_free(usb);
102 if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
103 sr_err("Transfer malloc failed.");
104 sr_usb_dev_inst_free(usb);
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);
117 fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major,
118 dev_info.fw_ver_minor);
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);
128 sdi = sr_dev_inst_new(SR_ST_INACTIVE, VENDOR_NAME,
129 MODEL_NAME, fw_ver_str);
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);
140 usb_get_port_path(libusb_get_device(usb->devhdl),
141 connection_id, sizeof(connection_id));
145 sdi->inst_type = SR_INST_USB;
147 sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
148 sdi->connection_id = g_strdup(connection_id);
150 for (i = 0; channel_names[i]; i++) {
151 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
153 sdi->channels = g_slist_append(sdi->channels, ch);
154 devc->channels[i] = ch;
157 devc->state = STATE_IDLE;
158 devc->next_state = STATE_IDLE;
160 /* Set default samplerate. */
161 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
163 /* Set default capture ratio. */
164 devc->capture_ratio = 0;
166 /* Set default after trigger delay. */
167 devc->after_trigger_delay = 0;
169 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
171 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
174 libusb_fill_control_setup(devc->xfer_buf_in,
175 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
176 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
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,
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;
188 drvc->instances = g_slist_append(drvc->instances, sdi);
189 devices = g_slist_append(devices, sdi);
192 g_slist_free(usb_devices);
197 static GSList *dev_list(void)
199 return ((struct drv_context *)(di->priv))->instances;
202 static void clear_dev_context(void *priv)
204 struct dev_context *devc;
208 sr_dbg("Device context cleared.");
210 libusb_free_transfer(devc->xfer_in);
211 libusb_free_transfer(devc->xfer_out);
215 static int dev_clear(void)
217 return std_dev_clear(di, &clear_dev_context);
220 static int dev_open(struct sr_dev_inst *sdi)
222 struct drv_context *drvc;
223 struct dev_context *devc;
224 struct sr_usb_dev_inst *usb;
225 uint8_t buffer[PACKET_LENGTH];
228 if (!(drvc = di->priv)) {
229 sr_err("Driver was not initialized.");
236 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
240 * Determine if a kernel driver is active on this interface and, if so,
243 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
244 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
246 sr_err("Failed to detach kernel driver: %s.",
247 libusb_error_name(ret));
252 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
253 sr_err("Failed to claim interface: %s.",
254 libusb_error_name(ret));
258 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
259 devc->xfer_buf_in, sl2_receive_transfer_in,
262 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
263 devc->xfer_buf_out, sl2_receive_transfer_out,
266 memset(buffer, 0, sizeof(buffer));
268 buffer[0] = CMD_RESET;
269 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
270 sr_err("Device reset failed: %s.", libusb_error_name(ret));
275 * Set the device to idle state. If the device is not in idle state it
276 * possibly will reset itself after a few seconds without being used
277 * and thereby close the connection.
279 buffer[0] = CMD_IDLE;
280 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
281 sr_err("Failed to set device in idle state: %s.",
282 libusb_error_name(ret));
286 sdi->status = SR_ST_ACTIVE;
291 static int dev_close(struct sr_dev_inst *sdi)
293 struct sr_usb_dev_inst *usb;
296 sr_err("Driver was not initialized.");
305 libusb_release_interface(usb->devhdl, USB_INTERFACE);
306 libusb_close(usb->devhdl);
309 sdi->status = SR_ST_INACTIVE;
314 static int cleanup(void)
319 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
320 const struct sr_channel_group *cg)
322 struct dev_context *devc;
331 case SR_CONF_SAMPLERATE:
332 *data = g_variant_new_uint64(devc->samplerate);
334 case SR_CONF_CAPTURE_RATIO:
335 *data = g_variant_new_uint64(devc->capture_ratio);
344 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
345 const struct sr_channel_group *cg)
347 uint64_t samplerate, limit_samples, capture_ratio;
352 if (sdi->status != SR_ST_ACTIVE)
353 return SR_ERR_DEV_CLOSED;
358 case SR_CONF_LIMIT_SAMPLES:
359 limit_samples = g_variant_get_uint64(data);
360 ret = sl2_set_limit_samples(sdi, limit_samples);
362 case SR_CONF_SAMPLERATE:
363 samplerate = g_variant_get_uint64(data);
364 ret = sl2_set_samplerate(sdi, samplerate);
366 case SR_CONF_CAPTURE_RATIO:
367 capture_ratio = g_variant_get_uint64(data);
368 ret = sl2_set_capture_ratio(sdi, capture_ratio);
377 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
378 const struct sr_channel_group *cg)
380 GVariant *gvar, *grange[2];
390 case SR_CONF_DEVICE_OPTIONS:
391 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
392 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
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"),
397 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
399 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
400 *data = g_variant_builder_end(&gvb);
402 case SR_CONF_TRIGGER_MATCH:
403 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
404 trigger_matches, ARRAY_SIZE(trigger_matches),
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);
419 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
421 struct drv_context *drvc;
422 struct dev_context *devc;
423 uint16_t trigger_bytes, tmp;
427 if (sdi->status != SR_ST_ACTIVE)
428 return SR_ERR_DEV_CLOSED;
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;
439 devc->sample_packet = 0;
442 * The trigger must be configured first because the calculation of the
443 * pre and post trigger samples depends on a configured trigger.
445 sl2_convert_trigger(sdi);
446 sl2_calculate_trigger_samples(sdi);
448 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
450 /* Calculate the number of expected sample packets. */
451 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
453 /* Round up the number of expected sample packets. */
454 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
455 devc->num_sample_packets++;
457 devc->num_enabled_channels = 0;
460 * Count the number of enabled channels and number them for a sequential
463 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
464 if (devc->channels[i]->enabled) {
465 devc->num_enabled_channels++;
466 devc->channel_map[j] = i;
471 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
473 /* Set up the transfer buffer for the acquisition. */
474 devc->xfer_data_out[0] = CMD_SAMPLE;
475 devc->xfer_data_out[1] = 0x00;
477 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
478 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
480 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
481 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
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;
488 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
489 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
491 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
492 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
496 usb_source_add(sdi->session, drvc->sr_ctx, 100,
497 ikalogic_scanalogic2_receive_data, (void *)sdi);
499 sr_dbg("Acquisition started successfully.");
501 /* Send header packet to the session bus. */
502 std_session_send_df_header(cb_data, LOG_PREFIX);
504 devc->next_state = STATE_SAMPLE;
509 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513 if (sdi->status != SR_ST_ACTIVE)
514 return SR_ERR_DEV_CLOSED;
516 sr_dbg("Stopping acquisition.");
518 sdi->status = SR_ST_STOPPING;
523 SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
524 .name = "ikalogic-scanalogic2",
525 .longname = "IKALOGIC Scanalogic-2",
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,