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
13AbstractModuleDevice::AbstractModuleDevice()
14	:
15	fNode(NULL),
16	fInitialized(0),
17	fDeviceModule(NULL),
18	fDeviceData(NULL)
19{
20}
21
22
23AbstractModuleDevice::~AbstractModuleDevice()
24{
25}
26
27
28bool
29AbstractModuleDevice::HasSelect() const
30{
31	return Module()->select != NULL;
32}
33
34
35bool
36AbstractModuleDevice::HasDeselect() const
37{
38	return Module()->deselect != NULL;
39}
40
41
42bool
43AbstractModuleDevice::HasRead() const
44{
45	return Module()->read != NULL;
46}
47
48
49bool
50AbstractModuleDevice::HasWrite() const
51{
52	return Module()->write != NULL;
53}
54
55
56bool
57AbstractModuleDevice::HasIO() const
58{
59	return Module()->io != NULL;
60}
61
62
63status_t
64AbstractModuleDevice::Open(const char* path, int openMode, void** _cookie)
65{
66	return Module()->open(Data(), path, openMode, _cookie);
67}
68
69
70status_t
71AbstractModuleDevice::_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
89status_t
90AbstractModuleDevice::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
102status_t
103AbstractModuleDevice::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
115status_t
116AbstractModuleDevice::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
124status_t
125AbstractModuleDevice::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
133status_t
134AbstractModuleDevice::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
142status_t
143AbstractModuleDevice::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
151status_t
152AbstractModuleDevice::Close(void* cookie)
153{
154	return Module()->close(cookie);
155}
156
157
158status_t
159AbstractModuleDevice::Free(void* cookie)
160{
161	return Module()->free(cookie);
162}
163