2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
32 #include <linux/kernel.h>
33 #include <linux/sched.h> /* For jiffies, task states */
34 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
35 #include <linux/vmalloc.h>
37 #include "dgnc_driver.h"
39 #define TRC_TO_CONSOLE 1
41 /* file level globals */
42 static char *dgnc_trcbuf; /* the ringbuffer */
44 #if defined(TRC_TO_KMEM)
45 static int dgnc_trcbufi = 0; /* index of the tilde at the end of */
48 #if defined(TRC_TO_KMEM)
49 static DEFINE_SPINLOCK(dgnc_tracef_lock);
55 #if !defined(TRC_TO_KMEM) && !defined(TRC_TO_CONSOLE)
57 void dgnc_tracef(const char *fmt, ...)
62 #else /* !defined(TRC_TO_KMEM) && !defined(TRC_TO_CONSOLE) */
64 void dgnc_tracef(const char *fmt, ...)
67 char buf[TRC_MAXMSG+1];
70 static int failed = FALSE;
71 # if defined(TRC_TO_KMEM)
77 # if defined(TRC_TO_KMEM)
78 DGNC_LOCK(dgnc_tracef_lock, flags);
81 /* Format buf using fmt and arguments contained in ap. */
83 i = vsprintf(buf, fmt, ap);
87 # if defined(TRC_TO_KMEM)
92 * Now, in addition to (or instead of) printing this stuff out
93 * (which is a buffered operation), also tuck it away into a
94 * corner of memory which can be examined post-crash in kdb.
97 dgnc_trcbuf = (char *) vmalloc(dgnc_trcbuf_size);
100 printk("dgnc: tracing init failed!\n");
104 memset(dgnc_trcbuf, '\0', dgnc_trcbuf_size);
108 printk("dgnc: tracing enabled - " TRC_DTRC
110 (unsigned long)dgnc_trcbuf,
114 # if defined(TRC_ON_OVERFLOW_WRAP_AROUND)
116 * This is the less CPU-intensive way to do things. We simply
117 * wrap around before we fall off the end of the buffer. A
118 * tilde (~) demarcates the current end of the trace.
120 * This method should be used if you are concerned about race
121 * conditions as it is less likely to affect the timing of
125 if (dgnc_trcbufi + lenbuf >= dgnc_trcbuf_size) {
126 /* We are wrapping, so wipe out the last tilde. */
127 dgnc_trcbuf[dgnc_trcbufi] = '\0';
128 /* put the new string at the beginning of the buffer */
132 strcpy(&dgnc_trcbuf[dgnc_trcbufi], buf);
133 dgnc_trcbufi += lenbuf;
134 dgnc_trcbuf[dgnc_trcbufi] = '~';
136 # elif defined(TRC_ON_OVERFLOW_SHIFT_BUFFER)
138 * This is the more CPU-intensive way to do things. If we
139 * venture into the last 1/8 of the buffer, we shift the
140 * last 7/8 of the buffer forward, wiping out the first 1/8.
141 * Advantage: No wrap-around, only truncation from the
144 * This method should not be used if you are concerned about
145 * timing changes affecting the behaviour of the driver (ie,
148 strcpy(&dgnc_trcbuf[dgnc_trcbufi], buf);
149 dgnc_trcbufi += lenbuf;
150 dgnc_trcbuf[dgnc_trcbufi] = '~';
151 dgnc_trcbuf[dgnc_trcbufi+1] = '\0';
153 /* If we're near the end of the trace buffer... */
154 if (dgnc_trcbufi > (dgnc_trcbuf_size/8)*7) {
155 /* Wipe out the first eighth to make some more room. */
156 strcpy(dgnc_trcbuf, &dgnc_trcbuf[dgnc_trcbuf_size/8]);
157 dgnc_trcbufi = strlen(dgnc_trcbuf)-1;
158 /* Plop overflow message at the top of the buffer. */
159 bcopy(TRC_OVERFLOW, dgnc_trcbuf, strlen(TRC_OVERFLOW));
162 # error "TRC_ON_OVERFLOW_WRAP_AROUND or TRC_ON_OVERFLOW_SHIFT_BUFFER?"
165 DGNC_UNLOCK(dgnc_tracef_lock, flags);
167 # endif /* defined(TRC_TO_KMEM) */
170 #endif /* !defined(TRC_TO_KMEM) && !defined(TRC_TO_CONSOLE) */
180 void dgnc_tracer_free(void)