xref: /haiku/src/system/kernel/device_manager/BaseDevice.h (revision b6b0567fbd186f8ce8a0c90bdc7a7b5b4c649678)
1 /*
2  * Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef BASE_DEVICE_H
6 #define BASE_DEVICE_H
7 
8 
9 #include <device_manager.h>
10 
11 
12 class BaseDevice {
13 public:
14 							BaseDevice();
15 	virtual					~BaseDevice();
16 
17 			void			SetID(ino_t id) { fID = id; }
18 			ino_t			ID() const { return fID; }
19 
20 			device_node*	Node() const { return fNode; }
21 
22 	virtual	status_t		InitDevice();
23 	virtual	void			UninitDevice();
24 
25 	virtual void			Removed();
26 
27 			device_module_info* Module() const { return fDeviceModule; }
28 			void*			Data() const { return fDeviceData; }
29 
30 			bool			HasSelect() const
31 								{ return Module()->select != NULL; }
32 			bool			HasDeselect() const
33 								{ return Module()->deselect != NULL; }
34 			bool			HasRead() const
35 								{ return Module()->read != NULL; }
36 			bool			HasWrite() const
37 								{ return Module()->write != NULL; }
38 			bool			HasIO() const
39 								{ return Module()->io != NULL; }
40 
41 	virtual	status_t		Open(const char* path, int openMode,
42 								void** _cookie);
43 			status_t		Read(void* cookie, off_t pos, void* buffer,
44 								size_t* _length);
45 			status_t		Write(void* cookie, off_t pos, const void* buffer,
46 								size_t* _length);
47 			status_t		IO(void* cookie, io_request* request);
48 			status_t		Control(void* cookie, int32 op, void* buffer,
49 								size_t length);
50 	virtual	status_t		Select(void* cookie, uint8 event, selectsync* sync);
51 			status_t		Deselect(void* cookie, uint8 event,
52 								selectsync* sync);
53 
54 			status_t		Close(void* cookie);
55 			status_t		Free(void* cookie);
56 
57 protected:
58 	ino_t					fID;
59 	device_node*			fNode;
60 	int32					fInitialized;
61 	device_module_info*		fDeviceModule;
62 	void*					fDeviceData;
63 };
64 
65 
66 inline status_t
67 BaseDevice::Open(const char* path, int openMode, void** _cookie)
68 {
69 	return Module()->open(Data(), path, openMode, _cookie);
70 }
71 
72 
73 inline status_t
74 BaseDevice::Read(void* cookie, off_t pos, void* buffer, size_t* _length)
75 {
76 	return Module()->read(cookie, pos, buffer, _length);
77 }
78 
79 
80 inline status_t
81 BaseDevice::Write(void* cookie, off_t pos, const void* buffer, size_t* _length)
82 {
83 	return Module()->write(cookie, pos, buffer, _length);
84 }
85 
86 
87 inline status_t
88 BaseDevice::IO(void* cookie, io_request* request)
89 {
90 	return Module()->io(cookie, request);
91 }
92 
93 
94 inline status_t
95 BaseDevice::Control(void* cookie, int32 op, void* buffer, size_t length)
96 {
97 	return Module()->control(cookie, op, buffer, length);
98 }
99 
100 
101 inline status_t
102 BaseDevice::Select(void* cookie, uint8 event, selectsync* sync)
103 {
104 	return Module()->select(cookie, event, sync);
105 }
106 
107 
108 inline status_t
109 BaseDevice::Deselect(void* cookie, uint8 event, selectsync* sync)
110 {
111 	return Module()->deselect(cookie, event, sync);
112 }
113 
114 
115 inline status_t
116 BaseDevice::Close(void* cookie)
117 {
118 	return Module()->close(cookie);
119 }
120 
121 
122 inline status_t
123 BaseDevice::Free(void* cookie)
124 {
125 	return Module()->free(cookie);
126 }
127 
128 #endif	// BASE_DEVICE_H
129