2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2009-2012 Semihalf
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
41 #include <machine/stdarg.h>
43 #include <dev/nand/nandsim_log.h>
45 int nandsim_log_level;
46 int nandsim_log_output;
47 int log_size = NANDSIM_RAM_LOG_SIZE;
49 static int nandsim_entry_size = NANDSIM_ENTRY_SIZE;
50 static int nandsim_entry_count = NANDSIM_ENTRY_COUNT;
51 static int str_index = 0;
52 static char string[NANDSIM_ENTRY_SIZE + 1] = {0};
55 nandsim_log_init(struct nandsim_softc *sc, char *filename)
59 if (nandsim_log_output == NANDSIM_OUTPUT_FILE) {
60 error = alq_open(&sc->alq, filename,
61 curthread->td_ucred, 0644,
62 nandsim_entry_size, nandsim_entry_count);
63 } else if (nandsim_log_output == NANDSIM_OUTPUT_RAM) {
64 sc->log_buff = malloc(log_size, M_NANDSIM, M_WAITOK | M_ZERO);
73 nandsim_log_close(struct nandsim_softc *sc)
76 if (nandsim_log_output == NANDSIM_OUTPUT_FILE) {
77 memset(&string[str_index], 0, NANDSIM_ENTRY_SIZE - str_index);
78 alq_write(sc->alq, (void *) string, ALQ_NOWAIT);
82 } else if (nandsim_log_output == NANDSIM_OUTPUT_RAM) {
83 free(sc->log_buff, M_NANDSIM);
89 nandsim_log(struct nandsim_chip *chip, int level, const char *fmt, ...)
91 char hdr[TIME_STR_SIZE];
92 char tmp[NANDSIM_ENTRY_SIZE];
93 struct nandsim_softc *sc;
94 struct timeval currtime;
96 int hdr_len, len, rest;
98 if (nandsim_log_output == NANDSIM_OUTPUT_NONE)
105 if (!sc->alq && nandsim_log_output == NANDSIM_OUTPUT_FILE)
108 if (level <= nandsim_log_level) {
109 microtime(&currtime);
110 hdr_len = sprintf(hdr, "%08jd.%08li [chip:%d, ctrl:%d]: ",
111 (intmax_t)currtime.tv_sec, currtime.tv_usec,
112 chip->chip_num, chip->ctrl_num);
114 switch(nandsim_log_output) {
115 case NANDSIM_OUTPUT_CONSOLE:
121 case NANDSIM_OUTPUT_RAM:
123 len = vsnprintf(tmp, NANDSIM_ENTRY_SIZE - 1, fmt, ap);
124 tmp[NANDSIM_ENTRY_SIZE - 1] = 0;
127 rest = log_size - sc->log_idx - 1;
128 if (rest >= hdr_len) {
129 bcopy(hdr, &sc->log_buff[sc->log_idx],
131 sc->log_idx += hdr_len;
132 sc->log_buff[sc->log_idx] = 0;
134 bcopy(hdr, &sc->log_buff[sc->log_idx], rest);
135 bcopy(&hdr[rest], sc->log_buff,
137 sc->log_idx = hdr_len - rest;
138 sc->log_buff[sc->log_idx] = 0;
141 rest = log_size - sc->log_idx - 1;
143 bcopy(tmp, &sc->log_buff[sc->log_idx], len);
145 sc->log_buff[sc->log_idx] = 0;
147 bcopy(tmp, &sc->log_buff[sc->log_idx], rest);
148 bcopy(&tmp[rest], sc->log_buff, len - rest);
149 sc->log_idx = len - rest;
150 sc->log_buff[sc->log_idx] = 0;
155 case NANDSIM_OUTPUT_FILE:
157 len = vsnprintf(tmp, NANDSIM_ENTRY_SIZE - 1, fmt, ap);
158 tmp[NANDSIM_ENTRY_SIZE - 1] = 0;
161 rest = NANDSIM_ENTRY_SIZE - str_index;
162 if (rest >= hdr_len) {
164 str_index += hdr_len;
166 strlcat(string, hdr, NANDSIM_ENTRY_SIZE + 1);
167 alq_write(sc->alq, (void *) string,
169 strcpy(string, &hdr[rest]);
170 str_index = hdr_len - rest;
172 rest = NANDSIM_ENTRY_SIZE - str_index;
177 strlcat(string, tmp, NANDSIM_ENTRY_SIZE + 1);
178 alq_write(sc->alq, (void *) string,
180 strcpy(string, &tmp[rest]);
181 str_index = len - rest;