]> sigrok.org Git - libsigrok.git/blame - hardware/fx2lafw/fx2lafw.c
fx2lafw: Small consistency fixes.
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
CommitLineData
f302a082
JH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 <stdio.h>
21#include <stdlib.h>
b1eeb67e 22#include <stdbool.h>
8b35f474 23#include <inttypes.h>
187b3582
JH
24#include <glib.h>
25#include <libusb.h>
f302a082
JH
26#include "config.h"
27#include "sigrok.h"
28#include "sigrok-internal.h"
29#include "fx2lafw.h"
30
187b3582
JH
31static struct fx2lafw_profile supported_fx2[] = {
32 /* USBee AX */
33 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
34 { 0, 0, 0, 0, 0, 0 }
35};
36
8b35f474
JH
37static int fx2lafw_capabilities[] = {
38 SR_HWCAP_LOGIC_ANALYZER,
39 SR_HWCAP_SAMPLERATE,
40
41 /* These are really implemented in the driver, not the hardware. */
42 SR_HWCAP_LIMIT_SAMPLES,
43 SR_HWCAP_CONTINUOUS,
772a0e61 44 0,
8b35f474
JH
45};
46
47static const char *fx2lafw_probe_names[] = {
48 "D0",
49 "D1",
50 "D2",
51 "D3",
52 "D4",
53 "D5",
54 "D6",
55 "D7",
772a0e61 56 NULL,
8b35f474
JH
57};
58
59static uint64_t fx2lafw_supported_samplerates[] = {
60 SR_MHZ(1),
61 SR_MHZ(2),
62 SR_MHZ(3),
63 SR_MHZ(4),
64 SR_MHZ(6),
65 SR_MHZ(8),
66 SR_MHZ(12),
67 SR_MHZ(16),
772a0e61 68 SR_MHZ(24),
8b35f474
JH
69};
70
71static struct sr_samplerates fx2lafw_samplerates = {
72 SR_MHZ(1),
73 SR_MHZ(24),
74 SR_HZ(0),
772a0e61 75 fx2lafw_supported_samplerates,
8b35f474
JH
76};
77
cac0bbaa 78static GSList *dev_insts = NULL;
187b3582
JH
79static libusb_context *usb_context = NULL;
80
610dbb70
JH
81static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id);
82
b1eeb67e
JH
83/**
84 * Check the USB configuration to determine if this is an fx2lafw device.
85 *
86 * @return true if the device's configuration profile match fx2lafw
772a0e61 87 * configuration, false otherwise.
b1eeb67e
JH
88 */
89static bool check_conf_profile(libusb_device *dev)
90{
91 struct libusb_device_descriptor des;
92 struct libusb_config_descriptor *conf_dsc = NULL;
93 const struct libusb_interface_descriptor *intf_dsc;
94 bool ret = false;
95
96 while (!ret) {
97 /* Assume it's not a Saleae Logic unless proven wrong. */
98 ret = 0;
99
100 if (libusb_get_device_descriptor(dev, &des) != 0)
101 break;
102
103 if (des.bNumConfigurations != 1)
104 /* Need exactly 1 configuration. */
105 break;
106
107 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
108 break;
109
110 if (conf_dsc->bNumInterfaces != 1)
111 /* Need exactly 1 interface. */
112 break;
113
114 if (conf_dsc->interface[0].num_altsetting != 1)
115 /* Need just one alternate setting. */
116 break;
117
118 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
119 if (intf_dsc->bNumEndpoints != 3)
120 /* Need exactly 3 end points. */
121 break;
122
123 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
124 (1 | LIBUSB_ENDPOINT_OUT))
125 /* The first endpoint should be 1 (outbound). */
126 break;
127
128 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
129 (2 | LIBUSB_ENDPOINT_IN))
130 /* The second endpoint should be 2 (inbound). */
131 break;
132
133 /* TODO: Check the debug channel... */
134
135 /* If we made it here, it must be an fx2lafw. */
136 ret = true;
137 }
138
139 if (conf_dsc)
140 libusb_free_config_descriptor(conf_dsc);
141
142 return ret;
143}
144
43125c69
JH
145static int fx2lafw_open_dev(int dev_index)
146{
147 libusb_device **devlist;
148 struct libusb_device_descriptor des;
149 struct sr_dev_inst *sdi;
772a0e61 150 struct context *ctx;
43125c69
JH
151 int err, skip, i;
152
153 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
154 return SR_ERR;
155 ctx = sdi->priv;
156
157 if (sdi->status == SR_ST_ACTIVE)
158 /* already in use */
159 return SR_ERR;
160
161 skip = 0;
162 libusb_get_device_list(usb_context, &devlist);
163 for (i = 0; devlist[i]; i++) {
164 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
165 sr_err("fx2lafw: failed to get device descriptor: %d", err);
166 continue;
167 }
168
169 if (des.idVendor != FIRMWARE_VID
170 || des.idProduct != FIRMWARE_PID)
171 continue;
172
173 if (sdi->status == SR_ST_INITIALIZING) {
174 if (skip != dev_index) {
175 /* Skip devices of this type that aren't the one we want. */
176 skip += 1;
177 continue;
178 }
179 } else if (sdi->status == SR_ST_INACTIVE) {
180 /*
181 * This device is fully enumerated, so we need to find
182 * this device by vendor, product, bus and address.
183 */
184 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
185 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
186 /* this is not the one */
187 continue;
188 }
189
190 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
191 if (ctx->usb->address == 0xff)
192 /*
193 * first time we touch this device after firmware upload,
194 * so we don't know the address yet.
195 */
196 ctx->usb->address = libusb_get_device_address(devlist[i]);
197
198 sdi->status = SR_ST_ACTIVE;
199 sr_info("fx2lafw: opened device %d on %d.%d interface %d",
200 sdi->index, ctx->usb->bus,
201 ctx->usb->address, USB_INTERFACE);
202 } else {
203 sr_err("fx2lafw: failed to open device: %d", err);
204 }
205
206 /* if we made it here, we handled the device one way or another */
207 break;
208 }
209 libusb_free_device_list(devlist, 1);
210
211 if (sdi->status != SR_ST_ACTIVE)
212 return SR_ERR;
213
214 return SR_OK;
215}
216
f1898235
JH
217static void close_dev(struct sr_dev_inst *sdi)
218{
772a0e61 219 struct context *ctx;
f1898235
JH
220
221 ctx = sdi->priv;
222
223 if (ctx->usb->devhdl == NULL)
224 return;
225
226 sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
227 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
228 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
229 libusb_close(ctx->usb->devhdl);
230 ctx->usb->devhdl = NULL;
231 sdi->status = SR_ST_INACTIVE;
232}
233
772a0e61 234static struct context *fx2lafw_device_new(void)
187b3582 235{
772a0e61 236 struct context *ctx;
187b3582 237
772a0e61
UH
238 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
239 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
187b3582
JH
240 return NULL;
241 }
242
772a0e61 243 return ctx;
187b3582 244}
f302a082
JH
245
246/*
247 * API callbacks
248 */
249
250static int hw_init(const char *deviceinfo)
251{
187b3582
JH
252 struct sr_dev_inst *sdi;
253 struct libusb_device_descriptor des;
254 struct fx2lafw_profile *fx2lafw_prof;
772a0e61 255 struct context *ctx;
187b3582
JH
256 libusb_device **devlist;
257 int err;
258 int devcnt = 0;
259 int i, j;
260
261 /* Avoid compiler warnings. */
f302a082 262 (void)deviceinfo;
187b3582
JH
263
264 if (libusb_init(&usb_context) != 0) {
265 sr_warn("Failed to initialize USB.");
266 return 0;
267 }
268
269 /* Find all fx2lafw compatible devices and upload firware to all of them. */
270 libusb_get_device_list(usb_context, &devlist);
271 for (i = 0; devlist[i]; i++) {
272
273 if ((err = libusb_get_device_descriptor(
274 devlist[i], &des)) != 0) {
275 sr_warn("failed to get device descriptor: %d", err);
276 continue;
277 }
278
279 fx2lafw_prof = NULL;
280 for (j = 0; supported_fx2[j].vid; j++) {
281 if (des.idVendor == supported_fx2[j].vid &&
282 des.idProduct == supported_fx2[j].pid) {
283 fx2lafw_prof = &supported_fx2[j];
284 }
285 }
286
287 /* Skip if the device was not found */
288 if(!fx2lafw_prof)
289 continue;
290
291 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
292 fx2lafw_prof->vendor, fx2lafw_prof->model,
293 fx2lafw_prof->model_version);
294 if(!sdi)
295 return 0;
296
90282c82
JH
297 ctx = fx2lafw_device_new();
298 ctx->profile = fx2lafw_prof;
299 sdi->priv = ctx;
be4b99e8 300 dev_insts = g_slist_append(dev_insts, sdi);
187b3582 301
b1eeb67e
JH
302 if (check_conf_profile(devlist[i])) {
303 /* Already has the firmware, so fix the new address. */
304 sr_dbg("fx2lafw: Found a fx2lafw device.");
305 sdi->status = SR_ST_INACTIVE;
306 ctx->usb = sr_usb_dev_inst_new
307 (libusb_get_bus_number(devlist[i]),
308 libusb_get_device_address(devlist[i]), NULL);
309 } else {
310 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
311 /* Remember when the firmware on this device was updated */
312 g_get_current_time(&ctx->fw_updated);
313 else
314 sr_err("fx2lafw: firmware upload failed for "
315 "device %d", devcnt);
316 ctx->usb = sr_usb_dev_inst_new
317 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
318 }
319
187b3582
JH
320 devcnt++;
321 }
322 libusb_free_device_list(devlist, 1);
323
324 return devcnt;
f302a082
JH
325}
326
772a0e61 327static int hw_dev_open(int dev_index)
f302a082 328{
43125c69
JH
329 GTimeVal cur_time;
330 struct sr_dev_inst *sdi;
772a0e61 331 struct context *ctx;
43125c69
JH
332 int timediff, err;
333
772a0e61 334 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
43125c69
JH
335 return SR_ERR;
336 ctx = sdi->priv;
337
338 /*
339 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
340 * for the FX2 to renumerate
341 */
342 err = 0;
343 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
344 sr_info("fx2lafw: waiting for device to reset");
345 /* takes at least 300ms for the FX2 to be gone from the USB bus */
346 g_usleep(300 * 1000);
347 timediff = 0;
348 while (timediff < MAX_RENUM_DELAY) {
772a0e61 349 if ((err = fx2lafw_open_dev(dev_index)) == SR_OK)
43125c69
JH
350 break;
351 g_usleep(100 * 1000);
352 g_get_current_time(&cur_time);
353 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
354 }
355 sr_info("fx2lafw: device came back after %d ms", timediff);
356 } else {
772a0e61 357 err = fx2lafw_open_dev(dev_index);
43125c69
JH
358 }
359
360 if (err != SR_OK) {
361 sr_err("fx2lafw: unable to open device");
362 return SR_ERR;
363 }
364 ctx = sdi->priv;
365
366 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
367 if (err != 0) {
368 sr_err("fx2lafw: Unable to claim interface: %d", err);
369 return SR_ERR;
370 }
371
f302a082
JH
372 return SR_OK;
373}
374
f1898235 375static int hw_dev_close(int dev_index)
f302a082 376{
f1898235
JH
377 struct sr_dev_inst *sdi;
378
379 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
380 sr_err("fx2lafw: %s: sdi was NULL", __func__);
381 return SR_ERR; /* TODO: SR_ERR_ARG? */
382 }
383
384 /* TODO */
385 close_dev(sdi);
386
f302a082
JH
387 return SR_OK;
388}
389
390static int hw_cleanup(void)
391{
187b3582
JH
392 GSList *l;
393 struct sr_dev_inst *sdi;
772a0e61 394 struct context *ctx;
62bc70e4 395 int ret = SR_OK;
187b3582 396
62bc70e4
JH
397 for(l = dev_insts; l; l = l->next) {
398 if (!(sdi = l->data)) {
399 /* Log error, but continue cleaning up the rest. */
400 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
401 ret = SR_ERR_BUG;
402 continue;
403 }
404 if (!(ctx = sdi->priv)) {
405 /* Log error, but continue cleaning up the rest. */
406 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
407 __func__);
408 ret = SR_ERR_BUG;
409 continue;
410 }
411 close_dev(sdi);
187b3582
JH
412 sdi = l->data;
413 sr_dev_inst_free(sdi);
414 }
415
62bc70e4
JH
416 g_slist_free(dev_insts);
417 dev_insts = NULL;
187b3582
JH
418
419 if(usb_context)
420 libusb_exit(usb_context);
421 usb_context = NULL;
422
62bc70e4 423 return ret;
f302a082
JH
424}
425
772a0e61 426static void *hw_dev_info_get(int dev_index, int dev_info_id)
f302a082 427{
8b35f474 428 struct sr_dev_inst *sdi;
772a0e61 429 struct context *ctx;
8b35f474 430
772a0e61 431 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
8b35f474 432 return NULL;
cdfdd711 433 ctx = sdi->priv;
8b35f474 434
772a0e61 435 switch (dev_info_id) {
8b35f474
JH
436 case SR_DI_INST:
437 return sdi;
438 case SR_DI_NUM_PROBES:
cdfdd711 439 return GINT_TO_POINTER(ctx->profile->num_probes);
8b35f474
JH
440 case SR_DI_PROBE_NAMES:
441 return fx2lafw_probe_names;
442 case SR_DI_SAMPLERATES:
443 return &fx2lafw_samplerates;
444 case SR_DI_TRIGGER_TYPES:
445 return TRIGGER_TYPES;
446 }
447
f302a082
JH
448 return NULL;
449}
450
772a0e61 451static int hw_dev_status_get(int dev_index)
f302a082 452{
aae2fed6 453 const struct sr_dev_inst *const sdi =
772a0e61 454 sr_dev_inst_get(dev_insts, dev_index);
aae2fed6
JH
455
456 if (!sdi)
457 return SR_ST_NOT_FOUND;
458
459 return sdi->status;
f302a082
JH
460}
461
462static int *hw_hwcap_get_all(void)
463{
8b35f474 464 return fx2lafw_capabilities;
f302a082
JH
465}
466
7cb621d4 467static int hw_dev_config_set(int dev_index, int hwcap, void *value)
f302a082 468{
7cb621d4 469 struct sr_dev_inst *sdi;
772a0e61 470 struct context *ctx;
7cb621d4
JH
471 int ret;
472
473 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
474 return SR_ERR;
475 ctx = sdi->priv;
476
477 if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
478 ctx->limit_samples = *(uint64_t *)value;
479 ret = SR_OK;
480 } else {
481 ret = SR_ERR;
482 }
483
484 return ret;
f302a082
JH
485}
486
610dbb70
JH
487static int receive_data(int fd, int revents, void *user_data)
488{
489 struct timeval tv;
490
491 /* Avoid compiler warnings. */
492 (void)fd;
493 (void)revents;
494 (void)user_data;
495
496 tv.tv_sec = tv.tv_usec = 0;
497 libusb_handle_events_timeout(usb_context, &tv);
498
499 return TRUE;
500}
501
502static void receive_transfer(struct libusb_transfer *transfer)
503{
504 /* TODO: These statics have to move to the ctx struct. */
505 static int num_samples = 0;
506 static int empty_transfer_count = 0;
507 struct sr_datafeed_packet packet;
508 struct sr_datafeed_logic logic;
772a0e61 509 struct context *ctx;
610dbb70
JH
510 int cur_buflen;
511 unsigned char *cur_buf, *new_buf;
512
513 /* hw_dev_acquisition_stop() is telling us to stop. */
514 if (transfer == NULL)
515 num_samples = -1;
516
517 /*
518 * If acquisition has already ended, just free any queued up
519 * transfer that come in.
520 */
521 if (num_samples == -1) {
522 if (transfer)
523 libusb_free_transfer(transfer);
524 return;
525 }
526
527 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
528 transfer->status, transfer->actual_length);
529
530 /* Save incoming transfer before reusing the transfer struct. */
531 cur_buf = transfer->buffer;
532 cur_buflen = transfer->actual_length;
533 ctx = transfer->user_data;
534
535 /* Fire off a new request. */
536 if (!(new_buf = g_try_malloc(4096))) {
537 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
538 return; /* TODO: SR_ERR_MALLOC */
539 }
540
541 transfer->buffer = new_buf;
542 transfer->length = 4096;
543 if (libusb_submit_transfer(transfer) != 0) {
544 /* TODO: Stop session? */
545 /* TODO: Better error message. */
546 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
547 }
548
549 if (cur_buflen == 0) {
550 empty_transfer_count++;
551 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
552 /*
553 * The FX2 gave up. End the acquisition, the frontend
554 * will work out that the samplecount is short.
555 */
556 hw_dev_acquisition_stop(-1, ctx->session_data);
557 }
558 return;
559 } else {
560 empty_transfer_count = 0;
561 }
562
563 /* Send the incoming transfer to the session bus. */
564 packet.type = SR_DF_LOGIC;
565 packet.payload = &logic;
566 logic.length = cur_buflen;
567 logic.unitsize = 1;
568 logic.data = cur_buf;
569 sr_session_bus(ctx->session_data, &packet);
570 g_free(cur_buf);
571
572 num_samples += cur_buflen;
573 if (ctx->limit_samples &&
574 (unsigned int) num_samples > ctx->limit_samples) {
575 hw_dev_acquisition_stop(-1, ctx->session_data);
576 }
577}
578
f302a082
JH
579static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
580{
610dbb70
JH
581 struct sr_dev_inst *sdi;
582 struct sr_datafeed_packet *packet;
583 struct sr_datafeed_header *header;
772a0e61 584 struct context *ctx;
610dbb70
JH
585 struct libusb_transfer *transfer;
586 const struct libusb_pollfd **lupfd;
587 int size, i;
588 unsigned char *buf;
589
590 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
591 return SR_ERR;
592 ctx = sdi->priv;
593 ctx->session_data = session_data;
594
595 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
596 sr_err("fx2lafw: %s: packet malloc failed", __func__);
597 return SR_ERR_MALLOC;
598 }
599
600 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
601 sr_err("fx2lafw: %s: header malloc failed", __func__);
602 return SR_ERR_MALLOC;
603 }
604
605 /* Start with 2K transfer, subsequently increased to 4K. */
606 size = 2048;
607 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
608 if (!(buf = g_try_malloc(size))) {
609 sr_err("fx2lafw: %s: buf malloc failed", __func__);
610 return SR_ERR_MALLOC;
611 }
612 transfer = libusb_alloc_transfer(0);
613 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
614 2 | LIBUSB_ENDPOINT_IN, buf, size,
615 receive_transfer, ctx, 40);
616 if (libusb_submit_transfer(transfer) != 0) {
617 /* TODO: Free them all. */
618 libusb_free_transfer(transfer);
619 g_free(buf);
620 return SR_ERR;
621 }
622 size = 4096;
623 }
624
625 lupfd = libusb_get_pollfds(usb_context);
626 for (i = 0; lupfd[i]; i++)
627 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
628 40, receive_data, NULL);
629 free(lupfd); /* NOT g_free()! */
630
631 packet->type = SR_DF_HEADER;
632 packet->payload = header;
633 header->feed_version = 1;
634 gettimeofday(&header->starttime, NULL);
635 header->samplerate = 24000000UL;
636 header->num_logic_probes = ctx->profile->num_probes;
637 sr_session_bus(session_data, packet);
638 g_free(header);
639 g_free(packet);
640
f302a082
JH
641 return SR_OK;
642}
643
772a0e61 644/* This stops acquisition on ALL devices, ignoring dev_index. */
f302a082
JH
645static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
646{
5da93902
JH
647 struct sr_datafeed_packet packet;
648
649 /* Avoid compiler warnings. */
f302a082 650 (void)dev_index;
5da93902
JH
651
652 packet.type = SR_DF_END;
653 sr_session_bus(session_data, &packet);
654
655 receive_transfer(NULL);
656
657 /* TODO: Need to cancel and free any queued up transfers. */
658
f302a082
JH
659 return SR_OK;
660}
661
662SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info = {
663 .name = "fx2lafw",
664 .longname = "fx2lafw",
665 .api_version = 1,
666 .init = hw_init,
667 .cleanup = hw_cleanup,
668 .dev_open = hw_dev_open,
669 .dev_close = hw_dev_close,
670 .dev_info_get = hw_dev_info_get,
671 .dev_status_get = hw_dev_status_get,
672 .hwcap_get_all = hw_hwcap_get_all,
673 .dev_config_set = hw_dev_config_set,
674 .dev_acquisition_start = hw_dev_acquisition_start,
675 .dev_acquisition_stop = hw_dev_acquisition_stop,
676};