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.6 2006/09/10 01:26:34 dillon 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>
53 #include <sys/thread2.h>
54 #include <sys/ucred.h>
56 #include <machine/bus.h>
62 #include <dev/dcons/dcons.h>
63 #include <dev/dcons/dcons_os.h>
67 #include <sys/reboot.h>
69 #include <sys/sysctl.h>
72 #include <vm/vm_param.h>
76 #include "opt_comconsole.h"
77 #include "opt_dcons.h"
80 #define DCONS_POLL_HZ 100
83 #ifndef DCONS_BUF_SIZE
84 #define DCONS_BUF_SIZE (16*1024)
87 #ifndef DCONS_FORCE_CONSOLE
88 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */
91 #ifndef DCONS_FORCE_GDB
92 #define DCONS_FORCE_GDB 1
95 #if __FreeBSD_version >= 500101
97 #if __FreeBSD_version < 502122
98 static struct consdev gdbconsdev;
102 #define CDEV_MAJOR 184
104 static d_open_t dcons_open;
105 static d_close_t dcons_close;
106 static d_ioctl_t dcons_ioctl;
108 static struct dev_ops dcons_ops = {
109 { "dcons", CDEV_MAJOR, D_TTY },
110 .d_open = dcons_open,
111 .d_close = dcons_close,
114 .d_ioctl = dcons_ioctl,
119 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */
123 static struct dcons_global dg;
124 struct dcons_global *dcons_conf;
125 static int poll_hz = DCONS_POLL_HZ;
127 static struct dcons_softc sc[DCONS_NPORT];
129 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
130 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
131 "dcons polling rate");
133 static int drv_init = 0;
134 static struct callout dcons_callout;
135 struct dcons_buf *dcons_buf; /* for local dconschat */
139 #define THREAD d_thread_t
140 #elif __FreeBSD_version < 500000
142 #define THREAD struct proc
144 #define DEV struct cdev *
145 #define THREAD struct thread
149 static void dcons_tty_start(struct tty *);
150 static int dcons_tty_param(struct tty *, struct termios *);
151 static void dcons_timeout(void *);
152 static int dcons_drv_init(int);
154 static cn_probe_t dcons_cnprobe;
155 static cn_init_t dcons_cninit;
156 static cn_getc_t dcons_cngetc;
157 static cn_checkc_t dcons_cncheckc;
158 static cn_putc_t dcons_cnputc;
160 CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, NULL, dcons_cngetc,
161 dcons_cncheckc, dcons_cnputc, NULL);
163 #if __FreeBSD_version >= 502122
164 static gdb_probe_f dcons_dbg_probe;
165 static gdb_init_f dcons_dbg_init;
166 static gdb_term_f dcons_dbg_term;
167 static gdb_getc_f dcons_dbg_getc;
168 static gdb_checkc_f dcons_dbg_checkc;
169 static gdb_putc_f dcons_dbg_putc;
171 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
172 dcons_dbg_checkc, dcons_dbg_getc, dcons_dbg_putc);
174 extern struct gdb_dbgport *gdb_cur;
177 #if (KDB || DDB) && ALT_BREAK_TO_DEBUGGER
179 dcons_check_break(struct dcons_softc *dc, int c)
184 #if __FreeBSD_version >= 502122
185 if (kdb_alt_break(c, &dc->brk_state)) {
186 if ((dc->flags & DC_GDB) != 0) {
187 if (gdb_cur == &dcons_gdb_dbgport) {
188 kdb_dbbe_select("gdb");
195 switch (dc->brk_state) {
198 dc->brk_state = STATE2;
200 dc->brk_state = STATE0;
203 dc->brk_state = STATE0;
204 if (c == KEY_CTRLB) {
206 if (dc->flags & DC_GDB)
213 dc->brk_state = STATE1;
218 #define dcons_check_break(dc, c) (c)
222 dcons_os_checkc(struct dcons_softc *dc)
226 if (dg.dma_tag != NULL)
227 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
229 c = dcons_check_break(dc, dcons_checkc(dc));
231 if (dg.dma_tag != NULL)
232 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
238 dcons_os_getc(struct dcons_softc *dc)
242 while ((c = dcons_os_checkc(dc)) == -1);
248 dcons_os_putc(struct dcons_softc *dc, int c)
250 if (dg.dma_tag != NULL)
251 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
255 if (dg.dma_tag != NULL)
256 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
259 dcons_open(struct dev_open_args *ap)
261 cdev_t dev = ap->a_head.a_dev;
269 tp = dev->si_tty = ttymalloc(dev->si_tty);
270 tp->t_oproc = dcons_tty_start;
271 tp->t_param = dcons_tty_param;
272 tp->t_stop = nottystop;
278 if ((tp->t_state & TS_ISOPEN) == 0) {
279 tp->t_state |= TS_CARR_ON;
281 tp->t_iflag = TTYDEF_IFLAG;
282 tp->t_oflag = TTYDEF_OFLAG;
283 tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
284 tp->t_lflag = TTYDEF_LFLAG;
285 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
287 } else if ((tp->t_state & TS_XCLUDE) && suser_cred(ap->a_cred, 0)) {
293 #if __FreeBSD_version < 502113
294 error = (*linesw[tp->t_line].l_open)(dev, tp);
296 error = ttyld_open(tp, dev);
303 dcons_close(struct dev_close_args *ap)
305 cdev_t dev = ap->a_head.a_dev;
314 if (tp->t_state & TS_ISOPEN) {
315 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
323 dcons_ioctl(struct dev_ioctl_args *ap)
325 cdev_t dev = ap->a_head.a_dev;
335 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
336 if (error != ENOIOCTL)
339 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
340 if (error != ENOIOCTL)
347 dcons_tty_param(struct tty *tp, struct termios *t)
349 tp->t_ispeed = t->c_ispeed;
350 tp->t_ospeed = t->c_ospeed;
351 tp->t_cflag = t->c_cflag;
356 dcons_tty_start(struct tty *tp)
358 struct dcons_softc *dc;
360 dc = (struct dcons_softc *)tp->t_dev->si_drv1;
362 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
367 tp->t_state |= TS_BUSY;
368 while (tp->t_outq.c_cc != 0)
369 dcons_os_putc(dc, clist_getc(&tp->t_outq));
370 tp->t_state &= ~TS_BUSY;
377 dcons_timeout(void *v)
380 struct dcons_softc *dc;
383 for (i = 0; i < DCONS_NPORT; i ++) {
385 tp = ((DEV)dc->dev)->si_tty;
386 while ((c = dcons_os_checkc(dc)) != -1)
387 if (tp->t_state & TS_ISOPEN)
388 #if __FreeBSD_version < 502113
389 (*linesw[tp->t_line].l_rint)(c, tp);
394 polltime = hz / poll_hz;
397 callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
401 dcons_cnprobe(struct consdev *cp)
404 cp->cn_dev = make_dev(&dcons_ops, DCONS_CON,
405 UID_ROOT, GID_WHEEL, 0600, "dcons");
406 #elif __FreeBSD_version >= 501109
407 sprintf(cp->cn_name, "dcons");
409 cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON);
411 #if DCONS_FORCE_CONSOLE
412 cp->cn_pri = CN_REMOTE;
414 cp->cn_pri = CN_NORMAL;
419 dcons_cninit(struct consdev *cp)
427 = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
432 dcons_cngetc(struct consdev *cp)
434 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
435 return (dcons_os_getc(dc));
438 dcons_cncheckc(struct consdev *cp)
440 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
441 return (dcons_os_checkc(dc));
444 dcons_cnputc(struct consdev *cp, int c)
446 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
447 dcons_os_putc(dc, c);
451 dcons_cngetc(DEV dev)
453 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
454 return (dcons_os_getc(dc));
457 dcons_cncheckc(DEV dev)
459 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
460 return (dcons_os_checkc(dc));
463 dcons_cnputc(DEV dev, int c)
465 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
466 dcons_os_putc(dc, c);
471 dcons_drv_init(int stage)
482 bzero(&dg, sizeof(dg));
484 dg.cdev = &dcons_consdev;
486 dg.size = DCONS_BUF_SIZE;
489 if (kgetenv_quad("dcons.addr", &addr) > 0 &&
490 kgetenv_quad("dcons.size", &size) > 0) {
493 * Allow read/write access to dcons buffer.
495 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
496 *vtopte(KERNBASE + pa) |= PG_RW;
499 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
501 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
509 if (stage == 0) { /* XXX or cold */
511 * DCONS_FORCE_CONSOLE == 1 and statically linked.
512 * called from cninit(). can't use contigmalloc yet .
514 dg.buf = (struct dcons_buf *) bssbuf;
515 dcons_init(dg.buf, dg.size, sc);
520 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
521 * if the module is loaded after boot,
522 * bssbuf could be non-continuous.
524 dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
525 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
526 dcons_init(dg.buf, dg.size, sc);
532 #if __FreeBSD_version < 502122
533 #if DDB && DCONS_FORCE_GDB
535 gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB];
536 #if __FreeBSD_version >= 501109
537 sprintf(gdbconsdev.cn_name, "dgdb");
539 gdb_arg = &gdbconsdev;
540 #elif defined(__DragonFly__)
541 gdbdev = make_dev(&dcons_ops, DCONS_GDB,
542 UID_ROOT, GID_WHEEL, 0600, "dgdb");
544 gdbdev = makedev(CDEV_MAJOR, DCONS_GDB);
546 gdb_getc = dcons_cngetc;
547 gdb_putc = dcons_cnputc;
557 dcons_attach_port(int port, char *name, int flags)
559 struct dcons_softc *dc;
565 dev = make_dev(&dcons_ops, port,
566 UID_ROOT, GID_WHEEL, 0600, name);
567 dc->dev = (void *)dev;
568 tp = ttymalloc(NULL);
570 dev->si_drv1 = (void *)dc;
573 tp->t_oproc = dcons_tty_start;
574 tp->t_param = dcons_tty_param;
575 tp->t_stop = nottystop;
587 dev_ops_add(&dcons_ops, -1, 0);
589 dcons_attach_port(DCONS_CON, "dcons", 0);
590 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
591 #if __FreeBSD_version < 500000
592 callout_init(&dcons_callout);
594 callout_init(&dcons_callout, 0);
596 polltime = hz / poll_hz;
599 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
604 dcons_detach(int port)
607 struct dcons_softc *dc;
611 tp = ((DEV)dc->dev)->si_tty;
613 if (tp->t_state & TS_ISOPEN) {
614 printf("dcons: still opened\n");
615 #if __FreeBSD_version < 502113
616 (*linesw[tp->t_line].l_close)(tp, 0);
627 * must wait until all device are closed.
630 tsleep((void *)dc, 0, "dcodtc", hz/4);
632 tsleep((void *)dc, PWAIT, "dcodtc", hz/4);
634 destroy_dev(dc->dev);
640 /* cnXXX works only for FreeBSD-5 */
642 dcons_modevent(module_t mode, int type, void *data)
648 ret = dcons_drv_init(1);
650 #if __FreeBSD_version >= 500000
652 dcons_cnprobe(&dcons_consdev);
653 dcons_cninit(&dcons_consdev);
654 cnadd(&dcons_consdev);
659 printf("dcons: unload\n");
660 callout_stop(&dcons_callout);
661 #if __FreeBSD_version < 502122
662 #if DDB && DCONS_FORCE_GDB
670 #if __FreeBSD_version >= 500000
671 cnremove(&dcons_consdev);
673 dcons_detach(DCONS_CON);
674 dcons_detach(DCONS_GDB);
677 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
690 #if __FreeBSD_version >= 502122
691 /* Debugger interface */
694 dcons_dbg_probe(void)
696 return(DCONS_FORCE_GDB);
710 dcons_dbg_putc(int c)
712 struct dcons_softc *dc = &sc[DCONS_GDB];
713 dcons_os_putc(dc, c);
717 dcons_dbg_checkc(void)
719 struct dcons_softc *dc = &sc[DCONS_GDB];
720 return (dcons_os_checkc(dc));
726 struct dcons_softc *dc = &sc[DCONS_GDB];
727 return (dcons_os_getc(dc));
731 DEV_MODULE(dcons, dcons_modevent, NULL);
732 MODULE_VERSION(dcons, DCONS_VERSION);