libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
fx2lafw.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
00005  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
00006  *
00007  * This program is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <inttypes.h>
00025 #include <libusb.h>
00026 #include "config.h"
00027 #include "sigrok.h"
00028 #include "sigrok-internal.h"
00029 #include "fx2lafw.h"
00030 #include "command.h"
00031 
00032 static const struct fx2lafw_profile supported_fx2[] = {
00033         /*
00034          * CWAV USBee AX
00035          * EE Electronics ESLA201A
00036          * ARMFLY AX-Pro
00037          */
00038         { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
00039                 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
00040 
00041         /*
00042          * CWAV USBee SX
00043          */
00044         { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
00045                 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
00046 
00047         /*
00048          * Saleae Logic
00049          * EE Electronics ESLA100
00050          * Robomotic MiniLogic
00051          * Robomotic BugLogic 3
00052          */
00053         { 0x0925, 0x3881, "Saleae", "Logic", NULL,
00054                 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
00055 
00056         /*
00057          * Default Cypress FX2 without EEPROM, e.g.:
00058          * Lcsoft Mini Board
00059          * Braintechnology USB Interface V2.x
00060          */
00061         { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
00062                 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", 8 },
00063 
00064         /*
00065          * Braintechnology USB-LPS
00066          */
00067         { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
00068                 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw", 8 },
00069 
00070         { 0, 0, 0, 0, 0, 0, 0 }
00071 };
00072 
00073 static int hwcaps[] = {
00074         SR_HWCAP_LOGIC_ANALYZER,
00075         SR_HWCAP_SAMPLERATE,
00076 
00077         /* These are really implemented in the driver, not the hardware. */
00078         SR_HWCAP_LIMIT_SAMPLES,
00079         SR_HWCAP_CONTINUOUS,
00080         0,
00081 };
00082 
00083 /*
00084  * TODO: Different probe_names[] for each supported device.
00085  */
00086 static const char *probe_names[] = {
00087         "0",
00088         "1",
00089         "2",
00090         "3",
00091         "4",
00092         "5",
00093         "6",
00094         "7",
00095         NULL,
00096 };
00097 
00098 static uint64_t supported_samplerates[] = {
00099         SR_KHZ(20),
00100         SR_KHZ(25),
00101         SR_KHZ(50),
00102         SR_KHZ(100),
00103         SR_KHZ(200),
00104         SR_KHZ(250),
00105         SR_KHZ(500),
00106         SR_MHZ(1),
00107         SR_MHZ(2),
00108         SR_MHZ(3),
00109         SR_MHZ(4),
00110         SR_MHZ(6),
00111         SR_MHZ(8),
00112         SR_MHZ(12),
00113         SR_MHZ(16),
00114         SR_MHZ(24),
00115         0,
00116 };
00117 
00118 static struct sr_samplerates samplerates = {
00119         0,
00120         0,
00121         0,
00122         supported_samplerates,
00123 };
00124 
00125 static GSList *dev_insts = NULL;
00126 static libusb_context *usb_context = NULL;
00127 
00128 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
00129 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
00130 
00131 /**
00132  * Check the USB configuration to determine if this is an fx2lafw device.
00133  *
00134  * @return TRUE if the device's configuration profile match fx2lafw
00135  *         configuration, FALSE otherwise.
00136  */
00137 static gboolean check_conf_profile(libusb_device *dev)
00138 {
00139         struct libusb_device_descriptor des;
00140         struct libusb_device_handle *hdl;
00141         gboolean ret;
00142         unsigned char strdesc[64];
00143 
00144         hdl = NULL;
00145         ret = FALSE;
00146         while (!ret) {
00147                 /* Assume the FW has not been loaded, unless proven wrong. */
00148                 if (libusb_get_device_descriptor(dev, &des) != 0)
00149                         break;
00150 
00151                 if (libusb_open(dev, &hdl) != 0)
00152                         break;
00153 
00154                 if (libusb_get_string_descriptor_ascii(hdl,
00155                                 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
00156                         break;
00157                 if (strncmp((const char *)strdesc, "sigrok", 6))
00158                         break;
00159 
00160                 if (libusb_get_string_descriptor_ascii(hdl,
00161                                 des.iProduct, strdesc, sizeof(strdesc)) < 0)
00162                         break;
00163                 if (strncmp((const char *)strdesc, "fx2lafw", 7))
00164                         break;
00165 
00166                 /* If we made it here, it must be an fx2lafw. */
00167                 ret = TRUE;
00168         }
00169         if (hdl)
00170                 libusb_close(hdl);
00171 
00172         return ret;
00173 }
00174 
00175 static int fx2lafw_dev_open(int dev_index)
00176 {
00177         libusb_device **devlist;
00178         struct libusb_device_descriptor des;
00179         struct sr_dev_inst *sdi;
00180         struct context *ctx;
00181         struct version_info vi;
00182         int ret, skip, i;
00183         uint8_t revid;
00184 
00185         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00186                 return SR_ERR;
00187         ctx = sdi->priv;
00188 
00189         if (sdi->status == SR_ST_ACTIVE)
00190                 /* already in use */
00191                 return SR_ERR;
00192 
00193         skip = 0;
00194         const int device_count = libusb_get_device_list(usb_context, &devlist);
00195         if (device_count < 0) {
00196                 sr_err("fx2lafw: Failed to retrieve device list (%d)",
00197                         device_count);
00198                 return SR_ERR;
00199         }
00200 
00201         for (i = 0; i < device_count; i++) {
00202                 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
00203                         sr_err("fx2lafw: Failed to get device descriptor: %d.",
00204                                ret);
00205                         continue;
00206                 }
00207 
00208                 if (des.idVendor != ctx->profile->vid
00209                     || des.idProduct != ctx->profile->pid)
00210                         continue;
00211 
00212                 if (sdi->status == SR_ST_INITIALIZING) {
00213                         if (skip != dev_index) {
00214                                 /* Skip devices of this type that aren't the one we want. */
00215                                 skip += 1;
00216                                 continue;
00217                         }
00218                 } else if (sdi->status == SR_ST_INACTIVE) {
00219                         /*
00220                          * This device is fully enumerated, so we need to find
00221                          * this device by vendor, product, bus and address.
00222                          */
00223                         if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
00224                                 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
00225                                 /* this is not the one */
00226                                 continue;
00227                 }
00228 
00229                 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
00230                         if (ctx->usb->address == 0xff)
00231                                 /*
00232                                  * first time we touch this device after firmware upload,
00233                                  * so we don't know the address yet.
00234                                  */
00235                                 ctx->usb->address = libusb_get_device_address(devlist[i]);
00236                 } else {
00237                         sr_err("fx2lafw: Failed to open device: %d.", ret);
00238                         break;
00239                 }
00240 
00241                 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
00242                 if (ret != SR_OK) {
00243                         sr_err("fx2lafw: Failed to retrieve "
00244                                "firmware version information.");
00245                         break;
00246                 }
00247 
00248                 ret = command_get_revid_version(ctx->usb->devhdl, &revid);
00249                 if (ret != SR_OK) {
00250                         sr_err("fx2lafw: Failed to retrieve REVID.");
00251                         break;
00252                 }
00253 
00254                 /*
00255                  * Changes in major version mean incompatible/API changes, so
00256                  * bail out if we encounter an incompatible version.
00257                  * Different minor versions are OK, they should be compatible.
00258                  */
00259                 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
00260                         sr_err("fx2lafw: Expected firmware version %d.x, "
00261                                "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
00262                                vi.major, vi.minor);
00263                         break;
00264                 }
00265 
00266                 sdi->status = SR_ST_ACTIVE;
00267                 sr_info("fx2lafw: Opened device %d on %d.%d "
00268                         "interface %d, firmware %d.%d, REVID %d.",
00269                         sdi->index, ctx->usb->bus, ctx->usb->address,
00270                         USB_INTERFACE, vi.major, vi.minor, revid);
00271 
00272                 break;
00273         }
00274         libusb_free_device_list(devlist, 1);
00275 
00276         if (sdi->status != SR_ST_ACTIVE)
00277                 return SR_ERR;
00278 
00279         return SR_OK;
00280 }
00281 
00282 static void close_dev(struct sr_dev_inst *sdi)
00283 {
00284         struct context *ctx;
00285 
00286         ctx = sdi->priv;
00287 
00288         if (ctx->usb->devhdl == NULL)
00289                 return;
00290 
00291         sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
00292                 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
00293         libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
00294         libusb_close(ctx->usb->devhdl);
00295         ctx->usb->devhdl = NULL;
00296         sdi->status = SR_ST_INACTIVE;
00297 }
00298 
00299 static int configure_probes(struct context *ctx, GSList *probes)
00300 {
00301         struct sr_probe *probe;
00302         GSList *l;
00303         int probe_bit, stage, i;
00304         char *tc;
00305 
00306         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
00307                 ctx->trigger_mask[i] = 0;
00308                 ctx->trigger_value[i] = 0;
00309         }
00310 
00311         stage = -1;
00312         for (l = probes; l; l = l->next) {
00313                 probe = (struct sr_probe *)l->data;
00314                 if (probe->enabled == FALSE)
00315                         continue;
00316                 probe_bit = 1 << (probe->index - 1);
00317                 if (!(probe->trigger))
00318                         continue;
00319 
00320                 stage = 0;
00321                 for (tc = probe->trigger; *tc; tc++) {
00322                         ctx->trigger_mask[stage] |= probe_bit;
00323                         if (*tc == '1')
00324                                 ctx->trigger_value[stage] |= probe_bit;
00325                         stage++;
00326                         if (stage > NUM_TRIGGER_STAGES)
00327                                 return SR_ERR;
00328                 }
00329         }
00330 
00331         if (stage == -1)
00332                 /*
00333                  * We didn't configure any triggers, make sure acquisition
00334                  * doesn't wait for any.
00335                  */
00336                 ctx->trigger_stage = TRIGGER_FIRED;
00337         else
00338                 ctx->trigger_stage = 0;
00339 
00340         return SR_OK;
00341 }
00342 
00343 static struct context *fx2lafw_dev_new(void)
00344 {
00345         struct context *ctx;
00346 
00347         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
00348                 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
00349                 return NULL;
00350         }
00351 
00352         ctx->trigger_stage = TRIGGER_FIRED;
00353 
00354         return ctx;
00355 }
00356 
00357 /*
00358  * API callbacks
00359  */
00360 
00361 static int hw_init(const char *devinfo)
00362 {
00363         struct sr_dev_inst *sdi;
00364         struct libusb_device_descriptor des;
00365         const struct fx2lafw_profile *prof;
00366         struct context *ctx;
00367         libusb_device **devlist;
00368         int ret;
00369         int devcnt = 0;
00370         int i, j;
00371 
00372         /* Avoid compiler warnings. */
00373         (void)devinfo;
00374 
00375         if (libusb_init(&usb_context) != 0) {
00376                 sr_warn("fx2lafw: Failed to initialize libusb.");
00377                 return 0;
00378         }
00379 
00380         /* Find all fx2lafw compatible devices and upload firmware to them. */
00381         libusb_get_device_list(usb_context, &devlist);
00382         for (i = 0; devlist[i]; i++) {
00383 
00384                 if ((ret = libusb_get_device_descriptor(
00385                      devlist[i], &des)) != 0) {
00386                         sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
00387                         continue;
00388                 }
00389 
00390                 prof = NULL;
00391                 for (j = 0; supported_fx2[j].vid; j++) {
00392                         if (des.idVendor == supported_fx2[j].vid &&
00393                                 des.idProduct == supported_fx2[j].pid) {
00394                                 prof = &supported_fx2[j];
00395                         }
00396                 }
00397 
00398                 /* Skip if the device was not found */
00399                 if (!prof)
00400                         continue;
00401 
00402                 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
00403                         prof->vendor, prof->model, prof->model_version);
00404                 if (!sdi)
00405                         return 0;
00406 
00407                 ctx = fx2lafw_dev_new();
00408                 ctx->profile = prof;
00409                 sdi->priv = ctx;
00410                 dev_insts = g_slist_append(dev_insts, sdi);
00411 
00412                 if (check_conf_profile(devlist[i])) {
00413                         /* Already has the firmware, so fix the new address. */
00414                         sr_dbg("fx2lafw: Found an fx2lafw device.");
00415                         sdi->status = SR_ST_INACTIVE;
00416                         ctx->usb = sr_usb_dev_inst_new
00417                             (libusb_get_bus_number(devlist[i]),
00418                              libusb_get_device_address(devlist[i]), NULL);
00419                 } else {
00420                         if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
00421                                 prof->firmware) == SR_OK)
00422                                 /* Remember when the firmware on this device was updated */
00423                                 ctx->fw_updated = g_get_monotonic_time();
00424                         else
00425                                 sr_err("fx2lafw: Firmware upload failed for "
00426                                        "device %d.", devcnt);
00427                         ctx->usb = sr_usb_dev_inst_new
00428                                 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
00429                 }
00430 
00431                 devcnt++;
00432         }
00433         libusb_free_device_list(devlist, 1);
00434 
00435         return devcnt;
00436 }
00437 
00438 static int hw_dev_open(int dev_index)
00439 {
00440         struct sr_dev_inst *sdi;
00441         struct context *ctx;
00442         int ret;
00443         int64_t timediff_us, timediff_ms;
00444 
00445         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00446                 return SR_ERR;
00447         ctx = sdi->priv;
00448 
00449         /*
00450          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
00451          * milliseconds for the FX2 to renumerate.
00452          */
00453         ret = 0;
00454 
00455         if (ctx->fw_updated > 0) {
00456                 sr_info("fx2lafw: Waiting for device to reset.");
00457                 /* takes at least 300ms for the FX2 to be gone from the USB bus */
00458                 g_usleep(300 * 1000);
00459                 timediff_ms = 0;
00460                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
00461                         if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
00462                                 break;
00463                         g_usleep(100 * 1000);
00464 
00465                         timediff_us = g_get_monotonic_time() - ctx->fw_updated;
00466                         timediff_ms = timediff_us / G_USEC_PER_SEC;
00467                         sr_spew("fx2lafw: timediff: %" PRIi64 " us.",
00468                                 timediff_us);
00469                 }
00470                 sr_info("fx2lafw: Device came back after %d ms.", timediff_ms);
00471         } else {
00472                 ret = fx2lafw_dev_open(dev_index);
00473         }
00474 
00475         if (ret != SR_OK) {
00476                 sr_err("fx2lafw: Unable to open device.");
00477                 return SR_ERR;
00478         }
00479         ctx = sdi->priv;
00480 
00481         ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
00482         if (ret != 0) {
00483                 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
00484                 return SR_ERR;
00485         }
00486 
00487         if (ctx->cur_samplerate == 0) {
00488                 /* Samplerate hasn't been set; default to the slowest one. */
00489                 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
00490                     &supported_samplerates[0]) == SR_ERR)
00491                         return SR_ERR;
00492         }
00493 
00494         return SR_OK;
00495 }
00496 
00497 static int hw_dev_close(int dev_index)
00498 {
00499         struct sr_dev_inst *sdi;
00500 
00501         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00502                 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
00503                 return SR_ERR_BUG;
00504         }
00505 
00506         /* TODO */
00507         close_dev(sdi);
00508 
00509         return SR_OK;
00510 }
00511 
00512 static int hw_cleanup(void)
00513 {
00514         GSList *l;
00515         struct sr_dev_inst *sdi;
00516         struct context *ctx;
00517         int ret = SR_OK;
00518 
00519         for (l = dev_insts; l; l = l->next) {
00520                 if (!(sdi = l->data)) {
00521                         /* Log error, but continue cleaning up the rest. */
00522                         sr_err("fx2lafw: %s: sdi was NULL, continuing.",
00523                                __func__);
00524                         ret = SR_ERR_BUG;
00525                         continue;
00526                 }
00527                 if (!(ctx = sdi->priv)) {
00528                         /* Log error, but continue cleaning up the rest. */
00529                         sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
00530                                __func__);
00531                         ret = SR_ERR_BUG;
00532                         continue;
00533                 }
00534                 close_dev(sdi);
00535                 sdi = l->data;
00536                 sr_dev_inst_free(sdi);
00537         }
00538 
00539         g_slist_free(dev_insts);
00540         dev_insts = NULL;
00541 
00542         if (usb_context)
00543                 libusb_exit(usb_context);
00544         usb_context = NULL;
00545 
00546         return ret;
00547 }
00548 
00549 static void *hw_dev_info_get(int dev_index, int dev_info_id)
00550 {
00551         struct sr_dev_inst *sdi;
00552         struct context *ctx;
00553 
00554         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00555                 return NULL;
00556         ctx = sdi->priv;
00557 
00558         switch (dev_info_id) {
00559         case SR_DI_INST:
00560                 return sdi;
00561         case SR_DI_NUM_PROBES:
00562                 return GINT_TO_POINTER(ctx->profile->num_probes);
00563         case SR_DI_PROBE_NAMES:
00564                 return probe_names;
00565         case SR_DI_SAMPLERATES:
00566                 return &samplerates;
00567         case SR_DI_TRIGGER_TYPES:
00568                 return TRIGGER_TYPES;
00569         case SR_DI_CUR_SAMPLERATE:
00570                 return &ctx->cur_samplerate;
00571         }
00572 
00573         return NULL;
00574 }
00575 
00576 static int hw_dev_status_get(int dev_index)
00577 {
00578         const struct sr_dev_inst *const sdi =
00579                 sr_dev_inst_get(dev_insts, dev_index);
00580 
00581         if (!sdi)
00582                 return SR_ST_NOT_FOUND;
00583 
00584         return sdi->status;
00585 }
00586 
00587 static int *hw_hwcap_get_all(void)
00588 {
00589         return hwcaps;
00590 }
00591 
00592 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
00593 {
00594         struct sr_dev_inst *sdi;
00595         struct context *ctx;
00596         int ret;
00597 
00598         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00599                 return SR_ERR;
00600         ctx = sdi->priv;
00601 
00602         if (hwcap == SR_HWCAP_SAMPLERATE) {
00603                 ctx->cur_samplerate = *(uint64_t *)value;
00604                 ret = SR_OK;
00605         } else if (hwcap == SR_HWCAP_PROBECONFIG) {
00606                 ret = configure_probes(ctx, (GSList *) value);
00607         } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
00608                 ctx->limit_samples = *(uint64_t *)value;
00609                 ret = SR_OK;
00610         } else {
00611                 ret = SR_ERR;
00612         }
00613 
00614         return ret;
00615 }
00616 
00617 static int receive_data(int fd, int revents, void *cb_data)
00618 {
00619         struct timeval tv;
00620 
00621         /* Avoid compiler warnings. */
00622         (void)fd;
00623         (void)revents;
00624         (void)cb_data;
00625 
00626         tv.tv_sec = tv.tv_usec = 0;
00627         libusb_handle_events_timeout(usb_context, &tv);
00628 
00629         return TRUE;
00630 }
00631 
00632 static void abort_acquisition(struct context *ctx)
00633 {
00634         ctx->num_samples = -1;
00635 }
00636 
00637 static void finish_acquisition(struct context *ctx)
00638 {
00639         struct sr_datafeed_packet packet;
00640         int i;
00641 
00642         /* Terminate session */
00643         packet.type = SR_DF_END;
00644         sr_session_send(ctx->session_dev_id, &packet);
00645 
00646         /* Remove fds from polling */
00647         const struct libusb_pollfd **const lupfd =
00648                 libusb_get_pollfds(usb_context);
00649         for (i = 0; lupfd[i]; i++)
00650                 sr_source_remove(lupfd[i]->fd);
00651         free(lupfd); /* NOT g_free()! */
00652 }
00653 
00654 static void receive_transfer(struct libusb_transfer *transfer)
00655 {
00656         /* TODO: These statics have to move to the ctx struct. */
00657         static int empty_transfer_count = 0;
00658         struct sr_datafeed_packet packet;
00659         struct sr_datafeed_logic logic;
00660         struct context *ctx = transfer->user_data;
00661         int cur_buflen, trigger_offset, i;
00662         unsigned char *cur_buf, *new_buf;
00663 
00664         /*
00665          * If acquisition has already ended, just free any queued up
00666          * transfer that come in.
00667          */
00668         if (ctx->num_samples == -1) {
00669                 if (transfer)
00670                         libusb_free_transfer(transfer);
00671 
00672                 ctx->submitted_transfers--;
00673                 if (ctx->submitted_transfers == 0)
00674                         finish_acquisition(ctx);
00675 
00676                 return;
00677         }
00678 
00679         sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
00680                 transfer->status, transfer->actual_length);
00681 
00682         /* Save incoming transfer before reusing the transfer struct. */
00683         cur_buf = transfer->buffer;
00684         cur_buflen = transfer->actual_length;
00685 
00686         /* Fire off a new request. */
00687         if (!(new_buf = g_try_malloc(4096))) {
00688                 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
00689                 return; /* TODO: SR_ERR_MALLOC */
00690         }
00691 
00692         transfer->buffer = new_buf;
00693         transfer->length = 4096;
00694         if (libusb_submit_transfer(transfer) != 0) {
00695                 /* TODO: Stop session? */
00696                 /* TODO: Better error message. */
00697                 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
00698         }
00699 
00700         if (cur_buflen == 0) {
00701                 empty_transfer_count++;
00702                 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
00703                         /*
00704                          * The FX2 gave up. End the acquisition, the frontend
00705                          * will work out that the samplecount is short.
00706                          */
00707                         abort_acquisition(ctx);
00708                 }
00709                 return;
00710         } else {
00711                 empty_transfer_count = 0;
00712         }
00713 
00714         trigger_offset = 0;
00715         if (ctx->trigger_stage >= 0) {
00716                 for (i = 0; i < cur_buflen; i++) {
00717 
00718                         if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
00719                                 /* Match on this trigger stage. */
00720                                 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
00721                                 ctx->trigger_stage++;
00722 
00723                                 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
00724                                         /* Match on all trigger stages, we're done. */
00725                                         trigger_offset = i + 1;
00726 
00727                                         /*
00728                                          * TODO: Send pre-trigger buffer to session bus.
00729                                          * Tell the frontend we hit the trigger here.
00730                                          */
00731                                         packet.type = SR_DF_TRIGGER;
00732                                         packet.payload = NULL;
00733                                         sr_session_send(ctx->session_dev_id, &packet);
00734 
00735                                         /*
00736                                          * Send the samples that triggered it, since we're
00737                                          * skipping past them.
00738                                          */
00739                                         packet.type = SR_DF_LOGIC;
00740                                         packet.payload = &logic;
00741                                         logic.length = ctx->trigger_stage;
00742                                         logic.unitsize = 1;
00743                                         logic.data = ctx->trigger_buffer;
00744                                         sr_session_send(ctx->session_dev_id, &packet);
00745 
00746                                         ctx->trigger_stage = TRIGGER_FIRED;
00747                                         break;
00748                                 }
00749                                 return;
00750                         }
00751 
00752                         /*
00753                          * We had a match before, but not in the next sample. However, we may
00754                          * have a match on this stage in the next bit -- trigger on 0001 will
00755                          * fail on seeing 00001, so we need to go back to stage 0 -- but at
00756                          * the next sample from the one that matched originally, which the
00757                          * counter increment at the end of the loop takes care of.
00758                          */
00759                         if (ctx->trigger_stage > 0) {
00760                                 i -= ctx->trigger_stage;
00761                                 if (i < -1)
00762                                         i = -1; /* Oops, went back past this buffer. */
00763                                 /* Reset trigger stage. */
00764                                 ctx->trigger_stage = 0;
00765                         }
00766                 }
00767         }
00768 
00769         if (ctx->trigger_stage == TRIGGER_FIRED) {
00770                 /* Send the incoming transfer to the session bus. */
00771                 packet.type = SR_DF_LOGIC;
00772                 packet.payload = &logic;
00773                 logic.length = cur_buflen - trigger_offset;
00774                 logic.unitsize = 1;
00775                 logic.data = cur_buf + trigger_offset;
00776                 sr_session_send(ctx->session_dev_id, &packet);
00777                 g_free(cur_buf);
00778 
00779                 ctx->num_samples += cur_buflen;
00780                 if (ctx->limit_samples &&
00781                         (unsigned int)ctx->num_samples > ctx->limit_samples) {
00782                         abort_acquisition(ctx);
00783                 }
00784         } else {
00785                 /*
00786                  * TODO: Buffer pre-trigger data in capture
00787                  * ratio-sized buffer.
00788                  */
00789         }
00790 }
00791 
00792 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
00793 {
00794         struct sr_dev_inst *sdi;
00795         struct sr_datafeed_packet *packet;
00796         struct sr_datafeed_header *header;
00797         struct context *ctx;
00798         struct libusb_transfer *transfer;
00799         const struct libusb_pollfd **lupfd;
00800         int ret, size, i;
00801         unsigned char *buf;
00802 
00803         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00804                 return SR_ERR;
00805         ctx = sdi->priv;
00806         ctx->session_dev_id = cb_data;
00807         ctx->num_samples = 0;
00808 
00809         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
00810                 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
00811                 return SR_ERR_MALLOC;
00812         }
00813 
00814         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
00815                 sr_err("fx2lafw: %s: header malloc failed.", __func__);
00816                 return SR_ERR_MALLOC;
00817         }
00818 
00819         /* Start with 2K transfer, subsequently increased to 4K. */
00820         size = 2048;
00821         for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
00822                 if (!(buf = g_try_malloc(size))) {
00823                         sr_err("fx2lafw: %s: buf malloc failed.", __func__);
00824                         return SR_ERR_MALLOC;
00825                 }
00826                 transfer = libusb_alloc_transfer(0);
00827                 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
00828                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
00829                                 receive_transfer, ctx, 40);
00830                 if (libusb_submit_transfer(transfer) != 0) {
00831                         /* TODO: Free them all. */
00832                         libusb_free_transfer(transfer);
00833                         g_free(buf);
00834                         return SR_ERR;
00835                 }
00836 
00837                 ctx->submitted_transfers++;
00838                 size = 4096;
00839         }
00840 
00841         lupfd = libusb_get_pollfds(usb_context);
00842         for (i = 0; lupfd[i]; i++)
00843                 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
00844                               40, receive_data, NULL);
00845         free(lupfd); /* NOT g_free()! */
00846 
00847         packet->type = SR_DF_HEADER;
00848         packet->payload = header;
00849         header->feed_version = 1;
00850         gettimeofday(&header->starttime, NULL);
00851         header->samplerate = ctx->cur_samplerate;
00852         header->num_logic_probes = ctx->profile->num_probes;
00853         sr_session_send(cb_data, packet);
00854         g_free(header);
00855         g_free(packet);
00856 
00857         if ((ret = command_start_acquisition (ctx->usb->devhdl,
00858                 ctx->cur_samplerate)) != SR_OK) {
00859                 return ret;
00860         }
00861 
00862         return SR_OK;
00863 }
00864 
00865 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
00866 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
00867 {
00868         struct sr_dev_inst *sdi;
00869 
00870         /* Avoid compiler warnings. */
00871         (void)cb_data;
00872 
00873         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00874                 return SR_ERR;
00875  
00876         abort_acquisition(sdi->priv);
00877 
00878         return SR_OK;
00879 }
00880 
00881 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
00882         .name = "fx2lafw",
00883         .longname = "fx2lafw (generic driver for FX2 based LAs)",
00884         .api_version = 1,
00885         .init = hw_init,
00886         .cleanup = hw_cleanup,
00887         .dev_open = hw_dev_open,
00888         .dev_close = hw_dev_close,
00889         .dev_info_get = hw_dev_info_get,
00890         .dev_status_get = hw_dev_status_get,
00891         .hwcap_get_all = hw_hwcap_get_all,
00892         .dev_config_set = hw_dev_config_set,
00893         .dev_acquisition_start = hw_dev_acquisition_start,
00894         .dev_acquisition_stop = hw_dev_acquisition_stop,
00895 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines