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