]> sigrok.org Git - libsigrok.git/blob - hardware/fx2lafw/fx2lafw.c
fx2lafw: Implemented initial support for start capture
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
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>
22 #include <inttypes.h>
23 #include <glib.h>
24 #include <libusb.h>
25 #include "config.h"
26 #include "sigrok.h"
27 #include "sigrok-internal.h"
28 #include "fx2lafw.h"
29 #include "command.h"
30
31 static 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
37 static 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,
44         0,
45 };
46
47 static const char *fx2lafw_probe_names[] = {
48         "D0",
49         "D1",
50         "D2",
51         "D3",
52         "D4",
53         "D5",
54         "D6",
55         "D7",
56         NULL,
57 };
58
59 static 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),
68         SR_MHZ(24),
69 };
70
71 static struct sr_samplerates fx2lafw_samplerates = {
72         SR_MHZ(1),
73         SR_MHZ(24),
74         SR_HZ(0),
75         fx2lafw_supported_samplerates,
76 };
77
78 static GSList *dev_insts = NULL;
79 static libusb_context *usb_context = NULL;
80
81 static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
82
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
87  *         configuration, FALSE otherwise.
88  */
89 static gboolean 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         gboolean 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
145 static int fx2lafw_open_dev(int dev_index)
146 {
147         libusb_device **devlist;
148         struct libusb_device_descriptor des;
149         struct sr_dev_inst *sdi;
150         struct context *ctx;
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 != ctx->profile->vid
170                     || des.idProduct != ctx->profile->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
217 static void close_dev(struct sr_dev_inst *sdi)
218 {
219         struct context *ctx;
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
234 static struct context *fx2lafw_device_new(void)
235 {
236         struct context *ctx;
237
238         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
239                 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
240                 return NULL;
241         }
242
243         return ctx;
244 }
245
246 /*
247  * API callbacks
248  */
249
250 static int hw_init(const char *deviceinfo)
251 {
252         struct sr_dev_inst *sdi;
253         struct libusb_device_descriptor des;
254         struct fx2lafw_profile *fx2lafw_prof;
255         struct context *ctx;
256         libusb_device **devlist;
257         int err;
258         int devcnt = 0;
259         int i, j;
260
261         /* Avoid compiler warnings. */
262         (void)deviceinfo;
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
297                 ctx = fx2lafw_device_new();
298                 ctx->profile = fx2lafw_prof;
299                 sdi->priv = ctx;
300                 dev_insts = g_slist_append(dev_insts, sdi);
301
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
320                 devcnt++;
321         }
322         libusb_free_device_list(devlist, 1);
323
324         return devcnt;
325 }
326
327 static int hw_dev_open(int dev_index)
328 {
329         GTimeVal cur_time;
330         struct sr_dev_inst *sdi;
331         struct context *ctx;
332         int timediff, err;
333
334         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
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) {
349                         if ((err = fx2lafw_open_dev(dev_index)) == SR_OK)
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 {
357                 err = fx2lafw_open_dev(dev_index);
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
372         return SR_OK;
373 }
374
375 static int hw_dev_close(int dev_index)
376 {
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_BUG;
382         }
383
384         /* TODO */
385         close_dev(sdi);
386
387         return SR_OK;
388 }
389
390 static int hw_cleanup(void)
391 {
392         GSList *l;
393         struct sr_dev_inst *sdi;
394         struct context *ctx;
395         int ret = SR_OK;
396
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);
412                 sdi = l->data;
413                 sr_dev_inst_free(sdi);
414         }
415
416         g_slist_free(dev_insts);
417         dev_insts = NULL;
418
419         if(usb_context)
420                 libusb_exit(usb_context);
421         usb_context = NULL;
422
423         return ret;
424 }
425
426 static void *hw_dev_info_get(int dev_index, int dev_info_id)
427 {
428         struct sr_dev_inst *sdi;
429         struct context *ctx;
430
431         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
432                 return NULL;
433         ctx = sdi->priv;
434
435         switch (dev_info_id) {
436         case SR_DI_INST:
437                 return sdi;
438         case SR_DI_NUM_PROBES:
439                 return GINT_TO_POINTER(ctx->profile->num_probes);
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         case SR_DI_CUR_SAMPLERATE:
447                 return &ctx->cur_samplerate;
448         }
449
450         return NULL;
451 }
452
453 static int hw_dev_status_get(int dev_index)
454 {
455         const struct sr_dev_inst *const sdi =
456                 sr_dev_inst_get(dev_insts, dev_index);
457
458         if (!sdi)
459                 return SR_ST_NOT_FOUND;
460
461         return sdi->status;
462 }
463
464 static int *hw_hwcap_get_all(void)
465 {
466         return fx2lafw_capabilities;
467 }
468
469 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
470 {
471         struct sr_dev_inst *sdi;
472         struct context *ctx;
473         int ret;
474
475         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
476                 return SR_ERR;
477         ctx = sdi->priv;
478
479         if (hwcap == SR_HWCAP_SAMPLERATE) {
480                 ctx->cur_samplerate = *(uint64_t *)value;
481                 ret = SR_OK;
482         } else if (hwcap == SR_HWCAP_PROBECONFIG) {
483                 ret = SR_OK;
484         } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
485                 ctx->limit_samples = *(uint64_t *)value;
486                 ret = SR_OK;
487         } else {
488                 ret = SR_ERR;
489         }
490
491         return ret;
492 }
493
494 static int receive_data(int fd, int revents, void *cb_data)
495 {
496         struct timeval tv;
497
498         /* Avoid compiler warnings. */
499         (void)fd;
500         (void)revents;
501         (void)cb_data;
502
503         tv.tv_sec = tv.tv_usec = 0;
504         libusb_handle_events_timeout(usb_context, &tv);
505
506         return TRUE;
507 }
508
509 static void receive_transfer(struct libusb_transfer *transfer)
510 {
511         /* TODO: These statics have to move to the ctx struct. */
512         static int num_samples = 0;
513         static int empty_transfer_count = 0;
514         struct sr_datafeed_packet packet;
515         struct sr_datafeed_logic logic;
516         struct context *ctx;
517         int cur_buflen;
518         unsigned char *cur_buf, *new_buf;
519
520         /* hw_dev_acquisition_stop() is telling us to stop. */
521         if (transfer == NULL)
522                 num_samples = -1;
523
524         /*
525          * If acquisition has already ended, just free any queued up
526          * transfer that come in.
527          */
528         if (num_samples == -1) {
529                 if (transfer)
530                         libusb_free_transfer(transfer);
531                 return;
532         }
533
534         sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
535                 transfer->status, transfer->actual_length);
536
537         /* Save incoming transfer before reusing the transfer struct. */
538         cur_buf = transfer->buffer;
539         cur_buflen = transfer->actual_length;
540         ctx = transfer->user_data;
541
542         /* Fire off a new request. */
543         if (!(new_buf = g_try_malloc(4096))) {
544                 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
545                 return; /* TODO: SR_ERR_MALLOC */
546         }
547
548         transfer->buffer = new_buf;
549         transfer->length = 4096;
550         if (libusb_submit_transfer(transfer) != 0) {
551                 /* TODO: Stop session? */
552                 /* TODO: Better error message. */
553                 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
554         }
555
556         if (cur_buflen == 0) {
557                 empty_transfer_count++;
558                 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
559                         /*
560                          * The FX2 gave up. End the acquisition, the frontend
561                          * will work out that the samplecount is short.
562                          */
563                         hw_dev_acquisition_stop(-1, ctx->session_dev_id);
564                 }
565                 return;
566         } else {
567                 empty_transfer_count = 0;
568         }
569
570         /* Send the incoming transfer to the session bus. */
571         packet.type = SR_DF_LOGIC;
572         packet.payload = &logic;
573         logic.length = cur_buflen;
574         logic.unitsize = 1;
575         logic.data = cur_buf;
576         sr_session_send(ctx->session_dev_id, &packet);
577         g_free(cur_buf);
578
579         num_samples += cur_buflen;
580         if (ctx->limit_samples &&
581                 (unsigned int) num_samples > ctx->limit_samples) {
582                 hw_dev_acquisition_stop(-1, ctx->session_dev_id);
583         }
584 }
585
586 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
587 {
588         struct sr_dev_inst *sdi;
589         struct sr_datafeed_packet *packet;
590         struct sr_datafeed_header *header;
591         struct context *ctx;
592         struct libusb_transfer *transfer;
593         const struct libusb_pollfd **lupfd;
594         int err, size, i;
595         unsigned char *buf;
596
597         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
598                 return SR_ERR;
599         ctx = sdi->priv;
600         ctx->session_dev_id = cb_data;
601
602         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
603                 sr_err("fx2lafw: %s: packet malloc failed", __func__);
604                 return SR_ERR_MALLOC;
605         }
606
607         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
608                 sr_err("fx2lafw: %s: header malloc failed", __func__);
609                 return SR_ERR_MALLOC;
610         }
611
612         if ((err = command_start_acquisition (ctx->usb->devhdl)) != SR_OK) {
613                 return err;
614         }
615
616         /* Start with 2K transfer, subsequently increased to 4K. */
617         size = 2048;
618         for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
619                 if (!(buf = g_try_malloc(size))) {
620                         sr_err("fx2lafw: %s: buf malloc failed", __func__);
621                         return SR_ERR_MALLOC;
622                 }
623                 transfer = libusb_alloc_transfer(0);
624                 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
625                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
626                                 receive_transfer, ctx, 40);
627                 if (libusb_submit_transfer(transfer) != 0) {
628                         /* TODO: Free them all. */
629                         libusb_free_transfer(transfer);
630                         g_free(buf);
631                         return SR_ERR;
632                 }
633                 size = 4096;
634         }
635
636         lupfd = libusb_get_pollfds(usb_context);
637         for (i = 0; lupfd[i]; i++)
638                 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
639                               40, receive_data, NULL);
640         free(lupfd); /* NOT g_free()! */
641
642         packet->type = SR_DF_HEADER;
643         packet->payload = header;
644         header->feed_version = 1;
645         gettimeofday(&header->starttime, NULL);
646         header->samplerate = ctx->cur_samplerate;
647         header->num_logic_probes = ctx->profile->num_probes;
648         sr_session_send(cb_data, packet);
649         g_free(header);
650         g_free(packet);
651
652         return SR_OK;
653 }
654
655 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
656 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
657 {
658         struct sr_datafeed_packet packet;
659
660         /* Avoid compiler warnings. */
661         (void)dev_index;
662
663         packet.type = SR_DF_END;
664         sr_session_send(cb_data, &packet);
665
666         receive_transfer(NULL);
667
668         /* TODO: Need to cancel and free any queued up transfers. */
669
670         return SR_OK;
671 }
672
673 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
674         .name = "fx2lafw",
675         .longname = "fx2lafw",
676         .api_version = 1,
677         .init = hw_init,
678         .cleanup = hw_cleanup,
679         .dev_open = hw_dev_open,
680         .dev_close = hw_dev_close,
681         .dev_info_get = hw_dev_info_get,
682         .dev_status_get = hw_dev_status_get,
683         .hwcap_get_all = hw_hwcap_get_all,
684         .dev_config_set = hw_dev_config_set,
685         .dev_acquisition_start = hw_dev_acquisition_start,
686         .dev_acquisition_stop = hw_dev_acquisition_stop,
687 };