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