]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/protocol.c
baylibre-acme: Use software limit helpers
[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);
a64fec2b 170 g_error_free(err);
6b80b80d 171 return ret;
6b80b80d
BG
172 }
173
391e23a9 174 if (!strncmp(buf, prb_name, strlen(prb_name))) {
6b80b80d
BG
175 /*
176 * Correct driver registered on this address - but is
177 * there an actual probe connected?
178 */
179 probe_hwmon_path(addr, path);
180 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
181 if (status) {
182 /* We have found an ACME probe. */
183 ret = TRUE;
184 }
185 }
186
187 g_free(buf);
188 g_string_free(path, TRUE);
189
190 return ret;
191}
192
193static int get_hwmon_index(unsigned int addr)
194{
195 int status, hwmon;
196 GString *path = g_string_sized_new(64);
197 GError *err = NULL;
198 GDir *dir;
199
200 probe_hwmon_path(addr, path);
201 dir = g_dir_open(path->str, 0, &err);
391e23a9 202 if (!dir) {
6b80b80d
BG
203 sr_err("Error opening %s: %s", path->str, err->message);
204 g_string_free(path, TRUE);
a64fec2b 205 g_error_free(err);
6b80b80d
BG
206 return -1;
207 }
208
209 g_string_free(path, TRUE);
210
211 /*
212 * The directory should contain a single file named hwmonX where X
213 * is the hwmon index.
214 */
215 status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
216 g_dir_close(dir);
217 if (status != 1) {
218 sr_err("Unable to determine the hwmon entry");
219 return -1;
220 }
221
222 return hwmon;
223}
224
391e23a9 225static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
6b80b80d
BG
226 int index, int type)
227{
228 struct channel_priv *cp;
229 struct dev_context *devc;
230 struct sr_channel *ch;
231 char *name;
232
233 devc = sdi->priv;
234
235 switch (type) {
236 case ENRG_PWR:
237 name = g_strdup_printf("P%d_ENRG_PWR", index);
238 break;
239 case ENRG_CURR:
240 name = g_strdup_printf("P%d_ENRG_CURR", index);
241 break;
242 case ENRG_VOL:
243 name = g_strdup_printf("P%d_ENRG_VOL", index);
244 break;
245 case TEMP_IN:
246 name = g_strdup_printf("P%d_TEMP_IN", index);
247 break;
248 case TEMP_OUT:
249 name = g_strdup_printf("P%d_TEMP_OUT", index);
250 break;
251 default:
391e23a9 252 sr_err("Invalid channel type: %d.", type);
6b80b80d
BG
253 return;
254 }
255
256 cp = g_malloc0(sizeof(struct channel_priv));
257 cp->ch_type = type;
258 cp->probe = cg->priv;
259
5e23fcab 260 ch = sr_channel_new(sdi, devc->num_channels++,
6b80b80d
BG
261 SR_CHANNEL_ANALOG, TRUE, name);
262 g_free(name);
263
264 ch->priv = cp;
265 cg->channels = g_slist_append(cg->channels, ch);
6b80b80d
BG
266}
267
4678bdb6
BG
268static int read_probe_eeprom(unsigned int addr, struct probe_eeprom *eeprom)
269{
4678bdb6 270 GString *path = g_string_sized_new(64);
4c3a4bca 271 char eeprom_buf[EEPROM_SIZE];
4678bdb6
BG
272 ssize_t rd;
273 int fd;
274
275 probe_eeprom_path(addr, path);
276 fd = g_open(path->str, O_RDONLY);
277 g_string_free(path, TRUE);
278 if (fd < 0)
279 return -1;
280
4c3a4bca 281 rd = read(fd, eeprom_buf, EEPROM_SIZE);
e4388768 282 close(fd);
4c3a4bca 283 if (rd != EEPROM_SIZE)
4678bdb6
BG
284 return -1;
285
a7da85f5
BG
286 eeprom->type = RB32(eeprom_buf + EEPROM_OFF_TYPE);
287 eeprom->rev = RB32(eeprom_buf + EEPROM_OFF_REV);
288 eeprom->shunt = RB32(eeprom_buf + EEPROM_OFF_SHUNT);
4c3a4bca
BG
289 eeprom->pwr_sw = R8(eeprom_buf + EEPROM_OFF_PWR_SW);
290 /* Don't care about the serial number and tag for now. */
4678bdb6
BG
291
292 /* Check if we have some sensible values. */
293 if (eeprom->rev != 'B')
294 /* 'B' is the only supported revision with EEPROM for now. */
295 return -1;
296
297 if (eeprom->type != EEPROM_PROBE_TYPE_USB &&
298 eeprom->type != EEPROM_PROBE_TYPE_JACK &&
299 eeprom->type != EEPROM_PROBE_TYPE_HE10)
300 return -1;
301
302 return 0;
303}
304
305/* Some i2c slave addresses on revision B probes differ from revision A. */
306static int revB_addr_to_num(unsigned int addr)
307{
308 switch (addr) {
309 case 0x44: return 5;
310 case 0x45: return 6;
311 case 0x42: return 3;
312 case 0x43: return 4;
313 default: return addr - 0x3f;
314 }
315}
316
6b80b80d
BG
317SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
318 unsigned int addr, int prb_num)
319{
320 struct sr_channel_group *cg;
321 struct channel_group_priv *cgp;
4678bdb6
BG
322 struct probe_eeprom eeprom;
323 int hwmon, status;
324 uint32_t gpio;
6b80b80d
BG
325
326 /* Obtain the hwmon index. */
327 hwmon = get_hwmon_index(addr);
328 if (hwmon < 0)
329 return FALSE;
330
331 cg = g_malloc0(sizeof(struct sr_channel_group));
332 cgp = g_malloc0(sizeof(struct channel_group_priv));
4678bdb6
BG
333 cg->priv = cgp;
334
335 /*
336 * See if we can read the EEPROM contents. If not, assume it's
337 * a revision A probe.
338 */
4c3a4bca 339 memset(&eeprom, 0, sizeof(struct probe_eeprom));
4678bdb6
BG
340 status = read_probe_eeprom(addr, &eeprom);
341 cgp->rev = status < 0 ? ACME_REV_A : ACME_REV_B;
342
343 prb_num = cgp->rev == ACME_REV_A ? prb_num : revB_addr_to_num(addr);
344
6b80b80d 345 cgp->hwmon_num = hwmon;
61f2b7f7 346 cgp->probe_type = type;
740ad48a 347 cgp->index = prb_num - 1;
6b80b80d 348 cg->name = g_strdup_printf("Probe_%d", prb_num);
4678bdb6
BG
349
350 if (cgp->rev == ACME_REV_A) {
351 gpio = revA_pws_info_gpios[cgp->index];
352 cgp->has_pws = sr_gpio_getval_export(gpio);
353 cgp->pws_gpio = revA_pws_gpios[cgp->index];
354 } else {
355 cgp->has_pws = eeprom.pwr_sw;
356 cgp->pws_gpio = revB_pws_gpios[cgp->index];
357
358 /*
359 * For revision B we can already try to set the shunt
360 * resistance according to the EEPROM contents.
361 *
362 * Keep the default value if shunt in EEPROM == 0.
363 */
364 if (eeprom.shunt > 0)
365 bl_acme_set_shunt(cg, UOHM_TO_MOHM(eeprom.shunt));
366 }
6b80b80d
BG
367
368 if (type == PROBE_ENRG) {
369 append_channel(sdi, cg, prb_num, ENRG_PWR);
370 append_channel(sdi, cg, prb_num, ENRG_CURR);
371 append_channel(sdi, cg, prb_num, ENRG_VOL);
372 } else if (type == PROBE_TEMP) {
373 append_channel(sdi, cg, prb_num, TEMP_IN);
374 append_channel(sdi, cg, prb_num, TEMP_OUT);
375 } else {
391e23a9 376 sr_err("Invalid probe type: %d.", type);
6b80b80d
BG
377 }
378
379 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
380
381 return TRUE;
382}
383
1fe04eb8
BG
384SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg)
385{
386 struct channel_group_priv *cgp = cg->priv;
387
388 return cgp->probe_type;
389}
390
391SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg)
392{
393 struct channel_group_priv *cgp = cg->priv;
394
8f196120 395 return cgp->has_pws;
1fe04eb8
BG
396}
397
61f2b7f7
BG
398/*
399 * Sets path to the hwmon attribute if this channel group
400 * supports shunt resistance setting. The caller has to supply
401 * a valid GString.
402 */
403static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
404{
405 struct channel_group_priv *cgp;
406 int ret = SR_OK, status;
407
408 cgp = cg->priv;
409
410 if (cgp->probe_type != PROBE_ENRG) {
411 sr_err("Probe doesn't support shunt resistance setting");
412 return SR_ERR_ARG;
413 }
414
415 g_string_append_printf(path,
416 "/sys/class/hwmon/hwmon%d/shunt_resistor",
417 cgp->hwmon_num);
418
419 /*
420 * The shunt_resistor sysfs attribute is available
421 * in the Linux kernel since version 3.20. We have
391e23a9 422 * to notify the user if this attribute is not present.
61f2b7f7
BG
423 */
424 status = g_file_test(path->str, G_FILE_TEST_EXISTS);
425 if (!status) {
391e23a9 426 sr_err("shunt_resistance attribute not present, please update "
61f2b7f7
BG
427 "your kernel to version >=3.20");
428 return SR_ERR_NA;
429 }
430
431 return ret;
432}
433
7c91c22a
BG
434/*
435 * Try setting the update_interval sysfs attribute for each probe according
436 * to samplerate.
437 */
438SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi,
439 uint64_t samplerate)
440{
441 struct sr_channel_group *cg;
442 struct channel_group_priv *cgp;
443 GString *hwmon;
444 GSList *l;
445 FILE *fd;
446
447 for (l = sdi->channel_groups; l != NULL; l = l->next) {
448 cg = l->data;
449 cgp = cg->priv;
450
451 hwmon = g_string_sized_new(64);
452 g_string_append_printf(hwmon,
453 "/sys/class/hwmon/hwmon%d/update_interval",
454 cgp->hwmon_num);
455
456 if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) {
457 fd = g_fopen(hwmon->str, "w");
458 if (!fd) {
459 g_string_free(hwmon, TRUE);
460 continue;
461 }
462
463 g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate);
464 fclose(fd);
465 }
466
467 g_string_free(hwmon, TRUE);
468 }
469}
470
61f2b7f7
BG
471SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
472 uint64_t *shunt)
473{
474 GString *path = g_string_sized_new(64);
475 gchar *contents;
476 int status, ret = SR_OK;
477 GError *err = NULL;
478
479 status = get_shunt_path(cg, path);
480 if (status != SR_OK) {
481 ret = status;
482 goto out;
483 }
484
485 status = g_file_get_contents(path->str, &contents, NULL, &err);
486 if (!status) {
487 sr_err("Error reading shunt resistance: %s", err->message);
488 ret = SR_ERR_IO;
a64fec2b 489 g_error_free(err);
61f2b7f7
BG
490 goto out;
491 }
492
493 *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
494
495out:
496 g_string_free(path, TRUE);
497 return ret;
498}
499
391e23a9 500SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
61f2b7f7
BG
501{
502 GString *path = g_string_sized_new(64);;
503 int status, ret = SR_OK;
504 FILE *fd;
505
506 status = get_shunt_path(cg, path);
507 if (status != SR_OK) {
508 ret = status;
509 goto out;
510 }
511
512 /*
513 * Can't use g_file_set_contents() here, as it calls open() with
514 * O_EXEC flag in a sysfs directory thus failing with EACCES.
515 */
516 fd = g_fopen(path->str, "w");
517 if (!fd) {
7237e912 518 sr_err("Error opening %s: %s", path->str, g_strerror(errno));
350b8b07
BG
519 ret = SR_ERR_IO;
520 goto out;
61f2b7f7
BG
521 }
522
34527854 523 g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
61f2b7f7
BG
524 fclose(fd);
525
526out:
527 g_string_free(path, TRUE);
528 return ret;
529}
530
740ad48a
BG
531SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
532 gboolean *off)
533{
534 struct channel_group_priv *cgp;
535 int val;
536
537 cgp = cg->priv;
538
1fe04eb8 539 if (!bl_acme_probe_has_pws(cg)) {
740ad48a
BG
540 sr_err("Probe has no power-switch");
541 return SR_ERR_ARG;
542 }
543
4678bdb6 544 val = sr_gpio_getval_export(cgp->pws_gpio);
740ad48a
BG
545 *off = val ? FALSE : TRUE;
546
547 return SR_OK;
548}
549
550SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
551 gboolean off)
552{
553 struct channel_group_priv *cgp;
09d217a4 554 int val;
740ad48a
BG
555
556 cgp = cg->priv;
557
1fe04eb8 558 if (!bl_acme_probe_has_pws(cg)) {
740ad48a
BG
559 sr_err("Probe has no power-switch");
560 return SR_ERR_ARG;
561 }
562
4678bdb6 563 val = sr_gpio_setval_export(cgp->pws_gpio, off ? 0 : 1);
192d37e7
BG
564 if (val < 0) {
565 sr_err("Error setting power-off state: gpio: %d",
4678bdb6 566 cgp->pws_gpio);
192d37e7
BG
567 return SR_ERR_IO;
568 }
740ad48a
BG
569
570 return SR_OK;
571}
572
6b80b80d 573static int channel_to_mq(struct sr_channel *ch)
dfaee1de 574{
6b80b80d
BG
575 struct channel_priv *chp;
576
577 chp = ch->priv;
578
579 switch (chp->ch_type) {
580 case ENRG_PWR:
581 return SR_MQ_POWER;
582 case ENRG_CURR:
583 return SR_MQ_CURRENT;
584 case ENRG_VOL:
585 return SR_MQ_VOLTAGE;
391e23a9 586 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
587 case TEMP_OUT:
588 return SR_MQ_TEMPERATURE;
589 default:
590 return -1;
591 }
592}
593
594static int channel_to_unit(struct sr_channel *ch)
595{
596 struct channel_priv *chp;
597
598 chp = ch->priv;
599
600 switch (chp->ch_type) {
601 case ENRG_PWR:
602 return SR_UNIT_WATT;
603 case ENRG_CURR:
604 return SR_UNIT_AMPERE;
605 case ENRG_VOL:
606 return SR_UNIT_VOLT;
391e23a9 607 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
608 case TEMP_OUT:
609 return SR_UNIT_CELSIUS;
610 default:
611 return -1;
612 }
613}
614
615/* We need to scale measurements down from the units used by the drivers. */
616static float adjust_data(int val, int type)
617{
618 switch (type) {
619 case ENRG_PWR:
620 return ((float)val) / 1000000.0;
391e23a9
UH
621 case ENRG_CURR: /* Fallthrough */
622 case ENRG_VOL: /* Fallthrough */
623 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
624 case TEMP_OUT:
625 return ((float)val) / 1000.0;
626 default:
627 return 0.0;
628 }
629}
630
631static float read_sample(struct sr_channel *ch)
632{
633 struct channel_priv *chp;
4e88b86c 634 char buf[16];
6b80b80d
BG
635 ssize_t len;
636 int fd;
637
4e88b86c
DL
638 chp = ch->priv;
639 fd = chp->fd;
640
641 lseek(fd, 0, SEEK_SET);
642
643 len = read(fd, buf, sizeof(buf));
644 if (len < 0) {
6433156c 645 sr_err("Error reading from channel %s (hwmon: %d): %s",
7237e912 646 ch->name, chp->probe->hwmon_num, g_strerror(errno));
4e88b86c
DL
647 ch->enabled = FALSE;
648 return -1.0;
649 }
650
651 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
652}
653
654SR_PRIV int bl_acme_open_channel(struct sr_channel *ch)
655{
656 struct channel_priv *chp;
2c240774
UH
657 char path[64];
658 const char *file;
4e88b86c
DL
659 int fd;
660
6b80b80d
BG
661 chp = ch->priv;
662
663 switch (chp->ch_type) {
664 case ENRG_PWR: file = "power1_input"; break;
665 case ENRG_CURR: file = "curr1_input"; break;
666 case ENRG_VOL: file = "in1_input"; break;
667 case TEMP_IN: file = "temp1_input"; break;
668 case TEMP_OUT: file = "temp2_input"; break;
669 default:
391e23a9 670 sr_err("Invalid channel type: %d.", chp->ch_type);
4e88b86c 671 return SR_ERR;
6b80b80d
BG
672 }
673
391e23a9 674 snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
6b80b80d 675 chp->probe->hwmon_num, file);
4e88b86c 676
6b80b80d
BG
677 fd = open(path, O_RDONLY);
678 if (fd < 0) {
7237e912 679 sr_err("Error opening %s: %s", path, g_strerror(errno));
6b80b80d 680 ch->enabled = FALSE;
4e88b86c 681 return SR_ERR;
6b80b80d
BG
682 }
683
4e88b86c 684 chp->fd = fd;
6b80b80d 685
4e88b86c
DL
686 return 0;
687}
688
689SR_PRIV void bl_acme_close_channel(struct sr_channel *ch)
690{
691 struct channel_priv *chp;
692
693 chp = ch->priv;
694 close(chp->fd);
695 chp->fd = -1;
6b80b80d
BG
696}
697
698SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
699{
a0648b1a 700 uint64_t nrexpiration;
6b80b80d 701 struct sr_datafeed_packet packet, framep;
5faebab2 702 struct sr_datafeed_analog_old analog;
6b80b80d
BG
703 struct sr_dev_inst *sdi;
704 struct sr_channel *ch;
7e5a048f 705 struct channel_priv *chp;
dfaee1de 706 struct dev_context *devc;
6b80b80d 707 GSList *chl, chonly;
7e5a048f 708 unsigned i;
dfaee1de
BG
709
710 (void)fd;
6b80b80d
BG
711 (void)revents;
712
713 sdi = cb_data;
714 if (!sdi)
715 return TRUE;
dfaee1de 716
6b80b80d
BG
717 devc = sdi->priv;
718 if (!devc)
dfaee1de
BG
719 return TRUE;
720
5faebab2 721 packet.type = SR_DF_ANALOG_OLD;
6b80b80d 722 packet.payload = &analog;
5faebab2 723 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
6b80b80d 724
a0648b1a
DL
725 if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) {
726 sr_warn("Failed to read timer information");
727 return TRUE;
728 }
729
6b80b80d 730 /*
a0648b1a
DL
731 * We were not able to process the previous timer expiration, we are
732 * overloaded.
6b80b80d 733 */
a0648b1a
DL
734 if (nrexpiration > 1)
735 devc->samples_missed += nrexpiration - 1;
6b80b80d 736
6b80b80d 737 /*
7e5a048f
BG
738 * XXX This is a nasty workaround...
739 *
740 * At high sampling rates and maximum channels we are not able to
741 * acquire samples fast enough, even though frontends still think
742 * that samples arrive on time. This causes shifts in frontend
743 * plots.
744 *
745 * To compensate for the delay we check if any clock events were
746 * missed and - if so - don't really read the next value, but send
747 * the same sample as fast as possible. We do it until we are back
748 * on schedule.
749 *
750 * At high sampling rate this doesn't seem to visibly reduce the
751 * accuracy.
6b80b80d 752 */
7e5a048f
BG
753 for (i = 0; i < nrexpiration; i++) {
754 framep.type = SR_DF_FRAME_BEGIN;
695dc859 755 sr_session_send(sdi, &framep);
7e5a048f
BG
756
757 /*
758 * Due to different units used in each channel we're sending
759 * samples one-by-one.
760 */
761 for (chl = sdi->channels; chl; chl = chl->next) {
762 ch = chl->data;
763 chp = ch->priv;
6b80b80d 764
7e5a048f
BG
765 if (!ch->enabled)
766 continue;
767 chonly.next = NULL;
768 chonly.data = ch;
769 analog.channels = &chonly;
770 analog.num_samples = 1;
771 analog.mq = channel_to_mq(chl->data);
772 analog.unit = channel_to_unit(ch);
773
774 if (i < 1)
775 chp->val = read_sample(ch);
776
777 analog.data = &chp->val;
695dc859 778 sr_session_send(sdi, &packet);
7e5a048f
BG
779 }
780
781 framep.type = SR_DF_FRAME_END;
695dc859 782 sr_session_send(sdi, &framep);
7e5a048f 783 }
6b80b80d 784
d54a7c42
LPC
785 sr_sw_limits_update_samples_read(&devc->limits, 1);
786
787 if (sr_sw_limits_check(&devc->limits)) {
695dc859 788 sdi->driver->dev_acquisition_stop(sdi);
dfaee1de 789 return TRUE;
dfaee1de
BG
790 }
791
6b80b80d 792 devc->last_sample_fin = g_get_monotonic_time();
dfaee1de
BG
793 return TRUE;
794}