]> sigrok.org Git - libsigrok.git/blob - src/hardware/ikalogic-scanalogic2/api.c
Don't reset instance list in scan() callback
[libsigrok.git] / src / hardware / ikalogic-scanalogic2 / api.c
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
23 static 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
31 static const int32_t trigger_matches[] = {
32         SR_TRIGGER_RISING,
33         SR_TRIGGER_FALLING,
34         SR_TRIGGER_EDGE,
35 };
36
37 SR_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
51 static const char *channel_names[] = {
52         "0", "1", "2", "3",
53 };
54
55 static 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->driver = di;
110                 sdi->inst_type = SR_INST_USB;
111                 sdi->conn = usb;
112
113                 for (i = 0; i < ARRAY_SIZE(channel_names); i++)
114                         devc->channels[i] = sr_channel_new(sdi, i,
115                                 SR_CHANNEL_LOGIC, TRUE, channel_names[i]);
116
117                 devc->state = STATE_IDLE;
118                 devc->next_state = STATE_IDLE;
119
120                 /* Set default samplerate. */
121                 sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
122
123                 /* Set default capture ratio. */
124                 devc->capture_ratio = 0;
125
126                 /* Set default after trigger delay. */
127                 devc->after_trigger_delay = 0;
128
129                 memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE +
130                         PACKET_LENGTH);
131                 memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE +
132                         PACKET_LENGTH);
133
134                 libusb_fill_control_setup(devc->xfer_buf_in,
135                         USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
136                         USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
137                         PACKET_LENGTH);
138                 libusb_fill_control_setup(devc->xfer_buf_out,
139                         USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT,
140                         USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
141                         PACKET_LENGTH);
142
143                 devc->xfer_data_in = devc->xfer_buf_in +
144                         LIBUSB_CONTROL_SETUP_SIZE;
145                 devc->xfer_data_out = devc->xfer_buf_out +
146                         LIBUSB_CONTROL_SETUP_SIZE;
147
148                 drvc->instances = g_slist_append(drvc->instances, sdi);
149                 devices = g_slist_append(devices, sdi);
150         }
151
152         g_slist_free(usb_devices);
153
154         return devices;
155 }
156
157 static void clear_dev_context(void *priv)
158 {
159         struct dev_context *devc;
160
161         devc = priv;
162
163         sr_dbg("Device context cleared.");
164
165         libusb_free_transfer(devc->xfer_in);
166         libusb_free_transfer(devc->xfer_out);
167         g_free(devc);
168 }
169
170 static int dev_clear(const struct sr_dev_driver *di)
171 {
172         return std_dev_clear(di, &clear_dev_context);
173 }
174
175 static int dev_open(struct sr_dev_inst *sdi)
176 {
177         struct sr_dev_driver *di = sdi->driver;
178         struct drv_context *drvc = di->context;
179         struct dev_context *devc;
180         struct sr_usb_dev_inst *usb;
181         uint8_t buffer[PACKET_LENGTH];
182         int ret;
183
184         usb = sdi->conn;
185         devc = sdi->priv;
186
187         if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
188                 return SR_ERR;
189
190         /*
191          * Determine if a kernel driver is active on this interface and, if so,
192          * detach it.
193          */
194         if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
195                 ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
196                 if (ret < 0) {
197                         sr_err("Failed to detach kernel driver: %s.",
198                                 libusb_error_name(ret));
199                         return SR_ERR;
200                 }
201         }
202
203         if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
204                 sr_err("Failed to claim interface: %s.",
205                         libusb_error_name(ret));
206                 return SR_ERR;
207         }
208
209         libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
210                 devc->xfer_buf_in, sl2_receive_transfer_in,
211                 sdi, USB_TIMEOUT_MS);
212
213         libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
214                 devc->xfer_buf_out, sl2_receive_transfer_out,
215                 sdi, USB_TIMEOUT_MS);
216
217         memset(buffer, 0, sizeof(buffer));
218
219         buffer[0] = CMD_RESET;
220         if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
221                 sr_err("Device reset failed: %s.", libusb_error_name(ret));
222                 return SR_ERR;
223         }
224
225         /*
226          * Set the device to idle state. If the device is not in idle state it
227          * possibly will reset itself after a few seconds without being used
228          * and thereby close the connection.
229          */
230         buffer[0] = CMD_IDLE;
231         if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
232                 sr_err("Failed to set device in idle state: %s.",
233                         libusb_error_name(ret));
234                 return SR_ERR;
235         }
236
237         sdi->status = SR_ST_ACTIVE;
238
239         return SR_OK;
240 }
241
242 static int dev_close(struct sr_dev_inst *sdi)
243 {
244         struct sr_usb_dev_inst *usb;
245
246         usb = sdi->conn;
247
248         if (!usb->devhdl)
249                 return SR_OK;
250
251         libusb_release_interface(usb->devhdl, USB_INTERFACE);
252         libusb_close(usb->devhdl);
253
254         usb->devhdl = NULL;
255         sdi->status = SR_ST_INACTIVE;
256
257         return SR_OK;
258 }
259
260 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
261                 const struct sr_channel_group *cg)
262 {
263         struct dev_context *devc;
264         int ret;
265
266         (void)cg;
267
268         ret = SR_OK;
269         devc = sdi->priv;
270
271         switch (key) {
272         case SR_CONF_SAMPLERATE:
273                 *data = g_variant_new_uint64(devc->samplerate);
274                 break;
275         case SR_CONF_CAPTURE_RATIO:
276                 *data = g_variant_new_uint64(devc->capture_ratio);
277                 break;
278         default:
279                 return SR_ERR_NA;
280         }
281
282         return ret;
283 }
284
285 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
286                 const struct sr_channel_group *cg)
287 {
288         uint64_t samplerate, limit_samples, capture_ratio;
289         int ret;
290
291         (void)cg;
292
293         if (sdi->status != SR_ST_ACTIVE)
294                 return SR_ERR_DEV_CLOSED;
295
296         switch (key) {
297         case SR_CONF_LIMIT_SAMPLES:
298                 limit_samples = g_variant_get_uint64(data);
299                 ret = sl2_set_limit_samples(sdi, limit_samples);
300                 break;
301         case SR_CONF_SAMPLERATE:
302                 samplerate = g_variant_get_uint64(data);
303                 ret = sl2_set_samplerate(sdi, samplerate);
304                 break;
305         case SR_CONF_CAPTURE_RATIO:
306                 capture_ratio = g_variant_get_uint64(data);
307                 ret = sl2_set_capture_ratio(sdi, capture_ratio);
308                 break;
309         default:
310                 return SR_ERR_NA;
311         }
312
313         return ret;
314 }
315
316 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
317                 const struct sr_channel_group *cg)
318 {
319         GVariant *gvar, *grange[2];
320         GVariantBuilder gvb;
321         int ret;
322
323         (void)sdi;
324         (void)cg;
325
326         ret = SR_OK;
327         switch (key) {
328         case SR_CONF_DEVICE_OPTIONS:
329                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
330                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
331                 break;
332         case SR_CONF_SAMPLERATE:
333                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
334                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
335                         sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
336                         sizeof(uint64_t));
337                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
338                 *data = g_variant_builder_end(&gvb);
339                 break;
340         case SR_CONF_TRIGGER_MATCH:
341                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
342                                 trigger_matches, ARRAY_SIZE(trigger_matches),
343                                 sizeof(int32_t));
344                 break;
345         case SR_CONF_LIMIT_SAMPLES:
346                 grange[0] = g_variant_new_uint64(0);
347                 grange[1] = g_variant_new_uint64(MAX_SAMPLES);
348                 *data = g_variant_new_tuple(grange, 2);
349                 break;
350         default:
351                 return SR_ERR_NA;
352         }
353
354         return ret;
355 }
356
357 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
358 {
359         struct sr_dev_driver *di = sdi->driver;
360         struct drv_context *drvc;
361         struct dev_context *devc;
362         uint16_t trigger_bytes, tmp;
363         unsigned int i, j;
364         int ret;
365
366         if (sdi->status != SR_ST_ACTIVE)
367                 return SR_ERR_DEV_CLOSED;
368
369         devc = sdi->priv;
370         drvc = di->context;
371
372         devc->wait_data_ready_locked = TRUE;
373         devc->stopping_in_progress = FALSE;
374         devc->transfer_error = FALSE;
375         devc->samples_processed = 0;
376         devc->channel = 0;
377         devc->sample_packet = 0;
378
379         /*
380          * The trigger must be configured first because the calculation of the
381          * pre and post trigger samples depends on a configured trigger.
382          */
383         sl2_convert_trigger(sdi);
384         sl2_calculate_trigger_samples(sdi);
385
386         trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
387
388         /* Calculate the number of expected sample packets. */
389         devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES;
390
391         /* Round up the number of expected sample packets. */
392         if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
393                 devc->num_sample_packets++;
394
395         devc->num_enabled_channels = 0;
396
397         /*
398          * Count the number of enabled channels and number them for a sequential
399          * access.
400          */
401         for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
402                 if (devc->channels[i]->enabled) {
403                         devc->num_enabled_channels++;
404                         devc->channel_map[j] = i;
405                         j++;
406                 }
407         }
408
409         sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
410
411         /* Set up the transfer buffer for the acquisition. */
412         devc->xfer_data_out[0] = CMD_SAMPLE;
413         devc->xfer_data_out[1] = 0x00;
414
415         tmp = GUINT16_TO_LE(devc->pre_trigger_bytes);
416         memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp));
417
418         tmp = GUINT16_TO_LE(devc->post_trigger_bytes);
419         memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp));
420
421         devc->xfer_data_out[6] = devc->samplerate_id;
422         devc->xfer_data_out[7] = devc->trigger_type;
423         devc->xfer_data_out[8] = devc->trigger_channel;
424         devc->xfer_data_out[9] = 0x00;
425
426         tmp = GUINT16_TO_LE(devc->after_trigger_delay);
427         memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
428
429         if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
430                 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
431                 return SR_ERR;
432         }
433
434         usb_source_add(sdi->session, drvc->sr_ctx, 100,
435                         ikalogic_scanalogic2_receive_data, (void *)sdi);
436
437         sr_dbg("Acquisition started successfully.");
438
439         std_session_send_df_header(sdi, LOG_PREFIX);
440
441         devc->next_state = STATE_SAMPLE;
442
443         return SR_OK;
444 }
445
446 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
447 {
448         if (sdi->status != SR_ST_ACTIVE)
449                 return SR_ERR_DEV_CLOSED;
450
451         sr_dbg("Stopping acquisition.");
452
453         sdi->status = SR_ST_STOPPING;
454
455         return SR_OK;
456 }
457
458 static struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
459         .name = "ikalogic-scanalogic2",
460         .longname = "IKALOGIC Scanalogic-2",
461         .api_version = 1,
462         .init = std_init,
463         .cleanup = std_cleanup,
464         .scan = scan,
465         .dev_list = std_dev_list,
466         .dev_clear = dev_clear,
467         .config_get = config_get,
468         .config_set = config_set,
469         .config_list = config_list,
470         .dev_open = dev_open,
471         .dev_close = dev_close,
472         .dev_acquisition_start = dev_acquisition_start,
473         .dev_acquisition_stop = dev_acquisition_stop,
474         .context = NULL,
475 };
476 SR_REGISTER_DEV_DRIVER(ikalogic_scanalogic2_driver_info);