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,
26 SR_CONF_TRIGGER_MATCH,
27 SR_CONF_CAPTURE_RATIO,
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;
72 int ret, device_index, i;
79 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(device_index, 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);
142 sdi->inst_type = SR_INST_USB;
145 for (i = 0; channel_names[i]; i++) {
146 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
148 sdi->channels = g_slist_append(sdi->channels, ch);
149 devc->channels[i] = ch;
152 devc->state = STATE_IDLE;
153 devc->next_state = STATE_IDLE;
155 /* Set default samplerate. */
156 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
158 /* Set default capture ratio. */
159 devc->capture_ratio = 0;
161 /* Set default after trigger delay. */
162 devc->after_trigger_delay = 0;
164 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
166 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
169 libusb_fill_control_setup(devc->xfer_buf_in,
170 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
171 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
173 libusb_fill_control_setup(devc->xfer_buf_out,
174 USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
175 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
178 devc->xfer_data_in = devc->xfer_buf_in +
179 LIBUSB_CONTROL_SETUP_SIZE;
180 devc->xfer_data_out = devc->xfer_buf_out +
181 LIBUSB_CONTROL_SETUP_SIZE;
183 drvc->instances = g_slist_append(drvc->instances, sdi);
184 devices = g_slist_append(devices, sdi);
189 g_slist_free(usb_devices);
194 static GSList *dev_list(void)
196 return ((struct drv_context *)(di->priv))->instances;
199 static void clear_dev_context(void *priv)
201 struct dev_context *devc;
205 sr_dbg("Device context cleared.");
207 libusb_free_transfer(devc->xfer_in);
208 libusb_free_transfer(devc->xfer_out);
212 static int dev_clear(void)
214 return std_dev_clear(di, &clear_dev_context);
217 static int dev_open(struct sr_dev_inst *sdi)
219 struct drv_context *drvc;
220 struct dev_context *devc;
221 struct sr_usb_dev_inst *usb;
222 uint8_t buffer[PACKET_LENGTH];
225 if (!(drvc = di->priv)) {
226 sr_err("Driver was not initialized.");
233 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
237 * Determine if a kernel driver is active on this interface and, if so,
240 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
241 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
243 sr_err("Failed to detach kernel driver: %s.",
244 libusb_error_name(ret));
249 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
250 sr_err("Failed to claim interface: %s.",
251 libusb_error_name(ret));
255 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
256 devc->xfer_buf_in, sl2_receive_transfer_in,
259 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
260 devc->xfer_buf_out, sl2_receive_transfer_out,
263 memset(buffer, 0, sizeof(buffer));
265 buffer[0] = CMD_RESET;
266 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
267 sr_err("Device reset failed: %s.", libusb_error_name(ret));
272 * Set the device to idle state. If the device is not in idle state it
273 * possibly will reset itself after a few seconds without being used
274 * and thereby close the connection.
276 buffer[0] = CMD_IDLE;
277 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
278 sr_err("Failed to set device in idle state: %s.",
279 libusb_error_name(ret));
283 sdi->status = SR_ST_ACTIVE;
288 static int dev_close(struct sr_dev_inst *sdi)
290 struct sr_usb_dev_inst *usb;
293 sr_err("Driver was not initialized.");
302 libusb_release_interface(usb->devhdl, USB_INTERFACE);
303 libusb_close(usb->devhdl);
306 sdi->status = SR_ST_INACTIVE;
311 static int cleanup(void)
316 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
317 const struct sr_channel_group *cg)
319 struct dev_context *devc;
328 case SR_CONF_SAMPLERATE:
329 *data = g_variant_new_uint64(devc->samplerate);
331 case SR_CONF_CAPTURE_RATIO:
332 *data = g_variant_new_uint64(devc->capture_ratio);
341 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
342 const struct sr_channel_group *cg)
344 uint64_t samplerate, limit_samples, capture_ratio;
349 if (sdi->status != SR_ST_ACTIVE)
350 return SR_ERR_DEV_CLOSED;
355 case SR_CONF_LIMIT_SAMPLES:
356 limit_samples = g_variant_get_uint64(data);
357 ret = sl2_set_limit_samples(sdi, limit_samples);
359 case SR_CONF_SAMPLERATE:
360 samplerate = g_variant_get_uint64(data);
361 ret = sl2_set_samplerate(sdi, samplerate);
363 case SR_CONF_CAPTURE_RATIO:
364 capture_ratio = g_variant_get_uint64(data);
365 ret = sl2_set_capture_ratio(sdi, capture_ratio);
374 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
375 const struct sr_channel_group *cg)
377 GVariant *gvar, *grange[2];
387 case SR_CONF_DEVICE_OPTIONS:
388 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, hwcaps,
389 ARRAY_SIZE(hwcaps), sizeof(int32_t));
391 case SR_CONF_SAMPLERATE:
392 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
393 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
394 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
396 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
397 *data = g_variant_builder_end(&gvb);
399 case SR_CONF_TRIGGER_MATCH:
400 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
401 trigger_matches, ARRAY_SIZE(trigger_matches),
404 case SR_CONF_LIMIT_SAMPLES:
405 grange[0] = g_variant_new_uint64(0);
406 grange[1] = g_variant_new_uint64(MAX_SAMPLES);
407 *data = g_variant_new_tuple(grange, 2);
416 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
418 struct drv_context *drvc;
419 struct dev_context *devc;
420 uint16_t trigger_bytes, tmp;
424 if (sdi->status != SR_ST_ACTIVE)
425 return SR_ERR_DEV_CLOSED;
430 devc->cb_data = cb_data;
431 devc->wait_data_ready_locked = TRUE;
432 devc->stopping_in_progress = FALSE;
433 devc->transfer_error = FALSE;
434 devc->samples_processed = 0;
436 devc->sample_packet = 0;
439 * The trigger must be configured first because the calculation of the
440 * pre and post trigger samples depends on a configured trigger.
442 sl2_convert_trigger(sdi);
443 sl2_calculate_trigger_samples(sdi);
445 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
447 /* Calculate the number of expected sample packets. */
448 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
450 /* Round up the number of expected sample packets. */
451 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
452 devc->num_sample_packets++;
454 devc->num_enabled_channels = 0;
457 * Count the number of enabled channels and number them for a sequential
460 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
461 if (devc->channels[i]->enabled) {
462 devc->num_enabled_channels++;
463 devc->channel_map[j] = i;
468 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
470 /* Set up the transfer buffer for the acquisition. */
471 devc->xfer_data_out[0] = CMD_SAMPLE;
472 devc->xfer_data_out[1] = 0x00;
474 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
475 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
477 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
478 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
480 devc->xfer_data_out[6] = devc->samplerate_id;
481 devc->xfer_data_out[7] = devc->trigger_type;
482 devc->xfer_data_out[8] = devc->trigger_channel;
483 devc->xfer_data_out[9] = 0x00;
485 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
486 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
488 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
489 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
493 usb_source_add(drvc->sr_ctx, 100, ikalogic_scanalogic2_receive_data, (void *)sdi);
495 sr_dbg("Acquisition started successfully.");
497 /* Send header packet to the session bus. */
498 std_session_send_df_header(cb_data, LOG_PREFIX);
500 devc->next_state = STATE_SAMPLE;
505 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
509 if (sdi->status != SR_ST_ACTIVE)
510 return SR_ERR_DEV_CLOSED;
512 sr_dbg("Stopping acquisition.");
514 sdi->status = SR_ST_STOPPING;
519 SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
520 .name = "ikalogic-scanalogic2",
521 .longname = "IKALOGIC Scanalogic-2",
526 .dev_list = dev_list,
527 .dev_clear = dev_clear,
528 .config_get = config_get,
529 .config_set = config_set,
530 .config_list = config_list,
531 .dev_open = dev_open,
532 .dev_close = dev_close,
533 .dev_acquisition_start = dev_acquisition_start,
534 .dev_acquisition_stop = dev_acquisition_stop,