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