libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
ols.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  *
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 3 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, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <stdio.h>
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 #include <unistd.h>
00027 #ifdef _WIN32
00028 #include <windows.h>
00029 #else
00030 #include <termios.h>
00031 #endif
00032 #include <string.h>
00033 #include <sys/time.h>
00034 #include <inttypes.h>
00035 #ifdef _WIN32
00036 /* TODO */
00037 #else
00038 #include <arpa/inet.h>
00039 #endif
00040 #include <glib.h>
00041 #include "sigrok.h"
00042 #include "sigrok-internal.h"
00043 #include "ols.h"
00044 
00045 #ifdef _WIN32
00046 #define O_NONBLOCK FIONBIO
00047 #endif
00048 
00049 static int hwcaps[] = {
00050         SR_HWCAP_LOGIC_ANALYZER,
00051         SR_HWCAP_SAMPLERATE,
00052         SR_HWCAP_CAPTURE_RATIO,
00053         SR_HWCAP_LIMIT_SAMPLES,
00054         SR_HWCAP_RLE,
00055         0,
00056 };
00057 
00058 /* Probes are numbered 0-31 (on the PCB silkscreen). */
00059 static const char *probe_names[NUM_PROBES + 1] = {
00060         "0",
00061         "1",
00062         "2",
00063         "3",
00064         "4",
00065         "5",
00066         "6",
00067         "7",
00068         "8",
00069         "9",
00070         "10",
00071         "11",
00072         "12",
00073         "13",
00074         "14",
00075         "15",
00076         "16",
00077         "17",
00078         "18",
00079         "19",
00080         "20",
00081         "21",
00082         "22",
00083         "23",
00084         "24",
00085         "25",
00086         "26",
00087         "27",
00088         "28",
00089         "29",
00090         "30",
00091         "31",
00092         NULL,
00093 };
00094 
00095 /* default supported samplerates, can be overridden by device metadata */
00096 static struct sr_samplerates samplerates = {
00097         SR_HZ(10),
00098         SR_MHZ(200),
00099         SR_HZ(1),
00100         NULL,
00101 };
00102 
00103 /* List of struct sr_serial_dev_inst */
00104 static GSList *dev_insts = NULL;
00105 
00106 static int send_shortcommand(int fd, uint8_t command)
00107 {
00108         char buf[1];
00109 
00110         sr_dbg("ols: sending cmd 0x%.2x", command);
00111         buf[0] = command;
00112         if (serial_write(fd, buf, 1) != 1)
00113                 return SR_ERR;
00114 
00115         return SR_OK;
00116 }
00117 
00118 static int send_longcommand(int fd, uint8_t command, uint32_t data)
00119 {
00120         char buf[5];
00121 
00122         sr_dbg("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
00123         buf[0] = command;
00124         buf[1] = (data & 0xff000000) >> 24;
00125         buf[2] = (data & 0xff0000) >> 16;
00126         buf[3] = (data & 0xff00) >> 8;
00127         buf[4] = data & 0xff;
00128         if (serial_write(fd, buf, 5) != 5)
00129                 return SR_ERR;
00130 
00131         return SR_OK;
00132 }
00133 
00134 static int configure_probes(struct context *ctx, GSList *probes)
00135 {
00136         struct sr_probe *probe;
00137         GSList *l;
00138         int probe_bit, stage, i;
00139         char *tc;
00140 
00141         ctx->probe_mask = 0;
00142         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
00143                 ctx->trigger_mask[i] = 0;
00144                 ctx->trigger_value[i] = 0;
00145         }
00146 
00147         ctx->num_stages = 0;
00148         for (l = probes; l; l = l->next) {
00149                 probe = (struct sr_probe *)l->data;
00150                 if (!probe->enabled)
00151                         continue;
00152 
00153                 /*
00154                  * Set up the probe mask for later configuration into the
00155                  * flag register.
00156                  */
00157                 probe_bit = 1 << (probe->index - 1);
00158                 ctx->probe_mask |= probe_bit;
00159 
00160                 if (!probe->trigger)
00161                         continue;
00162 
00163                 /* Configure trigger mask and value. */
00164                 stage = 0;
00165                 for (tc = probe->trigger; tc && *tc; tc++) {
00166                         ctx->trigger_mask[stage] |= probe_bit;
00167                         if (*tc == '1')
00168                                 ctx->trigger_value[stage] |= probe_bit;
00169                         stage++;
00170                         if (stage > 3)
00171                                 /*
00172                                  * TODO: Only supporting parallel mode, with
00173                                  * up to 4 stages.
00174                                  */
00175                                 return SR_ERR;
00176                 }
00177                 if (stage > ctx->num_stages)
00178                         ctx->num_stages = stage;
00179         }
00180 
00181         return SR_OK;
00182 }
00183 
00184 static uint32_t reverse16(uint32_t in)
00185 {
00186         uint32_t out;
00187 
00188         out = (in & 0xff) << 8;
00189         out |= (in & 0xff00) >> 8;
00190         out |= (in & 0xff0000) << 8;
00191         out |= (in & 0xff000000) >> 8;
00192 
00193         return out;
00194 }
00195 
00196 static uint32_t reverse32(uint32_t in)
00197 {
00198         uint32_t out;
00199 
00200         out = (in & 0xff) << 24;
00201         out |= (in & 0xff00) << 8;
00202         out |= (in & 0xff0000) >> 8;
00203         out |= (in & 0xff000000) >> 24;
00204 
00205         return out;
00206 }
00207 
00208 static struct context *ols_dev_new(void)
00209 {
00210         struct context *ctx;
00211 
00212         /* TODO: Is 'ctx' ever g_free()'d? */
00213         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
00214                 sr_err("ols: %s: ctx malloc failed", __func__);
00215                 return NULL;
00216         }
00217 
00218         ctx->trigger_at = -1;
00219         ctx->probe_mask = 0xffffffff;
00220         ctx->cur_samplerate = SR_KHZ(200);
00221         ctx->serial = NULL;
00222 
00223         return ctx;
00224 }
00225 
00226 static struct sr_dev_inst *get_metadata(int fd)
00227 {
00228         struct sr_dev_inst *sdi;
00229         struct context *ctx;
00230         uint32_t tmp_int;
00231         uint8_t key, type, token;
00232         GString *tmp_str, *devname, *version;
00233         gchar tmp_c;
00234 
00235         sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
00236         ctx = ols_dev_new();
00237         sdi->priv = ctx;
00238 
00239         devname = g_string_new("");
00240         version = g_string_new("");
00241 
00242         key = 0xff;
00243         while (key) {
00244                 if (serial_read(fd, &key, 1) != 1 || key == 0x00)
00245                         break;
00246                 type = key >> 5;
00247                 token = key & 0x1f;
00248                 switch (type) {
00249                 case 0:
00250                         /* NULL-terminated string */
00251                         tmp_str = g_string_new("");
00252                         while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0')
00253                                 g_string_append_c(tmp_str, tmp_c);
00254                         sr_dbg("ols: got metadata key 0x%.2x value '%s'",
00255                                key, tmp_str->str);
00256                         switch (token) {
00257                         case 0x01:
00258                                 /* Device name */
00259                                 devname = g_string_append(devname, tmp_str->str);
00260                                 break;
00261                         case 0x02:
00262                                 /* FPGA firmware version */
00263                                 if (version->len)
00264                                         g_string_append(version, ", ");
00265                                 g_string_append(version, "FPGA version ");
00266                                 g_string_append(version, tmp_str->str);
00267                                 break;
00268                         case 0x03:
00269                                 /* Ancillary version */
00270                                 if (version->len)
00271                                         g_string_append(version, ", ");
00272                                 g_string_append(version, "Ancillary version ");
00273                                 g_string_append(version, tmp_str->str);
00274                                 break;
00275                         default:
00276                                 sr_info("ols: unknown token 0x%.2x: '%s'",
00277                                         token, tmp_str->str);
00278                                 break;
00279                         }
00280                         g_string_free(tmp_str, TRUE);
00281                         break;
00282                 case 1:
00283                         /* 32-bit unsigned integer */
00284                         if (serial_read(fd, &tmp_int, 4) != 4)
00285                                 break;
00286                         tmp_int = reverse32(tmp_int);
00287                         sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x",
00288                                key, tmp_int);
00289                         switch (token) {
00290                         case 0x00:
00291                                 /* Number of usable probes */
00292                                 ctx->num_probes = tmp_int;
00293                                 break;
00294                         case 0x01:
00295                                 /* Amount of sample memory available (bytes) */
00296                                 ctx->max_samples = tmp_int;
00297                                 break;
00298                         case 0x02:
00299                                 /* Amount of dynamic memory available (bytes) */
00300                                 /* what is this for? */
00301                                 break;
00302                         case 0x03:
00303                                 /* Maximum sample rate (hz) */
00304                                 ctx->max_samplerate = tmp_int;
00305                                 break;
00306                         case 0x04:
00307                                 /* protocol version */
00308                                 ctx->protocol_version = tmp_int;
00309                                 break;
00310                         default:
00311                                 sr_info("ols: unknown token 0x%.2x: 0x%.8x",
00312                                         token, tmp_int);
00313                                 break;
00314                         }
00315                         break;
00316                 case 2:
00317                         /* 8-bit unsigned integer */
00318                         if (serial_read(fd, &tmp_c, 1) != 1)
00319                                 break;
00320                         sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x",
00321                                key, tmp_c);
00322                         switch (token) {
00323                         case 0x00:
00324                                 /* Number of usable probes */
00325                                 ctx->num_probes = tmp_c;
00326                                 break;
00327                         case 0x01:
00328                                 /* protocol version */
00329                                 ctx->protocol_version = tmp_c;
00330                                 break;
00331                         default:
00332                                 sr_info("ols: unknown token 0x%.2x: 0x%.2x",
00333                                         token, tmp_c);
00334                                 break;
00335                         }
00336                         break;
00337                 default:
00338                         /* unknown type */
00339                         break;
00340                 }
00341         }
00342 
00343         sdi->model = devname->str;
00344         sdi->version = version->str;
00345         g_string_free(devname, FALSE);
00346         g_string_free(version, FALSE);
00347 
00348         return sdi;
00349 }
00350 
00351 static int hw_init(const char *devinfo)
00352 {
00353         struct sr_dev_inst *sdi;
00354         struct context *ctx;
00355         GSList *ports, *l;
00356         GPollFD *fds, probefd;
00357         int devcnt, final_devcnt, num_ports, fd, ret, i;
00358         char buf[8], **dev_names, **serial_params;
00359 
00360         final_devcnt = 0;
00361 
00362         if (devinfo)
00363                 ports = g_slist_append(NULL, g_strdup(devinfo));
00364         else
00365                 /* No specific device given, so scan all serial ports. */
00366                 ports = list_serial_ports();
00367 
00368         num_ports = g_slist_length(ports);
00369 
00370         if (!(fds = g_try_malloc0(num_ports * sizeof(GPollFD)))) {
00371                 sr_err("ols: %s: fds malloc failed", __func__);
00372                 goto hw_init_free_ports; /* TODO: SR_ERR_MALLOC. */
00373         }
00374 
00375         if (!(dev_names = g_try_malloc(num_ports * sizeof(char *)))) {
00376                 sr_err("ols: %s: dev_names malloc failed", __func__);
00377                 goto hw_init_free_fds; /* TODO: SR_ERR_MALLOC. */
00378         }
00379 
00380         if (!(serial_params = g_try_malloc(num_ports * sizeof(char *)))) {
00381                 sr_err("ols: %s: serial_params malloc failed", __func__);
00382                 goto hw_init_free_dev_names; /* TODO: SR_ERR_MALLOC. */
00383         }
00384 
00385         devcnt = 0;
00386         for (l = ports; l; l = l->next) {
00387                 /* The discovery procedure is like this: first send the Reset
00388                  * command (0x00) 5 times, since the device could be anywhere
00389                  * in a 5-byte command. Then send the ID command (0x02).
00390                  * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
00391                  * have a match.
00392                  *
00393                  * Since it may take the device a while to respond at 115Kb/s,
00394                  * we do all the sending first, then wait for all of them to
00395                  * respond with g_poll().
00396                  */
00397                 sr_info("ols: probing %s...", (char *)l->data);
00398                 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
00399                 if (fd != -1) {
00400                         serial_params[devcnt] = serial_backup_params(fd);
00401                         serial_set_params(fd, 115200, 8, 0, 1, 2);
00402                         ret = SR_OK;
00403                         for (i = 0; i < 5; i++) {
00404                                 if ((ret = send_shortcommand(fd,
00405                                         CMD_RESET)) != SR_OK) {
00406                                         /* Serial port is not writable. */
00407                                         break;
00408                                 }
00409                         }
00410                         if (ret != SR_OK) {
00411                                 serial_restore_params(fd,
00412                                         serial_params[devcnt]);
00413                                 serial_close(fd);
00414                                 continue;
00415                         }
00416                         send_shortcommand(fd, CMD_ID);
00417                         fds[devcnt].fd = fd;
00418                         fds[devcnt].events = G_IO_IN;
00419                         dev_names[devcnt] = g_strdup(l->data);
00420                         devcnt++;
00421                 }
00422                 g_free(l->data);
00423         }
00424 
00425         /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
00426         usleep(10000);
00427 
00428         g_poll(fds, devcnt, 1);
00429 
00430         for (i = 0; i < devcnt; i++) {
00431                 if (fds[i].revents != G_IO_IN)
00432                         continue;
00433                 if (serial_read(fds[i].fd, buf, 4) != 4)
00434                         continue;
00435                 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
00436                         continue;
00437 
00438                 /* definitely using the OLS protocol, check if it supports
00439                  * the metadata command
00440                  */
00441                 send_shortcommand(fds[i].fd, CMD_METADATA);
00442                 probefd.fd = fds[i].fd;
00443                 probefd.events = G_IO_IN;
00444                 if (g_poll(&probefd, 1, 10) > 0) {
00445                         /* got metadata */
00446                         sdi = get_metadata(fds[i].fd);
00447                         sdi->index = final_devcnt;
00448                         ctx = sdi->priv;
00449                 } else {
00450                         /* not an OLS -- some other board that uses the sump protocol */
00451                         sdi = sr_dev_inst_new(final_devcnt, SR_ST_INACTIVE,
00452                                         "Sump", "Logic Analyzer", "v1.0");
00453                         ctx = ols_dev_new();
00454                         ctx->num_probes = 32;
00455                         sdi->priv = ctx;
00456                 }
00457                 ctx->serial = sr_serial_dev_inst_new(dev_names[i], -1);
00458                 dev_insts = g_slist_append(dev_insts, sdi);
00459                 final_devcnt++;
00460                 serial_close(fds[i].fd);
00461                 fds[i].fd = 0;
00462         }
00463 
00464         /* clean up after all the probing */
00465         for (i = 0; i < devcnt; i++) {
00466                 if (fds[i].fd != 0) {
00467                         serial_restore_params(fds[i].fd, serial_params[i]);
00468                         serial_close(fds[i].fd);
00469                 }
00470                 g_free(serial_params[i]);
00471                 g_free(dev_names[i]);
00472         }
00473 
00474         g_free(serial_params);
00475 hw_init_free_dev_names:
00476         g_free(dev_names);
00477 hw_init_free_fds:
00478         g_free(fds);
00479 hw_init_free_ports:
00480         g_slist_free(ports);
00481 
00482         return final_devcnt;
00483 }
00484 
00485 static int hw_dev_open(int dev_index)
00486 {
00487         struct sr_dev_inst *sdi;
00488         struct context *ctx;
00489 
00490         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00491                 return SR_ERR;
00492 
00493         ctx = sdi->priv;
00494 
00495         ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR);
00496         if (ctx->serial->fd == -1)
00497                 return SR_ERR;
00498 
00499         sdi->status = SR_ST_ACTIVE;
00500 
00501         return SR_OK;
00502 }
00503 
00504 static int hw_dev_close(int dev_index)
00505 {
00506         struct sr_dev_inst *sdi;
00507         struct context *ctx;
00508 
00509         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00510                 sr_err("ols: %s: sdi was NULL", __func__);
00511                 return SR_ERR_BUG;
00512         }
00513 
00514         ctx = sdi->priv;
00515 
00516         /* TODO */
00517         if (ctx->serial->fd != -1) {
00518                 serial_close(ctx->serial->fd);
00519                 ctx->serial->fd = -1;
00520                 sdi->status = SR_ST_INACTIVE;
00521         }
00522 
00523         return SR_OK;
00524 }
00525 
00526 static int hw_cleanup(void)
00527 {
00528         GSList *l;
00529         struct sr_dev_inst *sdi;
00530         struct context *ctx;
00531         int ret = SR_OK;
00532 
00533         /* Properly close and free all devices. */
00534         for (l = dev_insts; l; l = l->next) {
00535                 if (!(sdi = l->data)) {
00536                         /* Log error, but continue cleaning up the rest. */
00537                         sr_err("ols: %s: sdi was NULL, continuing", __func__);
00538                         ret = SR_ERR_BUG;
00539                         continue;
00540                 }
00541                 if (!(ctx = sdi->priv)) {
00542                         /* Log error, but continue cleaning up the rest. */
00543                         sr_err("ols: %s: sdi->priv was NULL, continuing",
00544                                __func__);
00545                         ret = SR_ERR_BUG;
00546                         continue;
00547                 }
00548                 /* TODO: Check for serial != NULL. */
00549                 if (ctx->serial->fd != -1)
00550                         serial_close(ctx->serial->fd);
00551                 sr_serial_dev_inst_free(ctx->serial);
00552                 sr_dev_inst_free(sdi);
00553         }
00554         g_slist_free(dev_insts);
00555         dev_insts = NULL;
00556 
00557         return ret;
00558 }
00559 
00560 static void *hw_dev_info_get(int dev_index, int dev_info_id)
00561 {
00562         struct sr_dev_inst *sdi;
00563         struct context *ctx;
00564         void *info;
00565 
00566         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00567                 return NULL;
00568         ctx = sdi->priv;
00569 
00570         info = NULL;
00571         switch (dev_info_id) {
00572         case SR_DI_INST:
00573                 info = sdi;
00574                 break;
00575         case SR_DI_NUM_PROBES:
00576                 info = GINT_TO_POINTER(NUM_PROBES);
00577                 break;
00578         case SR_DI_PROBE_NAMES:
00579                 info = probe_names;
00580                 break;
00581         case SR_DI_SAMPLERATES:
00582                 info = &samplerates;
00583                 break;
00584         case SR_DI_TRIGGER_TYPES:
00585                 info = (char *)TRIGGER_TYPES;
00586                 break;
00587         case SR_DI_CUR_SAMPLERATE:
00588                 info = &ctx->cur_samplerate;
00589                 break;
00590         }
00591 
00592         return info;
00593 }
00594 
00595 static int hw_dev_status_get(int dev_index)
00596 {
00597         struct sr_dev_inst *sdi;
00598 
00599         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00600                 return SR_ST_NOT_FOUND;
00601 
00602         return sdi->status;
00603 }
00604 
00605 static int *hw_hwcap_get_all(void)
00606 {
00607         return hwcaps;
00608 }
00609 
00610 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
00611 {
00612         struct context *ctx;
00613 
00614         ctx = sdi->priv;
00615         if (ctx->max_samplerate) {
00616                 if (samplerate > ctx->max_samplerate)
00617                         return SR_ERR_SAMPLERATE;
00618         } else if (samplerate < samplerates.low || samplerate > samplerates.high)
00619                 return SR_ERR_SAMPLERATE;
00620 
00621         if (samplerate > CLOCK_RATE) {
00622                 ctx->flag_reg |= FLAG_DEMUX;
00623                 ctx->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
00624         } else {
00625                 ctx->flag_reg &= ~FLAG_DEMUX;
00626                 ctx->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
00627         }
00628 
00629         /* Calculate actual samplerate used and complain if it is different
00630          * from the requested.
00631          */
00632         ctx->cur_samplerate = CLOCK_RATE / (ctx->cur_samplerate_divider + 1);
00633         if (ctx->flag_reg & FLAG_DEMUX)
00634                 ctx->cur_samplerate *= 2;
00635         if (ctx->cur_samplerate != samplerate)
00636                 sr_err("ols: can't match samplerate %" PRIu64 ", using %"
00637                        PRIu64, samplerate, ctx->cur_samplerate);
00638 
00639         return SR_OK;
00640 }
00641 
00642 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
00643 {
00644         struct sr_dev_inst *sdi;
00645         struct context *ctx;
00646         int ret;
00647         uint64_t *tmp_u64;
00648 
00649         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00650                 return SR_ERR;
00651         ctx = sdi->priv;
00652 
00653         if (sdi->status != SR_ST_ACTIVE)
00654                 return SR_ERR;
00655 
00656         switch (hwcap) {
00657         case SR_HWCAP_SAMPLERATE:
00658                 ret = set_samplerate(sdi, *(uint64_t *)value);
00659                 break;
00660         case SR_HWCAP_PROBECONFIG:
00661                 ret = configure_probes(ctx, (GSList *)value);
00662                 break;
00663         case SR_HWCAP_LIMIT_SAMPLES:
00664                 tmp_u64 = value;
00665                 if (*tmp_u64 < MIN_NUM_SAMPLES)
00666                         return SR_ERR;
00667                 if (*tmp_u64 > ctx->max_samples)
00668                         sr_err("ols: sample limit exceeds hw max");
00669                 ctx->limit_samples = *tmp_u64;
00670                 sr_info("ols: sample limit %" PRIu64, ctx->limit_samples);
00671                 ret = SR_OK;
00672                 break;
00673         case SR_HWCAP_CAPTURE_RATIO:
00674                 ctx->capture_ratio = *(uint64_t *)value;
00675                 if (ctx->capture_ratio < 0 || ctx->capture_ratio > 100) {
00676                         ctx->capture_ratio = 0;
00677                         ret = SR_ERR;
00678                 } else
00679                         ret = SR_OK;
00680                 break;
00681         case SR_HWCAP_RLE:
00682                 if (GPOINTER_TO_INT(value)) {
00683                         sr_info("ols: enabling RLE");
00684                         ctx->flag_reg |= FLAG_RLE;
00685                 }
00686                 ret = SR_OK;
00687                 break;
00688         default:
00689                 ret = SR_ERR;
00690         }
00691 
00692         return ret;
00693 }
00694 
00695 static int receive_data(int fd, int revents, void *cb_data)
00696 {
00697         struct sr_datafeed_packet packet;
00698         struct sr_datafeed_logic logic;
00699         struct sr_dev_inst *sdi;
00700         struct context *ctx;
00701         GSList *l;
00702         int num_channels, offset, i, j;
00703         unsigned char byte;
00704 
00705         /* Find this device's ctx struct by its fd. */
00706         ctx = NULL;
00707         for (l = dev_insts; l; l = l->next) {
00708                 sdi = l->data;
00709                 ctx = sdi->priv;
00710                 if (ctx->serial->fd == fd) {
00711                         break;
00712                 }
00713                 ctx = NULL;
00714         }
00715         if (!ctx)
00716                 /* Shouldn't happen. */
00717                 return TRUE;
00718 
00719         if (ctx->num_transfers++ == 0) {
00720                 /*
00721                  * First time round, means the device started sending data,
00722                  * and will not stop until done. If it stops sending for
00723                  * longer than it takes to send a byte, that means it's
00724                  * finished. We'll double that to 30ms to be sure...
00725                  */
00726                 sr_source_remove(fd);
00727                 sr_source_add(fd, G_IO_IN, 30, receive_data, cb_data);
00728                 ctx->raw_sample_buf = g_try_malloc(ctx->limit_samples * 4);
00729                 if (!ctx->raw_sample_buf) {
00730                         sr_err("ols: %s: ctx->raw_sample_buf malloc failed",
00731                                __func__);
00732                         return FALSE;
00733                 }
00734                 /* fill with 1010... for debugging */
00735                 memset(ctx->raw_sample_buf, 0x82, ctx->limit_samples * 4);
00736         }
00737 
00738         num_channels = 0;
00739         for (i = 0x20; i > 0x02; i /= 2) {
00740                 if ((ctx->flag_reg & i) == 0)
00741                         num_channels++;
00742         }
00743 
00744         if (revents == G_IO_IN) {
00745                 if (serial_read(fd, &byte, 1) != 1)
00746                         return FALSE;
00747 
00748                 /* Ignore it if we've read enough. */
00749                 if (ctx->num_samples >= ctx->limit_samples)
00750                         return TRUE;
00751 
00752                 ctx->sample[ctx->num_bytes++] = byte;
00753                 sr_dbg("ols: received byte 0x%.2x", byte);
00754                 if (ctx->num_bytes == num_channels) {
00755                         /* Got a full sample. */
00756                         sr_dbg("ols: received sample 0x%.*x",
00757                                ctx->num_bytes * 2, *(int *)ctx->sample);
00758                         if (ctx->flag_reg & FLAG_RLE) {
00759                                 /*
00760                                  * In RLE mode -1 should never come in as a
00761                                  * sample, because bit 31 is the "count" flag.
00762                                  */
00763                                 if (ctx->sample[ctx->num_bytes - 1] & 0x80) {
00764                                         ctx->sample[ctx->num_bytes - 1] &= 0x7f;
00765                                         /*
00766                                          * FIXME: This will only work on
00767                                          * little-endian systems.
00768                                          */
00769                                         ctx->rle_count = *(int *)(ctx->sample);
00770                                         sr_dbg("ols: RLE count = %d", ctx->rle_count);
00771                                         ctx->num_bytes = 0;
00772                                         return TRUE;
00773                                 }
00774                         }
00775                         ctx->num_samples += ctx->rle_count + 1;
00776                         if (ctx->num_samples > ctx->limit_samples) {
00777                                 /* Save us from overrunning the buffer. */
00778                                 ctx->rle_count -= ctx->num_samples - ctx->limit_samples;
00779                                 ctx->num_samples = ctx->limit_samples;
00780                         }
00781 
00782                         if (num_channels < 4) {
00783                                 /*
00784                                  * Some channel groups may have been turned
00785                                  * off, to speed up transfer between the
00786                                  * hardware and the PC. Expand that here before
00787                                  * submitting it over the session bus --
00788                                  * whatever is listening on the bus will be
00789                                  * expecting a full 32-bit sample, based on
00790                                  * the number of probes.
00791                                  */
00792                                 j = 0;
00793                                 memset(ctx->tmp_sample, 0, 4);
00794                                 for (i = 0; i < 4; i++) {
00795                                         if (((ctx->flag_reg >> 2) & (1 << i)) == 0) {
00796                                                 /*
00797                                                  * This channel group was
00798                                                  * enabled, copy from received
00799                                                  * sample.
00800                                                  */
00801                                                 ctx->tmp_sample[i] = ctx->sample[j++];
00802                                         }
00803                                 }
00804                                 memcpy(ctx->sample, ctx->tmp_sample, 4);
00805                                 sr_dbg("ols: full sample 0x%.8x", *(int *)ctx->sample);
00806                         }
00807 
00808                         /* the OLS sends its sample buffer backwards.
00809                          * store it in reverse order here, so we can dump
00810                          * this on the session bus later.
00811                          */
00812                         offset = (ctx->limit_samples - ctx->num_samples) * 4;
00813                         for (i = 0; i <= ctx->rle_count; i++) {
00814                                 memcpy(ctx->raw_sample_buf + offset + (i * 4),
00815                                        ctx->sample, 4);
00816                         }
00817                         memset(ctx->sample, 0, 4);
00818                         ctx->num_bytes = 0;
00819                         ctx->rle_count = 0;
00820                 }
00821         } else {
00822                 /*
00823                  * This is the main loop telling us a timeout was reached, or
00824                  * we've acquired all the samples we asked for -- we're done.
00825                  * Send the (properly-ordered) buffer to the frontend.
00826                  */
00827                 if (ctx->trigger_at != -1) {
00828                         /* a trigger was set up, so we need to tell the frontend
00829                          * about it.
00830                          */
00831                         if (ctx->trigger_at > 0) {
00832                                 /* there are pre-trigger samples, send those first */
00833                                 packet.type = SR_DF_LOGIC;
00834                                 packet.payload = &logic;
00835                                 logic.length = ctx->trigger_at * 4;
00836                                 logic.unitsize = 4;
00837                                 logic.data = ctx->raw_sample_buf +
00838                                         (ctx->limit_samples - ctx->num_samples) * 4;
00839                                 sr_session_send(cb_data, &packet);
00840                         }
00841 
00842                         /* send the trigger */
00843                         packet.type = SR_DF_TRIGGER;
00844                         sr_session_send(cb_data, &packet);
00845 
00846                         /* send post-trigger samples */
00847                         packet.type = SR_DF_LOGIC;
00848                         packet.payload = &logic;
00849                         logic.length = (ctx->num_samples * 4) - (ctx->trigger_at * 4);
00850                         logic.unitsize = 4;
00851                         logic.data = ctx->raw_sample_buf + ctx->trigger_at * 4 +
00852                                 (ctx->limit_samples - ctx->num_samples) * 4;
00853                         sr_session_send(cb_data, &packet);
00854                 } else {
00855                         /* no trigger was used */
00856                         packet.type = SR_DF_LOGIC;
00857                         packet.payload = &logic;
00858                         logic.length = ctx->num_samples * 4;
00859                         logic.unitsize = 4;
00860                         logic.data = ctx->raw_sample_buf +
00861                                 (ctx->limit_samples - ctx->num_samples) * 4;
00862                         sr_session_send(cb_data, &packet);
00863                 }
00864                 g_free(ctx->raw_sample_buf);
00865 
00866                 serial_flush(fd);
00867                 serial_close(fd);
00868                 packet.type = SR_DF_END;
00869                 sr_session_send(cb_data, &packet);
00870         }
00871 
00872         return TRUE;
00873 }
00874 
00875 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
00876 {
00877         struct sr_datafeed_packet *packet;
00878         struct sr_datafeed_header *header;
00879         struct sr_dev_inst *sdi;
00880         struct context *ctx;
00881         uint32_t trigger_config[4];
00882         uint32_t data;
00883         uint16_t readcount, delaycount;
00884         uint8_t changrp_mask;
00885         int num_channels;
00886         int i;
00887 
00888         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00889                 return SR_ERR;
00890 
00891         ctx = sdi->priv;
00892 
00893         if (sdi->status != SR_ST_ACTIVE)
00894                 return SR_ERR;
00895 
00896         /*
00897          * Enable/disable channel groups in the flag register according to the
00898          * probe mask. Calculate this here, because num_channels is needed
00899          * to limit readcount.
00900          */
00901         changrp_mask = 0;
00902         num_channels = 0;
00903         for (i = 0; i < 4; i++) {
00904                 if (ctx->probe_mask & (0xff << (i * 8))) {
00905                         changrp_mask |= (1 << i);
00906                         num_channels++;
00907                 }
00908         }
00909 
00910         /*
00911          * Limit readcount to prevent reading past the end of the hardware
00912          * buffer.
00913          */
00914         readcount = MIN(ctx->max_samples / num_channels, ctx->limit_samples) / 4;
00915 
00916         memset(trigger_config, 0, 16);
00917         trigger_config[ctx->num_stages - 1] |= 0x08;
00918         if (ctx->trigger_mask[0]) {
00919                 delaycount = readcount * (1 - ctx->capture_ratio / 100.0);
00920                 ctx->trigger_at = (readcount - delaycount) * 4 - ctx->num_stages;
00921 
00922                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_MASK_0,
00923                         reverse32(ctx->trigger_mask[0])) != SR_OK)
00924                         return SR_ERR;
00925                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_VALUE_0,
00926                         reverse32(ctx->trigger_value[0])) != SR_OK)
00927                         return SR_ERR;
00928                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
00929                         trigger_config[0]) != SR_OK)
00930                         return SR_ERR;
00931 
00932                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_MASK_1,
00933                         reverse32(ctx->trigger_mask[1])) != SR_OK)
00934                         return SR_ERR;
00935                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_VALUE_1,
00936                         reverse32(ctx->trigger_value[1])) != SR_OK)
00937                         return SR_ERR;
00938                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
00939                         trigger_config[1]) != SR_OK)
00940                         return SR_ERR;
00941 
00942                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_MASK_2,
00943                         reverse32(ctx->trigger_mask[2])) != SR_OK)
00944                         return SR_ERR;
00945                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_VALUE_2,
00946                         reverse32(ctx->trigger_value[2])) != SR_OK)
00947                         return SR_ERR;
00948                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
00949                         trigger_config[2]) != SR_OK)
00950                         return SR_ERR;
00951 
00952                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_MASK_3,
00953                         reverse32(ctx->trigger_mask[3])) != SR_OK)
00954                         return SR_ERR;
00955                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_VALUE_3,
00956                         reverse32(ctx->trigger_value[3])) != SR_OK)
00957                         return SR_ERR;
00958                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
00959                         trigger_config[3]) != SR_OK)
00960                         return SR_ERR;
00961         } else {
00962                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_MASK_0,
00963                                 ctx->trigger_mask[0]) != SR_OK)
00964                         return SR_ERR;
00965                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_VALUE_0,
00966                                 ctx->trigger_value[0]) != SR_OK)
00967                         return SR_ERR;
00968                 if (send_longcommand(ctx->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
00969                      0x00000008) != SR_OK)
00970                         return SR_ERR;
00971                 delaycount = readcount;
00972         }
00973 
00974         sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
00975                 "demux %s)", ctx->cur_samplerate, ctx->cur_samplerate_divider,
00976                 ctx->flag_reg & FLAG_DEMUX ? "on" : "off");
00977         if (send_longcommand(ctx->serial->fd, CMD_SET_DIVIDER,
00978                         reverse32(ctx->cur_samplerate_divider)) != SR_OK)
00979                 return SR_ERR;
00980 
00981         /* Send sample limit and pre/post-trigger capture ratio. */
00982         data = ((readcount - 1) & 0xffff) << 16;
00983         data |= (delaycount - 1) & 0xffff;
00984         if (send_longcommand(ctx->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
00985                 return SR_ERR;
00986 
00987         /* The flag register wants them here, and 1 means "disable channel". */
00988         ctx->flag_reg |= ~(changrp_mask << 2) & 0x3c;
00989         ctx->flag_reg |= FLAG_FILTER;
00990         ctx->rle_count = 0;
00991         data = (ctx->flag_reg << 24) | ((ctx->flag_reg << 8) & 0xff0000);
00992         if (send_longcommand(ctx->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
00993                 return SR_ERR;
00994 
00995         /* Start acquisition on the device. */
00996         if (send_shortcommand(ctx->serial->fd, CMD_RUN) != SR_OK)
00997                 return SR_ERR;
00998 
00999         sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data,
01000                       cb_data);
01001 
01002         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
01003                 sr_err("ols: %s: packet malloc failed", __func__);
01004                 return SR_ERR_MALLOC;
01005         }
01006 
01007         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
01008                 sr_err("ols: %s: header malloc failed", __func__);
01009                 g_free(packet);
01010                 return SR_ERR_MALLOC;
01011         }
01012 
01013         /* Send header packet to the session bus. */
01014         packet->type = SR_DF_HEADER;
01015         packet->payload = (unsigned char *)header;
01016         header->feed_version = 1;
01017         gettimeofday(&header->starttime, NULL);
01018         header->samplerate = ctx->cur_samplerate;
01019         header->num_logic_probes = NUM_PROBES;
01020         sr_session_send(cb_data, packet);
01021 
01022         g_free(header);
01023         g_free(packet);
01024 
01025         return SR_OK;
01026 }
01027 
01028 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
01029 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
01030 {
01031         struct sr_datafeed_packet packet;
01032 
01033         /* Avoid compiler warnings. */
01034         (void)dev_index;
01035 
01036         packet.type = SR_DF_END;
01037         sr_session_send(cb_data, &packet);
01038 
01039         return SR_OK;
01040 }
01041 
01042 SR_PRIV struct sr_dev_driver ols_driver_info = {
01043         .name = "ols",
01044         .longname = "Openbench Logic Sniffer",
01045         .api_version = 1,
01046         .init = hw_init,
01047         .cleanup = hw_cleanup,
01048         .dev_open = hw_dev_open,
01049         .dev_close = hw_dev_close,
01050         .dev_info_get = hw_dev_info_get,
01051         .dev_status_get = hw_dev_status_get,
01052         .hwcap_get_all = hw_hwcap_get_all,
01053         .dev_config_set = hw_dev_config_set,
01054         .dev_acquisition_start = hw_dev_acquisition_start,
01055         .dev_acquisition_stop = hw_dev_acquisition_stop,
01056 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines