xref: /haiku/src/add-ons/kernel/drivers/misc/test.c (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright (c) 2007 Marcus Overhagen <marcus@overhagen.de>
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify,
8  * merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <KernelExport.h>
26 #include <Drivers.h>
27 #include <Errors.h>
28 
29 int32 			api_version = B_CUR_DRIVER_API_VERSION;
30 static int32	sOpenMask;
31 
32 status_t
33 init_hardware(void)
34 {
35 	dprintf("test: init_hardware\n");
36 	return B_OK;
37 }
38 
39 
40 status_t
41 uninit_hardware(void)
42 {
43 	dprintf("test: uninit_hardware\n");
44 	return B_OK;
45 }
46 
47 
48 status_t
49 init_driver(void)
50 {
51 	dprintf("test: init_driver\n");
52 	return B_OK;
53 }
54 
55 
56 void
57 uninit_driver(void)
58 {
59 	dprintf("test: uninit_driver\n");
60 }
61 
62 
63 static status_t
64 driver_open(const char *name, uint32 flags, void** _cookie)
65 {
66 	dprintf("test: open\n");
67 
68 	if (atomic_or(&sOpenMask, 1)) {
69 		dprintf("test: open, BUSY!\n");
70 		return B_BUSY;
71 	}
72 
73 	dprintf("test: open, success\n");
74 	return B_OK;
75 }
76 
77 
78 static status_t
79 driver_close(void* cookie)
80 {
81 	dprintf("test: close enter\n");
82 	snooze(200000);
83 	dprintf("test: close leave\n");
84 	return B_OK;
85 }
86 
87 
88 static status_t
89 driver_free(void* cookie)
90 {
91 	dprintf("test: free\n");
92 	atomic_and(&sOpenMask, ~1);
93 	return B_OK;
94 }
95 
96 
97 static status_t
98 driver_read(void* cookie, off_t position, void *buf, size_t* num_bytes)
99 {
100 	dprintf("test: read\n");
101 	*num_bytes = 0; // nothing to read
102 	return B_ERROR;
103 }
104 
105 
106 static status_t
107 driver_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes)
108 {
109 	dprintf("test: write\n");
110 	snooze(1000000);	// 1s
111 	*num_bytes = 1;		// pretend 1 byte was written
112 	return B_OK;
113 }
114 
115 
116 static status_t
117 driver_control(void *cookie, uint32 op, void *arg, size_t len)
118 {
119 	dprintf("test: control\n");
120 	return B_ERROR;
121 }
122 
123 
124 const char**
125 publish_devices(void)
126 {
127 	static const char *names[] = {"misc/test/1", NULL};
128 	dprintf("test: publish_devices\n");
129 	return names;
130 }
131 
132 
133 device_hooks*
134 find_device(const char* name)
135 {
136 	static device_hooks hooks = {
137 		driver_open,
138 		driver_close,
139 		driver_free,
140 		driver_control,
141 		driver_read,
142 		driver_write,
143 	};
144 	dprintf("test: find_device\n");
145 	return &hooks;
146 }
147 
148