forked from StevenBaby/onix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a4b37c
commit d631173
Showing
14 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 虚拟设备 | ||
|
||
对硬件设备进行一层抽象,使得读写更加的统一,方便以后的操作。 | ||
|
||
```c++ | ||
// 安装设备 | ||
dev_t device_install( | ||
int type, int subtype, | ||
void *ptr, char *name, dev_t parent, | ||
void *ioctl, void *read, void *write); | ||
|
||
// 根据子类型查找设备 | ||
device_t *device_find(int type, idx_t idx); | ||
|
||
// 根据设备号查找设备 | ||
device_t *device_get(dev_t dev); | ||
|
||
// 控制设备 | ||
int device_ioctl(dev_t dev, int cmd, void *args, int flags); | ||
|
||
// 读设备 | ||
int device_read(dev_t dev, void *buf, size_t count, idx_t idx, int flags); | ||
|
||
// 写设备 | ||
int device_write(dev_t dev, void *buf, size_t count, idx_t idx, int flags); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,5 @@ | |
|
||
void console_init(); | ||
void console_clear(); | ||
int32 console_write(char *buf, u32 count); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef ONIX_DEVICE_H | ||
#define ONIX_DEVICE_H | ||
|
||
#include <onix/types.h> | ||
|
||
#define NAMELEN 16 | ||
|
||
// 设备类型 | ||
enum device_type_t | ||
{ | ||
DEV_NULL, // 空设备 | ||
DEV_CHAR, // 字符设备 | ||
DEV_BLOCK, // 块设备 | ||
}; | ||
|
||
// 设备子类型 | ||
enum device_subtype_t | ||
{ | ||
DEV_CONSOLE = 1, // 控制台 | ||
DEV_KEYBOARD, // 键盘 | ||
}; | ||
|
||
typedef struct device_t | ||
{ | ||
char name[NAMELEN]; // 设备名 | ||
int type; // 设备类型 | ||
int subtype; // 设备子类型 | ||
dev_t dev; // 设备号 | ||
dev_t parent; // 父设备号 | ||
void *ptr; // 设备指针 | ||
// 设备控制 | ||
int (*ioctl)(void *dev, int cmd, void *args, int flags); | ||
// 读设备 | ||
int (*read)(void *dev, void *buf, size_t count, idx_t idx, int flags); | ||
// 写设备 | ||
int (*write)(void *dev, void *buf, size_t count, idx_t idx, int flags); | ||
} device_t; | ||
|
||
// 安装设备 | ||
dev_t device_install( | ||
int type, int subtype, | ||
void *ptr, char *name, dev_t parent, | ||
void *ioctl, void *read, void *write); | ||
|
||
// 根据子类型查找设备 | ||
device_t *device_find(int type, idx_t idx); | ||
|
||
// 根据设备号查找设备 | ||
device_t *device_get(dev_t dev); | ||
|
||
// 控制设备 | ||
int device_ioctl(dev_t dev, int cmd, void *args, int flags); | ||
|
||
// 读设备 | ||
int device_read(dev_t dev, void *buf, size_t count, idx_t idx, int flags); | ||
|
||
// 写设备 | ||
int device_write(dev_t dev, void *buf, size_t count, idx_t idx, int flags); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <onix/device.h> | ||
#include <onix/string.h> | ||
#include <onix/task.h> | ||
#include <onix/assert.h> | ||
#include <onix/debug.h> | ||
#include <onix/arena.h> | ||
|
||
#define LOGK(fmt, args...) DEBUGK(fmt, ##args) | ||
|
||
#define DEVICE_NR 64 // 设备数量 | ||
|
||
static device_t devices[DEVICE_NR]; // 设备数组 | ||
|
||
// 获取空设备 | ||
static device_t *get_null_device() | ||
{ | ||
for (size_t i = 1; i < DEVICE_NR; i++) | ||
{ | ||
device_t *device = &devices[i]; | ||
if (device->type == DEV_NULL) | ||
return device; | ||
} | ||
panic("no more devices!!!"); | ||
} | ||
|
||
int device_ioctl(dev_t dev, int cmd, void *args, int flags) | ||
{ | ||
device_t *device = device_get(dev); | ||
if (device->ioctl) | ||
{ | ||
return device->ioctl(device->ptr, cmd, args, flags); | ||
} | ||
LOGK("ioctl of device %d not implemented!!!\n", dev); | ||
return EOF; | ||
} | ||
|
||
int device_read(dev_t dev, void *buf, size_t count, idx_t idx, int flags) | ||
{ | ||
device_t *device = device_get(dev); | ||
if (device->read) | ||
{ | ||
return device->read(device->ptr, buf, count, idx, flags); | ||
} | ||
LOGK("read of device %d not implemented!!!\n", dev); | ||
return EOF; | ||
} | ||
|
||
int device_write(dev_t dev, void *buf, size_t count, idx_t idx, int flags) | ||
{ | ||
device_t *device = device_get(dev); | ||
if (device->write) | ||
{ | ||
return device->write(device->ptr, buf, count, idx, flags); | ||
} | ||
LOGK("write of device %d not implemented!!!\n", dev); | ||
return EOF; | ||
} | ||
|
||
// 安装设备 | ||
dev_t device_install( | ||
int type, int subtype, | ||
void *ptr, char *name, dev_t parent, | ||
void *ioctl, void *read, void *write) | ||
{ | ||
device_t *device = get_null_device(); | ||
device->ptr = ptr; | ||
device->parent = parent; | ||
device->type = type; | ||
device->subtype = subtype; | ||
strncpy(device->name, name, NAMELEN); | ||
device->ioctl = ioctl; | ||
device->read = read; | ||
device->write = write; | ||
return device->dev; | ||
} | ||
|
||
void device_init() | ||
{ | ||
for (size_t i = 0; i < DEVICE_NR; i++) | ||
{ | ||
device_t *device = &devices[i]; | ||
strcpy((char *)device->name, "null"); | ||
device->type = DEV_NULL; | ||
device->subtype = DEV_NULL; | ||
device->dev = i; | ||
device->parent = 0; | ||
device->ioctl = NULL; | ||
device->read = NULL; | ||
device->write = NULL; | ||
} | ||
} | ||
|
||
device_t *device_find(int subtype, idx_t idx) | ||
{ | ||
idx_t nr = 0; | ||
for (size_t i = 0; i < DEVICE_NR; i++) | ||
{ | ||
device_t *device = &devices[i]; | ||
if (device->subtype != subtype) | ||
continue; | ||
if (nr == idx) | ||
return device; | ||
nr++; | ||
} | ||
return NULL; | ||
} | ||
|
||
device_t *device_get(dev_t dev) | ||
{ | ||
assert(dev < DEVICE_NR); | ||
device_t *device = &devices[dev]; | ||
assert(device->type != DEV_NULL); | ||
return device; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters