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