5907 xdrmblk_getpos() is unreliable
[illumos-gate.git] / usr / src / uts / common / io / wscons.c
blob48970bd69633cf38d218af3e1081e8e6a5e8e5e2
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
27 * "Workstation console" multiplexor driver for Sun.
29 * Sends output to the primary frame buffer using the PROM monitor;
30 * gets input from a stream linked below us that is the "keyboard
31 * driver", below which is linked the primary keyboard.
35 * Locking Policy:
36 * This module has a D_MTPERMOD inner perimeter which means STREAMS
37 * only allows one thread to enter this module through STREAMS entry
38 * points each time -- open() close() put() srv() qtimeout().
39 * So for the most time we do not need locking in this module, but with
40 * the following exceptions:
42 * - wc shares three global variables (wc_dip, vc_active_console,
43 * vc_cons_user, vc_avl_root) with virtual console devname part
44 * (fs/dev/sdev_vtops.c) which get compiled into genunix.
46 * - wc_modechg_cb() is a callback function which will triggered when
47 * framebuffer display mode is changed.
49 * - vt_send_hotkeys() is triggered by timeout() which is not STREAMS MT
50 * safe.
52 * Based on the fact that virtual console devname part and wc_modechg_cb()
53 * only do read access to the above mentioned shared four global variables,
54 * It is safe to do locking this way:
55 * 1) all read access to the four global variables in THIS WC MODULE do not
56 * need locking;
57 * 2) all write access to the four global variables in THIS WC MODULE must
58 * hold vc_lock;
59 * 3) any access to the four global variables in either DEVNAME PART or the
60 * CALLBACK must hold vc_lock;
61 * 4) other global variables which are only shared in this wc module and only
62 * accessible through STREAMS entry points such as "vc_last_console",
63 * "vc_inuse_max_minor", "vc_target_console" and "vc_waitactive_list"
64 * do not need explict locking.
66 * wc_modechg_cb() does read access to vc_state_t::vc_flags,
67 * vc_state_t::vc_state_lock is used to protect concurrently accesses to
68 * vc_state_t::vc_flags which may happen from both through STREAMS entry
69 * points and wc_modechg_cb().
70 * Since wc_modechg_cb() only does read access to vc_state_t::vc_flags,
71 * The other parts of wc module (except wc_modechg_cb()) only has to hold
72 * vc_state_t::vc_flags when writing to vc_state_t::vc_flags.
74 * vt_send_hotkeys() could access vt_pending_vtno at the same time with
75 * the rest of wc module, vt_pending_vtno_lock is used to protect
76 * vt_pending_vtno.
78 * Lock order: vc_lock -> vc_state_t::vc_state_lock.
79 * No overlap between vc_lock and vt_pending_vtno_lock.
82 #include <sys/types.h>
83 #include <sys/param.h>
84 #include <sys/signal.h>
85 #include <sys/cred.h>
86 #include <sys/vnode.h>
87 #include <sys/termios.h>
88 #include <sys/termio.h>
89 #include <sys/ttold.h>
90 #include <sys/stropts.h>
91 #include <sys/stream.h>
92 #include <sys/strsun.h>
93 #include <sys/tty.h>
94 #include <sys/buf.h>
95 #include <sys/uio.h>
96 #include <sys/stat.h>
97 #include <sys/sysmacros.h>
98 #include <sys/errno.h>
99 #include <sys/proc.h>
100 #include <sys/procset.h>
101 #include <sys/fault.h>
102 #include <sys/siginfo.h>
103 #include <sys/debug.h>
104 #include <sys/session.h>
105 #include <sys/kmem.h>
106 #include <sys/cpuvar.h>
107 #include <sys/kbio.h>
108 #include <sys/strredir.h>
109 #include <sys/fs/snode.h>
110 #include <sys/consdev.h>
111 #include <sys/conf.h>
112 #include <sys/cmn_err.h>
113 #include <sys/console.h>
114 #include <sys/promif.h>
115 #include <sys/note.h>
116 #include <sys/polled_io.h>
117 #include <sys/systm.h>
118 #include <sys/ddi.h>
119 #include <sys/sunddi.h>
120 #include <sys/sunndi.h>
121 #include <sys/esunddi.h>
122 #include <sys/sunldi.h>
123 #include <sys/debug.h>
124 #include <sys/console.h>
125 #include <sys/ddi_impldefs.h>
126 #include <sys/policy.h>
127 #include <sys/modctl.h>
128 #include <sys/tem.h>
129 #include <sys/wscons.h>
130 #include <sys/vt_impl.h>
132 /* streams stuff */
133 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", copyreq))
134 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", copyresp))
135 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", datab))
136 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", iocblk))
137 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", msgb))
138 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", queue))
140 #define MINLINES 10
141 #define MAXLINES 48
142 #define LOSCREENLINES 34
143 #define HISCREENLINES 48
145 #define MINCOLS 10
146 #define MAXCOLS 120
147 #define LOSCREENCOLS 80
148 #define HISCREENCOLS 120
150 struct wscons_state {
151 dev_t wc_dev; /* major/minor for this device */
152 #ifdef _HAVE_TEM_FIRMWARE
153 int wc_defer_output; /* set if output device is "slow" */
154 #endif /* _HAVE_TEM_FIRMWARE */
155 queue_t *wc_kbdqueue; /* "console keyboard" device queue */
156 /* below us */
157 cons_polledio_t wc_polledio; /* polled I/O function pointers */
158 cons_polledio_t *wc_kb_polledio; /* keyboard's polledio */
159 unsigned int wc_kb_getpolledio_id; /* id for kb CONSOPENPOLLEDIO */
160 queue_t *wc_pending_wq;
161 mblk_t *wc_pending_link; /* I_PLINK pending for kb polledio */
162 } wscons;
165 * This module has a D_MTPERMOD inner perimeter, so we don't need to protect
166 * the variables only shared within this module
168 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", wscons))
169 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", wscons_state))
170 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vt_stat))
171 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vc_waitactive_msg))
172 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", tty_common))
173 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vt_mode))
174 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vt_dispinfo))
175 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", winsize))
176 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vc_last_console))
178 #ifdef _HAVE_TEM_FIRMWARE
179 ssize_t wc_cons_wrtvec(promif_redir_arg_t arg, uchar_t *s, size_t n);
180 #endif /* _HAVE_TEM_FIRMWARE */
182 static int wcopen(queue_t *, dev_t *, int, int, cred_t *);
183 static int wcclose(queue_t *, int, cred_t *);
184 static int wcuwput(queue_t *, mblk_t *);
185 static int wclrput(queue_t *, mblk_t *);
187 static struct module_info wcm_info = {
189 "wc",
191 INFPSZ,
192 2048,
196 static struct qinit wcurinit = {
197 putq,
198 NULL,
199 wcopen,
200 wcclose,
201 NULL,
202 &wcm_info,
203 NULL
206 static struct qinit wcuwinit = {
207 wcuwput,
208 NULL,
209 wcopen,
210 wcclose,
211 NULL,
212 &wcm_info,
213 NULL
216 static struct qinit wclrinit = {
217 wclrput,
218 NULL,
219 NULL,
220 NULL,
221 NULL,
222 &wcm_info,
223 NULL
227 * We always putnext directly to the underlying queue.
229 static struct qinit wclwinit = {
230 NULL,
231 NULL,
232 NULL,
233 NULL,
234 NULL,
235 &wcm_info,
236 NULL
239 static struct streamtab wcinfo = {
240 &wcurinit,
241 &wcuwinit,
242 &wclrinit,
243 &wclwinit,
246 static int wc_info(dev_info_t *, ddi_info_cmd_t, void *, void **result);
247 static int wc_attach(dev_info_t *, ddi_attach_cmd_t);
249 DDI_DEFINE_STREAM_OPS(wc_ops, nulldev, nulldev, wc_attach, nodev, nodev,
250 wc_info, D_MTPERMOD | D_MP, &wcinfo, ddi_quiesce_not_supported);
252 static void wcreioctl(void *);
253 static void wcioctl(queue_t *, mblk_t *);
254 #ifdef _HAVE_TEM_FIRMWARE
255 static void wcopoll(void *);
256 static void wconsout(void *);
257 #endif /* _HAVE_TEM_FIRMWARE */
258 static void wcrstrt(void *);
259 static void wcstart(void *);
260 static void wc_open_kb_polledio(struct wscons_state *wc, queue_t *q,
261 mblk_t *mp);
262 static void wc_close_kb_polledio(struct wscons_state *wc, queue_t *q,
263 mblk_t *mp);
264 static void wc_polled_putchar(cons_polledio_arg_t arg,
265 unsigned char c);
266 static boolean_t wc_polled_ischar(cons_polledio_arg_t arg);
267 static int wc_polled_getchar(cons_polledio_arg_t arg);
268 static void wc_polled_enter(cons_polledio_arg_t arg);
269 static void wc_polled_exit(cons_polledio_arg_t arg);
270 void wc_get_size(vc_state_t *pvc);
271 static void wc_modechg_cb(tem_modechg_cb_arg_t arg);
273 static struct dev_ops wc_ops;
276 * Debug printing
278 #ifndef DPRINTF
279 #ifdef DEBUG
280 /*PRINTFLIKE1*/
281 static void wc_dprintf(const char *fmt, ...) __KPRINTFLIKE(1);
282 #define DPRINTF(l, m, args) \
283 (((l) >= wc_errlevel) && ((m) & wc_errmask) ? \
284 wc_dprintf args : \
285 (void) 0)
287 * Severity levels for printing
289 #define PRINT_L0 0 /* print every message */
290 #define PRINT_L1 1 /* debug */
291 #define PRINT_L2 2 /* quiet */
294 * Masks
296 #define PRINT_MASK_ALL 0xFFFFFFFFU
297 uint_t wc_errmask = PRINT_MASK_ALL;
298 uint_t wc_errlevel = PRINT_L2;
300 #else
301 #define DPRINTF(l, m, args) /* NOTHING */
302 #endif
303 #endif
306 * Module linkage information for the kernel.
308 static struct modldrv modldrv = {
309 &mod_driverops, /* Type of module. This one is a pseudo driver */
310 "Workstation multiplexer Driver 'wc'",
311 &wc_ops, /* driver ops */
314 static struct modlinkage modlinkage = {
315 MODREV_1,
316 &modldrv,
317 NULL
321 _init(void)
323 int rc;
324 if ((rc = mod_install(&modlinkage)) == 0)
325 vt_init();
326 return (rc);
330 _fini(void)
332 return (mod_remove(&modlinkage));
336 _info(struct modinfo *modinfop)
338 return (mod_info(&modlinkage, modinfop));
341 /*ARGSUSED*/
342 static int
343 wc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
345 /* create minor node for workstation hard console */
346 if (ddi_create_minor_node(devi, "wscons", S_IFCHR,
347 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
348 ddi_remove_minor_node(devi, NULL);
349 return (DDI_FAILURE);
352 mutex_enter(&vc_lock);
354 wc_dip = devi;
356 bzero(&(wscons.wc_polledio), sizeof (wscons.wc_polledio));
358 vt_resize(VC_DEFAULT_COUNT);
360 mutex_exit(&vc_lock);
362 return (DDI_SUCCESS);
365 /* ARGSUSED */
366 static int
367 wc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
368 void **result)
370 int error;
372 switch (infocmd) {
373 case DDI_INFO_DEVT2DEVINFO:
374 if (wc_dip == NULL) {
375 error = DDI_FAILURE;
376 } else {
377 *result = (void *) wc_dip;
378 error = DDI_SUCCESS;
380 break;
381 case DDI_INFO_DEVT2INSTANCE:
382 *result = (void *)0;
383 error = DDI_SUCCESS;
384 break;
385 default:
386 error = DDI_FAILURE;
388 return (error);
391 #ifdef _HAVE_TEM_FIRMWARE
393 * Output buffer. Protected by the per-module inner perimeter.
395 #define MAXHIWAT 2000
396 static char obuf[MAXHIWAT];
397 #endif /* _HAVE_TEM_FIRMWARE */
399 static void
400 wc_init_polledio(void)
402 static boolean_t polledio_inited = B_FALSE;
403 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data",
404 polledio_inited))
406 if (polledio_inited)
407 return;
409 polledio_inited = B_TRUE;
412 * Initialize the parts of the polled I/O struct that
413 * are common to both input and output modes, but which
414 * don't flag to the upper layers, which if any of the
415 * two modes are available. We don't know at this point
416 * if system is configured CONS_KFB, but we will when
417 * consconfig_dacf asks us with CONSOPENPOLLED I/O.
419 bzero(&(wscons.wc_polledio), sizeof (wscons.wc_polledio));
420 wscons.wc_polledio.cons_polledio_version =
421 CONSPOLLEDIO_V0;
422 wscons.wc_polledio.cons_polledio_argument =
423 (cons_polledio_arg_t)&wscons;
424 wscons.wc_polledio.cons_polledio_enter =
425 wc_polled_enter;
426 wscons.wc_polledio.cons_polledio_exit =
427 wc_polled_exit;
429 #ifdef _HAVE_TEM_FIRMWARE
431 * If we're talking directly to a framebuffer, we assume
432 * that it's a "slow" device, so that rendering should
433 * be deferred to a timeout or softcall so that we write
434 * a bunch of characters at once.
436 wscons.wc_defer_output = prom_stdout_is_framebuffer();
437 #endif /* _HAVE_TEM_FIRMWARE */
440 /*ARGSUSED*/
441 static int
442 wcopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
444 int minor;
446 wc_init_polledio();
447 minor = (int)getminor(*devp);
448 return (vt_open(minor, q, crp));
451 /*ARGSUSED*/
452 static int
453 wcclose(queue_t *q, int flag, cred_t *crp)
455 vc_state_t *pvc = (vc_state_t *)q->q_ptr;
457 qprocsoff(q);
459 mutex_enter(&vc_lock);
462 * If we are closing the VT node which
463 * /dev/vt/console_user points to, revert
464 * /dev/vt/console to /dev/console
466 if (vc_cons_user == pvc->vc_minor)
467 vc_cons_user = VT_MINOR_INVALID;
469 if (pvc->vc_minor == 0 || pvc->vc_minor == vc_active_console) {
472 * If we lose the system console,
473 * no any other active consoles.
475 if (pvc->vc_minor == 0 && pvc->vc_minor == vc_active_console) {
476 vc_active_console = VT_MINOR_INVALID;
477 vc_last_console = VT_MINOR_INVALID;
481 * just clean for our primary console
482 * and active console
484 mutex_enter(&pvc->vc_state_lock);
485 vt_clean(q, pvc);
486 mutex_exit(&pvc->vc_state_lock);
488 mutex_exit(&vc_lock);
490 return (0);
492 vt_close(q, pvc, crp);
494 mutex_exit(&vc_lock);
496 return (0);
500 * Put procedure for upper write queue.
501 * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
502 * queue up M_BREAK, M_DELAY, and M_DATA messages for processing by
503 * the start routine, and then call the start routine; discard
504 * everything else.
506 static int
507 wcuwput(queue_t *q, mblk_t *mp)
509 vc_state_t *pvc = (vc_state_t *)q->q_ptr;
511 switch (mp->b_datap->db_type) {
513 case M_STOP:
514 mutex_enter(&pvc->vc_state_lock);
515 pvc->vc_flags |= WCS_STOPPED;
516 mutex_exit(&pvc->vc_state_lock);
518 freemsg(mp);
519 break;
521 case M_START:
522 mutex_enter(&pvc->vc_state_lock);
523 pvc->vc_flags &= ~WCS_STOPPED;
524 mutex_exit(&pvc->vc_state_lock);
526 wcstart(pvc);
527 freemsg(mp);
528 break;
530 case M_IOCTL: {
531 struct iocblk *iocp;
532 struct linkblk *linkp;
534 iocp = (struct iocblk *)(void *)mp->b_rptr;
535 switch (iocp->ioc_cmd) {
537 case I_LINK: /* stupid, but permitted */
538 case I_PLINK:
539 if (wscons.wc_kbdqueue != NULL) {
540 /* somebody already linked */
541 miocnak(q, mp, 0, EINVAL);
542 return (0);
544 linkp = (struct linkblk *)(void *)mp->b_cont->b_rptr;
545 wscons.wc_kbdqueue = WR(linkp->l_qbot);
546 mp->b_datap->db_type = M_IOCACK;
547 iocp->ioc_count = 0;
548 wc_open_kb_polledio(&wscons, q, mp);
549 break;
551 case I_UNLINK: /* stupid, but permitted */
552 case I_PUNLINK:
553 linkp = (struct linkblk *)(void *)mp->b_cont->b_rptr;
554 if (wscons.wc_kbdqueue != WR(linkp->l_qbot)) {
555 /* not us */
556 miocnak(q, mp, 0, EINVAL);
557 return (0);
560 mp->b_datap->db_type = M_IOCACK;
561 iocp->ioc_count = 0;
562 wc_close_kb_polledio(&wscons, q, mp);
563 break;
565 case TCSETSW:
566 case TCSETSF:
567 case TCSETAW:
568 case TCSETAF:
569 case TCSBRK:
571 * The changes do not take effect until all
572 * output queued before them is drained.
573 * Put this message on the queue, so that
574 * "wcstart" will see it when it's done
575 * with the output before it. Poke the
576 * start routine, just in case.
578 (void) putq(q, mp);
579 wcstart(pvc);
580 break;
582 case CONSSETABORTENABLE:
583 case CONSGETABORTENABLE:
584 case KIOCSDIRECT:
585 if (wscons.wc_kbdqueue != NULL) {
586 wscons.wc_pending_wq = q;
587 (void) putnext(wscons.wc_kbdqueue, mp);
588 break;
590 /* fall through */
592 default:
594 * Do it now.
596 wcioctl(q, mp);
597 break;
599 break;
602 case M_FLUSH:
603 if (*mp->b_rptr & FLUSHW) {
605 * Flush our write queue.
607 flushq(q, FLUSHDATA); /* XXX doesn't flush M_DELAY */
608 *mp->b_rptr &= ~FLUSHW; /* it has been flushed */
610 if (*mp->b_rptr & FLUSHR) {
611 flushq(RD(q), FLUSHDATA);
612 qreply(q, mp); /* give the read queues a crack at it */
613 } else
614 freemsg(mp);
615 break;
617 case M_BREAK:
619 * Ignore these, as they make no sense.
621 freemsg(mp);
622 break;
624 case M_DELAY:
625 case M_DATA:
627 * Queue the message up to be transmitted,
628 * and poke the start routine.
630 (void) putq(q, mp);
631 wcstart(pvc);
632 break;
634 case M_IOCDATA:
635 vt_miocdata(q, mp);
636 break;
638 default:
640 * "No, I don't want a subscription to Chain Store Age,
641 * thank you anyway."
643 freemsg(mp);
644 break;
647 return (0);
651 * Retry an "ioctl", now that "qbufcall" claims we may be able to allocate
652 * the buffer we need.
654 /*ARGSUSED*/
655 static void
656 wcreioctl(void *arg)
658 vc_state_t *pvc = (vc_state_t *)arg;
659 queue_t *q;
660 mblk_t *mp;
662 pvc->vc_bufcallid = 0;
663 q = pvc->vc_ttycommon.t_writeq;
664 if ((mp = pvc->vc_ttycommon.t_iocpending) != NULL) {
665 /* not pending any more */
666 pvc->vc_ttycommon.t_iocpending = NULL;
667 wcioctl(q, mp);
671 static int
672 wc_getterm(mblk_t *mp)
674 char *term;
675 intptr_t arg;
676 int flag = ((struct iocblk *)(void *)mp->b_rptr)->ioc_flag;
678 STRUCT_DECL(cons_getterm, wcterm);
679 STRUCT_INIT(wcterm, flag);
681 arg = *((intptr_t *)(void *)mp->b_cont->b_rptr);
683 if (ddi_copyin((void *)arg, STRUCT_BUF(wcterm),
684 STRUCT_SIZE(wcterm), flag) != 0) {
685 return (EFAULT);
688 if (consmode == CONS_FW) {
689 /* PROM terminal emulator */
690 term = "sun";
691 } else {
692 /* Kernel terminal emulator */
693 ASSERT(consmode == CONS_KFB);
694 term = "sun-color";
697 if (STRUCT_FGET(wcterm, cn_term_len) <
698 strlen(term) + 1) {
699 return (EOVERFLOW);
702 if (ddi_copyout(term,
703 STRUCT_FGETP(wcterm, cn_term_type),
704 strlen(term) + 1, flag) != 0) {
705 return (EFAULT);
708 return (0);
712 * Process an "ioctl" message sent down to us.
714 static void
715 wcioctl(queue_t *q, mblk_t *mp)
717 vc_state_t *pvc = (vc_state_t *)q->q_ptr;
718 struct iocblk *iocp;
719 size_t datasize;
720 int error;
721 long len;
723 iocp = (struct iocblk *)(void *)mp->b_rptr;
725 if ((iocp->ioc_cmd & VTIOC) == VTIOC ||
726 (iocp->ioc_cmd & KDIOC) == KDIOC) {
727 vt_ioctl(q, mp);
728 return;
731 switch (iocp->ioc_cmd) {
732 case TIOCSWINSZ:
734 * Ignore all attempts to set the screen size; the
735 * value in the EEPROM is guaranteed (modulo PROM bugs)
736 * to be the value used by the PROM monitor code, so it
737 * is by definition correct. Many programs (e.g.,
738 * "login" and "tset") will attempt to reset the size
739 * to (0, 0) or (34, 80), neither of which is
740 * necessarily correct.
741 * We just ACK the message, so as not to disturb
742 * programs that set the sizes.
744 iocp->ioc_count = 0; /* no data returned */
745 mp->b_datap->db_type = M_IOCACK;
746 qreply(q, mp);
747 return;
749 case CONSOPENPOLLEDIO:
750 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
751 ("wcioctl: CONSOPENPOLLEDIO\n"));
753 error = miocpullup(mp, sizeof (struct cons_polledio *));
754 if (error != 0) {
755 miocnak(q, mp, 0, error);
756 return;
760 * We are given an appropriate-sized data block,
761 * and return a pointer to our structure in it.
763 if (consmode == CONS_KFB)
764 wscons.wc_polledio.cons_polledio_putchar =
765 wc_polled_putchar;
766 *(struct cons_polledio **)(void *)mp->b_cont->b_rptr =
767 &wscons.wc_polledio;
769 mp->b_datap->db_type = M_IOCACK;
771 qreply(q, mp);
772 break;
774 case CONS_GETTERM:
775 if ((error = wc_getterm(mp)) != 0)
776 miocnak(q, mp, 0, error);
777 else
778 miocack(q, mp, 0, 0);
779 return;
781 case WC_OPEN_FB:
783 * Start out pessimistic, so that we can just jump to
784 * the reply to bail out.
786 mp->b_datap->db_type = M_IOCNAK;
789 * First test: really, this should be done only from
790 * inside the kernel. Unfortunately, that information
791 * doesn't seem to be available in a streams ioctl,
792 * so restrict it to root only. (Perhaps we could check
793 * for ioc_cr == kcred.)
795 if ((iocp->ioc_error = secpolicy_console(iocp->ioc_cr)) != 0)
796 goto open_fail;
799 * Some miscellaneous checks...
801 iocp->ioc_error = EINVAL;
804 * If we don't have exactly one continuation block, fail.
806 if (mp->b_cont == NULL ||
807 mp->b_cont->b_cont != NULL)
808 goto open_fail;
811 * If there's no null terminator in the string, fail.
813 /* LINTED E_PTRDIFF_OVERFLOW */
814 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
815 if (memchr(mp->b_cont->b_rptr, 0, len) == NULL)
816 goto open_fail;
819 * NOTE: should eventually get default
820 * dimensions from a property, e.g. screen-#rows.
822 iocp->ioc_error = tem_info_init((char *)mp->b_cont->b_rptr,
823 iocp->ioc_cr);
825 * Of course, if the terminal emulator initialization
826 * failed, fail.
828 if (iocp->ioc_error != 0)
829 goto open_fail;
831 #ifdef _HAVE_TEM_FIRMWARE
832 if (prom_stdout_is_framebuffer()) {
834 * Drivers in the console stream may emit additional
835 * messages before we are ready. This causes text
836 * overwrite on the screen. So we set the redirection
837 * here. It is safe because the ioctl in consconfig_dacf
838 * will succeed and consmode will be set to CONS_KFB.
840 prom_set_stdout_redirect(wc_cons_wrtvec,
841 (promif_redir_arg_t)NULL);
844 #endif /* _HAVE_TEM_FIRMWARE */
846 tem_register_modechg_cb(wc_modechg_cb,
847 (tem_modechg_cb_arg_t)&wscons);
850 * ... and succeed.
852 mp->b_datap->db_type = M_IOCACK;
854 open_fail:
855 qreply(q, mp);
856 break;
858 case WC_CLOSE_FB:
860 * There's nothing that can call this, so it's not
861 * really implemented.
863 mp->b_datap->db_type = M_IOCNAK;
865 * However, if it were implemented, it would clearly
866 * be root-only.
868 if ((iocp->ioc_error = secpolicy_console(iocp->ioc_cr)) != 0)
869 goto close_fail;
871 iocp->ioc_error = EINVAL;
873 close_fail:
874 qreply(q, mp);
875 break;
877 default:
880 * The only way in which "ttycommon_ioctl" can fail is
881 * if the "ioctl" requires a response containing data
882 * to be returned to the user, and no mblk could be
883 * allocated for the data. No such "ioctl" alters our
884 * state. Thus, we always go ahead and do any
885 * state-changes the "ioctl" calls for. If we couldn't
886 * allocate the data, "ttycommon_ioctl" has stashed the
887 * "ioctl" away safely, so we just call "qbufcall" to
888 * request that we be called back when we stand a
889 * better chance of allocating the data.
891 datasize = ttycommon_ioctl(&pvc->vc_ttycommon, q, mp, &error);
892 if (datasize != 0) {
893 if (pvc->vc_bufcallid != 0)
894 qunbufcall(q, pvc->vc_bufcallid);
895 pvc->vc_bufcallid = qbufcall(q, datasize, BPRI_HI,
896 wcreioctl, pvc);
897 return;
900 if (error < 0) {
901 if (iocp->ioc_cmd == TCSBRK)
902 error = 0;
903 else
904 error = EINVAL;
906 if (error != 0) {
907 iocp->ioc_error = error;
908 mp->b_datap->db_type = M_IOCNAK;
910 qreply(q, mp);
911 break;
916 * This function gets the polled I/O structures from the lower
917 * keyboard driver. If any initialization or resource allocation
918 * needs to be done by the lower driver, it will be done when
919 * the lower driver services this message.
921 static void
922 wc_open_kb_polledio(struct wscons_state *wscons, queue_t *q, mblk_t *mp)
924 mblk_t *mp2;
925 struct iocblk *iocp;
927 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
928 ("wc_open_kb_polledio: sending CONSOPENPOLLEDIO\n"));
930 mp2 = mkiocb(CONSOPENPOLLEDIO);
932 if (mp2 == NULL) {
934 * If we can't get an mblk, then wait for it.
936 goto nomem;
939 mp2->b_cont = allocb(sizeof (struct cons_polledio *), BPRI_HI);
941 if (mp2->b_cont == NULL) {
943 * If we can't get an mblk, then wait for it, and release
944 * the mblk that we have already allocated.
946 freemsg(mp2);
947 goto nomem;
950 iocp = (struct iocblk *)(void *)mp2->b_rptr;
952 iocp->ioc_count = sizeof (struct cons_polledio *);
953 mp2->b_cont->b_wptr = mp2->b_cont->b_rptr +
954 sizeof (struct cons_polledio *);
956 wscons->wc_pending_wq = q;
957 wscons->wc_pending_link = mp;
958 wscons->wc_kb_getpolledio_id = iocp->ioc_id;
960 putnext(wscons->wc_kbdqueue, mp2);
962 return;
964 nomem:
965 iocp = (struct iocblk *)(void *)mp->b_rptr;
966 iocp->ioc_error = ENOMEM;
967 mp->b_datap->db_type = M_IOCNAK;
968 qreply(q, mp);
972 * This function releases the polled I/O structures from the lower
973 * keyboard driver. If any de-initialization needs to be done, or
974 * any resources need to be released, it will be done when the lower
975 * driver services this message.
977 static void
978 wc_close_kb_polledio(struct wscons_state *wscons, queue_t *q, mblk_t *mp)
980 mblk_t *mp2;
981 struct iocblk *iocp;
983 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
984 ("wc_close_kb_polledio: sending CONSCLOSEPOLLEDIO\n"));
986 mp2 = mkiocb(CONSCLOSEPOLLEDIO);
988 if (mp2 == NULL) {
990 * If we can't get an mblk, then wait for it.
992 goto nomem;
995 mp2->b_cont = allocb(sizeof (struct cons_polledio *), BPRI_HI);
997 if (mp2->b_cont == NULL) {
999 * If we can't get an mblk, then wait for it, and release
1000 * the mblk that we have already allocated.
1002 freemsg(mp2);
1004 goto nomem;
1007 iocp = (struct iocblk *)(void *)mp2->b_rptr;
1009 iocp->ioc_count = 0;
1011 wscons->wc_pending_wq = q;
1012 wscons->wc_pending_link = mp;
1013 wscons->wc_kb_getpolledio_id = iocp->ioc_id;
1015 putnext(wscons->wc_kbdqueue, mp2);
1017 return;
1019 nomem:
1020 iocp = (struct iocblk *)(void *)mp->b_rptr;
1021 iocp->ioc_error = ENOMEM;
1022 mp->b_datap->db_type = M_IOCNAK;
1023 qreply(q, mp);
1026 #ifdef _HAVE_TEM_FIRMWARE
1027 /* ARGSUSED */
1028 static void
1029 wcopoll(void *arg)
1031 vc_state_t *pvc = (vc_state_t *)arg;
1032 queue_t *q;
1034 q = pvc->vc_ttycommon.t_writeq;
1035 pvc->vc_timeoutid = 0;
1037 mutex_enter(&pvc->vc_state_lock);
1039 /* See if we can continue output */
1040 if ((pvc->vc_flags & WCS_BUSY) && pvc->vc_pendc != -1) {
1041 if (prom_mayput((char)pvc->vc_pendc) == 0) {
1042 pvc->vc_pendc = -1;
1043 pvc->vc_flags &= ~WCS_BUSY;
1044 if (!(pvc->vc_flags&(WCS_DELAY|WCS_STOPPED)))
1045 wcstart(pvc);
1046 } else
1047 pvc->vc_timeoutid = qtimeout(q, wcopoll, pvc, 1);
1050 mutex_exit(&pvc->vc_state_lock);
1052 #endif /* _HAVE_TEM_FIRMWARE */
1055 * Restart output on the console after a timeout.
1057 /* ARGSUSED */
1058 static void
1059 wcrstrt(void *arg)
1061 vc_state_t *pvc = (vc_state_t *)arg;
1063 ASSERT(pvc->vc_ttycommon.t_writeq != NULL);
1065 mutex_enter(&pvc->vc_state_lock);
1066 pvc->vc_flags &= ~WCS_DELAY;
1067 mutex_exit(&pvc->vc_state_lock);
1069 wcstart(pvc);
1073 * get screen terminal for current output
1075 static tem_vt_state_t
1076 wc_get_screen_tem(vc_state_t *pvc)
1078 if (!tem_initialized(pvc->vc_tem) ||
1079 tem_get_fbmode(pvc->vc_tem) != KD_TEXT)
1080 return (NULL);
1082 return (pvc->vc_tem);
1086 * Start console output
1088 static void
1089 wcstart(void *arg)
1091 vc_state_t *pvc = (vc_state_t *)arg;
1092 tem_vt_state_t ptem = NULL;
1093 #ifdef _HAVE_TEM_FIRMWARE
1094 int c;
1095 ssize_t cc;
1096 #endif /* _HAVE_TEM_FIRMWARE */
1097 queue_t *q;
1098 mblk_t *bp;
1099 mblk_t *nbp;
1102 * If we're waiting for something to happen (delay timeout to
1103 * expire, current transmission to finish, output to be
1104 * restarted, output to finish draining), don't grab anything
1105 * new.
1107 if (pvc->vc_flags & (WCS_DELAY|WCS_BUSY|WCS_STOPPED))
1108 return;
1110 q = pvc->vc_ttycommon.t_writeq;
1112 * assumes that we have been called by whoever holds the
1113 * exclusionary lock on the write-side queue (protects
1114 * vc_flags and vc_pendc).
1116 for (;;) {
1117 if ((bp = getq(q)) == NULL)
1118 return; /* nothing to transmit */
1121 * We have a new message to work on.
1122 * Check whether it's a delay or an ioctl (the latter
1123 * occurs if the ioctl in question was waiting for the output
1124 * to drain). If it's one of those, process it immediately.
1126 switch (bp->b_datap->db_type) {
1128 case M_DELAY:
1130 * Arrange for "wcrstrt" to be called when the
1131 * delay expires; it will turn WCS_DELAY off,
1132 * and call "wcstart" to grab the next message.
1134 if (pvc->vc_timeoutid != 0)
1135 (void) quntimeout(q, pvc->vc_timeoutid);
1136 pvc->vc_timeoutid = qtimeout(q, wcrstrt, pvc,
1137 (clock_t)(*(unsigned char *)bp->b_rptr + 6));
1139 mutex_enter(&pvc->vc_state_lock);
1140 pvc->vc_flags |= WCS_DELAY;
1141 mutex_exit(&pvc->vc_state_lock);
1143 freemsg(bp);
1144 return; /* wait for this to finish */
1146 case M_IOCTL:
1148 * This ioctl was waiting for the output ahead of
1149 * it to drain; obviously, it has. Do it, and
1150 * then grab the next message after it.
1152 wcioctl(q, bp);
1153 continue;
1156 #ifdef _HAVE_TEM_FIRMWARE
1157 if (consmode == CONS_KFB) {
1158 #endif /* _HAVE_TEM_FIRMWARE */
1159 if ((ptem = wc_get_screen_tem(pvc)) != NULL) {
1161 for (nbp = bp; nbp != NULL; nbp = nbp->b_cont) {
1162 if (nbp->b_wptr > nbp->b_rptr) {
1163 (void) tem_write(ptem,
1164 nbp->b_rptr,
1165 /* LINTED */
1166 nbp->b_wptr - nbp->b_rptr,
1167 kcred);
1173 freemsg(bp);
1175 #ifdef _HAVE_TEM_FIRMWARE
1176 continue;
1179 /* consmode = CONS_FW */
1180 if (pvc->vc_minor != 0) {
1181 freemsg(bp);
1182 continue;
1185 /* LINTED E_PTRDIFF_OVERFLOW */
1186 if ((cc = bp->b_wptr - bp->b_rptr) == 0) {
1187 freemsg(bp);
1188 continue;
1191 * Direct output to the frame buffer if this device
1192 * is not the "hardware" console.
1194 if (wscons.wc_defer_output) {
1196 * Never do output here;
1197 * it takes forever.
1199 mutex_enter(&pvc->vc_state_lock);
1200 pvc->vc_flags |= WCS_BUSY;
1201 mutex_exit(&pvc->vc_state_lock);
1203 pvc->vc_pendc = -1;
1204 (void) putbq(q, bp);
1205 if (q->q_count > 128) { /* do it soon */
1206 softcall(wconsout, pvc);
1207 } else { /* wait a bit */
1208 if (pvc->vc_timeoutid != 0)
1209 (void) quntimeout(q,
1210 pvc->vc_timeoutid);
1211 pvc->vc_timeoutid = qtimeout(q, wconsout,
1212 pvc, hz / 30);
1214 return;
1216 for (;;) {
1217 c = *bp->b_rptr++;
1218 cc--;
1219 if (prom_mayput((char)c) != 0) {
1221 mutex_enter(&pvc->vc_state_lock);
1222 pvc->vc_flags |= WCS_BUSY;
1223 mutex_exit(&pvc->vc_state_lock);
1225 pvc->vc_pendc = c;
1226 if (pvc->vc_timeoutid != 0)
1227 (void) quntimeout(q,
1228 pvc->vc_timeoutid);
1229 pvc->vc_timeoutid = qtimeout(q, wcopoll,
1230 pvc, 1);
1231 if (bp != NULL)
1232 /* not done with this message yet */
1233 (void) putbq(q, bp);
1234 return;
1236 while (cc <= 0) {
1237 nbp = bp;
1238 bp = bp->b_cont;
1239 freeb(nbp);
1240 if (bp == NULL)
1241 return;
1242 /* LINTED E_PTRDIFF_OVERFLOW */
1243 cc = bp->b_wptr - bp->b_rptr;
1246 #endif /* _HAVE_TEM_FIRMWARE */
1250 #ifdef _HAVE_TEM_FIRMWARE
1252 * Output to frame buffer console.
1253 * It takes a long time to scroll.
1255 /* ARGSUSED */
1256 static void
1257 wconsout(void *arg)
1259 vc_state_t *pvc = (vc_state_t *)arg;
1260 uchar_t *cp;
1261 ssize_t cc;
1262 queue_t *q;
1263 mblk_t *bp;
1264 mblk_t *nbp;
1265 char *current_position;
1266 ssize_t bytes_left;
1268 if ((q = pvc->vc_ttycommon.t_writeq) == NULL) {
1269 return; /* not attached to a stream */
1273 * Set up to copy up to MAXHIWAT bytes.
1275 current_position = &obuf[0];
1276 bytes_left = MAXHIWAT;
1277 while ((bp = getq(q)) != NULL) {
1278 if (bp->b_datap->db_type == M_IOCTL) {
1280 * This ioctl was waiting for the output ahead of
1281 * it to drain; obviously, it has. Put it back
1282 * so that "wcstart" can handle it, and transmit
1283 * what we've got.
1285 (void) putbq(q, bp);
1286 goto transmit;
1289 do {
1290 cp = bp->b_rptr;
1291 /* LINTED E_PTRDIFF_OVERFLOW */
1292 cc = bp->b_wptr - cp;
1293 while (cc != 0) {
1294 if (bytes_left == 0) {
1296 * Out of buffer space; put this
1297 * buffer back on the queue, and
1298 * transmit what we have.
1300 bp->b_rptr = cp;
1301 (void) putbq(q, bp);
1302 goto transmit;
1304 *current_position++ = *cp++;
1305 cc--;
1306 bytes_left--;
1308 nbp = bp;
1309 bp = bp->b_cont;
1310 freeb(nbp);
1311 } while (bp != NULL);
1314 transmit:
1315 if ((cc = MAXHIWAT - bytes_left) != 0)
1316 console_puts(obuf, cc);
1318 mutex_enter(&pvc->vc_state_lock);
1319 pvc->vc_flags &= ~WCS_BUSY;
1320 mutex_exit(&pvc->vc_state_lock);
1322 wcstart(pvc);
1324 #endif /* _HAVE_TEM_FIRMWARE */
1327 * Put procedure for lower read queue.
1328 * Pass everything up to queue above "upper half".
1330 static int
1331 wclrput(queue_t *q, mblk_t *mp)
1333 vc_state_t *pvc;
1334 queue_t *upq;
1335 struct iocblk *iocp;
1337 pvc = vt_minor2vc(VT_ACTIVE);
1339 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1340 ("wclrput: wclrput type = 0x%x\n", mp->b_datap->db_type));
1342 switch (mp->b_datap->db_type) {
1344 case M_FLUSH:
1345 if (*mp->b_rptr == FLUSHW || *mp->b_rptr == FLUSHRW) {
1347 * Flush our write queue.
1349 /* XXX doesn't flush M_DELAY */
1350 flushq(WR(q), FLUSHDATA);
1351 *mp->b_rptr = FLUSHR; /* it has been flushed */
1353 if (*mp->b_rptr == FLUSHR || *mp->b_rptr == FLUSHRW) {
1354 flushq(q, FLUSHDATA);
1355 *mp->b_rptr = FLUSHW; /* it has been flushed */
1356 qreply(q, mp); /* give the read queues a crack at it */
1357 } else
1358 freemsg(mp);
1359 break;
1361 case M_DATA:
1362 if (consmode == CONS_KFB && vt_check_hotkeys(mp)) {
1363 freemsg(mp);
1364 break;
1367 if ((upq = pvc->vc_ttycommon.t_readq) != NULL) {
1368 if (!canput(upq->q_next)) {
1369 ttycommon_qfull(&pvc->vc_ttycommon, upq);
1370 wcstart(pvc);
1371 freemsg(mp);
1372 } else {
1373 putnext(upq, mp);
1375 } else
1376 freemsg(mp);
1377 break;
1379 case M_IOCACK:
1380 case M_IOCNAK:
1381 iocp = (struct iocblk *)(void *)mp->b_rptr;
1382 if (wscons.wc_pending_link != NULL &&
1383 iocp->ioc_id == wscons.wc_kb_getpolledio_id) {
1384 switch (mp->b_datap->db_type) {
1386 case M_IOCACK:
1387 switch (iocp->ioc_cmd) {
1389 case CONSOPENPOLLEDIO:
1390 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1391 ("wclrput: "
1392 "ACK CONSOPENPOLLEDIO\n"));
1393 wscons.wc_kb_polledio =
1394 *(struct cons_polledio **)
1395 (void *)mp->b_cont->b_rptr;
1396 wscons.wc_polledio.
1397 cons_polledio_getchar =
1398 wc_polled_getchar;
1399 wscons.wc_polledio.
1400 cons_polledio_ischar =
1401 wc_polled_ischar;
1402 break;
1404 case CONSCLOSEPOLLEDIO:
1405 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1406 ("wclrput: "
1407 "ACK CONSCLOSEPOLLEDIO\n"));
1408 wscons.wc_kb_polledio = NULL;
1409 wscons.wc_kbdqueue = NULL;
1410 wscons.wc_polledio.
1411 cons_polledio_getchar = NULL;
1412 wscons.wc_polledio.
1413 cons_polledio_ischar = NULL;
1414 break;
1415 default:
1416 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1417 ("wclrput: "
1418 "ACK UNKNOWN\n"));
1421 break;
1422 case M_IOCNAK:
1424 * Keyboard may or may not support polled I/O.
1425 * This ioctl may have been rejected because
1426 * we only have the wc->conskbd chain built,
1427 * and the keyboard driver has not been linked
1428 * underneath conskbd yet.
1430 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1431 ("wclrput: NAK\n"));
1433 switch (iocp->ioc_cmd) {
1435 case CONSCLOSEPOLLEDIO:
1436 wscons.wc_kb_polledio = NULL;
1437 wscons.wc_kbdqueue = NULL;
1438 wscons.wc_polledio.
1439 cons_polledio_getchar = NULL;
1440 wscons.wc_polledio.
1441 cons_polledio_ischar = NULL;
1442 break;
1444 break;
1448 * Discard the response, replace it with the
1449 * pending response to the I_PLINK, then let it
1450 * flow upward.
1452 freemsg(mp);
1453 mp = wscons.wc_pending_link;
1454 wscons.wc_pending_link = NULL;
1455 wscons.wc_kb_getpolledio_id = 0;
1457 /* FALLTHROUGH */
1459 default: /* inc M_ERROR, M_HANGUP, M_IOCACK, M_IOCNAK, ... */
1460 if (wscons.wc_pending_wq != NULL) {
1461 qreply(wscons.wc_pending_wq, mp);
1462 wscons.wc_pending_wq = NULL;
1463 break;
1466 if ((upq = pvc->vc_ttycommon.t_readq) != NULL) {
1467 putnext(upq, mp);
1468 } else {
1469 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1470 ("wclrput: Message DISCARDED\n"));
1471 freemsg(mp);
1473 break;
1476 return (0);
1479 #ifdef _HAVE_TEM_FIRMWARE
1481 * This routine exists so that prom_write() can redirect writes
1482 * to the framebuffer through the kernel terminal emulator, if
1483 * that configuration is selected during consconfig.
1484 * When the kernel terminal emulator is enabled, consconfig_dacf
1485 * sets up the PROM output redirect vector to enter this function.
1486 * During panic the console will already be powered up as part of
1487 * calling into the prom_*() layer.
1489 /* ARGSUSED */
1490 ssize_t
1491 wc_cons_wrtvec(promif_redir_arg_t arg, uchar_t *s, size_t n)
1493 vc_state_t *pvc;
1495 pvc = vt_minor2vc(VT_ACTIVE);
1497 if (pvc->vc_tem == NULL)
1498 return (0);
1500 ASSERT(consmode == CONS_KFB);
1502 if (panicstr)
1503 polled_io_cons_write(s, n);
1504 else
1505 (void) tem_write(pvc->vc_tem, s, n, kcred);
1507 return (n);
1509 #endif /* _HAVE_TEM_FIRMWARE */
1512 * These are for systems without OBP, and for devices that cannot be
1513 * shared between Solaris and the OBP.
1515 static void
1516 wc_polled_putchar(cons_polledio_arg_t arg, unsigned char c)
1518 vc_state_t *pvc;
1520 pvc = vt_minor2vc(VT_ACTIVE);
1522 if (c == '\n')
1523 wc_polled_putchar(arg, '\r');
1525 if (pvc->vc_tem == NULL) {
1527 * We have no terminal emulator configured. We have no
1528 * recourse but to drop the output on the floor.
1530 return;
1533 tem_safe_polled_write(pvc->vc_tem, &c, 1);
1537 * These are for systems without OBP, and for devices that cannot be
1538 * shared between Solaris and the OBP.
1540 static int
1541 wc_polled_getchar(cons_polledio_arg_t arg)
1543 struct wscons_state *wscons = (struct wscons_state *)arg;
1545 if (wscons->wc_kb_polledio == NULL) {
1546 prom_printf("wscons: getchar with no keyboard support");
1547 prom_printf("Halted...");
1548 for (;;)
1549 /* HANG FOREVER */;
1552 return (wscons->wc_kb_polledio->cons_polledio_getchar(
1553 wscons->wc_kb_polledio->cons_polledio_argument));
1556 static boolean_t
1557 wc_polled_ischar(cons_polledio_arg_t arg)
1559 struct wscons_state *wscons = (struct wscons_state *)arg;
1561 if (wscons->wc_kb_polledio == NULL)
1562 return (B_FALSE);
1564 return (wscons->wc_kb_polledio->cons_polledio_ischar(
1565 wscons->wc_kb_polledio->cons_polledio_argument));
1568 static void
1569 wc_polled_enter(cons_polledio_arg_t arg)
1571 struct wscons_state *wscons = (struct wscons_state *)arg;
1573 if (wscons->wc_kb_polledio == NULL)
1574 return;
1576 if (wscons->wc_kb_polledio->cons_polledio_enter != NULL) {
1577 wscons->wc_kb_polledio->cons_polledio_enter(
1578 wscons->wc_kb_polledio->cons_polledio_argument);
1582 static void
1583 wc_polled_exit(cons_polledio_arg_t arg)
1585 struct wscons_state *wscons = (struct wscons_state *)arg;
1587 if (wscons->wc_kb_polledio == NULL)
1588 return;
1590 if (wscons->wc_kb_polledio->cons_polledio_exit != NULL) {
1591 wscons->wc_kb_polledio->cons_polledio_exit(
1592 wscons->wc_kb_polledio->cons_polledio_argument);
1597 #ifdef DEBUG
1598 static void
1599 wc_dprintf(const char *fmt, ...)
1601 char buf[256];
1602 va_list ap;
1604 va_start(ap, fmt);
1605 (void) vsprintf(buf, fmt, ap);
1606 va_end(ap);
1608 cmn_err(CE_WARN, "wc: %s", buf);
1610 #endif
1612 /*ARGSUSED*/
1613 static void
1614 update_property(vc_state_t *pvc, char *name, ushort_t value)
1616 char data[8];
1618 (void) snprintf(data, sizeof (data), "%u", value);
1620 (void) ddi_prop_update_string(wscons.wc_dev, wc_dip, name, data);
1624 * Gets the number of text rows and columns and the
1625 * width and height (in pixels) of the console.
1627 void
1628 wc_get_size(vc_state_t *pvc)
1630 struct winsize *t = &pvc->vc_ttycommon.t_size;
1631 ushort_t r = LOSCREENLINES, c = LOSCREENCOLS, x = 0, y = 0;
1633 if (pvc->vc_tem != NULL)
1634 tem_get_size(&r, &c, &x, &y);
1635 #ifdef _HAVE_TEM_FIRMWARE
1636 else
1637 console_get_size(&r, &c, &x, &y);
1638 #endif /* _HAVE_TEM_FIRMWARE */
1640 mutex_enter(&pvc->vc_ttycommon.t_excl);
1641 t->ws_col = c;
1642 t->ws_row = r;
1643 t->ws_xpixel = x;
1644 t->ws_ypixel = y;
1645 mutex_exit(&pvc->vc_ttycommon.t_excl);
1647 if (pvc->vc_minor != 0)
1648 return;
1650 /* only for the wscons:0 */
1651 update_property(pvc, "screen-#cols", c);
1652 update_property(pvc, "screen-#rows", r);
1653 update_property(pvc, "screen-width", x);
1654 update_property(pvc, "screen-height", y);
1657 /*ARGSUSED*/
1658 static void
1659 wc_modechg_cb(tem_modechg_cb_arg_t arg)
1661 minor_t index;
1662 vc_state_t *pvc;
1664 mutex_enter(&vc_lock);
1665 for (index = 0; index < VC_INSTANCES_COUNT; index++) {
1666 pvc = vt_minor2vc(index);
1668 mutex_enter(&pvc->vc_state_lock);
1670 if ((pvc->vc_flags & WCS_ISOPEN) &&
1671 (pvc->vc_flags & WCS_INIT))
1672 wc_get_size(pvc);
1674 mutex_exit(&pvc->vc_state_lock);
1676 mutex_exit(&vc_lock);