]> sigrok.org Git - libsigrok.git/blob - hardware/openbench-logic-sniffer/ols.c
ols: if no serial port specified, this driver is not used.
[libsigrok.git] / hardware / openbench-logic-sniffer / ols.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #ifdef _WIN32
28 #include <windows.h>
29 #else
30 #include <termios.h>
31 #endif
32 #include <string.h>
33 #include <sys/time.h>
34 #include <inttypes.h>
35 #ifdef _WIN32
36 /* TODO */
37 #else
38 #include <arpa/inet.h>
39 #endif
40 #include <glib.h>
41 #include "libsigrok.h"
42 #include "libsigrok-internal.h"
43 #include "ols.h"
44
45 #ifdef _WIN32
46 #define O_NONBLOCK FIONBIO
47 #endif
48
49 #define SERIALCOMM "115200/8n1"
50
51 static const int hwcaps[] = {
52         SR_HWCAP_LOGIC_ANALYZER,
53         SR_HWCAP_SAMPLERATE,
54         SR_HWCAP_CAPTURE_RATIO,
55         SR_HWCAP_LIMIT_SAMPLES,
56         SR_HWCAP_RLE,
57         0,
58 };
59
60 /* Probes are numbered 0-31 (on the PCB silkscreen). */
61 static const char *probe_names[NUM_PROBES + 1] = {
62         "0",
63         "1",
64         "2",
65         "3",
66         "4",
67         "5",
68         "6",
69         "7",
70         "8",
71         "9",
72         "10",
73         "11",
74         "12",
75         "13",
76         "14",
77         "15",
78         "16",
79         "17",
80         "18",
81         "19",
82         "20",
83         "21",
84         "22",
85         "23",
86         "24",
87         "25",
88         "26",
89         "27",
90         "28",
91         "29",
92         "30",
93         "31",
94         NULL,
95 };
96
97 /* default supported samplerates, can be overridden by device metadata */
98 static const struct sr_samplerates samplerates = {
99         SR_HZ(10),
100         SR_MHZ(200),
101         SR_HZ(1),
102         NULL,
103 };
104
105 SR_PRIV struct sr_dev_driver ols_driver_info;
106 static struct sr_dev_driver *odi = &ols_driver_info;
107
108 static int send_shortcommand(struct sr_serial_dev_inst *serial,
109                 uint8_t command)
110 {
111         char buf[1];
112
113         sr_dbg("ols: sending cmd 0x%.2x", command);
114         buf[0] = command;
115         if (serial_write(serial, buf, 1) != 1)
116                 return SR_ERR;
117
118         return SR_OK;
119 }
120
121 static int send_longcommand(struct sr_serial_dev_inst *serial,
122                 uint8_t command, uint32_t data)
123 {
124         char buf[5];
125
126         sr_dbg("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
127         buf[0] = command;
128         buf[1] = (data & 0xff000000) >> 24;
129         buf[2] = (data & 0xff0000) >> 16;
130         buf[3] = (data & 0xff00) >> 8;
131         buf[4] = data & 0xff;
132         if (serial_write(serial, buf, 5) != 5)
133                 return SR_ERR;
134
135         return SR_OK;
136 }
137
138 static int configure_probes(const struct sr_dev_inst *sdi)
139 {
140         struct dev_context *devc;
141         const struct sr_probe *probe;
142         const GSList *l;
143         int probe_bit, stage, i;
144         char *tc;
145
146         devc = sdi->priv;
147
148         devc->probe_mask = 0;
149         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
150                 devc->trigger_mask[i] = 0;
151                 devc->trigger_value[i] = 0;
152         }
153
154         devc->num_stages = 0;
155         for (l = sdi->probes; l; l = l->next) {
156                 probe = (const struct sr_probe *)l->data;
157                 if (!probe->enabled)
158                         continue;
159
160                 /*
161                  * Set up the probe mask for later configuration into the
162                  * flag register.
163                  */
164                 probe_bit = 1 << (probe->index);
165                 devc->probe_mask |= probe_bit;
166
167                 if (!probe->trigger)
168                         continue;
169
170                 /* Configure trigger mask and value. */
171                 stage = 0;
172                 for (tc = probe->trigger; tc && *tc; tc++) {
173                         devc->trigger_mask[stage] |= probe_bit;
174                         if (*tc == '1')
175                                 devc->trigger_value[stage] |= probe_bit;
176                         stage++;
177                         if (stage > 3)
178                                 /*
179                                  * TODO: Only supporting parallel mode, with
180                                  * up to 4 stages.
181                                  */
182                                 return SR_ERR;
183                 }
184                 if (stage > devc->num_stages)
185                         devc->num_stages = stage;
186         }
187
188         return SR_OK;
189 }
190
191 static uint32_t reverse16(uint32_t in)
192 {
193         uint32_t out;
194
195         out = (in & 0xff) << 8;
196         out |= (in & 0xff00) >> 8;
197         out |= (in & 0xff0000) << 8;
198         out |= (in & 0xff000000) >> 8;
199
200         return out;
201 }
202
203 static uint32_t reverse32(uint32_t in)
204 {
205         uint32_t out;
206
207         out = (in & 0xff) << 24;
208         out |= (in & 0xff00) << 8;
209         out |= (in & 0xff0000) >> 8;
210         out |= (in & 0xff000000) >> 24;
211
212         return out;
213 }
214
215 static struct dev_context *ols_dev_new(void)
216 {
217         struct dev_context *devc;
218
219         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
220                 sr_err("ols: %s: devc malloc failed", __func__);
221                 return NULL;
222         }
223
224         devc->trigger_at = -1;
225         devc->probe_mask = 0xffffffff;
226         devc->cur_samplerate = SR_KHZ(200);
227         devc->serial = NULL;
228
229         return devc;
230 }
231
232 static struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
233 {
234         struct sr_dev_inst *sdi;
235         struct dev_context *devc;
236         struct sr_probe *probe;
237         uint32_t tmp_int, ui;
238         uint8_t key, type, token;
239         GString *tmp_str, *devname, *version;
240         guchar tmp_c;
241
242         sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
243         sdi->driver = odi;
244         devc = ols_dev_new();
245         sdi->priv = devc;
246
247         devname = g_string_new("");
248         version = g_string_new("");
249
250         key = 0xff;
251         while (key) {
252                 if (serial_read(serial, &key, 1) != 1 || key == 0x00)
253                         break;
254                 type = key >> 5;
255                 token = key & 0x1f;
256                 switch (type) {
257                 case 0:
258                         /* NULL-terminated string */
259                         tmp_str = g_string_new("");
260                         while (serial_read(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
261                                 g_string_append_c(tmp_str, tmp_c);
262                         sr_dbg("ols: got metadata key 0x%.2x value '%s'",
263                                key, tmp_str->str);
264                         switch (token) {
265                         case 0x01:
266                                 /* Device name */
267                                 devname = g_string_append(devname, tmp_str->str);
268                                 break;
269                         case 0x02:
270                                 /* FPGA firmware version */
271                                 if (version->len)
272                                         g_string_append(version, ", ");
273                                 g_string_append(version, "FPGA version ");
274                                 g_string_append(version, tmp_str->str);
275                                 break;
276                         case 0x03:
277                                 /* Ancillary version */
278                                 if (version->len)
279                                         g_string_append(version, ", ");
280                                 g_string_append(version, "Ancillary version ");
281                                 g_string_append(version, tmp_str->str);
282                                 break;
283                         default:
284                                 sr_info("ols: unknown token 0x%.2x: '%s'",
285                                         token, tmp_str->str);
286                                 break;
287                         }
288                         g_string_free(tmp_str, TRUE);
289                         break;
290                 case 1:
291                         /* 32-bit unsigned integer */
292                         if (serial_read(serial, &tmp_int, 4) != 4)
293                                 break;
294                         tmp_int = reverse32(tmp_int);
295                         sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x",
296                                key, tmp_int);
297                         switch (token) {
298                         case 0x00:
299                                 /* Number of usable probes */
300                                 for (ui = 0; ui < tmp_int; ui++) {
301                                         if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
302                                                         probe_names[ui])))
303                                                 return 0;
304                                         sdi->probes = g_slist_append(sdi->probes, probe);
305                                 }
306                                 break;
307                         case 0x01:
308                                 /* Amount of sample memory available (bytes) */
309                                 devc->max_samples = tmp_int;
310                                 break;
311                         case 0x02:
312                                 /* Amount of dynamic memory available (bytes) */
313                                 /* what is this for? */
314                                 break;
315                         case 0x03:
316                                 /* Maximum sample rate (hz) */
317                                 devc->max_samplerate = tmp_int;
318                                 break;
319                         case 0x04:
320                                 /* protocol version */
321                                 devc->protocol_version = tmp_int;
322                                 break;
323                         default:
324                                 sr_info("ols: unknown token 0x%.2x: 0x%.8x",
325                                         token, tmp_int);
326                                 break;
327                         }
328                         break;
329                 case 2:
330                         /* 8-bit unsigned integer */
331                         if (serial_read(serial, &tmp_c, 1) != 1)
332                                 break;
333                         sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x",
334                                key, tmp_c);
335                         switch (token) {
336                         case 0x00:
337                                 /* Number of usable probes */
338                                 for (ui = 0; ui < tmp_c; ui++) {
339                                         if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
340                                                         probe_names[ui])))
341                                                 return 0;
342                                         sdi->probes = g_slist_append(sdi->probes, probe);
343                                 }
344                                 break;
345                         case 0x01:
346                                 /* protocol version */
347                                 devc->protocol_version = tmp_c;
348                                 break;
349                         default:
350                                 sr_info("ols: unknown token 0x%.2x: 0x%.2x",
351                                         token, tmp_c);
352                                 break;
353                         }
354                         break;
355                 default:
356                         /* unknown type */
357                         break;
358                 }
359         }
360
361         sdi->model = devname->str;
362         sdi->version = version->str;
363         g_string_free(devname, FALSE);
364         g_string_free(version, FALSE);
365
366         return sdi;
367 }
368
369 static int hw_init(void)
370 {
371         struct drv_context *drvc;
372
373         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
374                 sr_err("ols: driver context malloc failed.");
375                 return SR_ERR_MALLOC;
376         }
377         odi->priv = drvc;
378
379         return SR_OK;
380 }
381
382 static GSList *hw_scan(GSList *options)
383 {
384         struct sr_hwopt *opt;
385         struct sr_dev_inst *sdi;
386         struct drv_context *drvc;
387         struct dev_context *devc;
388         struct sr_probe *probe;
389         struct sr_serial_dev_inst *serial;
390         GPollFD probefd;
391         GSList *l, *devices;
392         int ret, i;
393         const char *conn, *serialcomm;
394         char buf[8];
395
396         (void)options;
397         drvc = odi->priv;
398         devices = NULL;
399
400         conn = serialcomm = NULL;
401         for (l = options; l; l = l->next) {
402                 opt = l->data;
403                 switch (opt->hwopt) {
404                 case SR_HWOPT_CONN:
405                         conn = opt->value;
406                         break;
407                 case SR_HWOPT_SERIALCOMM:
408                         serialcomm = opt->value;
409                         break;
410                 }
411         }
412         if (!conn)
413                 return NULL;
414
415         if (serialcomm == NULL)
416                 serialcomm = SERIALCOMM;
417
418         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
419                 return NULL;
420
421         /* The discovery procedure is like this: first send the Reset
422          * command (0x00) 5 times, since the device could be anywhere
423          * in a 5-byte command. Then send the ID command (0x02).
424          * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
425          * have a match.
426          */
427         sr_info("ols: probing %s .", conn);
428         if (serial_open(serial, O_RDWR | O_NONBLOCK) != SR_OK)
429                 return NULL;
430
431         ret = SR_OK;
432         for (i = 0; i < 5; i++) {
433                 if ((ret = send_shortcommand(serial, CMD_RESET)) != SR_OK) {
434                         sr_err("ols: port %s is not writable.", conn);
435                         break;
436                 }
437         }
438         if (ret != SR_OK) {
439                 serial_close(serial);
440                 sr_err("ols: Could not use port %s. Quitting.", conn);
441                 return NULL;
442         }
443         send_shortcommand(serial, CMD_ID);
444
445         /* Wait 10ms for a response. */
446         usleep(10000);
447
448         probefd.fd = serial->fd;
449         probefd.events = G_IO_IN;
450         g_poll(&probefd, 1, 1);
451
452         if (probefd.revents != G_IO_IN)
453                 return NULL;
454         if (serial_read(serial, buf, 4) != 4)
455                 return NULL;
456         if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
457                 return NULL;
458
459         /* Definitely using the OLS protocol, check if it supports
460          * the metadata command.
461          */
462         send_shortcommand(serial, CMD_METADATA);
463         if (g_poll(&probefd, 1, 10) > 0) {
464                 /* Got metadata. */
465                 sdi = get_metadata(serial);
466                 sdi->index = 0;
467                 devc = sdi->priv;
468         } else {
469                 /* Not an OLS -- some other board that uses the sump protocol. */
470                 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
471                                 "Sump", "Logic Analyzer", "v1.0");
472                 sdi->driver = odi;
473                 devc = ols_dev_new();
474                 for (i = 0; i < 32; i++) {
475                         if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
476                                         probe_names[i])))
477                                 return 0;
478                         sdi->probes = g_slist_append(sdi->probes, probe);
479                 }
480                 sdi->priv = devc;
481         }
482         devc->serial = serial;
483         drvc->instances = g_slist_append(drvc->instances, sdi);
484         devices = g_slist_append(devices, sdi);
485
486         serial_close(serial);
487
488         return devices;
489 }
490
491 static GSList *hw_dev_list(void)
492 {
493         struct drv_context *drvc;
494
495         drvc = odi->priv;
496
497         return drvc->instances;
498 }
499
500 static int hw_dev_open(struct sr_dev_inst *sdi)
501 {
502         struct dev_context *devc;
503
504         devc = sdi->priv;
505
506         if (serial_open(devc->serial, O_RDWR) != SR_OK)
507                 return SR_ERR;
508
509         sdi->status = SR_ST_ACTIVE;
510
511         return SR_OK;
512 }
513
514 static int hw_dev_close(struct sr_dev_inst *sdi)
515 {
516         struct dev_context *devc;
517
518         devc = sdi->priv;
519
520         if (devc->serial && devc->serial->fd != -1) {
521                 serial_close(devc->serial);
522                 sdi->status = SR_ST_INACTIVE;
523         }
524
525         return SR_OK;
526 }
527
528 static int hw_cleanup(void)
529 {
530         GSList *l;
531         struct sr_dev_inst *sdi;
532         struct drv_context *drvc;
533         struct dev_context *devc;
534         int ret = SR_OK;
535
536         if (!(drvc = odi->priv))
537                 return SR_OK;
538
539         /* Properly close and free all devices. */
540         for (l = drvc->instances; l; l = l->next) {
541                 if (!(sdi = l->data)) {
542                         /* Log error, but continue cleaning up the rest. */
543                         sr_err("ols: %s: sdi was NULL, continuing", __func__);
544                         ret = SR_ERR_BUG;
545                         continue;
546                 }
547                 if (!(devc = sdi->priv)) {
548                         /* Log error, but continue cleaning up the rest. */
549                         sr_err("ols: %s: sdi->priv was NULL, continuing",
550                                __func__);
551                         ret = SR_ERR_BUG;
552                         continue;
553                 }
554                 hw_dev_close(sdi);
555                 sr_serial_dev_inst_free(devc->serial);
556                 sr_dev_inst_free(sdi);
557         }
558         g_slist_free(drvc->instances);
559         drvc->instances = NULL;
560
561         return ret;
562 }
563
564 static int hw_info_get(int info_id, const void **data,
565        const struct sr_dev_inst *sdi)
566 {
567         struct dev_context *devc;
568
569         switch (info_id) {
570         case SR_DI_HWCAPS:
571                 *data = hwcaps;
572                 break;
573         case SR_DI_NUM_PROBES:
574                 *data = GINT_TO_POINTER(1);
575                 break;
576         case SR_DI_PROBE_NAMES:
577                 *data = probe_names;
578                 break;
579         case SR_DI_SAMPLERATES:
580                 *data = &samplerates;
581                 break;
582         case SR_DI_TRIGGER_TYPES:
583                 *data = (char *)TRIGGER_TYPES;
584                 break;
585         case SR_DI_CUR_SAMPLERATE:
586                 if (sdi) {
587                         devc = sdi->priv;
588                         *data = &devc->cur_samplerate;
589                 } else
590                         return SR_ERR;
591                 break;
592         default:
593                 return SR_ERR_ARG;
594         }
595
596         return SR_OK;
597 }
598
599 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
600 {
601         struct dev_context *devc;
602
603         devc = sdi->priv;
604         if (devc->max_samplerate) {
605                 if (samplerate > devc->max_samplerate)
606                         return SR_ERR_SAMPLERATE;
607         } else if (samplerate < samplerates.low || samplerate > samplerates.high)
608                 return SR_ERR_SAMPLERATE;
609
610         if (samplerate > CLOCK_RATE) {
611                 devc->flag_reg |= FLAG_DEMUX;
612                 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
613         } else {
614                 devc->flag_reg &= ~FLAG_DEMUX;
615                 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
616         }
617
618         /* Calculate actual samplerate used and complain if it is different
619          * from the requested.
620          */
621         devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
622         if (devc->flag_reg & FLAG_DEMUX)
623                 devc->cur_samplerate *= 2;
624         if (devc->cur_samplerate != samplerate)
625                 sr_err("ols: can't match samplerate %" PRIu64 ", using %"
626                        PRIu64, samplerate, devc->cur_samplerate);
627
628         return SR_OK;
629 }
630
631 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
632                 const void *value)
633 {
634         struct dev_context *devc;
635         int ret;
636         const uint64_t *tmp_u64;
637
638         devc = sdi->priv;
639
640         if (sdi->status != SR_ST_ACTIVE)
641                 return SR_ERR;
642
643         switch (hwcap) {
644         case SR_HWCAP_SAMPLERATE:
645                 ret = set_samplerate(sdi, *(const uint64_t *)value);
646                 break;
647         case SR_HWCAP_LIMIT_SAMPLES:
648                 tmp_u64 = value;
649                 if (*tmp_u64 < MIN_NUM_SAMPLES)
650                         return SR_ERR;
651                 if (*tmp_u64 > devc->max_samples)
652                         sr_err("ols: sample limit exceeds hw max");
653                 devc->limit_samples = *tmp_u64;
654                 sr_info("ols: sample limit %" PRIu64, devc->limit_samples);
655                 ret = SR_OK;
656                 break;
657         case SR_HWCAP_CAPTURE_RATIO:
658                 devc->capture_ratio = *(const uint64_t *)value;
659                 if (devc->capture_ratio < 0 || devc->capture_ratio > 100) {
660                         devc->capture_ratio = 0;
661                         ret = SR_ERR;
662                 } else
663                         ret = SR_OK;
664                 break;
665         case SR_HWCAP_RLE:
666                 if (GPOINTER_TO_INT(value)) {
667                         sr_info("ols: enabling RLE");
668                         devc->flag_reg |= FLAG_RLE;
669                 }
670                 ret = SR_OK;
671                 break;
672         default:
673                 ret = SR_ERR;
674         }
675
676         return ret;
677 }
678
679 static void abort_acquisition(const struct sr_dev_inst *sdi)
680 {
681         struct sr_datafeed_packet packet;
682         struct dev_context *devc;
683
684         devc = sdi->priv;
685         sr_source_remove(devc->serial->fd);
686
687         /* Terminate session */
688         packet.type = SR_DF_END;
689         sr_session_send(sdi, &packet);
690
691 }
692
693
694
695 static int receive_data(int fd, int revents, void *cb_data)
696 {
697         struct sr_datafeed_packet packet;
698         struct sr_datafeed_logic logic;
699         struct sr_dev_inst *sdi;
700         struct drv_context *drvc;
701         struct dev_context *devc;
702         GSList *l;
703         int num_channels, offset, i, j;
704         unsigned char byte;
705
706         drvc = odi->priv;
707
708         /* Find this device's devc struct by its fd. */
709         devc = NULL;
710         for (l = drvc->instances; l; l = l->next) {
711                 sdi = l->data;
712                 devc = sdi->priv;
713                 if (devc->serial->fd == fd)
714                         break;
715                 devc = NULL;
716         }
717         if (!devc)
718                 /* Shouldn't happen. */
719                 return TRUE;
720
721         if (devc->num_transfers++ == 0) {
722                 /*
723                  * First time round, means the device started sending data,
724                  * and will not stop until done. If it stops sending for
725                  * longer than it takes to send a byte, that means it's
726                  * finished. We'll double that to 30ms to be sure...
727                  */
728                 sr_source_remove(fd);
729                 sr_source_add(fd, G_IO_IN, 30, receive_data, cb_data);
730                 /* TODO: Check malloc return code. */
731                 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
732                 if (!devc->raw_sample_buf) {
733                         sr_err("ols: %s: devc->raw_sample_buf malloc failed",
734                                __func__);
735                         return FALSE;
736                 }
737                 /* fill with 1010... for debugging */
738                 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
739         }
740
741         num_channels = 0;
742         for (i = 0x20; i > 0x02; i /= 2) {
743                 if ((devc->flag_reg & i) == 0)
744                         num_channels++;
745         }
746
747         if (revents == G_IO_IN) {
748                 if (serial_read(devc->serial, &byte, 1) != 1)
749                         return FALSE;
750
751                 /* Ignore it if we've read enough. */
752                 if (devc->num_samples >= devc->limit_samples)
753                         return TRUE;
754
755                 devc->sample[devc->num_bytes++] = byte;
756                 sr_dbg("ols: received byte 0x%.2x", byte);
757                 if (devc->num_bytes == num_channels) {
758                         /* Got a full sample. */
759                         sr_dbg("ols: received sample 0x%.*x",
760                                devc->num_bytes * 2, *(int *)devc->sample);
761                         if (devc->flag_reg & FLAG_RLE) {
762                                 /*
763                                  * In RLE mode -1 should never come in as a
764                                  * sample, because bit 31 is the "count" flag.
765                                  */
766                                 if (devc->sample[devc->num_bytes - 1] & 0x80) {
767                                         devc->sample[devc->num_bytes - 1] &= 0x7f;
768                                         /*
769                                          * FIXME: This will only work on
770                                          * little-endian systems.
771                                          */
772                                         devc->rle_count = *(int *)(devc->sample);
773                                         sr_dbg("ols: RLE count = %d", devc->rle_count);
774                                         devc->num_bytes = 0;
775                                         return TRUE;
776                                 }
777                         }
778                         devc->num_samples += devc->rle_count + 1;
779                         if (devc->num_samples > devc->limit_samples) {
780                                 /* Save us from overrunning the buffer. */
781                                 devc->rle_count -= devc->num_samples - devc->limit_samples;
782                                 devc->num_samples = devc->limit_samples;
783                         }
784
785                         if (num_channels < 4) {
786                                 /*
787                                  * Some channel groups may have been turned
788                                  * off, to speed up transfer between the
789                                  * hardware and the PC. Expand that here before
790                                  * submitting it over the session bus --
791                                  * whatever is listening on the bus will be
792                                  * expecting a full 32-bit sample, based on
793                                  * the number of probes.
794                                  */
795                                 j = 0;
796                                 memset(devc->tmp_sample, 0, 4);
797                                 for (i = 0; i < 4; i++) {
798                                         if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
799                                                 /*
800                                                  * This channel group was
801                                                  * enabled, copy from received
802                                                  * sample.
803                                                  */
804                                                 devc->tmp_sample[i] = devc->sample[j++];
805                                         }
806                                 }
807                                 memcpy(devc->sample, devc->tmp_sample, 4);
808                                 sr_dbg("ols: full sample 0x%.8x", *(int *)devc->sample);
809                         }
810
811                         /* the OLS sends its sample buffer backwards.
812                          * store it in reverse order here, so we can dump
813                          * this on the session bus later.
814                          */
815                         offset = (devc->limit_samples - devc->num_samples) * 4;
816                         for (i = 0; i <= devc->rle_count; i++) {
817                                 memcpy(devc->raw_sample_buf + offset + (i * 4),
818                                        devc->sample, 4);
819                         }
820                         memset(devc->sample, 0, 4);
821                         devc->num_bytes = 0;
822                         devc->rle_count = 0;
823                 }
824         } else {
825                 /*
826                  * This is the main loop telling us a timeout was reached, or
827                  * we've acquired all the samples we asked for -- we're done.
828                  * Send the (properly-ordered) buffer to the frontend.
829                  */
830                 if (devc->trigger_at != -1) {
831                         /* a trigger was set up, so we need to tell the frontend
832                          * about it.
833                          */
834                         if (devc->trigger_at > 0) {
835                                 /* there are pre-trigger samples, send those first */
836                                 packet.type = SR_DF_LOGIC;
837                                 packet.payload = &logic;
838                                 logic.length = devc->trigger_at * 4;
839                                 logic.unitsize = 4;
840                                 logic.data = devc->raw_sample_buf +
841                                         (devc->limit_samples - devc->num_samples) * 4;
842                                 sr_session_send(cb_data, &packet);
843                         }
844
845                         /* send the trigger */
846                         packet.type = SR_DF_TRIGGER;
847                         sr_session_send(cb_data, &packet);
848
849                         /* send post-trigger samples */
850                         packet.type = SR_DF_LOGIC;
851                         packet.payload = &logic;
852                         logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
853                         logic.unitsize = 4;
854                         logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
855                                 (devc->limit_samples - devc->num_samples) * 4;
856                         sr_session_send(cb_data, &packet);
857                 } else {
858                         /* no trigger was used */
859                         packet.type = SR_DF_LOGIC;
860                         packet.payload = &logic;
861                         logic.length = devc->num_samples * 4;
862                         logic.unitsize = 4;
863                         logic.data = devc->raw_sample_buf +
864                                 (devc->limit_samples - devc->num_samples) * 4;
865                         sr_session_send(cb_data, &packet);
866                 }
867                 g_free(devc->raw_sample_buf);
868
869                 serial_flush(devc->serial);
870                 abort_acquisition(sdi);
871                 serial_close(devc->serial);
872         }
873
874         return TRUE;
875 }
876
877 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
878                 void *cb_data)
879 {
880         struct sr_datafeed_packet *packet;
881         struct sr_datafeed_header *header;
882         struct sr_datafeed_meta_logic meta;
883         struct dev_context *devc;
884         uint32_t trigger_config[4];
885         uint32_t data;
886         uint16_t readcount, delaycount;
887         uint8_t changrp_mask;
888         int num_channels;
889         int i;
890
891         devc = sdi->priv;
892
893         if (sdi->status != SR_ST_ACTIVE)
894                 return SR_ERR;
895
896         if (configure_probes(sdi) != SR_OK) {
897                 sr_err("ols: failed to configured probes");
898                 return SR_ERR;
899         }
900
901         /*
902          * Enable/disable channel groups in the flag register according to the
903          * probe mask. Calculate this here, because num_channels is needed
904          * to limit readcount.
905          */
906         changrp_mask = 0;
907         num_channels = 0;
908         for (i = 0; i < 4; i++) {
909                 if (devc->probe_mask & (0xff << (i * 8))) {
910                         changrp_mask |= (1 << i);
911                         num_channels++;
912                 }
913         }
914
915         /*
916          * Limit readcount to prevent reading past the end of the hardware
917          * buffer.
918          */
919         readcount = MIN(devc->max_samples / num_channels, devc->limit_samples) / 4;
920
921         memset(trigger_config, 0, 16);
922         trigger_config[devc->num_stages - 1] |= 0x08;
923         if (devc->trigger_mask[0]) {
924                 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
925                 devc->trigger_at = (readcount - delaycount) * 4 - devc->num_stages;
926
927                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_0,
928                         reverse32(devc->trigger_mask[0])) != SR_OK)
929                         return SR_ERR;
930                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_0,
931                         reverse32(devc->trigger_value[0])) != SR_OK)
932                         return SR_ERR;
933                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_0,
934                         trigger_config[0]) != SR_OK)
935                         return SR_ERR;
936
937                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_1,
938                         reverse32(devc->trigger_mask[1])) != SR_OK)
939                         return SR_ERR;
940                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_1,
941                         reverse32(devc->trigger_value[1])) != SR_OK)
942                         return SR_ERR;
943                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_1,
944                         trigger_config[1]) != SR_OK)
945                         return SR_ERR;
946
947                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_2,
948                         reverse32(devc->trigger_mask[2])) != SR_OK)
949                         return SR_ERR;
950                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_2,
951                         reverse32(devc->trigger_value[2])) != SR_OK)
952                         return SR_ERR;
953                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_2,
954                         trigger_config[2]) != SR_OK)
955                         return SR_ERR;
956
957                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_3,
958                         reverse32(devc->trigger_mask[3])) != SR_OK)
959                         return SR_ERR;
960                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_3,
961                         reverse32(devc->trigger_value[3])) != SR_OK)
962                         return SR_ERR;
963                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_3,
964                         trigger_config[3]) != SR_OK)
965                         return SR_ERR;
966         } else {
967                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_MASK_0,
968                                 devc->trigger_mask[0]) != SR_OK)
969                         return SR_ERR;
970                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_VALUE_0,
971                                 devc->trigger_value[0]) != SR_OK)
972                         return SR_ERR;
973                 if (send_longcommand(devc->serial, CMD_SET_TRIGGER_CONFIG_0,
974                      0x00000008) != SR_OK)
975                         return SR_ERR;
976                 delaycount = readcount;
977         }
978
979         sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
980                 "demux %s)", devc->cur_samplerate, devc->cur_samplerate_divider,
981                 devc->flag_reg & FLAG_DEMUX ? "on" : "off");
982         if (send_longcommand(devc->serial, CMD_SET_DIVIDER,
983                         reverse32(devc->cur_samplerate_divider)) != SR_OK)
984                 return SR_ERR;
985
986         /* Send sample limit and pre/post-trigger capture ratio. */
987         data = ((readcount - 1) & 0xffff) << 16;
988         data |= (delaycount - 1) & 0xffff;
989         if (send_longcommand(devc->serial, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
990                 return SR_ERR;
991
992         /* The flag register wants them here, and 1 means "disable channel". */
993         devc->flag_reg |= ~(changrp_mask << 2) & 0x3c;
994         devc->flag_reg |= FLAG_FILTER;
995         devc->rle_count = 0;
996         data = (devc->flag_reg << 24) | ((devc->flag_reg << 8) & 0xff0000);
997         if (send_longcommand(devc->serial, CMD_SET_FLAGS, data) != SR_OK)
998                 return SR_ERR;
999
1000         /* Start acquisition on the device. */
1001         if (send_shortcommand(devc->serial, CMD_RUN) != SR_OK)
1002                 return SR_ERR;
1003
1004         sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data,
1005                       cb_data);
1006
1007         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
1008                 sr_err("ols: %s: packet malloc failed", __func__);
1009                 return SR_ERR_MALLOC;
1010         }
1011
1012         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
1013                 sr_err("ols: %s: header malloc failed", __func__);
1014                 g_free(packet);
1015                 return SR_ERR_MALLOC;
1016         }
1017
1018         /* Send header packet to the session bus. */
1019         packet->type = SR_DF_HEADER;
1020         packet->payload = (unsigned char *)header;
1021         header->feed_version = 1;
1022         gettimeofday(&header->starttime, NULL);
1023         sr_session_send(cb_data, packet);
1024
1025         /* Send metadata about the SR_DF_LOGIC packets to come. */
1026         packet->type = SR_DF_META_LOGIC;
1027         packet->payload = &meta;
1028         meta.samplerate = devc->cur_samplerate;
1029         meta.num_probes = NUM_PROBES;
1030         sr_session_send(cb_data, packet);
1031
1032         g_free(header);
1033         g_free(packet);
1034
1035         return SR_OK;
1036 }
1037
1038 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
1039 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
1040 {
1041         /* Avoid compiler warnings. */
1042         (void)cb_data;
1043
1044         abort_acquisition(sdi);
1045
1046         return SR_OK;
1047 }
1048
1049 SR_PRIV struct sr_dev_driver ols_driver_info = {
1050         .name = "ols",
1051         .longname = "Openbench Logic Sniffer",
1052         .api_version = 1,
1053         .init = hw_init,
1054         .cleanup = hw_cleanup,
1055         .scan = hw_scan,
1056         .dev_list = hw_dev_list,
1057         .dev_clear = hw_cleanup,
1058         .dev_open = hw_dev_open,
1059         .dev_close = hw_dev_close,
1060         .info_get = hw_info_get,
1061         .dev_config_set = hw_dev_config_set,
1062         .dev_acquisition_start = hw_dev_acquisition_start,
1063         .dev_acquisition_stop = hw_dev_acquisition_stop,
1064         .priv = NULL,
1065 };