]> sigrok.org Git - libsigrok.git/blob - src/hardware/pipistrello-ols/protocol.c
dbe79c6a5a385283e5912d1b12231ab40cc6565c
[libsigrok.git] / src / hardware / pipistrello-ols / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 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 <config.h>
21 #include "protocol.h"
22
23 SR_PRIV int write_shortcommand(struct dev_context *devc, uint8_t command)
24 {
25         uint8_t buf[1];
26         int bytes_written;
27
28         sr_dbg("Sending cmd 0x%.2x.", command);
29         buf[0] = command;
30         bytes_written = ftdi_write_data(devc->ftdic, buf, 1);
31         if (bytes_written < 0) {
32                 sr_err("Failed to write FTDI data (%d): %s.",
33                        bytes_written, ftdi_get_error_string(devc->ftdic));
34                 return SR_ERR;
35         } else if (bytes_written != 1) {
36                 sr_err("FTDI write error, only %d/%d bytes written: %s.",
37                        bytes_written, 1, ftdi_get_error_string(devc->ftdic));
38                 return SR_ERR;
39         }
40
41         return SR_OK;
42 }
43
44 SR_PRIV int write_longcommand(struct dev_context *devc, uint8_t command, uint8_t *data)
45 {
46         uint8_t buf[5];
47         int bytes_written;
48
49         sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
50                         data[0], data[1], data[2], data[3]);
51         buf[0] = command;
52         buf[1] = data[0];
53         buf[2] = data[1];
54         buf[3] = data[2];
55         buf[4] = data[3];
56         bytes_written = ftdi_write_data(devc->ftdic, buf, 5);
57         if (bytes_written < 0) {
58                 sr_err("Failed to write FTDI data (%d): %s.",
59                        bytes_written, ftdi_get_error_string(devc->ftdic));
60                 return SR_ERR;
61         } else if (bytes_written != 5) {
62                 sr_err("FTDI write error, only %d/%d bytes written: %s.",
63                        bytes_written, 1, ftdi_get_error_string(devc->ftdic));
64                 return SR_ERR;
65         }
66
67         return SR_OK;
68 }
69
70 SR_PRIV int p_ols_open(struct dev_context *devc)
71 {
72         int ret;
73
74         /* Note: Caller checks devc and devc->ftdic. */
75
76         /* Select interface B, otherwise communication will fail. */
77         ret = ftdi_set_interface(devc->ftdic, INTERFACE_B);
78         if (ret < 0) {
79                 sr_err("Failed to set FTDI interface B (%d): %s", ret,
80                        ftdi_get_error_string(devc->ftdic));
81                 return SR_ERR;
82         }
83
84         /* Check for the device and temporarily open it. */
85         ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
86                                  USB_IPRODUCT, NULL);
87         if (ret < 0) {
88                 /* Log errors, except for -3 ("device not found"). */
89                 if (ret != -3)
90                         sr_err("Failed to open device (%d): %s", ret,
91                                ftdi_get_error_string(devc->ftdic));
92                 return SR_ERR;
93         }
94
95         /* Purge RX/TX buffers in the FTDI chip. */
96         if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
97                 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
98                        ret, ftdi_get_error_string(devc->ftdic));
99                 goto err_open_close_ftdic;
100         }
101
102         /* Reset the FTDI bitmode. */
103         ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
104         if (ret < 0) {
105                 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
106                        ret, ftdi_get_error_string(devc->ftdic));
107                 goto err_open_close_ftdic;
108         }
109
110         /* Set the FTDI latency timer to 16. */
111         ret = ftdi_set_latency_timer(devc->ftdic, 16);
112         if (ret < 0) {
113                 sr_err("Failed to set FTDI latency timer (%d): %s.",
114                        ret, ftdi_get_error_string(devc->ftdic));
115                 goto err_open_close_ftdic;
116         }
117
118         /* Set the FTDI read data chunk size to 64kB. */
119         ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
120         if (ret < 0) {
121                 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
122                        ret, ftdi_get_error_string(devc->ftdic));
123                 goto err_open_close_ftdic;
124         }
125
126         return SR_OK;
127
128 err_open_close_ftdic:
129         ftdi_usb_close(devc->ftdic);
130
131         return SR_ERR;
132 }
133
134 SR_PRIV int p_ols_close(struct dev_context *devc)
135 {
136         int ret;
137
138         /* Note: Caller checks devc and devc->ftdic. */
139
140         if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
141                 sr_err("Failed to close FTDI device (%d): %s.",
142                        ret, ftdi_get_error_string(devc->ftdic));
143                 return SR_ERR;
144         }
145
146         return SR_OK;
147 }
148
149 /* Configures the channel mask based on which channels are enabled. */
150 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
151 {
152         struct dev_context *devc;
153         struct sr_channel *channel;
154         const GSList *l;
155
156         devc = sdi->priv;
157
158         devc->channel_mask = 0;
159         for (l = sdi->channels; l; l = l->next) {
160                 channel = l->data;
161                 if (channel->enabled)
162                         devc->channel_mask |= 1 << channel->index;
163         }
164 }
165
166 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
167 {
168         struct dev_context *devc;
169         struct sr_trigger *trigger;
170         struct sr_trigger_stage *stage;
171         struct sr_trigger_match *match;
172         const GSList *l, *m;
173         int i;
174
175         devc = sdi->priv;
176
177         devc->num_stages = 0;
178         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
179                 devc->trigger_mask[i] = 0;
180                 devc->trigger_value[i] = 0;
181                 devc->trigger_edge[i] = 0;
182         }
183
184         if (!(trigger = sr_session_trigger_get(sdi->session)))
185                 return SR_OK;
186
187         devc->num_stages = g_slist_length(trigger->stages);
188         if (devc->num_stages > NUM_TRIGGER_STAGES) {
189                 sr_err("This device only supports %d trigger stages.",
190                                 NUM_TRIGGER_STAGES);
191                 return SR_ERR;
192         }
193
194         for (l = trigger->stages; l; l = l->next) {
195                 stage = l->data;
196                 for (m = stage->matches; m; m = m->next) {
197                         match = m->data;
198                         if (!match->channel->enabled)
199                                 /* Ignore disabled channels with a trigger. */
200                                 continue;
201                         devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
202                         if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
203                                 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
204                         if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
205                                 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
206                 }
207         }
208
209         return SR_OK;
210 }
211
212 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
213 {
214         struct sr_dev_inst *sdi;
215         uint32_t tmp_int, ui;
216         uint8_t key, type, token;
217         GString *tmp_str, *devname, *version;
218         guchar tmp_c;
219         int index, i;
220
221         sdi = g_malloc0(sizeof(struct sr_dev_inst));
222         sdi->status = SR_ST_INACTIVE;
223         sdi->priv = devc;
224
225         devname = g_string_new("");
226         version = g_string_new("");
227
228         index = 0;
229         while (index < bytes_read) {
230                 key = buf[index++];
231                 if (key == 0x00) {
232                         sr_dbg("Got metadata key 0x00, metadata ends.");
233                         break;
234                 }
235                 type = key >> 5;
236                 token = key & 0x1f;
237                 switch (type) {
238                 case 0:
239                         /* NULL-terminated string */
240                         tmp_str = g_string_new("");
241                         while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
242                                 g_string_append_c(tmp_str, tmp_c);
243                         sr_dbg("Got metadata key 0x%.2x value '%s'.",
244                                key, tmp_str->str);
245                         switch (token) {
246                         case 0x01:
247                                 /* Device name */
248                                 devname = g_string_append(devname, tmp_str->str);
249                                 break;
250                         case 0x02:
251                                 /* FPGA firmware version */
252                                 if (version->len)
253                                         g_string_append(version, ", ");
254                                 g_string_append(version, "FPGA version ");
255                                 g_string_append(version, tmp_str->str);
256                                 break;
257                         case 0x03:
258                                 /* Ancillary version */
259                                 if (version->len)
260                                         g_string_append(version, ", ");
261                                 g_string_append(version, "Ancillary version ");
262                                 g_string_append(version, tmp_str->str);
263                                 break;
264                         default:
265                                 sr_info("Unknown token 0x%.2x: '%s'",
266                                         token, tmp_str->str);
267                                 break;
268                         }
269                         g_string_free(tmp_str, TRUE);
270                         break;
271                 case 1:
272                         /* 32-bit unsigned integer */
273                         tmp_int = 0;
274                         for (i = 0; i < 4; i++) {
275                                 tmp_int = (tmp_int << 8) | buf[index++];
276                         }
277                         sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
278                                key, tmp_int);
279                         switch (token) {
280                         case 0x00:
281                                 /* Number of usable channels */
282                                 for (ui = 0; ui < tmp_int; ui++)
283                                         sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
284                                                         p_ols_channel_names[ui]);
285                                 break;
286                         case 0x01:
287                                 /* Amount of sample memory available (bytes) */
288                                 devc->max_samplebytes = tmp_int;
289                                 break;
290                         case 0x02:
291                                 /* Amount of dynamic memory available (bytes) */
292                                 /* what is this for? */
293                                 break;
294                         case 0x03:
295                                 /* Maximum sample rate (Hz) */
296                                 devc->max_samplerate = tmp_int;
297                                 break;
298                         case 0x04:
299                                 /* protocol version */
300                                 devc->protocol_version = tmp_int;
301                                 break;
302                         default:
303                                 sr_info("Unknown token 0x%.2x: 0x%.8x.",
304                                         token, tmp_int);
305                                 break;
306                         }
307                         break;
308                 case 2:
309                         /* 8-bit unsigned integer */
310                         tmp_c = buf[index++];
311                         sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
312                                key, tmp_c);
313                         switch (token) {
314                         case 0x00:
315                                 /* Number of usable channels */
316                                 for (ui = 0; ui < tmp_c; ui++)
317                                         sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
318                                                         p_ols_channel_names[ui]);
319                                 break;
320                         case 0x01:
321                                 /* protocol version */
322                                 devc->protocol_version = tmp_c;
323                                 break;
324                         default:
325                                 sr_info("Unknown token 0x%.2x: 0x%.2x.",
326                                         token, tmp_c);
327                                 break;
328                         }
329                         break;
330                 default:
331                         /* unknown type */
332                         break;
333                 }
334         }
335
336         sdi->model = devname->str;
337         sdi->version = version->str;
338         g_string_free(devname, FALSE);
339         g_string_free(version, FALSE);
340
341         return sdi;
342 }
343
344 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
345                 const uint64_t samplerate)
346 {
347         struct dev_context *devc;
348
349         devc = sdi->priv;
350         if (devc->max_samplerate && samplerate > devc->max_samplerate)
351                 return SR_ERR_SAMPLERATE;
352
353         if (samplerate > CLOCK_RATE) {
354                 sr_info("Enabling demux mode.");
355                 devc->flag_reg |= FLAG_DEMUX;
356                 devc->flag_reg &= ~FLAG_FILTER;
357                 devc->max_channels = NUM_CHANNELS / 2;
358                 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
359         } else {
360                 sr_info("Disabling demux mode.");
361                 devc->flag_reg &= ~FLAG_DEMUX;
362                 devc->flag_reg |= FLAG_FILTER;
363                 devc->max_channels = NUM_CHANNELS;
364                 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
365         }
366
367         /* Calculate actual samplerate used and complain if it is different
368          * from the requested.
369          */
370         devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
371         if (devc->flag_reg & FLAG_DEMUX)
372                 devc->cur_samplerate *= 2;
373         if (devc->cur_samplerate != samplerate)
374                 sr_info("Can't match samplerate %" PRIu64 ", using %"
375                        PRIu64 ".", samplerate, devc->cur_samplerate);
376
377         return SR_OK;
378 }
379
380 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
381 {
382         struct dev_context *devc;
383         struct sr_dev_inst *sdi;
384         struct sr_datafeed_packet packet;
385         struct sr_datafeed_logic logic;
386         uint32_t sample;
387         int num_channels, offset, j;
388         int bytes_read, index;
389         unsigned int i;
390         unsigned char byte;
391
392         (void)fd;
393         (void)revents;
394
395         sdi = cb_data;
396         devc = sdi->priv;
397
398         if (devc->num_transfers++ == 0) {
399                 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
400                 if (!devc->raw_sample_buf) {
401                         sr_err("Sample buffer malloc failed.");
402                         return FALSE;
403                 }
404                 /* fill with 1010... for debugging */
405                 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
406         }
407
408         if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
409
410                 num_channels = 0;
411                 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
412                         if ((devc->flag_reg & i) == 0) {
413                                 num_channels++;
414                         }
415                 }
416
417                 /* Get a block of data. */
418                 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
419                 if (bytes_read < 0) {
420                         sr_err("Failed to read FTDI data (%d): %s.",
421                                bytes_read, ftdi_get_error_string(devc->ftdic));
422                         sr_dev_acquisition_stop(sdi);
423                         return FALSE;
424                 }
425                 if (bytes_read == 0) {
426                         sr_spew("Received 0 bytes, nothing to do.");
427                         return TRUE;
428                 }
429
430                 sr_dbg("Received %d bytes", bytes_read);
431
432                 index = 0;
433                 while (index < bytes_read) {
434                         byte = devc->ftdi_buf[index++];
435                         devc->cnt_bytes++;
436
437                         devc->sample[devc->num_bytes++] = byte;
438                         sr_spew("Received byte 0x%.2x.", byte);
439
440                         if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
441                                 /* RLE in demux mode must be processed differently
442                                 * since in this case the RLE encoder is operating on pairs of samples.
443                                 */
444                                 if (devc->num_bytes == num_channels * 2) {
445                                         devc->cnt_samples += 2;
446                                         devc->cnt_samples_rle += 2;
447                                         /*
448                                          * Got a sample pair. Convert from the OLS's little-endian
449                                          * sample to the local format.
450                                          */
451                                         sample = devc->sample[0] | (devc->sample[1] << 8) \
452                                                         | (devc->sample[2] << 16) | (devc->sample[3] << 24);
453                                         sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
454
455                                         /*
456                                          * In RLE mode the high bit of the sample pair is the
457                                          * "count" flag, meaning this sample pair is the number
458                                          * of times the previous sample pair occurred.
459                                          */
460                                         if (devc->sample[devc->num_bytes - 1] & 0x80) {
461                                                 /* Clear the high bit. */
462                                                 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
463                                                 devc->rle_count = sample;
464                                                 devc->cnt_samples_rle += devc->rle_count * 2;
465                                                 sr_dbg("RLE count: %u.", devc->rle_count * 2);
466                                                 devc->num_bytes = 0;
467                                                 continue;
468                                         }
469                                         devc->num_samples += (devc->rle_count + 1) * 2;
470                                         if (devc->num_samples > devc->limit_samples) {
471                                                 /* Save us from overrunning the buffer. */
472                                                 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
473                                                 devc->num_samples = devc->limit_samples;
474                                                 index = bytes_read;
475                                         }
476
477                                         /*
478                                          * Some channel groups may have been turned
479                                          * off, to speed up transfer between the
480                                          * hardware and the PC. Expand that here before
481                                          * submitting it over the session bus --
482                                          * whatever is listening on the bus will be
483                                          * expecting a full 32-bit sample, based on
484                                          * the number of channels.
485                                          */
486                                         j = 0;
487                                         /* expand first sample */
488                                         memset(devc->tmp_sample, 0, 4);
489                                         for (i = 0; i < 2; i++) {
490                                                 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
491                                                         /*
492                                                          * This channel group was
493                                                          * enabled, copy from received
494                                                          * sample.
495                                                          */
496                                                         devc->tmp_sample[i] = devc->sample[j++];
497                                                 }
498                                         }
499                                         /* Clear out the most significant bit of the sample */
500                                         devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
501                                         sr_spew("Expanded sample 1: 0x%.2x%.2x%.2x%.2x.",
502                                                 devc->tmp_sample[3], devc->tmp_sample[2],
503                                                 devc->tmp_sample[1], devc->tmp_sample[0]);
504
505                                         /* expand second sample */
506                                         memset(devc->tmp_sample2, 0, 4);
507                                         for (i = 0; i < 2; i++) {
508                                                 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
509                                                         /*
510                                                          * This channel group was
511                                                          * enabled, copy from received
512                                                          * sample.
513                                                          */
514                                                         devc->tmp_sample2[i] = devc->sample[j++];
515                                                 }
516                                         }
517                                         /* Clear out the most significant bit of the sample */
518                                         devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
519                                         sr_spew("Expanded sample 2: 0x%.2x%.2x%.2x%.2x.",
520                                                 devc->tmp_sample2[3], devc->tmp_sample2[2],
521                                                 devc->tmp_sample2[1], devc->tmp_sample2[0]);
522
523                                         /*
524                                          * OLS sends its sample buffer backwards.
525                                          * store it in reverse order here, so we can dump
526                                          * this on the session bus later.
527                                          */
528                                         offset = (devc->limit_samples - devc->num_samples) * 4;
529                                         for (i = 0; i <= devc->rle_count; i++) {
530                                                 memcpy(devc->raw_sample_buf + offset + (i * 8),
531                                                                          devc->tmp_sample2, 4);
532                                                 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
533                                                                          devc->tmp_sample, 4);
534                                         }
535                                         memset(devc->sample, 0, 4);
536                                         devc->num_bytes = 0;
537                                         devc->rle_count = 0;
538                                 }
539                         }
540                         else {
541                                 if (devc->num_bytes == num_channels) {
542                                         devc->cnt_samples++;
543                                         devc->cnt_samples_rle++;
544                                         /*
545                                          * Got a full sample. Convert from the OLS's little-endian
546                                          * sample to the local format.
547                                          */
548                                         sample = devc->sample[0] | (devc->sample[1] << 8) \
549                                                         | (devc->sample[2] << 16) | (devc->sample[3] << 24);
550                                         sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
551                                         if (devc->flag_reg & FLAG_RLE) {
552                                                 /*
553                                                  * In RLE mode the high bit of the sample is the
554                                                  * "count" flag, meaning this sample is the number
555                                                  * of times the previous sample occurred.
556                                                  */
557                                                 if (devc->sample[devc->num_bytes - 1] & 0x80) {
558                                                         /* Clear the high bit. */
559                                                         sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
560                                                         devc->rle_count = sample;
561                                                         devc->cnt_samples_rle += devc->rle_count;
562                                                         sr_dbg("RLE count: %u.", devc->rle_count);
563                                                         devc->num_bytes = 0;
564                                                         continue;
565                                                 }
566                                         }
567                                         devc->num_samples += devc->rle_count + 1;
568                                         if (devc->num_samples > devc->limit_samples) {
569                                                 /* Save us from overrunning the buffer. */
570                                                 devc->rle_count -= devc->num_samples - devc->limit_samples;
571                                                 devc->num_samples = devc->limit_samples;
572                                                 index = bytes_read;
573                                         }
574
575                                         if (num_channels < 4) {
576                                                 /*
577                                                  * Some channel groups may have been turned
578                                                  * off, to speed up transfer between the
579                                                  * hardware and the PC. Expand that here before
580                                                  * submitting it over the session bus --
581                                                  * whatever is listening on the bus will be
582                                                  * expecting a full 32-bit sample, based on
583                                                  * the number of channels.
584                                                  */
585                                                 j = 0;
586                                                 memset(devc->tmp_sample, 0, 4);
587                                                 for (i = 0; i < 4; i++) {
588                                                         if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
589                                                                 /*
590                                                                  * This channel group was
591                                                                  * enabled, copy from received
592                                                                  * sample.
593                                                                  */
594                                                                 devc->tmp_sample[i] = devc->sample[j++];
595                                                         }
596                                                 }
597                                                 memcpy(devc->sample, devc->tmp_sample, 4);
598                                                 sr_spew("Expanded sample: 0x%.8x.", sample);
599                                         }
600
601                                         /*
602                                          * Pipistrello OLS sends its sample buffer backwards.
603                                          * store it in reverse order here, so we can dump
604                                          * this on the session bus later.
605                                          */
606                                         offset = (devc->limit_samples - devc->num_samples) * 4;
607                                         for (i = 0; i <= devc->rle_count; i++) {
608                                                 memcpy(devc->raw_sample_buf + offset + (i * 4),
609                                                                          devc->sample, 4);
610                                         }
611                                         memset(devc->sample, 0, 4);
612                                         devc->num_bytes = 0;
613                                         devc->rle_count = 0;
614                                 }
615                         }
616                 }
617                 return TRUE;
618         } else {
619                 do {
620                         bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
621                 } while (bytes_read > 0);
622
623                 /*
624                  * We've acquired all the samples we asked for -- we're done.
625                  * Send the (properly-ordered) buffer to the frontend.
626                  */
627                 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
628                                 devc->cnt_bytes, devc->cnt_samples,
629                                 devc->cnt_samples_rle);
630                 if (devc->trigger_at != -1) {
631                         /*
632                          * A trigger was set up, so we need to tell the frontend
633                          * about it.
634                          */
635                         if (devc->trigger_at > 0) {
636                                 /* There are pre-trigger samples, send those first. */
637                                 packet.type = SR_DF_LOGIC;
638                                 packet.payload = &logic;
639                                 logic.length = devc->trigger_at * 4;
640                                 logic.unitsize = 4;
641                                 logic.data = devc->raw_sample_buf +
642                                         (devc->limit_samples - devc->num_samples) * 4;
643                                 sr_session_send(sdi, &packet);
644                         }
645
646                         /* Send the trigger. */
647                         packet.type = SR_DF_TRIGGER;
648                         sr_session_send(sdi, &packet);
649
650                         /* Send post-trigger samples. */
651                         packet.type = SR_DF_LOGIC;
652                         packet.payload = &logic;
653                         logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
654                         logic.unitsize = 4;
655                         logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
656                                 (devc->limit_samples - devc->num_samples) * 4;
657                         sr_session_send(sdi, &packet);
658                 } else {
659                         /* no trigger was used */
660                         packet.type = SR_DF_LOGIC;
661                         packet.payload = &logic;
662                         logic.length = devc->num_samples * 4;
663                         logic.unitsize = 4;
664                         logic.data = devc->raw_sample_buf +
665                                 (devc->limit_samples - devc->num_samples) * 4;
666                         sr_session_send(sdi, &packet);
667                 }
668                 g_free(devc->raw_sample_buf);
669
670                 sr_dev_acquisition_stop(sdi);
671         }
672
673         return TRUE;
674 }