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