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