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