1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/platform/platform.hpp"
6
7#include <mos/io/io_types.h>
8#include <mos/mm/mm_types.h>
9#include <mos/types.hpp>
10
11typedef struct _io io_t;
12struct vmap_t; // forward declaration
13
14typedef enum
15{
16 IO_NULL, // null io port
17 IO_FILE, // a file
18 IO_DIR, // a directory (i.e. readdir())
19 IO_IPC, // an IPC channel
20 IO_PIPE, // an end of a pipe
21 IO_CONSOLE, // a console
22} io_type_t;
23
24typedef enum
25{
26 IO_NONE = MEM_PERM_NONE, // 0
27 IO_READABLE = MEM_PERM_READ, // 1 << 0
28 IO_WRITABLE = MEM_PERM_WRITE, // 1 << 1
29 IO_EXECUTABLE = MEM_PERM_EXEC, // 1 << 2
30 IO_SEEKABLE = 1 << 3,
31 IO_MMAPABLE = 1 << 4,
32} io_flags_t;
33
34MOS_ENUM_OPERATORS(io_flags_t);
35
36typedef struct
37{
38 size_t (*read)(io_t *io, void *buf, size_t count);
39 size_t (*write)(io_t *io, const void *buf, size_t count);
40 void (*close)(io_t *io);
41 off_t (*seek)(io_t *io, off_t offset, io_seek_whence_t whence);
42 bool (*mmap)(io_t *io, vmap_t *vmap, off_t offset);
43 bool (*munmap)(io_t *io, vmap_t *vmap, bool *unmapped);
44 void (*get_name)(const io_t *io, char *buf, size_t size);
45} io_op_t;
46
47typedef struct _io
48{
49 bool closed;
50 atomic_t refcount;
51 io_flags_t flags;
52 io_type_t type;
53 const io_op_t *ops;
54} io_t;
55
56extern io_t *const io_null;
57
58void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops);
59
60io_t *io_ref(io_t *io);
61io_t *io_unref(io_t *io);
62__nodiscard bool io_valid(const io_t *io);
63
64size_t io_read(io_t *io, void *buf, size_t count);
65size_t io_pread(io_t *io, void *buf, size_t count, off_t offset);
66size_t io_write(io_t *io, const void *buf, size_t count);
67off_t io_seek(io_t *io, off_t offset, io_seek_whence_t whence);
68off_t io_tell(io_t *io);
69bool io_mmap_perm_check(io_t *io, vm_flags flags, bool is_private);
70bool io_mmap(io_t *io, vmap_t *vmap, off_t offset);
71bool io_munmap(io_t *io, vmap_t *vmap, bool *unmapped);
72void io_get_name(const io_t *io, char *buf, size_t size);
73