]> sigrok.org Git - libsigrok.git/blob - src/hardware/baylibre-acme/protocol.c
baylibre-acme: Read EEPROM contents into buffer before processing.
[libsigrok.git] / src / hardware / baylibre-acme / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.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 <string.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <arpa/inet.h>
26 #include <glib/gstdio.h>
27 #include "protocol.h"
28 #include "gpio.h"
29
30 #define ACME_REV_A              1
31 #define ACME_REV_B              2
32
33 enum channel_type {
34         ENRG_PWR = 1,
35         ENRG_CURR,
36         ENRG_VOL,
37         TEMP_IN,
38         TEMP_OUT,
39 };
40
41 struct channel_group_priv {
42         uint8_t rev;
43         int hwmon_num;
44         int probe_type;
45         int index;
46         int has_pws;
47         uint32_t pws_gpio;
48 };
49
50 struct channel_priv {
51         int ch_type;
52         int fd;
53         float val;
54         struct channel_group_priv *probe;
55 };
56
57 #define EEPROM_SERIAL_SIZE              16
58 #define EEPROM_TAG_SIZE                 32
59
60 #define EEPROM_PROBE_TYPE_USB           1
61 #define EEPROM_PROBE_TYPE_JACK          2
62 #define EEPROM_PROBE_TYPE_HE10          3
63
64 struct probe_eeprom {
65         uint32_t type;
66         uint32_t rev;
67         uint32_t shunt;
68         uint8_t pwr_sw;
69         uint8_t serial[EEPROM_SERIAL_SIZE];
70         int8_t tag[EEPROM_TAG_SIZE];
71 };
72
73 #define EEPROM_SIZE (3 * sizeof(uint32_t) + 1 + EEPROM_SERIAL_SIZE + EEPROM_TAG_SIZE)
74
75 #define EEPROM_OFF_TYPE         0
76 #define EEPROM_OFF_REV          sizeof(uint32_t)
77 #define EEPROM_OFF_SHUNT        (2 * sizeof(uint32_t))
78 #define EEPROM_OFF_PWR_SW       (3 * sizeof(uint32_t))
79 #define EEPROM_OFF_SERIAL       (3 * sizeof(uint32_t) + 1)
80 #define EEPROM_OFF_TAG          (EEPROM_OFF_SERIAL + EEPROM_SERIAL_SIZE)
81
82 static const uint8_t enrg_i2c_addrs[] = {
83         0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
84 };
85
86 static const uint8_t temp_i2c_addrs[] = {
87         0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
88 };
89
90 static const uint32_t revA_pws_gpios[] = {
91         486, 498, 502, 482, 478, 506, 510, 474,
92 };
93
94 static const uint32_t revA_pws_info_gpios[] = {
95         487, 499, 503, 483, 479, 507, 511, 475,
96 };
97
98 static const uint32_t revB_pws_gpios[] = {
99         489, 491, 493, 495, 497, 499, 501, 503,
100 };
101
102 #define MOHM_TO_UOHM(x) ((x) * 1000)
103 #define UOHM_TO_MOHM(x) ((x) / 1000)
104
105 SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
106 {
107         return enrg_i2c_addrs[index];
108 }
109
110 SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
111 {
112         return temp_i2c_addrs[index];
113 }
114
115 SR_PRIV gboolean bl_acme_is_sane(void)
116 {
117         gboolean status;
118
119         /*
120          * We expect sysfs to be present and mounted at /sys, ina226 and
121          * tmp435 sensors detected by the system and their appropriate
122          * drivers loaded and functional.
123          */
124         status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
125         if (!status) {
126                 sr_err("/sys/ directory not found - sysfs not mounted?");
127                 return FALSE;
128         }
129
130         return TRUE;
131 }
132
133 static void probe_name_path(unsigned int addr, GString *path)
134 {
135         g_string_printf(path,
136                         "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
137 }
138
139 /*
140  * For given address fill buf with the path to appropriate hwmon entry.
141  */
142 static void probe_hwmon_path(unsigned int addr, GString *path)
143 {
144         g_string_printf(path,
145                         "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
146 }
147
148 static void probe_eeprom_path(unsigned int addr, GString *path)
149 {
150         g_string_printf(path,
151                         "/sys/class/i2c-dev/i2c-1/device/1-00%02x/eeprom",
152                         addr + 0x10);
153 }
154
155 SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
156                                       int prb_num, const char *prb_name)
157 {
158         gboolean ret = FALSE, status;
159         char *buf = NULL;
160         GString *path = g_string_sized_new(64);
161         GError *err = NULL;
162         gsize size;
163
164         probe_name_path(addr, path);
165         status = g_file_get_contents(path->str, &buf, &size, &err);
166         if (!status) {
167                 sr_dbg("Name for probe %d can't be read: %s",
168                        prb_num, err->message);
169                 g_string_free(path, TRUE);
170                 return ret;
171         }
172
173         if (!strncmp(buf, prb_name, strlen(prb_name))) {
174                 /*
175                  * Correct driver registered on this address - but is
176                  * there an actual probe connected?
177                  */
178                 probe_hwmon_path(addr, path);
179                 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
180                 if (status) {
181                         /* We have found an ACME probe. */
182                         ret = TRUE;
183                 }
184         }
185
186         g_free(buf);
187         g_string_free(path, TRUE);
188
189         return ret;
190 }
191
192 static int get_hwmon_index(unsigned int addr)
193 {
194         int status, hwmon;
195         GString *path = g_string_sized_new(64);
196         GError *err = NULL;
197         GDir *dir;
198
199         probe_hwmon_path(addr, path);
200         dir = g_dir_open(path->str, 0, &err);
201         if (!dir) {
202                 sr_err("Error opening %s: %s", path->str, err->message);
203                 g_string_free(path, TRUE);
204                 return -1;
205         }
206
207         g_string_free(path, TRUE);
208
209         /*
210          * The directory should contain a single file named hwmonX where X
211          * is the hwmon index.
212          */
213         status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
214         g_dir_close(dir);
215         if (status != 1) {
216                 sr_err("Unable to determine the hwmon entry");
217                 return -1;
218         }
219
220         return hwmon;
221 }
222
223 static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
224                            int index, int type)
225 {
226         struct channel_priv *cp;
227         struct dev_context *devc;
228         struct sr_channel *ch;
229         char *name;
230
231         devc = sdi->priv;
232
233         switch (type) {
234         case ENRG_PWR:
235                 name = g_strdup_printf("P%d_ENRG_PWR", index);
236                 break;
237         case ENRG_CURR:
238                 name = g_strdup_printf("P%d_ENRG_CURR", index);
239                 break;
240         case ENRG_VOL:
241                 name = g_strdup_printf("P%d_ENRG_VOL", index);
242                 break;
243         case TEMP_IN:
244                 name = g_strdup_printf("P%d_TEMP_IN", index);
245                 break;
246         case TEMP_OUT:
247                 name = g_strdup_printf("P%d_TEMP_OUT", index);
248                 break;
249         default:
250                 sr_err("Invalid channel type: %d.", type);
251                 return;
252         }
253
254         cp = g_malloc0(sizeof(struct channel_priv));
255         cp->ch_type = type;
256         cp->probe = cg->priv;
257
258         ch = sr_channel_new(sdi, devc->num_channels++,
259                             SR_CHANNEL_ANALOG, TRUE, name);
260         g_free(name);
261
262         ch->priv = cp;
263         cg->channels = g_slist_append(cg->channels, ch);
264 }
265
266 static int read_probe_eeprom(unsigned int addr, struct probe_eeprom *eeprom)
267 {
268         GString *path = g_string_sized_new(64);
269         char eeprom_buf[EEPROM_SIZE];
270         ssize_t rd;
271         int fd;
272
273         probe_eeprom_path(addr, path);
274         fd = g_open(path->str, O_RDONLY);
275         g_string_free(path, TRUE);
276         if (fd < 0)
277                 return -1;
278
279         rd = read(fd, eeprom_buf, EEPROM_SIZE);
280         g_close(fd, NULL);
281         if (rd != EEPROM_SIZE)
282                 return -1;
283
284         eeprom->type = RL32(eeprom_buf + EEPROM_OFF_TYPE);
285         eeprom->rev = RL32(eeprom_buf + EEPROM_OFF_REV);
286         eeprom->shunt = RL32(eeprom_buf + EEPROM_OFF_SHUNT);
287         eeprom->pwr_sw = R8(eeprom_buf + EEPROM_OFF_PWR_SW);
288         /* Don't care about the serial number and tag for now. */
289
290         /* Check if we have some sensible values. */
291         if (eeprom->rev != 'B')
292                 /* 'B' is the only supported revision with EEPROM for now. */
293                 return -1;
294
295         if (eeprom->type != EEPROM_PROBE_TYPE_USB &&
296             eeprom->type != EEPROM_PROBE_TYPE_JACK &&
297             eeprom->type != EEPROM_PROBE_TYPE_HE10)
298                 return -1;
299
300         return 0;
301 }
302
303 /* Some i2c slave addresses on revision B probes differ from revision A. */
304 static int revB_addr_to_num(unsigned int addr)
305 {
306         switch (addr) {
307         case 0x44:      return 5;
308         case 0x45:      return 6;
309         case 0x42:      return 3;
310         case 0x43:      return 4;
311         default:        return addr - 0x3f;
312         }
313 }
314
315 SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
316                                         unsigned int addr, int prb_num)
317 {
318         struct sr_channel_group *cg;
319         struct channel_group_priv *cgp;
320         struct probe_eeprom eeprom;
321         int hwmon, status;
322         uint32_t gpio;
323
324         /* Obtain the hwmon index. */
325         hwmon = get_hwmon_index(addr);
326         if (hwmon < 0)
327                 return FALSE;
328
329         cg = g_malloc0(sizeof(struct sr_channel_group));
330         cgp = g_malloc0(sizeof(struct channel_group_priv));
331         cg->priv = cgp;
332
333         /*
334          * See if we can read the EEPROM contents. If not, assume it's
335          * a revision A probe.
336          */
337         memset(&eeprom, 0, sizeof(struct probe_eeprom));
338         status = read_probe_eeprom(addr, &eeprom);
339         cgp->rev = status < 0 ? ACME_REV_A : ACME_REV_B;
340
341         prb_num = cgp->rev == ACME_REV_A ? prb_num : revB_addr_to_num(addr);
342
343         cgp->hwmon_num = hwmon;
344         cgp->probe_type = type;
345         cgp->index = prb_num - 1;
346         cg->name = g_strdup_printf("Probe_%d", prb_num);
347
348         if (cgp->rev == ACME_REV_A) {
349                 gpio = revA_pws_info_gpios[cgp->index];
350                 cgp->has_pws = sr_gpio_getval_export(gpio);
351                 cgp->pws_gpio = revA_pws_gpios[cgp->index];
352         } else {
353                 cgp->has_pws = eeprom.pwr_sw;
354                 cgp->pws_gpio = revB_pws_gpios[cgp->index];
355
356                 /*
357                  * For revision B we can already try to set the shunt
358                  * resistance according to the EEPROM contents.
359                  *
360                  * Keep the default value if shunt in EEPROM == 0.
361                  */
362                 if (eeprom.shunt > 0)
363                         bl_acme_set_shunt(cg, UOHM_TO_MOHM(eeprom.shunt));
364         }
365
366         if (type == PROBE_ENRG) {
367                 append_channel(sdi, cg, prb_num, ENRG_PWR);
368                 append_channel(sdi, cg, prb_num, ENRG_CURR);
369                 append_channel(sdi, cg, prb_num, ENRG_VOL);
370         } else if (type == PROBE_TEMP) {
371                 append_channel(sdi, cg, prb_num, TEMP_IN);
372                 append_channel(sdi, cg, prb_num, TEMP_OUT);
373         } else {
374                 sr_err("Invalid probe type: %d.", type);
375         }
376
377         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
378
379         return TRUE;
380 }
381
382 SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg)
383 {
384         struct channel_group_priv *cgp = cg->priv;
385
386         return cgp->probe_type;
387 }
388
389 SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg)
390 {
391         struct channel_group_priv *cgp = cg->priv;
392
393         return cgp->has_pws;
394 }
395
396 /*
397  * Sets path to the hwmon attribute if this channel group
398  * supports shunt resistance setting. The caller has to supply
399  * a valid GString.
400  */
401 static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
402 {
403         struct channel_group_priv *cgp;
404         int ret = SR_OK, status;
405
406         cgp = cg->priv;
407
408         if (cgp->probe_type != PROBE_ENRG) {
409                 sr_err("Probe doesn't support shunt resistance setting");
410                 return SR_ERR_ARG;
411         }
412
413         g_string_append_printf(path,
414                                "/sys/class/hwmon/hwmon%d/shunt_resistor",
415                                cgp->hwmon_num);
416
417         /*
418          * The shunt_resistor sysfs attribute is available
419          * in the Linux kernel since version 3.20. We have
420          * to notify the user if this attribute is not present.
421          */
422         status = g_file_test(path->str, G_FILE_TEST_EXISTS);
423         if (!status) {
424                 sr_err("shunt_resistance attribute not present, please update "
425                        "your kernel to version >=3.20");
426                 return SR_ERR_NA;
427         }
428
429         return ret;
430 }
431
432 /*
433  * Try setting the update_interval sysfs attribute for each probe according
434  * to samplerate.
435  */
436 SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi,
437                                                uint64_t samplerate)
438 {
439         struct sr_channel_group *cg;
440         struct channel_group_priv *cgp;
441         GString *hwmon;
442         GSList *l;
443         FILE *fd;
444
445         for (l = sdi->channel_groups; l != NULL; l = l->next) {
446                 cg = l->data;
447                 cgp = cg->priv;
448
449                 hwmon = g_string_sized_new(64);
450                 g_string_append_printf(hwmon,
451                                 "/sys/class/hwmon/hwmon%d/update_interval",
452                                 cgp->hwmon_num);
453
454                 if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) {
455                         fd = g_fopen(hwmon->str, "w");
456                         if (!fd) {
457                                 g_string_free(hwmon, TRUE);
458                                 continue;
459                         }
460
461                         g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate);
462                         fclose(fd);
463                 }
464
465                 g_string_free(hwmon, TRUE);
466         }
467 }
468
469 SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
470                               uint64_t *shunt)
471 {
472         GString *path = g_string_sized_new(64);
473         gchar *contents;
474         int status, ret = SR_OK;
475         GError *err = NULL;
476
477         status = get_shunt_path(cg, path);
478         if (status != SR_OK) {
479                 ret = status;
480                 goto out;
481         }
482
483         status = g_file_get_contents(path->str, &contents, NULL, &err);
484         if (!status) {
485                 sr_err("Error reading shunt resistance: %s", err->message);
486                 ret = SR_ERR_IO;
487                 goto out;
488         }
489
490         *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
491
492 out:
493         g_string_free(path, TRUE);
494         return ret;
495 }
496
497 SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
498 {
499         GString *path = g_string_sized_new(64);;
500         int status, ret = SR_OK;
501         FILE *fd;
502
503         status = get_shunt_path(cg, path);
504         if (status != SR_OK) {
505                 ret = status;
506                 goto out;
507         }
508
509         /*
510          * Can't use g_file_set_contents() here, as it calls open() with
511          * O_EXEC flag in a sysfs directory thus failing with EACCES.
512          */
513         fd = g_fopen(path->str, "w");
514         if (!fd) {
515                 sr_err("Error opening %s: %s", path->str, g_strerror(errno));
516                 ret = SR_ERR_IO;
517                 goto out;
518         }
519
520         g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
521         fclose(fd);
522
523 out:
524         g_string_free(path, TRUE);
525         return ret;
526 }
527
528 SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
529                                      gboolean *off)
530 {
531         struct channel_group_priv *cgp;
532         int val;
533
534         cgp = cg->priv;
535
536         if (!bl_acme_probe_has_pws(cg)) {
537                 sr_err("Probe has no power-switch");
538                 return SR_ERR_ARG;
539         }
540
541         val = sr_gpio_getval_export(cgp->pws_gpio);
542         *off = val ? FALSE : TRUE;
543
544         return SR_OK;
545 }
546
547 SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
548                                   gboolean off)
549 {
550         struct channel_group_priv *cgp;
551         int val;
552
553         cgp = cg->priv;
554
555         if (!bl_acme_probe_has_pws(cg)) {
556                 sr_err("Probe has no power-switch");
557                 return SR_ERR_ARG;
558         }
559
560         val = sr_gpio_setval_export(cgp->pws_gpio, off ? 0 : 1);
561         if (val < 0) {
562                 sr_err("Error setting power-off state: gpio: %d",
563                        cgp->pws_gpio);
564                 return SR_ERR_IO;
565         }
566
567         return SR_OK;
568 }
569
570 static int channel_to_mq(struct sr_channel *ch)
571 {
572         struct channel_priv *chp;
573
574         chp = ch->priv;
575
576         switch (chp->ch_type) {
577         case ENRG_PWR:
578                 return SR_MQ_POWER;
579         case ENRG_CURR:
580                 return SR_MQ_CURRENT;
581         case ENRG_VOL:
582                 return SR_MQ_VOLTAGE;
583         case TEMP_IN: /* Fallthrough */
584         case TEMP_OUT:
585                 return SR_MQ_TEMPERATURE;
586         default:
587                 return -1;
588         }
589 }
590
591 static int channel_to_unit(struct sr_channel *ch)
592 {
593         struct channel_priv *chp;
594
595         chp = ch->priv;
596
597         switch (chp->ch_type) {
598         case ENRG_PWR:
599                 return SR_UNIT_WATT;
600         case ENRG_CURR:
601                 return SR_UNIT_AMPERE;
602         case ENRG_VOL:
603                 return SR_UNIT_VOLT;
604         case TEMP_IN: /* Fallthrough */
605         case TEMP_OUT:
606                 return SR_UNIT_CELSIUS;
607         default:
608                 return -1;
609         }
610 }
611
612 /* We need to scale measurements down from the units used by the drivers. */
613 static float adjust_data(int val, int type)
614 {
615         switch (type) {
616         case ENRG_PWR:
617                 return ((float)val) / 1000000.0;
618         case ENRG_CURR: /* Fallthrough */
619         case ENRG_VOL: /* Fallthrough */
620         case TEMP_IN: /* Fallthrough */
621         case TEMP_OUT:
622                 return ((float)val) / 1000.0;
623         default:
624                 return 0.0;
625         }
626 }
627
628 static float read_sample(struct sr_channel *ch)
629 {
630         struct channel_priv *chp;
631         char buf[16];
632         ssize_t len;
633         int fd;
634
635         chp = ch->priv;
636         fd = chp->fd;
637
638         lseek(fd, 0, SEEK_SET);
639
640         len = read(fd, buf, sizeof(buf));
641         if (len < 0) {
642                 sr_err("Error reading from channel %s (hwmon: %d): %s",
643                         ch->name, chp->probe->hwmon_num, g_strerror(errno));
644                 ch->enabled = FALSE;
645                 return -1.0;
646         }
647
648         return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
649 }
650
651 SR_PRIV int bl_acme_open_channel(struct sr_channel *ch)
652 {
653         struct channel_priv *chp;
654         char path[64], *file;
655         int fd;
656
657         chp = ch->priv;
658
659         switch (chp->ch_type) {
660         case ENRG_PWR:  file = "power1_input";  break;
661         case ENRG_CURR: file = "curr1_input";   break;
662         case ENRG_VOL:  file = "in1_input";     break;
663         case TEMP_IN:   file = "temp1_input";   break;
664         case TEMP_OUT:  file = "temp2_input";   break;
665         default:
666                 sr_err("Invalid channel type: %d.", chp->ch_type);
667                 return SR_ERR;
668         }
669
670         snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
671                  chp->probe->hwmon_num, file);
672
673         fd = open(path, O_RDONLY);
674         if (fd < 0) {
675                 sr_err("Error opening %s: %s", path, g_strerror(errno));
676                 ch->enabled = FALSE;
677                 return SR_ERR;
678         }
679
680         chp->fd = fd;
681
682         return 0;
683 }
684
685 SR_PRIV void bl_acme_close_channel(struct sr_channel *ch)
686 {
687         struct channel_priv *chp;
688
689         chp = ch->priv;
690         close(chp->fd);
691         chp->fd = -1;
692 }
693
694 SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
695 {
696         uint32_t cur_time, elapsed_time;
697         uint64_t nrexpiration;
698         struct sr_datafeed_packet packet, framep;
699         struct sr_datafeed_analog analog;
700         struct sr_dev_inst *sdi;
701         struct sr_channel *ch;
702         struct channel_priv *chp;
703         struct dev_context *devc;
704         GSList *chl, chonly;
705         unsigned i;
706
707         (void)fd;
708         (void)revents;
709
710         sdi = cb_data;
711         if (!sdi)
712                 return TRUE;
713
714         devc = sdi->priv;
715         if (!devc)
716                 return TRUE;
717
718         packet.type = SR_DF_ANALOG;
719         packet.payload = &analog;
720         memset(&analog, 0, sizeof(struct sr_datafeed_analog));
721
722         if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) {
723                 sr_warn("Failed to read timer information");
724                 return TRUE;
725         }
726
727         /*
728          * We were not able to process the previous timer expiration, we are
729          * overloaded.
730          */
731         if (nrexpiration > 1)
732                 devc->samples_missed += nrexpiration - 1;
733
734         /*
735          * XXX This is a nasty workaround...
736          *
737          * At high sampling rates and maximum channels we are not able to
738          * acquire samples fast enough, even though frontends still think
739          * that samples arrive on time. This causes shifts in frontend
740          * plots.
741          *
742          * To compensate for the delay we check if any clock events were
743          * missed and - if so - don't really read the next value, but send
744          * the same sample as fast as possible. We do it until we are back
745          * on schedule.
746          *
747          * At high sampling rate this doesn't seem to visibly reduce the
748          * accuracy.
749          */
750         for (i = 0; i < nrexpiration; i++) {
751                 framep.type = SR_DF_FRAME_BEGIN;
752                 sr_session_send(cb_data, &framep);
753
754                 /*
755                  * Due to different units used in each channel we're sending
756                  * samples one-by-one.
757                  */
758                 for (chl = sdi->channels; chl; chl = chl->next) {
759                         ch = chl->data;
760                         chp = ch->priv;
761
762                         if (!ch->enabled)
763                                 continue;
764                         chonly.next = NULL;
765                         chonly.data = ch;
766                         analog.channels = &chonly;
767                         analog.num_samples = 1;
768                         analog.mq = channel_to_mq(chl->data);
769                         analog.unit = channel_to_unit(ch);
770
771                         if (i < 1)
772                                 chp->val = read_sample(ch);
773
774                         analog.data = &chp->val;
775                         sr_session_send(cb_data, &packet);
776                 }
777
778                 framep.type = SR_DF_FRAME_END;
779                 sr_session_send(cb_data, &framep);
780         }
781
782         devc->samples_read++;
783         if (devc->limit_samples > 0 &&
784             devc->samples_read >= devc->limit_samples) {
785                 sr_info("Requested number of samples reached.");
786                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
787                 devc->last_sample_fin = g_get_monotonic_time();
788                 return TRUE;
789         } else if (devc->limit_msec > 0) {
790                 cur_time = g_get_monotonic_time();
791                 elapsed_time = cur_time - devc->start_time;
792
793                 if (elapsed_time >= devc->limit_msec) {
794                         sr_info("Sampling time limit reached.");
795                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
796                         devc->last_sample_fin = g_get_monotonic_time();
797                         return TRUE;
798                 }
799         }
800
801         devc->last_sample_fin = g_get_monotonic_time();
802         return TRUE;
803 }