reorganize device initialization for mem devs

This commit is contained in:
Ryan Hileman 2018-12-10 00:16:23 -08:00
parent def615d367
commit 362935a57c
4 changed files with 68 additions and 20 deletions

View File

@ -1,6 +1,7 @@
#include "kernel/errno.h"
#include "fs/fd.h"
#include "fs/dev.h"
#include "fs/mem.h"
#include "fs/tty.h"
#pragma GCC diagnostic ignored "-Winitializer-overrides"
@ -9,7 +10,7 @@ struct dev_ops *block_devs[] = {
};
struct dev_ops *char_devs[] = {
[0 ... 255] = NULL,
[1] = &null_dev,
[1] = &mem_dev,
[4] = &tty_dev,
[5] = &tty_dev,
};
@ -23,22 +24,3 @@ int dev_open(int major, int minor, int type, struct fd *fd) {
return 0;
return dev->open(major, minor, type, fd);
}
// this device seemed so simple it was hardly worth making a new file for it
static int null_open(int major, int minor, int type, struct fd *fd) {
if (minor != 3)
return _ENXIO;
return 0;
}
static ssize_t null_read(struct fd *fd, void *buf, size_t bufsize) {
return 0;
}
static ssize_t null_write(struct fd *fd, const void *buf, size_t bufsize) {
return bufsize;
}
struct dev_ops null_dev = {
.open = null_open,
.fd.read = null_read,
.fd.write = null_write,
};

53
fs/mem.c Normal file
View File

@ -0,0 +1,53 @@
#include "kernel/errno.h"
#include "fs/mem.h"
#include "fs/dev.h"
// this file handles major device number 1, minor device numbers are mapped in table below
#pragma GCC diagnostic ignored "-Winitializer-overrides"
struct dev_ops *mem_devs[] = {
[0 ... 255] = NULL,
// [1] = &prog_mem_dev,
// [2] = &kmem_dev, // (not really applicable)
[3] = &null_dev,
// [4] = &port_dev,
// [5] = &zero_dev,
// [7] = &full_dev,
// [8] = &random_dev,
// [9] = &random_dev,
// [10] = &aio_dev,
// [11] = &kmsg_dev,
// [12] = &oldmem_dev, // replaced by /proc/vmcore
};
// dispatch device for major device 1
static int mem_open(int major, int minor, int type, struct fd *fd) {
struct dev_ops *dev = mem_devs[minor];
if (dev == NULL) {
return _ENXIO;
}
fd->ops = &dev->fd;
if (!dev->open)
return 0;
return dev->open(major, minor, type, fd);
}
struct dev_ops mem_dev = {
.open = mem_open,
};
// begin inline devices
static int null_open(int major, int minor, int type, struct fd *fd) {
return 0;
}
static ssize_t null_read(struct fd *fd, void *buf, size_t bufsize) {
return 0;
}
static ssize_t null_write(struct fd *fd, const void *buf, size_t bufsize) {
return bufsize;
}
struct dev_ops null_dev = {
.open = null_open,
.fd.read = null_read,
.fd.write = null_write,
};

12
fs/mem.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef FS_NULL_H
#define FS_NULL_H
#include "kernel/fs.h"
#include "fs/dev.h"
extern struct dev_ops
mem_dev,
null_dev,
random_dev;
#endif

View File

@ -77,6 +77,7 @@ src = [
'fs/pipe.c',
'fs/dev.c',
'fs/mem.c',
'fs/tty.c',
'fs/tty-real.c',