devfs: add devfs
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
7833400893
commit
4b17525d33
2 changed files with 108 additions and 0 deletions
93
fs/vfs/devfs.c
Normal file
93
fs/vfs/devfs.c
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/block.h>
|
||||
#include <drivers/tty.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
#include <fs/inode.h>
|
||||
#include <fs/ramfs.h>
|
||||
#include <fs/devfs.h>
|
||||
|
||||
#include <lib/errno.h>
|
||||
|
||||
static fs_t *f_devfs = NULL;
|
||||
|
||||
static ssize_t i_read_chr(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
*buf = tty_getchar(inode->data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ssize_t i_write_chr(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
for (size_t i = offset; i < size + offset; i++)
|
||||
tty_putchar(inode->data, buf[offset + i]);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static ssize_t i_read_blk(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
return block_read(inode->data, buf, offset, size);
|
||||
}
|
||||
|
||||
static ssize_t i_write_blk(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
return block_write(inode->data, buf, offset, size);
|
||||
}
|
||||
|
||||
static int devfs_insert_device(const char *name, u64 mode, dirent_t **out)
|
||||
{
|
||||
return vfs_creat(f_devfs->root_dirent, name, mode, 0, out);
|
||||
}
|
||||
|
||||
int devfs_insert_device_chr(const char *name, struct tty *device, dirent_t **out)
|
||||
{
|
||||
int res = devfs_insert_device(name, S_IFCHR, out);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
inode_t *ino = (*out)->inode;
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
ino->data = device;
|
||||
ino->i_ops.write = i_write_chr;
|
||||
ino->i_ops.read = i_read_chr;
|
||||
|
||||
write_unlock(&ino->rwlock);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int devfs_insert_device_blk(const char *name, struct block_device *device, dirent_t **out)
|
||||
{
|
||||
int res = devfs_insert_device(name, S_IFBLK, out);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
inode_t *ino = (*out)->inode;
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
ino->data = device;
|
||||
ino->i_ops.write = i_write_blk;
|
||||
ino->i_ops.read = i_read_blk;
|
||||
|
||||
write_unlock(&ino->rwlock);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void devfs_init(void)
|
||||
{
|
||||
int res = ramfs_create(0, &f_devfs);
|
||||
if (res < 0)
|
||||
panic("failed to create devfs: %s\n", strerror(-res));
|
||||
|
||||
res = vfs_mount("/dev", f_devfs, 0);
|
||||
if (res < 0)
|
||||
panic("failed to mount /dev: %s\n", strerror(-res));
|
||||
|
||||
krdy("devfs ready\n");
|
||||
}
|
||||
15
include/fs/devfs.h
Normal file
15
include/fs/devfs.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _DEVFS_H_
|
||||
#define _DEVFS_H_
|
||||
|
||||
#include <kernel/block.h>
|
||||
|
||||
#include <drivers/tty.h>
|
||||
|
||||
#include <fs/inode.h>
|
||||
|
||||
int devfs_insert_device_chr(const char *name, struct tty *device, dirent_t **out);
|
||||
int devfs_insert_device_blk(const char *name, struct block_device *device, dirent_t **out);
|
||||
void devfs_init();
|
||||
|
||||
#endif /* _DEVFS_H_ */
|
||||
Loading…
Reference in a new issue