xref: /haiku/src/system/kernel/device_manager/AbstractModuleDevice.cpp (revision 103c671ef02c6b7417f4842f5675f180d1544cb9)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "AbstractModuleDevice.h"
9 
10 #include "IORequest.h"
11 
12 
AbstractModuleDevice()13 AbstractModuleDevice::AbstractModuleDevice()
14 	:
15 	fNode(NULL),
16 	fInitialized(0),
17 	fDeviceModule(NULL),
18 	fDeviceData(NULL)
19 {
20 }
21 
22 
~AbstractModuleDevice()23 AbstractModuleDevice::~AbstractModuleDevice()
24 {
25 }
26 
27 
28 bool
HasSelect() const29 AbstractModuleDevice::HasSelect() const
30 {
31 	return Module()->select != NULL;
32 }
33 
34 
35 bool
HasDeselect() const36 AbstractModuleDevice::HasDeselect() const
37 {
38 	return Module()->deselect != NULL;
39 }
40 
41 
42 bool
HasRead() const43 AbstractModuleDevice::HasRead() const
44 {
45 	return Module()->read != NULL;
46 }
47 
48 
49 bool
HasWrite() const50 AbstractModuleDevice::HasWrite() const
51 {
52 	return Module()->write != NULL;
53 }
54 
55 
56 bool
HasIO() const57 AbstractModuleDevice::HasIO() const
58 {
59 	return Module()->io != NULL;
60 }
61 
62 
63 status_t
Open(const char * path,int openMode,void ** _cookie)64 AbstractModuleDevice::Open(const char* path, int openMode, void** _cookie)
65 {
66 	return Module()->open(Data(), path, openMode, _cookie);
67 }
68 
69 
70 status_t
_DoIO(void * cookie,off_t pos,void * buffer,size_t * _length,bool isWrite)71 AbstractModuleDevice::_DoIO(void* cookie, off_t pos,
72 	void* buffer, size_t* _length, bool isWrite)
73 {
74 	IORequest request;
75 	status_t status = request.Init(pos, (addr_t)buffer, *_length, isWrite, 0);
76 	if (status != B_OK)
77 		return status;
78 
79 	status = IO(cookie, &request);
80 	if (status != B_OK)
81 		return status;
82 
83 	status = request.Wait(0, 0);
84 	*_length = request.TransferredBytes();
85 	return status;
86 }
87 
88 
89 status_t
Read(void * cookie,off_t pos,void * buffer,size_t * _length)90 AbstractModuleDevice::Read(void* cookie, off_t pos, void* buffer, size_t* _length)
91 {
92 	if (Module()->read == NULL) {
93 		if (Module()->io == NULL)
94 			return BaseDevice::Read(cookie, pos, buffer, _length);
95 
96 		return _DoIO(cookie, pos, buffer, _length, false);
97 	}
98 	return Module()->read(cookie, pos, buffer, _length);
99 }
100 
101 
102 status_t
Write(void * cookie,off_t pos,const void * buffer,size_t * _length)103 AbstractModuleDevice::Write(void* cookie, off_t pos, const void* buffer, size_t* _length)
104 {
105 	if (Module()->write == NULL) {
106 		if (Module()->io == NULL)
107 			return BaseDevice::Write(cookie, pos, buffer, _length);
108 
109 		return _DoIO(cookie, pos, const_cast<void*>(buffer), _length, true);
110 	}
111 	return Module()->write(cookie, pos, buffer, _length);
112 }
113 
114 
115 status_t
IO(void * cookie,io_request * request)116 AbstractModuleDevice::IO(void* cookie, io_request* request)
117 {
118 	if (Module()->io == NULL)
119 		return BaseDevice::IO(cookie, request);
120 	return Module()->io(cookie, request);
121 }
122 
123 
124 status_t
Control(void * cookie,int32 op,void * buffer,size_t length)125 AbstractModuleDevice::Control(void* cookie, int32 op, void* buffer, size_t length)
126 {
127 	if (Module()->control == NULL)
128 		return BaseDevice::Control(cookie, op, buffer, length);
129 	return Module()->control(cookie, op, buffer, length);
130 }
131 
132 
133 status_t
Select(void * cookie,uint8 event,selectsync * sync)134 AbstractModuleDevice::Select(void* cookie, uint8 event, selectsync* sync)
135 {
136 	if (Module()->select == NULL)
137 		return BaseDevice::Select(cookie, event, sync);
138 	return Module()->select(cookie, event, sync);
139 }
140 
141 
142 status_t
Deselect(void * cookie,uint8 event,selectsync * sync)143 AbstractModuleDevice::Deselect(void* cookie, uint8 event, selectsync* sync)
144 {
145 	if (Module()->deselect == NULL)
146 		return BaseDevice::Deselect(cookie, event, sync);
147 	return Module()->deselect(cookie, event, sync);
148 }
149 
150 
151 status_t
Close(void * cookie)152 AbstractModuleDevice::Close(void* cookie)
153 {
154 	return Module()->close(cookie);
155 }
156 
157 
158 status_t
Free(void * cookie)159 AbstractModuleDevice::Free(void* cookie)
160 {
161 	return Module()->free(cookie);
162 }
163