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