]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/protocol.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[libsigrok.git] / src / hardware / baylibre-acme / protocol.c
CommitLineData
dfaee1de
BG
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
6ec6c43b 20#include <config.h>
6b80b80d 21#include <string.h>
391e23a9 22#include <stdlib.h>
6b80b80d 23#include <errno.h>
391e23a9 24#include <fcntl.h>
4678bdb6 25#include <arpa/inet.h>
61f2b7f7 26#include <glib/gstdio.h>
dfaee1de 27#include "protocol.h"
740ad48a 28#include "gpio.h"
dfaee1de 29
4678bdb6
BG
30#define ACME_REV_A 1
31#define ACME_REV_B 2
32
133016f6
BG
33enum channel_type {
34 ENRG_PWR = 1,
35 ENRG_CURR,
36 ENRG_VOL,
37 TEMP_IN,
38 TEMP_OUT,
39};
40
6b80b80d 41struct channel_group_priv {
4678bdb6 42 uint8_t rev;
6b80b80d 43 int hwmon_num;
61f2b7f7 44 int probe_type;
740ad48a 45 int index;
8f196120 46 int has_pws;
4678bdb6 47 uint32_t pws_gpio;
6b80b80d
BG
48};
49
50struct channel_priv {
51 int ch_type;
4e88b86c 52 int fd;
7e5a048f 53 float val;
6b80b80d
BG
54 struct channel_group_priv *probe;
55};
56
4678bdb6
BG
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
64struct probe_eeprom {
453a0c2e
BG
65 uint32_t type;
66 uint32_t rev;
67 uint32_t shunt;
4678bdb6
BG
68 uint8_t pwr_sw;
69 uint8_t serial[EEPROM_SERIAL_SIZE];
453a0c2e 70 int8_t tag[EEPROM_TAG_SIZE];
4c3a4bca
BG
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)
4678bdb6 81
6b80b80d
BG
82static const uint8_t enrg_i2c_addrs[] = {
83 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
84};
85
86static const uint8_t temp_i2c_addrs[] = {
87 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
88};
89
4678bdb6 90static const uint32_t revA_pws_gpios[] = {
391e23a9 91 486, 498, 502, 482, 478, 506, 510, 474,
740ad48a
BG
92};
93
4678bdb6 94static const uint32_t revA_pws_info_gpios[] = {
740ad48a
BG
95 487, 499, 503, 483, 479, 507, 511, 475,
96};
97
4678bdb6
BG
98static const uint32_t revB_pws_gpios[] = {
99 489, 491, 493, 495, 497, 499, 501, 503,
100};
101
61f2b7f7
BG
102#define MOHM_TO_UOHM(x) ((x) * 1000)
103#define UOHM_TO_MOHM(x) ((x) / 1000)
104
6b80b80d
BG
105SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
106{
107 return enrg_i2c_addrs[index];
108}
109
110SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
111{
112 return temp_i2c_addrs[index];
113}
114
115SR_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
133static 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 */
142static 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
4678bdb6
BG
148static 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
6b80b80d
BG
155SR_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;
6b80b80d
BG
171 }
172
391e23a9 173 if (!strncmp(buf, prb_name, strlen(prb_name))) {
6b80b80d
BG
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
192static 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);
391e23a9 201 if (!dir) {
6b80b80d
BG
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
391e23a9 223static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
6b80b80d
BG
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:
391e23a9 250 sr_err("Invalid channel type: %d.", type);
6b80b80d
BG
251 return;
252 }
253
254 cp = g_malloc0(sizeof(struct channel_priv));
255 cp->ch_type = type;
256 cp->probe = cg->priv;
257
5e23fcab 258 ch = sr_channel_new(sdi, devc->num_channels++,
6b80b80d
BG
259 SR_CHANNEL_ANALOG, TRUE, name);
260 g_free(name);
261
262 ch->priv = cp;
263 cg->channels = g_slist_append(cg->channels, ch);
6b80b80d
BG
264}
265
4678bdb6
BG
266static int read_probe_eeprom(unsigned int addr, struct probe_eeprom *eeprom)
267{
4678bdb6 268 GString *path = g_string_sized_new(64);
4c3a4bca 269 char eeprom_buf[EEPROM_SIZE];
4678bdb6
BG
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
4c3a4bca 279 rd = read(fd, eeprom_buf, EEPROM_SIZE);
4678bdb6 280 g_close(fd, NULL);
4c3a4bca 281 if (rd != EEPROM_SIZE)
4678bdb6
BG
282 return -1;
283
a7da85f5
BG
284 eeprom->type = RB32(eeprom_buf + EEPROM_OFF_TYPE);
285 eeprom->rev = RB32(eeprom_buf + EEPROM_OFF_REV);
286 eeprom->shunt = RB32(eeprom_buf + EEPROM_OFF_SHUNT);
4c3a4bca
BG
287 eeprom->pwr_sw = R8(eeprom_buf + EEPROM_OFF_PWR_SW);
288 /* Don't care about the serial number and tag for now. */
4678bdb6
BG
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. */
304static 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
6b80b80d
BG
315SR_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;
4678bdb6
BG
320 struct probe_eeprom eeprom;
321 int hwmon, status;
322 uint32_t gpio;
6b80b80d
BG
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));
4678bdb6
BG
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 */
4c3a4bca 337 memset(&eeprom, 0, sizeof(struct probe_eeprom));
4678bdb6
BG
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
6b80b80d 343 cgp->hwmon_num = hwmon;
61f2b7f7 344 cgp->probe_type = type;
740ad48a 345 cgp->index = prb_num - 1;
6b80b80d 346 cg->name = g_strdup_printf("Probe_%d", prb_num);
4678bdb6
BG
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 }
6b80b80d
BG
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 {
391e23a9 374 sr_err("Invalid probe type: %d.", type);
6b80b80d
BG
375 }
376
377 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
378
379 return TRUE;
380}
381
1fe04eb8
BG
382SR_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
389SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg)
390{
391 struct channel_group_priv *cgp = cg->priv;
392
8f196120 393 return cgp->has_pws;
1fe04eb8
BG
394}
395
61f2b7f7
BG
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 */
401static 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
391e23a9 420 * to notify the user if this attribute is not present.
61f2b7f7
BG
421 */
422 status = g_file_test(path->str, G_FILE_TEST_EXISTS);
423 if (!status) {
391e23a9 424 sr_err("shunt_resistance attribute not present, please update "
61f2b7f7
BG
425 "your kernel to version >=3.20");
426 return SR_ERR_NA;
427 }
428
429 return ret;
430}
431
7c91c22a
BG
432/*
433 * Try setting the update_interval sysfs attribute for each probe according
434 * to samplerate.
435 */
436SR_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
61f2b7f7
BG
469SR_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
492out:
493 g_string_free(path, TRUE);
494 return ret;
495}
496
391e23a9 497SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
61f2b7f7
BG
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) {
7237e912 515 sr_err("Error opening %s: %s", path->str, g_strerror(errno));
350b8b07
BG
516 ret = SR_ERR_IO;
517 goto out;
61f2b7f7
BG
518 }
519
34527854 520 g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
61f2b7f7
BG
521 fclose(fd);
522
523out:
524 g_string_free(path, TRUE);
525 return ret;
526}
527
740ad48a
BG
528SR_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
1fe04eb8 536 if (!bl_acme_probe_has_pws(cg)) {
740ad48a
BG
537 sr_err("Probe has no power-switch");
538 return SR_ERR_ARG;
539 }
540
4678bdb6 541 val = sr_gpio_getval_export(cgp->pws_gpio);
740ad48a
BG
542 *off = val ? FALSE : TRUE;
543
544 return SR_OK;
545}
546
547SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
548 gboolean off)
549{
550 struct channel_group_priv *cgp;
09d217a4 551 int val;
740ad48a
BG
552
553 cgp = cg->priv;
554
1fe04eb8 555 if (!bl_acme_probe_has_pws(cg)) {
740ad48a
BG
556 sr_err("Probe has no power-switch");
557 return SR_ERR_ARG;
558 }
559
4678bdb6 560 val = sr_gpio_setval_export(cgp->pws_gpio, off ? 0 : 1);
192d37e7
BG
561 if (val < 0) {
562 sr_err("Error setting power-off state: gpio: %d",
4678bdb6 563 cgp->pws_gpio);
192d37e7
BG
564 return SR_ERR_IO;
565 }
740ad48a
BG
566
567 return SR_OK;
568}
569
6b80b80d 570static int channel_to_mq(struct sr_channel *ch)
dfaee1de 571{
6b80b80d
BG
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;
391e23a9 583 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
584 case TEMP_OUT:
585 return SR_MQ_TEMPERATURE;
586 default:
587 return -1;
588 }
589}
590
591static 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;
391e23a9 604 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
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. */
613static float adjust_data(int val, int type)
614{
615 switch (type) {
616 case ENRG_PWR:
617 return ((float)val) / 1000000.0;
391e23a9
UH
618 case ENRG_CURR: /* Fallthrough */
619 case ENRG_VOL: /* Fallthrough */
620 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
621 case TEMP_OUT:
622 return ((float)val) / 1000.0;
623 default:
624 return 0.0;
625 }
626}
627
628static float read_sample(struct sr_channel *ch)
629{
630 struct channel_priv *chp;
4e88b86c 631 char buf[16];
6b80b80d
BG
632 ssize_t len;
633 int fd;
634
4e88b86c
DL
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) {
6433156c 642 sr_err("Error reading from channel %s (hwmon: %d): %s",
7237e912 643 ch->name, chp->probe->hwmon_num, g_strerror(errno));
4e88b86c
DL
644 ch->enabled = FALSE;
645 return -1.0;
646 }
647
648 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
649}
650
651SR_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
6b80b80d
BG
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:
391e23a9 666 sr_err("Invalid channel type: %d.", chp->ch_type);
4e88b86c 667 return SR_ERR;
6b80b80d
BG
668 }
669
391e23a9 670 snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
6b80b80d 671 chp->probe->hwmon_num, file);
4e88b86c 672
6b80b80d
BG
673 fd = open(path, O_RDONLY);
674 if (fd < 0) {
7237e912 675 sr_err("Error opening %s: %s", path, g_strerror(errno));
6b80b80d 676 ch->enabled = FALSE;
4e88b86c 677 return SR_ERR;
6b80b80d
BG
678 }
679
4e88b86c 680 chp->fd = fd;
6b80b80d 681
4e88b86c
DL
682 return 0;
683}
684
685SR_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;
6b80b80d
BG
692}
693
694SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
695{
a0648b1a
DL
696 uint32_t cur_time, elapsed_time;
697 uint64_t nrexpiration;
6b80b80d 698 struct sr_datafeed_packet packet, framep;
5faebab2 699 struct sr_datafeed_analog_old analog;
6b80b80d
BG
700 struct sr_dev_inst *sdi;
701 struct sr_channel *ch;
7e5a048f 702 struct channel_priv *chp;
dfaee1de 703 struct dev_context *devc;
6b80b80d 704 GSList *chl, chonly;
7e5a048f 705 unsigned i;
dfaee1de
BG
706
707 (void)fd;
6b80b80d
BG
708 (void)revents;
709
710 sdi = cb_data;
711 if (!sdi)
712 return TRUE;
dfaee1de 713
6b80b80d
BG
714 devc = sdi->priv;
715 if (!devc)
dfaee1de
BG
716 return TRUE;
717
5faebab2 718 packet.type = SR_DF_ANALOG_OLD;
6b80b80d 719 packet.payload = &analog;
5faebab2 720 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
6b80b80d 721
a0648b1a
DL
722 if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) {
723 sr_warn("Failed to read timer information");
724 return TRUE;
725 }
726
6b80b80d 727 /*
a0648b1a
DL
728 * We were not able to process the previous timer expiration, we are
729 * overloaded.
6b80b80d 730 */
a0648b1a
DL
731 if (nrexpiration > 1)
732 devc->samples_missed += nrexpiration - 1;
6b80b80d 733
6b80b80d 734 /*
7e5a048f
BG
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.
6b80b80d 749 */
7e5a048f
BG
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;
6b80b80d 761
7e5a048f
BG
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 }
6b80b80d
BG
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();
dfaee1de 788 return TRUE;
6b80b80d
BG
789 } else if (devc->limit_msec > 0) {
790 cur_time = g_get_monotonic_time();
791 elapsed_time = cur_time - devc->start_time;
dfaee1de 792
6b80b80d
BG
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 }
dfaee1de
BG
799 }
800
6b80b80d 801 devc->last_sample_fin = g_get_monotonic_time();
dfaee1de
BG
802 return TRUE;
803}