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