usb_ecm: Use the current configuration instead of a fixed one.
[haiku.git] / src / add-ons / kernel / drivers / common / dprintf.cpp
blob612a2b1e8f8802224948e0e263d31d3a78faaf58
1 /*
2 * Copyright 2005-2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Author(s):
6 * Ingo Weinhold <bonefish@users.sourceforge.net>
8 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
9 * Distributed under the terms of the NewOS License.
13 #include <debug.h>
15 #include <Drivers.h>
16 #include <KernelExport.h>
18 #include <string.h>
19 #include <termios.h>
22 #define DEVICE_NAME "dprintf"
24 int32 api_version = B_CUR_DRIVER_API_VERSION;
27 static status_t
28 dprintf_open(const char *name, uint32 flags, void **cookie)
30 *cookie = NULL;
31 return B_OK;
35 static status_t
36 dprintf_close(void *cookie)
38 return B_OK;
42 static status_t
43 dprintf_freecookie(void *cookie)
45 return B_OK;
49 static status_t
50 dprintf_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
52 if (op == TCGETA) {
53 // let isatty() think we are a terminal
54 // (this lets libroot use unbuffered I/O)
55 return B_OK;
58 return EPERM;
62 static status_t
63 dprintf_read(void *cookie, off_t pos, void *buffer, size_t *length)
65 *length = 0;
66 return B_OK;
70 static status_t
71 dprintf_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
73 const char *str = (const char*)buffer;
75 int bytesLeft = *_length;
76 while (bytesLeft > 0) {
77 int chunkSize = strnlen(str, bytesLeft);
78 if (chunkSize == 0) {
79 // null bytes -- skip
80 str++;
81 bytesLeft--;
82 continue;
85 if (chunkSize == bytesLeft) {
86 // no null-byte in the remainder of the buffer
87 // we need to copy to a local buffer and null-terminate
88 while (bytesLeft > 0) {
89 chunkSize = bytesLeft;
91 char localBuffer[512];
92 if (bytesLeft > (int)sizeof(localBuffer) - 1)
93 chunkSize = (int)sizeof(localBuffer) - 1;
94 memcpy(localBuffer, str, chunkSize);
95 localBuffer[chunkSize] = '\0';
97 debug_puts(localBuffer, chunkSize);
99 str += chunkSize;
100 bytesLeft -= chunkSize;
102 } else {
103 // null-terminated chunk -- just write it
104 debug_puts(str, chunkSize);
106 str += chunkSize + 1;
107 bytesLeft -= chunkSize + 1;
111 return B_OK;
115 // #pragma mark -
118 status_t
119 init_hardware(void)
121 return B_OK;
125 const char **
126 publish_devices(void)
128 static const char *devices[] = {
129 DEVICE_NAME,
130 NULL
133 return devices;
137 device_hooks *
138 find_device(const char *name)
140 static device_hooks hooks = {
141 &dprintf_open,
142 &dprintf_close,
143 &dprintf_freecookie,
144 &dprintf_ioctl,
145 &dprintf_read,
146 &dprintf_write,
147 /* Leave select/deselect/readv/writev undefined. The kernel will
148 * use its own default implementation. The basic hooks above this
149 * line MUST be defined, however. */
150 NULL,
151 NULL,
152 NULL,
153 NULL
156 if (!strcmp(name, DEVICE_NAME))
157 return &hooks;
159 return NULL;
163 status_t
164 init_driver(void)
166 return B_OK;
170 void
171 uninit_driver(void)