Debian Codegraph @

tests/gpiosim/gpiosim.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]>
3
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <libkmod.h>
7 #include <libmount.h>
8 #include <linux/version.h>
9 #include <pthread.h>
10 #include <search.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mount.h>
16 #include <sys/prctl.h>
17 #include <sys/random.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/utsname.h>
21 #include <unistd.h>
22
23 #include "gpiosim.h"
24
25 #define GPIOSIM_API __attribute__((visibility("default")))
26 #define UNUSED __attribute__((unused))
27 #define MIN_KERNEL_VERSION KERNEL_VERSION(5, 17, 4)
28
29 static pthread_mutex_t id_lock = PTHREAD_MUTEX_INITIALIZER;
30 static pthread_once_t id_init_once = PTHREAD_ONCE_INIT;
31 static void *id_root;
32
33 struct {
34 int lowest;
35 bool found;
36 } id_find_next_ctx;
37
38 struct {
39 int id;
40 int *idp;
41 } id_del_ctx;
42
43 static void id_cleanup(void)
44 {
45 tdestroy(id_root, free);
46 }
47
48 static void id_schedule_cleanup(void)
49 {
50 atexit(id_cleanup);
51 }
52
53 static int id_compare(const void *p1, const void *p2)
54 {
55 int id1 = *(int *)p1;
56 int id2 = *(int *)p2;
57
58 if (id1 < id2)
59 return -1;
60 if (id1 > id2)
61 return 1;
62 return 0;
63 }
64
65 static void id_find_next(const void *node, VISIT which, int depth UNUSED)
66 {
67 int *id = *(int **)node;
68
69 if (id_find_next_ctx.found)
70 return;
71
72 switch (which) {
73 case postorder:
74 case leaf:
75 if (*id != id_find_next_ctx.lowest)
76 id_find_next_ctx.found = true;
77 else
78 id_find_next_ctx.lowest++;
79 break;
80 default:
81 break;
82 };
83 }
84
85 static void id_del(const void *node, VISIT which, int depth UNUSED)
86 {
87 int *id = *(int **)node;
88
89 if (id_del_ctx.idp)
90 return;
91
92 switch (which) {
93 case postorder:
94 case leaf:
95 if (*id == id_del_ctx.id)
96 id_del_ctx.idp = id;
97 break;
98 default:
99 break;
100 }
101 }
102
103 static int id_alloc(void)
104 {
105 void *ret;
106 int *id;
107
108 pthread_once(&id_init_once, id_schedule_cleanup);
109
110 pthread_mutex_lock(&id_lock);
111
112 id_find_next_ctx.lowest = 0;
113 id_find_next_ctx.found = false;
114
115 twalk(id_root, id_find_next);
116
117 id = malloc(sizeof(*id));
118 if (!id) {
119 pthread_mutex_unlock(&id_lock);
120 return -1;
121 }
122
123 *id = id_find_next_ctx.lowest;
124
125 ret = tsearch(id, &id_root, id_compare);
126 if (!ret) {
127 pthread_mutex_unlock(&id_lock);
128 /* tsearch() doesn't set errno. */
129 errno = ENOMEM;
130 return -1;
131 }
132
133 pthread_mutex_unlock(&id_lock);
134
135 return *id;
136 }
137
138 static void id_free(int id)
139 {
140 pthread_mutex_lock(&id_lock);
141
142 id_del_ctx.id = id;
143 id_del_ctx.idp = NULL;
144
145 twalk(id_root, id_del);
146 if (id_del_ctx.idp) {
147 tdelete(id_del_ctx.idp, &id_root, id_compare);
148 free(id_del_ctx.idp);
149 }
150
151 pthread_mutex_unlock(&id_lock);
152 }
153
154 struct refcount {
155 unsigned int cnt;
156 void (*release)(struct refcount *);
157 };
158
159 static void refcount_init(struct refcount *ref,
160 void (*release)(struct refcount *))
161 {
162 ref->cnt = 1;
163 ref->release = release;
164 }
165
166 static void refcount_inc(struct refcount *ref)
167 {
168 ref->cnt++;
169 }
170
171 static void refcount_dec(struct refcount *ref)
172 {
173 ref->cnt--;
174
175 if (!ref->cnt)
176 ref->release(ref);
177 }
178
179 struct list_head {
180 struct list_head *prev;
181 struct list_head *next;
182 };
183
184 static void list_init(struct list_head *list)
185 {
186 list->next = list;
187 list->prev = list;
188 }
189
190 static void list_add(struct list_head *new, struct list_head *head)
191 {
192 struct list_head *prev = head->prev;
193
194 head->prev = new;
195 new->next = head;
196 new->prev = prev;
197 prev->next = new;
198 }
199
200 static void list_del(struct list_head *entry)
201 {
202 struct list_head *prev = entry->prev, *next = entry->next;
203
204 prev->next = next;
205 next->prev = prev;
206 }
207
208 #define container_of(ptr, type, member) ({ \
209 void *__mptr = (void *)(ptr); \
210 ((type *)(__mptr - offsetof(type, member))); \
211 })
212
213 #define list_entry(ptr, type, member) container_of(ptr, type, member)
214
215 #define list_first_entry(ptr, type, member) \
216 list_entry((ptr)->next, type, member)
217
218 #define list_next_entry(pos, member) \
219 list_entry((pos)->member.next, typeof(*(pos)), member)
220
221 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
222
223 #define list_for_each_entry(pos, head, member) \
224 for (pos = list_first_entry(head, typeof(*pos), member); \
225 !list_entry_is_head(pos, head, member); \
226 pos = list_next_entry(pos, member))
227
228 #define list_for_each_entry_safe(pos, next, head, member) \
229 for (pos = list_first_entry(head, typeof(*pos), member), \
230 next = list_next_entry(pos, member); \
231 !list_entry_is_head(pos, head, member); \
232 pos = next, next = list_next_entry(next, member))
233
234 static int open_write_close(int base_fd, const char *where, const char *what)
235 {
236 ssize_t written, size;
237 int fd;
238
239 if (what)
240 size = strlen(what) + 1;
241 else
242 size = 1;
243
244 fd = openat(base_fd, where, O_WRONLY);
245 if (fd < 0)
246 return -1;
247
248 written = write(fd, what ?: "", size);
249 close(fd);
250 if (written < 0) {
251 return -1;
252 } else if (written != size) {
253 errno = EIO;
254 return -1;
255 }
256
257 return 0;
258 }
259
260 static int open_read_close(int base_fd, const char *where,
261 char *buf, size_t bufsize)
262 {
263 ssize_t rd;
264 int fd;
265
266 fd = openat(base_fd, where, O_RDONLY);
267 if (fd < 0)
268 return -1;
269
270 memset(buf, 0, bufsize);
271 rd = read(fd, buf, bufsize);
272 close(fd);
273 if (rd < 0)
274 return -1;
275
276 if (buf[rd - 1] == '\n')
277 buf[rd - 1] = '\0';
278
279 return 0;
280 }
281
282 static int check_kernel_version(void)
283 {
284 unsigned int major, minor, release;
285 struct utsname un;
286 int ret;
287
288 ret = uname(&un);
289 if (ret)
290 return -1;
291
292 ret = sscanf(un.release, "%u.%u.%u", &major, &minor, &release);
293 if (ret != 3) {
294 errno = EFAULT;
295 return -1;
296 }
297
298 if (KERNEL_VERSION(major, minor, release) < MIN_KERNEL_VERSION) {
299 errno = EOPNOTSUPP;
300 return -1;
301 }
302
303 return 0;
304 }
305
306 static int check_gpiosim_module(void)
307 {
308 struct kmod_module *module;
309 struct kmod_ctx *kmod;
310 const char *modpath;
311 int ret, initstate;
312
313 kmod = kmod_new(NULL, NULL);
314 if (!kmod)
315 return -1;
316
317 ret = kmod_module_new_from_name(kmod, "gpio-sim", &module);
318 if (ret)
319 goto out_unref_kmod;
320
321 again:
322 /* First check if the module is already loaded or built-in. */
323 initstate = kmod_module_get_initstate(module);
324 if (initstate < 0) {
325 if (errno == ENOENT) {
326 /*
327 * It's not loaded, let's see if we can do it manually.
328 * See if we can find the module.
329 */
330 modpath = kmod_module_get_path(module);
331 if (!modpath) {
332 /* libkmod doesn't set errno. */
333 errno = ENOENT;
334 ret = -1;
335 goto out_unref_module;
336 }
337
338 ret = kmod_module_probe_insert_module(
339 module, KMOD_PROBE_IGNORE_LOADED, NULL, NULL,
340 NULL, NULL);
341 if (ret)
342 goto out_unref_module;
343
344 goto again;
345 } else {
346 if (errno == 0)
347 errno = EOPNOTSUPP;
348
349 goto out_unref_module;
350 }
351 }
352
353 if (initstate != KMOD_MODULE_BUILTIN &&
354 initstate != KMOD_MODULE_LIVE &&
355 initstate != KMOD_MODULE_COMING) {
356 errno = EPERM;
357 goto out_unref_module;
358 }
359
360 ret = 0;
361
362 out_unref_module:
363 kmod_module_unref(module);
364 out_unref_kmod:
365 kmod_unref(kmod);
366 return ret;
367 }
368
369 static char *configfs_make_item(int at, int id)
370 {
371 char *item_name, prname[17];
372 int ret;
373
374 ret = prctl(PR_GET_NAME, prname);
375 if (ret)
376 return NULL;
377
378 ret = asprintf(&item_name, "%s.%u.%d", prname, getpid(), id);
379 if (ret < 0)
380 return NULL;
381
382 ret = mkdirat(at, item_name, 0600);
383 if (ret) {
384 free(item_name);
385 return NULL;
386 }
387
388 return item_name;
389 }
390
391 struct gpiosim_ctx {
392 struct refcount refcnt;
393 int cfs_dir_fd;
394 char *cfs_mnt_dir;
395 };
396
397 struct gpiosim_dev {
398 struct refcount refcnt;
399 struct gpiosim_ctx *ctx;
400 bool live;
401 char *item_name;
402 int id;
403 char *dev_name;
404 int cfs_dir_fd;
405 int sysfs_dir_fd;
406 struct list_head banks;
407 struct list_head deferred_banks;
408 };
409
410 struct gpiosim_bank {
411 struct refcount refcnt;
412 struct gpiosim_dev *dev;
413 struct list_head siblings;
414 struct list_head deferred;
415 char *item_name;
416 int id;
417 char *chip_name;
418 char *dev_path;
419 int cfs_dir_fd;
420 int sysfs_dir_fd;
421 size_t num_lines;
422 struct list_head lines;
423 };
424
425 struct gpiosim_line {
426 struct list_head siblings;
427 unsigned int offset;
428 };
429
430 static inline struct gpiosim_ctx *to_gpiosim_ctx(struct refcount *ref)
431 {
432 return container_of(ref, struct gpiosim_ctx, refcnt);
433 }
434
435 static inline struct gpiosim_dev *to_gpiosim_dev(struct refcount *ref)
436 {
437 return container_of(ref, struct gpiosim_dev, refcnt);
438 }
439
440 static inline struct gpiosim_bank *to_gpiosim_bank(struct refcount *ref)
441 {
442 return container_of(ref, struct gpiosim_bank, refcnt);
443 }
444
445 static int ctx_open_configfs_dir(struct gpiosim_ctx *ctx, const char *cfs_path)
446 {
447 char *path;
448 int ret;
449
450 ret = asprintf(&path, "%s/gpio-sim", cfs_path);
451 if (ret < 0)
452 return -1;
453
454 ctx->cfs_dir_fd = open(path, O_RDONLY);
455 free(path);
456 if (ctx->cfs_dir_fd < 0)
457 return -1;
458
459 return 0;
460 }
461
462 /*
463 * We don't need to check the configfs module as loading gpio-sim will pull it
464 * in but we need to find out if and where configfs was mounted. If it wasn't
465 * then as a last resort we'll try to mount it ourselves.
466 */
467 static int ctx_get_configfs_fd(struct gpiosim_ctx *ctx)
468 {
469 struct libmnt_context *mntctx;
470 struct libmnt_iter *iter;
471 struct libmnt_table *tb;
472 struct libmnt_fs *fs;
473 const char *type;
474 int ret;
475
476 /* Try to find out if and where configfs is mounted. */
477 mntctx = mnt_new_context();
478 if (!mntctx)
479 return -1;
480
481 ret = mnt_context_get_mtab(mntctx, &tb);
482 if (ret)
483 goto out_free_ctx;
484
485 iter = mnt_new_iter(MNT_ITER_FORWARD);
486 if (!iter)
487 goto out_free_ctx;
488
489 while (mnt_table_next_fs(tb, iter, &fs) == 0) {
490 type = mnt_fs_get_fstype(fs);
491
492 if (strcmp(type, "configfs") == 0) {
493 ret = ctx_open_configfs_dir(ctx, mnt_fs_get_target(fs));
494 if (ret)
495 goto out_free_iter;
496
497 ret = 0;
498 goto out_free_iter;
499 }
500 }
501
502 /* Didn't find any configfs mounts - let's try to do it ourselves. */
503 ctx->cfs_mnt_dir = strdup("/tmp/gpiosim-configfs-XXXXXX");
504 if (!ctx->cfs_mnt_dir)
505 goto out_free_iter;
506
507 ctx->cfs_mnt_dir = mkdtemp(ctx->cfs_mnt_dir);
508 if (!ctx->cfs_mnt_dir)
509 goto out_free_tmpdir;
510
511 ret = mount(NULL, ctx->cfs_mnt_dir, "configfs", MS_RELATIME, NULL);
512 if (ret)
513 goto out_rm_tmpdir;
514
515 ret = ctx_open_configfs_dir(ctx, ctx->cfs_mnt_dir);
516 if (ret == 0)
517 /* Skip unmounting & deleting the tmp directory on success. */
518 goto out_free_iter;
519
520 umount(ctx->cfs_mnt_dir);
521 out_rm_tmpdir:
522 rmdir(ctx->cfs_mnt_dir);
523 out_free_tmpdir:
524 free(ctx->cfs_mnt_dir);
525 ctx->cfs_mnt_dir = NULL;
526 out_free_iter:
527 mnt_free_iter(iter);
528 out_free_ctx:
529 mnt_free_context(mntctx);
530
531 return ret;
532 }
533
534 static void ctx_release(struct refcount *ref)
535 {
536 struct gpiosim_ctx *ctx = to_gpiosim_ctx(ref);
537
538 close(ctx->cfs_dir_fd);
539
540 if (ctx->cfs_mnt_dir) {
541 umount(ctx->cfs_mnt_dir);
542 rmdir(ctx->cfs_mnt_dir);
543 free(ctx->cfs_mnt_dir);
544 }
545
546 free(ctx);
547 }
548
549 GPIOSIM_API struct gpiosim_ctx *gpiosim_ctx_new(void)
550 {
551 struct gpiosim_ctx *ctx;
552 int ret;
553
554 ret = check_kernel_version();
555 if (ret)
556 return NULL;
557
558 ret = check_gpiosim_module();
559 if (ret)
560 return NULL;
561
562 ctx = malloc(sizeof(*ctx));
563 if (!ctx)
564 return NULL;
565
566 memset(ctx, 0, sizeof(*ctx));
567 refcount_init(&ctx->refcnt, ctx_release);
568
569 ret = ctx_get_configfs_fd(ctx);
570 if (ret) {
571 free(ctx);
572 return NULL;
573 }
574
575 return ctx;
576 }
577
578 GPIOSIM_API struct gpiosim_ctx *gpiosim_ctx_ref(struct gpiosim_ctx *ctx)
579 {
580 refcount_inc(&ctx->refcnt);
581
582 return ctx;
583 }
584
585 GPIOSIM_API void gpiosim_ctx_unref(struct gpiosim_ctx *ctx)
586 {
587 refcount_dec(&ctx->refcnt);
588 }
589
590 static void dev_release(struct refcount *ref)
591 {
592 struct gpiosim_dev *dev = to_gpiosim_dev(ref);
593 struct gpiosim_ctx *ctx = dev->ctx;
594
595 if (dev->live)
596 gpiosim_dev_disable(dev);
597
598 unlinkat(ctx->cfs_dir_fd, dev->item_name, AT_REMOVEDIR);
599 close(dev->cfs_dir_fd);
600 free(dev->dev_name);
601 free(dev->item_name);
602 id_free(dev->id);
603 gpiosim_ctx_unref(ctx);
604 free(dev);
605 }
606
607 GPIOSIM_API struct gpiosim_dev *gpiosim_dev_new(struct gpiosim_ctx *ctx)
608 {
609 int configfs_fd, ret, id;
610 struct gpiosim_dev *dev;
611 char devname[128];
612 char *item_name;
613
614 id = id_alloc();
615 if (id < 0)
616 return NULL;
617
618 item_name = configfs_make_item(ctx->cfs_dir_fd, id);
619 if (!item_name)
620 goto err_free_id;
621
622 configfs_fd = openat(ctx->cfs_dir_fd, item_name, O_RDONLY);
623 if (configfs_fd < 0)
624 goto err_unlink;
625
626 dev = malloc(sizeof(*dev));
627 if (!dev)
628 goto err_close_fd;
629
630 ret = open_read_close(configfs_fd, "dev_name",
631 devname, sizeof(devname));
632 if (ret)
633 goto err_free_dev;
634
635 memset(dev, 0, sizeof(*dev));
636 refcount_init(&dev->refcnt, dev_release);
637 list_init(&dev->banks);
638 list_init(&dev->deferred_banks);
639 dev->cfs_dir_fd = configfs_fd;
640 dev->sysfs_dir_fd = -1;
641 dev->item_name = item_name;
642 dev->id = id;
643
644 dev->dev_name = strdup(devname);
645 if (!dev->dev_name)
646 goto err_free_dev;
647
648 dev->ctx = gpiosim_ctx_ref(ctx);
649
650 return dev;
651
652 err_free_dev:
653 free(dev);
654 err_close_fd:
655 close(configfs_fd);
656 err_unlink:
657 unlinkat(ctx->cfs_dir_fd, item_name, AT_REMOVEDIR);
658 free(item_name);
659 err_free_id:
660 id_free(id);
661
662 return NULL;
663 }
664
665 GPIOSIM_API struct gpiosim_dev *gpiosim_dev_ref(struct gpiosim_dev *dev)
666 {
667 refcount_inc(&dev->refcnt);
668
669 return dev;
670 }
671
672 GPIOSIM_API void gpiosim_dev_unref(struct gpiosim_dev *dev)
673 {
674 refcount_dec(&dev->refcnt);
675 }
676
677 GPIOSIM_API struct gpiosim_ctx *gpiosim_dev_get_ctx(struct gpiosim_dev *dev)
678 {
679 return gpiosim_ctx_ref(dev->ctx);
680 }
681
682 GPIOSIM_API const char *gpiosim_dev_get_name(struct gpiosim_dev *dev)
683 {
684 return dev->dev_name;
685 }
686
687 static bool dev_check_pending(struct gpiosim_dev *dev)
688 {
689 if (dev->live)
690 errno = EBUSY;
691
692 return !dev->live;
693 }
694
695 static bool dev_check_live(struct gpiosim_dev *dev)
696 {
697 if (!dev->live)
698 errno = ENODEV;
699
700 return dev->live;
701 }
702
703 static int bank_set_chip_name(struct gpiosim_bank *bank)
704 {
705 char chip_name[32];
706 int ret;
707
708 ret = open_read_close(bank->cfs_dir_fd, "chip_name",
709 chip_name, sizeof(chip_name));
710 if (ret)
711 return -1;
712
713 bank->chip_name = strdup(chip_name);
714 if (!bank->chip_name)
715 return -1;
716
717 return 0;
718 }
719
720 static int bank_set_dev_path(struct gpiosim_bank *bank)
721 {
722 char dev_path[64];
723
724 snprintf(dev_path, sizeof(dev_path), "/dev/%s", bank->chip_name);
725
726 bank->dev_path = strdup(dev_path);
727 if (!bank->dev_path)
728 return -1;
729
730 return 0;
731 }
732
733 static int bank_open_sysfs_dir(struct gpiosim_bank *bank)
734 {
735 struct gpiosim_dev *dev = bank->dev;
736 int fd;
737
738 fd = openat(dev->sysfs_dir_fd, bank->chip_name, O_RDONLY);
739 if (fd < 0)
740 return -1;
741
742 bank->sysfs_dir_fd = fd;
743
744 return 0;
745 }
746
747 static int bank_enable(struct gpiosim_bank *bank)
748 {
749 int ret;
750
751 ret = bank_set_chip_name(bank);
752 if (ret)
753 return -1;
754
755 ret = bank_set_dev_path(bank);
756 if (ret)
757 return -1;
758
759 return bank_open_sysfs_dir(bank);
760 }
761
762 static int dev_open_sysfs_dir(struct gpiosim_dev *dev)
763 {
764 int ret, fd;
765 char *sysp;
766
767 ret = asprintf(&sysp, "/sys/devices/platform/%s", dev->dev_name);
768 if (ret < 0)
769 return -1;
770
771 fd = open(sysp, O_RDONLY);
772 free(sysp);
773 if (fd < 0)
774 return -1;
775
776 dev->sysfs_dir_fd = fd;
777
778 return 0;
779 }
780
781 /* Closes the sysfs dir for this device and all its child banks. */
782 static void dev_close_sysfs_dirs(struct gpiosim_dev *dev)
783 {
784 struct gpiosim_bank *bank;
785
786 list_for_each_entry(bank, &dev->banks, siblings) {
787 free(bank->chip_name);
788 free(bank->dev_path);
789 bank->chip_name = bank->dev_path = NULL;
790 close(bank->sysfs_dir_fd);
791 bank->sysfs_dir_fd = -1;
792 }
793
794 close(dev->sysfs_dir_fd);
795 dev->sysfs_dir_fd = -1;
796 }
797
798 GPIOSIM_API int gpiosim_dev_enable(struct gpiosim_dev *dev)
799 {
800 struct gpiosim_bank *bank;
801 int ret;
802
803 if (!dev_check_pending(dev))
804 return -1;
805
806 ret = open_write_close(dev->cfs_dir_fd, "live", "1");
807 if (ret)
808 return -1;
809
810 ret = dev_open_sysfs_dir(dev);
811 if (ret) {
812 open_write_close(dev->cfs_dir_fd, "live", "0");
813 return -1;
814 }
815
816 bank = container_of(&dev->banks, struct gpiosim_bank, siblings);
817
818 list_for_each_entry(bank, &dev->banks, siblings) {
819 ret = bank_enable(bank);
820 if (ret) {
821 dev_close_sysfs_dirs(dev);
822 open_write_close(dev->cfs_dir_fd, "live", "0");
823 return -1;
824 }
825 }
826
827 dev->live = true;
828
829 return 0;
830 }
831
832 static void bank_release_finish(struct gpiosim_bank *bank)
833 {
834 struct gpiosim_dev *dev = bank->dev;
835 struct gpiosim_line *line, *tmp;
836 char buf[64];
837
838 list_for_each_entry_safe(line, tmp, &bank->lines, siblings) {
839 snprintf(buf, sizeof(buf), "line%u/hog", line->offset);
840 unlinkat(bank->cfs_dir_fd, buf, AT_REMOVEDIR);
841
842 snprintf(buf, sizeof(buf), "line%u", line->offset);
843 unlinkat(bank->cfs_dir_fd, buf, AT_REMOVEDIR);
844
845 list_del(&line->siblings);
846 free(line);
847 }
848
849 close(bank->cfs_dir_fd);
850 unlinkat(dev->cfs_dir_fd, bank->item_name, AT_REMOVEDIR);
851 free(bank->item_name);
852 id_free(bank->id);
853 free(bank);
854 }
855
856 GPIOSIM_API int gpiosim_dev_disable(struct gpiosim_dev *dev)
857 {
858 struct gpiosim_bank *bank, *tmp;
859 int ret;
860
861 if (!dev_check_live(dev))
862 return -1;
863
864 ret = open_write_close(dev->cfs_dir_fd, "live", "0");
865 if (ret)
866 return ret;
867
868 list_for_each_entry_safe(bank, tmp, &dev->deferred_banks, deferred) {
869 list_del(&bank->deferred);
870 bank_release_finish(bank);
871 }
872
873 dev_close_sysfs_dirs(dev);
874
875 dev->live = false;
876
877 return 0;
878 }
879
880 GPIOSIM_API bool gpiosim_dev_is_live(struct gpiosim_dev *dev)
881 {
882 return dev->live;
883 }
884
885 static void bank_release(struct refcount *ref)
886 {
887 struct gpiosim_bank *bank = to_gpiosim_bank(ref);
888 struct gpiosim_dev *dev = bank->dev;
889
890 list_del(&bank->siblings);
891
892 /*
893 * If the device is still active, defer removing the bank directories
894 * until it's disabled. Otherwise, do it now.
895 */
896 if (dev->live)
897 list_add(&bank->deferred, &dev->deferred_banks);
898 else
899 bank_release_finish(bank);
900
901 gpiosim_dev_unref(dev);
902 }
903
904 GPIOSIM_API struct gpiosim_bank *gpiosim_bank_new(struct gpiosim_dev *dev)
905 {
906 struct gpiosim_bank *bank;
907 int configfs_fd, id;
908 char *item_name;
909
910 if (!dev_check_pending(dev))
911 return NULL;
912
913 id = id_alloc();
914 if (id < 0)
915 return NULL;
916
917 item_name = configfs_make_item(dev->cfs_dir_fd, id);
918 if (!item_name)
919 goto err_free_id;
920
921 configfs_fd = openat(dev->cfs_dir_fd, item_name, O_RDONLY);
922 if (configfs_fd < 0)
923 goto err_unlink;
924
925 bank = malloc(sizeof(*bank));
926 if (!bank)
927 goto err_close_cfs;
928
929 memset(bank, 0, sizeof(*bank));
930
931 refcount_init(&bank->refcnt, bank_release);
932 list_add(&bank->siblings, &dev->banks);
933 bank->cfs_dir_fd = configfs_fd;
934 bank->dev = gpiosim_dev_ref(dev);
935 bank->item_name = item_name;
936 bank->num_lines = 1;
937 bank->id = id;
938 list_init(&bank->lines);
939
940 return bank;
941
942 err_close_cfs:
943 close(configfs_fd);
944 err_unlink:
945 unlinkat(dev->cfs_dir_fd, item_name, AT_REMOVEDIR);
946 err_free_id:
947 id_free(id);
948
949 return NULL;
950 }
951
952 GPIOSIM_API struct gpiosim_bank *gpiosim_bank_ref(struct gpiosim_bank *bank)
953 {
954 refcount_inc(&bank->refcnt);
955
956 return bank;
957 }
958
959 GPIOSIM_API void gpiosim_bank_unref(struct gpiosim_bank *bank)
960 {
961 refcount_dec(&bank->refcnt);
962 }
963
964 GPIOSIM_API struct gpiosim_dev *gpiosim_bank_get_dev(struct gpiosim_bank *bank)
965 {
966 return gpiosim_dev_ref(bank->dev);
967 }
968
969 GPIOSIM_API const char *gpiosim_bank_get_chip_name(struct gpiosim_bank *bank)
970 {
971 return bank->chip_name;
972 }
973
974 GPIOSIM_API const char *gpiosim_bank_get_dev_path(struct gpiosim_bank *bank)
975 {
976 return bank->dev_path;
977 }
978
979 GPIOSIM_API int gpiosim_bank_set_label(struct gpiosim_bank *bank,
980 const char *label)
981 {
982 if (!dev_check_pending(bank->dev))
983 return -1;
984
985 return open_write_close(bank->cfs_dir_fd, "label", label);
986 }
987
988 GPIOSIM_API int gpiosim_bank_set_num_lines(struct gpiosim_bank *bank,
989 size_t num_lines)
990 {
991 char buf[32];
992 int ret;
993
994 if (!dev_check_pending(bank->dev))
995 return -1;
996
997 snprintf(buf, sizeof(buf), "%zu", num_lines);
998
999 ret = open_write_close(bank->cfs_dir_fd, "num_lines", buf);
1000 if (ret)
1001 return -1;
1002
1003 bank->num_lines = num_lines;
1004
1005 return 0;
1006 }
1007
1008 static int bank_make_line_dir(struct gpiosim_bank *bank, unsigned int offset)
1009 {
1010 struct gpiosim_line *line;
1011 char buf[32];
1012 int ret;
1013
1014 snprintf(buf, sizeof(buf), "line%u", offset);
1015
1016 ret = faccessat(bank->cfs_dir_fd, buf, W_OK, 0);
1017 if (!ret)
1018 return 0;
1019 if (ret && errno != ENOENT)
1020 return -1;
1021
1022 line = malloc(sizeof(*line));
1023 if (!line)
1024 return -1;
1025
1026 ret = mkdirat(bank->cfs_dir_fd, buf, O_RDONLY);
1027 if (ret) {
1028 free(line);
1029 return -1;
1030 }
1031
1032 memset(line, 0, sizeof(*line));
1033 line->offset = offset;
1034 list_add(&line->siblings, &bank->lines);
1035
1036 return 0;
1037 }
1038
1039 GPIOSIM_API int gpiosim_bank_set_line_name(struct gpiosim_bank *bank,
1040 unsigned int offset,
1041 const char *name)
1042 {
1043 char buf[32];
1044 int ret;
1045
1046 if (!dev_check_pending(bank->dev))
1047 return -1;
1048
1049 ret = bank_make_line_dir(bank, offset);
1050 if (ret)
1051 return -1;
1052
1053 snprintf(buf, sizeof(buf), "line%u/name", offset);
1054
1055 return open_write_close(bank->cfs_dir_fd, buf, name ?: "");
1056 }
1057
1058 GPIOSIM_API int gpiosim_bank_hog_line(struct gpiosim_bank *bank,
1059 unsigned int offset, const char *name,
1060 enum gpiosim_direction direction)
1061 {
1062 char buf[64], *dir;
1063 int ret, fd;
1064
1065 switch (direction) {
1066 case GPIOSIM_DIRECTION_INPUT:
1067 dir = "input";
1068 break;
1069 case GPIOSIM_DIRECTION_OUTPUT_HIGH:
1070 dir = "output-high";
1071 break;
1072 case GPIOSIM_DIRECTION_OUTPUT_LOW:
1073 dir = "output-low";
1074 break;
1075 default:
1076 errno = EINVAL;
1077 return -1;
1078 }
1079
1080 if (!dev_check_pending(bank->dev))
1081 return -1;
1082
1083 ret = bank_make_line_dir(bank, offset);
1084 if (ret)
1085 return -1;
1086
1087 snprintf(buf, sizeof(buf), "line%u/hog", offset);
1088
1089 ret = faccessat(bank->cfs_dir_fd, buf, W_OK, 0);
1090 if (ret) {
1091 if (errno == ENOENT) {
1092 ret = mkdirat(bank->cfs_dir_fd, buf, O_RDONLY);
1093 if (ret)
1094 return -1;
1095 } else {
1096 return -1;
1097 }
1098 }
1099
1100 fd = openat(bank->cfs_dir_fd, buf, O_RDONLY);
1101 if (fd < 0)
1102 return -1;
1103
1104 ret = open_write_close(fd, "name", name ?: "");
1105 if (ret) {
1106 close(fd);
1107 return -1;
1108 }
1109
1110 ret = open_write_close(fd, "direction", dir);
1111 close(fd);
1112 return ret;
1113 }
1114
1115 GPIOSIM_API int gpiosim_bank_clear_hog(struct gpiosim_bank *bank,
1116 unsigned int offset)
1117 {
1118 char buf[64];
1119
1120 if (!dev_check_pending(bank->dev))
1121 return -1;
1122
1123 snprintf(buf, sizeof(buf), "line%u/hog", offset);
1124
1125 return unlinkat(bank->cfs_dir_fd, buf, AT_REMOVEDIR);
1126 }
1127
1128 static int sysfs_read_bank_attr(struct gpiosim_bank *bank, unsigned int offset,
1129 const char *attr, char *buf, size_t bufsize)
1130 {
1131 struct gpiosim_dev *dev = bank->dev;
1132 char where[32];
1133
1134 if (!dev_check_live(dev))
1135 return -1;
1136
1137 snprintf(where, sizeof(where), "sim_gpio%u/%s", offset, attr);
1138
1139 return open_read_close(bank->sysfs_dir_fd, where, buf, bufsize);
1140 }
1141
1142 GPIOSIM_API enum
1143 gpiosim_value gpiosim_bank_get_value(struct gpiosim_bank *bank,
1144 unsigned int offset)
1145 {
1146 char what[3];
1147 int ret;
1148
1149 ret = sysfs_read_bank_attr(bank, offset, "value", what, sizeof(what));
1150 if (ret)
1151 return GPIOSIM_VALUE_ERROR;
1152
1153 if (what[0] == '0')
1154 return GPIOSIM_VALUE_INACTIVE;
1155 if (what[0] == '1')
1156 return GPIOSIM_VALUE_ACTIVE;
1157
1158 errno = EIO;
1159 return GPIOSIM_VALUE_ERROR;
1160 }
1161
1162 GPIOSIM_API enum gpiosim_pull
1163 gpiosim_bank_get_pull(struct gpiosim_bank *bank, unsigned int offset)
1164 {
1165 char what[16];
1166 int ret;
1167
1168 ret = sysfs_read_bank_attr(bank, offset, "pull", what, sizeof(what));
1169 if (ret)
1170 return GPIOSIM_PULL_ERROR;
1171
1172 if (strcmp(what, "pull-down") == 0)
1173 return GPIOSIM_PULL_DOWN;
1174 if (strcmp(what, "pull-up") == 0)
1175 return GPIOSIM_PULL_UP;
1176
1177 errno = EIO;
1178 return GPIOSIM_PULL_ERROR;
1179 }
1180
1181 GPIOSIM_API int
1182 gpiosim_bank_set_pull(struct gpiosim_bank *bank,
1183 unsigned int offset, enum gpiosim_pull pull)
1184 {
1185 struct gpiosim_dev *dev = bank->dev;
1186 char where[32], what[16];
1187
1188 if (!dev_check_live(dev))
1189 return -1;
1190
1191 if (pull != GPIOSIM_PULL_DOWN && pull != GPIOSIM_PULL_UP) {
1192 errno = EINVAL;
1193 return -1;
1194 }
1195
1196 snprintf(where, sizeof(where), "sim_gpio%u/pull", offset);
1197 snprintf(what, sizeof(what),
1198 pull == GPIOSIM_PULL_DOWN ? "pull-down" : "pull-up");
1199
1200 return open_write_close(bank->sysfs_dir_fd, where, what);
1201 }