dcons_os.c revision 158959
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 158959 2006-05-26 13:51:38Z 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; 174158959Sphkstatic cn_term_t dcons_cnterm; 175136467Ssimokawastatic cn_getc_t dcons_cngetc; 176136467Ssimokawastatic cn_putc_t dcons_cnputc; 177136467Ssimokawa 178158959SphkCONSOLE_DRIVER(dcons); 179136467Ssimokawa 180136467Ssimokawa#if __FreeBSD_version >= 502122 181136467Ssimokawastatic gdb_probe_f dcons_dbg_probe; 182136467Ssimokawastatic gdb_init_f dcons_dbg_init; 183136467Ssimokawastatic gdb_term_f dcons_dbg_term; 184136467Ssimokawastatic gdb_getc_f dcons_dbg_getc; 185136467Ssimokawastatic gdb_putc_f dcons_dbg_putc; 186136467Ssimokawa 187136467SsimokawaGDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 188158950Sphk dcons_dbg_getc, dcons_dbg_putc); 189136467Ssimokawa 190136467Ssimokawaextern struct gdb_dbgport *gdb_cur; 191136467Ssimokawa#endif 192136467Ssimokawa 193153110Sru#if (defined(KDB) || defined(DDB)) && defined(ALT_BREAK_TO_DEBUGGER) 194136467Ssimokawastatic int 195136467Ssimokawadcons_check_break(struct dcons_softc *dc, int c) 196136467Ssimokawa{ 197136467Ssimokawa if (c < 0) 198136467Ssimokawa return (c); 199136467Ssimokawa 200136467Ssimokawa#if __FreeBSD_version >= 502122 201136467Ssimokawa if (kdb_alt_break(c, &dc->brk_state)) { 202136467Ssimokawa if ((dc->flags & DC_GDB) != 0) { 203136467Ssimokawa if (gdb_cur == &dcons_gdb_dbgport) { 204136467Ssimokawa kdb_dbbe_select("gdb"); 205136467Ssimokawa breakpoint(); 206136467Ssimokawa } 207136467Ssimokawa } else 208136467Ssimokawa breakpoint(); 209136467Ssimokawa } 210136467Ssimokawa#else 211136467Ssimokawa switch (dc->brk_state) { 212136467Ssimokawa case STATE1: 213136467Ssimokawa if (c == KEY_TILDE) 214136467Ssimokawa dc->brk_state = STATE2; 215136467Ssimokawa else 216136467Ssimokawa dc->brk_state = STATE0; 217136467Ssimokawa break; 218136467Ssimokawa case STATE2: 219136467Ssimokawa dc->brk_state = STATE0; 220136467Ssimokawa if (c == KEY_CTRLB) { 221136467Ssimokawa#if DCONS_FORCE_GDB 222136467Ssimokawa if (dc->flags & DC_GDB) 223136467Ssimokawa boothowto |= RB_GDB; 224136467Ssimokawa#endif 225136467Ssimokawa breakpoint(); 226136467Ssimokawa } 227136467Ssimokawa } 228136467Ssimokawa if (c == KEY_CR) 229136467Ssimokawa dc->brk_state = STATE1; 230136467Ssimokawa#endif 231136467Ssimokawa return (c); 232136467Ssimokawa} 233136467Ssimokawa#else 234136467Ssimokawa#define dcons_check_break(dc, c) (c) 235136467Ssimokawa#endif 236136467Ssimokawa 237136467Ssimokawastatic int 238136467Ssimokawadcons_os_checkc(struct dcons_softc *dc) 239136467Ssimokawa{ 240136467Ssimokawa int c; 241136467Ssimokawa 242136467Ssimokawa if (dg.dma_tag != NULL) 243136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 244136467Ssimokawa 245136467Ssimokawa c = dcons_check_break(dc, dcons_checkc(dc)); 246136467Ssimokawa 247136467Ssimokawa if (dg.dma_tag != NULL) 248136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 249136467Ssimokawa 250136467Ssimokawa return (c); 251136467Ssimokawa} 252136467Ssimokawa 253136467Ssimokawastatic int 254136467Ssimokawadcons_os_getc(struct dcons_softc *dc) 255136467Ssimokawa{ 256136467Ssimokawa int c; 257136467Ssimokawa 258136467Ssimokawa while ((c = dcons_os_checkc(dc)) == -1); 259136467Ssimokawa 260136467Ssimokawa return (c & 0xff); 261136467Ssimokawa} 262136467Ssimokawa 263136467Ssimokawastatic void 264136467Ssimokawadcons_os_putc(struct dcons_softc *dc, int c) 265136467Ssimokawa{ 266136467Ssimokawa if (dg.dma_tag != NULL) 267136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 268136467Ssimokawa 269136467Ssimokawa dcons_putc(dc, c); 270136467Ssimokawa 271136467Ssimokawa if (dg.dma_tag != NULL) 272136467Ssimokawa bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 273136467Ssimokawa} 274136467Ssimokawastatic int 275136467Ssimokawadcons_open(DEV dev, int flag, int mode, THREAD *td) 276136467Ssimokawa{ 277136467Ssimokawa struct tty *tp; 278136467Ssimokawa int unit, error, s; 279136467Ssimokawa 280136467Ssimokawa unit = minor(dev); 281136467Ssimokawa if (unit != 0) 282136467Ssimokawa return (ENXIO); 283136467Ssimokawa 284154016Sphk tp = dev->si_tty; 285136467Ssimokawa tp->t_oproc = dcons_tty_start; 286136467Ssimokawa tp->t_param = dcons_tty_param; 287136467Ssimokawa tp->t_stop = nottystop; 288136467Ssimokawa tp->t_dev = dev; 289136467Ssimokawa 290136467Ssimokawa error = 0; 291136467Ssimokawa 292136467Ssimokawa s = spltty(); 293136467Ssimokawa if ((tp->t_state & TS_ISOPEN) == 0) { 294136467Ssimokawa tp->t_state |= TS_CARR_ON; 295136680Sphk ttyconsolemode(tp, 0); 296136467Ssimokawa } else if ((tp->t_state & TS_XCLUDE) && suser(td)) { 297136467Ssimokawa splx(s); 298136467Ssimokawa return (EBUSY); 299136467Ssimokawa } 300136467Ssimokawa splx(s); 301136467Ssimokawa 302136467Ssimokawa#if __FreeBSD_version < 502113 303136467Ssimokawa error = (*linesw[tp->t_line].l_open)(dev, tp); 304136467Ssimokawa#else 305136467Ssimokawa error = ttyld_open(tp, dev); 306136467Ssimokawa#endif 307136467Ssimokawa 308136467Ssimokawa return (error); 309136467Ssimokawa} 310136467Ssimokawa 311136467Ssimokawastatic int 312136467Ssimokawadcons_close(DEV dev, int flag, int mode, THREAD *td) 313136467Ssimokawa{ 314136467Ssimokawa int unit; 315136467Ssimokawa struct tty *tp; 316136467Ssimokawa 317136467Ssimokawa unit = minor(dev); 318136467Ssimokawa if (unit != 0) 319136467Ssimokawa return (ENXIO); 320136467Ssimokawa 321136467Ssimokawa tp = dev->si_tty; 322136467Ssimokawa if (tp->t_state & TS_ISOPEN) { 323136467Ssimokawa#if __FreeBSD_version < 502113 324136467Ssimokawa (*linesw[tp->t_line].l_close)(tp, flag); 325136467Ssimokawa ttyclose(tp); 326136467Ssimokawa#else 327136467Ssimokawa ttyld_close(tp, flag); 328136467Ssimokawa tty_close(tp); 329136467Ssimokawa#endif 330136467Ssimokawa } 331136467Ssimokawa 332136467Ssimokawa return (0); 333136467Ssimokawa} 334136467Ssimokawa 335136467Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500104 336136467Ssimokawastatic int 337136467Ssimokawadcons_ioctl(DEV dev, u_long cmd, caddr_t data, int flag, THREAD *td) 338136467Ssimokawa{ 339136467Ssimokawa int unit; 340136467Ssimokawa struct tty *tp; 341136467Ssimokawa int error; 342136467Ssimokawa 343136467Ssimokawa unit = minor(dev); 344136467Ssimokawa if (unit != 0) 345136467Ssimokawa return (ENXIO); 346136467Ssimokawa 347136467Ssimokawa tp = dev->si_tty; 348136467Ssimokawa error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 349136467Ssimokawa if (error != ENOIOCTL) 350136467Ssimokawa return (error); 351136467Ssimokawa 352136467Ssimokawa error = ttioctl(tp, cmd, data, flag); 353136467Ssimokawa if (error != ENOIOCTL) 354136467Ssimokawa return (error); 355136467Ssimokawa 356136467Ssimokawa return (ENOTTY); 357136467Ssimokawa} 358136467Ssimokawa#endif 359136467Ssimokawa 360136467Ssimokawastatic int 361136467Ssimokawadcons_tty_param(struct tty *tp, struct termios *t) 362136467Ssimokawa{ 363136467Ssimokawa tp->t_ispeed = t->c_ispeed; 364136467Ssimokawa tp->t_ospeed = t->c_ospeed; 365136467Ssimokawa tp->t_cflag = t->c_cflag; 366136467Ssimokawa return 0; 367136467Ssimokawa} 368136467Ssimokawa 369136467Ssimokawastatic void 370136467Ssimokawadcons_tty_start(struct tty *tp) 371136467Ssimokawa{ 372136467Ssimokawa struct dcons_softc *dc; 373136467Ssimokawa int s; 374136467Ssimokawa 375136467Ssimokawa dc = (struct dcons_softc *)tp->t_dev->si_drv1; 376136467Ssimokawa s = spltty(); 377136467Ssimokawa if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 378136467Ssimokawa ttwwakeup(tp); 379136467Ssimokawa return; 380136467Ssimokawa } 381136467Ssimokawa 382136467Ssimokawa tp->t_state |= TS_BUSY; 383136467Ssimokawa while (tp->t_outq.c_cc != 0) 384136467Ssimokawa dcons_os_putc(dc, getc(&tp->t_outq)); 385136467Ssimokawa tp->t_state &= ~TS_BUSY; 386136467Ssimokawa 387136467Ssimokawa ttwwakeup(tp); 388136467Ssimokawa splx(s); 389136467Ssimokawa} 390136467Ssimokawa 391136467Ssimokawastatic void 392136467Ssimokawadcons_timeout(void *v) 393136467Ssimokawa{ 394136467Ssimokawa struct tty *tp; 395136467Ssimokawa struct dcons_softc *dc; 396136467Ssimokawa int i, c, polltime; 397136467Ssimokawa 398136467Ssimokawa for (i = 0; i < DCONS_NPORT; i ++) { 399136467Ssimokawa dc = &sc[i]; 400136467Ssimokawa tp = ((DEV)dc->dev)->si_tty; 401136467Ssimokawa while ((c = dcons_os_checkc(dc)) != -1) 402136467Ssimokawa if (tp->t_state & TS_ISOPEN) 403136467Ssimokawa#if __FreeBSD_version < 502113 404136467Ssimokawa (*linesw[tp->t_line].l_rint)(c, tp); 405136467Ssimokawa#else 406136467Ssimokawa ttyld_rint(tp, c); 407136467Ssimokawa#endif 408136467Ssimokawa } 409136467Ssimokawa polltime = hz / poll_hz; 410136467Ssimokawa if (polltime < 1) 411136467Ssimokawa polltime = 1; 412136467Ssimokawa callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 413136467Ssimokawa} 414136467Ssimokawa 415136467Ssimokawastatic void 416136467Ssimokawadcons_cnprobe(struct consdev *cp) 417136467Ssimokawa{ 418136467Ssimokawa#ifdef __DragonFly__ 419136467Ssimokawa cp->cn_dev = make_dev(&dcons_cdevsw, DCONS_CON, 420136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, "dcons"); 421136467Ssimokawa#elif __FreeBSD_version >= 501109 422136467Ssimokawa sprintf(cp->cn_name, "dcons"); 423136467Ssimokawa#else 424136467Ssimokawa cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON); 425136467Ssimokawa#endif 426136467Ssimokawa#if DCONS_FORCE_CONSOLE 427136467Ssimokawa cp->cn_pri = CN_REMOTE; 428136467Ssimokawa#else 429136467Ssimokawa cp->cn_pri = CN_NORMAL; 430136467Ssimokawa#endif 431136467Ssimokawa} 432136467Ssimokawa 433136467Ssimokawastatic void 434136467Ssimokawadcons_cninit(struct consdev *cp) 435136467Ssimokawa{ 436136467Ssimokawa dcons_drv_init(0); 437136467Ssimokawa#if CONS_NODEV 438136467Ssimokawa cp->cn_arg 439136467Ssimokawa#else 440136467Ssimokawa cp->cn_dev->si_drv1 441136467Ssimokawa#endif 442136467Ssimokawa = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 443136467Ssimokawa} 444136467Ssimokawa 445158959Sphkstatic void 446158959Sphkdcons_cnterm(struct consdev *cp) 447158959Sphk{ 448158959Sphk} 449158959Sphk 450136467Ssimokawa#if CONS_NODEV 451136467Ssimokawastatic int 452136467Ssimokawadcons_cngetc(struct consdev *cp) 453136467Ssimokawa{ 454136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 455136467Ssimokawa return (dcons_os_checkc(dc)); 456136467Ssimokawa} 457136467Ssimokawastatic void 458136467Ssimokawadcons_cnputc(struct consdev *cp, int c) 459136467Ssimokawa{ 460136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 461136467Ssimokawa dcons_os_putc(dc, c); 462136467Ssimokawa} 463136467Ssimokawa#else 464136467Ssimokawastatic int 465136467Ssimokawadcons_cngetc(DEV dev) 466136467Ssimokawa{ 467136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 468136467Ssimokawa return (dcons_os_getc(dc)); 469136467Ssimokawa} 470136467Ssimokawastatic int 471136467Ssimokawadcons_cncheckc(DEV dev) 472136467Ssimokawa{ 473136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 474136467Ssimokawa return (dcons_os_checkc(dc)); 475136467Ssimokawa} 476136467Ssimokawastatic void 477136467Ssimokawadcons_cnputc(DEV dev, int c) 478136467Ssimokawa{ 479136467Ssimokawa struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 480136467Ssimokawa dcons_os_putc(dc, c); 481136467Ssimokawa} 482136467Ssimokawa#endif 483136467Ssimokawa 484136467Ssimokawastatic int 485136467Ssimokawadcons_drv_init(int stage) 486136467Ssimokawa{ 487137013Ssimokawa#if defined(__i386__) || defined(__amd64__) 488136898Ssimokawa quad_t addr, size; 489136498Ssimokawa#endif 490136467Ssimokawa 491136467Ssimokawa if (drv_init) 492136467Ssimokawa return(drv_init); 493136467Ssimokawa 494136467Ssimokawa drv_init = -1; 495136467Ssimokawa 496136467Ssimokawa bzero(&dg, sizeof(dg)); 497136467Ssimokawa dcons_conf = &dg; 498136467Ssimokawa dg.cdev = &dcons_consdev; 499136467Ssimokawa dg.buf = NULL; 500136467Ssimokawa dg.size = DCONS_BUF_SIZE; 501136467Ssimokawa 502137013Ssimokawa#if defined(__i386__) || defined(__amd64__) 503136898Ssimokawa if (getenv_quad("dcons.addr", &addr) > 0 && 504136898Ssimokawa getenv_quad("dcons.size", &size) > 0) { 505137013Ssimokawa#ifdef __i386__ 506136898Ssimokawa vm_paddr_t pa; 507136898Ssimokawa /* 508136898Ssimokawa * Allow read/write access to dcons buffer. 509136898Ssimokawa */ 510136898Ssimokawa for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 511136898Ssimokawa *vtopte(KERNBASE + pa) |= PG_RW; 512136898Ssimokawa invltlb(); 513137013Ssimokawa#endif 514136467Ssimokawa /* XXX P to V */ 515136898Ssimokawa dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 516136467Ssimokawa dg.size = size; 517136467Ssimokawa if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 518136467Ssimokawa dg.buf = NULL; 519136467Ssimokawa } 520136467Ssimokawa#endif 521136467Ssimokawa if (dg.buf != NULL) 522136467Ssimokawa goto ok; 523136467Ssimokawa 524136467Ssimokawa#ifndef KLD_MODULE 525136467Ssimokawa if (stage == 0) { /* XXX or cold */ 526136467Ssimokawa /* 527136467Ssimokawa * DCONS_FORCE_CONSOLE == 1 and statically linked. 528136467Ssimokawa * called from cninit(). can't use contigmalloc yet . 529136467Ssimokawa */ 530136467Ssimokawa dg.buf = (struct dcons_buf *) bssbuf; 531136467Ssimokawa dcons_init(dg.buf, dg.size, sc); 532136467Ssimokawa } else 533136467Ssimokawa#endif 534136467Ssimokawa { 535136467Ssimokawa /* 536136467Ssimokawa * DCONS_FORCE_CONSOLE == 0 or kernel module case. 537136467Ssimokawa * if the module is loaded after boot, 538136467Ssimokawa * bssbuf could be non-continuous. 539136467Ssimokawa */ 540136467Ssimokawa dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 541136467Ssimokawa M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 542136467Ssimokawa dcons_init(dg.buf, dg.size, sc); 543136467Ssimokawa } 544136467Ssimokawa 545136467Ssimokawaok: 546136467Ssimokawa dcons_buf = dg.buf; 547136467Ssimokawa 548136467Ssimokawa#if __FreeBSD_version < 502122 549153110Sru#if defined(DDB) && DCONS_FORCE_GDB 550136467Ssimokawa#if CONS_NODEV 551136467Ssimokawa gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB]; 552136467Ssimokawa#if __FreeBSD_version >= 501109 553136467Ssimokawa sprintf(gdbconsdev.cn_name, "dgdb"); 554136467Ssimokawa#endif 555136467Ssimokawa gdb_arg = &gdbconsdev; 556136467Ssimokawa#elif defined(__DragonFly__) 557136467Ssimokawa gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB, 558136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, "dgdb"); 559136467Ssimokawa#else 560136467Ssimokawa gdbdev = makedev(CDEV_MAJOR, DCONS_GDB); 561136467Ssimokawa#endif 562136467Ssimokawa gdb_getc = dcons_cngetc; 563136467Ssimokawa gdb_putc = dcons_cnputc; 564136467Ssimokawa#endif 565136467Ssimokawa#endif 566136467Ssimokawa drv_init = 1; 567136467Ssimokawa 568136467Ssimokawa return 0; 569136467Ssimokawa} 570136467Ssimokawa 571136467Ssimokawa 572136467Ssimokawastatic int 573136467Ssimokawadcons_attach_port(int port, char *name, int flags) 574136467Ssimokawa{ 575136467Ssimokawa struct dcons_softc *dc; 576136467Ssimokawa struct tty *tp; 577136467Ssimokawa DEV dev; 578136467Ssimokawa 579136467Ssimokawa dc = &sc[port]; 580136467Ssimokawa dc->flags = flags; 581136467Ssimokawa dev = make_dev(&dcons_cdevsw, port, 582136467Ssimokawa UID_ROOT, GID_WHEEL, 0600, name); 583136467Ssimokawa dc->dev = (void *)dev; 584154016Sphk tp = ttyalloc(); 585136467Ssimokawa 586136467Ssimokawa dev->si_drv1 = (void *)dc; 587136467Ssimokawa dev->si_tty = tp; 588136467Ssimokawa 589136467Ssimokawa tp->t_oproc = dcons_tty_start; 590136467Ssimokawa tp->t_param = dcons_tty_param; 591136467Ssimokawa tp->t_stop = nottystop; 592136467Ssimokawa tp->t_dev = dc->dev; 593136467Ssimokawa 594136467Ssimokawa return(0); 595136467Ssimokawa} 596136467Ssimokawa 597136467Ssimokawastatic int 598136467Ssimokawadcons_attach(void) 599136467Ssimokawa{ 600136467Ssimokawa int polltime; 601136467Ssimokawa 602136467Ssimokawa#ifdef __DragonFly__ 603136467Ssimokawa cdevsw_add(&dcons_cdevsw, -1, 0); 604136467Ssimokawa#endif 605136467Ssimokawa dcons_attach_port(DCONS_CON, "dcons", 0); 606136467Ssimokawa dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 607136467Ssimokawa#if __FreeBSD_version < 500000 608136467Ssimokawa callout_init(&dcons_callout); 609136467Ssimokawa#else 610136467Ssimokawa callout_init(&dcons_callout, 0); 611136467Ssimokawa#endif 612136467Ssimokawa polltime = hz / poll_hz; 613136467Ssimokawa if (polltime < 1) 614136467Ssimokawa polltime = 1; 615136467Ssimokawa callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 616136467Ssimokawa return(0); 617136467Ssimokawa} 618136467Ssimokawa 619136467Ssimokawastatic int 620136467Ssimokawadcons_detach(int port) 621136467Ssimokawa{ 622136467Ssimokawa struct tty *tp; 623136467Ssimokawa struct dcons_softc *dc; 624136467Ssimokawa 625136467Ssimokawa dc = &sc[port]; 626136467Ssimokawa 627136467Ssimokawa tp = ((DEV)dc->dev)->si_tty; 628136467Ssimokawa 629136467Ssimokawa if (tp->t_state & TS_ISOPEN) { 630136467Ssimokawa printf("dcons: still opened\n"); 631136467Ssimokawa#if __FreeBSD_version < 502113 632136467Ssimokawa (*linesw[tp->t_line].l_close)(tp, 0); 633136467Ssimokawa tp->t_gen++; 634136467Ssimokawa ttyclose(tp); 635136467Ssimokawa ttwakeup(tp); 636136467Ssimokawa ttwwakeup(tp); 637136467Ssimokawa#else 638136467Ssimokawa ttyld_close(tp, 0); 639136467Ssimokawa tty_close(tp); 640136467Ssimokawa#endif 641136467Ssimokawa } 642136467Ssimokawa /* XXX 643136467Ssimokawa * must wait until all device are closed. 644136467Ssimokawa */ 645136467Ssimokawa#ifdef __DragonFly__ 646136467Ssimokawa tsleep((void *)dc, 0, "dcodtc", hz/4); 647136467Ssimokawa#else 648136467Ssimokawa tsleep((void *)dc, PWAIT, "dcodtc", hz/4); 649136467Ssimokawa#endif 650136467Ssimokawa destroy_dev(dc->dev); 651136467Ssimokawa 652136467Ssimokawa return(0); 653136467Ssimokawa} 654136467Ssimokawa 655136467Ssimokawa 656136467Ssimokawa/* cnXXX works only for FreeBSD-5 */ 657136467Ssimokawastatic int 658136467Ssimokawadcons_modevent(module_t mode, int type, void *data) 659136467Ssimokawa{ 660136467Ssimokawa int err = 0, ret; 661136467Ssimokawa 662136467Ssimokawa switch (type) { 663136467Ssimokawa case MOD_LOAD: 664136467Ssimokawa ret = dcons_drv_init(1); 665136467Ssimokawa dcons_attach(); 666136467Ssimokawa#if __FreeBSD_version >= 500000 667136467Ssimokawa if (ret == 0) { 668136467Ssimokawa dcons_cnprobe(&dcons_consdev); 669136467Ssimokawa dcons_cninit(&dcons_consdev); 670136467Ssimokawa cnadd(&dcons_consdev); 671136467Ssimokawa } 672136467Ssimokawa#endif 673136467Ssimokawa break; 674136467Ssimokawa case MOD_UNLOAD: 675136467Ssimokawa printf("dcons: unload\n"); 676136467Ssimokawa callout_stop(&dcons_callout); 677136467Ssimokawa#if __FreeBSD_version < 502122 678153110Sru#if defined(DDB) && DCONS_FORCE_GDB 679136467Ssimokawa#if CONS_NODEV 680136467Ssimokawa gdb_arg = NULL; 681136467Ssimokawa#else 682136467Ssimokawa gdbdev = NULL; 683136467Ssimokawa#endif 684136467Ssimokawa#endif 685136467Ssimokawa#endif 686136467Ssimokawa#if __FreeBSD_version >= 500000 687136467Ssimokawa cnremove(&dcons_consdev); 688136467Ssimokawa#endif 689136467Ssimokawa dcons_detach(DCONS_CON); 690136467Ssimokawa dcons_detach(DCONS_GDB); 691136467Ssimokawa dg.buf->magic = 0; 692136467Ssimokawa 693136467Ssimokawa contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 694136467Ssimokawa 695136467Ssimokawa break; 696136467Ssimokawa case MOD_SHUTDOWN: 697136467Ssimokawa dg.buf->magic = 0; 698136467Ssimokawa break; 699136467Ssimokawa default: 700136467Ssimokawa err = EOPNOTSUPP; 701136467Ssimokawa break; 702136467Ssimokawa } 703136467Ssimokawa return(err); 704136467Ssimokawa} 705136467Ssimokawa 706136467Ssimokawa#if __FreeBSD_version >= 502122 707136467Ssimokawa/* Debugger interface */ 708136467Ssimokawa 709136467Ssimokawastatic int 710136467Ssimokawadcons_dbg_probe(void) 711136467Ssimokawa{ 712136467Ssimokawa return(DCONS_FORCE_GDB); 713136467Ssimokawa} 714136467Ssimokawa 715136467Ssimokawastatic void 716136467Ssimokawadcons_dbg_init(void) 717136467Ssimokawa{ 718136467Ssimokawa} 719136467Ssimokawa 720136467Ssimokawastatic void 721136467Ssimokawadcons_dbg_term(void) 722136467Ssimokawa{ 723136467Ssimokawa} 724136467Ssimokawa 725136467Ssimokawastatic void 726136467Ssimokawadcons_dbg_putc(int c) 727136467Ssimokawa{ 728136467Ssimokawa struct dcons_softc *dc = &sc[DCONS_GDB]; 729136467Ssimokawa dcons_os_putc(dc, c); 730136467Ssimokawa} 731136467Ssimokawa 732136467Ssimokawastatic int 733136467Ssimokawadcons_dbg_getc(void) 734136467Ssimokawa{ 735136467Ssimokawa struct dcons_softc *dc = &sc[DCONS_GDB]; 736136467Ssimokawa return (dcons_os_getc(dc)); 737136467Ssimokawa} 738136467Ssimokawa#endif 739136467Ssimokawa 740136467SsimokawaDEV_MODULE(dcons, dcons_modevent, NULL); 741136467SsimokawaMODULE_VERSION(dcons, DCONS_VERSION); 742