2 * Copyright (C) 2009-2012 Semihalf
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
39 #include <machine/stdarg.h>
41 #include <dev/nand/nandsim_log.h>
43 int nandsim_log_level;
44 int nandsim_log_output;
45 int log_size = NANDSIM_RAM_LOG_SIZE;
47 static int nandsim_entry_size = NANDSIM_ENTRY_SIZE;
48 static int nandsim_entry_count = NANDSIM_ENTRY_COUNT;
49 static int str_index = 0;
50 static char string[NANDSIM_ENTRY_SIZE + 1] = {0};
53 nandsim_log_init(struct nandsim_softc *sc, char *filename)
57 if (nandsim_log_output == NANDSIM_OUTPUT_FILE) {
58 error = alq_open(&sc->alq, filename,
59 curthread->td_ucred, 0644,
60 nandsim_entry_size, nandsim_entry_count);
61 } else if (nandsim_log_output == NANDSIM_OUTPUT_RAM) {
62 sc->log_buff = malloc(log_size, M_NANDSIM, M_WAITOK | M_ZERO);
71 nandsim_log_close(struct nandsim_softc *sc)
74 if (nandsim_log_output == NANDSIM_OUTPUT_FILE) {
75 memset(&string[str_index], 0, NANDSIM_ENTRY_SIZE - str_index);
76 alq_write(sc->alq, (void *) string, ALQ_NOWAIT);
80 } else if (nandsim_log_output == NANDSIM_OUTPUT_RAM) {
81 free(sc->log_buff, M_NANDSIM);
87 nandsim_log(struct nandsim_chip *chip, int level, const char *fmt, ...)
89 char hdr[TIME_STR_SIZE];
90 char tmp[NANDSIM_ENTRY_SIZE];
91 struct nandsim_softc *sc;
92 struct timeval currtime;
94 int hdr_len, len, rest;
96 if (nandsim_log_output == NANDSIM_OUTPUT_NONE)
103 if (!sc->alq && nandsim_log_output == NANDSIM_OUTPUT_FILE)
106 if (level <= nandsim_log_level) {
107 microtime(&currtime);
108 hdr_len = sprintf(hdr, "%08jd.%08li [chip:%d, ctrl:%d]: ",
109 (intmax_t)currtime.tv_sec, currtime.tv_usec,
110 chip->chip_num, chip->ctrl_num);
112 switch(nandsim_log_output) {
113 case NANDSIM_OUTPUT_CONSOLE:
119 case NANDSIM_OUTPUT_RAM:
121 len = vsnprintf(tmp, NANDSIM_ENTRY_SIZE - 1, fmt, ap);
122 tmp[NANDSIM_ENTRY_SIZE - 1] = 0;
125 rest = log_size - sc->log_idx - 1;
126 if (rest >= hdr_len) {
127 bcopy(hdr, &sc->log_buff[sc->log_idx],
129 sc->log_idx += hdr_len;
130 sc->log_buff[sc->log_idx] = 0;
132 bcopy(hdr, &sc->log_buff[sc->log_idx], rest);
133 bcopy(&hdr[rest], sc->log_buff,
135 sc->log_idx = hdr_len - rest;
136 sc->log_buff[sc->log_idx] = 0;
139 rest = log_size - sc->log_idx - 1;
141 bcopy(tmp, &sc->log_buff[sc->log_idx], len);
143 sc->log_buff[sc->log_idx] = 0;
145 bcopy(tmp, &sc->log_buff[sc->log_idx], rest);
146 bcopy(&tmp[rest], sc->log_buff, len - rest);
147 sc->log_idx = len - rest;
148 sc->log_buff[sc->log_idx] = 0;
153 case NANDSIM_OUTPUT_FILE:
155 len = vsnprintf(tmp, NANDSIM_ENTRY_SIZE - 1, fmt, ap);
156 tmp[NANDSIM_ENTRY_SIZE - 1] = 0;
159 rest = NANDSIM_ENTRY_SIZE - str_index;
160 if (rest >= hdr_len) {
162 str_index += hdr_len;
164 strlcat(string, hdr, NANDSIM_ENTRY_SIZE + 1);
165 alq_write(sc->alq, (void *) string,
167 strcpy(string, &hdr[rest]);
168 str_index = hdr_len - rest;
170 rest = NANDSIM_ENTRY_SIZE - str_index;
175 strlcat(string, tmp, NANDSIM_ENTRY_SIZE + 1);
176 alq_write(sc->alq, (void *) string,
178 strcpy(string, &tmp[rest]);
179 str_index = len - rest;