]> sigrok.org Git - libsigrok.git/blame - src/hardware/ikalogic-scanaplus/api.c
ftdi: address ftdi_usb_purge_buffers() deprecation in libftdi 1.5
[libsigrok.git] / src / hardware / ikalogic-scanaplus / api.c
CommitLineData
fdf4a1f5
UH
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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
fdf4a1f5
UH
18 */
19
6ec6c43b 20#include <config.h>
fdf4a1f5
UH
21#include "protocol.h"
22
ab4bb6eb
UH
23#define USB_VENDOR_ID 0x0403
24#define USB_DEVICE_ID 0x6014
ab4bb6eb
UH
25#define USB_IPRODUCT "SCANAPLUS"
26
27#define SAMPLE_BUF_SIZE (8 * 1024 * 1024)
28
05199c0a 29static const uint32_t drvopts[] = {
ab4bb6eb 30 SR_CONF_LOGIC_ANALYZER,
05199c0a
UH
31};
32
33static const uint32_t devopts[] = {
5827f61b
BV
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
ab4bb6eb
UH
37};
38
ba7dd8bb 39static const char *channel_names[] = {
ab4bb6eb 40 "1", "2", "3", "4", "5", "6", "7", "8", "9",
ab4bb6eb
UH
41};
42
ce95428c 43/* Note: The IKALOGIC ScanaPLUS always samples at 100MHz. */
329733d9 44static const uint64_t samplerates[1] = { SR_MHZ(100) };
ab4bb6eb 45
3553451f 46static void clear_helper(struct dev_context *devc)
ab4bb6eb 47{
ab4bb6eb
UH
48 ftdi_free(devc->ftdic);
49 g_free(devc->compressed_buf);
50 g_free(devc->sample_buf);
51}
52
4f840ce9 53static int dev_clear(const struct sr_dev_driver *di)
ab4bb6eb 54{
3553451f 55 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
ab4bb6eb
UH
56}
57
4f840ce9 58static GSList *scan(struct sr_dev_driver *di, GSList *options)
fdf4a1f5 59{
ab4bb6eb 60 struct sr_dev_inst *sdi;
ab4bb6eb 61 struct dev_context *devc;
ab4bb6eb
UH
62 unsigned int i;
63 int ret;
fdf4a1f5
UH
64
65 (void)options;
66
f57d8ffe 67 devc = g_malloc0(sizeof(struct dev_context));
ab4bb6eb
UH
68
69 /* Allocate memory for the incoming compressed samples. */
70 if (!(devc->compressed_buf = g_try_malloc0(COMPRESSED_BUF_SIZE))) {
71 sr_err("compressed_buf malloc failed.");
72 goto err_free_devc;
73 }
74
75 /* Allocate memory for the uncompressed samples. */
76 if (!(devc->sample_buf = g_try_malloc0(SAMPLE_BUF_SIZE))) {
77 sr_err("sample_buf malloc failed.");
78 goto err_free_compressed_buf;
79 }
80
ab4bb6eb
UH
81 if (!(devc->ftdic = ftdi_new())) {
82 sr_err("Failed to initialize libftdi.");
83 goto err_free_sample_buf;
84 }
85
ab4bb6eb
UH
86 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
87 USB_IPRODUCT, NULL);
88 if (ret < 0) {
89 /* Log errors, except for -3 ("device not found"). */
90 if (ret != -3)
91 sr_err("Failed to open device (%d): %s", ret,
92 ftdi_get_error_string(devc->ftdic));
93 goto err_free_ftdic;
94 }
95
aac29cc1 96 sdi = g_malloc0(sizeof(struct sr_dev_inst));
45884333 97 sdi->status = SR_ST_INACTIVE;
b15ff1c9
UH
98 sdi->vendor = g_strdup("IKALOGIC");
99 sdi->model = g_strdup("ScanaPLUS");
ab4bb6eb
UH
100 sdi->priv = devc;
101
0f34cb47
UH
102 for (i = 0; i < ARRAY_SIZE(channel_names); i++)
103 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]);
ab4bb6eb 104
ab4bb6eb 105 scanaplus_close(devc);
fdf4a1f5 106
43376f33 107 return std_scan_complete(di, g_slist_append(NULL, sdi));
ab4bb6eb 108
ab4bb6eb
UH
109 scanaplus_close(devc);
110err_free_ftdic:
ca314e06 111 ftdi_free(devc->ftdic);
ab4bb6eb
UH
112err_free_sample_buf:
113 g_free(devc->sample_buf);
114err_free_compressed_buf:
115 g_free(devc->compressed_buf);
116err_free_devc:
117 g_free(devc);
ab4bb6eb
UH
118
119 return NULL;
fdf4a1f5
UH
120}
121
fdf4a1f5
UH
122static int dev_open(struct sr_dev_inst *sdi)
123{
ab4bb6eb
UH
124 struct dev_context *devc;
125 int ret;
126
127 devc = sdi->priv;
128
ab4bb6eb
UH
129 ret = ftdi_set_interface(devc->ftdic, INTERFACE_A);
130 if (ret < 0) {
131 sr_err("Failed to set FTDI interface A (%d): %s", ret,
132 ftdi_get_error_string(devc->ftdic));
133 return SR_ERR;
134 }
ab4bb6eb 135
ab4bb6eb
UH
136 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
137 USB_IPRODUCT, NULL);
138 if (ret < 0) {
139 sr_err("Failed to open device (%d): %s", ret,
140 ftdi_get_error_string(devc->ftdic));
141 return SR_ERR;
142 }
ab4bb6eb 143
e1a712ca 144 if ((ret = PURGE_FTDI_BOTH(devc->ftdic)) < 0) {
ab4bb6eb
UH
145 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
146 ret, ftdi_get_error_string(devc->ftdic));
147 goto err_dev_open_close_ftdic;
148 }
ab4bb6eb 149
ab4bb6eb
UH
150 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
151 if (ret < 0) {
152 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
153 ret, ftdi_get_error_string(devc->ftdic));
154 goto err_dev_open_close_ftdic;
155 }
ab4bb6eb 156
ab4bb6eb
UH
157 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF);
158 if (ret < 0) {
159 sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.",
160 ret, ftdi_get_error_string(devc->ftdic));
161 goto err_dev_open_close_ftdic;
162 }
ab4bb6eb 163
ab4bb6eb
UH
164 ret = ftdi_set_latency_timer(devc->ftdic, 2);
165 if (ret < 0) {
166 sr_err("Failed to set FTDI latency timer (%d): %s.",
167 ret, ftdi_get_error_string(devc->ftdic));
168 goto err_dev_open_close_ftdic;
169 }
ab4bb6eb 170
ab4bb6eb
UH
171 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
172 if (ret < 0) {
173 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
174 ret, ftdi_get_error_string(devc->ftdic));
175 goto err_dev_open_close_ftdic;
176 }
ab4bb6eb
UH
177
178 /* Get the ScanaPLUS device ID from the FTDI EEPROM. */
179 if ((ret = scanaplus_get_device_id(devc)) < 0) {
180 sr_err("Failed to get ScanaPLUS device ID: %d.", ret);
181 goto err_dev_open_close_ftdic;
182 }
183 sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
184 devc->devid[0], devc->devid[1], devc->devid[2]);
fdf4a1f5 185
fdf4a1f5 186 return SR_OK;
ab4bb6eb
UH
187
188err_dev_open_close_ftdic:
189 scanaplus_close(devc);
7e463623 190
ab4bb6eb 191 return SR_ERR;
fdf4a1f5
UH
192}
193
194static int dev_close(struct sr_dev_inst *sdi)
195{
ab4bb6eb
UH
196 struct dev_context *devc;
197
ab4bb6eb
UH
198 devc = sdi->priv;
199
093e1cba 200 return scanaplus_close(devc);
fdf4a1f5
UH
201}
202
dd7a72ea
UH
203static int config_get(uint32_t key, GVariant **data,
204 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
fdf4a1f5 205{
fdf4a1f5 206 (void)sdi;
53b4680f 207 (void)cg;
fdf4a1f5 208
584560f1 209 switch (key) {
ab4bb6eb
UH
210 case SR_CONF_SAMPLERATE:
211 /* The ScanaPLUS samplerate is 100MHz and can't be changed. */
212 *data = g_variant_new_uint64(SR_MHZ(100));
213 break;
fdf4a1f5
UH
214 default:
215 return SR_ERR_NA;
216 }
217
ab4bb6eb 218 return SR_OK;
fdf4a1f5
UH
219}
220
dd7a72ea
UH
221static int config_set(uint32_t key, GVariant *data,
222 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
fdf4a1f5 223{
ab4bb6eb 224 struct dev_context *devc;
fdf4a1f5 225
53b4680f 226 (void)cg;
d3c74a6f 227
ab4bb6eb
UH
228 devc = sdi->priv;
229
584560f1 230 switch (key) {
ab4bb6eb
UH
231 case SR_CONF_SAMPLERATE:
232 if (g_variant_get_uint64(data) != SR_MHZ(100)) {
233 sr_err("ScanaPLUS only supports samplerate = 100MHz.");
234 return SR_ERR_ARG;
235 }
236 /* Nothing to do, the ScanaPLUS samplerate is always 100MHz. */
237 break;
238 case SR_CONF_LIMIT_MSEC:
ab4bb6eb
UH
239 devc->limit_msec = g_variant_get_uint64(data);
240 break;
241 case SR_CONF_LIMIT_SAMPLES:
ab4bb6eb
UH
242 devc->limit_samples = g_variant_get_uint64(data);
243 break;
fdf4a1f5 244 default:
ab4bb6eb 245 return SR_ERR_NA;
fdf4a1f5
UH
246 }
247
ab4bb6eb 248 return SR_OK;
fdf4a1f5
UH
249}
250
dd7a72ea
UH
251static int config_list(uint32_t key, GVariant **data,
252 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
fdf4a1f5 253{
fdf4a1f5 254 switch (key) {
ab4bb6eb 255 case SR_CONF_DEVICE_OPTIONS:
23772462 256 return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts);
ab4bb6eb 257 case SR_CONF_SAMPLERATE:
53012da6 258 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
ab4bb6eb 259 break;
fdf4a1f5
UH
260 default:
261 return SR_ERR_NA;
262 }
263
ab4bb6eb 264 return SR_OK;
fdf4a1f5
UH
265}
266
695dc859 267static int dev_acquisition_start(const struct sr_dev_inst *sdi)
fdf4a1f5 268{
ab4bb6eb
UH
269 int ret;
270 struct dev_context *devc;
fdf4a1f5 271
208c1d35 272 devc = sdi->priv;
ab4bb6eb
UH
273
274 if (!devc->ftdic)
275 return SR_ERR_BUG;
276
ba7dd8bb 277 /* TODO: Configure channels later (thresholds etc.). */
ab4bb6eb 278
ab4bb6eb
UH
279 /* Properly reset internal variables before every new acquisition. */
280 devc->compressed_bytes_ignored = 0;
281 devc->samples_sent = 0;
282 devc->bytes_received = 0;
283
284 if ((ret = scanaplus_init(devc)) < 0)
285 return ret;
286
287 if ((ret = scanaplus_start_acquisition(devc)) < 0)
288 return ret;
289
bee2b016 290 std_session_send_df_header(sdi);
ab4bb6eb
UH
291
292 /* Hook up a dummy handler to receive data from the device. */
c650d3ec 293 sr_session_source_add(sdi->session, -1, 0, 0, scanaplus_receive_data, (void *)sdi);
ab4bb6eb 294
fdf4a1f5
UH
295 return SR_OK;
296}
297
695dc859 298static int dev_acquisition_stop(struct sr_dev_inst *sdi)
fdf4a1f5 299{
102f1239 300 sr_session_source_remove(sdi->session, -1);
bee2b016 301 std_session_send_df_end(sdi);
fdf4a1f5
UH
302
303 return SR_OK;
304}
305
dd5c48a6 306static struct sr_dev_driver ikalogic_scanaplus_driver_info = {
fdf4a1f5 307 .name = "ikalogic-scanaplus",
ce95428c 308 .longname = "IKALOGIC ScanaPLUS",
fdf4a1f5 309 .api_version = 1,
c2fdcc25 310 .init = std_init,
700d6b64 311 .cleanup = std_cleanup,
fdf4a1f5 312 .scan = scan,
c01bf34c 313 .dev_list = std_dev_list,
fdf4a1f5
UH
314 .dev_clear = dev_clear,
315 .config_get = config_get,
316 .config_set = config_set,
317 .config_list = config_list,
318 .dev_open = dev_open,
319 .dev_close = dev_close,
320 .dev_acquisition_start = dev_acquisition_start,
321 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 322 .context = NULL,
fdf4a1f5 323};
dd5c48a6 324SR_REGISTER_DEV_DRIVER(ikalogic_scanaplus_driver_info);