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