]> sigrok.org Git - libsigrok.git/blob - hardware/openbench-logic-sniffer/ols.c
add serial_flush()
[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("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                                                      "Sump", "Logic Analyzer",
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("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                 ret = SIGROK_OK;
462                 break;
463         case HWCAP_CAPTURE_RATIO:
464                 tmp_u64 = value;
465                 capture_ratio = *tmp_u64;
466                 if (capture_ratio < 0 || capture_ratio > 100) {
467                         capture_ratio = 0;
468                         ret = SIGROK_ERR;
469                 } else
470                         ret = SIGROK_OK;
471                 break;
472         default:
473                 ret = SIGROK_ERR;
474         }
475
476         return ret;
477 }
478
479 static int receive_data(int fd, int revents, void *user_data)
480 {
481         static unsigned int num_transfers = 0;
482         static int num_bytes = 0;
483         static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
484         static unsigned char sample[4] = { 0, 0, 0, 0 };
485         static unsigned char tmp_sample[4];
486         static unsigned char *raw_sample_buf = NULL;
487         int count, buflen, num_channels, offset, i, j;
488         struct datafeed_packet packet;
489         unsigned char byte, *buffer;
490
491         if (num_transfers++ == 0) {
492                 /*
493                  * First time round, means the device started sending data,
494                  * and will not stop until done. If it stops sending for
495                  * longer than it takes to send a byte, that means it's
496                  * finished. We'll double that to 30ms to be sure...
497                  */
498                 source_remove(fd);
499                 source_add(fd, G_IO_IN, 30, receive_data, user_data);
500                 raw_sample_buf = malloc(limit_samples * 4);
501                 /* fill with 1010... for debugging */
502                 memset(raw_sample_buf, 0x82, limit_samples * 4);
503         }
504
505         num_channels = 0;
506         for (i = 0x20; i > 0x02; i /= 2) {
507                 if ((flag_reg & i) == 0)
508                         num_channels++;
509         }
510
511         if (revents == G_IO_IN
512             && num_transfers / num_channels <= limit_samples) {
513                 if (read(fd, &byte, 1) != 1)
514                         return FALSE;
515
516                 sample[num_bytes++] = byte;
517                 g_debug("received byte 0x%.2x", byte);
518                 if (num_bytes == num_channels) {
519                         g_debug("received sample 0x%.*x", num_bytes * 2, (int) *sample);
520                         /* Got a full sample. */
521                         if (flag_reg & FLAG_RLE) {
522                                 /*
523                                  * In RLE mode -1 should never come in as a
524                                  * sample, because bit 31 is the "count" flag.
525                                  * TODO: Endianness may be wrong here, could be
526                                  * sample[3].
527                                  */
528                                 if (sample[0] & 0x80
529                                     && !(last_sample[0] & 0x80)) {
530                                         count = (int)(*sample) & 0x7fffffff;
531                                         buffer = g_malloc(count);
532                                         buflen = 0;
533                                         for (i = 0; i < count; i++) {
534                                                 memcpy(buffer + buflen, last_sample, 4);
535                                                 buflen += 4;
536                                         }
537                                 } else {
538                                         /*
539                                          * Just a single sample, next sample
540                                          * will probably be a count referring
541                                          * to this -- but this one is still a
542                                          * part of the stream.
543                                          */
544                                         buffer = sample;
545                                         buflen = 4;
546                                 }
547                         } else {
548                                 /* No compression. */
549                                 buffer = sample;
550                                 buflen = 4;
551                         }
552
553                         if (num_channels < 4) {
554                                 /*
555                                  * Some channel groups may have been turned
556                                  * off, to speed up transfer between the
557                                  * hardware and the PC. Expand that here before
558                                  * submitting it over the session bus --
559                                  * whatever is listening on the bus will be
560                                  * expecting a full 32-bit sample, based on
561                                  * the number of probes.
562                                  */
563                                 j = 0;
564                                 memset(tmp_sample, 0, 4);
565                                 for (i = 0; i < 4; i++) {
566                                         if (((flag_reg >> 2) & (1 << i)) == 0) {
567                                                 /*
568                                                  * This channel group was
569                                                  * enabled, copy from received
570                                                  * sample.
571                                                  */
572                                                 tmp_sample[i] = sample[j++];
573                                         }
574                                 }
575                                 memcpy(sample, tmp_sample, 4);
576                                 g_debug("full sample 0x%.8x", (int) *sample);
577                         }
578
579                         /* the OLS sends its sample buffer backwards.
580                          * store it in reverse order here, so we can dump
581                          * this on the session bus later.
582                          */
583                         offset = (limit_samples - num_transfers / num_channels) * 4;
584                         memcpy(raw_sample_buf + offset, sample, 4);
585
586                         if (buffer == sample)
587                                 memcpy(last_sample, buffer, num_channels);
588                         else
589                                 g_free(buffer);
590
591                         memset(sample, 0, 4);
592                         num_bytes = 0;
593                 }
594         } else {
595                 /*
596                  * This is the main loop telling us a timeout was reached, or
597                  * we've acquired all the samples we asked for -- we're done.
598                  * Send the (properly-ordered) buffer to the frontend.
599                  */
600                 if (trigger_at != -1) {
601                         /* a trigger was set up, so we need to tell the frontend
602                          * about it.
603                          */
604                         if (trigger_at > 0) {
605                                 /* there are pre-trigger samples, send those first */
606                                 packet.type = DF_LOGIC32;
607                                 packet.length = trigger_at * 4;
608                                 packet.payload = raw_sample_buf;
609                                 session_bus(user_data, &packet);
610                         }
611
612                         packet.type = DF_TRIGGER;
613                         packet.length = 0;
614                         session_bus(user_data, &packet);
615
616                         packet.type = DF_LOGIC32;
617                         packet.length = (limit_samples * 4) - (trigger_at * 4);
618                         packet.payload = raw_sample_buf + trigger_at * 4;
619                         session_bus(user_data, &packet);
620                 } else {
621                         packet.type = DF_LOGIC32;
622                         packet.length = limit_samples * 4;
623                         packet.payload = raw_sample_buf;
624                         session_bus(user_data, &packet);
625                 }
626                 free(raw_sample_buf);
627
628                 serial_flush(fd);
629                 serial_close(fd);
630                 packet.type = DF_END;
631                 packet.length = 0;
632                 session_bus(user_data, &packet);
633         }
634
635         return TRUE;
636 }
637
638 static int hw_start_acquisition(int device_index, gpointer session_device_id)
639 {
640         int i;
641         struct datafeed_packet *packet;
642         struct datafeed_header *header;
643         struct sigrok_device_instance *sdi;
644         uint32_t trigger_config[4];
645         uint32_t data;
646         uint16_t readcount, delaycount;
647         uint8_t changrp_mask;
648
649         if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
650                 return SIGROK_ERR;
651
652         if (sdi->status != ST_ACTIVE)
653                 return SIGROK_ERR;
654
655         readcount = limit_samples / 4;
656
657         memset(trigger_config, 0, 16);
658         trigger_config[num_stages-1] |= 0x08;
659         if (trigger_mask[0]) {
660                 delaycount = readcount * (1 - capture_ratio / 100.0);
661                 trigger_at = (readcount - delaycount) * 4 - num_stages;
662                 g_message("ta %d", trigger_at);
663
664                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
665                         reverse32(trigger_mask[0])) != SIGROK_OK)
666                         return SIGROK_ERR;
667                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
668                         reverse32(trigger_value[0])) != SIGROK_OK)
669                         return SIGROK_ERR;
670                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
671                         trigger_config[0]) != SIGROK_OK)
672                         return SIGROK_ERR;
673
674                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
675                         reverse32(trigger_mask[1])) != SIGROK_OK)
676                         return SIGROK_ERR;
677                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
678                         reverse32(trigger_value[1])) != SIGROK_OK)
679                         return SIGROK_ERR;
680                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
681                         trigger_config[1]) != SIGROK_OK)
682                         return SIGROK_ERR;
683
684                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
685                         reverse32(trigger_mask[2])) != SIGROK_OK)
686                         return SIGROK_ERR;
687                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
688                         reverse32(trigger_value[2])) != SIGROK_OK)
689                         return SIGROK_ERR;
690                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
691                         trigger_config[2]) != SIGROK_OK)
692                         return SIGROK_ERR;
693
694                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
695                         reverse32(trigger_mask[3])) != SIGROK_OK)
696                         return SIGROK_ERR;
697                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
698                         reverse32(trigger_value[3])) != SIGROK_OK)
699                         return SIGROK_ERR;
700                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
701                         trigger_config[3]) != SIGROK_OK)
702                         return SIGROK_ERR;
703         } else {
704                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
705                      trigger_mask[0]) != SIGROK_OK)
706                         return SIGROK_ERR;
707                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
708                      trigger_value[0]) != SIGROK_OK)
709                         return SIGROK_ERR;
710                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
711                      0x00000008) != SIGROK_OK)
712                         return SIGROK_ERR;
713                 delaycount = readcount;
714         }
715
716         set_configuration_samplerate(sdi, cur_samplerate);
717
718         /* Send sample limit and pre/post-trigger capture ratio. */
719         data = ((readcount - 1) & 0xffff) << 16;
720         data |= (delaycount - 1) & 0xffff;
721         if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SIGROK_OK)
722                 return SIGROK_ERR;
723
724         /*
725          * Enable/disable channel groups in the flag register according to the
726          * probe mask.
727          */
728         changrp_mask = 0;
729         for (i = 0; i < 4; i++) {
730                 if (probe_mask & (0xff << (i * 8)))
731                         changrp_mask |= (1 << i);
732         }
733         g_message("changrp_mask 0x%.2x", changrp_mask);
734
735         /* The flag register wants them here, and 1 means "disable channel". */
736         flag_reg |= ~(changrp_mask << 2) & 0x3c;
737         g_message("flag_reg 0x%.2x", flag_reg & 0x3c);
738
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_probes = NUM_PROBES;
764         session_bus(session_device_id, packet);
765         g_free(header);
766         g_free(packet);
767
768         return SIGROK_OK;
769 }
770
771 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
772 {
773         struct datafeed_packet packet;
774
775         /* QUICK HACK */
776         device_index = device_index;
777
778         packet.type = DF_END;
779         packet.length = 0;
780         session_bus(session_device_id, &packet);
781 }
782
783 struct device_plugin ols_plugin_info = {
784         "sump",
785         1,
786         hw_init,
787         hw_cleanup,
788         hw_opendev,
789         hw_closedev,
790         hw_get_device_info,
791         hw_get_status,
792         hw_get_capabilities,
793         hw_set_configuration,
794         hw_start_acquisition,
795         hw_stop_acquisition,
796 };