-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysfs-gpio-shim.c
795 lines (717 loc) · 22.4 KB
/
sysfs-gpio-shim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
* Copyright (c) 2024 Florian Wesch <[email protected]>
*
* (See LICENSE)
*/
#define VERSION "0.1"
#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
#include <poll.h>
#include <stdarg.h>
#include <limits.h>
#include <sys/epoll.h>
#define FUSE_USE_VERSION 31
#include <fuse.h>
#include "uthash.h"
#define v(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
void die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("CRITICAL ERROR: ");
vprintf(fmt, ap);
printf("\n");
va_end(ap);
#ifdef DEBUG
abort();
#else
exit(1);
#endif
}
void *xmalloc(size_t size) {
void *ptr = calloc(1, size);
if (!ptr) die("cannot malloc");
return ptr;
}
#define FILE2FH(p) ((uint64_t)(uintptr_t)(p))
#define FH2FILE(u) ((gpio_file_t*)(uintptr_t)(u))
#define str_equal(a, b) (strcmp(a, b) == 0)
#define str_startswith(a, b) (strncmp(a, b, sizeof(b)-1) == 0)
int str_buf_equal(const char *buf, size_t size, const char *value) {
size_t value_len = strlen(value);
if (size < value_len)
return 0;
size_t buf_len = 0;
for (; buf_len < size; buf_len++) {
if (buf[buf_len] == '\0' ||
buf[buf_len] == '\n'
)
break;
}
if (buf_len != value_len)
return 0;
return strncmp(buf, value, buf_len) == 0;
}
static inline int min(int a, int b) {
return a < b ? a : b;
}
typedef struct pin_s pin_t;
static void edge_detect_add_pin(pin_t *);
static void edge_detect_remove_pin(pin_t *);
static void edge_detect_setup();
/////////////////////////////////////////////////////////////////////
static struct gpiod_chip **chips = NULL;
static int num_chips = 0;
static int chip_dir_filter(const struct dirent *entry) {
char path[NAME_MAX + 6];
snprintf(path, sizeof(path), "/dev/%s", entry->d_name);
struct stat sb;
return lstat(path, &sb) == 0 &&
!S_ISLNK(sb.st_mode) &&
gpiod_is_gpiochip_device(path);
}
static int get_chips(struct gpiod_chip ***chips_ptr) {
int i, num_chips;
struct dirent **entries;
struct gpiod_chip **chips;
num_chips = scandir("/dev/", &entries, chip_dir_filter, versionsort);
if (num_chips < 0)
return 0;
chips = xmalloc(num_chips * sizeof(*chips));
for (i = 0; i < num_chips; i++) {
char path[NAME_MAX + 6];
snprintf(path, sizeof(path), "/dev/%s", entries[i]->d_name);
chips[i] = gpiod_chip_open(path);
}
*chips_ptr = chips;
for (i = 0; i < num_chips; i++)
free(entries[i]);
free(entries);
return num_chips;
}
static int find_gpio_by_name(
const char *name, struct gpiod_chip **found_chip, unsigned int *found_offset
) {
for (int i = 0; i < num_chips; i++) {
struct gpiod_chip *chip = chips[i];
int offset = gpiod_chip_get_line_offset_from_name(chip, name);
if (offset == -1)
continue;
struct gpiod_chip_info *info = gpiod_chip_get_info(chip);
v("%s: %s @ %d\n", name, gpiod_chip_info_get_name(info), offset);
*found_chip = chip;
*found_offset = offset;
gpiod_chip_info_free(info);
return 1;
}
return 0;
}
/////////////////////////////////////////////////////////////////////
typedef struct {
void *waiter_key;
struct fuse_pollhandle *ph;
int edge_detected;
UT_hash_handle hh;
} pin_edge_wait_t;
struct pin_s {
int n;
int in_use;
enum gpiod_line_direction direction;
enum gpiod_line_edge edge;
int active_low;
pthread_mutex_t lock;
struct gpiod_chip *gpio_chip;
struct gpiod_line_request *gpio_line_request;
unsigned int gpio_line_offset;
pin_edge_wait_t *edge_waiters;
};
#define NUM_PINS 54
static pin_t pins[NUM_PINS] = {0};
typedef struct {
const char *suffix;
pin_t *pin;
int buf_fill;
char buf[16];
} gpio_file_t;
static gpio_file_t *gpio_file(const char *path) {
if (!str_startswith(path, "/gpio"))
return NULL;
const char *p_start = path + 5;
char *p_end = NULL;
long int p = strtol(p_start, &p_end, 10);
if (errno == ERANGE) {
return NULL;
} else if (errno == EINVAL) {
return NULL;
} else if (p_start == p_end) {
return NULL;
} else if (p < 0 || p >= NUM_PINS) {
return NULL;
}
pin_t *pin = &pins[p];
gpio_file_t *file = NULL;
pthread_mutex_lock(&pin->lock);
if (!pin->in_use)
goto out;
const char *suffix = NULL;
if (*p_end == '\0') {
suffix = NULL;
} else if (*p_end != '/') {
goto out;
} else {
suffix = p_end;
}
file = xmalloc(sizeof(gpio_file_t));
file->suffix = suffix ? strdup(suffix) : NULL;
file->pin = pin;
out:
pthread_mutex_unlock(&pin->lock);
return file;
}
static void gpio_file_free(gpio_file_t *file) {
assert(file);
if (file->suffix)
free((void*)file->suffix);
free(file);
}
static pin_t *pin_from_num(const char *buf, size_t size) {
if (size > 10)
return NULL;
char spec[8];
snprintf(spec, sizeof(spec), "%%%dd", (int)size);
int p;
int s = sscanf(buf, spec, &p);
if (s != 1)
return NULL;
if (p < 0 || p >= NUM_PINS)
return NULL;
return &pins[p];
}
static int pin_reconfigure_line(pin_t *pin) {
struct gpiod_line_settings *settings = gpiod_line_settings_new();
if (!settings)
die("cannot alloc settings");
gpiod_line_settings_set_direction(settings, pin->direction);
gpiod_line_settings_set_active_low(settings, pin->active_low);
// gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_UP);
if (pin->direction == GPIOD_LINE_DIRECTION_INPUT) {
gpiod_line_settings_set_debounce_period_us(settings, 300);
gpiod_line_settings_set_event_clock(settings, GPIOD_LINE_CLOCK_MONOTONIC);
gpiod_line_settings_set_edge_detection(settings, pin->edge);
}
struct gpiod_line_config *line_cfg = gpiod_line_config_new();
if (!line_cfg)
die("cannot alloc line config");
if (gpiod_line_config_add_line_settings(
line_cfg, &pin->gpio_line_offset, 1, settings
))
die("cannot add settings");
if (!pin->gpio_line_request) {
struct gpiod_request_config *req_cfg = gpiod_request_config_new();
if (!req_cfg)
die("cannot alloc request config");
gpiod_request_config_set_consumer(req_cfg, "sysfs-gpio-shim");
pin->gpio_line_request = gpiod_chip_request_lines(
pin->gpio_chip, req_cfg, line_cfg
);
gpiod_request_config_free(req_cfg);
} else {
if (gpiod_line_request_reconfigure_lines(
pin->gpio_line_request, line_cfg
))
pin->gpio_line_request = NULL;
}
gpiod_line_config_free(line_cfg);
gpiod_line_settings_free(settings);
return !!pin->gpio_line_request;
}
static int pin_setup(pin_t *pin) {
pthread_mutex_lock(&pin->lock);
assert(!pin->in_use);
char name[20];
snprintf(name, sizeof(name), "GPIO%d", pin->n);
if (!find_gpio_by_name(name, &pin->gpio_chip, &pin->gpio_line_offset)) {
pthread_mutex_unlock(&pin->lock);
return 0;
}
pin->direction = GPIOD_LINE_DIRECTION_INPUT;
pin->edge = GPIOD_LINE_EDGE_NONE;
pin->active_low = 0;
pin->gpio_line_request = NULL;
if (!pin_reconfigure_line(pin)) {
pthread_mutex_unlock(&pin->lock);
return 0;
}
pin->in_use = 1;
pin->edge_waiters = NULL;
pthread_mutex_unlock(&pin->lock);
return 1;
}
static void pin_release(pin_t *pin) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
if (pin->edge != GPIOD_LINE_EDGE_NONE)
edge_detect_remove_pin(pin);
gpiod_line_request_release(pin->gpio_line_request);
pin->in_use = 0;
pin_edge_wait_t *waiter, *tmp;
HASH_ITER(hh, pin->edge_waiters, waiter, tmp) {
if (waiter->ph)
fuse_pollhandle_destroy(waiter->ph);
HASH_DEL(pin->edge_waiters, waiter);
free(waiter);
}
assert(pin->edge_waiters == NULL);
pthread_mutex_unlock(&pin->lock);
}
static void pin_set_dir_in(pin_t *pin) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
if (pin->direction != GPIOD_LINE_DIRECTION_INPUT) {
pin->direction = GPIOD_LINE_DIRECTION_INPUT;
pin_reconfigure_line(pin);
}
pthread_mutex_unlock(&pin->lock);
}
static void pin_set_dir_out(pin_t *pin, int initial_value) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
if (pin->direction != GPIOD_LINE_DIRECTION_OUTPUT) {
pin->direction = GPIOD_LINE_DIRECTION_OUTPUT;
pin_reconfigure_line(pin);
gpiod_line_request_set_value(
pin->gpio_line_request, pin->gpio_line_offset, initial_value
);
}
pthread_mutex_unlock(&pin->lock);
}
static void pin_set_edge(pin_t *pin, enum gpiod_line_edge edge) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
if (pin->edge != edge) {
if (pin->edge != GPIOD_LINE_EDGE_NONE)
edge_detect_remove_pin(pin);
pin->edge = edge;
pin_reconfigure_line(pin);
if (pin->edge != GPIOD_LINE_EDGE_NONE)
edge_detect_add_pin(pin);
}
pthread_mutex_unlock(&pin->lock);
}
static void pin_set_active_low(pin_t *pin, int active_low) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
if (pin->active_low != active_low) {
pin->active_low = active_low;
pin_reconfigure_line(pin);
}
pthread_mutex_unlock(&pin->lock);
}
static void pin_set_value(pin_t *pin, int value) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
gpiod_line_request_set_value(
pin->gpio_line_request, pin->gpio_line_offset, value
);
pthread_mutex_unlock(&pin->lock);
}
static int pin_get_value(pin_t *pin) {
pthread_mutex_lock(&pin->lock);
assert(pin->in_use);
int value = gpiod_line_request_get_value(
pin->gpio_line_request, pin->gpio_line_offset
);
pthread_mutex_unlock(&pin->lock);
return value;
}
static int pin_poll_edge(
pin_t *pin, void *waiter_key, struct fuse_pollhandle *ph
) {
pthread_mutex_lock(&pin->lock);
pin_edge_wait_t *waiter = NULL;
HASH_FIND_PTR(pin->edge_waiters, &waiter_key, waiter);
if (!waiter) {
waiter = xmalloc(sizeof(pin_edge_wait_t));
waiter->waiter_key = waiter_key;
waiter->edge_detected = 0;
HASH_ADD_PTR(pin->edge_waiters, waiter_key, waiter);
} else if (waiter->edge_detected) {
HASH_DEL(pin->edge_waiters, waiter);
assert(!waiter->ph);
free(waiter);
pthread_mutex_unlock(&pin->lock);
return 1;
}
if (waiter->ph)
fuse_pollhandle_destroy(waiter->ph);
waiter->ph = ph;
pthread_mutex_unlock(&pin->lock);
return 0;
}
static void pin_release_poll(
pin_t *pin, void *waiter_key
) {
pthread_mutex_lock(&pin->lock);
pin_edge_wait_t *waiter = NULL;
HASH_FIND_PTR(pin->edge_waiters, &waiter_key, waiter);
if (waiter) {
if (waiter->ph)
fuse_pollhandle_destroy(waiter->ph);
HASH_DEL(pin->edge_waiters, waiter);
free(waiter);
}
pthread_mutex_unlock(&pin->lock);
}
static void pin_edge_detected_locked(pin_t *pin) {
pin_edge_wait_t *waiter, *tmp;
HASH_ITER(hh, pin->edge_waiters, waiter, tmp) {
if (waiter->ph) {
waiter->edge_detected = 1;
fuse_notify_poll(waiter->ph);
fuse_pollhandle_destroy(waiter->ph);
waiter->ph = NULL;
}
}
}
static int gpio_getattr(
const char *path, struct stat *stbuf,
struct fuse_file_info *fi
) {
memset(stbuf, 0, sizeof(struct stat));
if (str_equal(path, "/")) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
if (str_equal(path, "/export")) {
stbuf->st_mode = S_IFREG | 0220;
stbuf->st_nlink = 1;
stbuf->st_size = 4096;
return 0;
}
if (str_equal(path, "/unexport")) {
stbuf->st_mode = S_IFREG | 0220;
stbuf->st_nlink = 1;
stbuf->st_size = 4096;
return 0;
}
gpio_file_t *file = gpio_file(path);
if (!file)
return -ENOENT;
if (!file->suffix) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else {
stbuf->st_mode = S_IFREG | 0660;
stbuf->st_nlink = 1;
stbuf->st_size = 4096;
}
gpio_file_free(file);
return 0;
}
static int gpio_readdir(
const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags
) {
if (str_equal(path, "/")) {
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
filler(buf, "export", NULL, 0, 0);
filler(buf, "unexport", NULL, 0, 0);
for (int p = 0; p < NUM_PINS; p++) {
pin_t *pin = &pins[p];
if (!pin->in_use)
continue;
char name[10];
snprintf(name, sizeof(name), "gpio%d", p);
filler(buf, name, NULL, 0, 0);
}
return 0;
}
gpio_file_t *file = gpio_file(path);
if (!file)
return -ENOENT;
if (file->suffix) {
gpio_file_free(file);
return -ENOENT;
}
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
filler(buf, "direction", NULL, 0, 0);
filler(buf, "value", NULL, 0, 0);
filler(buf, "edge", NULL, 0, 0);
filler(buf, "active_low", NULL, 0, 0);
gpio_file_free(file);
return 0;
}
static int gpio_open(
const char *path, struct fuse_file_info *fi
) {
if (str_equal(path, "/export")) {
if ((fi->flags & O_ACCMODE) != O_WRONLY)
return -EACCES;
return 0;
} else if (str_equal(path, "/unexport")) {
if ((fi->flags & O_ACCMODE) != O_WRONLY)
return -EACCES;
return 0;
}
gpio_file_t *file = gpio_file(path);
if (!file)
return -ENOENT;
if (!file->suffix) {
gpio_file_free(file);
return -ENOENT;
}
if (!str_equal(file->suffix, "/direction") &&
!str_equal(file->suffix, "/value") &&
!str_equal(file->suffix, "/edge") &&
!str_equal(file->suffix, "/active_low"))
{
gpio_file_free(file);
return -ENOENT;
}
fi->fh = FILE2FH(file);
fi->direct_io = 1;
return 0;
}
static int gpio_release(
const char *path, struct fuse_file_info *fi
) {
if (fi->fh) {
gpio_file_t *file = FH2FILE(fi->fh);
assert(file->suffix);
pin_release_poll(file->pin, file);
gpio_file_free(file);
}
return 0;
}
#define buf_update_maybe(file, offset, fmt, value) do { \
if (file->buf_fill == 0 || offset == 0) { \
file->buf_fill = snprintf(file->buf, sizeof(file->buf), fmt, value); \
} \
} while (0)
static int gpio_read(
const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *fi
) {
if (!fi->fh)
return -EINVAL;
gpio_file_t *file = FH2FILE(fi->fh);
assert(file->suffix);
if (str_equal(file->suffix, "/value")) {
buf_update_maybe(file, offset, "%d\n",
pin_get_value(file->pin)
);
} else if (str_equal(file->suffix, "/direction")) {
buf_update_maybe(file, offset, "%s\n",
file->pin->direction == GPIOD_LINE_DIRECTION_INPUT ? "in" : "out"
);
} else if (str_equal(file->suffix, "/edge")) {
buf_update_maybe(file, offset, "%s\n",
file->pin->edge == GPIOD_LINE_EDGE_NONE ? "none" :
file->pin->edge == GPIOD_LINE_EDGE_RISING ? "rising" :
file->pin->edge == GPIOD_LINE_EDGE_FALLING ? "falling" :
"both"
);
} else if (str_equal(file->suffix, "/active_low")) {
buf_update_maybe(file, offset, "%d\n",
file->pin->active_low
);
} else {
__builtin_unreachable();
}
assert(file->buf_fill > 0);
if (offset >= file->buf_fill)
return 0;
int readable = min(size, file->buf_fill - offset);
memcpy(buf, file->buf + offset, readable);
return readable;
}
static int gpio_write(
const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi
) {
if (str_equal(path, "/export")) {
pin_t *pin = pin_from_num(buf, size);
if (!pin)
return -EINVAL;
if (!pin->in_use && !pin_setup(pin))
return -EBUSY;
return size;
}
if (str_equal(path, "/unexport")) {
pin_t *pin = pin_from_num(buf, size);
if (!pin)
return -EINVAL;
if (!pin->in_use)
return -EINVAL;
pin_release(pin);
return size;
}
if (!fi->fh)
return -EINVAL;
gpio_file_t *file = FH2FILE(fi->fh);
assert(file->suffix);
if (str_equal(file->suffix, "/value")) {
if (file->pin->direction != GPIOD_LINE_DIRECTION_OUTPUT)
return -EPERM;
if (str_buf_equal(buf, size, "0")) {
pin_set_value(file->pin, 0);
} else { // non-zero. lol
pin_set_value(file->pin, 1);
}
} else if (str_equal(file->suffix, "/direction")) {
if (str_buf_equal(buf, size, "out")) {
pin_set_dir_out(file->pin, 0);
} else if (str_buf_equal(buf, size, "low")) {
pin_set_dir_out(file->pin, 0);
} else if (str_buf_equal(buf, size, "high")) {
pin_set_dir_out(file->pin, 1);
} else if (str_buf_equal(buf, size, "in")) {
pin_set_dir_in(file->pin);
} else {
return -EINVAL;
}
} else if (str_equal(file->suffix, "/edge")) {
if (str_buf_equal(buf, size, "none")) {
pin_set_edge(file->pin, GPIOD_LINE_EDGE_NONE);
} else if (str_buf_equal(buf, size, "rising")) {
pin_set_edge(file->pin, GPIOD_LINE_EDGE_RISING);
} else if (str_buf_equal(buf, size, "falling")) {
pin_set_edge(file->pin, GPIOD_LINE_EDGE_FALLING);
} else if (str_buf_equal(buf, size, "both")) {
pin_set_edge(file->pin, GPIOD_LINE_EDGE_BOTH);
} else {
return -EINVAL;
}
} else if (str_equal(file->suffix, "/active_low")) {
if (str_buf_equal(buf, size, "0")) {
pin_set_active_low(file->pin, 0);
} else { // non-zero. lol
pin_set_active_low(file->pin, 1);
}
}
return size;
}
static int gpio_poll(
const char *path, struct fuse_file_info *fi,
struct fuse_pollhandle *ph, unsigned *reventsp
) {
if (!fi->fh)
return -EINVAL;
gpio_file_t *file = FH2FILE(fi->fh);
assert(file->suffix);
if (str_equal(file->suffix, "/value")) {
if (pin_poll_edge(file->pin, file, ph))
*reventsp |= POLLPRI;
return 0;
}
return -EACCES;
}
static void *gpio_init(
struct fuse_conn_info *conn,
struct fuse_config *cfg
) {
for (int p = 0; p < NUM_PINS; p++) {
pins[p].n = p;
pthread_mutex_init(&pins[p].lock, NULL);
}
num_chips = get_chips(&chips);
edge_detect_setup();
return NULL;
}
static const struct fuse_operations gpio_oper = {
.init = gpio_init,
.getattr = gpio_getattr,
.readdir = gpio_readdir,
.open = gpio_open,
.release = gpio_release,
.read = gpio_read,
.write = gpio_write,
.poll = gpio_poll,
};
/////////////////////////////////////////////////////////////////////
static int epoll_fd;
static pthread_t edge_detect_thread;
#define MAX_EPOLL_EVENTS 10
#define MAX_EDGE_EVENTS 32
static void *edge_detect_loop(void *data) {
struct gpiod_edge_event_buffer *edge_events = \
gpiod_edge_event_buffer_new(MAX_EDGE_EVENTS);
if (!edge_events)
die("no mem");
while (1) {
struct epoll_event epoll_events[MAX_EPOLL_EVENTS];
int num_epolls = epoll_wait(epoll_fd, epoll_events, MAX_EPOLL_EVENTS, -1);
if (num_epolls == -1) {
if (errno == EINTR)
continue;
die("epoll failed");
}
for (int i = 0; i < num_epolls; i++) {
pin_t *pin = epoll_events[i].data.ptr;
pthread_mutex_lock(&pin->lock);
if (!pin->in_use) {
pthread_mutex_unlock(&pin->lock);
continue;
}
// XXX: for unknown reasons this read seems to read a buffer
// of the right size but completely filled with zeros. The
// resulting gpiod_edge_events are useless as a result.
// Not sure why that is. It doesn't really matter for what
// the code does with the events, but if anyone knowns what's
// going on, please get in contact.
int num_edges = gpiod_line_request_read_edge_events(
pin->gpio_line_request, edge_events, MAX_EDGE_EVENTS
);
if (num_edges < 0)
die("edge buf fail");
for (int j = 0; j < num_edges; j++) {
struct gpiod_edge_event *edge_event = \
gpiod_edge_event_buffer_get_event(edge_events, j);
if (!edge_event)
die("event fetch fail");
// if (gpiod_edge_event_get_event_type(edge_event) == GPIOD_EDGE_EVENT_RISING_EDGE) {
// v("rising\n");
// } else {
// v("falling\n");
// }
pin_edge_detected_locked(pin);
}
pthread_mutex_unlock(&pin->lock);
}
}
return NULL;
}
static void edge_detect_add_pin(pin_t *pin) {
int pin_fd = gpiod_line_request_get_fd(pin->gpio_line_request);
struct epoll_event ev = {0};
ev.events = EPOLLIN;
ev.data.ptr = pin;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pin_fd, &ev) == -1)
die("cannot epoll_ctl");
}
static void edge_detect_remove_pin(pin_t *pin) {
int pin_fd = gpiod_line_request_get_fd(pin->gpio_line_request);
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, pin_fd, NULL) == -1)
die("cannot epoll_ctl");
}
static void edge_detect_setup() {
epoll_fd = epoll_create1(0);
if (pthread_create(&edge_detect_thread, NULL, edge_detect_loop, NULL) != 0)
die("cannot start thread\n");
}
int main(int argc, char **argv) {
return fuse_main(argc, argv, &gpio_oper, NULL);
}