]> sigrok.org Git - libsigrok.git/blob - hardware/ikalogic-scanaplus/api.c
build: Portability fixes.
[libsigrok.git] / hardware / ikalogic-scanaplus / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "protocol.h"
22
23 #define USB_VENDOR_ID                   0x0403
24 #define USB_DEVICE_ID                   0x6014
25 #define USB_VENDOR_NAME                 "IKALOGIC"
26 #define USB_MODEL_NAME                  "ScanaPLUS"
27 #define USB_IPRODUCT                    "SCANAPLUS"
28
29 #define SAMPLE_BUF_SIZE                 (8 * 1024 * 1024)
30
31 static const int32_t hwcaps[] = {
32         SR_CONF_LOGIC_ANALYZER,
33         SR_CONF_SAMPLERATE,
34         SR_CONF_LIMIT_MSEC,
35         SR_CONF_LIMIT_SAMPLES,
36         SR_CONF_CONTINUOUS, // TODO?
37 };
38
39 /* Channels are numbered 1-9. */
40 static const char *channel_names[] = {
41         "1", "2", "3", "4", "5", "6", "7", "8", "9",
42         NULL,
43 };
44
45 /* Note: The IKALOGIC ScanaPLUS always samples at 100MHz. */
46 static uint64_t samplerates[1] = { SR_MHZ(100) };
47
48 SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info;
49 static struct sr_dev_driver *di = &ikalogic_scanaplus_driver_info;
50
51 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
52
53 static void clear_helper(void *priv)
54 {
55         struct dev_context *devc;
56
57         devc = priv;
58
59         ftdi_free(devc->ftdic);
60         g_free(devc->compressed_buf);
61         g_free(devc->sample_buf);
62 }
63
64 static int dev_clear(void)
65 {
66         return std_dev_clear(di, clear_helper);
67 }
68
69 static int init(struct sr_context *sr_ctx)
70 {
71         return std_init(sr_ctx, di, LOG_PREFIX);
72 }
73
74 static GSList *scan(GSList *options)
75 {
76         struct sr_dev_inst *sdi;
77         struct sr_channel *ch;
78         struct drv_context *drvc;
79         struct dev_context *devc;
80         GSList *devices;
81         unsigned int i;
82         int ret;
83
84         (void)options;
85
86         drvc = di->priv;
87
88         devices = NULL;
89
90         /* Allocate memory for our private device context. */
91         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
92                 sr_err("Device context malloc failed.");
93                 goto err_free_nothing;
94         }
95
96         /* Allocate memory for the incoming compressed samples. */
97         if (!(devc->compressed_buf = g_try_malloc0(COMPRESSED_BUF_SIZE))) {
98                 sr_err("compressed_buf malloc failed.");
99                 goto err_free_devc;
100         }
101
102         /* Allocate memory for the uncompressed samples. */
103         if (!(devc->sample_buf = g_try_malloc0(SAMPLE_BUF_SIZE))) {
104                 sr_err("sample_buf malloc failed.");
105                 goto err_free_compressed_buf;
106         }
107
108         /* Allocate memory for the FTDI context (ftdic) and initialize it. */
109         if (!(devc->ftdic = ftdi_new())) {
110                 sr_err("Failed to initialize libftdi.");
111                 goto err_free_sample_buf;
112         }
113
114         /* Check for the device and temporarily open it. */
115         ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
116                                  USB_IPRODUCT, NULL);
117         if (ret < 0) {
118                 /* Log errors, except for -3 ("device not found"). */
119                 if (ret != -3)
120                         sr_err("Failed to open device (%d): %s", ret,
121                                ftdi_get_error_string(devc->ftdic));
122                 goto err_free_ftdic;
123         }
124
125         /* Register the device with libsigrok. */
126         sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
127                         USB_VENDOR_NAME, USB_MODEL_NAME, NULL);
128         if (!sdi) {
129                 sr_err("Failed to create device instance.");
130                 goto err_close_ftdic;
131         }
132         sdi->driver = di;
133         sdi->priv = devc;
134
135         for (i = 0; channel_names[i]; i++) {
136                 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
137                                            channel_names[i])))
138                         return NULL;
139                 sdi->channels = g_slist_append(sdi->channels, ch);
140         }
141
142         devices = g_slist_append(devices, sdi);
143         drvc->instances = g_slist_append(drvc->instances, sdi);
144
145         /* Close device. We'll reopen it again when we need it. */
146         scanaplus_close(devc);
147
148         return devices;
149
150 err_close_ftdic:
151         scanaplus_close(devc);
152 err_free_ftdic:
153         ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
154 err_free_sample_buf:
155         g_free(devc->sample_buf);
156 err_free_compressed_buf:
157         g_free(devc->compressed_buf);
158 err_free_devc:
159         g_free(devc);
160 err_free_nothing:
161
162         return NULL;
163 }
164
165 static GSList *dev_list(void)
166 {
167         return ((struct drv_context *)(di->priv))->instances;
168 }
169
170 static int dev_open(struct sr_dev_inst *sdi)
171 {
172         struct dev_context *devc;
173         int ret;
174
175         devc = sdi->priv;
176
177         /* Select interface A, otherwise communication will fail. */
178         ret = ftdi_set_interface(devc->ftdic, INTERFACE_A);
179         if (ret < 0) {
180                 sr_err("Failed to set FTDI interface A (%d): %s", ret,
181                        ftdi_get_error_string(devc->ftdic));
182                 return SR_ERR;
183         }
184         sr_dbg("FTDI chip interface A set successfully.");
185
186         /* Open the device. */
187         ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
188                                  USB_IPRODUCT, NULL);
189         if (ret < 0) {
190                 sr_err("Failed to open device (%d): %s", ret,
191                        ftdi_get_error_string(devc->ftdic));
192                 return SR_ERR;
193         }
194         sr_dbg("FTDI device opened successfully.");
195
196         /* Purge RX/TX buffers in the FTDI chip. */
197         if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
198                 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
199                        ret, ftdi_get_error_string(devc->ftdic));
200                 goto err_dev_open_close_ftdic;
201         }
202         sr_dbg("FTDI chip buffers purged successfully.");
203
204         /* Reset the FTDI bitmode. */
205         ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
206         if (ret < 0) {
207                 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
208                        ret, ftdi_get_error_string(devc->ftdic));
209                 goto err_dev_open_close_ftdic;
210         }
211         sr_dbg("FTDI chip bitmode reset successfully.");
212
213         /* Set FTDI bitmode to "sync FIFO". */
214         ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF);
215         if (ret < 0) {
216                 sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.",
217                        ret, ftdi_get_error_string(devc->ftdic));
218                 goto err_dev_open_close_ftdic;
219         }
220         sr_dbg("FTDI chip sync FIFO mode entered successfully.");
221
222         /* Set the FTDI latency timer to 2. */
223         ret = ftdi_set_latency_timer(devc->ftdic, 2);
224         if (ret < 0) {
225                 sr_err("Failed to set FTDI latency timer (%d): %s.",
226                        ret, ftdi_get_error_string(devc->ftdic));
227                 goto err_dev_open_close_ftdic;
228         }
229         sr_dbg("FTDI chip latency timer set successfully.");
230
231         /* Set the FTDI read data chunk size to 64kB. */
232         ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
233         if (ret < 0) {
234                 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
235                        ret, ftdi_get_error_string(devc->ftdic));
236                 goto err_dev_open_close_ftdic;
237         }
238         sr_dbg("FTDI chip read data chunk size set successfully.");
239
240         /* Get the ScanaPLUS device ID from the FTDI EEPROM. */
241         if ((ret = scanaplus_get_device_id(devc)) < 0) {
242                 sr_err("Failed to get ScanaPLUS device ID: %d.", ret);
243                 goto err_dev_open_close_ftdic;
244         }
245         sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
246                devc->devid[0], devc->devid[1], devc->devid[2]);
247
248         sdi->status = SR_ST_ACTIVE;
249
250         return SR_OK;
251
252 err_dev_open_close_ftdic:
253         scanaplus_close(devc);
254         return SR_ERR;
255 }
256
257 static int dev_close(struct sr_dev_inst *sdi)
258 {
259         int ret;
260         struct dev_context *devc;
261
262         ret = SR_OK;
263         devc = sdi->priv;
264
265         if (sdi->status == SR_ST_ACTIVE) {
266                 sr_dbg("Status ACTIVE, closing device.");
267                 ret = scanaplus_close(devc);
268         } else {
269                 sr_spew("Status not ACTIVE, nothing to do.");
270         }
271
272         sdi->status = SR_ST_INACTIVE;
273
274         return ret;
275 }
276
277 static int cleanup(void)
278 {
279         return dev_clear();
280 }
281
282 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
283                 const struct sr_channel_group *cg)
284 {
285         (void)sdi;
286         (void)cg;
287
288         switch (id) {
289         case SR_CONF_SAMPLERATE:
290                 /* The ScanaPLUS samplerate is 100MHz and can't be changed. */
291                 *data = g_variant_new_uint64(SR_MHZ(100));
292                 break;
293         default:
294                 return SR_ERR_NA;
295         }
296
297         return SR_OK;
298 }
299
300 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
301                 const struct sr_channel_group *cg)
302 {
303         struct dev_context *devc;
304
305         (void)cg;
306
307         if (sdi->status != SR_ST_ACTIVE)
308                 return SR_ERR_DEV_CLOSED;
309
310         devc = sdi->priv;
311
312         switch (id) {
313         case SR_CONF_SAMPLERATE:
314                 if (g_variant_get_uint64(data) != SR_MHZ(100)) {
315                         sr_err("ScanaPLUS only supports samplerate = 100MHz.");
316                         return SR_ERR_ARG;
317                 }
318                 /* Nothing to do, the ScanaPLUS samplerate is always 100MHz. */
319                 break;
320         case SR_CONF_LIMIT_MSEC:
321                 if (g_variant_get_uint64(data) == 0)
322                         return SR_ERR_ARG;
323                 devc->limit_msec = g_variant_get_uint64(data);
324                 break;
325         case SR_CONF_LIMIT_SAMPLES:
326                 if (g_variant_get_uint64(data) == 0)
327                         return SR_ERR_ARG;
328                 devc->limit_samples = g_variant_get_uint64(data);
329                 break;
330         default:
331                 return SR_ERR_NA;
332         }
333
334         return SR_OK;
335 }
336
337 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
338                 const struct sr_channel_group *cg)
339 {
340         GVariant *gvar;
341         GVariantBuilder gvb;
342
343         (void)sdi;
344         (void)cg;
345
346         switch (key) {
347         case SR_CONF_DEVICE_OPTIONS:
348                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
349                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
350                 break;
351         case SR_CONF_SAMPLERATE:
352                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
353                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
354                                 samplerates, ARRAY_SIZE(samplerates),
355                                 sizeof(uint64_t));
356                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
357                 *data = g_variant_builder_end(&gvb);
358                 break;
359         default:
360                 return SR_ERR_NA;
361         }
362
363         return SR_OK;
364 }
365
366 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
367 {
368         int ret;
369         struct dev_context *devc;
370
371         if (sdi->status != SR_ST_ACTIVE)
372                 return SR_ERR_DEV_CLOSED;
373
374         if (!(devc = sdi->priv))
375                 return SR_ERR_BUG;
376
377         if (!devc->ftdic)
378                 return SR_ERR_BUG;
379
380         /* TODO: Configure channels later (thresholds etc.). */
381
382         devc->cb_data = cb_data;
383
384         /* Properly reset internal variables before every new acquisition. */
385         devc->compressed_bytes_ignored = 0;
386         devc->samples_sent = 0;
387         devc->bytes_received = 0;
388
389         if ((ret = scanaplus_init(devc)) < 0)
390                 return ret;
391
392         if ((ret = scanaplus_start_acquisition(devc)) < 0)
393                 return ret;
394
395         /* Send header packet to the session bus. */
396         std_session_send_df_header(sdi, LOG_PREFIX);
397
398         /* Hook up a dummy handler to receive data from the device. */
399         sr_session_source_add(sdi->session, -1, G_IO_IN, 0, scanaplus_receive_data, (void *)sdi);
400
401         return SR_OK;
402 }
403
404 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
405 {
406         struct sr_datafeed_packet packet;
407
408         (void)cb_data;
409
410         sr_dbg("Stopping acquisition.");
411         sr_session_source_remove(sdi->session, -1);
412
413         /* Send end packet to the session bus. */
414         sr_dbg("Sending SR_DF_END.");
415         packet.type = SR_DF_END;
416         sr_session_send(sdi, &packet);
417
418         return SR_OK;
419 }
420
421 SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info = {
422         .name = "ikalogic-scanaplus",
423         .longname = "IKALOGIC ScanaPLUS",
424         .api_version = 1,
425         .init = init,
426         .cleanup = cleanup,
427         .scan = scan,
428         .dev_list = dev_list,
429         .dev_clear = dev_clear,
430         .config_get = config_get,
431         .config_set = config_set,
432         .config_list = config_list,
433         .dev_open = dev_open,
434         .dev_close = dev_close,
435         .dev_acquisition_start = dev_acquisition_start,
436         .dev_acquisition_stop = dev_acquisition_stop,
437         .priv = NULL,
438 };