dcons_os.c revision 178766
1139749Simp/*- 2136467Ssimokawa * Copyright (C) 2003,2004 3136467Ssimokawa * Hidetoshi Shimokawa. All rights reserved. 4136467Ssimokawa * 5136467Ssimokawa * Redistribution and use in source and binary forms, with or without 6136467Ssimokawa * modification, are permitted provided that the following conditions 7136467Ssimokawa * are met: 8136467Ssimokawa * 1. Redistributions of source code must retain the above copyright 9136467Ssimokawa * notice, this list of conditions and the following disclaimer. 10136467Ssimokawa * 2. Redistributions in binary form must reproduce the above copyright 11136467Ssimokawa * notice, this list of conditions and the following disclaimer in the 12136467Ssimokawa * documentation and/or other materials provided with the distribution. 13136467Ssimokawa * 3. All advertising materials mentioning features or use of this software 14136467Ssimokawa * must display the following acknowledgement: 15136467Ssimokawa * 16136467Ssimokawa * This product includes software developed by Hidetoshi Shimokawa. 17136467Ssimokawa * 18136467Ssimokawa * 4. Neither the name of the author nor the names of its contributors 19136467Ssimokawa * may be used to endorse or promote products derived from this software 20136467Ssimokawa * without specific prior written permission. 21136467Ssimokawa * 22136467Ssimokawa * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23136467Ssimokawa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24136467Ssimokawa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25136467Ssimokawa * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26136467Ssimokawa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27136467Ssimokawa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28136467Ssimokawa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29136467Ssimokawa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30136467Ssimokawa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31136467Ssimokawa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32136467Ssimokawa * SUCH DAMAGE. 33136467Ssimokawa * 34136467Ssimokawa * $FreeBSD: head/sys/dev/dcons/dcons_os.c 178766 2008-05-04 23:29:38Z peter $ 35136467Ssimokawa */ 36136467Ssimokawa 37136467Ssimokawa#include <sys/param.h> 38136467Ssimokawa#if __FreeBSD_version >= 502122 39136467Ssimokawa#include <sys/kdb.h> 40136467Ssimokawa#include <gdb/gdb.h> 41136467Ssimokawa#endif 42136467Ssimokawa#include <sys/kernel.h> 43136467Ssimokawa#include <sys/module.h> 44136467Ssimokawa#include <sys/systm.h> 45136467Ssimokawa#include <sys/types.h> 46136467Ssimokawa#include <sys/conf.h> 47136467Ssimokawa#include <sys/cons.h> 48136467Ssimokawa#include <sys/consio.h> 49136467Ssimokawa#include <sys/tty.h> 50136467Ssimokawa#include <sys/malloc.h> 51164033Srwatson#include <sys/priv.h> 52136467Ssimokawa#include <sys/proc.h> 53136467Ssimokawa#include <sys/ucred.h> 54136467Ssimokawa 55136467Ssimokawa#include <machine/bus.h> 56136467Ssimokawa 57136467Ssimokawa#ifdef __DragonFly__ 58136467Ssimokawa#include "dcons.h" 59136467Ssimokawa#include "dcons_os.h" 60136467Ssimokawa#else 61136467Ssimokawa#include <dev/dcons/dcons.h> 62136467Ssimokawa#include <dev/dcons/dcons_os.h> 63136467Ssimokawa#endif 64136467Ssimokawa 65136467Ssimokawa#include <ddb/ddb.h> 66136467Ssimokawa#include <sys/reboot.h> 67136467Ssimokawa 68136467Ssimokawa#include <sys/sysctl.h> 69136467Ssimokawa 70136467Ssimokawa#include <vm/vm.h> 71136467Ssimokawa#include <vm/vm_param.h> 72136467Ssimokawa#include <vm/pmap.h> 73136467Ssimokawa 74136467Ssimokawa#include "opt_comconsole.h" 75136467Ssimokawa#include "opt_dcons.h" 76170017Ssimokawa#include "opt_kdb.h" 77170017Ssimokawa#include "opt_gdb.h" 78170017Ssimokawa#include "opt_ddb.h" 79136467Ssimokawa 80170017Ssimokawa 81136467Ssimokawa#ifndef DCONS_POLL_HZ 82136467Ssimokawa#define DCONS_POLL_HZ 100 83136467Ssimokawa#endif 84136467Ssimokawa 85136467Ssimokawa#ifndef DCONS_BUF_SIZE 86136467Ssimokawa#define DCONS_BUF_SIZE (16*1024) 87136467Ssimokawa#endif 88136467Ssimokawa 89136467Ssimokawa#ifndef DCONS_FORCE_CONSOLE 90136467Ssimokawa#define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 91136467Ssimokawa#endif 92136467Ssimokawa 93136467Ssimokawa#ifndef DCONS_FORCE_GDB 94136467Ssimokawa#define DCONS_FORCE_GDB 1 95136467Ssimokawa#endif 96136467Ssimokawa 97136467Ssimokawa#if __FreeBSD_version >= 500101 98136467Ssimokawa#define CONS_NODEV 1 99136467Ssimokawa#if __FreeBSD_version < 502122 100136467Ssimokawastatic struct consdev gdbconsdev; 101136467Ssimokawa#endif 102136467Ssimokawa#endif 103136467Ssimokawa 104136467Ssimokawastatic d_open_t dcons_open; 105136467Ssimokawastatic d_close_t dcons_close; 106136467Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500104 107136467Ssimokawastatic d_ioctl_t dcons_ioctl; 108136467Ssimokawa#endif 109136467Ssimokawa 110136467Ssimokawastatic struct cdevsw dcons_cdevsw = { 111136467Ssimokawa#ifdef __DragonFly__ 112136467Ssimokawa#define CDEV_MAJOR 184 113136467Ssimokawa "dcons", CDEV_MAJOR, D_TTY, NULL, 0, 114136467Ssimokawa dcons_open, dcons_close, ttyread, ttywrite, dcons_ioctl, 115136467Ssimokawa ttypoll, nommap, nostrategy, nodump, nopsize, 116136467Ssimokawa#elif __FreeBSD_version >= 500104 117136467Ssimokawa .d_version = D_VERSION, 118136467Ssimokawa .d_open = dcons_open, 119136467Ssimokawa .d_close = dcons_close, 120136467Ssimokawa .d_name = "dcons", 121136467Ssimokawa .d_flags = D_TTY | D_NEEDGIANT, 122136467Ssimokawa#else 123136467Ssimokawa#define CDEV_MAJOR 184 124136467Ssimokawa /* open */ dcons_open, 125136467Ssimokawa /* close */ dcons_close, 126136467Ssimokawa /* read */ ttyread, 127136467Ssimokawa /* write */ ttywrite, 128136467Ssimokawa /* ioctl */ dcons_ioctl, 129136467Ssimokawa /* poll */ ttypoll, 130136467Ssimokawa /* mmap */ nommap, 131136467Ssimokawa /* strategy */ nostrategy, 132136467Ssimokawa /* name */ "dcons", 133136467Ssimokawa /* major */ CDEV_MAJOR, 134136467Ssimokawa /* dump */ nodump, 135136467Ssimokawa /* psize */ nopsize, 136136467Ssimokawa /* flags */ D_TTY, 137136467Ssimokawa#endif 138136467Ssimokawa}; 139136467Ssimokawa 140136467Ssimokawa#ifndef KLD_MODULE 141136467Ssimokawastatic char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 142136467Ssimokawa#endif 143136467Ssimokawa 144136467Ssimokawa/* global data */ 145136467Ssimokawastatic struct dcons_global dg; 146136467Ssimokawastruct dcons_global *dcons_conf; 147136467Ssimokawastatic int poll_hz = DCONS_POLL_HZ; 148136467Ssimokawa 149136467Ssimokawastatic struct dcons_softc sc[DCONS_NPORT]; 150136467Ssimokawa 151136467SsimokawaSYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 152136467SsimokawaSYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 153136467Ssimokawa "dcons polling rate"); 154136467Ssimokawa 155136467Ssimokawastatic int drv_init = 0; 156136467Ssimokawastatic struct callout dcons_callout; 157136467Ssimokawastruct dcons_buf *dcons_buf; /* for local dconschat */ 158136467Ssimokawa 159136467Ssimokawa#ifdef __DragonFly__ 160136467Ssimokawa#define DEV dev_t 161136467Ssimokawa#define THREAD d_thread_t 162136467Ssimokawa#elif __FreeBSD_version < 500000 163136467Ssimokawa#define DEV dev_t 164136467Ssimokawa#define THREAD struct proc 165136467Ssimokawa#else 166136467Ssimokawa#define DEV struct cdev * 167136467Ssimokawa#define THREAD struct thread 168136467Ssimokawa#endif 169136467Ssimokawa 170136467Ssimokawa 171136467Ssimokawastatic void dcons_tty_start(struct tty *); 172136467Ssimokawastatic int dcons_tty_param(struct tty *, struct termios *); 173136467Ssimokawastatic void dcons_timeout(void *); 174136467Ssimokawastatic int dcons_drv_init(int); 175136467Ssimokawa 176136467Ssimokawastatic cn_probe_t dcons_cnprobe; 177136467Ssimokawastatic cn_init_t dcons_cninit; 178158959Sphkstatic cn_term_t dcons_cnterm; 179136467Ssimokawastatic cn_getc_t dcons_cngetc; 180136467Ssimokawastatic cn_putc_t dcons_cnputc; 181136467Ssimokawa 182158959SphkCONSOLE_DRIVER(dcons); 183136467Ssimokawa 184170017Ssimokawa#if defined(GDB) && (__FreeBSD_version >= 502122) 185136467Ssimokawastatic gdb_probe_f dcons_dbg_probe; 186136467Ssimokawastatic gdb_init_f dcons_dbg_init; 187136467Ssimokawastatic gdb_term_f dcons_dbg_term; 188136467Ssimokawastatic gdb_getc_f dcons_dbg_getc; 189136467Ssimokawastatic gdb_putc_f dcons_dbg_putc; 190136467Ssimokawa 191136467SsimokawaGDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 192158950Sphk dcons_dbg_getc, dcons_dbg_putc); 193136467Ssimokawa 194136467Ssimokawaextern struct gdb_dbgport *gdb_cur; 195136467Ssimokawa#endif 196136467Ssimokawa 197170017Ssimokawa#if (defined(GDB) || defined(DDB)) && defined(ALT_BREAK_TO_DEBUGGER) 198136467Ssimokawastatic int 199136467Ssimokawadcons_check_break(struct dcons_softc *dc, int c) 200136467Ssimokawa{ 201178766Speter#if __FreeBSD_version >= 502122 202178766Speter int kdb_brk; 203178766Speter#endif 204136467Ssimokawa if (c < 0) 205136467Ssimokawa return (c); 206136467Ssimokawa 207136467Ssimokawa#if __FreeBSD_version >= 502122 208178766Speter if ((kdb_brk = kdb_alt_break(c, &dc->brk_state)) != 0) { 209178766Speter switch (kdb_brk) { 210178766Speter case KDB_REQ_DEBUGGER: 211178766Speter 212178766Speter if ((dc->flags & DC_GDB) != 0) { 213170017Ssimokawa#ifdef GDB 214178766Speter if (gdb_cur == &dcons_gdb_dbgport) { 215178766Speter kdb_dbbe_select("gdb"); 216178766Speter kdb_enter(KDB_WHY_BREAK, 217178766Speter "Break sequence on dcons gdb port"); 218178766Speter } 219178766Speter#endif 220178766Speter } else 221174898Srwatson kdb_enter(KDB_WHY_BREAK, 222178766Speter "Break sequence on dcons console port"); 223178766Speter break; 224178766Speter case KDB_REQ_PANIC: 225178766Speter kdb_panic("Panic sequence on dcons console port"); 226178766Speter break; 227178766Speter case KDB_REQ_BREAK: 228178766Speter kdb_reboot(); 229178766Speter break; 230178766Speter } 231136467Ssimokawa } 232136467Ssimokawa#else 233136467Ssimokawa switch (dc->brk_state) { 234136467Ssimokawa case STATE1: 235136467Ssimokawa if (c == KEY_TILDE) 236136467Ssimokawa dc->brk_state = STATE2; 237136467Ssimokawa else 238136467Ssimokawa dc->brk_state = STATE0; 239136467Ssimokawa break; 240136467Ssimokawa case STATE2: 241136467Ssimokawa dc->brk_state = STATE0; 242136467Ssimokawa if (c == KEY_CTRLB) { 243136467Ssimokawa#if DCONS_FORCE_GDB 244136467Ssimokawa if (dc->flags & DC_GDB) 245136467Ssimokawa boothowto |= RB_GDB; 246136467Ssimokawa#endif 247136467Ssimokawa breakpoint(); 248136467Ssimokawa } 249136467Ssimokawa } 250136467Ssimokawa if (c == KEY_CR) 251136467Ssimokawa dc->brk_state = STATE1; 252136467Ssimokawa#endif 253136467Ssimokawa return (c); 254136467Ssimokawa} 255136467Ssimokawa#else 256136467Ssimokawa#define dcons_check_break(dc, c) (c) 257136467Ssimokawa#endif 258136467Ssimokawa 259136467Ssimokawastatic int 260171867Ssimokawadcons_os_checkc_nopoll(struct dcons_softc *dc) 261136467Ssimokawa{ 262136467Ssimokawa int c; 263136467Ssimokawa 264136467Ssimokawa if (dg.dma_tag != NULL) 265136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 266136467Ssimokawa 267136467Ssimokawa c = dcons_check_break(dc, dcons_checkc(dc)); 268136467Ssimokawa 269136467Ssimokawa if (dg.dma_tag != NULL) 270136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 271136467Ssimokawa 272136467Ssimokawa return (c); 273136467Ssimokawa} 274136467Ssimokawa 275171867Ssimokawastatic int 276171867Ssimokawadcons_os_checkc(struct dcons_softc *dc) 277171867Ssimokawa{ 278171867Ssimokawa EVENTHANDLER_INVOKE(dcons_poll, 0); 279171867Ssimokawa return (dcons_os_checkc_nopoll(dc)); 280171867Ssimokawa} 281171867Ssimokawa 282170017Ssimokawa#if defined(GDB) || !defined(CONS_NODEV) 283136467Ssimokawastatic int 284136467Ssimokawadcons_os_getc(struct dcons_softc *dc) 285136467Ssimokawa{ 286136467Ssimokawa int c; 287136467Ssimokawa 288136467Ssimokawa while ((c = dcons_os_checkc(dc)) == -1); 289136467Ssimokawa 290136467Ssimokawa return (c & 0xff); 291136467Ssimokawa} 292170017Ssimokawa#endif 293136467Ssimokawa 294136467Ssimokawastatic void 295136467Ssimokawadcons_os_putc(struct dcons_softc *dc, int c) 296136467Ssimokawa{ 297136467Ssimokawa if (dg.dma_tag != NULL) 298136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 299136467Ssimokawa 300136467Ssimokawa dcons_putc(dc, c); 301136467Ssimokawa 302136467Ssimokawa if (dg.dma_tag != NULL) 303136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 304136467Ssimokawa} 305136467Ssimokawastatic int 306136467Ssimokawadcons_open(DEV dev, int flag, int mode, THREAD *td) 307136467Ssimokawa{ 308136467Ssimokawa struct tty *tp; 309136467Ssimokawa int unit, error, s; 310136467Ssimokawa 311136467Ssimokawa unit = minor(dev); 312136467Ssimokawa if (unit != 0) 313136467Ssimokawa return (ENXIO); 314136467Ssimokawa 315154016Sphk tp = dev->si_tty; 316136467Ssimokawa tp->t_oproc = dcons_tty_start; 317136467Ssimokawa tp->t_param = dcons_tty_param; 318136467Ssimokawa tp->t_stop = nottystop; 319136467Ssimokawa tp->t_dev = dev; 320136467Ssimokawa 321136467Ssimokawa error = 0; 322136467Ssimokawa 323136467Ssimokawa s = spltty(); 324136467Ssimokawa if ((tp->t_state & TS_ISOPEN) == 0) { 325136467Ssimokawa tp->t_state |= TS_CARR_ON; 326136680Sphk ttyconsolemode(tp, 0); 327164033Srwatson } else if ((tp->t_state & TS_XCLUDE) && 328164033Srwatson priv_check(td, PRIV_TTY_EXCLUSIVE)) { 329136467Ssimokawa splx(s); 330136467Ssimokawa return (EBUSY); 331136467Ssimokawa } 332136467Ssimokawa splx(s); 333136467Ssimokawa 334136467Ssimokawa#if __FreeBSD_version < 502113 335136467Ssimokawa error = (*linesw[tp->t_line].l_open)(dev, tp); 336136467Ssimokawa#else 337136467Ssimokawa error = ttyld_open(tp, dev); 338136467Ssimokawa#endif 339136467Ssimokawa 340136467Ssimokawa return (error); 341136467Ssimokawa} 342136467Ssimokawa 343136467Ssimokawastatic int 344136467Ssimokawadcons_close(DEV dev, int flag, int mode, THREAD *td) 345136467Ssimokawa{ 346136467Ssimokawa int unit; 347136467Ssimokawa struct tty *tp; 348136467Ssimokawa 349136467Ssimokawa unit = minor(dev); 350136467Ssimokawa if (unit != 0) 351136467Ssimokawa return (ENXIO); 352136467Ssimokawa 353136467Ssimokawa tp = dev->si_tty; 354136467Ssimokawa if (tp->t_state & TS_ISOPEN) { 355136467Ssimokawa#if __FreeBSD_version < 502113 356136467Ssimokawa (*linesw[tp->t_line].l_close)(tp, flag); 357136467Ssimokawa ttyclose(tp); 358136467Ssimokawa#else 359136467Ssimokawa ttyld_close(tp, flag); 360136467Ssimokawa tty_close(tp); 361136467Ssimokawa#endif 362136467Ssimokawa } 363136467Ssimokawa 364136467Ssimokawa return (0); 365136467Ssimokawa} 366136467Ssimokawa 367136467Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500104 368136467Ssimokawastatic int 369136467Ssimokawadcons_ioctl(DEV dev, u_long cmd, caddr_t data, int flag, THREAD *td) 370136467Ssimokawa{ 371136467Ssimokawa int unit; 372136467Ssimokawa struct tty *tp; 373136467Ssimokawa int error; 374136467Ssimokawa 375136467Ssimokawa unit = minor(dev); 376136467Ssimokawa if (unit != 0) 377136467Ssimokawa return (ENXIO); 378136467Ssimokawa 379136467Ssimokawa tp = dev->si_tty; 380136467Ssimokawa error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 381136467Ssimokawa if (error != ENOIOCTL) 382136467Ssimokawa return (error); 383136467Ssimokawa 384136467Ssimokawa error = ttioctl(tp, cmd, data, flag); 385136467Ssimokawa if (error != ENOIOCTL) 386136467Ssimokawa return (error); 387136467Ssimokawa 388136467Ssimokawa return (ENOTTY); 389136467Ssimokawa} 390136467Ssimokawa#endif 391136467Ssimokawa 392136467Ssimokawastatic int 393136467Ssimokawadcons_tty_param(struct tty *tp, struct termios *t) 394136467Ssimokawa{ 395136467Ssimokawa tp->t_ispeed = t->c_ispeed; 396136467Ssimokawa tp->t_ospeed = t->c_ospeed; 397136467Ssimokawa tp->t_cflag = t->c_cflag; 398136467Ssimokawa return 0; 399136467Ssimokawa} 400136467Ssimokawa 401136467Ssimokawastatic void 402136467Ssimokawadcons_tty_start(struct tty *tp) 403136467Ssimokawa{ 404136467Ssimokawa struct dcons_softc *dc; 405136467Ssimokawa int s; 406136467Ssimokawa 407136467Ssimokawa dc = (struct dcons_softc *)tp->t_dev->si_drv1; 408136467Ssimokawa s = spltty(); 409136467Ssimokawa if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 410136467Ssimokawa ttwwakeup(tp); 411136467Ssimokawa return; 412136467Ssimokawa } 413136467Ssimokawa 414136467Ssimokawa tp->t_state |= TS_BUSY; 415136467Ssimokawa while (tp->t_outq.c_cc != 0) 416136467Ssimokawa dcons_os_putc(dc, getc(&tp->t_outq)); 417136467Ssimokawa tp->t_state &= ~TS_BUSY; 418136467Ssimokawa 419136467Ssimokawa ttwwakeup(tp); 420136467Ssimokawa splx(s); 421136467Ssimokawa} 422136467Ssimokawa 423136467Ssimokawastatic void 424136467Ssimokawadcons_timeout(void *v) 425136467Ssimokawa{ 426136467Ssimokawa struct tty *tp; 427136467Ssimokawa struct dcons_softc *dc; 428136467Ssimokawa int i, c, polltime; 429136467Ssimokawa 430136467Ssimokawa for (i = 0; i < DCONS_NPORT; i ++) { 431136467Ssimokawa dc = &sc[i]; 432136467Ssimokawa tp = ((DEV)dc->dev)->si_tty; 433171867Ssimokawa while ((c = dcons_os_checkc_nopoll(dc)) != -1) 434136467Ssimokawa if (tp->t_state & TS_ISOPEN) 435136467Ssimokawa#if __FreeBSD_version < 502113 436136467Ssimokawa (*linesw[tp->t_line].l_rint)(c, tp); 437136467Ssimokawa#else 438136467Ssimokawa ttyld_rint(tp, c); 439136467Ssimokawa#endif 440136467Ssimokawa } 441136467Ssimokawa polltime = hz / poll_hz; 442136467Ssimokawa if (polltime < 1) 443136467Ssimokawa polltime = 1; 444136467Ssimokawa callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 445136467Ssimokawa} 446136467Ssimokawa 447136467Ssimokawastatic void 448136467Ssimokawadcons_cnprobe(struct consdev *cp) 449136467Ssimokawa{ 450136467Ssimokawa#ifdef __DragonFly__ 451136467Ssimokawa cp->cn_dev = make_dev(&dcons_cdevsw, DCONS_CON, 452136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, "dcons"); 453136467Ssimokawa#elif __FreeBSD_version >= 501109 454136467Ssimokawa sprintf(cp->cn_name, "dcons"); 455136467Ssimokawa#else 456136467Ssimokawa cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON); 457136467Ssimokawa#endif 458136467Ssimokawa#if DCONS_FORCE_CONSOLE 459136467Ssimokawa cp->cn_pri = CN_REMOTE; 460136467Ssimokawa#else 461136467Ssimokawa cp->cn_pri = CN_NORMAL; 462136467Ssimokawa#endif 463136467Ssimokawa} 464136467Ssimokawa 465136467Ssimokawastatic void 466136467Ssimokawadcons_cninit(struct consdev *cp) 467136467Ssimokawa{ 468136467Ssimokawa dcons_drv_init(0); 469136467Ssimokawa#if CONS_NODEV 470136467Ssimokawa cp->cn_arg 471136467Ssimokawa#else 472136467Ssimokawa cp->cn_dev->si_drv1 473136467Ssimokawa#endif 474136467Ssimokawa = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 475136467Ssimokawa} 476136467Ssimokawa 477158959Sphkstatic void 478158959Sphkdcons_cnterm(struct consdev *cp) 479158959Sphk{ 480158959Sphk} 481158959Sphk 482136467Ssimokawa#if CONS_NODEV 483136467Ssimokawastatic int 484136467Ssimokawadcons_cngetc(struct consdev *cp) 485136467Ssimokawa{ 486136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 487136467Ssimokawa return (dcons_os_checkc(dc)); 488136467Ssimokawa} 489136467Ssimokawastatic void 490136467Ssimokawadcons_cnputc(struct consdev *cp, int c) 491136467Ssimokawa{ 492136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 493136467Ssimokawa dcons_os_putc(dc, c); 494136467Ssimokawa} 495136467Ssimokawa#else 496136467Ssimokawastatic int 497136467Ssimokawadcons_cngetc(DEV dev) 498136467Ssimokawa{ 499136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 500136467Ssimokawa return (dcons_os_getc(dc)); 501136467Ssimokawa} 502136467Ssimokawastatic int 503136467Ssimokawadcons_cncheckc(DEV dev) 504136467Ssimokawa{ 505136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 506136467Ssimokawa return (dcons_os_checkc(dc)); 507136467Ssimokawa} 508136467Ssimokawastatic void 509136467Ssimokawadcons_cnputc(DEV dev, int c) 510136467Ssimokawa{ 511136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 512136467Ssimokawa dcons_os_putc(dc, c); 513136467Ssimokawa} 514136467Ssimokawa#endif 515136467Ssimokawa 516136467Ssimokawastatic int 517136467Ssimokawadcons_drv_init(int stage) 518136467Ssimokawa{ 519137013Ssimokawa#if defined(__i386__) || defined(__amd64__) 520136898Ssimokawa quad_t addr, size; 521136498Ssimokawa#endif 522136467Ssimokawa 523136467Ssimokawa if (drv_init) 524136467Ssimokawa return(drv_init); 525136467Ssimokawa 526136467Ssimokawa drv_init = -1; 527136467Ssimokawa 528136467Ssimokawa bzero(&dg, sizeof(dg)); 529136467Ssimokawa dcons_conf = &dg; 530136467Ssimokawa dg.cdev = &dcons_consdev; 531136467Ssimokawa dg.buf = NULL; 532136467Ssimokawa dg.size = DCONS_BUF_SIZE; 533136467Ssimokawa 534137013Ssimokawa#if defined(__i386__) || defined(__amd64__) 535136898Ssimokawa if (getenv_quad("dcons.addr", &addr) > 0 && 536136898Ssimokawa getenv_quad("dcons.size", &size) > 0) { 537137013Ssimokawa#ifdef __i386__ 538136898Ssimokawa vm_paddr_t pa; 539136898Ssimokawa /* 540136898Ssimokawa * Allow read/write access to dcons buffer. 541136898Ssimokawa */ 542136898Ssimokawa for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 543136898Ssimokawa *vtopte(KERNBASE + pa) |= PG_RW; 544136898Ssimokawa invltlb(); 545137013Ssimokawa#endif 546136467Ssimokawa /* XXX P to V */ 547136898Ssimokawa dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 548136467Ssimokawa dg.size = size; 549136467Ssimokawa if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 550136467Ssimokawa dg.buf = NULL; 551136467Ssimokawa } 552136467Ssimokawa#endif 553136467Ssimokawa if (dg.buf != NULL) 554136467Ssimokawa goto ok; 555136467Ssimokawa 556136467Ssimokawa#ifndef KLD_MODULE 557136467Ssimokawa if (stage == 0) { /* XXX or cold */ 558136467Ssimokawa /* 559136467Ssimokawa * DCONS_FORCE_CONSOLE == 1 and statically linked. 560136467Ssimokawa * called from cninit(). can't use contigmalloc yet . 561136467Ssimokawa */ 562136467Ssimokawa dg.buf = (struct dcons_buf *) bssbuf; 563136467Ssimokawa dcons_init(dg.buf, dg.size, sc); 564136467Ssimokawa } else 565136467Ssimokawa#endif 566136467Ssimokawa { 567136467Ssimokawa /* 568136467Ssimokawa * DCONS_FORCE_CONSOLE == 0 or kernel module case. 569136467Ssimokawa * if the module is loaded after boot, 570136467Ssimokawa * bssbuf could be non-continuous. 571136467Ssimokawa */ 572136467Ssimokawa dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 573136467Ssimokawa M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 574136467Ssimokawa dcons_init(dg.buf, dg.size, sc); 575136467Ssimokawa } 576136467Ssimokawa 577136467Ssimokawaok: 578136467Ssimokawa dcons_buf = dg.buf; 579136467Ssimokawa 580136467Ssimokawa#if __FreeBSD_version < 502122 581153110Sru#if defined(DDB) && DCONS_FORCE_GDB 582136467Ssimokawa#if CONS_NODEV 583136467Ssimokawa gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB]; 584136467Ssimokawa#if __FreeBSD_version >= 501109 585136467Ssimokawa sprintf(gdbconsdev.cn_name, "dgdb"); 586136467Ssimokawa#endif 587136467Ssimokawa gdb_arg = &gdbconsdev; 588136467Ssimokawa#elif defined(__DragonFly__) 589136467Ssimokawa gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB, 590136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, "dgdb"); 591136467Ssimokawa#else 592136467Ssimokawa gdbdev = makedev(CDEV_MAJOR, DCONS_GDB); 593136467Ssimokawa#endif 594136467Ssimokawa gdb_getc = dcons_cngetc; 595136467Ssimokawa gdb_putc = dcons_cnputc; 596136467Ssimokawa#endif 597136467Ssimokawa#endif 598136467Ssimokawa drv_init = 1; 599136467Ssimokawa 600136467Ssimokawa return 0; 601136467Ssimokawa} 602136467Ssimokawa 603136467Ssimokawa 604136467Ssimokawastatic int 605136467Ssimokawadcons_attach_port(int port, char *name, int flags) 606136467Ssimokawa{ 607136467Ssimokawa struct dcons_softc *dc; 608136467Ssimokawa struct tty *tp; 609136467Ssimokawa DEV dev; 610136467Ssimokawa 611136467Ssimokawa dc = &sc[port]; 612136467Ssimokawa dc->flags = flags; 613136467Ssimokawa dev = make_dev(&dcons_cdevsw, port, 614136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, name); 615136467Ssimokawa dc->dev = (void *)dev; 616154016Sphk tp = ttyalloc(); 617136467Ssimokawa 618136467Ssimokawa dev->si_drv1 = (void *)dc; 619136467Ssimokawa dev->si_tty = tp; 620136467Ssimokawa 621136467Ssimokawa tp->t_oproc = dcons_tty_start; 622136467Ssimokawa tp->t_param = dcons_tty_param; 623136467Ssimokawa tp->t_stop = nottystop; 624136467Ssimokawa tp->t_dev = dc->dev; 625136467Ssimokawa 626136467Ssimokawa return(0); 627136467Ssimokawa} 628136467Ssimokawa 629136467Ssimokawastatic int 630136467Ssimokawadcons_attach(void) 631136467Ssimokawa{ 632136467Ssimokawa int polltime; 633136467Ssimokawa 634136467Ssimokawa#ifdef __DragonFly__ 635136467Ssimokawa cdevsw_add(&dcons_cdevsw, -1, 0); 636136467Ssimokawa#endif 637136467Ssimokawa dcons_attach_port(DCONS_CON, "dcons", 0); 638136467Ssimokawa dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 639136467Ssimokawa#if __FreeBSD_version < 500000 640136467Ssimokawa callout_init(&dcons_callout); 641136467Ssimokawa#else 642136467Ssimokawa callout_init(&dcons_callout, 0); 643136467Ssimokawa#endif 644136467Ssimokawa polltime = hz / poll_hz; 645136467Ssimokawa if (polltime < 1) 646136467Ssimokawa polltime = 1; 647136467Ssimokawa callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 648136467Ssimokawa return(0); 649136467Ssimokawa} 650136467Ssimokawa 651136467Ssimokawastatic int 652136467Ssimokawadcons_detach(int port) 653136467Ssimokawa{ 654136467Ssimokawa struct tty *tp; 655136467Ssimokawa struct dcons_softc *dc; 656136467Ssimokawa 657136467Ssimokawa dc = &sc[port]; 658136467Ssimokawa 659136467Ssimokawa tp = ((DEV)dc->dev)->si_tty; 660136467Ssimokawa 661136467Ssimokawa if (tp->t_state & TS_ISOPEN) { 662136467Ssimokawa printf("dcons: still opened\n"); 663136467Ssimokawa#if __FreeBSD_version < 502113 664136467Ssimokawa (*linesw[tp->t_line].l_close)(tp, 0); 665136467Ssimokawa tp->t_gen++; 666136467Ssimokawa ttyclose(tp); 667136467Ssimokawa ttwakeup(tp); 668136467Ssimokawa ttwwakeup(tp); 669136467Ssimokawa#else 670136467Ssimokawa ttyld_close(tp, 0); 671136467Ssimokawa tty_close(tp); 672136467Ssimokawa#endif 673136467Ssimokawa } 674136467Ssimokawa /* XXX 675136467Ssimokawa * must wait until all device are closed. 676136467Ssimokawa */ 677136467Ssimokawa#ifdef __DragonFly__ 678136467Ssimokawa tsleep((void *)dc, 0, "dcodtc", hz/4); 679136467Ssimokawa#else 680136467Ssimokawa tsleep((void *)dc, PWAIT, "dcodtc", hz/4); 681136467Ssimokawa#endif 682136467Ssimokawa destroy_dev(dc->dev); 683136467Ssimokawa 684136467Ssimokawa return(0); 685136467Ssimokawa} 686136467Ssimokawa 687136467Ssimokawa 688136467Ssimokawa/* cnXXX works only for FreeBSD-5 */ 689136467Ssimokawastatic int 690136467Ssimokawadcons_modevent(module_t mode, int type, void *data) 691136467Ssimokawa{ 692136467Ssimokawa int err = 0, ret; 693136467Ssimokawa 694136467Ssimokawa switch (type) { 695136467Ssimokawa case MOD_LOAD: 696136467Ssimokawa ret = dcons_drv_init(1); 697136467Ssimokawa dcons_attach(); 698136467Ssimokawa#if __FreeBSD_version >= 500000 699136467Ssimokawa if (ret == 0) { 700136467Ssimokawa dcons_cnprobe(&dcons_consdev); 701136467Ssimokawa dcons_cninit(&dcons_consdev); 702136467Ssimokawa cnadd(&dcons_consdev); 703136467Ssimokawa } 704136467Ssimokawa#endif 705136467Ssimokawa break; 706136467Ssimokawa case MOD_UNLOAD: 707136467Ssimokawa printf("dcons: unload\n"); 708136467Ssimokawa callout_stop(&dcons_callout); 709136467Ssimokawa#if __FreeBSD_version < 502122 710153110Sru#if defined(DDB) && DCONS_FORCE_GDB 711136467Ssimokawa#if CONS_NODEV 712136467Ssimokawa gdb_arg = NULL; 713136467Ssimokawa#else 714136467Ssimokawa gdbdev = NULL; 715136467Ssimokawa#endif 716136467Ssimokawa#endif 717136467Ssimokawa#endif 718136467Ssimokawa#if __FreeBSD_version >= 500000 719136467Ssimokawa cnremove(&dcons_consdev); 720136467Ssimokawa#endif 721136467Ssimokawa dcons_detach(DCONS_CON); 722136467Ssimokawa dcons_detach(DCONS_GDB); 723136467Ssimokawa dg.buf->magic = 0; 724136467Ssimokawa 725136467Ssimokawa contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 726136467Ssimokawa 727136467Ssimokawa break; 728136467Ssimokawa case MOD_SHUTDOWN: 729170426Ssimokawa#if 0 /* Keep connection after halt */ 730136467Ssimokawa dg.buf->magic = 0; 731170426Ssimokawa#endif 732136467Ssimokawa break; 733136467Ssimokawa default: 734136467Ssimokawa err = EOPNOTSUPP; 735136467Ssimokawa break; 736136467Ssimokawa } 737136467Ssimokawa return(err); 738136467Ssimokawa} 739136467Ssimokawa 740170017Ssimokawa#if defined(GDB) && (__FreeBSD_version >= 502122) 741136467Ssimokawa/* Debugger interface */ 742136467Ssimokawa 743136467Ssimokawastatic int 744136467Ssimokawadcons_dbg_probe(void) 745136467Ssimokawa{ 746170537Ssimokawa int dcons_gdb; 747170537Ssimokawa 748170537Ssimokawa if (getenv_int("dcons_gdb", &dcons_gdb) == 0) 749170537Ssimokawa return (-1); 750170537Ssimokawa return (dcons_gdb); 751136467Ssimokawa} 752136467Ssimokawa 753136467Ssimokawastatic void 754136467Ssimokawadcons_dbg_init(void) 755136467Ssimokawa{ 756136467Ssimokawa} 757136467Ssimokawa 758136467Ssimokawastatic void 759136467Ssimokawadcons_dbg_term(void) 760136467Ssimokawa{ 761136467Ssimokawa} 762136467Ssimokawa 763136467Ssimokawastatic void 764136467Ssimokawadcons_dbg_putc(int c) 765136467Ssimokawa{ 766136467Ssimokawa struct dcons_softc *dc = &sc[DCONS_GDB]; 767136467Ssimokawa dcons_os_putc(dc, c); 768136467Ssimokawa} 769136467Ssimokawa 770136467Ssimokawastatic int 771136467Ssimokawadcons_dbg_getc(void) 772136467Ssimokawa{ 773136467Ssimokawa struct dcons_softc *dc = &sc[DCONS_GDB]; 774136467Ssimokawa return (dcons_os_getc(dc)); 775136467Ssimokawa} 776136467Ssimokawa#endif 777136467Ssimokawa 778136467SsimokawaDEV_MODULE(dcons, dcons_modevent, NULL); 779136467SsimokawaMODULE_VERSION(dcons, DCONS_VERSION); 780