]> sigrok.org Git - libsigrok.git/blame - hardware/ikalogic-scanalogic2/api.c
scanalogic2: Cosmetics, whitespace, typos, etc.
[libsigrok.git] / hardware / ikalogic-scanalogic2 / api.c
CommitLineData
16e76bae
MS
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
e52e712d
MS
22static const int hwcaps[] = {
23 SR_CONF_LOGIC_ANALYZER,
24 SR_CONF_SAMPLERATE,
25 SR_CONF_LIMIT_SAMPLES,
26 SR_CONF_TRIGGER_TYPE,
c824eb63 27 SR_CONF_CAPTURE_RATIO,
e52e712d
MS
28};
29
30SR_PRIV const uint64_t ikalogic_scanalogic2_samplerates[NUM_SAMPLERATES] = {
31 SR_KHZ(1.25),
32 SR_KHZ(10),
33 SR_KHZ(50),
34 SR_KHZ(100),
35 SR_KHZ(250),
36 SR_KHZ(500),
37 SR_MHZ(1),
38 SR_MHZ(2.5),
39 SR_MHZ(5),
40 SR_MHZ(10),
c824eb63 41 SR_MHZ(20),
e52e712d
MS
42};
43
44static const char *probe_names[NUM_PROBES + 1] = {
45 "0", "1", "2", "3",
c824eb63 46 NULL,
e52e712d
MS
47};
48
16e76bae
MS
49SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
50static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
51
16e76bae
MS
52static int init(struct sr_context *sr_ctx)
53{
e52e712d 54 return std_init(sr_ctx, di, LOG_PREFIX);
16e76bae
MS
55}
56
57static GSList *scan(GSList *options)
58{
e52e712d 59 GSList *usb_devices, *devices, *l;
16e76bae 60 struct drv_context *drvc;
e52e712d
MS
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;
67 char *fw_ver_str;
16e76bae
MS
68
69 (void)options;
70
71 devices = NULL;
72 drvc = di->priv;
73 drvc->instances = NULL;
e52e712d
MS
74 device_index = 0;
75
76 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
77
78 if (usb_devices == NULL)
79 return NULL;
80
c824eb63 81 for (l = usb_devices; l; l = l->next) {
e52e712d
MS
82 usb = l->data;
83
84 ret = ikalogic_scanalogic2_get_device_info(*usb, &dev_info);
e52e712d 85 if (ret != SR_OK) {
c824eb63 86 sr_warn("Failed to get device information.");
e52e712d
MS
87 sr_usb_dev_inst_free(usb);
88 continue;
89 }
90
c824eb63 91 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
e52e712d
MS
92 sr_err("Device instance malloc failed.");
93 sr_usb_dev_inst_free(usb);
94 continue;
95 }
96
97 if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
98 sr_err("Transfer malloc failed.");
99 sr_usb_dev_inst_free(usb);
100 g_free(devc);
101 continue;
102 }
103
104 if (!(devc->xfer_out = libusb_alloc_transfer(0))) {
105 sr_err("Transfer malloc failed.");
106 sr_usb_dev_inst_free(usb);
107 libusb_free_transfer(devc->xfer_in);
108 g_free(devc);
109 continue;
110 }
111
112 fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major,
113 dev_info.fw_ver_minor);
e52e712d
MS
114 if (!fw_ver_str) {
115 sr_err("Firmware string malloc failed.");
116 sr_usb_dev_inst_free(usb);
117 libusb_free_transfer(devc->xfer_in);
118 libusb_free_transfer(devc->xfer_out);
119 g_free(devc);
120 continue;
121 }
122
123 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE, VENDOR_NAME,
124 MODEL_NAME, fw_ver_str);
e52e712d 125 g_free(fw_ver_str);
e52e712d
MS
126 if (!sdi) {
127 sr_err("sr_dev_inst_new failed.");
128 sr_usb_dev_inst_free(usb);
129 libusb_free_transfer(devc->xfer_in);
130 libusb_free_transfer(devc->xfer_out);
131 g_free(devc);
132 continue;
133 }
134
135 sdi->priv = devc;
136 sdi->driver = di;
137 sdi->inst_type = SR_INST_USB;
138 sdi->conn = usb;
139
140 for (i = 0; probe_names[i]; i++) {
141 probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
142 probe_names[i]);
143 sdi->probes = g_slist_append(sdi->probes, probe);
144 devc->probes[i] = probe;
145 }
146
147 devc->state = STATE_IDLE;
148 devc->next_state = STATE_IDLE;
149
150 /* Set default samplerate. */
151 ikalogic_scanalogic2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
152
153 /* Set default capture ratio. */
154 devc->capture_ratio = 0;
155
156 /* Set default after trigger delay. */
157 devc->after_trigger_delay = 0;
158
159 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
160 PACKET_LENGTH);
161 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
162 PACKET_LENGTH);
163
164 libusb_fill_control_setup(devc->xfer_buf_in,
165 USB_REQUEST_TYPE_IN, USB_HID_SET_REPORT,
166 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
167 PACKET_LENGTH);
168 libusb_fill_control_setup(devc->xfer_buf_out,
169 USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
170 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
171 PACKET_LENGTH);
172
173 devc->xfer_data_in = devc->xfer_buf_in +
174 LIBUSB_CONTROL_SETUP_SIZE;
175 devc->xfer_data_out = devc->xfer_buf_out +
176 LIBUSB_CONTROL_SETUP_SIZE;
177
178 drvc->instances = g_slist_append(drvc->instances, sdi);
179 devices = g_slist_append(devices, sdi);
180
181 device_index++;
182 }
16e76bae 183
e52e712d 184 g_slist_free(usb_devices);
16e76bae
MS
185
186 return devices;
187}
188
189static GSList *dev_list(void)
190{
c824eb63 191 return ((struct drv_context *)(di->priv))->instances;
16e76bae
MS
192}
193
e52e712d
MS
194static void clear_dev_context(void *priv)
195{
c824eb63
UH
196 struct dev_context *devc;
197
198 devc = priv;
e52e712d 199
c824eb63 200 sr_dbg("Device context cleared.");
e52e712d
MS
201
202 libusb_free_transfer(devc->xfer_in);
203 libusb_free_transfer(devc->xfer_out);
204 g_free(devc);
205}
206
16e76bae
MS
207static int dev_clear(void)
208{
e52e712d 209 return std_dev_clear(di, &clear_dev_context);
16e76bae
MS
210}
211
212static int dev_open(struct sr_dev_inst *sdi)
213{
e52e712d
MS
214 struct drv_context *drvc;
215 struct dev_context *devc;
216 struct sr_usb_dev_inst *usb;
217 uint8_t buffer[PACKET_LENGTH];
218 int ret;
219
220 if (!(drvc = di->priv)) {
221 sr_err("Driver was not initialized.");
222 return SR_ERR;
223 }
224
225 usb = sdi->conn;
226 devc = sdi->priv;
16e76bae 227
e52e712d
MS
228 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
229 return SR_ERR;
230
231 /*
232 * Determine if a kernel driver is active on this interface and, if so,
233 * detach it.
234 */
235 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
236 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
e52e712d
MS
237 if (ret < 0) {
238 sr_err("Failed to detach kernel driver: %i.",
239 libusb_error_name(ret));
240 return SR_ERR;
241 }
242 }
243
244 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
e52e712d
MS
245 if (ret) {
246 sr_err("Failed to claim interface: %s.",
247 libusb_error_name(ret));
248 return SR_ERR;
249 }
250
251 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
252 devc->xfer_buf_in, ikalogic_scanalogic2_receive_transfer_in,
253 sdi, USB_TIMEOUT);
254
255 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
256 devc->xfer_buf_out, ikalogic_scanalogic2_receive_transfer_out,
257 sdi, USB_TIMEOUT);
258
259 memset(buffer, 0, sizeof(buffer));
260
261 buffer[0] = CMD_RESET;
262 ret = ikalogic_scanalogic2_transfer_out(usb->devhdl, buffer);
e52e712d
MS
263 if (ret != PACKET_LENGTH) {
264 sr_err("Device reset failed: %s.", libusb_error_name(ret));
265 return SR_ERR;
266 }
267
268 /*
269 * Set the device to idle state. If the device is not in idle state it
c824eb63
UH
270 * possibly will reset itself after a few seconds without being used
271 * and thereby close the connection.
e52e712d
MS
272 */
273 buffer[0] = CMD_IDLE;
274 ret = ikalogic_scanalogic2_transfer_out(usb->devhdl, buffer);
e52e712d
MS
275 if (ret != PACKET_LENGTH) {
276 sr_err("Failed to set device in idle state: %s.",
277 libusb_error_name(ret));
278 return SR_ERR;
279 }
16e76bae
MS
280
281 sdi->status = SR_ST_ACTIVE;
282
283 return SR_OK;
284}
285
286static int dev_close(struct sr_dev_inst *sdi)
287{
e52e712d
MS
288 struct sr_usb_dev_inst *usb;
289
290 if (!di->priv) {
291 sr_err("Driver was not initialized.");
292 return SR_ERR;
293 }
294
295 usb = sdi->conn;
296
297 if (!usb->devhdl)
298 return SR_OK;
16e76bae 299
e52e712d
MS
300 libusb_release_interface(usb->devhdl, USB_INTERFACE);
301 libusb_close(usb->devhdl);
16e76bae 302
e52e712d 303 usb->devhdl = NULL;
16e76bae
MS
304 sdi->status = SR_ST_INACTIVE;
305
306 return SR_OK;
307}
308
309static int cleanup(void)
310{
c824eb63 311 return dev_clear();
16e76bae
MS
312}
313
314static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
315{
e52e712d 316 struct dev_context *devc;
16e76bae
MS
317 int ret;
318
16e76bae 319 ret = SR_OK;
e52e712d
MS
320 devc = sdi->priv;
321
16e76bae 322 switch (key) {
e52e712d
MS
323 case SR_CONF_SAMPLERATE:
324 *data = g_variant_new_uint64(devc->samplerate);
325 break;
326 case SR_CONF_CAPTURE_RATIO:
327 *data = g_variant_new_uint64(devc->capture_ratio);
328 break;
16e76bae
MS
329 default:
330 return SR_ERR_NA;
331 }
332
333 return ret;
334}
335
336static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
337{
e52e712d 338 uint64_t samplerate, limit_samples, capture_ratio;
16e76bae
MS
339 int ret;
340
16e76bae
MS
341 if (sdi->status != SR_ST_ACTIVE)
342 return SR_ERR_DEV_CLOSED;
343
344 ret = SR_OK;
e52e712d 345
16e76bae 346 switch (key) {
e52e712d
MS
347 case SR_CONF_LIMIT_SAMPLES:
348 limit_samples = g_variant_get_uint64(data);
349 ret = ikalogic_scanalogic2_set_limit_samples(sdi,
350 limit_samples);
351 break;
352 case SR_CONF_SAMPLERATE:
353 samplerate = g_variant_get_uint64(data);
354 ret = ikalogic_scanalogic2_set_samplerate(sdi, samplerate);
355 break;
356 case SR_CONF_CAPTURE_RATIO:
357 capture_ratio = g_variant_get_uint64(data);
358 ret = ikalogic_scanalogic2_set_capture_ratio(sdi,
359 capture_ratio);
360 break;
16e76bae 361 default:
e52e712d 362 return SR_ERR_NA;
16e76bae
MS
363 }
364
365 return ret;
366}
367
368static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
369{
e52e712d
MS
370 GVariant *gvar;
371 GVariantBuilder gvb;
16e76bae
MS
372 int ret;
373
374 (void)sdi;
16e76bae
MS
375
376 ret = SR_OK;
e52e712d 377
16e76bae 378 switch (key) {
e52e712d
MS
379 case SR_CONF_DEVICE_OPTIONS:
380 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, hwcaps,
381 ARRAY_SIZE(hwcaps), sizeof(int32_t));
382 break;
383 case SR_CONF_SAMPLERATE:
384 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
385 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
386 ikalogic_scanalogic2_samplerates,
387 ARRAY_SIZE(ikalogic_scanalogic2_samplerates),
388 sizeof(uint64_t));
389 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
390 *data = g_variant_builder_end(&gvb);
391 break;
392 case SR_CONF_TRIGGER_TYPE:
393 *data = g_variant_new_string(TRIGGER_TYPES);
394 break;
16e76bae
MS
395 default:
396 return SR_ERR_NA;
397 }
398
399 return ret;
400}
401
e52e712d 402static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
16e76bae 403{
e52e712d
MS
404 const struct libusb_pollfd **pfd;
405 struct drv_context *drvc;
406 struct dev_context *devc;
407 uint16_t trigger_bytes, tmp;
408 unsigned int i, j;
409 int ret;
16e76bae
MS
410
411 if (sdi->status != SR_ST_ACTIVE)
412 return SR_ERR_DEV_CLOSED;
413
e52e712d
MS
414 devc = sdi->priv;
415 drvc = di->priv;
416
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;
422 devc->channel = 0;
423 devc->sample_packet = 0;
424
425 /*
426 * The trigger must be configured first because the calculation of the
427 * pre and post trigger samples depends on a configured trigger.
428 */
429 ikalogic_scanalogic2_configure_trigger(sdi);
430 ikalogic_scanalogic2_calculate_trigger_samples(sdi);
431
432 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
433
434 /* Calculate the number of expected sample packets. */
435 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
436
437 /* Round up the number of expected sample packets. */
438 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
439 devc->num_sample_packets++;
440
441 devc->num_enabled_probes = 0;
442
443 /*
444 * Count the number of enabled probes and number them for a sequential
445 * access.
446 */
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;
451 j++;
452 }
453 }
454
455 sr_dbg("Number of enabled probes: %i.", devc->num_enabled_probes);
456
457 /* Set up the transfer buffer for the acquisition. */
458 devc->xfer_data_out[0] = CMD_SAMPLE;
459 devc->xfer_data_out[1] = 0x00;
460
461 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
462 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
463
464 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
465 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
466
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;
471
472 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
473 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
474
475 if (!(pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx))) {
476 sr_err("libusb_get_pollfds failed.");
477 return SR_ERR;
478 }
479
480 /* Count the number of file descriptors. */
481 for (devc->num_usbfd = 0; pfd[devc->num_usbfd]; devc->num_usbfd++);
482
483 if (!(devc->usbfd = g_try_malloc(devc->num_usbfd * sizeof(int)))) {
484 sr_err("File descriptor array malloc failed.");
485 free(pfd);
e52e712d
MS
486 return SR_ERR_MALLOC;
487 }
488
489 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
c824eb63 490 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
e52e712d
MS
491 g_free(devc->usbfd);
492 return SR_ERR;
493 }
494
495 for (i = 0; i < devc->num_usbfd; i++) {
496 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
497 ikalogic_scanalogic2_receive_data, (void *)sdi);
498
499 devc->usbfd[i] = pfd[i]->fd;
500 }
501
502 free(pfd);
503
504 sr_dbg("Acquisition started successfully.");
505
506 /* Send header packet to the session bus. */
507 std_session_send_df_header(cb_data, LOG_PREFIX);
508
509 devc->next_state = STATE_SAMPLE;
16e76bae
MS
510
511 return SR_OK;
512}
513
514static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
515{
516 (void)cb_data;
517
518 if (sdi->status != SR_ST_ACTIVE)
519 return SR_ERR_DEV_CLOSED;
520
e52e712d
MS
521 sr_dbg("Stopping acquisition.");
522
523 sdi->status = SR_ST_STOPPING;
16e76bae
MS
524
525 return SR_OK;
526}
527
528SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
529 .name = "ikalogic-scanalogic2",
e52e712d 530 .longname = "IKALOGIC Scanalogic-2",
16e76bae
MS
531 .api_version = 1,
532 .init = init,
533 .cleanup = cleanup,
534 .scan = scan,
535 .dev_list = dev_list,
536 .dev_clear = dev_clear,
537 .config_get = config_get,
538 .config_set = config_set,
539 .config_list = config_list,
540 .dev_open = dev_open,
541 .dev_close = dev_close,
542 .dev_acquisition_start = dev_acquisition_start,
543 .dev_acquisition_stop = dev_acquisition_stop,
544 .priv = NULL,
545};