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
34 * $FreeBSD: src/sys/dev/dcons/dcons_os.c,v 1.4 2004/10/24 12:41:04 simokawa Exp $
35 * $DragonFly: src/sys/dev/misc/dcons/dcons_os.c,v 1.13 2007/08/07 13:14:11 hasso Exp $
38 #include <sys/param.h>
39 #if __FreeBSD_version >= 502122
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
46 #include <sys/types.h>
49 #include <sys/consio.h>
51 #include <sys/malloc.h>
54 #include <sys/thread2.h>
55 #include <sys/ucred.h>
62 #include <sys/reboot.h>
64 #include <sys/sysctl.h>
67 #include <vm/vm_param.h>
71 #include "opt_comconsole.h"
72 #include "opt_dcons.h"
75 #define DCONS_POLL_HZ 100
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 */
86 #ifndef DCONS_FORCE_GDB
87 #define DCONS_FORCE_GDB 1
90 #define CDEV_MAJOR 184
92 static d_open_t dcons_open;
93 static d_close_t dcons_close;
94 static d_ioctl_t dcons_ioctl;
96 static struct dev_ops dcons_ops = {
97 { "dcons", CDEV_MAJOR, D_TTY | D_KQFILTER },
99 .d_close = dcons_close,
102 .d_ioctl = dcons_ioctl,
104 .d_kqfilter = ttykqfilter,
105 .d_revoke = ttyrevoke
109 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */
113 static struct dcons_global dg;
114 struct dcons_global *dcons_conf;
115 static int poll_hz = DCONS_POLL_HZ;
117 static struct dcons_softc sc[DCONS_NPORT];
119 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
120 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
121 "dcons polling rate");
123 static int drv_init = 0;
124 static struct callout dcons_callout;
125 struct dcons_buf *dcons_buf; /* for local dconschat */
129 #define THREAD d_thread_t
130 #elif __FreeBSD_version < 500000
132 #define THREAD struct proc
134 #define DEV struct cdev *
135 #define THREAD struct thread
139 static void dcons_tty_start(struct tty *);
140 static int dcons_tty_param(struct tty *, struct termios *);
141 static void dcons_timeout(void *);
142 static int dcons_drv_init(int);
144 static cn_probe_t dcons_cnprobe;
145 static cn_init_t dcons_cninit;
146 static cn_init_fini_t dcons_cninit_fini;
147 static cn_getc_t dcons_cngetc;
148 static cn_checkc_t dcons_cncheckc;
149 static cn_putc_t dcons_cnputc;
151 CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, dcons_cninit_fini,
152 NULL, dcons_cngetc, dcons_cncheckc, dcons_cnputc, NULL);
154 #if __FreeBSD_version >= 502122
155 static gdb_probe_f dcons_dbg_probe;
156 static gdb_init_f dcons_dbg_init;
157 static gdb_term_f dcons_dbg_term;
158 static gdb_getc_f dcons_dbg_getc;
159 static gdb_checkc_f dcons_dbg_checkc;
160 static gdb_putc_f dcons_dbg_putc;
162 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
163 dcons_dbg_checkc, dcons_dbg_getc, dcons_dbg_putc);
165 extern struct gdb_dbgport *gdb_cur;
168 #if (KDB || DDB) && ALT_BREAK_TO_DEBUGGER
170 dcons_check_break(struct dcons_softc *dc, int c)
175 #if __FreeBSD_version >= 502122
176 if (kdb_alt_break(c, &dc->brk_state)) {
177 if ((dc->flags & DC_GDB) != 0) {
178 if (gdb_cur == &dcons_gdb_dbgport) {
179 kdb_dbbe_select("gdb");
186 switch (dc->brk_state) {
189 dc->brk_state = STATE2;
191 dc->brk_state = STATE0;
194 dc->brk_state = STATE0;
195 if (c == KEY_CTRLB) {
197 if (dc->flags & DC_GDB)
204 dc->brk_state = STATE1;
209 #define dcons_check_break(dc, c) (c)
213 dcons_os_checkc(struct dcons_softc *dc)
217 if (dg.dma_tag != NULL)
218 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
220 c = dcons_check_break(dc, dcons_checkc(dc));
222 if (dg.dma_tag != NULL)
223 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
229 dcons_os_getc(struct dcons_softc *dc)
233 while ((c = dcons_os_checkc(dc)) == -1);
239 dcons_os_putc(struct dcons_softc *dc, int c)
241 if (dg.dma_tag != NULL)
242 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
246 if (dg.dma_tag != NULL)
247 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
250 dcons_open(struct dev_open_args *ap)
252 cdev_t dev = ap->a_head.a_dev;
260 tp = dev->si_tty = ttymalloc(dev->si_tty);
261 tp->t_oproc = dcons_tty_start;
262 tp->t_param = dcons_tty_param;
263 tp->t_stop = nottystop;
269 if ((tp->t_state & TS_ISOPEN) == 0) {
270 tp->t_state |= TS_CARR_ON;
272 tp->t_iflag = TTYDEF_IFLAG;
273 tp->t_oflag = TTYDEF_OFLAG;
274 tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
275 tp->t_lflag = TTYDEF_LFLAG;
276 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
278 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
284 #if __FreeBSD_version < 502113
285 error = (*linesw[tp->t_line].l_open)(dev, tp);
287 error = ttyld_open(tp, dev);
294 dcons_close(struct dev_close_args *ap)
296 cdev_t dev = ap->a_head.a_dev;
305 if (tp->t_state & TS_ISOPEN) {
306 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
314 dcons_ioctl(struct dev_ioctl_args *ap)
316 cdev_t dev = ap->a_head.a_dev;
326 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
327 if (error != ENOIOCTL)
330 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
331 if (error != ENOIOCTL)
338 dcons_tty_param(struct tty *tp, struct termios *t)
340 tp->t_ispeed = t->c_ispeed;
341 tp->t_ospeed = t->c_ospeed;
342 tp->t_cflag = t->c_cflag;
347 dcons_tty_start(struct tty *tp)
349 struct dcons_softc *dc;
351 dc = (struct dcons_softc *)tp->t_dev->si_drv1;
353 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
358 tp->t_state |= TS_BUSY;
359 while (tp->t_outq.c_cc != 0)
360 dcons_os_putc(dc, clist_getc(&tp->t_outq));
361 tp->t_state &= ~TS_BUSY;
368 dcons_timeout(void *v)
371 struct dcons_softc *dc;
374 for (i = 0; i < DCONS_NPORT; i ++) {
376 tp = ((DEV)dc->dev)->si_tty;
377 while ((c = dcons_os_checkc(dc)) != -1)
378 if (tp->t_state & TS_ISOPEN)
379 #if __FreeBSD_version < 502113
380 (*linesw[tp->t_line].l_rint)(c, tp);
385 polltime = hz / poll_hz;
388 callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
392 dcons_cnprobe(struct consdev *cp)
395 cp->cn_probegood = 1;
396 #elif __FreeBSD_version >= 501109
397 ksprintf(cp->cn_name, "dcons");
399 cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON);
401 #if DCONS_FORCE_CONSOLE
402 cp->cn_pri = CN_REMOTE;
404 cp->cn_pri = CN_NORMAL;
409 dcons_cninit(struct consdev *cp)
417 = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
421 dcons_cninit_fini(struct consdev *cp)
423 cp->cn_dev = make_dev(&dcons_ops, DCONS_CON,
424 UID_ROOT, GID_WHEEL, 0600, "dcons");
429 dcons_cngetc(struct consdev *cp)
431 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
432 return (dcons_os_getc(dc));
435 dcons_cncheckc(struct consdev *cp)
437 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
438 return (dcons_os_checkc(dc));
441 dcons_cnputc(struct consdev *cp, int c)
443 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
444 dcons_os_putc(dc, c);
448 dcons_cngetc(void *private)
450 struct dcons_softc *dc = (struct dcons_softc *)private;
451 return (dcons_os_getc(dc));
454 dcons_cncheckc(void *private)
456 struct dcons_softc *dc = (struct dcons_softc *)private;
457 return (dcons_os_checkc(dc));
460 dcons_cnputc(void *private, int c)
462 struct dcons_softc *dc = (struct dcons_softc *)private;
463 dcons_os_putc(dc, c);
468 dcons_drv_init(int stage)
479 bzero(&dg, sizeof(dg));
481 dg.cdev = &dcons_consdev;
483 dg.size = DCONS_BUF_SIZE;
486 if (kgetenv_quad("dcons.addr", &addr) > 0 &&
487 kgetenv_quad("dcons.size", &size) > 0) {
490 * Allow read/write access to dcons buffer.
492 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
493 pmap_kmodify_rw((vm_offset_t)(KERNBASE + pa));
496 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
498 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
506 if (stage == 0) { /* XXX or cold */
508 * DCONS_FORCE_CONSOLE == 1 and statically linked.
509 * called from cninit(). can't use contigmalloc yet .
511 dg.buf = (struct dcons_buf *) bssbuf;
512 dcons_init(dg.buf, dg.size, sc);
517 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
518 * if the module is loaded after boot,
519 * bssbuf could be non-continuous.
521 dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
522 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
523 dcons_init(dg.buf, dg.size, sc);
529 #if DDB && DCONS_FORCE_GDB
530 if (gdb_tab == NULL) {
531 gdb_tab = &dcons_consdev;
532 dcons_consdev.cn_gdbprivate = &sc[DCONS_GDB];
542 dcons_attach_port(int port, char *name, int flags)
544 struct dcons_softc *dc;
550 dev = make_dev(&dcons_ops, port,
551 UID_ROOT, GID_WHEEL, 0600, name);
552 dc->dev = (void *)dev;
553 tp = ttymalloc(NULL);
555 dev->si_drv1 = (void *)dc;
558 tp->t_oproc = dcons_tty_start;
559 tp->t_param = dcons_tty_param;
560 tp->t_stop = nottystop;
571 dcons_attach_port(DCONS_CON, "dcons", 0);
572 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
573 #if __FreeBSD_version < 500000
574 callout_init(&dcons_callout);
576 callout_init(&dcons_callout, 0);
578 polltime = hz / poll_hz;
581 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
586 dcons_detach(int port)
589 struct dcons_softc *dc;
593 tp = ((DEV)dc->dev)->si_tty;
595 if (tp->t_state & TS_ISOPEN) {
596 kprintf("dcons: still opened\n");
597 #if __FreeBSD_version < 502113
598 (*linesw[tp->t_line].l_close)(tp, 0);
606 * must wait until all device are closed.
609 tsleep((void *)dc, 0, "dcodtc", hz/4);
611 tsleep((void *)dc, PWAIT, "dcodtc", hz/4);
613 destroy_dev(dc->dev);
619 /* cnXXX works only for FreeBSD-5 */
621 dcons_modevent(module_t mode, int type, void *data)
627 ret = dcons_drv_init(1);
629 #if __FreeBSD_version >= 500000
631 dcons_cnprobe(&dcons_consdev);
632 dcons_cninit(&dcons_consdev);
633 cnadd(&dcons_consdev);
638 kprintf("dcons: unload\n");
639 callout_stop(&dcons_callout);
640 #if __FreeBSD_version < 502122
641 #if DDB && DCONS_FORCE_GDB
645 if (gdb_tab == &dcons_consdev)
650 #if __FreeBSD_version >= 500000
651 cnremove(&dcons_consdev);
653 dcons_detach(DCONS_CON);
654 dcons_detach(DCONS_GDB);
657 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
670 #if __FreeBSD_version >= 502122
671 /* Debugger interface */
674 dcons_dbg_probe(void)
676 return(DCONS_FORCE_GDB);
690 dcons_dbg_putc(int c)
692 struct dcons_softc *dc = &sc[DCONS_GDB];
693 dcons_os_putc(dc, c);
697 dcons_dbg_checkc(void)
699 struct dcons_softc *dc = &sc[DCONS_GDB];
700 return (dcons_os_checkc(dc));
706 struct dcons_softc *dc = &sc[DCONS_GDB];
707 return (dcons_os_getc(dc));
711 DEV_MODULE(dcons, dcons_modevent, NULL);
712 MODULE_VERSION(dcons, DCONS_VERSION);