xref: /haiku/src/add-ons/kernel/drivers/common/dprintf.cpp (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2  * Copyright 2005, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Author(s):
6  *   Ingo Weinhold <bonefish@users.sourceforge.net>
7  *
8  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
9  * Distributed under the terms of the NewOS License.
10  */
11 
12 
13 #include <string.h>
14 
15 #include <debug.h>
16 
17 #include <Drivers.h>
18 #include <KernelExport.h>
19 
20 
21 
22 #define DEVICE_NAME "dprintf"
23 
24 int32 api_version = B_CUR_DRIVER_API_VERSION;
25 
26 
27 static status_t
28 dprintf_open(const char *name, uint32 flags, void **cookie)
29 {
30 	*cookie = NULL;
31 	return B_OK;
32 }
33 
34 
35 static status_t
36 dprintf_close(void *cookie)
37 {
38 	return B_OK;
39 }
40 
41 
42 static status_t
43 dprintf_freecookie(void *cookie)
44 {
45 	return B_OK;
46 }
47 
48 
49 static status_t
50 dprintf_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
51 {
52 	return EPERM;
53 }
54 
55 
56 static status_t
57 dprintf_read(void *cookie, off_t pos, void *buffer, size_t *length)
58 {
59 	*length = 0;
60 	return B_OK;
61 }
62 
63 
64 static status_t
65 dprintf_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
66 {
67 	const char *str = (const char*)buffer;
68 
69 	int bytesLeft = *_length;
70 	while (bytesLeft > 0) {
71 		int chunkSize = strnlen(str, bytesLeft);
72 		if (chunkSize == 0) {
73 			// null bytes -- skip
74 			str++;
75 			bytesLeft--;
76 			continue;
77 		}
78 
79 		if (chunkSize == bytesLeft) {
80 			// no null-byte in the remainder of the buffer
81 			// we need to copy to a local buffer and null-terminate
82 			while (bytesLeft > 0) {
83 				chunkSize = bytesLeft;
84 
85 				char localBuffer[512];
86 				if (bytesLeft > (int)sizeof(localBuffer) - 1)
87 					chunkSize = (int)sizeof(localBuffer) - 1;
88 				memcpy(localBuffer, str, chunkSize);
89 				localBuffer[chunkSize] = '\0';
90 
91 				debug_puts(localBuffer, chunkSize);
92 
93 				str += chunkSize;
94 				bytesLeft -= chunkSize;
95 			}
96 		} else {
97 			// null-terminated chunk -- just write it
98 			debug_puts(str, chunkSize);
99 
100 			str += chunkSize + 1;
101 			bytesLeft -= chunkSize + 1;
102 		}
103 	}
104 
105 	return B_OK;
106 }
107 
108 
109 //	#pragma mark -
110 
111 
112 status_t
113 init_hardware(void)
114 {
115 	return B_OK;
116 }
117 
118 
119 const char **
120 publish_devices(void)
121 {
122 	static const char *devices[] = {
123 		DEVICE_NAME,
124 		NULL
125 	};
126 
127 	return devices;
128 }
129 
130 
131 device_hooks *
132 find_device(const char *name)
133 {
134 	static device_hooks hooks = {
135 		&dprintf_open,
136 		&dprintf_close,
137 		&dprintf_freecookie,
138 		&dprintf_ioctl,
139 		&dprintf_read,
140 		&dprintf_write,
141 		/* Leave select/deselect/readv/writev undefined. The kernel will
142 		 * use its own default implementation. The basic hooks above this
143 		 * line MUST be defined, however. */
144 		NULL,
145 		NULL,
146 		NULL,
147 		NULL
148 	};
149 
150 	if (!strcmp(name, DEVICE_NAME))
151 		return &hooks;
152 
153 	return NULL;
154 }
155 
156 
157 status_t
158 init_driver(void)
159 {
160 	return B_OK;
161 }
162 
163 
164 void
165 uninit_driver(void)
166 {
167 }
168 
169