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