xref: /haiku/src/tests/system/kernel/device_manager/playground/driver.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "bus.h"
8 
9 #include <KernelExport.h>
10 
11 
12 #define DRIVER_MODULE_NAME "drivers/net/sample_driver/driver_v1"
13 #define DRIVER_DEVICE_MODULE_NAME "drivers/net/sample_driver/device_v1"
14 
15 
16 //	#pragma mark - driver
17 
18 
19 static float
20 supports_device(device_node* parent)
21 {
22 #if 0
23 	const char* bus;
24 	if (gDeviceManager->get_attr_string(parent, B_DRIVER_BUS, &bus, false)
25 			!= B_OK)
26 		return -1;
27 
28 	if (bus == NULL || strcmp(bus, BUS_NAME))
29 		return -1;
30 #endif
31 
32 	bus_for_driver_module_info* module;
33 	void* data;
34 	gDeviceManager->get_driver(parent, (driver_module_info**)&module, &data);
35 
36 	if (strcmp(module->info.info.name, BUS_FOR_DRIVER_NAME))
37 		return -1;
38 
39 	bus_info info;
40 	if (module->get_bus_info(data, &info) == B_OK
41 		&& info.vendor_id == 0x1001
42 		&& info.device_id == 0x0001)
43 		return 1.0;
44 
45 	return 0.0;
46 }
47 
48 
49 static status_t
50 register_device(device_node* parent)
51 {
52 	return gDeviceManager->register_node(parent, DRIVER_MODULE_NAME, NULL,
53 		NULL, NULL);
54 }
55 
56 
57 static status_t
58 init_driver(device_node* node, void** _cookie)
59 {
60 	*_cookie = node;
61 	return B_OK;
62 }
63 
64 
65 static void
66 uninit_driver(void* cookie)
67 {
68 }
69 
70 
71 static status_t
72 register_child_devices(void* cookie)
73 {
74 	device_node* node = (device_node*)cookie;
75 
76 	gDeviceManager->publish_device(node, "net/sample/0",
77 		DRIVER_DEVICE_MODULE_NAME);
78 	return B_OK;
79 }
80 
81 
82 static void
83 driver_device_removed(void* cookie)
84 {
85 }
86 
87 
88 //	#pragma mark - device
89 
90 
91 static status_t
92 init_device(void* driverCookie, void** _deviceCookie)
93 {
94 	// called once before one or several open() calls
95 	return B_OK;
96 }
97 
98 
99 static void
100 uninit_device(void* deviceCookie)
101 {
102 	// supposed to free deviceCookie, called when the last reference to
103 	// the device is closed
104 }
105 
106 
107 static void
108 device_removed(void* deviceCookie)
109 {
110 	dprintf("network device removed!\n");
111 }
112 
113 
114 static status_t
115 device_open(void* deviceCookie, int openMode, void** _cookie)
116 {
117 	// deviceCookie is an object attached to the published device
118 	return B_ERROR;
119 }
120 
121 
122 static status_t
123 device_close(void* cookie)
124 {
125 	return B_ERROR;
126 }
127 
128 
129 static status_t
130 device_free(void* cookie)
131 {
132 	return B_ERROR;
133 }
134 
135 
136 static status_t
137 device_read(void* cookie, off_t pos, void* buffer, size_t* _length)
138 {
139 	return B_ERROR;
140 }
141 
142 
143 static status_t
144 device_write(void* cookie, off_t pos, const void* buffer, size_t* _length)
145 {
146 	return B_ERROR;
147 }
148 
149 
150 static status_t
151 device_ioctl(void* cookie, int32 op, void* buffer, size_t length)
152 {
153 	return B_ERROR;
154 }
155 
156 
157 static status_t
158 device_io(void* cookie, io_request* request)
159 {
160 	// new function to deal with I/O requests directly.
161 	return B_ERROR;
162 }
163 
164 
165 //	#pragma mark -
166 
167 
168 struct driver_module_info gDriverModuleInfo = {
169 	{
170 		DRIVER_MODULE_NAME,
171 		0,
172 		NULL,
173 	},
174 
175 	supports_device,
176 	register_device,
177 	init_driver,
178 	uninit_driver,
179 	register_child_devices,
180 	NULL,
181 	driver_device_removed,
182 };
183 
184 struct device_module_info gDeviceModuleInfo = {
185 	{
186 		DRIVER_DEVICE_MODULE_NAME,
187 		0,
188 		NULL,
189 	},
190 
191 	init_device,
192 	uninit_device,
193 	device_removed,
194 
195 	device_open,
196 	device_close,
197 	device_free,
198 	device_read,
199 	device_write,
200 	device_ioctl,
201 	device_io,
202 };
203