]> sigrok.org Git - libsigrok.git/blob - hardware/openbench-logic-sniffer/ols.c
Add SR_HZ macro for consistency.
[libsigrok.git] / hardware / openbench-logic-sniffer / ols.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 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 <sigrok.h>
42 #include <sigrok-internal.h>
43
44 #ifdef _WIN32
45 #define O_NONBLOCK FIONBIO
46 #endif
47
48 #define NUM_PROBES             32
49 #define NUM_TRIGGER_STAGES     4
50 #define TRIGGER_TYPES          "01"
51 #define SERIAL_SPEED           B115200
52 #define CLOCK_RATE             SR_MHZ(100)
53 #define MIN_NUM_SAMPLES        4
54
55 /* Command opcodes */
56 #define CMD_RESET                  0x00
57 #define CMD_ID                     0x02
58 #define CMD_SET_FLAGS              0x82
59 #define CMD_SET_DIVIDER            0x80
60 #define CMD_RUN                    0x01
61 #define CMD_CAPTURE_SIZE           0x81
62 #define CMD_SET_TRIGGER_MASK_0     0xc0
63 #define CMD_SET_TRIGGER_MASK_1     0xc4
64 #define CMD_SET_TRIGGER_MASK_2     0xc8
65 #define CMD_SET_TRIGGER_MASK_3     0xcc
66 #define CMD_SET_TRIGGER_VALUE_0    0xc1
67 #define CMD_SET_TRIGGER_VALUE_1    0xc5
68 #define CMD_SET_TRIGGER_VALUE_2    0xc9
69 #define CMD_SET_TRIGGER_VALUE_3    0xcd
70 #define CMD_SET_TRIGGER_CONFIG_0   0xc2
71 #define CMD_SET_TRIGGER_CONFIG_1   0xc6
72 #define CMD_SET_TRIGGER_CONFIG_2   0xca
73 #define CMD_SET_TRIGGER_CONFIG_3   0xce
74
75 /* Bitmasks for CMD_FLAGS */
76 #define FLAG_DEMUX                 0x01
77 #define FLAG_FILTER                0x02
78 #define FLAG_CHANNELGROUP_1        0x04
79 #define FLAG_CHANNELGROUP_2        0x08
80 #define FLAG_CHANNELGROUP_3        0x10
81 #define FLAG_CHANNELGROUP_4        0x20
82 #define FLAG_CLOCK_EXTERNAL        0x40
83 #define FLAG_CLOCK_INVERTED        0x80
84 #define FLAG_RLE                   0x0100
85
86 static int capabilities[] = {
87         SR_HWCAP_LOGIC_ANALYZER,
88         SR_HWCAP_SAMPLERATE,
89         SR_HWCAP_CAPTURE_RATIO,
90         SR_HWCAP_LIMIT_SAMPLES,
91         0,
92 };
93
94 static struct sr_samplerates samplerates = {
95         SR_HZ(10),
96         SR_MHZ(200),
97         SR_HZ(1),
98         NULL,
99 };
100
101 /* List of struct sr_serial_device_instance */
102 static GSList *device_instances = NULL;
103
104 /* Current state of the flag register */
105 static uint32_t flag_reg = 0;
106
107 static uint64_t cur_samplerate = 0;
108 static uint64_t limit_samples = 0;
109 /*
110  * Pre/post trigger capture ratio, in percentage.
111  * 0 means no pre-trigger data.
112  */
113 static int capture_ratio = 0;
114 static int trigger_at = -1;
115 static uint32_t probe_mask = 0xffffffff;
116 static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
117 static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
118 static int num_stages = 0;
119
120 static int send_shortcommand(int fd, uint8_t command)
121 {
122         char buf[1];
123
124         g_debug("ols: sending cmd 0x%.2x", command);
125         buf[0] = command;
126         if (serial_write(fd, buf, 1) != 1)
127                 return SR_ERR;
128
129         return SR_OK;
130 }
131
132 static int send_longcommand(int fd, uint8_t command, uint32_t data)
133 {
134         char buf[5];
135
136         g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
137         buf[0] = command;
138         buf[1] = (data & 0xff000000) >> 24;
139         buf[2] = (data & 0xff0000) >> 16;
140         buf[3] = (data & 0xff00) >> 8;
141         buf[4] = data & 0xff;
142         if (serial_write(fd, buf, 5) != 5)
143                 return SR_ERR;
144
145         return SR_OK;
146 }
147
148 static int configure_probes(GSList *probes)
149 {
150         struct sr_probe *probe;
151         GSList *l;
152         int probe_bit, stage, i;
153         char *tc;
154
155         probe_mask = 0;
156         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
157                 trigger_mask[i] = 0;
158                 trigger_value[i] = 0;
159         }
160
161         num_stages = 0;
162         for (l = probes; l; l = l->next) {
163                 probe = (struct sr_probe *)l->data;
164                 if (!probe->enabled)
165                         continue;
166
167                 /*
168                  * Set up the probe mask for later configuration into the
169                  * flag register.
170                  */
171                 probe_bit = 1 << (probe->index - 1);
172                 probe_mask |= probe_bit;
173
174                 if (!probe->trigger)
175                         continue;
176
177                 /* Configure trigger mask and value. */
178                 stage = 0;
179                 for (tc = probe->trigger; tc && *tc; tc++) {
180                         trigger_mask[stage] |= probe_bit;
181                         if (*tc == '1')
182                                 trigger_value[stage] |= probe_bit;
183                         stage++;
184                         if (stage > 3)
185                                 /*
186                                  * TODO: Only supporting parallel mode, with
187                                  * up to 4 stages.
188                                  */
189                                 return SR_ERR;
190                 }
191                 if (stage > num_stages)
192                         num_stages = stage;
193         }
194
195         return SR_OK;
196 }
197
198 static uint32_t reverse16(uint32_t in)
199 {
200         uint32_t out;
201
202         out = (in & 0xff) << 8;
203         out |= (in & 0xff00) >> 8;
204         out |= (in & 0xff0000) << 8;
205         out |= (in & 0xff000000) >> 8;
206
207         return out;
208 }
209
210 static uint32_t reverse32(uint32_t in)
211 {
212         uint32_t out;
213
214         out = (in & 0xff) << 24;
215         out |= (in & 0xff00) << 8;
216         out |= (in & 0xff0000) >> 8;
217         out |= (in & 0xff000000) >> 24;
218
219         return out;
220 }
221
222 static int hw_init(const char *deviceinfo)
223 {
224         struct sr_device_instance *sdi;
225         GSList *ports, *l;
226         GPollFD *fds;
227         int devcnt, final_devcnt, num_ports, fd, ret, i;
228         char buf[8], **device_names, **serial_params;
229
230         if (deviceinfo)
231                 ports = g_slist_append(NULL, strdup(deviceinfo));
232         else
233                 /* No specific device given, so scan all serial ports. */
234                 ports = list_serial_ports();
235
236         num_ports = g_slist_length(ports);
237         fds = calloc(1, num_ports * sizeof(GPollFD));
238         device_names = malloc(num_ports * sizeof(char *));
239         serial_params = malloc(num_ports * sizeof(char *));
240         devcnt = 0;
241         for (l = ports; l; l = l->next) {
242                 /* The discovery procedure is like this: first send the Reset
243                  * command (0x00) 5 times, since the device could be anywhere
244                  * in a 5-byte command. Then send the ID command (0x02).
245                  * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
246                  * have a match.
247                  *
248                  * Since it may take the device a while to respond at 115Kb/s,
249                  * we do all the sending first, then wait for all of them to
250                  * respond with g_poll().
251                  */
252                 g_message("ols: probing %s...", (char *)l->data);
253                 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
254                 if (fd != -1) {
255                         serial_params[devcnt] = serial_backup_params(fd);
256                         serial_set_params(fd, 115200, 8, 0, 1, 2);
257                         ret = SR_OK;
258                         for (i = 0; i < 5; i++) {
259                                 if ((ret = send_shortcommand(fd,
260                                         CMD_RESET)) != SR_OK) {
261                                         /* Serial port is not writable. */
262                                         break;
263                                 }
264                         }
265                         if (ret != SR_OK) {
266                                 serial_restore_params(fd,
267                                         serial_params[devcnt]);
268                                 serial_close(fd);
269                                 continue;
270                         }
271                         send_shortcommand(fd, CMD_ID);
272                         fds[devcnt].fd = fd;
273                         fds[devcnt].events = G_IO_IN;
274                         device_names[devcnt] = strdup(l->data);
275                         devcnt++;
276                 }
277                 free(l->data);
278         }
279
280         /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
281         usleep(10000);
282
283         final_devcnt = 0;
284         g_poll(fds, devcnt, 1);
285         for (i = 0; i < devcnt; i++) {
286                 if (fds[i].revents == G_IO_IN) {
287                         if (serial_read(fds[i].fd, buf, 4) == 4) {
288                                 if (!strncmp(buf, "1SLO", 4)
289                                     || !strncmp(buf, "1ALS", 4)) {
290                                         if (!strncmp(buf, "1SLO", 4))
291                                                 sdi = sr_device_instance_new
292                                                     (final_devcnt, SR_ST_INACTIVE,
293                                                      "Openbench",
294                                                      "Logic Sniffer", "v1.0");
295                                         else
296                                                 sdi = sr_device_instance_new
297                                                     (final_devcnt, SR_ST_INACTIVE,
298                                                      "Openbench", "Logic Sniffer",
299                                                      "v1.0");
300                                         sdi->serial = sr_serial_device_instance_new
301                                             (device_names[i], -1);
302                                         device_instances =
303                                             g_slist_append(device_instances, sdi);
304                                         final_devcnt++;
305                                         serial_close(fds[i].fd);
306                                         fds[i].fd = 0;
307                                 }
308                         }
309                         free(device_names[i]);
310                 }
311
312                 if (fds[i].fd != 0) {
313                         serial_restore_params(fds[i].fd, serial_params[i]);
314                         serial_close(fds[i].fd);
315                 }
316                 free(serial_params[i]);
317         }
318
319         free(fds);
320         free(device_names);
321         free(serial_params);
322         g_slist_free(ports);
323
324         cur_samplerate = SR_KHZ(200);
325
326         return final_devcnt;
327 }
328
329 static int hw_opendev(int device_index)
330 {
331         struct sr_device_instance *sdi;
332
333         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
334                 return SR_ERR;
335
336         sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
337         if (sdi->serial->fd == -1)
338                 return SR_ERR;
339
340         sdi->status = SR_ST_ACTIVE;
341
342         return SR_OK;
343 }
344
345 static void hw_closedev(int device_index)
346 {
347         struct sr_device_instance *sdi;
348
349         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
350                 return;
351
352         if (sdi->serial->fd != -1) {
353                 serial_close(sdi->serial->fd);
354                 sdi->serial->fd = -1;
355                 sdi->status = SR_ST_INACTIVE;
356         }
357 }
358
359 static void hw_cleanup(void)
360 {
361         GSList *l;
362         struct sr_device_instance *sdi;
363
364         /* Properly close all devices. */
365         for (l = device_instances; l; l = l->next) {
366                 sdi = l->data;
367                 if (sdi->serial->fd != -1)
368                         serial_close(sdi->serial->fd);
369                 sr_device_instance_free(sdi);
370         }
371         g_slist_free(device_instances);
372         device_instances = NULL;
373 }
374
375 static void *hw_get_device_info(int device_index, int device_info_id)
376 {
377         struct sr_device_instance *sdi;
378         void *info;
379
380         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
381                 return NULL;
382
383         info = NULL;
384         switch (device_info_id) {
385         case SR_DI_INSTANCE:
386                 info = sdi;
387                 break;
388         case SR_DI_NUM_PROBES:
389                 info = GINT_TO_POINTER(NUM_PROBES);
390                 break;
391         case SR_DI_SAMPLERATES:
392                 info = &samplerates;
393                 break;
394         case SR_DI_TRIGGER_TYPES:
395                 info = (char *)TRIGGER_TYPES;
396                 break;
397         case SR_DI_CUR_SAMPLERATE:
398                 info = &cur_samplerate;
399                 break;
400         }
401
402         return info;
403 }
404
405 static int hw_get_status(int device_index)
406 {
407         struct sr_device_instance *sdi;
408
409         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
410                 return SR_ST_NOT_FOUND;
411
412         return sdi->status;
413 }
414
415 static int *hw_get_capabilities(void)
416 {
417         return capabilities;
418 }
419
420 static int set_configuration_samplerate(struct sr_device_instance *sdi,
421                                         uint64_t samplerate)
422 {
423         uint32_t divider;
424
425         if (samplerate < samplerates.low || samplerate > samplerates.high)
426                 return SR_ERR_SAMPLERATE;
427
428         if (samplerate > CLOCK_RATE) {
429                 flag_reg |= FLAG_DEMUX;
430                 divider = (CLOCK_RATE * 2 / samplerate) - 1;
431         } else {
432                 flag_reg &= ~FLAG_DEMUX;
433                 divider = (CLOCK_RATE / samplerate) - 1;
434         }
435
436         g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
437                         samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
438
439         if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, reverse32(divider)) != SR_OK)
440                 return SR_ERR;
441         cur_samplerate = samplerate;
442
443         return SR_OK;
444 }
445
446 static int hw_set_configuration(int device_index, int capability, void *value)
447 {
448         struct sr_device_instance *sdi;
449         int ret;
450         uint64_t *tmp_u64;
451
452         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
453                 return SR_ERR;
454
455         if (sdi->status != SR_ST_ACTIVE)
456                 return SR_ERR;
457
458         switch (capability) {
459         case SR_HWCAP_SAMPLERATE:
460                 tmp_u64 = value;
461                 ret = set_configuration_samplerate(sdi, *tmp_u64);
462                 break;
463         case SR_HWCAP_PROBECONFIG:
464                 ret = configure_probes((GSList *) value);
465                 break;
466         case SR_HWCAP_LIMIT_SAMPLES:
467                 tmp_u64 = value;
468                 if (*tmp_u64 < MIN_NUM_SAMPLES)
469                         return SR_ERR;
470                 limit_samples = *tmp_u64;
471                 g_message("ols: sample limit %" PRIu64, limit_samples);
472                 ret = SR_OK;
473                 break;
474         case SR_HWCAP_CAPTURE_RATIO:
475                 tmp_u64 = value;
476                 capture_ratio = *tmp_u64;
477                 if (capture_ratio < 0 || capture_ratio > 100) {
478                         capture_ratio = 0;
479                         ret = SR_ERR;
480                 } else
481                         ret = SR_OK;
482                 break;
483         default:
484                 ret = SR_ERR;
485         }
486
487         return ret;
488 }
489
490 static int receive_data(int fd, int revents, void *user_data)
491 {
492         static unsigned int num_transfers = 0;
493         static int num_bytes = 0;
494         static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
495         static unsigned char sample[4] = { 0, 0, 0, 0 };
496         static unsigned char tmp_sample[4];
497         static unsigned char *raw_sample_buf = NULL;
498         int count, buflen, num_channels, offset, i, j;
499         struct sr_datafeed_packet packet;
500         unsigned char byte, *buffer;
501
502         if (num_transfers++ == 0) {
503                 /*
504                  * First time round, means the device started sending data,
505                  * and will not stop until done. If it stops sending for
506                  * longer than it takes to send a byte, that means it's
507                  * finished. We'll double that to 30ms to be sure...
508                  */
509                 sr_source_remove(fd);
510                 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
511                 raw_sample_buf = malloc(limit_samples * 4);
512                 /* fill with 1010... for debugging */
513                 memset(raw_sample_buf, 0x82, limit_samples * 4);
514         }
515
516         num_channels = 0;
517         for (i = 0x20; i > 0x02; i /= 2) {
518                 if ((flag_reg & i) == 0)
519                         num_channels++;
520         }
521
522         if (revents == G_IO_IN
523             && num_transfers / num_channels <= limit_samples) {
524                 if (serial_read(fd, &byte, 1) != 1)
525                         return FALSE;
526
527                 sample[num_bytes++] = byte;
528                 g_debug("ols: received byte 0x%.2x", byte);
529                 if (num_bytes == num_channels) {
530                         g_debug("ols: received sample 0x%.*x", num_bytes * 2, (int) *sample);
531                         /* Got a full sample. */
532                         if (flag_reg & FLAG_RLE) {
533                                 /*
534                                  * In RLE mode -1 should never come in as a
535                                  * sample, because bit 31 is the "count" flag.
536                                  * TODO: Endianness may be wrong here, could be
537                                  * sample[3].
538                                  */
539                                 if (sample[0] & 0x80
540                                     && !(last_sample[0] & 0x80)) {
541                                         count = (int)(*sample) & 0x7fffffff;
542                                         buffer = g_malloc(count);
543                                         buflen = 0;
544                                         for (i = 0; i < count; i++) {
545                                                 memcpy(buffer + buflen, last_sample, 4);
546                                                 buflen += 4;
547                                         }
548                                 } else {
549                                         /*
550                                          * Just a single sample, next sample
551                                          * will probably be a count referring
552                                          * to this -- but this one is still a
553                                          * part of the stream.
554                                          */
555                                         buffer = sample;
556                                         buflen = 4;
557                                 }
558                         } else {
559                                 /* No compression. */
560                                 buffer = sample;
561                                 buflen = 4;
562                         }
563
564                         if (num_channels < 4) {
565                                 /*
566                                  * Some channel groups may have been turned
567                                  * off, to speed up transfer between the
568                                  * hardware and the PC. Expand that here before
569                                  * submitting it over the session bus --
570                                  * whatever is listening on the bus will be
571                                  * expecting a full 32-bit sample, based on
572                                  * the number of probes.
573                                  */
574                                 j = 0;
575                                 memset(tmp_sample, 0, 4);
576                                 for (i = 0; i < 4; i++) {
577                                         if (((flag_reg >> 2) & (1 << i)) == 0) {
578                                                 /*
579                                                  * This channel group was
580                                                  * enabled, copy from received
581                                                  * sample.
582                                                  */
583                                                 tmp_sample[i] = sample[j++];
584                                         }
585                                 }
586                                 memcpy(sample, tmp_sample, 4);
587                                 g_debug("ols: full sample 0x%.8x", (int) *sample);
588                         }
589
590                         /* the OLS sends its sample buffer backwards.
591                          * store it in reverse order here, so we can dump
592                          * this on the session bus later.
593                          */
594                         offset = (limit_samples - num_transfers / num_channels) * 4;
595                         memcpy(raw_sample_buf + offset, sample, 4);
596
597                         if (buffer == sample)
598                                 memcpy(last_sample, buffer, num_channels);
599                         else
600                                 g_free(buffer);
601
602                         memset(sample, 0, 4);
603                         num_bytes = 0;
604                 }
605         } else {
606                 /*
607                  * This is the main loop telling us a timeout was reached, or
608                  * we've acquired all the samples we asked for -- we're done.
609                  * Send the (properly-ordered) buffer to the frontend.
610                  */
611                 if (trigger_at != -1) {
612                         /* a trigger was set up, so we need to tell the frontend
613                          * about it.
614                          */
615                         if (trigger_at > 0) {
616                                 /* there are pre-trigger samples, send those first */
617                                 packet.type = SR_DF_LOGIC;
618                                 packet.length = trigger_at * 4;
619                                 packet.unitsize = 4;
620                                 packet.payload = raw_sample_buf;
621                                 sr_session_bus(user_data, &packet);
622                         }
623
624                         packet.type = SR_DF_TRIGGER;
625                         packet.length = 0;
626                         sr_session_bus(user_data, &packet);
627
628                         packet.type = SR_DF_LOGIC;
629                         packet.length = (limit_samples * 4) - (trigger_at * 4);
630                         packet.unitsize = 4;
631                         packet.payload = raw_sample_buf + trigger_at * 4;
632                         sr_session_bus(user_data, &packet);
633                 } else {
634                         packet.type = SR_DF_LOGIC;
635                         packet.length = limit_samples * 4;
636                         packet.unitsize = 4;
637                         packet.payload = raw_sample_buf;
638                         sr_session_bus(user_data, &packet);
639                 }
640                 free(raw_sample_buf);
641
642                 serial_flush(fd);
643                 serial_close(fd);
644                 packet.type = SR_DF_END;
645                 packet.length = 0;
646                 sr_session_bus(user_data, &packet);
647         }
648
649         return TRUE;
650 }
651
652 static int hw_start_acquisition(int device_index, gpointer session_device_id)
653 {
654         int i;
655         struct sr_datafeed_packet *packet;
656         struct sr_datafeed_header *header;
657         struct sr_device_instance *sdi;
658         uint32_t trigger_config[4];
659         uint32_t data;
660         uint16_t readcount, delaycount;
661         uint8_t changrp_mask;
662
663         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
664                 return SR_ERR;
665
666         if (sdi->status != SR_ST_ACTIVE)
667                 return SR_ERR;
668
669         readcount = limit_samples / 4;
670
671         memset(trigger_config, 0, 16);
672         trigger_config[num_stages-1] |= 0x08;
673         if (trigger_mask[0]) {
674                 delaycount = readcount * (1 - capture_ratio / 100.0);
675                 trigger_at = (readcount - delaycount) * 4 - num_stages;
676
677                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
678                         reverse32(trigger_mask[0])) != SR_OK)
679                         return SR_ERR;
680                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
681                         reverse32(trigger_value[0])) != SR_OK)
682                         return SR_ERR;
683                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
684                         trigger_config[0]) != SR_OK)
685                         return SR_ERR;
686
687                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
688                         reverse32(trigger_mask[1])) != SR_OK)
689                         return SR_ERR;
690                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
691                         reverse32(trigger_value[1])) != SR_OK)
692                         return SR_ERR;
693                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
694                         trigger_config[1]) != SR_OK)
695                         return SR_ERR;
696
697                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
698                         reverse32(trigger_mask[2])) != SR_OK)
699                         return SR_ERR;
700                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
701                         reverse32(trigger_value[2])) != SR_OK)
702                         return SR_ERR;
703                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
704                         trigger_config[2]) != SR_OK)
705                         return SR_ERR;
706
707                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
708                         reverse32(trigger_mask[3])) != SR_OK)
709                         return SR_ERR;
710                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
711                         reverse32(trigger_value[3])) != SR_OK)
712                         return SR_ERR;
713                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
714                         trigger_config[3]) != SR_OK)
715                         return SR_ERR;
716         } else {
717                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
718                      trigger_mask[0]) != SR_OK)
719                         return SR_ERR;
720                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
721                      trigger_value[0]) != SR_OK)
722                         return SR_ERR;
723                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
724                      0x00000008) != SR_OK)
725                         return SR_ERR;
726                 delaycount = readcount;
727         }
728
729         set_configuration_samplerate(sdi, cur_samplerate);
730
731         /* Send sample limit and pre/post-trigger capture ratio. */
732         data = ((readcount - 1) & 0xffff) << 16;
733         data |= (delaycount - 1) & 0xffff;
734         if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
735                 return SR_ERR;
736
737         /*
738          * Enable/disable channel groups in the flag register according to the
739          * probe mask.
740          */
741         changrp_mask = 0;
742         for (i = 0; i < 4; i++) {
743                 if (probe_mask & (0xff << (i * 8)))
744                         changrp_mask |= (1 << i);
745         }
746
747         /* The flag register wants them here, and 1 means "disable channel". */
748         flag_reg |= ~(changrp_mask << 2) & 0x3c;
749         flag_reg |= FLAG_FILTER;
750         data = flag_reg << 24;
751         if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
752                 return SR_ERR;
753
754         /* Start acquisition on the device. */
755         if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
756                 return SR_ERR;
757
758         sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
759                       session_device_id);
760
761         /* Send header packet to the session bus. */
762         packet = g_malloc(sizeof(struct sr_datafeed_packet));
763         header = g_malloc(sizeof(struct sr_datafeed_header));
764         if (!packet || !header)
765                 return SR_ERR;
766         packet->type = SR_DF_HEADER;
767         packet->length = sizeof(struct sr_datafeed_header);
768         packet->payload = (unsigned char *)header;
769         header->feed_version = 1;
770         gettimeofday(&header->starttime, NULL);
771         header->samplerate = cur_samplerate;
772         header->protocol_id = SR_PROTO_RAW;
773         header->num_logic_probes = NUM_PROBES;
774         header->num_analog_probes = 0;
775         sr_session_bus(session_device_id, packet);
776         g_free(header);
777         g_free(packet);
778
779         return SR_OK;
780 }
781
782 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
783 {
784         struct sr_datafeed_packet packet;
785
786         /* Avoid compiler warnings. */
787         device_index = device_index;
788
789         packet.type = SR_DF_END;
790         packet.length = 0;
791         sr_session_bus(session_device_id, &packet);
792 }
793
794 struct sr_device_plugin ols_plugin_info = {
795         "ols",
796         "Openbench Logic Sniffer",
797         1,
798         hw_init,
799         hw_cleanup,
800         hw_opendev,
801         hw_closedev,
802         hw_get_device_info,
803         hw_get_status,
804         hw_get_capabilities,
805         hw_set_configuration,
806         hw_start_acquisition,
807         hw_stop_acquisition,
808 };