2 * Copyright (C) 2003,2004
3 * Hidetoshi Shimokawa. All rights reserved.
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
46 #include <sys/consio.h>
48 #include <sys/malloc.h>
51 #include <sys/ucred.h>
53 #include <machine/bus.h>
55 #include <dev/dcons/dcons.h>
56 #include <dev/dcons/dcons_os.h>
59 #include <sys/reboot.h>
61 #include <sys/sysctl.h>
64 #include <vm/vm_param.h>
67 #include "opt_comconsole.h"
68 #include "opt_dcons.h"
75 #define DCONS_POLL_HZ 25
78 #ifndef DCONS_BUF_SIZE
79 #define DCONS_BUF_SIZE (16*1024)
82 #ifndef DCONS_FORCE_CONSOLE
83 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */
87 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */
91 static struct dcons_global dg;
92 struct dcons_global *dcons_conf;
93 static int poll_hz = DCONS_POLL_HZ;
95 static struct dcons_softc sc[DCONS_NPORT];
97 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
98 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
99 "dcons polling rate");
101 static int drv_init = 0;
102 static struct callout dcons_callout;
103 struct dcons_buf *dcons_buf; /* for local dconschat */
105 static void dcons_timeout(void *);
106 static int dcons_drv_init(int);
108 static cn_probe_t dcons_cnprobe;
109 static cn_init_t dcons_cninit;
110 static cn_term_t dcons_cnterm;
111 static cn_getc_t dcons_cngetc;
112 static cn_putc_t dcons_cnputc;
114 CONSOLE_DRIVER(dcons);
117 static gdb_probe_f dcons_dbg_probe;
118 static gdb_init_f dcons_dbg_init;
119 static gdb_term_f dcons_dbg_term;
120 static gdb_getc_f dcons_dbg_getc;
121 static gdb_putc_f dcons_dbg_putc;
123 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
124 dcons_dbg_getc, dcons_dbg_putc);
126 extern struct gdb_dbgport *gdb_cur;
129 static tsw_outwakeup_t dcons_outwakeup;
131 static struct ttydevsw dcons_ttydevsw = {
132 .tsw_flags = TF_NOPREFIX,
133 .tsw_outwakeup = dcons_outwakeup,
136 #if (defined(GDB) || defined(DDB)) && defined(ALT_BREAK_TO_DEBUGGER)
138 dcons_check_break(struct dcons_softc *dc, int c)
145 if ((kdb_brk = kdb_alt_break(c, &dc->brk_state)) != 0) {
147 case KDB_REQ_DEBUGGER:
148 if ((dc->flags & DC_GDB) != 0) {
150 if (gdb_cur == &dcons_gdb_dbgport) {
151 kdb_dbbe_select("gdb");
152 kdb_enter(KDB_WHY_BREAK,
153 "Break sequence on dcons gdb port");
157 kdb_enter(KDB_WHY_BREAK,
158 "Break sequence on dcons console port");
161 kdb_panic("Panic sequence on dcons console port");
171 #define dcons_check_break(dc, c) (c)
175 dcons_os_checkc_nopoll(struct dcons_softc *dc)
179 if (dg.dma_tag != NULL)
180 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
182 c = dcons_check_break(dc, dcons_checkc(dc));
184 if (dg.dma_tag != NULL)
185 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
191 dcons_os_checkc(struct dcons_softc *dc)
193 EVENTHANDLER_INVOKE(dcons_poll, 0);
194 return (dcons_os_checkc_nopoll(dc));
198 dcons_os_putc(struct dcons_softc *dc, int c)
200 if (dg.dma_tag != NULL)
201 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
205 if (dg.dma_tag != NULL)
206 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
210 dcons_outwakeup(struct tty *tp)
212 struct dcons_softc *dc;
217 while (ttydisc_getc(tp, &ch, sizeof ch) != 0)
218 dcons_os_putc(dc, ch);
222 dcons_timeout(void *v)
225 struct dcons_softc *dc;
228 for (i = 0; i < DCONS_NPORT; i ++) {
233 while ((c = dcons_os_checkc_nopoll(dc)) != -1)
234 ttydisc_rint(tp, c, 0);
235 ttydisc_rint_done(tp);
238 polltime = hz / poll_hz;
241 callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
245 dcons_cnprobe(struct consdev *cp)
247 sprintf(cp->cn_name, "dcons");
248 #if DCONS_FORCE_CONSOLE
249 cp->cn_pri = CN_REMOTE;
251 cp->cn_pri = CN_NORMAL;
256 dcons_cninit(struct consdev *cp)
259 cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
263 dcons_cnterm(struct consdev *cp)
268 dcons_cngetc(struct consdev *cp)
270 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
271 return (dcons_os_checkc(dc));
275 dcons_cnputc(struct consdev *cp, int c)
277 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
278 dcons_os_putc(dc, c);
282 dcons_drv_init(int stage)
284 #if defined(__i386__) || defined(__amd64__)
293 bzero(&dg, sizeof(dg));
295 dg.cdev = &dcons_consdev;
297 dg.size = DCONS_BUF_SIZE;
299 #if defined(__i386__) || defined(__amd64__)
300 if (getenv_quad("dcons.addr", &addr) > 0 &&
301 getenv_quad("dcons.size", &size) > 0) {
305 * Allow read/write access to dcons buffer.
307 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
308 *vtopte(KERNBASE + pa) |= PG_RW;
312 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
314 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
322 if (stage == 0) { /* XXX or cold */
324 * DCONS_FORCE_CONSOLE == 1 and statically linked.
325 * called from cninit(). can't use contigmalloc yet .
327 dg.buf = (struct dcons_buf *) bssbuf;
328 dcons_init(dg.buf, dg.size, sc);
333 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
334 * if the module is loaded after boot,
335 * bssbuf could be non-continuous.
337 dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
338 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
341 dcons_init(dg.buf, dg.size, sc);
354 dcons_attach_port(int port, char *name, int flags)
356 struct dcons_softc *dc;
360 tp = tty_alloc(&dcons_ttydevsw, dc);
363 tty_init_console(tp, 0);
364 tty_makedev(tp, NULL, "%s", name);
373 dcons_attach_port(DCONS_CON, "dcons", 0);
374 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
375 callout_init(&dcons_callout, CALLOUT_MPSAFE);
376 polltime = hz / poll_hz;
379 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
384 dcons_detach(int port)
387 struct dcons_softc *dc;
399 dcons_modevent(module_t mode, int type, void *data)
405 ret = dcons_drv_init(1);
409 dcons_cnprobe(&dcons_consdev);
410 dcons_cninit(&dcons_consdev);
411 cnadd(&dcons_consdev);
415 printf("dcons: unload\n");
417 callout_stop(&dcons_callout);
418 cnremove(&dcons_consdev);
419 dcons_detach(DCONS_CON);
420 dcons_detach(DCONS_GDB);
423 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
428 #if 0 /* Keep connection after halt */
440 /* Debugger interface */
443 dcons_os_getc(struct dcons_softc *dc)
447 while ((c = dcons_os_checkc(dc)) == -1);
453 dcons_dbg_probe(void)
457 if (getenv_int("dcons_gdb", &dcons_gdb) == 0)
473 dcons_dbg_putc(int c)
475 struct dcons_softc *dc = &sc[DCONS_GDB];
476 dcons_os_putc(dc, c);
482 struct dcons_softc *dc = &sc[DCONS_GDB];
483 return (dcons_os_getc(dc));
487 DEV_MODULE(dcons, dcons_modevent, NULL);
488 MODULE_VERSION(dcons, DCONS_VERSION);