dcons_os.c revision 158950
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 158950 2006-05-26 11:54:32Z phk $ 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> 51136467Ssimokawa#include <sys/proc.h> 52136467Ssimokawa#include <sys/ucred.h> 53136467Ssimokawa 54136467Ssimokawa#include <machine/bus.h> 55136467Ssimokawa 56136467Ssimokawa#ifdef __DragonFly__ 57136467Ssimokawa#include "dcons.h" 58136467Ssimokawa#include "dcons_os.h" 59136467Ssimokawa#else 60136467Ssimokawa#include <dev/dcons/dcons.h> 61136467Ssimokawa#include <dev/dcons/dcons_os.h> 62136467Ssimokawa#endif 63136467Ssimokawa 64136467Ssimokawa#include <ddb/ddb.h> 65136467Ssimokawa#include <sys/reboot.h> 66136467Ssimokawa 67136467Ssimokawa#include <sys/sysctl.h> 68136467Ssimokawa 69136467Ssimokawa#include <vm/vm.h> 70136467Ssimokawa#include <vm/vm_param.h> 71136467Ssimokawa#include <vm/pmap.h> 72136467Ssimokawa 73136467Ssimokawa#include "opt_ddb.h" 74136467Ssimokawa#include "opt_comconsole.h" 75136467Ssimokawa#include "opt_dcons.h" 76136467Ssimokawa 77136467Ssimokawa#ifndef DCONS_POLL_HZ 78136467Ssimokawa#define DCONS_POLL_HZ 100 79136467Ssimokawa#endif 80136467Ssimokawa 81136467Ssimokawa#ifndef DCONS_BUF_SIZE 82136467Ssimokawa#define DCONS_BUF_SIZE (16*1024) 83136467Ssimokawa#endif 84136467Ssimokawa 85136467Ssimokawa#ifndef DCONS_FORCE_CONSOLE 86136467Ssimokawa#define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 87136467Ssimokawa#endif 88136467Ssimokawa 89136467Ssimokawa#ifndef DCONS_FORCE_GDB 90136467Ssimokawa#define DCONS_FORCE_GDB 1 91136467Ssimokawa#endif 92136467Ssimokawa 93136467Ssimokawa#if __FreeBSD_version >= 500101 94136467Ssimokawa#define CONS_NODEV 1 95136467Ssimokawa#if __FreeBSD_version < 502122 96136467Ssimokawastatic struct consdev gdbconsdev; 97136467Ssimokawa#endif 98136467Ssimokawa#endif 99136467Ssimokawa 100136467Ssimokawastatic d_open_t dcons_open; 101136467Ssimokawastatic d_close_t dcons_close; 102136467Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500104 103136467Ssimokawastatic d_ioctl_t dcons_ioctl; 104136467Ssimokawa#endif 105136467Ssimokawa 106136467Ssimokawastatic struct cdevsw dcons_cdevsw = { 107136467Ssimokawa#ifdef __DragonFly__ 108136467Ssimokawa#define CDEV_MAJOR 184 109136467Ssimokawa "dcons", CDEV_MAJOR, D_TTY, NULL, 0, 110136467Ssimokawa dcons_open, dcons_close, ttyread, ttywrite, dcons_ioctl, 111136467Ssimokawa ttypoll, nommap, nostrategy, nodump, nopsize, 112136467Ssimokawa#elif __FreeBSD_version >= 500104 113136467Ssimokawa .d_version = D_VERSION, 114136467Ssimokawa .d_open = dcons_open, 115136467Ssimokawa .d_close = dcons_close, 116136467Ssimokawa .d_name = "dcons", 117136467Ssimokawa .d_flags = D_TTY | D_NEEDGIANT, 118136467Ssimokawa#else 119136467Ssimokawa#define CDEV_MAJOR 184 120136467Ssimokawa /* open */ dcons_open, 121136467Ssimokawa /* close */ dcons_close, 122136467Ssimokawa /* read */ ttyread, 123136467Ssimokawa /* write */ ttywrite, 124136467Ssimokawa /* ioctl */ dcons_ioctl, 125136467Ssimokawa /* poll */ ttypoll, 126136467Ssimokawa /* mmap */ nommap, 127136467Ssimokawa /* strategy */ nostrategy, 128136467Ssimokawa /* name */ "dcons", 129136467Ssimokawa /* major */ CDEV_MAJOR, 130136467Ssimokawa /* dump */ nodump, 131136467Ssimokawa /* psize */ nopsize, 132136467Ssimokawa /* flags */ D_TTY, 133136467Ssimokawa#endif 134136467Ssimokawa}; 135136467Ssimokawa 136136467Ssimokawa#ifndef KLD_MODULE 137136467Ssimokawastatic char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 138136467Ssimokawa#endif 139136467Ssimokawa 140136467Ssimokawa/* global data */ 141136467Ssimokawastatic struct dcons_global dg; 142136467Ssimokawastruct dcons_global *dcons_conf; 143136467Ssimokawastatic int poll_hz = DCONS_POLL_HZ; 144136467Ssimokawa 145136467Ssimokawastatic struct dcons_softc sc[DCONS_NPORT]; 146136467Ssimokawa 147136467SsimokawaSYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 148136467SsimokawaSYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 149136467Ssimokawa "dcons polling rate"); 150136467Ssimokawa 151136467Ssimokawastatic int drv_init = 0; 152136467Ssimokawastatic struct callout dcons_callout; 153136467Ssimokawastruct dcons_buf *dcons_buf; /* for local dconschat */ 154136467Ssimokawa 155136467Ssimokawa#ifdef __DragonFly__ 156136467Ssimokawa#define DEV dev_t 157136467Ssimokawa#define THREAD d_thread_t 158136467Ssimokawa#elif __FreeBSD_version < 500000 159136467Ssimokawa#define DEV dev_t 160136467Ssimokawa#define THREAD struct proc 161136467Ssimokawa#else 162136467Ssimokawa#define DEV struct cdev * 163136467Ssimokawa#define THREAD struct thread 164136467Ssimokawa#endif 165136467Ssimokawa 166136467Ssimokawa 167136467Ssimokawastatic void dcons_tty_start(struct tty *); 168136467Ssimokawastatic int dcons_tty_param(struct tty *, struct termios *); 169136467Ssimokawastatic void dcons_timeout(void *); 170136467Ssimokawastatic int dcons_drv_init(int); 171136467Ssimokawa 172136467Ssimokawastatic cn_probe_t dcons_cnprobe; 173136467Ssimokawastatic cn_init_t dcons_cninit; 174136467Ssimokawastatic cn_getc_t dcons_cngetc; 175136467Ssimokawastatic cn_checkc_t dcons_cncheckc; 176136467Ssimokawastatic cn_putc_t dcons_cnputc; 177136467Ssimokawa 178136467SsimokawaCONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, NULL, dcons_cngetc, 179136467Ssimokawa dcons_cncheckc, dcons_cnputc, NULL); 180136467Ssimokawa 181136467Ssimokawa#if __FreeBSD_version >= 502122 182136467Ssimokawastatic gdb_probe_f dcons_dbg_probe; 183136467Ssimokawastatic gdb_init_f dcons_dbg_init; 184136467Ssimokawastatic gdb_term_f dcons_dbg_term; 185136467Ssimokawastatic gdb_getc_f dcons_dbg_getc; 186136467Ssimokawastatic gdb_putc_f dcons_dbg_putc; 187136467Ssimokawa 188136467SsimokawaGDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 189158950Sphk dcons_dbg_getc, dcons_dbg_putc); 190136467Ssimokawa 191136467Ssimokawaextern struct gdb_dbgport *gdb_cur; 192136467Ssimokawa#endif 193136467Ssimokawa 194153110Sru#if (defined(KDB) || defined(DDB)) && defined(ALT_BREAK_TO_DEBUGGER) 195136467Ssimokawastatic int 196136467Ssimokawadcons_check_break(struct dcons_softc *dc, int c) 197136467Ssimokawa{ 198136467Ssimokawa if (c < 0) 199136467Ssimokawa return (c); 200136467Ssimokawa 201136467Ssimokawa#if __FreeBSD_version >= 502122 202136467Ssimokawa if (kdb_alt_break(c, &dc->brk_state)) { 203136467Ssimokawa if ((dc->flags & DC_GDB) != 0) { 204136467Ssimokawa if (gdb_cur == &dcons_gdb_dbgport) { 205136467Ssimokawa kdb_dbbe_select("gdb"); 206136467Ssimokawa breakpoint(); 207136467Ssimokawa } 208136467Ssimokawa } else 209136467Ssimokawa breakpoint(); 210136467Ssimokawa } 211136467Ssimokawa#else 212136467Ssimokawa switch (dc->brk_state) { 213136467Ssimokawa case STATE1: 214136467Ssimokawa if (c == KEY_TILDE) 215136467Ssimokawa dc->brk_state = STATE2; 216136467Ssimokawa else 217136467Ssimokawa dc->brk_state = STATE0; 218136467Ssimokawa break; 219136467Ssimokawa case STATE2: 220136467Ssimokawa dc->brk_state = STATE0; 221136467Ssimokawa if (c == KEY_CTRLB) { 222136467Ssimokawa#if DCONS_FORCE_GDB 223136467Ssimokawa if (dc->flags & DC_GDB) 224136467Ssimokawa boothowto |= RB_GDB; 225136467Ssimokawa#endif 226136467Ssimokawa breakpoint(); 227136467Ssimokawa } 228136467Ssimokawa } 229136467Ssimokawa if (c == KEY_CR) 230136467Ssimokawa dc->brk_state = STATE1; 231136467Ssimokawa#endif 232136467Ssimokawa return (c); 233136467Ssimokawa} 234136467Ssimokawa#else 235136467Ssimokawa#define dcons_check_break(dc, c) (c) 236136467Ssimokawa#endif 237136467Ssimokawa 238136467Ssimokawastatic int 239136467Ssimokawadcons_os_checkc(struct dcons_softc *dc) 240136467Ssimokawa{ 241136467Ssimokawa int c; 242136467Ssimokawa 243136467Ssimokawa if (dg.dma_tag != NULL) 244136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 245136467Ssimokawa 246136467Ssimokawa c = dcons_check_break(dc, dcons_checkc(dc)); 247136467Ssimokawa 248136467Ssimokawa if (dg.dma_tag != NULL) 249136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 250136467Ssimokawa 251136467Ssimokawa return (c); 252136467Ssimokawa} 253136467Ssimokawa 254136467Ssimokawastatic int 255136467Ssimokawadcons_os_getc(struct dcons_softc *dc) 256136467Ssimokawa{ 257136467Ssimokawa int c; 258136467Ssimokawa 259136467Ssimokawa while ((c = dcons_os_checkc(dc)) == -1); 260136467Ssimokawa 261136467Ssimokawa return (c & 0xff); 262136467Ssimokawa} 263136467Ssimokawa 264136467Ssimokawastatic void 265136467Ssimokawadcons_os_putc(struct dcons_softc *dc, int c) 266136467Ssimokawa{ 267136467Ssimokawa if (dg.dma_tag != NULL) 268136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 269136467Ssimokawa 270136467Ssimokawa dcons_putc(dc, c); 271136467Ssimokawa 272136467Ssimokawa if (dg.dma_tag != NULL) 273136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 274136467Ssimokawa} 275136467Ssimokawastatic int 276136467Ssimokawadcons_open(DEV dev, int flag, int mode, THREAD *td) 277136467Ssimokawa{ 278136467Ssimokawa struct tty *tp; 279136467Ssimokawa int unit, error, s; 280136467Ssimokawa 281136467Ssimokawa unit = minor(dev); 282136467Ssimokawa if (unit != 0) 283136467Ssimokawa return (ENXIO); 284136467Ssimokawa 285154016Sphk tp = dev->si_tty; 286136467Ssimokawa tp->t_oproc = dcons_tty_start; 287136467Ssimokawa tp->t_param = dcons_tty_param; 288136467Ssimokawa tp->t_stop = nottystop; 289136467Ssimokawa tp->t_dev = dev; 290136467Ssimokawa 291136467Ssimokawa error = 0; 292136467Ssimokawa 293136467Ssimokawa s = spltty(); 294136467Ssimokawa if ((tp->t_state & TS_ISOPEN) == 0) { 295136467Ssimokawa tp->t_state |= TS_CARR_ON; 296136680Sphk ttyconsolemode(tp, 0); 297136467Ssimokawa } else if ((tp->t_state & TS_XCLUDE) && suser(td)) { 298136467Ssimokawa splx(s); 299136467Ssimokawa return (EBUSY); 300136467Ssimokawa } 301136467Ssimokawa splx(s); 302136467Ssimokawa 303136467Ssimokawa#if __FreeBSD_version < 502113 304136467Ssimokawa error = (*linesw[tp->t_line].l_open)(dev, tp); 305136467Ssimokawa#else 306136467Ssimokawa error = ttyld_open(tp, dev); 307136467Ssimokawa#endif 308136467Ssimokawa 309136467Ssimokawa return (error); 310136467Ssimokawa} 311136467Ssimokawa 312136467Ssimokawastatic int 313136467Ssimokawadcons_close(DEV dev, int flag, int mode, THREAD *td) 314136467Ssimokawa{ 315136467Ssimokawa int unit; 316136467Ssimokawa struct tty *tp; 317136467Ssimokawa 318136467Ssimokawa unit = minor(dev); 319136467Ssimokawa if (unit != 0) 320136467Ssimokawa return (ENXIO); 321136467Ssimokawa 322136467Ssimokawa tp = dev->si_tty; 323136467Ssimokawa if (tp->t_state & TS_ISOPEN) { 324136467Ssimokawa#if __FreeBSD_version < 502113 325136467Ssimokawa (*linesw[tp->t_line].l_close)(tp, flag); 326136467Ssimokawa ttyclose(tp); 327136467Ssimokawa#else 328136467Ssimokawa ttyld_close(tp, flag); 329136467Ssimokawa tty_close(tp); 330136467Ssimokawa#endif 331136467Ssimokawa } 332136467Ssimokawa 333136467Ssimokawa return (0); 334136467Ssimokawa} 335136467Ssimokawa 336136467Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500104 337136467Ssimokawastatic int 338136467Ssimokawadcons_ioctl(DEV dev, u_long cmd, caddr_t data, int flag, THREAD *td) 339136467Ssimokawa{ 340136467Ssimokawa int unit; 341136467Ssimokawa struct tty *tp; 342136467Ssimokawa int error; 343136467Ssimokawa 344136467Ssimokawa unit = minor(dev); 345136467Ssimokawa if (unit != 0) 346136467Ssimokawa return (ENXIO); 347136467Ssimokawa 348136467Ssimokawa tp = dev->si_tty; 349136467Ssimokawa error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 350136467Ssimokawa if (error != ENOIOCTL) 351136467Ssimokawa return (error); 352136467Ssimokawa 353136467Ssimokawa error = ttioctl(tp, cmd, data, flag); 354136467Ssimokawa if (error != ENOIOCTL) 355136467Ssimokawa return (error); 356136467Ssimokawa 357136467Ssimokawa return (ENOTTY); 358136467Ssimokawa} 359136467Ssimokawa#endif 360136467Ssimokawa 361136467Ssimokawastatic int 362136467Ssimokawadcons_tty_param(struct tty *tp, struct termios *t) 363136467Ssimokawa{ 364136467Ssimokawa tp->t_ispeed = t->c_ispeed; 365136467Ssimokawa tp->t_ospeed = t->c_ospeed; 366136467Ssimokawa tp->t_cflag = t->c_cflag; 367136467Ssimokawa return 0; 368136467Ssimokawa} 369136467Ssimokawa 370136467Ssimokawastatic void 371136467Ssimokawadcons_tty_start(struct tty *tp) 372136467Ssimokawa{ 373136467Ssimokawa struct dcons_softc *dc; 374136467Ssimokawa int s; 375136467Ssimokawa 376136467Ssimokawa dc = (struct dcons_softc *)tp->t_dev->si_drv1; 377136467Ssimokawa s = spltty(); 378136467Ssimokawa if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 379136467Ssimokawa ttwwakeup(tp); 380136467Ssimokawa return; 381136467Ssimokawa } 382136467Ssimokawa 383136467Ssimokawa tp->t_state |= TS_BUSY; 384136467Ssimokawa while (tp->t_outq.c_cc != 0) 385136467Ssimokawa dcons_os_putc(dc, getc(&tp->t_outq)); 386136467Ssimokawa tp->t_state &= ~TS_BUSY; 387136467Ssimokawa 388136467Ssimokawa ttwwakeup(tp); 389136467Ssimokawa splx(s); 390136467Ssimokawa} 391136467Ssimokawa 392136467Ssimokawastatic void 393136467Ssimokawadcons_timeout(void *v) 394136467Ssimokawa{ 395136467Ssimokawa struct tty *tp; 396136467Ssimokawa struct dcons_softc *dc; 397136467Ssimokawa int i, c, polltime; 398136467Ssimokawa 399136467Ssimokawa for (i = 0; i < DCONS_NPORT; i ++) { 400136467Ssimokawa dc = &sc[i]; 401136467Ssimokawa tp = ((DEV)dc->dev)->si_tty; 402136467Ssimokawa while ((c = dcons_os_checkc(dc)) != -1) 403136467Ssimokawa if (tp->t_state & TS_ISOPEN) 404136467Ssimokawa#if __FreeBSD_version < 502113 405136467Ssimokawa (*linesw[tp->t_line].l_rint)(c, tp); 406136467Ssimokawa#else 407136467Ssimokawa ttyld_rint(tp, c); 408136467Ssimokawa#endif 409136467Ssimokawa } 410136467Ssimokawa polltime = hz / poll_hz; 411136467Ssimokawa if (polltime < 1) 412136467Ssimokawa polltime = 1; 413136467Ssimokawa callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 414136467Ssimokawa} 415136467Ssimokawa 416136467Ssimokawastatic void 417136467Ssimokawadcons_cnprobe(struct consdev *cp) 418136467Ssimokawa{ 419136467Ssimokawa#ifdef __DragonFly__ 420136467Ssimokawa cp->cn_dev = make_dev(&dcons_cdevsw, DCONS_CON, 421136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, "dcons"); 422136467Ssimokawa#elif __FreeBSD_version >= 501109 423136467Ssimokawa sprintf(cp->cn_name, "dcons"); 424136467Ssimokawa#else 425136467Ssimokawa cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON); 426136467Ssimokawa#endif 427136467Ssimokawa#if DCONS_FORCE_CONSOLE 428136467Ssimokawa cp->cn_pri = CN_REMOTE; 429136467Ssimokawa#else 430136467Ssimokawa cp->cn_pri = CN_NORMAL; 431136467Ssimokawa#endif 432136467Ssimokawa} 433136467Ssimokawa 434136467Ssimokawastatic void 435136467Ssimokawadcons_cninit(struct consdev *cp) 436136467Ssimokawa{ 437136467Ssimokawa dcons_drv_init(0); 438136467Ssimokawa#if CONS_NODEV 439136467Ssimokawa cp->cn_arg 440136467Ssimokawa#else 441136467Ssimokawa cp->cn_dev->si_drv1 442136467Ssimokawa#endif 443136467Ssimokawa = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 444136467Ssimokawa} 445136467Ssimokawa 446136467Ssimokawa#if CONS_NODEV 447136467Ssimokawastatic int 448136467Ssimokawadcons_cngetc(struct consdev *cp) 449136467Ssimokawa{ 450136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 451136467Ssimokawa return (dcons_os_getc(dc)); 452136467Ssimokawa} 453136467Ssimokawastatic int 454136467Ssimokawadcons_cncheckc(struct consdev *cp) 455136467Ssimokawa{ 456136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 457136467Ssimokawa return (dcons_os_checkc(dc)); 458136467Ssimokawa} 459136467Ssimokawastatic void 460136467Ssimokawadcons_cnputc(struct consdev *cp, int c) 461136467Ssimokawa{ 462136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 463136467Ssimokawa dcons_os_putc(dc, c); 464136467Ssimokawa} 465136467Ssimokawa#else 466136467Ssimokawastatic int 467136467Ssimokawadcons_cngetc(DEV dev) 468136467Ssimokawa{ 469136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 470136467Ssimokawa return (dcons_os_getc(dc)); 471136467Ssimokawa} 472136467Ssimokawastatic int 473136467Ssimokawadcons_cncheckc(DEV dev) 474136467Ssimokawa{ 475136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 476136467Ssimokawa return (dcons_os_checkc(dc)); 477136467Ssimokawa} 478136467Ssimokawastatic void 479136467Ssimokawadcons_cnputc(DEV dev, int c) 480136467Ssimokawa{ 481136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 482136467Ssimokawa dcons_os_putc(dc, c); 483136467Ssimokawa} 484136467Ssimokawa#endif 485136467Ssimokawa 486136467Ssimokawastatic int 487136467Ssimokawadcons_drv_init(int stage) 488136467Ssimokawa{ 489137013Ssimokawa#if defined(__i386__) || defined(__amd64__) 490136898Ssimokawa quad_t addr, size; 491136498Ssimokawa#endif 492136467Ssimokawa 493136467Ssimokawa if (drv_init) 494136467Ssimokawa return(drv_init); 495136467Ssimokawa 496136467Ssimokawa drv_init = -1; 497136467Ssimokawa 498136467Ssimokawa bzero(&dg, sizeof(dg)); 499136467Ssimokawa dcons_conf = &dg; 500136467Ssimokawa dg.cdev = &dcons_consdev; 501136467Ssimokawa dg.buf = NULL; 502136467Ssimokawa dg.size = DCONS_BUF_SIZE; 503136467Ssimokawa 504137013Ssimokawa#if defined(__i386__) || defined(__amd64__) 505136898Ssimokawa if (getenv_quad("dcons.addr", &addr) > 0 && 506136898Ssimokawa getenv_quad("dcons.size", &size) > 0) { 507137013Ssimokawa#ifdef __i386__ 508136898Ssimokawa vm_paddr_t pa; 509136898Ssimokawa /* 510136898Ssimokawa * Allow read/write access to dcons buffer. 511136898Ssimokawa */ 512136898Ssimokawa for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 513136898Ssimokawa *vtopte(KERNBASE + pa) |= PG_RW; 514136898Ssimokawa invltlb(); 515137013Ssimokawa#endif 516136467Ssimokawa /* XXX P to V */ 517136898Ssimokawa dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 518136467Ssimokawa dg.size = size; 519136467Ssimokawa if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 520136467Ssimokawa dg.buf = NULL; 521136467Ssimokawa } 522136467Ssimokawa#endif 523136467Ssimokawa if (dg.buf != NULL) 524136467Ssimokawa goto ok; 525136467Ssimokawa 526136467Ssimokawa#ifndef KLD_MODULE 527136467Ssimokawa if (stage == 0) { /* XXX or cold */ 528136467Ssimokawa /* 529136467Ssimokawa * DCONS_FORCE_CONSOLE == 1 and statically linked. 530136467Ssimokawa * called from cninit(). can't use contigmalloc yet . 531136467Ssimokawa */ 532136467Ssimokawa dg.buf = (struct dcons_buf *) bssbuf; 533136467Ssimokawa dcons_init(dg.buf, dg.size, sc); 534136467Ssimokawa } else 535136467Ssimokawa#endif 536136467Ssimokawa { 537136467Ssimokawa /* 538136467Ssimokawa * DCONS_FORCE_CONSOLE == 0 or kernel module case. 539136467Ssimokawa * if the module is loaded after boot, 540136467Ssimokawa * bssbuf could be non-continuous. 541136467Ssimokawa */ 542136467Ssimokawa dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 543136467Ssimokawa M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 544136467Ssimokawa dcons_init(dg.buf, dg.size, sc); 545136467Ssimokawa } 546136467Ssimokawa 547136467Ssimokawaok: 548136467Ssimokawa dcons_buf = dg.buf; 549136467Ssimokawa 550136467Ssimokawa#if __FreeBSD_version < 502122 551153110Sru#if defined(DDB) && DCONS_FORCE_GDB 552136467Ssimokawa#if CONS_NODEV 553136467Ssimokawa gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB]; 554136467Ssimokawa#if __FreeBSD_version >= 501109 555136467Ssimokawa sprintf(gdbconsdev.cn_name, "dgdb"); 556136467Ssimokawa#endif 557136467Ssimokawa gdb_arg = &gdbconsdev; 558136467Ssimokawa#elif defined(__DragonFly__) 559136467Ssimokawa gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB, 560136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, "dgdb"); 561136467Ssimokawa#else 562136467Ssimokawa gdbdev = makedev(CDEV_MAJOR, DCONS_GDB); 563136467Ssimokawa#endif 564136467Ssimokawa gdb_getc = dcons_cngetc; 565136467Ssimokawa gdb_putc = dcons_cnputc; 566136467Ssimokawa#endif 567136467Ssimokawa#endif 568136467Ssimokawa drv_init = 1; 569136467Ssimokawa 570136467Ssimokawa return 0; 571136467Ssimokawa} 572136467Ssimokawa 573136467Ssimokawa 574136467Ssimokawastatic int 575136467Ssimokawadcons_attach_port(int port, char *name, int flags) 576136467Ssimokawa{ 577136467Ssimokawa struct dcons_softc *dc; 578136467Ssimokawa struct tty *tp; 579136467Ssimokawa DEV dev; 580136467Ssimokawa 581136467Ssimokawa dc = &sc[port]; 582136467Ssimokawa dc->flags = flags; 583136467Ssimokawa dev = make_dev(&dcons_cdevsw, port, 584136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, name); 585136467Ssimokawa dc->dev = (void *)dev; 586154016Sphk tp = ttyalloc(); 587136467Ssimokawa 588136467Ssimokawa dev->si_drv1 = (void *)dc; 589136467Ssimokawa dev->si_tty = tp; 590136467Ssimokawa 591136467Ssimokawa tp->t_oproc = dcons_tty_start; 592136467Ssimokawa tp->t_param = dcons_tty_param; 593136467Ssimokawa tp->t_stop = nottystop; 594136467Ssimokawa tp->t_dev = dc->dev; 595136467Ssimokawa 596136467Ssimokawa return(0); 597136467Ssimokawa} 598136467Ssimokawa 599136467Ssimokawastatic int 600136467Ssimokawadcons_attach(void) 601136467Ssimokawa{ 602136467Ssimokawa int polltime; 603136467Ssimokawa 604136467Ssimokawa#ifdef __DragonFly__ 605136467Ssimokawa cdevsw_add(&dcons_cdevsw, -1, 0); 606136467Ssimokawa#endif 607136467Ssimokawa dcons_attach_port(DCONS_CON, "dcons", 0); 608136467Ssimokawa dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 609136467Ssimokawa#if __FreeBSD_version < 500000 610136467Ssimokawa callout_init(&dcons_callout); 611136467Ssimokawa#else 612136467Ssimokawa callout_init(&dcons_callout, 0); 613136467Ssimokawa#endif 614136467Ssimokawa polltime = hz / poll_hz; 615136467Ssimokawa if (polltime < 1) 616136467Ssimokawa polltime = 1; 617136467Ssimokawa callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 618136467Ssimokawa return(0); 619136467Ssimokawa} 620136467Ssimokawa 621136467Ssimokawastatic int 622136467Ssimokawadcons_detach(int port) 623136467Ssimokawa{ 624136467Ssimokawa struct tty *tp; 625136467Ssimokawa struct dcons_softc *dc; 626136467Ssimokawa 627136467Ssimokawa dc = &sc[port]; 628136467Ssimokawa 629136467Ssimokawa tp = ((DEV)dc->dev)->si_tty; 630136467Ssimokawa 631136467Ssimokawa if (tp->t_state & TS_ISOPEN) { 632136467Ssimokawa printf("dcons: still opened\n"); 633136467Ssimokawa#if __FreeBSD_version < 502113 634136467Ssimokawa (*linesw[tp->t_line].l_close)(tp, 0); 635136467Ssimokawa tp->t_gen++; 636136467Ssimokawa ttyclose(tp); 637136467Ssimokawa ttwakeup(tp); 638136467Ssimokawa ttwwakeup(tp); 639136467Ssimokawa#else 640136467Ssimokawa ttyld_close(tp, 0); 641136467Ssimokawa tty_close(tp); 642136467Ssimokawa#endif 643136467Ssimokawa } 644136467Ssimokawa /* XXX 645136467Ssimokawa * must wait until all device are closed. 646136467Ssimokawa */ 647136467Ssimokawa#ifdef __DragonFly__ 648136467Ssimokawa tsleep((void *)dc, 0, "dcodtc", hz/4); 649136467Ssimokawa#else 650136467Ssimokawa tsleep((void *)dc, PWAIT, "dcodtc", hz/4); 651136467Ssimokawa#endif 652136467Ssimokawa destroy_dev(dc->dev); 653136467Ssimokawa 654136467Ssimokawa return(0); 655136467Ssimokawa} 656136467Ssimokawa 657136467Ssimokawa 658136467Ssimokawa/* cnXXX works only for FreeBSD-5 */ 659136467Ssimokawastatic int 660136467Ssimokawadcons_modevent(module_t mode, int type, void *data) 661136467Ssimokawa{ 662136467Ssimokawa int err = 0, ret; 663136467Ssimokawa 664136467Ssimokawa switch (type) { 665136467Ssimokawa case MOD_LOAD: 666136467Ssimokawa ret = dcons_drv_init(1); 667136467Ssimokawa dcons_attach(); 668136467Ssimokawa#if __FreeBSD_version >= 500000 669136467Ssimokawa if (ret == 0) { 670136467Ssimokawa dcons_cnprobe(&dcons_consdev); 671136467Ssimokawa dcons_cninit(&dcons_consdev); 672136467Ssimokawa cnadd(&dcons_consdev); 673136467Ssimokawa } 674136467Ssimokawa#endif 675136467Ssimokawa break; 676136467Ssimokawa case MOD_UNLOAD: 677136467Ssimokawa printf("dcons: unload\n"); 678136467Ssimokawa callout_stop(&dcons_callout); 679136467Ssimokawa#if __FreeBSD_version < 502122 680153110Sru#if defined(DDB) && DCONS_FORCE_GDB 681136467Ssimokawa#if CONS_NODEV 682136467Ssimokawa gdb_arg = NULL; 683136467Ssimokawa#else 684136467Ssimokawa gdbdev = NULL; 685136467Ssimokawa#endif 686136467Ssimokawa#endif 687136467Ssimokawa#endif 688136467Ssimokawa#if __FreeBSD_version >= 500000 689136467Ssimokawa cnremove(&dcons_consdev); 690136467Ssimokawa#endif 691136467Ssimokawa dcons_detach(DCONS_CON); 692136467Ssimokawa dcons_detach(DCONS_GDB); 693136467Ssimokawa dg.buf->magic = 0; 694136467Ssimokawa 695136467Ssimokawa contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 696136467Ssimokawa 697136467Ssimokawa break; 698136467Ssimokawa case MOD_SHUTDOWN: 699136467Ssimokawa dg.buf->magic = 0; 700136467Ssimokawa break; 701136467Ssimokawa default: 702136467Ssimokawa err = EOPNOTSUPP; 703136467Ssimokawa break; 704136467Ssimokawa } 705136467Ssimokawa return(err); 706136467Ssimokawa} 707136467Ssimokawa 708136467Ssimokawa#if __FreeBSD_version >= 502122 709136467Ssimokawa/* Debugger interface */ 710136467Ssimokawa 711136467Ssimokawastatic int 712136467Ssimokawadcons_dbg_probe(void) 713136467Ssimokawa{ 714136467Ssimokawa return(DCONS_FORCE_GDB); 715136467Ssimokawa} 716136467Ssimokawa 717136467Ssimokawastatic void 718136467Ssimokawadcons_dbg_init(void) 719136467Ssimokawa{ 720136467Ssimokawa} 721136467Ssimokawa 722136467Ssimokawastatic void 723136467Ssimokawadcons_dbg_term(void) 724136467Ssimokawa{ 725136467Ssimokawa} 726136467Ssimokawa 727136467Ssimokawastatic void 728136467Ssimokawadcons_dbg_putc(int c) 729136467Ssimokawa{ 730136467Ssimokawa struct dcons_softc *dc = &sc[DCONS_GDB]; 731136467Ssimokawa dcons_os_putc(dc, c); 732136467Ssimokawa} 733136467Ssimokawa 734136467Ssimokawastatic int 735136467Ssimokawadcons_dbg_getc(void) 736136467Ssimokawa{ 737136467Ssimokawa struct dcons_softc *dc = &sc[DCONS_GDB]; 738136467Ssimokawa return (dcons_os_getc(dc)); 739136467Ssimokawa} 740136467Ssimokawa#endif 741136467Ssimokawa 742136467SsimokawaDEV_MODULE(dcons, dcons_modevent, NULL); 743136467SsimokawaMODULE_VERSION(dcons, DCONS_VERSION); 744