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