]> sigrok.org Git - libsigrok.git/blame - src/hardware/saleae-logic-pro/api.c
Add sr_dev_acquisition_start(), factor out SR_ERR_DEV_CLOSED check.
[libsigrok.git] / src / hardware / saleae-logic-pro / api.c
CommitLineData
a8e913c4
JL
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2017 Jan Luebbe <jluebbe@lasnet.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>
ca7d19b5
JL
21#include <glib.h>
22#include <string.h>
a8e913c4
JL
23#include "protocol.h"
24
bb0c5271
UH
25#define BUF_COUNT 8
26#define BUF_SIZE (16 * 1024)
27#define BUF_TIMEOUT (1000 * 1000)
ca7d19b5 28
b6189f7c 29SR_PRIV struct sr_dev_driver saleae_logic_pro_driver_info;
a8e913c4 30
ca7d19b5
JL
31static const uint32_t drvopts[] = {
32 SR_CONF_LOGIC_ANALYZER,
33};
34
35static const uint32_t scanopts[] = {
36 SR_CONF_CONN,
37};
38
39static const uint32_t devopts[] = {
40 SR_CONF_CONTINUOUS,
41 SR_CONF_CONN | SR_CONF_GET,
42 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43};
44
45static const char *channel_names[] = {
46 "0", "1", "2", "3", "4", "5", "6", "7",
47 "8", "9", "10", "11", "12", "13", "14", "15",
48};
49
50static const uint64_t samplerates[] = {
51 SR_MHZ(1),
52 SR_MHZ(2),
53 SR_KHZ(2500),
54 SR_MHZ(10),
55 SR_MHZ(25),
56 SR_MHZ(50),
57};
58
59static gboolean scan_firmware(libusb_device *dev)
60{
61 struct libusb_device_descriptor des;
62 struct libusb_device_handle *hdl;
63 gboolean ret;
64 unsigned char strdesc[64];
65
66 hdl = NULL;
67 ret = FALSE;
68
69 libusb_get_device_descriptor(dev, &des);
70
71 if (libusb_open(dev, &hdl) != 0)
72 goto out;
73
74 if (libusb_get_string_descriptor_ascii(hdl,
75 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
76 goto out;
77 if (strcmp((const char *)strdesc, "Saleae"))
78 goto out;
79
80 if (libusb_get_string_descriptor_ascii(hdl,
81 des.iProduct, strdesc, sizeof(strdesc)) < 0)
82 goto out;
83 if (strcmp((const char *)strdesc, "Logic Pro"))
84 goto out;
85
86 ret = TRUE;
87
88out:
89 if (hdl)
90 libusb_close(hdl);
91
92 return ret;
93}
94
a8e913c4
JL
95static GSList *scan(struct sr_dev_driver *di, GSList *options)
96{
97 struct drv_context *drvc;
ca7d19b5
JL
98 struct dev_context *devc;
99 struct sr_dev_inst *sdi;
100 GSList *devices, *conn_devices;
101 libusb_device **devlist;
102 struct libusb_device_descriptor des;
103 const char *conn;
104 char connection_id[64];
a8e913c4
JL
105
106 devices = NULL;
107 drvc = di->context;
108 drvc->instances = NULL;
109
ca7d19b5
JL
110 conn = NULL;
111 for (GSList *l = options; l; l = l->next) {
112 struct sr_config *src = l->data;
113
114 switch (src->key) {
115 case SR_CONF_CONN:
116 conn = g_variant_get_string(src->data, NULL);
117 break;
118 }
119 }
120
121 if (conn)
122 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
123 else
124 conn_devices = NULL;
125
126 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
127 for (unsigned int i = 0; devlist[i]; i++) {
128 if (conn) {
129 struct sr_usb_dev_inst *usb = NULL;
130 GSList *l;
131
132 for (l = conn_devices; l; l = l->next) {
133 usb = l->data;
134 if (usb->bus == libusb_get_bus_number(devlist[i])
135 && usb->address == libusb_get_device_address(devlist[i]))
136 break;
137 }
138 if (!l)
139 /* This device matched none of the ones that
140 * matched the conn specification. */
141 continue;
142 }
143
144 libusb_get_device_descriptor(devlist[i], &des);
145
146 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
147
148 if (des.idVendor != 0x21a9 || des.idProduct != 0x1006)
149 continue;
150
151 if (!scan_firmware(devlist[i])) {
152 sr_err("Found a Logic Pro device, but firmware is not loaded (use Saleae application).");
153 continue;
154 }
155
156 sdi = g_malloc0(sizeof(struct sr_dev_inst));
157 sdi->status = SR_ST_INITIALIZING;
158 sdi->vendor = g_strdup("Saleae");
159 sdi->model = g_strdup("Logic Pro 16");
160 sdi->connection_id = g_strdup(connection_id);
161
162 for (unsigned int j = 0; j < ARRAY_SIZE(channel_names); j++)
163 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
164 channel_names[j]);
165
166 sr_dbg("Found a Logic Pro 16 device.");
167 sdi->status = SR_ST_INACTIVE;
168 sdi->inst_type = SR_INST_USB;
169 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
170 libusb_get_device_address(devlist[i]), NULL);
171
172 devc = g_malloc0(sizeof(struct dev_context));
173 sdi->priv = devc;
174 devices = g_slist_append(devices, sdi);
175
176 }
177 libusb_free_device_list(devlist, 1);
178 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
a8e913c4 179
ca7d19b5 180 return std_scan_complete(di, devices);
a8e913c4
JL
181}
182
183static int dev_clear(const struct sr_dev_driver *di)
184{
185 return std_dev_clear(di, NULL);
186}
187
188static int dev_open(struct sr_dev_inst *sdi)
189{
ca7d19b5
JL
190 struct sr_dev_driver *di = sdi->driver;
191 struct drv_context *drvc = di->context;
192 struct dev_context *devc = sdi->priv;
193 struct sr_usb_dev_inst *usb = sdi->conn;
194 int ret;
a8e913c4 195
ca7d19b5
JL
196 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
197 return SR_ERR;
198
199 if ((ret = libusb_claim_interface(usb->devhdl, 0))) {
200 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
201 return SR_ERR;
202 }
203
bb0c5271 204 /* Configure default samplerate. */
ca7d19b5
JL
205 if (devc->dig_samplerate == 0)
206 devc->dig_samplerate = samplerates[3];
a8e913c4
JL
207
208 sdi->status = SR_ST_ACTIVE;
209
210 return SR_OK;
211}
212
213static int dev_close(struct sr_dev_inst *sdi)
214{
ca7d19b5 215 struct sr_usb_dev_inst *usb = sdi->conn;
a8e913c4 216
ca7d19b5 217 sr_usb_close(usb);
a8e913c4
JL
218
219 sdi->status = SR_ST_INACTIVE;
220
221 return SR_OK;
222}
223
224static int config_get(uint32_t key, GVariant **data,
ca7d19b5 225 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a8e913c4 226{
ca7d19b5
JL
227 struct sr_usb_dev_inst *usb;
228 struct dev_context *devc;
a8e913c4
JL
229 int ret;
230
a8e913c4
JL
231 (void)cg;
232
233 ret = SR_OK;
234 switch (key) {
ca7d19b5
JL
235 case SR_CONF_CONN:
236 if (!sdi || !sdi->conn)
237 return SR_ERR_ARG;
238 usb = sdi->conn;
239 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
240 break;
241 case SR_CONF_SAMPLERATE:
242 if (!sdi)
243 return SR_ERR;
244 devc = sdi->priv;
245 *data = g_variant_new_uint64(devc->dig_samplerate);
246 break;
a8e913c4
JL
247 default:
248 return SR_ERR_NA;
249 }
250
251 return ret;
252}
253
254static int config_set(uint32_t key, GVariant *data,
ca7d19b5 255 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a8e913c4 256{
ca7d19b5 257 struct dev_context *devc;
a8e913c4
JL
258 int ret;
259
a8e913c4
JL
260 (void)cg;
261
262 if (sdi->status != SR_ST_ACTIVE)
263 return SR_ERR_DEV_CLOSED;
ca7d19b5 264 devc = sdi->priv;
a8e913c4
JL
265
266 ret = SR_OK;
267 switch (key) {
ca7d19b5
JL
268 case SR_CONF_SAMPLERATE:
269 devc->dig_samplerate = g_variant_get_uint64(data);
270 break;
a8e913c4
JL
271 default:
272 ret = SR_ERR_NA;
273 }
274
275 return ret;
276}
277
278static int config_list(uint32_t key, GVariant **data,
ca7d19b5 279 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a8e913c4 280{
ca7d19b5
JL
281 GVariant *gvar;
282 GVariantBuilder gvb;
a8e913c4
JL
283 int ret;
284
285 (void)sdi;
a8e913c4
JL
286 (void)cg;
287
288 ret = SR_OK;
289 switch (key) {
ca7d19b5
JL
290 case SR_CONF_SCAN_OPTIONS:
291 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
bb0c5271 292 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
ca7d19b5
JL
293 break;
294 case SR_CONF_DEVICE_OPTIONS:
295 if (!sdi) {
296 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
bb0c5271 297 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
ca7d19b5
JL
298 } else {
299 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
bb0c5271 300 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
ca7d19b5
JL
301 }
302 break;
303 case SR_CONF_SAMPLERATE:
304 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
305 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
306 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
307 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
308 *data = g_variant_builder_end(&gvb);
309 break;
a8e913c4
JL
310 default:
311 return SR_ERR_NA;
312 }
313
314 return ret;
315}
316
ca7d19b5
JL
317static void dev_acquisition_abort(const struct sr_dev_inst *sdi)
318{
319 struct dev_context *devc = sdi->priv;
320 unsigned int i;
321
322 for (i = 0; i < devc->num_transfers; i++) {
323 if (devc->transfers[i])
324 libusb_cancel_transfer(devc->transfers[i]);
325 }
326}
327
328static int dev_acquisition_handle(int fd, int revents, void *cb_data)
329{
330 struct sr_dev_inst *sdi = cb_data;
331 struct drv_context *drvc = sdi->driver->context;
fbbafc69 332 struct timeval tv = ALL_ZERO;
ca7d19b5
JL
333
334 (void)fd;
335 (void)revents;
336
337 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
338
339 return TRUE;
340}
341
a8e913c4
JL
342static int dev_acquisition_start(const struct sr_dev_inst *sdi)
343{
ca7d19b5
JL
344 struct dev_context *devc = sdi->priv;
345 struct drv_context *drvc = sdi->driver->context;
346 struct libusb_transfer *transfer;
347 struct sr_usb_dev_inst *usb;
348 uint8_t *buf;
349 unsigned int i, ret;
350
b6189f7c 351 ret = saleae_logic_pro_init(sdi);
ca7d19b5
JL
352 if (ret != SR_OK)
353 return ret;
354
b6189f7c 355 ret = saleae_logic_pro_prepare(sdi);
ca7d19b5
JL
356 if (ret != SR_OK)
357 return ret;
358
359 usb = sdi->conn;
360
361 devc->conv_buffer = g_malloc(CONV_BUFFER_SIZE);
362
363 devc->num_transfers = BUF_COUNT;
364 devc->transfers = g_malloc0(sizeof(*devc->transfers) * BUF_COUNT);
365 for (i = 0; i < devc->num_transfers; i++) {
366 buf = g_malloc(BUF_SIZE);
367 transfer = libusb_alloc_transfer(0);
368 libusb_fill_bulk_transfer(transfer, usb->devhdl,
bb0c5271
UH
369 2 | LIBUSB_ENDPOINT_IN, buf, BUF_SIZE,
370 saleae_logic_pro_receive_data, (void *)sdi, BUF_TIMEOUT);
ca7d19b5
JL
371 if ((ret = libusb_submit_transfer(transfer)) != 0) {
372 sr_err("Failed to submit transfer: %s.",
373 libusb_error_name(ret));
374 libusb_free_transfer(transfer);
375 g_free(buf);
376 dev_acquisition_abort(sdi);
377 return SR_ERR;
378 }
379 devc->transfers[i] = transfer;
380 devc->submitted_transfers++;
381 }
382
383 usb_source_add(sdi->session, drvc->sr_ctx, BUF_TIMEOUT, dev_acquisition_handle, (void *)sdi);
384
385 std_session_send_df_header(sdi);
386
b6189f7c 387 saleae_logic_pro_start(sdi);
ca7d19b5
JL
388 if (ret != SR_OK)
389 return ret;
a8e913c4
JL
390
391 return SR_OK;
392}
393
394static int dev_acquisition_stop(struct sr_dev_inst *sdi)
395{
ca7d19b5
JL
396 struct dev_context *devc = sdi->priv;
397 struct drv_context *drvc = sdi->driver->context;
398
b6189f7c 399 saleae_logic_pro_stop(sdi);
ca7d19b5
JL
400
401 std_session_send_df_end(sdi);
402
403 usb_source_remove(sdi->session, drvc->sr_ctx);
404
405 g_free(devc->conv_buffer);
a8e913c4
JL
406
407 return SR_OK;
408}
409
b6189f7c
UH
410SR_PRIV struct sr_dev_driver saleae_logic_pro_driver_info = {
411 .name = "saleae-logic-pro",
412 .longname = "Saleae Logic Pro",
a8e913c4
JL
413 .api_version = 1,
414 .init = std_init,
415 .cleanup = std_cleanup,
416 .scan = scan,
417 .dev_list = std_dev_list,
418 .dev_clear = dev_clear,
419 .config_get = config_get,
420 .config_set = config_set,
421 .config_list = config_list,
422 .dev_open = dev_open,
423 .dev_close = dev_close,
424 .dev_acquisition_start = dev_acquisition_start,
425 .dev_acquisition_stop = dev_acquisition_stop,
426 .context = NULL,
427};
428
b6189f7c 429SR_REGISTER_DEV_DRIVER(saleae_logic_pro_driver_info);