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>
38 #if __FreeBSD_version >= 502122
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
48 #include <sys/consio.h>
50 #include <sys/malloc.h>
52 #include <sys/ucred.h>
54 #include <machine/bus.h>
60 #include <dev/dcons/dcons.h>
61 #include <dev/dcons/dcons_os.h>
65 #include <sys/reboot.h>
67 #include <sys/sysctl.h>
70 #include <vm/vm_param.h>
74 #include "opt_comconsole.h"
75 #include "opt_dcons.h"
78 #define DCONS_POLL_HZ 100
81 #ifndef DCONS_BUF_SIZE
82 #define DCONS_BUF_SIZE (16*1024)
85 #ifndef DCONS_FORCE_CONSOLE
86 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */
89 #ifndef DCONS_FORCE_GDB
90 #define DCONS_FORCE_GDB 1
93 #if __FreeBSD_version >= 500101
95 #if __FreeBSD_version < 502122
96 static struct consdev gdbconsdev;
100 static d_open_t dcons_open;
101 static d_close_t dcons_close;
102 #if defined(__DragonFly__) || __FreeBSD_version < 500104
103 static d_ioctl_t dcons_ioctl;
106 static struct cdevsw dcons_cdevsw = {
108 #define CDEV_MAJOR 184
109 "dcons", CDEV_MAJOR, D_TTY, NULL, 0,
110 dcons_open, dcons_close, ttyread, ttywrite, dcons_ioctl,
111 ttypoll, nommap, nostrategy, nodump, nopsize,
112 #elif __FreeBSD_version >= 500104
113 .d_version = D_VERSION,
114 .d_open = dcons_open,
115 .d_close = dcons_close,
117 .d_flags = D_TTY | D_NEEDGIANT,
119 #define CDEV_MAJOR 184
120 /* open */ dcons_open,
121 /* close */ dcons_close,
123 /* write */ ttywrite,
124 /* ioctl */ dcons_ioctl,
127 /* strategy */ nostrategy,
129 /* major */ CDEV_MAJOR,
137 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */
141 static struct dcons_global dg;
142 struct dcons_global *dcons_conf;
143 static int poll_hz = DCONS_POLL_HZ;
145 static struct dcons_softc sc[DCONS_NPORT];
147 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
148 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
149 "dcons polling rate");
151 static int drv_init = 0;
152 static struct callout dcons_callout;
153 struct dcons_buf *dcons_buf; /* for local dconschat */
157 #define THREAD d_thread_t
158 #elif __FreeBSD_version < 500000
160 #define THREAD struct proc
162 #define DEV struct cdev *
163 #define THREAD struct thread
167 static void dcons_tty_start(struct tty *);
168 static int dcons_tty_param(struct tty *, struct termios *);
169 static void dcons_timeout(void *);
170 static int dcons_drv_init(int);
172 static cn_probe_t dcons_cnprobe;
173 static cn_init_t dcons_cninit;
174 static cn_getc_t dcons_cngetc;
175 static cn_checkc_t dcons_cncheckc;
176 static cn_putc_t dcons_cnputc;
178 CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, NULL, dcons_cngetc,
179 dcons_cncheckc, dcons_cnputc, NULL);
181 #if __FreeBSD_version >= 502122
182 static gdb_probe_f dcons_dbg_probe;
183 static gdb_init_f dcons_dbg_init;
184 static gdb_term_f dcons_dbg_term;
185 static gdb_getc_f dcons_dbg_getc;
186 static gdb_checkc_f dcons_dbg_checkc;
187 static gdb_putc_f dcons_dbg_putc;
189 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
190 dcons_dbg_checkc, dcons_dbg_getc, dcons_dbg_putc);
192 extern struct gdb_dbgport *gdb_cur;
195 #if (KDB || DDB) && ALT_BREAK_TO_DEBUGGER
197 dcons_check_break(struct dcons_softc *dc, int c)
202 #if __FreeBSD_version >= 502122
203 if (kdb_alt_break(c, &dc->brk_state)) {
204 if ((dc->flags & DC_GDB) != 0) {
205 if (gdb_cur == &dcons_gdb_dbgport) {
206 kdb_dbbe_select("gdb");
213 switch (dc->brk_state) {
216 dc->brk_state = STATE2;
218 dc->brk_state = STATE0;
221 dc->brk_state = STATE0;
222 if (c == KEY_CTRLB) {
224 if (dc->flags & DC_GDB)
231 dc->brk_state = STATE1;
236 #define dcons_check_break(dc, c) (c)
240 dcons_os_checkc(struct dcons_softc *dc)
244 if (dg.dma_tag != NULL)
245 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
247 c = dcons_check_break(dc, dcons_checkc(dc));
249 if (dg.dma_tag != NULL)
250 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
256 dcons_os_getc(struct dcons_softc *dc)
260 while ((c = dcons_os_checkc(dc)) == -1);
266 dcons_os_putc(struct dcons_softc *dc, int c)
268 if (dg.dma_tag != NULL)
269 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
273 if (dg.dma_tag != NULL)
274 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
277 dcons_open(DEV dev, int flag, int mode, THREAD *td)
286 tp = dev->si_tty = ttymalloc(dev->si_tty);
287 tp->t_oproc = dcons_tty_start;
288 tp->t_param = dcons_tty_param;
289 tp->t_stop = nottystop;
295 if ((tp->t_state & TS_ISOPEN) == 0) {
296 tp->t_state |= TS_CARR_ON;
297 ttyconsolemode(tp, 0);
299 } else if ((tp->t_state & TS_XCLUDE) && suser(td)) {
305 #if __FreeBSD_version < 502113
306 error = (*linesw[tp->t_line].l_open)(dev, tp);
308 error = ttyld_open(tp, dev);
315 dcons_close(DEV dev, int flag, int mode, THREAD *td)
325 if (tp->t_state & TS_ISOPEN) {
326 #if __FreeBSD_version < 502113
327 (*linesw[tp->t_line].l_close)(tp, flag);
330 ttyld_close(tp, flag);
338 #if defined(__DragonFly__) || __FreeBSD_version < 500104
340 dcons_ioctl(DEV dev, u_long cmd, caddr_t data, int flag, THREAD *td)
351 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
352 if (error != ENOIOCTL)
355 error = ttioctl(tp, cmd, data, flag);
356 if (error != ENOIOCTL)
364 dcons_tty_param(struct tty *tp, struct termios *t)
366 tp->t_ispeed = t->c_ispeed;
367 tp->t_ospeed = t->c_ospeed;
368 tp->t_cflag = t->c_cflag;
373 dcons_tty_start(struct tty *tp)
375 struct dcons_softc *dc;
378 dc = (struct dcons_softc *)tp->t_dev->si_drv1;
380 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
385 tp->t_state |= TS_BUSY;
386 while (tp->t_outq.c_cc != 0)
387 dcons_os_putc(dc, getc(&tp->t_outq));
388 tp->t_state &= ~TS_BUSY;
395 dcons_timeout(void *v)
398 struct dcons_softc *dc;
401 for (i = 0; i < DCONS_NPORT; i ++) {
403 tp = ((DEV)dc->dev)->si_tty;
404 while ((c = dcons_os_checkc(dc)) != -1)
405 if (tp->t_state & TS_ISOPEN)
406 #if __FreeBSD_version < 502113
407 (*linesw[tp->t_line].l_rint)(c, tp);
412 polltime = hz / poll_hz;
415 callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
419 dcons_cnprobe(struct consdev *cp)
422 cp->cn_dev = make_dev(&dcons_cdevsw, DCONS_CON,
423 UID_ROOT, GID_WHEEL, 0600, "dcons");
424 #elif __FreeBSD_version >= 501109
425 sprintf(cp->cn_name, "dcons");
427 cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON);
429 #if DCONS_FORCE_CONSOLE
430 cp->cn_pri = CN_REMOTE;
432 cp->cn_pri = CN_NORMAL;
437 dcons_cninit(struct consdev *cp)
445 = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
450 dcons_cngetc(struct consdev *cp)
452 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
453 return (dcons_os_getc(dc));
456 dcons_cncheckc(struct consdev *cp)
458 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
459 return (dcons_os_checkc(dc));
462 dcons_cnputc(struct consdev *cp, int c)
464 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
465 dcons_os_putc(dc, c);
469 dcons_cngetc(DEV dev)
471 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
472 return (dcons_os_getc(dc));
475 dcons_cncheckc(DEV dev)
477 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
478 return (dcons_os_checkc(dc));
481 dcons_cnputc(DEV dev, int c)
483 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
484 dcons_os_putc(dc, c);
489 dcons_drv_init(int stage)
491 #if defined(__i386__) || defined(__amd64__)
500 bzero(&dg, sizeof(dg));
502 dg.cdev = &dcons_consdev;
504 dg.size = DCONS_BUF_SIZE;
506 #if defined(__i386__) || defined(__amd64__)
507 if (getenv_quad("dcons.addr", &addr) > 0 &&
508 getenv_quad("dcons.size", &size) > 0) {
512 * Allow read/write access to dcons buffer.
514 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
515 *vtopte(KERNBASE + pa) |= PG_RW;
519 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
521 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
529 if (stage == 0) { /* XXX or cold */
531 * DCONS_FORCE_CONSOLE == 1 and statically linked.
532 * called from cninit(). can't use contigmalloc yet .
534 dg.buf = (struct dcons_buf *) bssbuf;
535 dcons_init(dg.buf, dg.size, sc);
540 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
541 * if the module is loaded after boot,
542 * bssbuf could be non-continuous.
544 dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
545 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
546 dcons_init(dg.buf, dg.size, sc);
552 #if __FreeBSD_version < 502122
553 #if DDB && DCONS_FORCE_GDB
555 gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB];
556 #if __FreeBSD_version >= 501109
557 sprintf(gdbconsdev.cn_name, "dgdb");
559 gdb_arg = &gdbconsdev;
560 #elif defined(__DragonFly__)
561 gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB,
562 UID_ROOT, GID_WHEEL, 0600, "dgdb");
564 gdbdev = makedev(CDEV_MAJOR, DCONS_GDB);
566 gdb_getc = dcons_cngetc;
567 gdb_putc = dcons_cnputc;
577 dcons_attach_port(int port, char *name, int flags)
579 struct dcons_softc *dc;
585 dev = make_dev(&dcons_cdevsw, port,
586 UID_ROOT, GID_WHEEL, 0600, name);
587 dc->dev = (void *)dev;
588 tp = ttymalloc(NULL);
590 dev->si_drv1 = (void *)dc;
593 tp->t_oproc = dcons_tty_start;
594 tp->t_param = dcons_tty_param;
595 tp->t_stop = nottystop;
607 cdevsw_add(&dcons_cdevsw, -1, 0);
609 dcons_attach_port(DCONS_CON, "dcons", 0);
610 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
611 #if __FreeBSD_version < 500000
612 callout_init(&dcons_callout);
614 callout_init(&dcons_callout, 0);
616 polltime = hz / poll_hz;
619 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
624 dcons_detach(int port)
627 struct dcons_softc *dc;
631 tp = ((DEV)dc->dev)->si_tty;
633 if (tp->t_state & TS_ISOPEN) {
634 printf("dcons: still opened\n");
635 #if __FreeBSD_version < 502113
636 (*linesw[tp->t_line].l_close)(tp, 0);
647 * must wait until all device are closed.
650 tsleep((void *)dc, 0, "dcodtc", hz/4);
652 tsleep((void *)dc, PWAIT, "dcodtc", hz/4);
654 destroy_dev(dc->dev);
660 /* cnXXX works only for FreeBSD-5 */
662 dcons_modevent(module_t mode, int type, void *data)
668 ret = dcons_drv_init(1);
670 #if __FreeBSD_version >= 500000
672 dcons_cnprobe(&dcons_consdev);
673 dcons_cninit(&dcons_consdev);
674 cnadd(&dcons_consdev);
679 printf("dcons: unload\n");
680 callout_stop(&dcons_callout);
681 #if __FreeBSD_version < 502122
682 #if DDB && DCONS_FORCE_GDB
690 #if __FreeBSD_version >= 500000
691 cnremove(&dcons_consdev);
693 dcons_detach(DCONS_CON);
694 dcons_detach(DCONS_GDB);
697 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
710 #if __FreeBSD_version >= 502122
711 /* Debugger interface */
714 dcons_dbg_probe(void)
716 return(DCONS_FORCE_GDB);
730 dcons_dbg_putc(int c)
732 struct dcons_softc *dc = &sc[DCONS_GDB];
733 dcons_os_putc(dc, c);
737 dcons_dbg_checkc(void)
739 struct dcons_softc *dc = &sc[DCONS_GDB];
740 return (dcons_os_checkc(dc));
746 struct dcons_softc *dc = &sc[DCONS_GDB];
747 return (dcons_os_getc(dc));
751 DEV_MODULE(dcons, dcons_modevent, NULL);
752 MODULE_VERSION(dcons, DCONS_VERSION);