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