2
* Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
4
* This library is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; either version 2.1 of the License, or
7
* (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this library; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
* An IOStream implementation using printk() for output.
21
* Input is not implemented.
25
#include <linux/config.h>
26
#include <linux/module.h>
27
#include <linux/kernel.h>
28
#include <linux/types.h>
29
#include <linux/errno.h>
30
#include <linux/slab.h>
31
#include <linux/spinlock.h>
33
#include "kernel_stream.h"
36
/** Number of characters in the output buffer.
37
* The kernel uses 1024 for printk, so that should suffice.
41
/** State for a kernel stream. */
42
typedef struct KernelData {
43
/** Stream lock. We need a lock to serialize access to the stream. */
45
/** Saved flags for locking. */
47
/** Size of the output buffer. */
53
static int kernel_write(IOStream *s, const void *msg, size_t n);
54
static void kernel_free(IOStream *s);
55
static void kernel_stream_lock(IOStream *s);
56
static void kernel_stream_unlock(IOStream *s);
58
/** Methods for a kernel stream. Output only. */
59
static const IOMethods kernel_methods = {
62
lock: kernel_stream_lock,
63
unlock: kernel_stream_unlock,
66
/** Shared state for kernel streams.
67
* All implementations write using printk, so we can use
68
* shared state and avoid allocating it.
70
static const KernelData kernel_data = {
71
lock: SPIN_LOCK_UNLOCKED,
76
/** Stream for kernel printk. */
77
static IOStream iokernel = {
78
methods: &kernel_methods,
83
/** Stream for kernel printk. */
84
IOStream *iostdout = &iokernel;
86
/** Stream for kernel printk. */
87
IOStream *iostdin = &iokernel;
89
/** Stream for kernel printk. */
90
IOStream *iostderr = &iokernel;
92
/** Get an output-only stream implementation using
93
* printk(). The stream uses static storage, and must not be freed.
95
* @return kernel stream
97
IOStream get_stream_kernel(void){
101
/** Obtain the lock on the stream state.
103
* @param kdata stream state
105
static inline void KernelData_lock(KernelData *kdata){
106
spin_lock_irqsave(&kdata->lock, kdata->flags);
109
/** Release the lock on the stream state.
111
* @param kdata stream state
113
static inline void KernelData_unlock(KernelData *kdata){
114
spin_unlock_irqrestore(&kdata->lock, kdata->flags);
117
/** Get the stream state.
119
* @param s kernel stream
120
* @return stream state
122
static inline KernelData *get_kernel_data(IOStream *s){
123
return (KernelData*)s->data;
126
/** Obtain the lock on the stream state.
130
void kernel_stream_lock(IOStream *s){
131
KernelData_lock(get_kernel_data(s));
134
/** Release the lock on the stream state.
138
void kernel_stream_unlock(IOStream *s){
139
KernelData_unlock(get_kernel_data(s));
142
/** Write to a kernel stream.
144
* @param stream kernel stream
145
* @param format print format
146
* @param args print arguments
147
* @return result of the print
149
static int kernel_write(IOStream *stream, const void *buf, size_t n){
150
KernelData *kdata = get_kernel_data(stream);
152
k = kdata->buf_n - 1;
154
memcpy(kdata->buf, buf, k);
155
kdata->buf[k] = '\0';
160
/** Free a kernel stream.
161
* Frees the internal state of the stream.
162
* Do not call this unless the stream was dynamically allocated.
163
* Do not call this on a stream returned from get_stream_kernel().
165
* @param io stream to free
167
static void kernel_free(IOStream *io){
169
if(io == &iokernel) return;
170
kdata = get_kernel_data(io);
171
memset(kdata, 0, sizeof(*kdata));
174
#endif /* __KERNEL__ */