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 int hwcaps[] = {
23 SR_CONF_LOGIC_ANALYZER,
25 SR_CONF_LIMIT_SAMPLES,
27 SR_CONF_CAPTURE_RATIO,
30 SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
44 static const char *probe_names[NUM_PROBES + 1] = {
49 SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
50 static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
52 static int init(struct sr_context *sr_ctx)
54 return std_init(sr_ctx, di, LOG_PREFIX);
57 static GSList *scan(GSList *options)
59 GSList *usb_devices, *devices, *l;
60 struct drv_context *drvc;
61 struct sr_dev_inst *sdi;
62 struct sr_probe *probe;
63 struct dev_context *devc;
64 struct sr_usb_dev_inst *usb;
65 struct device_info dev_info;
66 int ret, device_index, i;
73 drvc->instances = NULL;
76 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
78 if (usb_devices == NULL)
81 for (l = usb_devices; l; l = l->next) {
84 if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
85 sr_warn("Failed to get device information: %d.", ret);
86 sr_usb_dev_inst_free(usb);
90 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
91 sr_err("Device instance malloc failed.");
92 sr_usb_dev_inst_free(usb);
96 if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
97 sr_err("Transfer malloc failed.");
98 sr_usb_dev_inst_free(usb);
103 if (!(devc->xfer_out = libusb_alloc_transfer(0))) {
104 sr_err("Transfer malloc failed.");
105 sr_usb_dev_inst_free(usb);
106 libusb_free_transfer(devc->xfer_in);
111 fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major,
112 dev_info.fw_ver_minor);
114 sr_err("Firmware string malloc failed.");
115 sr_usb_dev_inst_free(usb);
116 libusb_free_transfer(devc->xfer_in);
117 libusb_free_transfer(devc->xfer_out);
122 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE, VENDOR_NAME,
123 MODEL_NAME, fw_ver_str);
126 sr_err("sr_dev_inst_new failed.");
127 sr_usb_dev_inst_free(usb);
128 libusb_free_transfer(devc->xfer_in);
129 libusb_free_transfer(devc->xfer_out);
136 sdi->inst_type = SR_INST_USB;
139 for (i = 0; probe_names[i]; i++) {
140 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
142 sdi->probes = g_slist_append(sdi->probes, probe);
143 devc->probes[i] = probe;
146 devc->state = STATE_IDLE;
147 devc->next_state = STATE_IDLE;
149 /* Set default samplerate. */
150 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
152 /* Set default capture ratio. */
153 devc->capture_ratio = 0;
155 /* Set default after trigger delay. */
156 devc->after_trigger_delay = 0;
158 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
160 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
163 libusb_fill_control_setup(devc->xfer_buf_in,
164 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
165 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
167 libusb_fill_control_setup(devc->xfer_buf_out,
168 USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
169 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
172 devc->xfer_data_in = devc->xfer_buf_in +
173 LIBUSB_CONTROL_SETUP_SIZE;
174 devc->xfer_data_out = devc->xfer_buf_out +
175 LIBUSB_CONTROL_SETUP_SIZE;
177 drvc->instances = g_slist_append(drvc->instances, sdi);
178 devices = g_slist_append(devices, sdi);
183 g_slist_free(usb_devices);
188 static GSList *dev_list(void)
190 return ((struct drv_context *)(di->priv))->instances;
193 static void clear_dev_context(void *priv)
195 struct dev_context *devc;
199 sr_dbg("Device context cleared.");
201 libusb_free_transfer(devc->xfer_in);
202 libusb_free_transfer(devc->xfer_out);
206 static int dev_clear(void)
208 return std_dev_clear(di, &clear_dev_context);
211 static int dev_open(struct sr_dev_inst *sdi)
213 struct drv_context *drvc;
214 struct dev_context *devc;
215 struct sr_usb_dev_inst *usb;
216 uint8_t buffer[PACKET_LENGTH];
219 if (!(drvc = di->priv)) {
220 sr_err("Driver was not initialized.");
227 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
231 * Determine if a kernel driver is active on this interface and, if so,
234 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
235 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
237 sr_err("Failed to detach kernel driver: %s.",
238 libusb_error_name(ret));
243 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
244 sr_err("Failed to claim interface: %s.",
245 libusb_error_name(ret));
249 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
250 devc->xfer_buf_in, sl2_receive_transfer_in,
253 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
254 devc->xfer_buf_out, sl2_receive_transfer_out,
257 memset(buffer, 0, sizeof(buffer));
259 buffer[0] = CMD_RESET;
260 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
261 sr_err("Device reset failed: %s.", libusb_error_name(ret));
266 * Set the device to idle state. If the device is not in idle state it
267 * possibly will reset itself after a few seconds without being used
268 * and thereby close the connection.
270 buffer[0] = CMD_IDLE;
271 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
272 sr_err("Failed to set device in idle state: %s.",
273 libusb_error_name(ret));
277 sdi->status = SR_ST_ACTIVE;
282 static int dev_close(struct sr_dev_inst *sdi)
284 struct sr_usb_dev_inst *usb;
287 sr_err("Driver was not initialized.");
296 libusb_release_interface(usb->devhdl, USB_INTERFACE);
297 libusb_close(usb->devhdl);
300 sdi->status = SR_ST_INACTIVE;
305 static int cleanup(void)
310 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
311 const struct sr_probe_group *probe_group)
313 struct dev_context *devc;
322 case SR_CONF_SAMPLERATE:
323 *data = g_variant_new_uint64(devc->samplerate);
325 case SR_CONF_CAPTURE_RATIO:
326 *data = g_variant_new_uint64(devc->capture_ratio);
335 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
336 const struct sr_probe_group *probe_group)
338 uint64_t samplerate, limit_samples, capture_ratio;
343 if (sdi->status != SR_ST_ACTIVE)
344 return SR_ERR_DEV_CLOSED;
349 case SR_CONF_LIMIT_SAMPLES:
350 limit_samples = g_variant_get_uint64(data);
351 ret = sl2_set_limit_samples(sdi, limit_samples);
353 case SR_CONF_SAMPLERATE:
354 samplerate = g_variant_get_uint64(data);
355 ret = sl2_set_samplerate(sdi, samplerate);
357 case SR_CONF_CAPTURE_RATIO:
358 capture_ratio = g_variant_get_uint64(data);
359 ret = sl2_set_capture_ratio(sdi, capture_ratio);
368 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
369 const struct sr_probe_group *probe_group)
381 case SR_CONF_DEVICE_OPTIONS:
382 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, hwcaps,
383 ARRAY_SIZE(hwcaps), sizeof(int32_t));
385 case SR_CONF_SAMPLERATE:
386 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
387 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
388 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
390 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
391 *data = g_variant_builder_end(&gvb);
393 case SR_CONF_TRIGGER_TYPE:
394 *data = g_variant_new_string(TRIGGER_TYPES);
403 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
405 struct drv_context *drvc;
406 struct dev_context *devc;
407 uint16_t trigger_bytes, tmp;
411 if (sdi->status != SR_ST_ACTIVE)
412 return SR_ERR_DEV_CLOSED;
417 devc->cb_data = cb_data;
418 devc->wait_data_ready_locked = TRUE;
419 devc->stopping_in_progress = FALSE;
420 devc->transfer_error = FALSE;
421 devc->samples_processed = 0;
423 devc->sample_packet = 0;
426 * The trigger must be configured first because the calculation of the
427 * pre and post trigger samples depends on a configured trigger.
429 sl2_configure_trigger(sdi);
430 sl2_calculate_trigger_samples(sdi);
432 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
434 /* Calculate the number of expected sample packets. */
435 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
437 /* Round up the number of expected sample packets. */
438 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
439 devc->num_sample_packets++;
441 devc->num_enabled_probes = 0;
444 * Count the number of enabled probes and number them for a sequential
447 for (i = 0, j = 0; i < NUM_PROBES; i++) {
448 if (devc->probes[i]->enabled) {
449 devc->num_enabled_probes++;
450 devc->probe_map[j] = i;
455 sr_dbg("Number of enabled probes: %i.", devc->num_enabled_probes);
457 /* Set up the transfer buffer for the acquisition. */
458 devc->xfer_data_out[0] = CMD_SAMPLE;
459 devc->xfer_data_out[1] = 0x00;
461 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
462 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
464 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
465 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
467 devc->xfer_data_out[6] = devc->samplerate_id;
468 devc->xfer_data_out[7] = devc->trigger_type;
469 devc->xfer_data_out[8] = devc->trigger_channel;
470 devc->xfer_data_out[9] = 0x00;
472 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
473 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
475 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
476 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
480 usb_source_add(drvc->sr_ctx, 100, ikalogic_scanalogic2_receive_data, (void *)sdi);
482 sr_dbg("Acquisition started successfully.");
484 /* Send header packet to the session bus. */
485 std_session_send_df_header(cb_data, LOG_PREFIX);
487 devc->next_state = STATE_SAMPLE;
492 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
496 if (sdi->status != SR_ST_ACTIVE)
497 return SR_ERR_DEV_CLOSED;
499 sr_dbg("Stopping acquisition.");
501 sdi->status = SR_ST_STOPPING;
506 SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
507 .name = "ikalogic-scanalogic2",
508 .longname = "IKALOGIC Scanalogic-2",
513 .dev_list = dev_list,
514 .dev_clear = dev_clear,
515 .config_get = config_get,
516 .config_set = config_set,
517 .config_list = config_list,
518 .dev_open = dev_open,
519 .dev_close = dev_close,
520 .dev_acquisition_start = dev_acquisition_start,
521 .dev_acquisition_stop = dev_acquisition_stop,