libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
chronovu-la8.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include <ftdi.h>
00022 #include <glib.h>
00023 #include <string.h>
00024 #include <stdlib.h>
00025 #include "sigrok.h"
00026 #include "sigrok-internal.h"
00027 
00028 #define USB_VENDOR_ID                   0x0403
00029 #define USB_PRODUCT_ID                  0x6001
00030 #define USB_DESCRIPTION                 "ChronoVu LA8"
00031 #define USB_VENDOR_NAME                 "ChronoVu"
00032 #define USB_MODEL_NAME                  "LA8"
00033 #define USB_MODEL_VERSION               ""
00034 
00035 #define NUM_PROBES                      8
00036 #define TRIGGER_TYPES                   "01"
00037 #define SDRAM_SIZE                      (8 * 1024 * 1024)
00038 #define MIN_NUM_SAMPLES                 1
00039 
00040 #define BS                              4096 /* Block size */
00041 #define NUM_BLOCKS                      2048 /* Number of blocks */
00042 
00043 static GSList *dev_insts = NULL;
00044 
00045 /* Probes are numbered 0-7. */
00046 static const char *probe_names[NUM_PROBES + 1] = {
00047         "0",
00048         "1",
00049         "2",
00050         "3",
00051         "4",
00052         "5",
00053         "6",
00054         "7",
00055         NULL,
00056 };
00057 
00058 /* Private, per-device-instance driver context. */
00059 struct context {
00060         /** FTDI device context (used by libftdi). */
00061         struct ftdi_context *ftdic;
00062 
00063         /** The currently configured samplerate of the device. */
00064         uint64_t cur_samplerate;
00065 
00066         /** The current sampling limit (in ms). */
00067         uint64_t limit_msec;
00068 
00069         /** The current sampling limit (in number of samples). */
00070         uint64_t limit_samples;
00071 
00072         /** TODO */
00073         void *session_dev_id;
00074 
00075         /**
00076          * A buffer containing some (mangled) samples from the device.
00077          * Format: Pretty mangled-up (due to hardware reasons), see code.
00078          */
00079         uint8_t mangled_buf[BS];
00080 
00081         /**
00082          * An 8MB buffer where we'll store the de-mangled samples.
00083          * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
00084          */
00085         uint8_t *final_buf;
00086 
00087         /**
00088          * Trigger pattern (MSB = channel 7, LSB = channel 0).
00089          * A 1 bit matches a high signal, 0 matches a low signal on a probe.
00090          * Only low/high triggers (but not e.g. rising/falling) are supported.
00091          */
00092         uint8_t trigger_pattern;
00093 
00094         /**
00095          * Trigger mask (MSB = channel 7, LSB = channel 0).
00096          * A 1 bit means "must match trigger_pattern", 0 means "don't care".
00097          */
00098         uint8_t trigger_mask;
00099 
00100         /** Time (in seconds) before the trigger times out. */
00101         uint64_t trigger_timeout;
00102 
00103         /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
00104         int trigger_found;
00105 
00106         /** TODO */
00107         time_t done;
00108 
00109         /** Counter/index for the data block to be read. */
00110         int block_counter;
00111 
00112         /** The divcount value (determines the sample period) for the LA8. */
00113         uint8_t divcount;
00114 };
00115 
00116 /* This will be initialized via hw_dev_info_get()/SR_DI_SAMPLERATES. */
00117 static uint64_t supported_samplerates[255 + 1] = { 0 };
00118 
00119 /*
00120  * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
00121  * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
00122  */
00123 static struct sr_samplerates samplerates = {
00124         .low  = 0,
00125         .high = 0,
00126         .step = 0,
00127         .list = supported_samplerates,
00128 };
00129 
00130 /* Note: Continuous sampling is not supported by the hardware. */
00131 static int hwcaps[] = {
00132         SR_HWCAP_LOGIC_ANALYZER,
00133         SR_HWCAP_SAMPLERATE,
00134         SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
00135         SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
00136         0,
00137 };
00138 
00139 /* Function prototypes. */
00140 static int la8_close_usb_reset_sequencer(struct context *ctx);
00141 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
00142 static int la8_reset(struct context *ctx);
00143 
00144 static void fill_supported_samplerates_if_needed(void)
00145 {
00146         int i;
00147 
00148         /* Do nothing if supported_samplerates[] is already filled. */
00149         if (supported_samplerates[0] != 0)
00150                 return;
00151 
00152         /* Fill supported_samplerates[] with the proper values. */
00153         for (i = 0; i < 255; i++)
00154                 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
00155         supported_samplerates[255] = 0;
00156 }
00157 
00158 /**
00159  * Check if the given samplerate is supported by the LA8 hardware.
00160  *
00161  * @param samplerate The samplerate (in Hz) to check.
00162  * @return 1 if the samplerate is supported/valid, 0 otherwise.
00163  */
00164 static int is_valid_samplerate(uint64_t samplerate)
00165 {
00166         int i;
00167 
00168         fill_supported_samplerates_if_needed();
00169 
00170         for (i = 0; i < 255; i++) {
00171                 if (supported_samplerates[i] == samplerate)
00172                         return 1;
00173         }
00174 
00175         sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
00176                __func__, samplerate);
00177 
00178         return 0;
00179 }
00180 
00181 /**
00182  * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
00183  *
00184  * LA8 hardware: sample period = (divcount + 1) * 10ns.
00185  * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
00186  * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
00187  *
00188  * @param samplerate The samplerate in Hz.
00189  * @return The divcount value as needed by the hardware, or 0xff upon errors.
00190  */
00191 static uint8_t samplerate_to_divcount(uint64_t samplerate)
00192 {
00193         if (samplerate == 0) {
00194                 sr_err("la8: %s: samplerate was 0", __func__);
00195                 return 0xff;
00196         }
00197 
00198         if (!is_valid_samplerate(samplerate)) {
00199                 sr_err("la8: %s: can't get divcount, samplerate invalid",
00200                        __func__);
00201                 return 0xff;
00202         }
00203 
00204         return (SR_MHZ(100) / samplerate) - 1;
00205 }
00206 
00207 /**
00208  * Write data of a certain length to the LA8's FTDI device.
00209  *
00210  * @param ctx The struct containing private per-device-instance data. Must not
00211  *            be NULL. ctx->ftdic must not be NULL either.
00212  * @param buf The buffer containing the data to write. Must not be NULL.
00213  * @param size The number of bytes to write. Must be >= 0.
00214  * @return The number of bytes written, or a negative value upon errors.
00215  */
00216 static int la8_write(struct context *ctx, uint8_t *buf, int size)
00217 {
00218         int bytes_written;
00219 
00220         /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
00221 
00222         if (!buf) {
00223                 sr_err("la8: %s: buf was NULL", __func__);
00224                 return SR_ERR_ARG;
00225         }
00226 
00227         if (size < 0) {
00228                 sr_err("la8: %s: size was < 0", __func__);
00229                 return SR_ERR_ARG;
00230         }
00231 
00232         bytes_written = ftdi_write_data(ctx->ftdic, buf, size);
00233 
00234         if (bytes_written < 0) {
00235                 sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__,
00236                        bytes_written, ftdi_get_error_string(ctx->ftdic));
00237                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00238         } else if (bytes_written != size) {
00239                 sr_err("la8: %s: bytes to write: %d, bytes written: %d",
00240                        __func__, size, bytes_written);
00241                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00242         }
00243 
00244         return bytes_written;
00245 }
00246 
00247 /**
00248  * Read a certain amount of bytes from the LA8's FTDI device.
00249  *
00250  * @param ctx The struct containing private per-device-instance data. Must not
00251  *            be NULL. ctx->ftdic must not be NULL either.
00252  * @param buf The buffer where the received data will be stored. Must not
00253  *            be NULL.
00254  * @param size The number of bytes to read. Must be >= 1.
00255  * @return The number of bytes read, or a negative value upon errors.
00256  */
00257 static int la8_read(struct context *ctx, uint8_t *buf, int size)
00258 {
00259         int bytes_read;
00260 
00261         /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
00262 
00263         if (!buf) {
00264                 sr_err("la8: %s: buf was NULL", __func__);
00265                 return SR_ERR_ARG;
00266         }
00267 
00268         if (size <= 0) {
00269                 sr_err("la8: %s: size was <= 0", __func__);
00270                 return SR_ERR_ARG;
00271         }
00272 
00273         bytes_read = ftdi_read_data(ctx->ftdic, buf, size);
00274 
00275         if (bytes_read < 0) {
00276                 sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__,
00277                        bytes_read, ftdi_get_error_string(ctx->ftdic));
00278         } else if (bytes_read != size) {
00279                 // sr_err("la8: %s: bytes to read: %d, bytes read: %d",
00280                 //        __func__, size, bytes_read);
00281         }
00282 
00283         return bytes_read;
00284 }
00285 
00286 static int la8_close(struct context *ctx)
00287 {
00288         int ret;
00289 
00290         if (!ctx) {
00291                 sr_err("la8: %s: ctx was NULL", __func__);
00292                 return SR_ERR_ARG;
00293         }
00294 
00295         if (!ctx->ftdic) {
00296                 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00297                 return SR_ERR_ARG;
00298         }
00299 
00300         if ((ret = ftdi_usb_close(ctx->ftdic)) < 0) {
00301                 sr_err("la8: %s: ftdi_usb_close: (%d) %s",
00302                        __func__, ret, ftdi_get_error_string(ctx->ftdic));
00303         }
00304 
00305         return ret;
00306 }
00307 
00308 /**
00309  * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
00310  *
00311  * @param ctx The struct containing private per-device-instance data.
00312  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
00313  */
00314 static int la8_close_usb_reset_sequencer(struct context *ctx)
00315 {
00316         /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
00317         uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
00318         int ret;
00319 
00320         if (!ctx) {
00321                 sr_err("la8: %s: ctx was NULL", __func__);
00322                 return SR_ERR_ARG;
00323         }
00324 
00325         if (!ctx->ftdic) {
00326                 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00327                 return SR_ERR_ARG;
00328         }
00329 
00330         if (ctx->ftdic->usb_dev) {
00331                 /* Reset the LA8 sequencer logic, then wait 100ms. */
00332                 sr_dbg("la8: Resetting sequencer logic.");
00333                 (void) la8_write(ctx, buf, 8); /* Ignore errors. */
00334                 g_usleep(100 * 1000);
00335 
00336                 /* Purge FTDI buffers, then reset and close the FTDI device. */
00337                 sr_dbg("la8: Purging buffers, resetting+closing FTDI device.");
00338 
00339                 /* Log errors, but ignore them (i.e., don't abort). */
00340                 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0)
00341                         sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
00342                             __func__, ret, ftdi_get_error_string(ctx->ftdic));
00343                 if ((ret = ftdi_usb_reset(ctx->ftdic)) < 0)
00344                         sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
00345                                ret, ftdi_get_error_string(ctx->ftdic));
00346                 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0)
00347                         sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__,
00348                                ret, ftdi_get_error_string(ctx->ftdic));
00349         }
00350 
00351         /* Close USB device, deinitialize and free the FTDI context. */
00352         ftdi_free(ctx->ftdic); /* Returns void. */
00353         ctx->ftdic = NULL;
00354 
00355         return SR_OK;
00356 }
00357 
00358 /**
00359  * Reset the ChronoVu LA8.
00360  *
00361  * The LA8 must be reset after a failed read/write operation or upon timeouts.
00362  *
00363  * @param ctx The struct containing private per-device-instance data.
00364  * @return SR_OK upon success, SR_ERR upon failure.
00365  */
00366 static int la8_reset(struct context *ctx)
00367 {
00368         uint8_t buf[BS];
00369         time_t done, now;
00370         int bytes_read;
00371 
00372         if (!ctx) {
00373                 sr_err("la8: %s: ctx was NULL", __func__);
00374                 return SR_ERR_ARG;
00375         }
00376 
00377         if (!ctx->ftdic) {
00378                 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00379                 return SR_ERR_ARG;
00380         }
00381 
00382         sr_dbg("la8: Resetting the device.");
00383 
00384         /*
00385          * Purge pending read data from the FTDI hardware FIFO until
00386          * no more data is left, or a timeout occurs (after 20s).
00387          */
00388         done = 20 + time(NULL);
00389         do {
00390                 /* TODO: Ignore errors? Check for < 0 at least! */
00391                 bytes_read = la8_read(ctx, (uint8_t *)&buf, BS);
00392                 now = time(NULL);
00393         } while ((done > now) && (bytes_read > 0));
00394 
00395         /* Reset the LA8 sequencer logic and close the USB port. */
00396         (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00397 
00398         sr_dbg("la8: Device reset finished.");
00399 
00400         return SR_OK;
00401 }
00402 
00403 static int configure_probes(struct context *ctx, GSList *probes)
00404 {
00405         struct sr_probe *probe;
00406         GSList *l;
00407         uint8_t probe_bit;
00408         char *tc;
00409 
00410         /* Note: Caller checked that ctx != NULL. */
00411 
00412         ctx->trigger_pattern = 0;
00413         ctx->trigger_mask = 0; /* Default to "don't care" for all probes. */
00414 
00415         for (l = probes; l; l = l->next) {
00416                 probe = (struct sr_probe *)l->data;
00417 
00418                 if (!probe) {
00419                         sr_err("la8: %s: probe was NULL", __func__);
00420                         return SR_ERR;
00421                 }
00422 
00423                 /* Skip disabled probes. */
00424                 if (!probe->enabled)
00425                         continue;
00426 
00427                 /* Skip (enabled) probes with no configured trigger. */
00428                 if (!probe->trigger)
00429                         continue;
00430 
00431                 /* Note: Must only be run if probe->trigger != NULL. */
00432                 if (probe->index < 0 || probe->index > 7) {
00433                         sr_err("la8: %s: invalid probe index %d, must be "
00434                                "between 0 and 7", __func__, probe->index);
00435                         return SR_ERR;
00436                 }
00437 
00438                 probe_bit = (1 << (probe->index - 1));
00439 
00440                 /* Configure the probe's trigger mask and trigger pattern. */
00441                 for (tc = probe->trigger; tc && *tc; tc++) {
00442                         ctx->trigger_mask |= probe_bit;
00443 
00444                         /* Sanity check, LA8 only supports low/high trigger. */
00445                         if (*tc != '0' && *tc != '1') {
00446                                 sr_err("la8: %s: invalid trigger '%c', only "
00447                                        "'0'/'1' supported", __func__, *tc);
00448                                 return SR_ERR;
00449                         }
00450 
00451                         if (*tc == '1')
00452                                 ctx->trigger_pattern |= probe_bit;
00453                 }
00454         }
00455 
00456         sr_dbg("la8: trigger_mask = 0x%x, trigger_pattern = 0x%x",
00457                ctx->trigger_mask, ctx->trigger_pattern);
00458 
00459         return SR_OK;
00460 }
00461 
00462 static int hw_init(const char *devinfo)
00463 {
00464         int ret;
00465         struct sr_dev_inst *sdi;
00466         struct context *ctx;
00467 
00468         /* Avoid compiler errors. */
00469         (void)devinfo;
00470 
00471         /* Allocate memory for our private driver context. */
00472         if (!(ctx = g_try_malloc(sizeof(struct context)))) {
00473                 sr_err("la8: %s: struct context malloc failed", __func__);
00474                 goto err_free_nothing;
00475         }
00476 
00477         /* Set some sane defaults. */
00478         ctx->ftdic = NULL;
00479         ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
00480         ctx->limit_msec = 0;
00481         ctx->limit_samples = 0;
00482         ctx->session_dev_id = NULL;
00483         memset(ctx->mangled_buf, 0, BS);
00484         ctx->final_buf = NULL;
00485         ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
00486         ctx->trigger_mask = 0x00; /* All probes are "don't care". */
00487         ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
00488         ctx->trigger_found = 0;
00489         ctx->done = 0;
00490         ctx->block_counter = 0;
00491         ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
00492 
00493         /* Allocate memory where we'll store the de-mangled data. */
00494         if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
00495                 sr_err("la8: %s: final_buf malloc failed", __func__);
00496                 goto err_free_ctx;
00497         }
00498 
00499         /* Allocate memory for the FTDI context (ftdic) and initialize it. */
00500         if (!(ctx->ftdic = ftdi_new())) {
00501                 sr_err("la8: %s: ftdi_new failed", __func__);
00502                 goto err_free_final_buf;
00503         }
00504 
00505         /* Check for the device and temporarily open it. */
00506         if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
00507                         USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
00508                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00509                 goto err_free_ftdic;
00510         }
00511         sr_dbg("la8: Found LA8 device (%04x:%04x).", USB_VENDOR_ID,
00512                USB_PRODUCT_ID);
00513 
00514         /* Register the device with libsigrok. */
00515         sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
00516                         USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
00517         if (!sdi) {
00518                 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
00519                 goto err_close_ftdic;
00520         }
00521 
00522         sdi->priv = ctx;
00523 
00524         dev_insts = g_slist_append(dev_insts, sdi);
00525 
00526         sr_spew("la8: Device init successful.");
00527 
00528         /* Close device. We'll reopen it again when we need it. */
00529         (void) la8_close(ctx); /* Log, but ignore errors. */
00530 
00531         return 1;
00532 
00533 err_close_ftdic:
00534         (void) la8_close(ctx); /* Log, but ignore errors. */
00535 err_free_ftdic:
00536         free(ctx->ftdic); /* NOT g_free()! */
00537 err_free_final_buf:
00538         g_free(ctx->final_buf);
00539 err_free_ctx:
00540         g_free(ctx);
00541 err_free_nothing:
00542 
00543         return 0;
00544 }
00545 
00546 static int hw_dev_open(int dev_index)
00547 {
00548         int ret;
00549         struct sr_dev_inst *sdi;
00550         struct context *ctx;
00551 
00552         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00553                 sr_err("la8: %s: sdi was NULL", __func__);
00554                 return SR_ERR_BUG;
00555         }
00556 
00557         if (!(ctx = sdi->priv)) {
00558                 sr_err("la8: %s: sdi->priv was NULL", __func__);
00559                 return SR_ERR_BUG;
00560         }
00561 
00562         sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
00563                USB_PRODUCT_ID);
00564 
00565         /* Open the device. */
00566         if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
00567                         USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
00568                 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
00569                        __func__, ret, ftdi_get_error_string(ctx->ftdic));
00570                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00571                 return SR_ERR;
00572         }
00573         sr_dbg("la8: Device opened successfully.");
00574 
00575         /* Purge RX/TX buffers in the FTDI chip. */
00576         if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
00577                 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
00578                        __func__, ret, ftdi_get_error_string(ctx->ftdic));
00579                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00580                 goto err_dev_open_close_ftdic;
00581         }
00582         sr_dbg("la8: FTDI buffers purged successfully.");
00583 
00584         /* Enable flow control in the FTDI chip. */
00585         if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
00586                 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
00587                        __func__, ret, ftdi_get_error_string(ctx->ftdic));
00588                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00589                 goto err_dev_open_close_ftdic;
00590         }
00591         sr_dbg("la8: FTDI flow control enabled successfully.");
00592 
00593         /* Wait 100ms. */
00594         g_usleep(100 * 1000);
00595 
00596         sdi->status = SR_ST_ACTIVE;
00597 
00598         return SR_OK;
00599 
00600 err_dev_open_close_ftdic:
00601         (void) la8_close(ctx); /* Log, but ignore errors. */
00602         return SR_ERR;
00603 }
00604 
00605 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
00606 {
00607         struct context *ctx;
00608 
00609         /* Note: Caller checked that sdi and sdi->priv != NULL. */
00610 
00611         ctx = sdi->priv;
00612 
00613         sr_spew("la8: Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
00614 
00615         fill_supported_samplerates_if_needed();
00616 
00617         /* Check if this is a samplerate supported by the hardware. */
00618         if (!is_valid_samplerate(samplerate))
00619                 return SR_ERR;
00620 
00621         /* Set the new samplerate. */
00622         ctx->cur_samplerate = samplerate;
00623 
00624         sr_dbg("la8: Samplerate set to %" PRIu64 "Hz.", ctx->cur_samplerate);
00625 
00626         return SR_OK;
00627 }
00628 
00629 static int hw_dev_close(int dev_index)
00630 {
00631         struct sr_dev_inst *sdi;
00632         struct context *ctx;
00633 
00634         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00635                 sr_err("la8: %s: sdi was NULL", __func__);
00636                 return SR_ERR_BUG;
00637         }
00638 
00639         if (!(ctx = sdi->priv)) {
00640                 sr_err("la8: %s: sdi->priv was NULL", __func__);
00641                 return SR_ERR_BUG;
00642         }
00643 
00644         sr_dbg("la8: Closing device.");
00645 
00646         if (sdi->status == SR_ST_ACTIVE) {
00647                 sr_dbg("la8: Status ACTIVE, closing device.");
00648                 /* TODO: Really ignore errors here, or return SR_ERR? */
00649                 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
00650         } else {
00651                 sr_spew("la8: Status not ACTIVE, nothing to do.");
00652         }
00653 
00654         sdi->status = SR_ST_INACTIVE;
00655 
00656         sr_dbg("la8: Freeing sample buffer.");
00657         g_free(ctx->final_buf);
00658 
00659         return SR_OK;
00660 }
00661 
00662 static int hw_cleanup(void)
00663 {
00664         GSList *l;
00665         struct sr_dev_inst *sdi;
00666         int ret = SR_OK;
00667 
00668         /* Properly close all devices. */
00669         for (l = dev_insts; l; l = l->next) {
00670                 if (!(sdi = l->data)) {
00671                         /* Log error, but continue cleaning up the rest. */
00672                         sr_err("la8: %s: sdi was NULL, continuing", __func__);
00673                         ret = SR_ERR_BUG;
00674                         continue;
00675                 }
00676                 sr_dev_inst_free(sdi); /* Returns void. */
00677         }
00678         g_slist_free(dev_insts); /* Returns void. */
00679         dev_insts = NULL;
00680 
00681         return ret;
00682 }
00683 
00684 static void *hw_dev_info_get(int dev_index, int dev_info_id)
00685 {
00686         struct sr_dev_inst *sdi;
00687         struct context *ctx;
00688         void *info;
00689 
00690         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00691                 sr_err("la8: %s: sdi was NULL", __func__);
00692                 return NULL;
00693         }
00694 
00695         if (!(ctx = sdi->priv)) {
00696                 sr_err("la8: %s: sdi->priv was NULL", __func__);
00697                 return NULL;
00698         }
00699 
00700         sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
00701                 dev_index, dev_info_id);
00702 
00703         switch (dev_info_id) {
00704         case SR_DI_INST:
00705                 info = sdi;
00706                 sr_spew("la8: %s: Returning sdi.", __func__);
00707                 break;
00708         case SR_DI_NUM_PROBES:
00709                 info = GINT_TO_POINTER(NUM_PROBES);
00710                 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
00711                         NUM_PROBES);
00712                 break;
00713         case SR_DI_PROBE_NAMES:
00714                 info = probe_names;
00715                 sr_spew("la8: %s: Returning probenames.", __func__);
00716                 break;
00717         case SR_DI_SAMPLERATES:
00718                 fill_supported_samplerates_if_needed();
00719                 info = &samplerates;
00720                 sr_spew("la8: %s: Returning samplerates.", __func__);
00721                 break;
00722         case SR_DI_TRIGGER_TYPES:
00723                 info = (char *)TRIGGER_TYPES;
00724                 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
00725                         TRIGGER_TYPES);
00726                 break;
00727         case SR_DI_CUR_SAMPLERATE:
00728                 info = &ctx->cur_samplerate;
00729                 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
00730                         __func__, ctx->cur_samplerate);
00731                 break;
00732         default:
00733                 /* Unknown device info ID, return NULL. */
00734                 sr_err("la8: %s: Unknown device info ID", __func__);
00735                 info = NULL;
00736                 break;
00737         }
00738 
00739         return info;
00740 }
00741 
00742 static int hw_dev_status_get(int dev_index)
00743 {
00744         struct sr_dev_inst *sdi;
00745 
00746         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00747                 sr_err("la8: %s: sdi was NULL, device not found", __func__);
00748                 return SR_ST_NOT_FOUND;
00749         }
00750 
00751         sr_dbg("la8: Returning status: %d.", sdi->status);
00752 
00753         return sdi->status;
00754 }
00755 
00756 static int *hw_hwcap_get_all(void)
00757 {
00758         sr_spew("la8: Returning list of device capabilities.");
00759 
00760         return hwcaps;
00761 }
00762 
00763 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
00764 {
00765         struct sr_dev_inst *sdi;
00766         struct context *ctx;
00767 
00768         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00769                 sr_err("la8: %s: sdi was NULL", __func__);
00770                 return SR_ERR_BUG;
00771         }
00772 
00773         if (!(ctx = sdi->priv)) {
00774                 sr_err("la8: %s: sdi->priv was NULL", __func__);
00775                 return SR_ERR_BUG;
00776         }
00777 
00778         sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
00779 
00780         switch (hwcap) {
00781         case SR_HWCAP_SAMPLERATE:
00782                 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR) {
00783                         sr_err("la8: %s: setting samplerate failed.", __func__);
00784                         return SR_ERR;
00785                 }
00786                 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
00787                 break;
00788         case SR_HWCAP_PROBECONFIG:
00789                 if (configure_probes(ctx, (GSList *)value) != SR_OK) {
00790                         sr_err("la8: %s: probe config failed.", __func__);
00791                         return SR_ERR;
00792                 }
00793                 break;
00794         case SR_HWCAP_LIMIT_MSEC:
00795                 if (*(uint64_t *)value == 0) {
00796                         sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
00797                         return SR_ERR;
00798                 }
00799                 ctx->limit_msec = *(uint64_t *)value;
00800                 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
00801                 break;
00802         case SR_HWCAP_LIMIT_SAMPLES:
00803                 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
00804                         sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
00805                         return SR_ERR;
00806                 }
00807                 ctx->limit_samples = *(uint64_t *)value;
00808                 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
00809                 break;
00810         default:
00811                 /* Unknown capability, return SR_ERR. */
00812                 sr_err("la8: %s: Unknown capability.", __func__);
00813                 return SR_ERR;
00814                 break;
00815         }
00816 
00817         return SR_OK;
00818 }
00819 
00820 /**
00821  * Get a block of data from the LA8.
00822  *
00823  * @param ctx The struct containing private per-device-instance data. Must not
00824  *            be NULL. ctx->ftdic must not be NULL either.
00825  * @return SR_OK upon success, or SR_ERR upon errors.
00826  */
00827 static int la8_read_block(struct context *ctx)
00828 {
00829         int i, byte_offset, m, mi, p, index, bytes_read;
00830         time_t now;
00831 
00832         /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
00833 
00834         sr_spew("la8: Reading block %d.", ctx->block_counter);
00835 
00836         bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
00837 
00838         /* If first block read got 0 bytes, retry until success or timeout. */
00839         if ((bytes_read == 0) && (ctx->block_counter == 0)) {
00840                 do {
00841                         sr_spew("la8: Reading block 0 (again).");
00842                         bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
00843                         /* TODO: How to handle read errors here? */
00844                         now = time(NULL);
00845                 } while ((ctx->done > now) && (bytes_read == 0));
00846         }
00847 
00848         /* Check if block read was successful or a timeout occured. */
00849         if (bytes_read != BS) {
00850                 sr_err("la8: Trigger timed out. Bytes read: %d.", bytes_read);
00851                 (void) la8_reset(ctx); /* Ignore errors. */
00852                 return SR_ERR;
00853         }
00854 
00855         /* De-mangle the data. */
00856         sr_spew("la8: Demangling block %d.", ctx->block_counter);
00857         byte_offset = ctx->block_counter * BS;
00858         m = byte_offset / (1024 * 1024);
00859         mi = m * (1024 * 1024);
00860         for (i = 0; i < BS; i++) {
00861                 p = i & (1 << 0);
00862                 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
00863                 index += (ctx->divcount == 0) ? p : (1 - p);
00864                 ctx->final_buf[index] = ctx->mangled_buf[i];
00865         }
00866 
00867         return SR_OK;
00868 }
00869 
00870 static void send_block_to_session_bus(struct context *ctx, int block)
00871 {
00872         int i;
00873         uint8_t sample, expected_sample;
00874         struct sr_datafeed_packet packet;
00875         struct sr_datafeed_logic logic;
00876         int trigger_point; /* Relative trigger point (in this block). */
00877 
00878         /* Note: No sanity checks on ctx/block, caller is responsible. */
00879 
00880         /* Check if we can find the trigger condition in this block. */
00881         trigger_point = -1;
00882         expected_sample = ctx->trigger_pattern & ctx->trigger_mask;
00883         for (i = 0; i < BS; i++) {
00884                 /* Don't continue if the trigger was found previously. */
00885                 if (ctx->trigger_found)
00886                         break;
00887 
00888                 /*
00889                  * Also, don't continue if triggers are "don't care", i.e. if
00890                  * no trigger conditions were specified by the user. In that
00891                  * case we don't want to send an SR_DF_TRIGGER packet at all.
00892                  */
00893                 if (ctx->trigger_mask == 0x00)
00894                         break;
00895 
00896                 sample = *(ctx->final_buf + (block * BS) + i);
00897 
00898                 if ((sample & ctx->trigger_mask) == expected_sample) {
00899                         trigger_point = i;
00900                         ctx->trigger_found = 1;
00901                         break;
00902                 }
00903         }
00904 
00905         /* If no trigger was found, send one SR_DF_LOGIC packet. */
00906         if (trigger_point == -1) {
00907                 /* Send an SR_DF_LOGIC packet to the session bus. */
00908                 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
00909                         "block %d", BS, block);
00910                 packet.type = SR_DF_LOGIC;
00911                 packet.payload = &logic;
00912                 logic.length = BS;
00913                 logic.unitsize = 1;
00914                 logic.data = ctx->final_buf + (block * BS);
00915                 sr_session_send(ctx->session_dev_id, &packet);
00916                 return;
00917         }
00918 
00919         /*
00920          * We found the trigger, so some special handling is needed. We have
00921          * to send an SR_DF_LOGIC packet with the samples before the trigger
00922          * (if any), then the SD_DF_TRIGGER packet itself, then another
00923          * SR_DF_LOGIC packet with the samples after the trigger (if any).
00924          */
00925 
00926         /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
00927 
00928         /* If at least one sample is located before the trigger... */
00929         if (trigger_point > 0) {
00930                 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
00931                 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
00932                         "start = %d, length = %d", block * BS, trigger_point);
00933                 packet.type = SR_DF_LOGIC;
00934                 packet.payload = &logic;
00935                 logic.length = trigger_point;
00936                 logic.unitsize = 1;
00937                 logic.data = ctx->final_buf + (block * BS);
00938                 sr_session_send(ctx->session_dev_id, &packet);
00939         }
00940 
00941         /* Send the SR_DF_TRIGGER packet to the session bus. */
00942         sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
00943                 (block * BS) + trigger_point);
00944         packet.type = SR_DF_TRIGGER;
00945         packet.payload = NULL;
00946         sr_session_send(ctx->session_dev_id, &packet);
00947 
00948         /* If at least one sample is located after the trigger... */
00949         if (trigger_point < (BS - 1)) {
00950                 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
00951                 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
00952                         "start = %d, length = %d",
00953                         (block * BS) + trigger_point, BS - trigger_point);
00954                 packet.type = SR_DF_LOGIC;
00955                 packet.payload = &logic;
00956                 logic.length = BS - trigger_point;
00957                 logic.unitsize = 1;
00958                 logic.data = ctx->final_buf + (block * BS) + trigger_point;
00959                 sr_session_send(ctx->session_dev_id, &packet);
00960         }
00961 }
00962 
00963 static int receive_data(int fd, int revents, void *cb_data)
00964 {
00965         int i, ret;
00966         struct sr_dev_inst *sdi;
00967         struct context *ctx;
00968 
00969         /* Avoid compiler errors. */
00970         (void)fd;
00971         (void)revents;
00972 
00973         if (!(sdi = cb_data)) {
00974                 sr_err("la8: %s: cb_data was NULL", __func__);
00975                 return FALSE;
00976         }
00977 
00978         if (!(ctx = sdi->priv)) {
00979                 sr_err("la8: %s: sdi->priv was NULL", __func__);
00980                 return FALSE;
00981         }
00982 
00983         if (!ctx->ftdic) {
00984                 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00985                 return FALSE;
00986         }
00987 
00988         /* Get one block of data. */
00989         if ((ret = la8_read_block(ctx)) < 0) {
00990                 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
00991                 hw_dev_acquisition_stop(sdi->index, sdi);
00992                 return FALSE;
00993         }
00994 
00995         /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
00996         if (ctx->block_counter != (NUM_BLOCKS - 1)) {
00997                 ctx->block_counter++;
00998                 return TRUE;
00999         }
01000 
01001         sr_dbg("la8: Sampling finished, sending data to session bus now.");
01002 
01003         /* All data was received and demangled, send it to the session bus. */
01004         for (i = 0; i < NUM_BLOCKS; i++)
01005                 send_block_to_session_bus(ctx, i);
01006 
01007         hw_dev_acquisition_stop(sdi->index, sdi);
01008 
01009         // return FALSE; /* FIXME? */
01010         return TRUE;
01011 }
01012 
01013 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
01014 {
01015         struct sr_dev_inst *sdi;
01016         struct context *ctx;
01017         struct sr_datafeed_packet packet;
01018         struct sr_datafeed_header header;
01019         uint8_t buf[4];
01020         int bytes_written;
01021 
01022         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
01023                 sr_err("la8: %s: sdi was NULL", __func__);
01024                 return SR_ERR_BUG;
01025         }
01026 
01027         if (!(ctx = sdi->priv)) {
01028                 sr_err("la8: %s: sdi->priv was NULL", __func__);
01029                 return SR_ERR_BUG;
01030         }
01031 
01032         if (!ctx->ftdic) {
01033                 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
01034                 return SR_ERR_BUG;
01035         }
01036 
01037         ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
01038         if (ctx->divcount == 0xff) {
01039                 sr_err("la8: %s: invalid divcount/samplerate", __func__);
01040                 return SR_ERR;
01041         }
01042 
01043         sr_dbg("la8: Starting acquisition.");
01044 
01045         /* Fill acquisition parameters into buf[]. */
01046         buf[0] = ctx->divcount;
01047         buf[1] = 0xff; /* This byte must always be 0xff. */
01048         buf[2] = ctx->trigger_pattern;
01049         buf[3] = ctx->trigger_mask;
01050 
01051         /* Start acquisition. */
01052         bytes_written = la8_write(ctx, buf, 4);
01053 
01054         if (bytes_written < 0) {
01055                 sr_err("la8: Acquisition failed to start.");
01056                 return SR_ERR;
01057         } else if (bytes_written != 4) {
01058                 sr_err("la8: Acquisition failed to start.");
01059                 return SR_ERR; /* TODO: Other error and return code? */
01060         }
01061 
01062         sr_dbg("la8: Acquisition started successfully.");
01063 
01064         ctx->session_dev_id = cb_data;
01065 
01066         /* Send header packet to the session bus. */
01067         sr_dbg("la8: Sending SR_DF_HEADER.");
01068         packet.type = SR_DF_HEADER;
01069         packet.payload = &header;
01070         header.feed_version = 1;
01071         gettimeofday(&header.starttime, NULL);
01072         header.samplerate = ctx->cur_samplerate;
01073         header.num_logic_probes = NUM_PROBES;
01074         sr_session_send(ctx->session_dev_id, &packet);
01075 
01076         /* Time when we should be done (for detecting trigger timeouts). */
01077         ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
01078                         + ctx->trigger_timeout;
01079         ctx->block_counter = 0;
01080         ctx->trigger_found = 0;
01081 
01082         /* Hook up a dummy handler to receive data from the LA8. */
01083         sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
01084 
01085         return SR_OK;
01086 }
01087 
01088 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
01089 {
01090         struct sr_dev_inst *sdi;
01091         struct context *ctx;
01092         struct sr_datafeed_packet packet;
01093 
01094         sr_dbg("la8: Stopping acquisition.");
01095 
01096         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
01097                 sr_err("la8: %s: sdi was NULL", __func__);
01098                 return SR_ERR_BUG;
01099         }
01100 
01101         if (!(ctx = sdi->priv)) {
01102                 sr_err("la8: %s: sdi->priv was NULL", __func__);
01103                 return SR_ERR_BUG;
01104         }
01105 
01106         /* Send end packet to the session bus. */
01107         sr_dbg("la8: Sending SR_DF_END.");
01108         packet.type = SR_DF_END;
01109         sr_session_send(cb_data, &packet);
01110 
01111         return SR_OK;
01112 }
01113 
01114 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
01115         .name = "chronovu-la8",
01116         .longname = "ChronoVu LA8",
01117         .api_version = 1,
01118         .init = hw_init,
01119         .cleanup = hw_cleanup,
01120         .dev_open = hw_dev_open,
01121         .dev_close = hw_dev_close,
01122         .dev_info_get = hw_dev_info_get,
01123         .dev_status_get = hw_dev_status_get,
01124         .hwcap_get_all = hw_hwcap_get_all,
01125         .dev_config_set = hw_dev_config_set,
01126         .dev_acquisition_start = hw_dev_acquisition_start,
01127         .dev_acquisition_stop = hw_dev_acquisition_stop,
01128 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines