]> sigrok.org Git - libsigrok.git/blame - src/hardware/ikalogic-scanalogic2/api.c
Consistently use g_malloc0() for allocating devc.
[libsigrok.git] / src / 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
f254bc4b 22static const uint32_t devopts[] = {
e52e712d 23 SR_CONF_LOGIC_ANALYZER,
5827f61b
BV
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,
e52e712d
MS
28};
29
02d5c0d8
BV
30static const int32_t trigger_matches[] = {
31 SR_TRIGGER_RISING,
32 SR_TRIGGER_FALLING,
33 SR_TRIGGER_EDGE,
34};
35
79914b3a 36SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
e52e712d
MS
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),
c824eb63 47 SR_MHZ(20),
e52e712d
MS
48};
49
3f239f08 50static const char *channel_names[NUM_CHANNELS + 1] = {
e52e712d 51 "0", "1", "2", "3",
c824eb63 52 NULL,
e52e712d
MS
53};
54
16e76bae
MS
55SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
56static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
57
16e76bae
MS
58static int init(struct sr_context *sr_ctx)
59{
e52e712d 60 return std_init(sr_ctx, di, LOG_PREFIX);
16e76bae
MS
61}
62
63static GSList *scan(GSList *options)
64{
e52e712d 65 GSList *usb_devices, *devices, *l;
16e76bae 66 struct drv_context *drvc;
e52e712d 67 struct sr_dev_inst *sdi;
ba7dd8bb 68 struct sr_channel *ch;
e52e712d
MS
69 struct dev_context *devc;
70 struct sr_usb_dev_inst *usb;
71 struct device_info dev_info;
65340dd5 72 int ret, i;
16e76bae
MS
73
74 (void)options;
75
76 devices = NULL;
77 drvc = di->priv;
78 drvc->instances = NULL;
e52e712d
MS
79
80 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
81
82 if (usb_devices == NULL)
83 return NULL;
84
c824eb63 85 for (l = usb_devices; l; l = l->next) {
e52e712d
MS
86 usb = l->data;
87
79914b3a
UH
88 if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
89 sr_warn("Failed to get device information: %d.", ret);
e52e712d
MS
90 sr_usb_dev_inst_free(usb);
91 continue;
92 }
93
f57d8ffe 94 devc = g_malloc0(sizeof(struct dev_context));
e52e712d
MS
95
96 if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
97 sr_err("Transfer malloc failed.");
98 sr_usb_dev_inst_free(usb);
99 g_free(devc);
100 continue;
101 }
102
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);
107 g_free(devc);
108 continue;
109 }
110
aac29cc1 111 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
112 sdi->status = SR_ST_INACTIVE;
113 sdi->vendor = g_strdup(VENDOR_NAME);
114 sdi->model = g_strdup(MODEL_NAME);
115 sdi->version = g_strdup_printf("%u.%u", dev_info.fw_ver_major, dev_info.fw_ver_minor);
116 sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
e52e712d
MS
117 sdi->priv = devc;
118 sdi->driver = di;
119 sdi->inst_type = SR_INST_USB;
120 sdi->conn = usb;
121
ba7dd8bb 122 for (i = 0; channel_names[i]; i++) {
3f239f08 123 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
ba7dd8bb
UH
124 channel_names[i]);
125 sdi->channels = g_slist_append(sdi->channels, ch);
126 devc->channels[i] = ch;
e52e712d
MS
127 }
128
129 devc->state = STATE_IDLE;
130 devc->next_state = STATE_IDLE;
131
132 /* Set default samplerate. */
79914b3a 133 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
e52e712d
MS
134
135 /* Set default capture ratio. */
136 devc->capture_ratio = 0;
137
138 /* Set default after trigger delay. */
139 devc->after_trigger_delay = 0;
140
141 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
142 PACKET_LENGTH);
143 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
144 PACKET_LENGTH);
145
146 libusb_fill_control_setup(devc->xfer_buf_in,
fb8d593c 147 USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
e52e712d
MS
148 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
149 PACKET_LENGTH);
150 libusb_fill_control_setup(devc->xfer_buf_out,
151 USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
152 USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
153 PACKET_LENGTH);
154
155 devc->xfer_data_in = devc->xfer_buf_in +
156 LIBUSB_CONTROL_SETUP_SIZE;
157 devc->xfer_data_out = devc->xfer_buf_out +
158 LIBUSB_CONTROL_SETUP_SIZE;
159
160 drvc->instances = g_slist_append(drvc->instances, sdi);
161 devices = g_slist_append(devices, sdi);
e52e712d 162 }
16e76bae 163
e52e712d 164 g_slist_free(usb_devices);
16e76bae
MS
165
166 return devices;
167}
168
169static GSList *dev_list(void)
170{
c824eb63 171 return ((struct drv_context *)(di->priv))->instances;
16e76bae
MS
172}
173
e52e712d
MS
174static void clear_dev_context(void *priv)
175{
c824eb63
UH
176 struct dev_context *devc;
177
178 devc = priv;
e52e712d 179
c824eb63 180 sr_dbg("Device context cleared.");
e52e712d
MS
181
182 libusb_free_transfer(devc->xfer_in);
183 libusb_free_transfer(devc->xfer_out);
184 g_free(devc);
185}
186
16e76bae
MS
187static int dev_clear(void)
188{
e52e712d 189 return std_dev_clear(di, &clear_dev_context);
16e76bae
MS
190}
191
192static int dev_open(struct sr_dev_inst *sdi)
193{
e52e712d
MS
194 struct drv_context *drvc;
195 struct dev_context *devc;
196 struct sr_usb_dev_inst *usb;
197 uint8_t buffer[PACKET_LENGTH];
198 int ret;
199
200 if (!(drvc = di->priv)) {
201 sr_err("Driver was not initialized.");
202 return SR_ERR;
203 }
204
205 usb = sdi->conn;
206 devc = sdi->priv;
16e76bae 207
e52e712d
MS
208 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
209 return SR_ERR;
210
211 /*
212 * Determine if a kernel driver is active on this interface and, if so,
213 * detach it.
214 */
215 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
216 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
e52e712d 217 if (ret < 0) {
9526f2d4 218 sr_err("Failed to detach kernel driver: %s.",
e52e712d
MS
219 libusb_error_name(ret));
220 return SR_ERR;
221 }
222 }
223
79914b3a 224 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
e52e712d
MS
225 sr_err("Failed to claim interface: %s.",
226 libusb_error_name(ret));
227 return SR_ERR;
228 }
229
230 libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
79914b3a 231 devc->xfer_buf_in, sl2_receive_transfer_in,
e52e712d
MS
232 sdi, USB_TIMEOUT);
233
234 libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
79914b3a 235 devc->xfer_buf_out, sl2_receive_transfer_out,
e52e712d
MS
236 sdi, USB_TIMEOUT);
237
238 memset(buffer, 0, sizeof(buffer));
239
240 buffer[0] = CMD_RESET;
79914b3a 241 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
242 sr_err("Device reset failed: %s.", libusb_error_name(ret));
243 return SR_ERR;
244 }
245
246 /*
247 * Set the device to idle state. If the device is not in idle state it
c824eb63
UH
248 * possibly will reset itself after a few seconds without being used
249 * and thereby close the connection.
e52e712d
MS
250 */
251 buffer[0] = CMD_IDLE;
79914b3a 252 if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
253 sr_err("Failed to set device in idle state: %s.",
254 libusb_error_name(ret));
255 return SR_ERR;
256 }
16e76bae
MS
257
258 sdi->status = SR_ST_ACTIVE;
259
260 return SR_OK;
261}
262
263static int dev_close(struct sr_dev_inst *sdi)
264{
e52e712d
MS
265 struct sr_usb_dev_inst *usb;
266
267 if (!di->priv) {
268 sr_err("Driver was not initialized.");
269 return SR_ERR;
270 }
271
272 usb = sdi->conn;
273
274 if (!usb->devhdl)
275 return SR_OK;
16e76bae 276
e52e712d
MS
277 libusb_release_interface(usb->devhdl, USB_INTERFACE);
278 libusb_close(usb->devhdl);
16e76bae 279
e52e712d 280 usb->devhdl = NULL;
16e76bae
MS
281 sdi->status = SR_ST_INACTIVE;
282
283 return SR_OK;
284}
285
286static int cleanup(void)
287{
c824eb63 288 return dev_clear();
16e76bae
MS
289}
290
584560f1 291static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 292 const struct sr_channel_group *cg)
16e76bae 293{
e52e712d 294 struct dev_context *devc;
16e76bae
MS
295 int ret;
296
53b4680f 297 (void)cg;
d3c74a6f 298
16e76bae 299 ret = SR_OK;
e52e712d
MS
300 devc = sdi->priv;
301
16e76bae 302 switch (key) {
e52e712d
MS
303 case SR_CONF_SAMPLERATE:
304 *data = g_variant_new_uint64(devc->samplerate);
305 break;
306 case SR_CONF_CAPTURE_RATIO:
307 *data = g_variant_new_uint64(devc->capture_ratio);
308 break;
16e76bae
MS
309 default:
310 return SR_ERR_NA;
311 }
312
313 return ret;
314}
315
584560f1 316static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 317 const struct sr_channel_group *cg)
16e76bae 318{
e52e712d 319 uint64_t samplerate, limit_samples, capture_ratio;
16e76bae
MS
320 int ret;
321
53b4680f 322 (void)cg;
d3c74a6f 323
16e76bae
MS
324 if (sdi->status != SR_ST_ACTIVE)
325 return SR_ERR_DEV_CLOSED;
326
327 ret = SR_OK;
e52e712d 328
16e76bae 329 switch (key) {
e52e712d
MS
330 case SR_CONF_LIMIT_SAMPLES:
331 limit_samples = g_variant_get_uint64(data);
79914b3a 332 ret = sl2_set_limit_samples(sdi, limit_samples);
e52e712d
MS
333 break;
334 case SR_CONF_SAMPLERATE:
335 samplerate = g_variant_get_uint64(data);
79914b3a 336 ret = sl2_set_samplerate(sdi, samplerate);
e52e712d
MS
337 break;
338 case SR_CONF_CAPTURE_RATIO:
339 capture_ratio = g_variant_get_uint64(data);
79914b3a 340 ret = sl2_set_capture_ratio(sdi, capture_ratio);
e52e712d 341 break;
16e76bae 342 default:
e52e712d 343 return SR_ERR_NA;
16e76bae
MS
344 }
345
346 return ret;
347}
348
584560f1 349static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 350 const struct sr_channel_group *cg)
16e76bae 351{
f0de2dd0 352 GVariant *gvar, *grange[2];
e52e712d 353 GVariantBuilder gvb;
16e76bae
MS
354 int ret;
355
356 (void)sdi;
53b4680f 357 (void)cg;
16e76bae
MS
358
359 ret = SR_OK;
e52e712d 360
16e76bae 361 switch (key) {
e52e712d 362 case SR_CONF_DEVICE_OPTIONS:
584560f1 363 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 364 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
e52e712d
MS
365 break;
366 case SR_CONF_SAMPLERATE:
367 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
368 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
79914b3a 369 sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
e52e712d
MS
370 sizeof(uint64_t));
371 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
372 *data = g_variant_builder_end(&gvb);
373 break;
02d5c0d8
BV
374 case SR_CONF_TRIGGER_MATCH:
375 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
376 trigger_matches, ARRAY_SIZE(trigger_matches),
377 sizeof(int32_t));
e52e712d 378 break;
f0de2dd0
BV
379 case SR_CONF_LIMIT_SAMPLES:
380 grange[0] = g_variant_new_uint64(0);
381 grange[1] = g_variant_new_uint64(MAX_SAMPLES);
382 *data = g_variant_new_tuple(grange, 2);
383 break;
16e76bae
MS
384 default:
385 return SR_ERR_NA;
386 }
387
388 return ret;
389}
390
e52e712d 391static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
16e76bae 392{
e52e712d
MS
393 struct drv_context *drvc;
394 struct dev_context *devc;
395 uint16_t trigger_bytes, tmp;
396 unsigned int i, j;
397 int ret;
16e76bae
MS
398
399 if (sdi->status != SR_ST_ACTIVE)
400 return SR_ERR_DEV_CLOSED;
401
e52e712d
MS
402 devc = sdi->priv;
403 drvc = di->priv;
404
405 devc->cb_data = cb_data;
406 devc->wait_data_ready_locked = TRUE;
407 devc->stopping_in_progress = FALSE;
408 devc->transfer_error = FALSE;
409 devc->samples_processed = 0;
410 devc->channel = 0;
411 devc->sample_packet = 0;
412
413 /*
414 * The trigger must be configured first because the calculation of the
415 * pre and post trigger samples depends on a configured trigger.
416 */
02d5c0d8 417 sl2_convert_trigger(sdi);
79914b3a 418 sl2_calculate_trigger_samples(sdi);
e52e712d
MS
419
420 trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
421
422 /* Calculate the number of expected sample packets. */
423 devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
424
425 /* Round up the number of expected sample packets. */
426 if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
427 devc->num_sample_packets++;
428
ba7dd8bb 429 devc->num_enabled_channels = 0;
e52e712d
MS
430
431 /*
ba7dd8bb 432 * Count the number of enabled channels and number them for a sequential
e52e712d
MS
433 * access.
434 */
3f239f08 435 for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
ba7dd8bb
UH
436 if (devc->channels[i]->enabled) {
437 devc->num_enabled_channels++;
438 devc->channel_map[j] = i;
e52e712d
MS
439 j++;
440 }
441 }
442
ba7dd8bb 443 sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
e52e712d
MS
444
445 /* Set up the transfer buffer for the acquisition. */
446 devc->xfer_data_out[0] = CMD_SAMPLE;
447 devc->xfer_data_out[1] = 0x00;
448
449 tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
450 memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
451
452 tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
453 memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
454
455 devc->xfer_data_out[6] = devc->samplerate_id;
456 devc->xfer_data_out[7] = devc->trigger_type;
457 devc->xfer_data_out[8] = devc->trigger_channel;
458 devc->xfer_data_out[9] = 0x00;
459
460 tmp = GUINT16_TO_LE(devc->after_trigger_delay);
461 memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
462
e52e712d 463 if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
c824eb63 464 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
e52e712d
MS
465 return SR_ERR;
466 }
467
102f1239
BV
468 usb_source_add(sdi->session, drvc->sr_ctx, 100,
469 ikalogic_scanalogic2_receive_data, (void *)sdi);
e52e712d
MS
470
471 sr_dbg("Acquisition started successfully.");
472
473 /* Send header packet to the session bus. */
474 std_session_send_df_header(cb_data, LOG_PREFIX);
475
476 devc->next_state = STATE_SAMPLE;
16e76bae
MS
477
478 return SR_OK;
479}
480
481static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
482{
483 (void)cb_data;
484
485 if (sdi->status != SR_ST_ACTIVE)
486 return SR_ERR_DEV_CLOSED;
487
e52e712d
MS
488 sr_dbg("Stopping acquisition.");
489
490 sdi->status = SR_ST_STOPPING;
16e76bae
MS
491
492 return SR_OK;
493}
494
495SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
496 .name = "ikalogic-scanalogic2",
e52e712d 497 .longname = "IKALOGIC Scanalogic-2",
16e76bae
MS
498 .api_version = 1,
499 .init = init,
500 .cleanup = cleanup,
501 .scan = scan,
502 .dev_list = dev_list,
503 .dev_clear = dev_clear,
504 .config_get = config_get,
505 .config_set = config_set,
506 .config_list = config_list,
507 .dev_open = dev_open,
508 .dev_close = dev_close,
509 .dev_acquisition_start = dev_acquisition_start,
510 .dev_acquisition_stop = dev_acquisition_stop,
511 .priv = NULL,
512};