forked from haiku/haiku
-
Notifications
You must be signed in to change notification settings - Fork 2
/
null.cpp
115 lines (79 loc) · 2.07 KB
/
null.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <new>
#include <dm2/device_manager.h>
#include <AutoDeleter.h>
#define CHECK_RET(err) {status_t _err = (err); if (_err < B_OK) return _err;}
#define NULL_DRIVER_MODULE_NAME "drivers/null/driver/v1"
class NullDriver;
class NullDevFsNode: public DevFsNode, public DevFsNodeHandle {
public:
NullDevFsNode(NullDriver& driver): fDriver(driver) {}
virtual ~NullDevFsNode() = default;
Capabilities GetCapabilities() const final {return {.read = true, .write = true};}
status_t Open(const char* path, int openMode, DevFsNodeHandle** outHandle) final;
status_t Read(off_t pos, void* buffer, size_t* _length) final;
status_t Write(off_t pos, const void* buffer, size_t* _length) final;
private:
NullDriver& fDriver;
};
class NullDriver: public DeviceDriver {
public:
NullDriver(DeviceNode* node): fNode(node), fDevFsNode(*this) {}
virtual ~NullDriver() = default;
static status_t Probe(DeviceNode* node, DeviceDriver** driver);
void Free() final;
private:
status_t Init();
private:
DeviceNode* fNode {};
NullDevFsNode fDevFsNode;
};
// #pragma mark - NullDevFsNode
status_t
NullDevFsNode::Open(const char* path, int openMode, DevFsNodeHandle** outHandle)
{
*outHandle = static_cast<DevFsNodeHandle*>(this);
return B_OK;
}
status_t
NullDevFsNode::Read(off_t pos, void* buffer, size_t* _length)
{
*_length = 0;
return B_OK;
}
status_t
NullDevFsNode::Write(off_t pos, const void* buffer, size_t* _length)
{
return B_OK;
}
// #pragma mark - NullDriver
status_t
NullDriver::Probe(DeviceNode* node, DeviceDriver** outDriver)
{
ObjectDeleter<NullDriver> driver(new(std::nothrow) NullDriver(node));
if (!driver.IsSet())
return B_NO_MEMORY;
CHECK_RET(driver->Init());
*outDriver = driver.Detach();
return B_OK;
}
void
NullDriver::Free()
{
delete this;
}
status_t
NullDriver::Init()
{
CHECK_RET(fNode->RegisterDevFsNode("null", &fDevFsNode));
return B_OK;
}
static driver_module_info sNullModuleInfo = {
.info = {
.name = NULL_DRIVER_MODULE_NAME,
},
.probe = NullDriver::Probe
};
_EXPORT module_info* modules[] = {
(module_info* )&sNullModuleInfo,
NULL
};