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