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