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,
30 SR_PRIV const uint64_t ikalogic_scanalogic2_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)
85 ret = ikalogic_scanalogic2_get_device_info(*usb, &dev_info);
88 sr_warn("Failed to get device information.\n");
89 sr_usb_dev_inst_free(usb);
93 devc = g_try_malloc(sizeof(struct dev_context));
96 sr_err("Device instance malloc failed.");
97 sr_usb_dev_inst_free(usb);
101 if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
102 sr_err("Transfer malloc failed.");
103 sr_usb_dev_inst_free(usb);
108 if (!(devc->xfer_out = libusb_alloc_transfer(0))) {
109 sr_err("Transfer malloc failed.");
110 sr_usb_dev_inst_free(usb);
111 libusb_free_transfer(devc->xfer_in);
116 fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major,
117 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(device_index, SR_ST_INACTIVE, VENDOR_NAME,
129 MODEL_NAME, fw_ver_str);
134 sr_err("sr_dev_inst_new failed.");
135 sr_usb_dev_inst_free(usb);
136 libusb_free_transfer(devc->xfer_in);
137 libusb_free_transfer(devc->xfer_out);
144 sdi->inst_type = SR_INST_USB;
147 for (i = 0; probe_names[i]; i++) {
148 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
150 sdi->probes = g_slist_append(sdi->probes, probe);
151 devc->probes[i] = probe;
154 devc->state = STATE_IDLE;
155 devc->next_state = STATE_IDLE;
157 /* Set default samplerate. */
158 ikalogic_scanalogic2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
160 /* Set default capture ratio. */
161 devc->capture_ratio = 0;
163 /* Set default after trigger delay. */
164 devc->after_trigger_delay = 0;
166 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
168 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
171 libusb_fill_control_setup(devc->xfer_buf_in,
172 USB_REQUEST_TYPE_IN, USB_HID_SET_REPORT,
173 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
175 libusb_fill_control_setup(devc->xfer_buf_out,
176 USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
177 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
180 devc->xfer_data_in = devc->xfer_buf_in +
181 LIBUSB_CONTROL_SETUP_SIZE;
182 devc->xfer_data_out = devc->xfer_buf_out +
183 LIBUSB_CONTROL_SETUP_SIZE;
185 drvc->instances = g_slist_append(drvc->instances, sdi);
186 devices = g_slist_append(devices, sdi);
191 g_slist_free(usb_devices);
196 static GSList *dev_list(void)
198 struct drv_context *drvc;
202 return drvc->instances;
205 static void clear_dev_context(void *priv)
207 struct dev_context *devc = priv;
209 sr_dbg("Device context cleard.");
211 libusb_free_transfer(devc->xfer_in);
212 libusb_free_transfer(devc->xfer_out);
216 static int dev_clear(void)
218 return std_dev_clear(di, &clear_dev_context);
221 static int dev_open(struct sr_dev_inst *sdi)
223 struct drv_context *drvc;
224 struct dev_context *devc;
225 struct sr_usb_dev_inst *usb;
226 uint8_t buffer[PACKET_LENGTH];
229 if (!(drvc = di->priv)) {
230 sr_err("Driver was not initialized.");
237 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
241 * Determine if a kernel driver is active on this interface and, if so,
244 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
245 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
248 sr_err("Failed to detach kernel driver: %i.",
249 libusb_error_name(ret));
254 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
257 sr_err("Failed to claim interface: %s.",
258 libusb_error_name(ret));
262 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
263 devc->xfer_buf_in, ikalogic_scanalogic2_receive_transfer_in,
266 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
267 devc->xfer_buf_out, ikalogic_scanalogic2_receive_transfer_out,
270 memset(buffer, 0, sizeof(buffer));
272 buffer[0] = CMD_RESET;
273 ret = ikalogic_scanalogic2_transfer_out(usb->devhdl, buffer);
275 if (ret != PACKET_LENGTH) {
276 sr_err("Device reset failed: %s.", libusb_error_name(ret));
281 * Set the device to idle state. If the device is not in idle state it
282 * possibly will reset itself after a few seconds without being used and
283 * thereby close the connection.
285 buffer[0] = CMD_IDLE;
286 ret = ikalogic_scanalogic2_transfer_out(usb->devhdl, buffer);
288 if (ret != PACKET_LENGTH) {
289 sr_err("Failed to set device in idle state: %s.",
290 libusb_error_name(ret));
294 sdi->status = SR_ST_ACTIVE;
299 static int dev_close(struct sr_dev_inst *sdi)
301 struct sr_usb_dev_inst *usb;
304 sr_err("Driver was not initialized.");
313 libusb_release_interface(usb->devhdl, USB_INTERFACE);
314 libusb_close(usb->devhdl);
317 sdi->status = SR_ST_INACTIVE;
322 static int cleanup(void)
329 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
331 struct dev_context *devc;
338 case SR_CONF_SAMPLERATE:
339 *data = g_variant_new_uint64(devc->samplerate);
341 case SR_CONF_CAPTURE_RATIO:
342 *data = g_variant_new_uint64(devc->capture_ratio);
351 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
353 uint64_t samplerate, limit_samples, capture_ratio;
356 if (sdi->status != SR_ST_ACTIVE)
357 return SR_ERR_DEV_CLOSED;
362 case SR_CONF_LIMIT_SAMPLES:
363 limit_samples = g_variant_get_uint64(data);
364 ret = ikalogic_scanalogic2_set_limit_samples(sdi,
367 case SR_CONF_SAMPLERATE:
368 samplerate = g_variant_get_uint64(data);
369 ret = ikalogic_scanalogic2_set_samplerate(sdi, samplerate);
371 case SR_CONF_CAPTURE_RATIO:
372 capture_ratio = g_variant_get_uint64(data);
373 ret = ikalogic_scanalogic2_set_capture_ratio(sdi,
383 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
394 case SR_CONF_DEVICE_OPTIONS:
395 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, hwcaps,
396 ARRAY_SIZE(hwcaps), sizeof(int32_t));
398 case SR_CONF_SAMPLERATE:
399 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
400 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
401 ikalogic_scanalogic2_samplerates,
402 ARRAY_SIZE(ikalogic_scanalogic2_samplerates),
404 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
405 *data = g_variant_builder_end(&gvb);
407 case SR_CONF_TRIGGER_TYPE:
408 *data = g_variant_new_string(TRIGGER_TYPES);
417 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
419 const struct libusb_pollfd **pfd;
420 struct drv_context *drvc;
421 struct dev_context *devc;
422 uint16_t trigger_bytes, tmp;
426 if (sdi->status != SR_ST_ACTIVE)
427 return SR_ERR_DEV_CLOSED;
432 devc->cb_data = cb_data;
433 devc->wait_data_ready_locked = TRUE;
434 devc->stopping_in_progress = FALSE;
435 devc->transfer_error = FALSE;
436 devc->samples_processed = 0;
438 devc->sample_packet = 0;
441 * The trigger must be configured first because the calculation of the
442 * pre and post trigger samples depends on a configured trigger.
444 ikalogic_scanalogic2_configure_trigger(sdi);
445 ikalogic_scanalogic2_calculate_trigger_samples(sdi);
447 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
449 /* Calculate the number of expected sample packets. */
450 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
452 /* Round up the number of expected sample packets. */
453 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
454 devc->num_sample_packets++;
456 devc->num_enabled_probes = 0;
459 * Count the number of enabled probes and number them for a sequential
462 for (i = 0, j = 0; i < NUM_PROBES; i++) {
463 if (devc->probes[i]->enabled) {
464 devc->num_enabled_probes++;
465 devc->probe_map[j] = i;
470 sr_dbg("Number of enabled probes: %i.", devc->num_enabled_probes);
472 /* Set up the transfer buffer for the acquisition. */
473 devc->xfer_data_out[0] = CMD_SAMPLE;
474 devc->xfer_data_out[1] = 0x00;
476 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
477 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
479 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
480 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
482 devc->xfer_data_out[6] = devc->samplerate_id;
483 devc->xfer_data_out[7] = devc->trigger_type;
484 devc->xfer_data_out[8] = devc->trigger_channel;
485 devc->xfer_data_out[9] = 0x00;
487 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
488 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
490 if (!(pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx))) {
491 sr_err("libusb_get_pollfds failed.");
495 /* Count the number of file descriptors. */
496 for (devc->num_usbfd = 0; pfd[devc->num_usbfd]; devc->num_usbfd++);
498 if (!(devc->usbfd = g_try_malloc(devc->num_usbfd * sizeof(int)))) {
499 sr_err("File descriptor array malloc failed.");
502 return SR_ERR_MALLOC;
505 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
506 sr_err("Submit transfer failed: %s", libusb_error_name(ret));
511 for (i = 0; i < devc->num_usbfd; i++) {
512 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
513 ikalogic_scanalogic2_receive_data, (void *)sdi);
515 devc->usbfd[i] = pfd[i]->fd;
520 sr_dbg("Acquisition started successfully.");
522 /* Send header packet to the session bus. */
523 std_session_send_df_header(cb_data, LOG_PREFIX);
525 devc->next_state = STATE_SAMPLE;
530 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
534 if (sdi->status != SR_ST_ACTIVE)
535 return SR_ERR_DEV_CLOSED;
537 sr_dbg("Stopping acquisition.");
539 sdi->status = SR_ST_STOPPING;
544 SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
545 .name = "ikalogic-scanalogic2",
546 .longname = "IKALOGIC Scanalogic-2",
551 .dev_list = dev_list,
552 .dev_clear = dev_clear,
553 .config_get = config_get,
554 .config_set = config_set,
555 .config_list = config_list,
556 .dev_open = dev_open,
557 .dev_close = dev_close,
558 .dev_acquisition_start = dev_acquisition_start,
559 .dev_acquisition_stop = dev_acquisition_stop,