]> sigrok.org Git - libsigrok.git/blame - hardware/saleae-logic16/api.c
saleae-logic16: Initialize the FPGA.
[libsigrok.git] / hardware / saleae-logic16 / api.c
CommitLineData
c463dcf0
MC
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
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
f6a21fa5
MC
20#include <glib.h>
21#include <libusb.h>
22#include <stdlib.h>
23#include <string.h>
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
c463dcf0
MC
26#include "protocol.h"
27
f6a21fa5
MC
28#define LOGIC16_VID 0x21a9
29#define LOGIC16_PID 0x1001
30#define NUM_PROBES 16
31
5eea4305
MC
32#define USB_INTERFACE 0
33#define USB_CONFIGURATION 1
34#define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw"
35
36#define MAX_RENUM_DELAY_MS 3000
37
38
c463dcf0
MC
39SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
40static struct sr_dev_driver *di = &saleae_logic16_driver_info;
41
f6a21fa5
MC
42static const char *probe_names[NUM_PROBES + 1] = {
43 "0", "1", "2", "3", "4", "5", "6", "7", "8",
44 "9", "10", "11", "12", "13", "14", "15",
45 NULL,
46};
c463dcf0
MC
47
48static int init(struct sr_context *sr_ctx)
49{
50 return std_init(sr_ctx, di, LOG_PREFIX);
51}
52
5eea4305
MC
53static gboolean check_conf_profile(libusb_device *dev)
54{
55 struct libusb_device_descriptor des;
56 struct libusb_device_handle *hdl;
57 gboolean ret;
58 unsigned char strdesc[64];
59
60 hdl = NULL;
61 ret = FALSE;
62 while (!ret) {
63 /* Assume the FW has not been loaded, unless proven wrong. */
64 if (libusb_get_device_descriptor(dev, &des) != 0)
65 break;
66
67 if (libusb_open(dev, &hdl) != 0)
68 break;
69
70 if (libusb_get_string_descriptor_ascii(hdl,
71 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
72 break;
73 if (strcmp((const char *)strdesc, "Saleae LLC"))
74 break;
75
76 if (libusb_get_string_descriptor_ascii(hdl,
77 des.iProduct, strdesc, sizeof(strdesc)) < 0)
78 break;
79 if (strcmp((const char *)strdesc, "Logic S/16"))
80 break;
81
82 /* If we made it here, it must be a configured Logic16. */
83 ret = TRUE;
84 }
85 if (hdl)
86 libusb_close(hdl);
87
88 return ret;
89}
90
c463dcf0
MC
91static GSList *scan(GSList *options)
92{
93 struct drv_context *drvc;
f6a21fa5
MC
94 struct dev_context *devc;
95 struct sr_dev_inst *sdi;
5eea4305 96 struct sr_usb_dev_inst *usb;
f6a21fa5 97 struct sr_probe *probe;
5eea4305
MC
98 struct sr_config *src;
99 GSList *l, *devices, *conn_devices;
f6a21fa5
MC
100 struct libusb_device_descriptor des;
101 libusb_device **devlist;
5eea4305
MC
102 int devcnt, ret, i, j;
103 const char *conn;
c463dcf0 104
c463dcf0 105 drvc = di->priv;
c463dcf0 106
5eea4305
MC
107 conn = NULL;
108 for (l = options; l; l = l->next) {
109 src = l->data;
110 switch (src->key) {
111 case SR_CONF_CONN:
112 conn = g_variant_get_string(src->data, NULL);
113 break;
114 }
115 }
116 if (conn)
117 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
118 else
119 conn_devices = NULL;
120
121 /* Find all Logic16 devices and upload firmware to them. */
f6a21fa5
MC
122 devices = NULL;
123 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
124 for (i = 0; devlist[i]; i++) {
5eea4305
MC
125 if (conn) {
126 usb = NULL;
127 for (l = conn_devices; l; l = l->next) {
128 usb = l->data;
129 if (usb->bus == libusb_get_bus_number(devlist[i])
130 && usb->address == libusb_get_device_address(devlist[i]))
131 break;
132 }
133 if (!l)
134 /* This device matched none of the ones that
135 * matched the conn specification. */
136 continue;
137 }
138
139 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
140 sr_warn("Failed to get device descriptor: %s.",
141 libusb_error_name(ret));
f6a21fa5
MC
142 continue;
143 }
144
145 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
146 continue;
147
148 devcnt = g_slist_length(drvc->instances);
5eea4305
MC
149 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
150 "Saleae", "Logic16", NULL);
151 if (!sdi)
f6a21fa5
MC
152 return NULL;
153 sdi->driver = di;
154
f6a21fa5
MC
155 for (j = 0; probe_names[j]; j++) {
156 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
157 probe_names[j])))
158 return NULL;
159 sdi->probes = g_slist_append(sdi->probes, probe);
160 }
161
5eea4305 162 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
f6a21fa5 163 return NULL;
5eea4305 164 sdi->priv = devc;
f6a21fa5
MC
165 drvc->instances = g_slist_append(drvc->instances, sdi);
166 devices = g_slist_append(devices, sdi);
5eea4305
MC
167
168 if (check_conf_profile(devlist[i])) {
169 /* Already has the firmware, so fix the new address. */
170 sr_dbg("Found a Logic16 device.");
171 sdi->status = SR_ST_INACTIVE;
172 sdi->inst_type = SR_INST_USB;
173 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
174 libusb_get_device_address(devlist[i]), NULL);
175 } else {
176 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
177 FX2_FIRMWARE) == SR_OK)
178 /* Store when this device's FW was updated. */
179 devc->fw_updated = g_get_monotonic_time();
180 else
181 sr_err("Firmware upload failed for "
182 "device %d.", devcnt);
183 sdi->inst_type = SR_INST_USB;
184 sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
185 0xff, NULL);
186 }
f6a21fa5
MC
187 }
188 libusb_free_device_list(devlist, 1);
5eea4305 189 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
c463dcf0
MC
190
191 return devices;
192}
193
194static GSList *dev_list(void)
195{
196 struct drv_context *drvc;
197
198 drvc = di->priv;
199
200 return drvc->instances;
201}
202
203static int dev_clear(void)
204{
205 return std_dev_clear(di, NULL);
206}
207
5eea4305
MC
208static int logic16_dev_open(struct sr_dev_inst *sdi)
209{
210 libusb_device **devlist;
211 struct sr_usb_dev_inst *usb;
212 struct libusb_device_descriptor des;
213 struct dev_context *devc;
214 struct drv_context *drvc;
215 int ret, skip, i, device_count;
216
217 drvc = di->priv;
218 devc = sdi->priv;
219 usb = sdi->conn;
220
221 if (sdi->status == SR_ST_ACTIVE)
222 /* Device is already in use. */
223 return SR_ERR;
224
225 skip = 0;
226 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
227 if (device_count < 0) {
228 sr_err("Failed to get device list: %s.",
229 libusb_error_name(device_count));
230 return SR_ERR;
231 }
232
233 for (i = 0; i < device_count; i++) {
234 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
235 sr_err("Failed to get device descriptor: %s.",
236 libusb_error_name(ret));
237 continue;
238 }
239
240 if (des.idVendor != LOGIC16_VID
241 || des.idProduct != LOGIC16_PID)
242 continue;
243
244 if (sdi->status == SR_ST_INITIALIZING) {
245 if (skip != sdi->index) {
246 /* Skip devices of this type that aren't the one we want. */
247 skip += 1;
248 continue;
249 }
250 } else if (sdi->status == SR_ST_INACTIVE) {
251 /*
252 * This device is fully enumerated, so we need to find
253 * this device by vendor, product, bus and address.
254 */
255 if (libusb_get_bus_number(devlist[i]) != usb->bus
256 || libusb_get_device_address(devlist[i]) != usb->address)
257 /* This is not the one. */
258 continue;
259 }
260
261 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
262 if (usb->address == 0xff)
263 /*
264 * First time we touch this device after FW
265 * upload, so we don't know the address yet.
266 */
267 usb->address = libusb_get_device_address(devlist[i]);
268 } else {
269 sr_err("Failed to open device: %s.",
270 libusb_error_name(ret));
271 break;
272 }
273
15abcf0f
MC
274 if ((ret = saleae_logic16_init_device(sdi)) != SR_OK) {
275 sr_err("Failed to init device.");
276 break;
277 }
278
5eea4305
MC
279 sdi->status = SR_ST_ACTIVE;
280 sr_info("Opened device %d on %d.%d, "
281 "interface %d.",
282 sdi->index, usb->bus, usb->address,
283 USB_INTERFACE);
284
285 break;
286 }
287 libusb_free_device_list(devlist, 1);
288
289 if (sdi->status != SR_ST_ACTIVE)
290 return SR_ERR;
291
292 return SR_OK;
293}
294
c463dcf0
MC
295static int dev_open(struct sr_dev_inst *sdi)
296{
5eea4305
MC
297 struct sr_usb_dev_inst *usb;
298 struct dev_context *devc;
299 int ret;
300 int64_t timediff_us, timediff_ms;
301
302 devc = sdi->priv;
303 usb = sdi->conn;
304
305 /*
306 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
307 * milliseconds for the FX2 to renumerate.
308 */
309 ret = SR_ERR;
310 if (devc->fw_updated > 0) {
311 sr_info("Waiting for device to reset.");
312 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
313 g_usleep(300 * 1000);
314 timediff_ms = 0;
315 while (timediff_ms < MAX_RENUM_DELAY_MS) {
316 if ((ret = logic16_dev_open(sdi)) == SR_OK)
317 break;
318 g_usleep(100 * 1000);
319
320 timediff_us = g_get_monotonic_time() - devc->fw_updated;
321 timediff_ms = timediff_us / 1000;
322 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
323 }
324 if (ret != SR_OK) {
325 sr_err("Device failed to renumerate.");
326 return SR_ERR;
327 }
328 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
329 } else {
330 sr_info("Firmware upload was not needed.");
331 ret = logic16_dev_open(sdi);
332 }
333
334 if (ret != SR_OK) {
335 sr_err("Unable to open device.");
336 return SR_ERR;
337 }
338
339 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
340 if (ret != 0) {
341 switch(ret) {
342 case LIBUSB_ERROR_BUSY:
343 sr_err("Unable to claim USB interface. Another "
344 "program or driver has already claimed it.");
345 break;
346 case LIBUSB_ERROR_NO_DEVICE:
347 sr_err("Device has been disconnected.");
348 break;
349 default:
350 sr_err("Unable to claim interface: %s.",
351 libusb_error_name(ret));
352 break;
353 }
c463dcf0 354
5eea4305
MC
355 return SR_ERR;
356 }
c463dcf0 357
5eea4305
MC
358 if (devc->cur_samplerate == 0) {
359 /* Samplerate hasn't been set; default to the slowest one. */
360 devc->cur_samplerate = 500000;
361 }
c463dcf0
MC
362
363 return SR_OK;
364}
365
366static int dev_close(struct sr_dev_inst *sdi)
367{
5eea4305 368 struct sr_usb_dev_inst *usb;
c463dcf0 369
5eea4305
MC
370 usb = sdi->conn;
371 if (usb->devhdl == NULL)
372 return SR_ERR;
c463dcf0 373
5eea4305
MC
374 sr_info("Closing device %d on %d.%d interface %d.",
375 sdi->index, usb->bus, usb->address, USB_INTERFACE);
376 libusb_release_interface(usb->devhdl, USB_INTERFACE);
377 libusb_close(usb->devhdl);
378 usb->devhdl = NULL;
c463dcf0
MC
379 sdi->status = SR_ST_INACTIVE;
380
381 return SR_OK;
382}
383
384static int cleanup(void)
385{
f6a21fa5
MC
386 int ret;
387 struct drv_context *drvc;
c463dcf0 388
f6a21fa5
MC
389 if (!(drvc = di->priv))
390 /* Can get called on an unused driver, doesn't matter. */
391 return SR_OK;
c463dcf0 392
f6a21fa5
MC
393 ret = dev_clear();
394 g_free(drvc);
395 di->priv = NULL;
396
397 return ret;
c463dcf0
MC
398}
399
400static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
401{
402 int ret;
403
404 (void)sdi;
405 (void)data;
406
407 ret = SR_OK;
408 switch (key) {
409 /* TODO */
410 default:
411 return SR_ERR_NA;
412 }
413
414 return ret;
415}
416
417static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
418{
419 int ret;
420
421 (void)data;
422
423 if (sdi->status != SR_ST_ACTIVE)
424 return SR_ERR_DEV_CLOSED;
425
426 ret = SR_OK;
427 switch (key) {
428 /* TODO */
429 default:
430 ret = SR_ERR_NA;
431 }
432
433 return ret;
434}
435
436static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
437{
438 int ret;
439
440 (void)sdi;
441 (void)data;
442
443 ret = SR_OK;
444 switch (key) {
445 /* TODO */
446 default:
447 return SR_ERR_NA;
448 }
449
450 return ret;
451}
452
453static int dev_acquisition_start(const struct sr_dev_inst *sdi,
454 void *cb_data)
455{
456 (void)sdi;
457 (void)cb_data;
458
459 if (sdi->status != SR_ST_ACTIVE)
460 return SR_ERR_DEV_CLOSED;
461
462 /* TODO: configure hardware, reset acquisition state, set up
463 * callbacks and send header packet. */
464
465 return SR_OK;
466}
467
468static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
469{
470 (void)cb_data;
471
472 if (sdi->status != SR_ST_ACTIVE)
473 return SR_ERR_DEV_CLOSED;
474
475 /* TODO: stop acquisition. */
476
477 return SR_OK;
478}
479
480SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
481 .name = "saleae-logic16",
482 .longname = "Saleae Logic16",
483 .api_version = 1,
484 .init = init,
485 .cleanup = cleanup,
486 .scan = scan,
487 .dev_list = dev_list,
488 .dev_clear = dev_clear,
489 .config_get = config_get,
490 .config_set = config_set,
491 .config_list = config_list,
492 .dev_open = dev_open,
493 .dev_close = dev_close,
494 .dev_acquisition_start = dev_acquisition_start,
495 .dev_acquisition_stop = dev_acquisition_stop,
496 .priv = NULL,
497};