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