]> sigrok.org Git - libsigrok.git/blob - hardware/openbench-logic-sniffer/ols.c
Replace g_malloc{0,} with g_try_malloc{0,}.
[libsigrok.git] / hardware / openbench-logic-sniffer / ols.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #ifdef _WIN32
28 #include <windows.h>
29 #else
30 #include <termios.h>
31 #endif
32 #include <string.h>
33 #include <sys/time.h>
34 #include <inttypes.h>
35 #ifdef _WIN32
36 /* TODO */
37 #else
38 #include <arpa/inet.h>
39 #endif
40 #include <glib.h>
41 #include <sigrok.h>
42 #include <sigrok-internal.h>
43 #include "ols.h"
44
45 #ifdef _WIN32
46 #define O_NONBLOCK FIONBIO
47 #endif
48
49
50 static int capabilities[] = {
51         SR_HWCAP_LOGIC_ANALYZER,
52         SR_HWCAP_SAMPLERATE,
53         SR_HWCAP_CAPTURE_RATIO,
54         SR_HWCAP_LIMIT_SAMPLES,
55         0,
56 };
57
58 /* default supported samplerates, can be overridden by device metadata */
59 static struct sr_samplerates samplerates = {
60         SR_HZ(10),
61         SR_MHZ(200),
62         SR_HZ(1),
63         NULL,
64 };
65
66 /* List of struct sr_serial_device_instance */
67 static GSList *device_instances = NULL;
68
69
70
71 static int send_shortcommand(int fd, uint8_t command)
72 {
73         char buf[1];
74
75         sr_dbg("ols: sending cmd 0x%.2x", command);
76         buf[0] = command;
77         if (serial_write(fd, buf, 1) != 1)
78                 return SR_ERR;
79
80         return SR_OK;
81 }
82
83 static int send_longcommand(int fd, uint8_t command, uint32_t data)
84 {
85         char buf[5];
86
87         sr_dbg("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
88         buf[0] = command;
89         buf[1] = (data & 0xff000000) >> 24;
90         buf[2] = (data & 0xff0000) >> 16;
91         buf[3] = (data & 0xff00) >> 8;
92         buf[4] = data & 0xff;
93         if (serial_write(fd, buf, 5) != 5)
94                 return SR_ERR;
95
96         return SR_OK;
97 }
98
99 static int configure_probes(struct ols_device *ols, GSList *probes)
100 {
101         struct sr_probe *probe;
102         GSList *l;
103         int probe_bit, stage, i;
104         char *tc;
105
106         ols->probe_mask = 0;
107         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
108                 ols->trigger_mask[i] = 0;
109                 ols->trigger_value[i] = 0;
110         }
111
112         ols->num_stages = 0;
113         for (l = probes; l; l = l->next) {
114                 probe = (struct sr_probe *)l->data;
115                 if (!probe->enabled)
116                         continue;
117
118                 /*
119                  * Set up the probe mask for later configuration into the
120                  * flag register.
121                  */
122                 probe_bit = 1 << (probe->index - 1);
123                 ols->probe_mask |= probe_bit;
124
125                 if (!probe->trigger)
126                         continue;
127
128                 /* Configure trigger mask and value. */
129                 stage = 0;
130                 for (tc = probe->trigger; tc && *tc; tc++) {
131                         ols->trigger_mask[stage] |= probe_bit;
132                         if (*tc == '1')
133                                 ols->trigger_value[stage] |= probe_bit;
134                         stage++;
135                         if (stage > 3)
136                                 /*
137                                  * TODO: Only supporting parallel mode, with
138                                  * up to 4 stages.
139                                  */
140                                 return SR_ERR;
141                 }
142                 if (stage > ols->num_stages)
143                         ols->num_stages = stage;
144         }
145
146         return SR_OK;
147 }
148
149 static uint32_t reverse16(uint32_t in)
150 {
151         uint32_t out;
152
153         out = (in & 0xff) << 8;
154         out |= (in & 0xff00) >> 8;
155         out |= (in & 0xff0000) << 8;
156         out |= (in & 0xff000000) >> 8;
157
158         return out;
159 }
160
161 static uint32_t reverse32(uint32_t in)
162 {
163         uint32_t out;
164
165         out = (in & 0xff) << 24;
166         out |= (in & 0xff00) << 8;
167         out |= (in & 0xff0000) >> 8;
168         out |= (in & 0xff000000) >> 24;
169
170         return out;
171 }
172
173 static struct ols_device *ols_device_new(void)
174 {
175         struct ols_device *ols;
176
177         if (!(ols = g_try_malloc0(sizeof(struct ols_device)))) {
178                 sr_err("ols: %s: ols malloc failed", __func__);
179                 return NULL;
180         }
181
182         ols->trigger_at = -1;
183         ols->probe_mask = 0xffffffff;
184         ols->cur_samplerate = SR_KHZ(200);
185
186         return ols;
187 }
188
189 static struct sr_device_instance *get_metadata(int fd)
190 {
191         struct sr_device_instance *sdi;
192         struct ols_device *ols;
193         uint32_t tmp_int;
194         uint8_t key, type, token;
195         GString *tmp_str, *devicename, *version;
196         gchar tmp_c;
197
198         sdi = sr_device_instance_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
199         ols = ols_device_new();
200         sdi->priv = ols;
201
202         devicename = g_string_new("");
203         version = g_string_new("");
204
205         key = 0xff;
206         while (key) {
207                 if (serial_read(fd, &key, 1) != 1 || key == 0x00)
208                         break;
209                 type = key >> 5;
210                 token = key & 0x1f;
211                 switch (type) {
212                 case 0:
213                         /* NULL-terminated string */
214                         tmp_str = g_string_new("");
215                         while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0')
216                                 g_string_append_c(tmp_str, tmp_c);
217                         sr_dbg("ols: got metadata key 0x%.2x value '%s'",
218                                key, tmp_str->str);
219                         switch (token) {
220                         case 0x01:
221                                 /* Device name */
222                                 devicename = g_string_append(devicename, tmp_str->str);
223                                 break;
224                         case 0x02:
225                                 /* FPGA firmware version */
226                                 if (version->len)
227                                         g_string_append(version, ", ");
228                                 g_string_append(version, "FPGA version ");
229                                 g_string_append(version, tmp_str->str);
230                                 break;
231                         case 0x03:
232                                 /* Ancillary version */
233                                 if (version->len)
234                                         g_string_append(version, ", ");
235                                 g_string_append(version, "Ancillary version ");
236                                 g_string_append(version, tmp_str->str);
237                                 break;
238                         default:
239                                 sr_info("ols: unknown token 0x%.2x: '%s'",
240                                         token, tmp_str->str);
241                                 break;
242                         }
243                         g_string_free(tmp_str, TRUE);
244                         break;
245                 case 1:
246                         /* 32-bit unsigned integer */
247                         if (serial_read(fd, &tmp_int, 4) != 4)
248                                 break;
249                         tmp_int = reverse32(tmp_int);
250                         sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x",
251                                key, tmp_int);
252                         switch (token) {
253                         case 0x00:
254                                 /* Number of usable probes */
255                                 ols->num_probes = tmp_int;
256                                 break;
257                         case 0x01:
258                                 /* Amount of sample memory available (bytes) */
259                                 ols->max_samples = tmp_int;
260                                 break;
261                         case 0x02:
262                                 /* Amount of dynamic memory available (bytes) */
263                                 /* what is this for? */
264                                 break;
265                         case 0x03:
266                                 /* Maximum sample rate (hz) */
267                                 ols->max_samplerate = tmp_int;
268                                 break;
269                         case 0x04:
270                                 /* protocol version */
271                                 ols->protocol_version = tmp_int;
272                                 break;
273                         default:
274                                 sr_info("ols: unknown token 0x%.2x: 0x%.8x",
275                                         token, tmp_int);
276                                 break;
277                         }
278                         break;
279                 case 2:
280                         /* 8-bit unsigned integer */
281                         if (serial_read(fd, &tmp_c, 1) != 1)
282                                 break;
283                         sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x",
284                                key, tmp_c);
285                         switch (token) {
286                         case 0x00:
287                                 /* Number of usable probes */
288                                 ols->num_probes = tmp_c;
289                                 break;
290                         case 0x01:
291                                 /* protocol version */
292                                 ols->protocol_version = tmp_c;
293                                 break;
294                         default:
295                                 sr_info("ols: unknown token 0x%.2x: 0x%.2x",
296                                         token, tmp_c);
297                                 break;
298                         }
299                         break;
300                 default:
301                         /* unknown type */
302                         break;
303                 }
304         }
305
306         sdi->model = devicename->str;
307         sdi->version = version->str;
308         g_string_free(devicename, FALSE);
309         g_string_free(version, FALSE);
310
311         return sdi;
312 }
313
314
315 static int hw_init(const char *deviceinfo)
316 {
317         struct sr_device_instance *sdi;
318         struct ols_device *ols;
319         GSList *ports, *l;
320         GPollFD *fds, probefd;
321         int devcnt, final_devcnt, num_ports, fd, ret, i;
322         char buf[8], **device_names, **serial_params;
323
324         if (deviceinfo)
325                 ports = g_slist_append(NULL, strdup(deviceinfo));
326         else
327                 /* No specific device given, so scan all serial ports. */
328                 ports = list_serial_ports();
329
330         num_ports = g_slist_length(ports);
331         fds = calloc(1, num_ports * sizeof(GPollFD));
332         device_names = malloc(num_ports * sizeof(char *));
333         serial_params = malloc(num_ports * sizeof(char *));
334         devcnt = 0;
335         for (l = ports; l; l = l->next) {
336                 /* The discovery procedure is like this: first send the Reset
337                  * command (0x00) 5 times, since the device could be anywhere
338                  * in a 5-byte command. Then send the ID command (0x02).
339                  * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
340                  * have a match.
341                  *
342                  * Since it may take the device a while to respond at 115Kb/s,
343                  * we do all the sending first, then wait for all of them to
344                  * respond with g_poll().
345                  */
346                 sr_info("ols: probing %s...", (char *)l->data);
347                 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
348                 if (fd != -1) {
349                         serial_params[devcnt] = serial_backup_params(fd);
350                         serial_set_params(fd, 115200, 8, 0, 1, 2);
351                         ret = SR_OK;
352                         for (i = 0; i < 5; i++) {
353                                 if ((ret = send_shortcommand(fd,
354                                         CMD_RESET)) != SR_OK) {
355                                         /* Serial port is not writable. */
356                                         break;
357                                 }
358                         }
359                         if (ret != SR_OK) {
360                                 serial_restore_params(fd,
361                                         serial_params[devcnt]);
362                                 serial_close(fd);
363                                 continue;
364                         }
365                         send_shortcommand(fd, CMD_ID);
366                         fds[devcnt].fd = fd;
367                         fds[devcnt].events = G_IO_IN;
368                         device_names[devcnt] = strdup(l->data);
369                         devcnt++;
370                 }
371                 free(l->data);
372         }
373
374         /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
375         usleep(10000);
376
377         final_devcnt = 0;
378         g_poll(fds, devcnt, 1);
379
380         for (i = 0; i < devcnt; i++) {
381                 if (fds[i].revents != G_IO_IN)
382                         continue;
383                 if (serial_read(fds[i].fd, buf, 4) != 4)
384                         continue;
385                 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
386                         continue;
387
388                 /* definitely using the OLS protocol, check if it supports
389                  * the metadata command
390                  */
391                 send_shortcommand(fds[i].fd, CMD_METADATA);
392                 probefd.fd = fds[i].fd;
393                 probefd.events = G_IO_IN;
394                 if (g_poll(&probefd, 1, 10) > 0) {
395                         /* got metadata */
396                         sdi = get_metadata(fds[i].fd);
397                         sdi->index = final_devcnt;
398                 } else {
399                         /* not an OLS -- some other board that uses the sump protocol */
400                         sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE,
401                                         "Sump", "Logic Analyzer", "v1.0");
402                         ols = ols_device_new();
403                         ols->num_probes = 32;
404                         sdi->priv = ols;
405                 }
406                 sdi->serial = sr_serial_device_instance_new(device_names[i], -1);
407                 device_instances = g_slist_append(device_instances, sdi);
408                 final_devcnt++;
409                 serial_close(fds[i].fd);
410                 fds[i].fd = 0;
411
412         }
413
414         /* clean up after all the probing */
415         for (i = 0; i < devcnt; i++) {
416                 if (fds[i].fd != 0) {
417                         serial_restore_params(fds[i].fd, serial_params[i]);
418                         serial_close(fds[i].fd);
419                 }
420                 free(serial_params[i]);
421                 free(device_names[i]);
422         }
423
424         free(fds);
425         free(device_names);
426         free(serial_params);
427         g_slist_free(ports);
428
429         return final_devcnt;
430 }
431
432 static int hw_opendev(int device_index)
433 {
434         struct sr_device_instance *sdi;
435
436         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
437                 return SR_ERR;
438
439         sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
440         if (sdi->serial->fd == -1)
441                 return SR_ERR;
442
443         sdi->status = SR_ST_ACTIVE;
444
445         return SR_OK;
446 }
447
448 static void hw_closedev(int device_index)
449 {
450         struct sr_device_instance *sdi;
451
452         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
453                 return;
454
455         if (sdi->serial->fd != -1) {
456                 serial_close(sdi->serial->fd);
457                 sdi->serial->fd = -1;
458                 sdi->status = SR_ST_INACTIVE;
459         }
460 }
461
462 static void hw_cleanup(void)
463 {
464         GSList *l;
465         struct sr_device_instance *sdi;
466
467         /* Properly close all devices. */
468         for (l = device_instances; l; l = l->next) {
469                 sdi = l->data;
470                 if (sdi->serial->fd != -1)
471                         serial_close(sdi->serial->fd);
472                 sr_device_instance_free(sdi);
473         }
474         g_slist_free(device_instances);
475         device_instances = NULL;
476 }
477
478 static void *hw_get_device_info(int device_index, int device_info_id)
479 {
480         struct sr_device_instance *sdi;
481         struct ols_device *ols;
482         void *info;
483
484         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
485                 return NULL;
486         ols = sdi->priv;
487
488         info = NULL;
489         switch (device_info_id) {
490         case SR_DI_INSTANCE:
491                 info = sdi;
492                 break;
493         case SR_DI_NUM_PROBES:
494                 info = GINT_TO_POINTER(NUM_PROBES);
495                 break;
496         case SR_DI_SAMPLERATES:
497                 info = &samplerates;
498                 break;
499         case SR_DI_TRIGGER_TYPES:
500                 info = (char *)TRIGGER_TYPES;
501                 break;
502         case SR_DI_CUR_SAMPLERATE:
503                 info = &ols->cur_samplerate;
504                 break;
505         }
506
507         return info;
508 }
509
510 static int hw_get_status(int device_index)
511 {
512         struct sr_device_instance *sdi;
513
514         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
515                 return SR_ST_NOT_FOUND;
516
517         return sdi->status;
518 }
519
520 static int *hw_get_capabilities(void)
521 {
522         return capabilities;
523 }
524
525 static int set_configuration_samplerate(struct sr_device_instance *sdi,
526                                         uint64_t samplerate)
527 {
528         struct ols_device *ols;
529
530         ols = sdi->priv;
531         if (ols->max_samplerate) {
532                 if (samplerate > ols->max_samplerate)
533                         return SR_ERR_SAMPLERATE;
534         } else if (samplerate < samplerates.low || samplerate > samplerates.high)
535                 return SR_ERR_SAMPLERATE;
536
537         ols->cur_samplerate = samplerate;
538         if (samplerate > CLOCK_RATE) {
539                 ols->flag_reg |= FLAG_DEMUX;
540                 ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
541         } else {
542                 ols->flag_reg &= ~FLAG_DEMUX;
543                 ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
544         }
545
546         return SR_OK;
547 }
548
549 static int hw_set_configuration(int device_index, int capability, void *value)
550 {
551         struct sr_device_instance *sdi;
552         struct ols_device *ols;
553         int ret;
554         uint64_t *tmp_u64;
555
556         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
557                 return SR_ERR;
558         ols = sdi->priv;
559
560         if (sdi->status != SR_ST_ACTIVE)
561                 return SR_ERR;
562
563         switch (capability) {
564         case SR_HWCAP_SAMPLERATE:
565                 tmp_u64 = value;
566                 ret = set_configuration_samplerate(sdi, *tmp_u64);
567                 break;
568         case SR_HWCAP_PROBECONFIG:
569                 ret = configure_probes(ols, (GSList *) value);
570                 break;
571         case SR_HWCAP_LIMIT_SAMPLES:
572                 tmp_u64 = value;
573                 if (*tmp_u64 < MIN_NUM_SAMPLES)
574                         return SR_ERR;
575                 ols->limit_samples = *tmp_u64;
576                 sr_info("ols: sample limit %" PRIu64, ols->limit_samples);
577                 ret = SR_OK;
578                 break;
579         case SR_HWCAP_CAPTURE_RATIO:
580                 tmp_u64 = value;
581                 ols->capture_ratio = *tmp_u64;
582                 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
583                         ols->capture_ratio = 0;
584                         ret = SR_ERR;
585                 } else
586                         ret = SR_OK;
587                 break;
588         default:
589                 ret = SR_ERR;
590         }
591
592         return ret;
593 }
594
595 static int receive_data(int fd, int revents, void *user_data)
596 {
597         struct sr_datafeed_packet packet;
598         struct sr_device_instance *sdi;
599         struct ols_device *ols;
600         GSList *l;
601         int count, buflen, num_channels, offset, i, j;
602         unsigned char byte, *buffer;
603
604         /* find this device's ols_device struct by its fd */
605         ols = NULL;
606         for (l = device_instances; l; l = l->next) {
607                 sdi = l->data;
608                 if (sdi->serial->fd == fd) {
609                         ols = sdi->priv;
610                         break;
611                 }
612         }
613         if (!ols)
614                 /* shouldn't happen */
615                 return TRUE;
616
617         if (ols->num_transfers++ == 0) {
618                 /*
619                  * First time round, means the device started sending data,
620                  * and will not stop until done. If it stops sending for
621                  * longer than it takes to send a byte, that means it's
622                  * finished. We'll double that to 30ms to be sure...
623                  */
624                 sr_source_remove(fd);
625                 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
626                 ols->raw_sample_buf = malloc(ols->limit_samples * 4);
627                 /* fill with 1010... for debugging */
628                 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
629         }
630
631         num_channels = 0;
632         for (i = 0x20; i > 0x02; i /= 2) {
633                 if ((ols->flag_reg & i) == 0)
634                         num_channels++;
635         }
636
637         if (revents == G_IO_IN
638             && ols->num_transfers / num_channels <= ols->limit_samples) {
639                 if (serial_read(fd, &byte, 1) != 1)
640                         return FALSE;
641
642                 ols->sample[ols->num_bytes++] = byte;
643                 sr_dbg("ols: received byte 0x%.2x", byte);
644                 if (ols->num_bytes == num_channels) {
645                         /* Got a full sample. */
646                         sr_dbg("ols: received sample 0x%.*x",
647                                ols->num_bytes * 2, (int) *ols->sample);
648                         if (ols->flag_reg & FLAG_RLE) {
649                                 /*
650                                  * In RLE mode -1 should never come in as a
651                                  * sample, because bit 31 is the "count" flag.
652                                  * TODO: Endianness may be wrong here, could be
653                                  * sample[3].
654                                  */
655                                 if (ols->sample[0] & 0x80
656                                     && !(ols->last_sample[0] & 0x80)) {
657                                         count = (int)(*ols->sample) & 0x7fffffff;
658                                         if (!(buffer = g_try_malloc(count))) {
659                                                 sr_err("ols: %s: buffer malloc "
660                                                        "failed", __func__);
661                                                 return FALSE;
662                                         }
663
664                                         buflen = 0;
665                                         for (i = 0; i < count; i++) {
666                                                 memcpy(buffer + buflen, ols->last_sample, 4);
667                                                 buflen += 4;
668                                         }
669                                 } else {
670                                         /*
671                                          * Just a single sample, next sample
672                                          * will probably be a count referring
673                                          * to this -- but this one is still a
674                                          * part of the stream.
675                                          */
676                                         buffer = ols->sample;
677                                         buflen = 4;
678                                 }
679                         } else {
680                                 /* No compression. */
681                                 buffer = ols->sample;
682                                 buflen = 4;
683                         }
684
685                         if (num_channels < 4) {
686                                 /*
687                                  * Some channel groups may have been turned
688                                  * off, to speed up transfer between the
689                                  * hardware and the PC. Expand that here before
690                                  * submitting it over the session bus --
691                                  * whatever is listening on the bus will be
692                                  * expecting a full 32-bit sample, based on
693                                  * the number of probes.
694                                  */
695                                 j = 0;
696                                 memset(ols->tmp_sample, 0, 4);
697                                 for (i = 0; i < 4; i++) {
698                                         if (((ols->flag_reg >> 2) & (1 << i)) == 0) {
699                                                 /*
700                                                  * This channel group was
701                                                  * enabled, copy from received
702                                                  * sample.
703                                                  */
704                                                 ols->tmp_sample[i] = ols->sample[j++];
705                                         }
706                                 }
707                                 memcpy(ols->sample, ols->tmp_sample, 4);
708                                 sr_dbg("ols: full sample 0x%.8x", (int) *ols->sample);
709                         }
710
711                         /* the OLS sends its sample buffer backwards.
712                          * store it in reverse order here, so we can dump
713                          * this on the session bus later.
714                          */
715                         offset = (ols->limit_samples - ols->num_transfers / num_channels) * 4;
716                         memcpy(ols->raw_sample_buf + offset, ols->sample, 4);
717
718                         if (buffer == ols->sample)
719                                 memcpy(ols->last_sample, buffer, num_channels);
720                         else
721                                 g_free(buffer);
722
723                         memset(ols->sample, 0, 4);
724                         ols->num_bytes = 0;
725                 }
726         } else {
727                 /*
728                  * This is the main loop telling us a timeout was reached, or
729                  * we've acquired all the samples we asked for -- we're done.
730                  * Send the (properly-ordered) buffer to the frontend.
731                  */
732                 if (ols->trigger_at != -1) {
733                         /* a trigger was set up, so we need to tell the frontend
734                          * about it.
735                          */
736                         if (ols->trigger_at > 0) {
737                                 /* there are pre-trigger samples, send those first */
738                                 packet.type = SR_DF_LOGIC;
739                                 packet.length = ols->trigger_at * 4;
740                                 packet.unitsize = 4;
741                                 packet.payload = ols->raw_sample_buf;
742                                 sr_session_bus(user_data, &packet);
743                         }
744
745                         packet.type = SR_DF_TRIGGER;
746                         packet.length = 0;
747                         sr_session_bus(user_data, &packet);
748
749                         packet.type = SR_DF_LOGIC;
750                         packet.length = (ols->limit_samples * 4) - (ols->trigger_at * 4);
751                         packet.unitsize = 4;
752                         packet.payload = ols->raw_sample_buf + ols->trigger_at * 4;
753                         sr_session_bus(user_data, &packet);
754                 } else {
755                         packet.type = SR_DF_LOGIC;
756                         packet.length = ols->limit_samples * 4;
757                         packet.unitsize = 4;
758                         packet.payload = ols->raw_sample_buf;
759                         sr_session_bus(user_data, &packet);
760                 }
761                 free(ols->raw_sample_buf);
762
763                 serial_flush(fd);
764                 serial_close(fd);
765                 packet.type = SR_DF_END;
766                 packet.length = 0;
767                 sr_session_bus(user_data, &packet);
768         }
769
770         return TRUE;
771 }
772
773 static int hw_start_acquisition(int device_index, gpointer session_device_id)
774 {
775         struct sr_datafeed_packet *packet;
776         struct sr_datafeed_header *header;
777         struct sr_device_instance *sdi;
778         struct ols_device *ols;
779         uint32_t trigger_config[4];
780         uint32_t data;
781         uint16_t readcount, delaycount;
782         uint8_t changrp_mask;
783         int i;
784
785         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
786                 return SR_ERR;
787         ols = sdi->priv;
788
789         if (sdi->status != SR_ST_ACTIVE)
790                 return SR_ERR;
791
792         readcount = ols->limit_samples / 4;
793
794         memset(trigger_config, 0, 16);
795         trigger_config[ols->num_stages - 1] |= 0x08;
796         if (ols->trigger_mask[0]) {
797                 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
798                 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
799
800                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
801                         reverse32(ols->trigger_mask[0])) != SR_OK)
802                         return SR_ERR;
803                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
804                         reverse32(ols->trigger_value[0])) != SR_OK)
805                         return SR_ERR;
806                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
807                         trigger_config[0]) != SR_OK)
808                         return SR_ERR;
809
810                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
811                         reverse32(ols->trigger_mask[1])) != SR_OK)
812                         return SR_ERR;
813                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
814                         reverse32(ols->trigger_value[1])) != SR_OK)
815                         return SR_ERR;
816                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
817                         trigger_config[1]) != SR_OK)
818                         return SR_ERR;
819
820                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
821                         reverse32(ols->trigger_mask[2])) != SR_OK)
822                         return SR_ERR;
823                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
824                         reverse32(ols->trigger_value[2])) != SR_OK)
825                         return SR_ERR;
826                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
827                         trigger_config[2]) != SR_OK)
828                         return SR_ERR;
829
830                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
831                         reverse32(ols->trigger_mask[3])) != SR_OK)
832                         return SR_ERR;
833                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
834                         reverse32(ols->trigger_value[3])) != SR_OK)
835                         return SR_ERR;
836                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
837                         trigger_config[3]) != SR_OK)
838                         return SR_ERR;
839         } else {
840                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
841                                 ols->trigger_mask[0]) != SR_OK)
842                         return SR_ERR;
843                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
844                                 ols->trigger_value[0]) != SR_OK)
845                         return SR_ERR;
846                 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
847                      0x00000008) != SR_OK)
848                         return SR_ERR;
849                 delaycount = readcount;
850         }
851
852         sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
853                 "demux %s)", ols->cur_samplerate, ols->cur_samplerate_divider,
854                 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
855         if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
856                         reverse32(ols->cur_samplerate_divider)) != SR_OK)
857                 return SR_ERR;
858
859         /* Send sample limit and pre/post-trigger capture ratio. */
860         data = ((readcount - 1) & 0xffff) << 16;
861         data |= (delaycount - 1) & 0xffff;
862         if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
863                 return SR_ERR;
864
865         /*
866          * Enable/disable channel groups in the flag register according to the
867          * probe mask.
868          */
869         changrp_mask = 0;
870         for (i = 0; i < 4; i++) {
871                 if (ols->probe_mask & (0xff << (i * 8)))
872                         changrp_mask |= (1 << i);
873         }
874
875         /* The flag register wants them here, and 1 means "disable channel". */
876         ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
877         ols->flag_reg |= FLAG_FILTER;
878         data = ols->flag_reg << 24;
879         if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
880                 return SR_ERR;
881
882         /* Start acquisition on the device. */
883         if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
884                 return SR_ERR;
885
886         sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
887                       session_device_id);
888
889         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
890                 sr_err("ols: %s: packet malloc failed", __func__);
891                 return SR_ERR_MALLOC;
892         }
893
894         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
895                 sr_err("ols: %s: header malloc failed", __func__);
896                 return SR_ERR_MALLOC;
897         }
898
899         /* Send header packet to the session bus. */
900         packet->type = SR_DF_HEADER;
901         packet->length = sizeof(struct sr_datafeed_header);
902         packet->payload = (unsigned char *)header;
903         header->feed_version = 1;
904         gettimeofday(&header->starttime, NULL);
905         header->samplerate = ols->cur_samplerate;
906         header->protocol_id = SR_PROTO_RAW;
907         header->num_logic_probes = NUM_PROBES;
908         header->num_analog_probes = 0;
909         sr_session_bus(session_device_id, packet);
910         g_free(header);
911         g_free(packet);
912
913         return SR_OK;
914 }
915
916 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
917 {
918         struct sr_datafeed_packet packet;
919
920         /* Avoid compiler warnings. */
921         device_index = device_index;
922
923         packet.type = SR_DF_END;
924         packet.length = 0;
925         sr_session_bus(session_device_id, &packet);
926 }
927
928 struct sr_device_plugin ols_plugin_info = {
929         "ols",
930         "Openbench Logic Sniffer",
931         1,
932         hw_init,
933         hw_cleanup,
934         hw_opendev,
935         hw_closedev,
936         hw_get_device_info,
937         hw_get_status,
938         hw_get_capabilities,
939         hw_set_configuration,
940         hw_start_acquisition,
941         hw_stop_acquisition,
942 };