MOS Source Code
Loading...
Searching...
No Matches
sysfs.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3
5
7
8typedef struct _sysfs_file sysfs_file_t;
9
19
20typedef struct _sysfs_item
21{
22 const char *name;
24 bool (*show)(sysfs_file_t *file);
25 size_t (*store)(sysfs_file_t *file, const char *buf, size_t count, off_t offset);
26 ino_t ino;
27
28 union
29 {
30 struct
31 {
32 bool (*mmap)(sysfs_file_t *file, vmap_t *vmap, off_t offset);
33 bool (*munmap)(sysfs_file_t *file, vmap_t *vmap, bool *unmapped);
34 size_t size;
35 } mem;
36
37 struct
38 {
40 void (*iterate)(struct _sysfs_item *item, dentry_t *dentry, vfs_listdir_state_t *iterator_state, dentry_iterator_op op);
41 bool (*lookup)(inode_t *parent_dir, dentry_t *dentry);
42 bool (*create)(inode_t *parent_dir, dentry_t *dentry, file_type_t type, file_perm_t perm);
43 } dyn;
44 };
46
47// clang-format off
48#define SYSFS_RO_ITEM(_name, _show_fn) { .name = _name, .type = SYSFS_RO, .show = _show_fn }
49#define SYSFS_RW_ITEM(_name, _show_fn, _store_fn) { .name = _name, .type = SYSFS_RW, .show = _show_fn, .store = _store_fn }
50#define SYSFS_WO_ITEM(_name, _store_fn) { .name = _name, .type = SYSFS_WO, .store = _store_fn }
51#define SYSFS_MEM_ITEM(_name, _mmap_fn, _munmap_fn) { .name = _name, .type = SYSFS_MEM, .mem.mmap = _mmap_fn, .mem.munmap = _munmap_fn }
52#define SYSFS_DYN_ITEMS(_name, _iterate_fn, _lookup_fn) { .type = SYSFS_DYN, .dyn.iterate = _iterate_fn, .dyn.lookup = _lookup_fn }
53#define SYSFS_DYN_DIR(_name, _iterate_fn, _lookup_fn, _create_fn) { .type = SYSFS_DYN, .dyn.iterate = _iterate_fn, .dyn.lookup = _lookup_fn, .dyn.create = _create_fn }
54// clang-format on
55
56#define SYSFS_ITEM_RO_PRINTF(name, fmt, ...) \
57 static bool name(sysfs_file_t *file) \
58 { \
59 sysfs_printf(file, fmt, ##__VA_ARGS__); \
60 return true; \
61 }
62
63#define SYSFS_ITEM_RO_STRING(name, value) SYSFS_ITEM_RO_PRINTF(name, "%s\n", value)
64
65#define SYSFS_DEFINE_DIR(sysfs_name, sysfs_items) \
66 static sysfs_dir_t __sysfs_##sysfs_name = { \
67 .list_node = LIST_NODE_INIT(__sysfs_##sysfs_name), \
68 .name = #sysfs_name, \
69 .items = sysfs_items, \
70 .num_items = MOS_ARRAY_SIZE(sysfs_items), \
71 ._dentry = NULL, \
72 ._dynamic_items = LIST_HEAD_INIT(__sysfs_##sysfs_name._dynamic_items), \
73 }
74
84
90void sysfs_register(sysfs_dir_t *entry);
91
98void sysfs_register_file(sysfs_dir_t *sysfs_dir, sysfs_item_t *item);
99
109
110void sysfs_file_set_data(sysfs_file_t *file, void *data);
113
114inode_t *sysfs_create_inode(file_type_t type, void *data);
115
116__printf(2, 3) ssize_t sysfs_printf(sysfs_file_t *file, const char *fmt, ...);
117ssize_t sysfs_put_data(sysfs_file_t *file, const void *data, size_t count);
u32 const char * fmt
Definition assert.h:36
u16 file_perm_t
Definition fs_types.h:52
file_type_t
Definition fs_types.h:14
MOSAPI void(1, 2) fatal_abort(const char *fmt
list_node_t list_head
A linked list head.
Definition list.h:23
bool munmap(ptr_t addr, size_t size)
Unmap a page from the current process's address space.
Definition mmap.c:111
#define should_inline
Definition mos_global.h:37
#define __printf(a, b)
Definition mos_global.h:30
#define NULL
Definition pb_syshdr.h:46
int bool
Definition pb_syshdr.h:57
uint32_t size_t
Definition pb_syshdr.h:42
const size_t num_items
Definition sysfs.h:80
sysfs_item_t *const items
Definition sysfs.h:79
as_linked_list
Definition sysfs.h:77
list_head _dynamic_items
for internal use only
Definition sysfs.h:82
dentry_t * _dentry
for internal use only
Definition sysfs.h:81
const char * name
Definition sysfs.h:78
sysfs_item_type_t type
Definition sysfs.h:23
size_t size
Definition sysfs.h:34
const char * name
Definition sysfs.h:22
ino_t ino
Definition sysfs.h:26
Definition mm.h:58
should_inline void sysfs_register_root_file(sysfs_item_t *item)
Register an entry in the sysfs root directory.
Definition sysfs.h:105
void sysfs_register(sysfs_dir_t *entry)
Register a sysfs directory.
Definition sysfs.c:48
void sysfs_file_set_data(sysfs_file_t *file, void *data)
Definition sysfs.c:105
void sysfs_register_file(sysfs_dir_t *sysfs_dir, sysfs_item_t *item)
Register an entry in a sysfs directory.
Definition sysfs.c:383
ssize_t sysfs_printf(sysfs_file_t *file, const char *fmt,...)
Definition sysfs.c:73
inode_t * sysfs_create_inode(file_type_t type, void *data)
Definition sysfs.c:376
ssize_t sysfs_put_data(sysfs_file_t *file, const void *data, size_t count)
Definition sysfs.c:93
sysfs_item_type_t
Definition sysfs.h:11
@ SYSFS_RW
Definition sysfs.h:14
@ SYSFS_MEM
memory-backed file
Definition sysfs.h:16
@ SYSFS_DYN
dynamic directory items
Definition sysfs.h:17
@ _SYSFS_INVALID
Definition sysfs.h:12
@ SYSFS_RO
Definition sysfs.h:13
@ SYSFS_WO
Definition sysfs.h:15
sysfs_item_t * sysfs_file_get_item(sysfs_file_t *file)
Definition sysfs.c:115
void * sysfs_file_get_data(sysfs_file_t *file)
Definition sysfs.c:110
ssize_t off_t
Definition types.h:84
signed long ssize_t
Definition types.h:83
void dentry_iterator_op(vfs_listdir_state_t *state, u64 ino, const char *name, size_t name_len, file_type_t type)
Definition vfs_types.h:60