blob: 48970bd69633cf38d218af3e1081e8e6a5e8e5e2 [file] [log] [blame]
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
lq150181fea9cb92006-01-12 18:17:46 -08005 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07007 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
lq150181fea9cb92006-01-12 18:17:46 -080021
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070022/*
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +080023 * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070024 */
25
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070026/*
27 * "Workstation console" multiplexor driver for Sun.
28 *
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.
32 */
33
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080034/*
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:
41 *
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +080042 * - 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.
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080045 *
46 * - wc_modechg_cb() is a callback function which will triggered when
47 * framebuffer display mode is changed.
48 *
49 * - vt_send_hotkeys() is triggered by timeout() which is not STREAMS MT
50 * safe.
51 *
52 * Based on the fact that virtual console devname part and wc_modechg_cb()
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +080053 * only do read access to the above mentioned shared four global variables,
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080054 * It is safe to do locking this way:
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +080055 * 1) all read access to the four global variables in THIS WC MODULE do not
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080056 * need locking;
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +080057 * 2) all write access to the four global variables in THIS WC MODULE must
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080058 * hold vc_lock;
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +080059 * 3) any access to the four global variables in either DEVNAME PART or the
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080060 * 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.
65 *
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.
73 *
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.
77 *
78 * Lock order: vc_lock -> vc_state_t::vc_state_lock.
79 * No overlap between vc_lock and vt_pending_vtno_lock.
80 */
81
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070082#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>
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +080097#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>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700105#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>
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800112#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>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700118#include <sys/ddi.h>
119#include <sys/sunddi.h>
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800120#include <sys/sunndi.h>
121#include <sys/esunddi.h>
122#include <sys/sunldi.h>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700123#include <sys/debug.h>
124#include <sys/console.h>
125#include <sys/ddi_impldefs.h>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700126#include <sys/policy.h>
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800127#include <sys/modctl.h>
lq150181fea9cb92006-01-12 18:17:46 -0800128#include <sys/tem.h>
129#include <sys/wscons.h>
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800130#include <sys/vt_impl.h>
131
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))
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700139
140#define MINLINES 10
141#define MAXLINES 48
142#define LOSCREENLINES 34
143#define HISCREENLINES 48
144
145#define MINCOLS 10
146#define MAXCOLS 120
147#define LOSCREENCOLS 80
148#define HISCREENCOLS 120
149
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800150struct wscons_state {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700151 dev_t wc_dev; /* major/minor for this device */
lq150181fea9cb92006-01-12 18:17:46 -0800152#ifdef _HAVE_TEM_FIRMWARE
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700153 int wc_defer_output; /* set if output device is "slow" */
lq150181fea9cb92006-01-12 18:17:46 -0800154#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700155 queue_t *wc_kbdqueue; /* "console keyboard" device queue */
156 /* below us */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700157 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 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800160 queue_t *wc_pending_wq;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700161 mblk_t *wc_pending_link; /* I_PLINK pending for kb polledio */
162} wscons;
163
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800164/*
165 * This module has a D_MTPERMOD inner perimeter, so we don't need to protect
166 * the variables only shared within this module
167 */
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))
177
178#ifdef _HAVE_TEM_FIRMWARE
179ssize_t wc_cons_wrtvec(promif_redir_arg_t arg, uchar_t *s, size_t n);
180#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700181
182static int wcopen(queue_t *, dev_t *, int, int, cred_t *);
183static int wcclose(queue_t *, int, cred_t *);
184static int wcuwput(queue_t *, mblk_t *);
185static int wclrput(queue_t *, mblk_t *);
186
187static struct module_info wcm_info = {
188 0,
189 "wc",
190 0,
191 INFPSZ,
192 2048,
193 128
194};
195
196static struct qinit wcurinit = {
197 putq,
198 NULL,
199 wcopen,
200 wcclose,
201 NULL,
202 &wcm_info,
203 NULL
204};
205
206static struct qinit wcuwinit = {
207 wcuwput,
208 NULL,
209 wcopen,
210 wcclose,
211 NULL,
212 &wcm_info,
213 NULL
214};
215
216static struct qinit wclrinit = {
217 wclrput,
218 NULL,
219 NULL,
220 NULL,
221 NULL,
222 &wcm_info,
223 NULL
224};
225
226/*
227 * We always putnext directly to the underlying queue.
228 */
229static struct qinit wclwinit = {
230 NULL,
231 NULL,
232 NULL,
233 NULL,
234 NULL,
235 &wcm_info,
236 NULL
237};
238
239static struct streamtab wcinfo = {
240 &wcurinit,
241 &wcuwinit,
242 &wclrinit,
243 &wclwinit,
244};
245
246static int wc_info(dev_info_t *, ddi_info_cmd_t, void *, void **result);
247static int wc_attach(dev_info_t *, ddi_attach_cmd_t);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700248
249DDI_DEFINE_STREAM_OPS(wc_ops, nulldev, nulldev, wc_attach, nodev, nodev,
Sherry Moore19397402008-09-22 16:30:26 -0700250 wc_info, D_MTPERMOD | D_MP, &wcinfo, ddi_quiesce_not_supported);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700251
252static void wcreioctl(void *);
253static void wcioctl(queue_t *, mblk_t *);
lq150181fea9cb92006-01-12 18:17:46 -0800254#ifdef _HAVE_TEM_FIRMWARE
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700255static void wcopoll(void *);
256static void wconsout(void *);
lq150181fea9cb92006-01-12 18:17:46 -0800257#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700258static void wcrstrt(void *);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800259static void wcstart(void *);
260static void wc_open_kb_polledio(struct wscons_state *wc, queue_t *q,
261 mblk_t *mp);
262static void wc_close_kb_polledio(struct wscons_state *wc, queue_t *q,
263 mblk_t *mp);
264static void wc_polled_putchar(cons_polledio_arg_t arg,
265 unsigned char c);
lt200341281f0742006-04-07 01:23:22 -0700266static boolean_t wc_polled_ischar(cons_polledio_arg_t arg);
267static int wc_polled_getchar(cons_polledio_arg_t arg);
268static void wc_polled_enter(cons_polledio_arg_t arg);
269static void wc_polled_exit(cons_polledio_arg_t arg);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800270void wc_get_size(vc_state_t *pvc);
lq150181fea9cb92006-01-12 18:17:46 -0800271static void wc_modechg_cb(tem_modechg_cb_arg_t arg);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700272
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700273static struct dev_ops wc_ops;
274
275/*
276 * Debug printing
277 */
278#ifndef DPRINTF
279#ifdef DEBUG
280/*PRINTFLIKE1*/
281static void wc_dprintf(const char *fmt, ...) __KPRINTFLIKE(1);
282#define DPRINTF(l, m, args) \
283 (((l) >= wc_errlevel) && ((m) & wc_errmask) ? \
lq150181fea9cb92006-01-12 18:17:46 -0800284 wc_dprintf args : \
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700285 (void) 0)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700286/*
287 * Severity levels for printing
288 */
289#define PRINT_L0 0 /* print every message */
290#define PRINT_L1 1 /* debug */
291#define PRINT_L2 2 /* quiet */
292
293/*
294 * Masks
295 */
296#define PRINT_MASK_ALL 0xFFFFFFFFU
297uint_t wc_errmask = PRINT_MASK_ALL;
298uint_t wc_errlevel = PRINT_L2;
299
300#else
301#define DPRINTF(l, m, args) /* NOTHING */
302#endif
303#endif
304
305/*
306 * Module linkage information for the kernel.
307 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700308static struct modldrv modldrv = {
309 &mod_driverops, /* Type of module. This one is a pseudo driver */
Sherry Moore19397402008-09-22 16:30:26 -0700310 "Workstation multiplexer Driver 'wc'",
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700311 &wc_ops, /* driver ops */
312};
313
314static struct modlinkage modlinkage = {
315 MODREV_1,
316 &modldrv,
317 NULL
318};
319
320int
321_init(void)
322{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800323 int rc;
324 if ((rc = mod_install(&modlinkage)) == 0)
325 vt_init();
326 return (rc);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700327}
328
329int
330_fini(void)
331{
332 return (mod_remove(&modlinkage));
333}
334
335int
336_info(struct modinfo *modinfop)
337{
338 return (mod_info(&modlinkage, modinfop));
339}
340
341/*ARGSUSED*/
342static int
343wc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
344{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800345 /* create minor node for workstation hard console */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700346 if (ddi_create_minor_node(devi, "wscons", S_IFCHR,
347 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
348 ddi_remove_minor_node(devi, NULL);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800349 return (DDI_FAILURE);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700350 }
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800351
352 mutex_enter(&vc_lock);
353
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700354 wc_dip = devi;
lq150181fea9cb92006-01-12 18:17:46 -0800355
356 bzero(&(wscons.wc_polledio), sizeof (wscons.wc_polledio));
357
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800358 vt_resize(VC_DEFAULT_COUNT);
359
360 mutex_exit(&vc_lock);
361
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700362 return (DDI_SUCCESS);
363}
364
365/* ARGSUSED */
366static int
367wc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
368 void **result)
369{
370 int error;
371
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;
379 }
380 break;
381 case DDI_INFO_DEVT2INSTANCE:
382 *result = (void *)0;
383 error = DDI_SUCCESS;
384 break;
385 default:
386 error = DDI_FAILURE;
387 }
388 return (error);
389}
390
lq150181fea9cb92006-01-12 18:17:46 -0800391#ifdef _HAVE_TEM_FIRMWARE
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700392/*
393 * Output buffer. Protected by the per-module inner perimeter.
394 */
395#define MAXHIWAT 2000
396static char obuf[MAXHIWAT];
lq150181fea9cb92006-01-12 18:17:46 -0800397#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700398
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800399static void
400wc_init_polledio(void)
401{
402 static boolean_t polledio_inited = B_FALSE;
403 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data",
404 polledio_inited))
405
406 if (polledio_inited)
407 return;
408
409 polledio_inited = B_TRUE;
410
411 /*
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.
418 */
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;
428
429#ifdef _HAVE_TEM_FIRMWARE
430 /*
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.
435 */
436 wscons.wc_defer_output = prom_stdout_is_framebuffer();
437#endif /* _HAVE_TEM_FIRMWARE */
438}
439
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700440/*ARGSUSED*/
441static int
442wcopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
443{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800444 int minor;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700445
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800446 wc_init_polledio();
447 minor = (int)getminor(*devp);
448 return (vt_open(minor, q, crp));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700449}
450
451/*ARGSUSED*/
452static int
453wcclose(queue_t *q, int flag, cred_t *crp)
454{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800455 vc_state_t *pvc = (vc_state_t *)q->q_ptr;
456
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700457 qprocsoff(q);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800458
459 mutex_enter(&vc_lock);
460
rui zang - Sun Microsystems - Beijing Chinaceeba6f2010-04-21 13:58:16 +0800461 /*
462 * If we are closing the VT node which
463 * /dev/vt/console_user points to, revert
464 * /dev/vt/console to /dev/console
465 */
466 if (vc_cons_user == pvc->vc_minor)
467 vc_cons_user = VT_MINOR_INVALID;
468
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800469 if (pvc->vc_minor == 0 || pvc->vc_minor == vc_active_console) {
470
471 /*
472 * If we lose the system console,
473 * no any other active consoles.
474 */
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;
478 }
479
480 /*
481 * just clean for our primary console
482 * and active console
483 */
484 mutex_enter(&pvc->vc_state_lock);
485 vt_clean(q, pvc);
486 mutex_exit(&pvc->vc_state_lock);
487
488 mutex_exit(&vc_lock);
489
490 return (0);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700491 }
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800492 vt_close(q, pvc, crp);
493
494 mutex_exit(&vc_lock);
495
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700496 return (0);
497}
498
499/*
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.
505 */
506static int
507wcuwput(queue_t *q, mblk_t *mp)
508{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800509 vc_state_t *pvc = (vc_state_t *)q->q_ptr;
510
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700511 switch (mp->b_datap->db_type) {
512
513 case M_STOP:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800514 mutex_enter(&pvc->vc_state_lock);
515 pvc->vc_flags |= WCS_STOPPED;
516 mutex_exit(&pvc->vc_state_lock);
517
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700518 freemsg(mp);
519 break;
520
521 case M_START:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800522 mutex_enter(&pvc->vc_state_lock);
523 pvc->vc_flags &= ~WCS_STOPPED;
524 mutex_exit(&pvc->vc_state_lock);
525
526 wcstart(pvc);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700527 freemsg(mp);
528 break;
529
530 case M_IOCTL: {
531 struct iocblk *iocp;
532 struct linkblk *linkp;
533
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800534 iocp = (struct iocblk *)(void *)mp->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700535 switch (iocp->ioc_cmd) {
536
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);
543 }
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800544 linkp = (struct linkblk *)(void *)mp->b_cont->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700545 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;
550
551 case I_UNLINK: /* stupid, but permitted */
552 case I_PUNLINK:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800553 linkp = (struct linkblk *)(void *)mp->b_cont->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700554 if (wscons.wc_kbdqueue != WR(linkp->l_qbot)) {
555 /* not us */
556 miocnak(q, mp, 0, EINVAL);
557 return (0);
558 }
559
560 mp->b_datap->db_type = M_IOCACK;
561 iocp->ioc_count = 0;
562 wc_close_kb_polledio(&wscons, q, mp);
563 break;
564
565 case TCSETSW:
566 case TCSETSF:
567 case TCSETAW:
568 case TCSETAF:
569 case TCSBRK:
570 /*
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.
577 */
578 (void) putq(q, mp);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800579 wcstart(pvc);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700580 break;
581
582 case CONSSETABORTENABLE:
583 case CONSGETABORTENABLE:
584 case KIOCSDIRECT:
585 if (wscons.wc_kbdqueue != NULL) {
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800586 wscons.wc_pending_wq = q;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700587 (void) putnext(wscons.wc_kbdqueue, mp);
588 break;
589 }
590 /* fall through */
591
592 default:
593 /*
594 * Do it now.
595 */
596 wcioctl(q, mp);
597 break;
598 }
599 break;
600 }
601
602 case M_FLUSH:
603 if (*mp->b_rptr & FLUSHW) {
604 /*
605 * Flush our write queue.
606 */
607 flushq(q, FLUSHDATA); /* XXX doesn't flush M_DELAY */
608 *mp->b_rptr &= ~FLUSHW; /* it has been flushed */
609 }
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;
616
617 case M_BREAK:
618 /*
619 * Ignore these, as they make no sense.
620 */
621 freemsg(mp);
622 break;
623
624 case M_DELAY:
625 case M_DATA:
626 /*
627 * Queue the message up to be transmitted,
628 * and poke the start routine.
629 */
630 (void) putq(q, mp);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800631 wcstart(pvc);
632 break;
633
634 case M_IOCDATA:
635 vt_miocdata(q, mp);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700636 break;
637
638 default:
639 /*
640 * "No, I don't want a subscription to Chain Store Age,
641 * thank you anyway."
642 */
643 freemsg(mp);
644 break;
645 }
646
647 return (0);
648}
649
650/*
651 * Retry an "ioctl", now that "qbufcall" claims we may be able to allocate
652 * the buffer we need.
653 */
654/*ARGSUSED*/
655static void
656wcreioctl(void *arg)
657{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800658 vc_state_t *pvc = (vc_state_t *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700659 queue_t *q;
660 mblk_t *mp;
661
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800662 pvc->vc_bufcallid = 0;
663 q = pvc->vc_ttycommon.t_writeq;
664 if ((mp = pvc->vc_ttycommon.t_iocpending) != NULL) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700665 /* not pending any more */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800666 pvc->vc_ttycommon.t_iocpending = NULL;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700667 wcioctl(q, mp);
668 }
669}
670
lq150181fea9cb92006-01-12 18:17:46 -0800671static int
672wc_getterm(mblk_t *mp)
673{
674 char *term;
675 intptr_t arg;
gd7805922eb7cb2008-07-01 11:22:16 -0700676 int flag = ((struct iocblk *)(void *)mp->b_rptr)->ioc_flag;
lq150181fea9cb92006-01-12 18:17:46 -0800677
678 STRUCT_DECL(cons_getterm, wcterm);
679 STRUCT_INIT(wcterm, flag);
680
gd7805922eb7cb2008-07-01 11:22:16 -0700681 arg = *((intptr_t *)(void *)mp->b_cont->b_rptr);
lq150181fea9cb92006-01-12 18:17:46 -0800682
683 if (ddi_copyin((void *)arg, STRUCT_BUF(wcterm),
684 STRUCT_SIZE(wcterm), flag) != 0) {
685 return (EFAULT);
686 }
687
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";
695 }
696
697 if (STRUCT_FGET(wcterm, cn_term_len) <
698 strlen(term) + 1) {
699 return (EOVERFLOW);
700 }
701
702 if (ddi_copyout(term,
703 STRUCT_FGETP(wcterm, cn_term_type),
704 strlen(term) + 1, flag) != 0) {
705 return (EFAULT);
706 }
707
708 return (0);
709}
710
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700711/*
712 * Process an "ioctl" message sent down to us.
713 */
714static void
715wcioctl(queue_t *q, mblk_t *mp)
716{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800717 vc_state_t *pvc = (vc_state_t *)q->q_ptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700718 struct iocblk *iocp;
719 size_t datasize;
720 int error;
lq150181fea9cb92006-01-12 18:17:46 -0800721 long len;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700722
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800723 iocp = (struct iocblk *)(void *)mp->b_rptr;
724
725 if ((iocp->ioc_cmd & VTIOC) == VTIOC ||
726 (iocp->ioc_cmd & KDIOC) == KDIOC) {
727 vt_ioctl(q, mp);
728 return;
729 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700730
731 switch (iocp->ioc_cmd) {
732 case TIOCSWINSZ:
733 /*
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.
743 */
744 iocp->ioc_count = 0; /* no data returned */
745 mp->b_datap->db_type = M_IOCACK;
746 qreply(q, mp);
747 return;
748
749 case CONSOPENPOLLEDIO:
750 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -0700751 ("wcioctl: CONSOPENPOLLEDIO\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700752
753 error = miocpullup(mp, sizeof (struct cons_polledio *));
754 if (error != 0) {
755 miocnak(q, mp, 0, error);
756 return;
757 }
758
759 /*
lq150181fea9cb92006-01-12 18:17:46 -0800760 * We are given an appropriate-sized data block,
761 * and return a pointer to our structure in it.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700762 */
lq150181fea9cb92006-01-12 18:17:46 -0800763 if (consmode == CONS_KFB)
764 wscons.wc_polledio.cons_polledio_putchar =
765 wc_polled_putchar;
gd7805922eb7cb2008-07-01 11:22:16 -0700766 *(struct cons_polledio **)(void *)mp->b_cont->b_rptr =
767 &wscons.wc_polledio;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700768
lq150181fea9cb92006-01-12 18:17:46 -0800769 mp->b_datap->db_type = M_IOCACK;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700770
771 qreply(q, mp);
772 break;
773
lq150181fea9cb92006-01-12 18:17:46 -0800774 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;
780
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700781 case WC_OPEN_FB:
782 /*
783 * Start out pessimistic, so that we can just jump to
784 * the reply to bail out.
785 */
786 mp->b_datap->db_type = M_IOCNAK;
787
788 /*
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.)
794 */
795 if ((iocp->ioc_error = secpolicy_console(iocp->ioc_cr)) != 0)
796 goto open_fail;
797
798 /*
799 * Some miscellaneous checks...
800 */
801 iocp->ioc_error = EINVAL;
802
803 /*
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700804 * If we don't have exactly one continuation block, fail.
805 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800806 if (mp->b_cont == NULL ||
807 mp->b_cont->b_cont != NULL)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700808 goto open_fail;
809
810 /*
811 * If there's no null terminator in the string, fail.
812 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800813 /* LINTED E_PTRDIFF_OVERFLOW */
814 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700815 if (memchr(mp->b_cont->b_rptr, 0, len) == NULL)
816 goto open_fail;
817
818 /*
819 * NOTE: should eventually get default
820 * dimensions from a property, e.g. screen-#rows.
821 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800822 iocp->ioc_error = tem_info_init((char *)mp->b_cont->b_rptr,
823 iocp->ioc_cr);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700824 /*
825 * Of course, if the terminal emulator initialization
826 * failed, fail.
827 */
828 if (iocp->ioc_error != 0)
829 goto open_fail;
830
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800831#ifdef _HAVE_TEM_FIRMWARE
832 if (prom_stdout_is_framebuffer()) {
833 /*
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.
839 */
840 prom_set_stdout_redirect(wc_cons_wrtvec,
841 (promif_redir_arg_t)NULL);
lq150181fea9cb92006-01-12 18:17:46 -0800842
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800843 }
844#endif /* _HAVE_TEM_FIRMWARE */
845
846 tem_register_modechg_cb(wc_modechg_cb,
847 (tem_modechg_cb_arg_t)&wscons);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700848
849 /*
850 * ... and succeed.
851 */
852 mp->b_datap->db_type = M_IOCACK;
853
854open_fail:
855 qreply(q, mp);
856 break;
857
858 case WC_CLOSE_FB:
859 /*
860 * There's nothing that can call this, so it's not
861 * really implemented.
862 */
863 mp->b_datap->db_type = M_IOCNAK;
864 /*
865 * However, if it were implemented, it would clearly
866 * be root-only.
867 */
868 if ((iocp->ioc_error = secpolicy_console(iocp->ioc_cr)) != 0)
869 goto close_fail;
870
871 iocp->ioc_error = EINVAL;
872
873close_fail:
874 qreply(q, mp);
875 break;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700876
877 default:
878
879 /*
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.
890 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800891 datasize = ttycommon_ioctl(&pvc->vc_ttycommon, q, mp, &error);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700892 if (datasize != 0) {
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800893 if (pvc->vc_bufcallid != 0)
894 qunbufcall(q, pvc->vc_bufcallid);
895 pvc->vc_bufcallid = qbufcall(q, datasize, BPRI_HI,
896 wcreioctl, pvc);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700897 return;
898 }
899
900 if (error < 0) {
901 if (iocp->ioc_cmd == TCSBRK)
902 error = 0;
903 else
904 error = EINVAL;
905 }
906 if (error != 0) {
907 iocp->ioc_error = error;
908 mp->b_datap->db_type = M_IOCNAK;
909 }
910 qreply(q, mp);
911 break;
912 }
913}
914
915/*
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.
920 */
921static void
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800922wc_open_kb_polledio(struct wscons_state *wscons, queue_t *q, mblk_t *mp)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700923{
924 mblk_t *mp2;
925 struct iocblk *iocp;
926
927 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -0700928 ("wc_open_kb_polledio: sending CONSOPENPOLLEDIO\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700929
930 mp2 = mkiocb(CONSOPENPOLLEDIO);
931
932 if (mp2 == NULL) {
933 /*
934 * If we can't get an mblk, then wait for it.
935 */
936 goto nomem;
937 }
938
939 mp2->b_cont = allocb(sizeof (struct cons_polledio *), BPRI_HI);
940
941 if (mp2->b_cont == NULL) {
942 /*
943 * If we can't get an mblk, then wait for it, and release
944 * the mblk that we have already allocated.
945 */
946 freemsg(mp2);
947 goto nomem;
948 }
949
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800950 iocp = (struct iocblk *)(void *)mp2->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700951
952 iocp->ioc_count = sizeof (struct cons_polledio *);
953 mp2->b_cont->b_wptr = mp2->b_cont->b_rptr +
gd7805922eb7cb2008-07-01 11:22:16 -0700954 sizeof (struct cons_polledio *);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700955
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800956 wscons->wc_pending_wq = q;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700957 wscons->wc_pending_link = mp;
958 wscons->wc_kb_getpolledio_id = iocp->ioc_id;
959
960 putnext(wscons->wc_kbdqueue, mp2);
961
962 return;
963
964nomem:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800965 iocp = (struct iocblk *)(void *)mp->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700966 iocp->ioc_error = ENOMEM;
967 mp->b_datap->db_type = M_IOCNAK;
968 qreply(q, mp);
969}
970
971/*
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.
976 */
977static void
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +0800978wc_close_kb_polledio(struct wscons_state *wscons, queue_t *q, mblk_t *mp)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700979{
980 mblk_t *mp2;
981 struct iocblk *iocp;
982
983 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -0700984 ("wc_close_kb_polledio: sending CONSCLOSEPOLLEDIO\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700985
986 mp2 = mkiocb(CONSCLOSEPOLLEDIO);
987
988 if (mp2 == NULL) {
989 /*
990 * If we can't get an mblk, then wait for it.
991 */
992 goto nomem;
993 }
994
995 mp2->b_cont = allocb(sizeof (struct cons_polledio *), BPRI_HI);
996
997 if (mp2->b_cont == NULL) {
998 /*
999 * If we can't get an mblk, then wait for it, and release
1000 * the mblk that we have already allocated.
1001 */
1002 freemsg(mp2);
1003
1004 goto nomem;
1005 }
1006
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001007 iocp = (struct iocblk *)(void *)mp2->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001008
1009 iocp->ioc_count = 0;
1010
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001011 wscons->wc_pending_wq = q;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001012 wscons->wc_pending_link = mp;
1013 wscons->wc_kb_getpolledio_id = iocp->ioc_id;
1014
1015 putnext(wscons->wc_kbdqueue, mp2);
1016
1017 return;
1018
1019nomem:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001020 iocp = (struct iocblk *)(void *)mp->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001021 iocp->ioc_error = ENOMEM;
1022 mp->b_datap->db_type = M_IOCNAK;
1023 qreply(q, mp);
1024}
1025
lq150181fea9cb92006-01-12 18:17:46 -08001026#ifdef _HAVE_TEM_FIRMWARE
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001027/* ARGSUSED */
1028static void
1029wcopoll(void *arg)
1030{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001031 vc_state_t *pvc = (vc_state_t *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001032 queue_t *q;
1033
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001034 q = pvc->vc_ttycommon.t_writeq;
1035 pvc->vc_timeoutid = 0;
1036
1037 mutex_enter(&pvc->vc_state_lock);
1038
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001039 /* See if we can continue output */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001040 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);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001046 } else
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001047 pvc->vc_timeoutid = qtimeout(q, wcopoll, pvc, 1);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001048 }
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001049
1050 mutex_exit(&pvc->vc_state_lock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001051}
lq150181fea9cb92006-01-12 18:17:46 -08001052#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001053
1054/*
1055 * Restart output on the console after a timeout.
1056 */
1057/* ARGSUSED */
1058static void
1059wcrstrt(void *arg)
1060{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001061 vc_state_t *pvc = (vc_state_t *)arg;
1062
1063 ASSERT(pvc->vc_ttycommon.t_writeq != NULL);
1064
1065 mutex_enter(&pvc->vc_state_lock);
1066 pvc->vc_flags &= ~WCS_DELAY;
1067 mutex_exit(&pvc->vc_state_lock);
1068
1069 wcstart(pvc);
1070}
1071
1072/*
1073 * get screen terminal for current output
1074 */
1075static tem_vt_state_t
1076wc_get_screen_tem(vc_state_t *pvc)
1077{
1078 if (!tem_initialized(pvc->vc_tem) ||
1079 tem_get_fbmode(pvc->vc_tem) != KD_TEXT)
1080 return (NULL);
1081
1082 return (pvc->vc_tem);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001083}
1084
1085/*
1086 * Start console output
1087 */
1088static void
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001089wcstart(void *arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001090{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001091 vc_state_t *pvc = (vc_state_t *)arg;
1092 tem_vt_state_t ptem = NULL;
lq150181fea9cb92006-01-12 18:17:46 -08001093#ifdef _HAVE_TEM_FIRMWARE
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001094 int c;
1095 ssize_t cc;
lq150181fea9cb92006-01-12 18:17:46 -08001096#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001097 queue_t *q;
1098 mblk_t *bp;
1099 mblk_t *nbp;
1100
1101 /*
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.
1106 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001107 if (pvc->vc_flags & (WCS_DELAY|WCS_BUSY|WCS_STOPPED))
lq150181fea9cb92006-01-12 18:17:46 -08001108 return;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001109
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001110 q = pvc->vc_ttycommon.t_writeq;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001111 /*
1112 * assumes that we have been called by whoever holds the
1113 * exclusionary lock on the write-side queue (protects
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001114 * vc_flags and vc_pendc).
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001115 */
1116 for (;;) {
1117 if ((bp = getq(q)) == NULL)
lq150181fea9cb92006-01-12 18:17:46 -08001118 return; /* nothing to transmit */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001119
1120 /*
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.
1125 */
1126 switch (bp->b_datap->db_type) {
1127
1128 case M_DELAY:
1129 /*
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.
1133 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001134 if (pvc->vc_timeoutid != 0)
1135 (void) quntimeout(q, pvc->vc_timeoutid);
1136 pvc->vc_timeoutid = qtimeout(q, wcrstrt, pvc,
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001137 (clock_t)(*(unsigned char *)bp->b_rptr + 6));
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001138
1139 mutex_enter(&pvc->vc_state_lock);
1140 pvc->vc_flags |= WCS_DELAY;
1141 mutex_exit(&pvc->vc_state_lock);
1142
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001143 freemsg(bp);
lq150181fea9cb92006-01-12 18:17:46 -08001144 return; /* wait for this to finish */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001145
1146 case M_IOCTL:
1147 /*
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.
1151 */
1152 wcioctl(q, bp);
1153 continue;
1154 }
1155
lq150181fea9cb92006-01-12 18:17:46 -08001156#ifdef _HAVE_TEM_FIRMWARE
1157 if (consmode == CONS_KFB) {
1158#endif /* _HAVE_TEM_FIRMWARE */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001159 if ((ptem = wc_get_screen_tem(pvc)) != NULL) {
1160
lq150181fea9cb92006-01-12 18:17:46 -08001161 for (nbp = bp; nbp != NULL; nbp = nbp->b_cont) {
1162 if (nbp->b_wptr > nbp->b_rptr) {
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001163 (void) tem_write(ptem,
lq150181fea9cb92006-01-12 18:17:46 -08001164 nbp->b_rptr,
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001165 /* LINTED */
1166 nbp->b_wptr - nbp->b_rptr,
lq150181fea9cb92006-01-12 18:17:46 -08001167 kcred);
1168 }
1169 }
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001170
lq150181fea9cb92006-01-12 18:17:46 -08001171 }
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001172
1173 freemsg(bp);
1174
lq150181fea9cb92006-01-12 18:17:46 -08001175#ifdef _HAVE_TEM_FIRMWARE
1176 continue;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001177 }
lq150181fea9cb92006-01-12 18:17:46 -08001178
1179 /* consmode = CONS_FW */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001180 if (pvc->vc_minor != 0) {
1181 freemsg(bp);
1182 continue;
1183 }
1184
1185 /* LINTED E_PTRDIFF_OVERFLOW */
1186 if ((cc = bp->b_wptr - bp->b_rptr) == 0) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001187 freemsg(bp);
1188 continue;
1189 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001190 /*
1191 * Direct output to the frame buffer if this device
1192 * is not the "hardware" console.
1193 */
1194 if (wscons.wc_defer_output) {
1195 /*
1196 * Never do output here;
1197 * it takes forever.
1198 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001199 mutex_enter(&pvc->vc_state_lock);
1200 pvc->vc_flags |= WCS_BUSY;
1201 mutex_exit(&pvc->vc_state_lock);
1202
1203 pvc->vc_pendc = -1;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001204 (void) putbq(q, bp);
lq150181fea9cb92006-01-12 18:17:46 -08001205 if (q->q_count > 128) { /* do it soon */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001206 softcall(wconsout, pvc);
lq150181fea9cb92006-01-12 18:17:46 -08001207 } else { /* wait a bit */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001208 if (pvc->vc_timeoutid != 0)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001209 (void) quntimeout(q,
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001210 pvc->vc_timeoutid);
1211 pvc->vc_timeoutid = qtimeout(q, wconsout,
1212 pvc, hz / 30);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001213 }
lq150181fea9cb92006-01-12 18:17:46 -08001214 return;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001215 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001216 for (;;) {
1217 c = *bp->b_rptr++;
1218 cc--;
1219 if (prom_mayput((char)c) != 0) {
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001220
1221 mutex_enter(&pvc->vc_state_lock);
1222 pvc->vc_flags |= WCS_BUSY;
1223 mutex_exit(&pvc->vc_state_lock);
1224
1225 pvc->vc_pendc = c;
1226 if (pvc->vc_timeoutid != 0)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001227 (void) quntimeout(q,
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001228 pvc->vc_timeoutid);
1229 pvc->vc_timeoutid = qtimeout(q, wcopoll,
1230 pvc, 1);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001231 if (bp != NULL)
lq150181fea9cb92006-01-12 18:17:46 -08001232 /* not done with this message yet */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001233 (void) putbq(q, bp);
lq150181fea9cb92006-01-12 18:17:46 -08001234 return;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001235 }
1236 while (cc <= 0) {
1237 nbp = bp;
1238 bp = bp->b_cont;
1239 freeb(nbp);
1240 if (bp == NULL)
lq150181fea9cb92006-01-12 18:17:46 -08001241 return;
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001242 /* LINTED E_PTRDIFF_OVERFLOW */
1243 cc = bp->b_wptr - bp->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001244 }
1245 }
lq150181fea9cb92006-01-12 18:17:46 -08001246#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001247 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001248}
1249
lq150181fea9cb92006-01-12 18:17:46 -08001250#ifdef _HAVE_TEM_FIRMWARE
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001251/*
1252 * Output to frame buffer console.
1253 * It takes a long time to scroll.
1254 */
1255/* ARGSUSED */
1256static void
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001257wconsout(void *arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001258{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001259 vc_state_t *pvc = (vc_state_t *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001260 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;
1267
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001268 if ((q = pvc->vc_ttycommon.t_writeq) == NULL) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001269 return; /* not attached to a stream */
1270 }
1271
1272 /*
1273 * Set up to copy up to MAXHIWAT bytes.
1274 */
1275 current_position = &obuf[0];
1276 bytes_left = MAXHIWAT;
1277 while ((bp = getq(q)) != NULL) {
1278 if (bp->b_datap->db_type == M_IOCTL) {
1279 /*
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.
1284 */
1285 (void) putbq(q, bp);
1286 goto transmit;
1287 }
1288
1289 do {
1290 cp = bp->b_rptr;
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001291 /* LINTED E_PTRDIFF_OVERFLOW */
1292 cc = bp->b_wptr - cp;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001293 while (cc != 0) {
1294 if (bytes_left == 0) {
1295 /*
1296 * Out of buffer space; put this
1297 * buffer back on the queue, and
1298 * transmit what we have.
1299 */
1300 bp->b_rptr = cp;
1301 (void) putbq(q, bp);
1302 goto transmit;
1303 }
1304 *current_position++ = *cp++;
1305 cc--;
1306 bytes_left--;
1307 }
1308 nbp = bp;
1309 bp = bp->b_cont;
1310 freeb(nbp);
1311 } while (bp != NULL);
1312 }
1313
1314transmit:
1315 if ((cc = MAXHIWAT - bytes_left) != 0)
1316 console_puts(obuf, cc);
1317
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001318 mutex_enter(&pvc->vc_state_lock);
1319 pvc->vc_flags &= ~WCS_BUSY;
1320 mutex_exit(&pvc->vc_state_lock);
1321
1322 wcstart(pvc);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001323}
lq150181fea9cb92006-01-12 18:17:46 -08001324#endif /* _HAVE_TEM_FIRMWARE */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001325
1326/*
1327 * Put procedure for lower read queue.
1328 * Pass everything up to queue above "upper half".
1329 */
1330static int
1331wclrput(queue_t *q, mblk_t *mp)
1332{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001333 vc_state_t *pvc;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001334 queue_t *upq;
1335 struct iocblk *iocp;
1336
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001337 pvc = vt_minor2vc(VT_ACTIVE);
1338
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001339 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -07001340 ("wclrput: wclrput type = 0x%x\n", mp->b_datap->db_type));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001341
1342 switch (mp->b_datap->db_type) {
1343
1344 case M_FLUSH:
1345 if (*mp->b_rptr == FLUSHW || *mp->b_rptr == FLUSHRW) {
1346 /*
1347 * Flush our write queue.
1348 */
1349 /* XXX doesn't flush M_DELAY */
1350 flushq(WR(q), FLUSHDATA);
1351 *mp->b_rptr = FLUSHR; /* it has been flushed */
1352 }
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;
1360
1361 case M_DATA:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001362 if (consmode == CONS_KFB && vt_check_hotkeys(mp)) {
1363 freemsg(mp);
1364 break;
1365 }
1366
1367 if ((upq = pvc->vc_ttycommon.t_readq) != NULL) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001368 if (!canput(upq->q_next)) {
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001369 ttycommon_qfull(&pvc->vc_ttycommon, upq);
1370 wcstart(pvc);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001371 freemsg(mp);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001372 } else {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001373 putnext(upq, mp);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001374 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001375 } else
1376 freemsg(mp);
1377 break;
1378
1379 case M_IOCACK:
1380 case M_IOCNAK:
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001381 iocp = (struct iocblk *)(void *)mp->b_rptr;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001382 if (wscons.wc_pending_link != NULL &&
1383 iocp->ioc_id == wscons.wc_kb_getpolledio_id) {
1384 switch (mp->b_datap->db_type) {
1385
1386 case M_IOCACK:
1387 switch (iocp->ioc_cmd) {
1388
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001389 case CONSOPENPOLLEDIO:
1390 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -07001391 ("wclrput: "
1392 "ACK CONSOPENPOLLEDIO\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001393 wscons.wc_kb_polledio =
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001394 *(struct cons_polledio **)
1395 (void *)mp->b_cont->b_rptr;
lq150181fea9cb92006-01-12 18:17:46 -08001396 wscons.wc_polledio.
1397 cons_polledio_getchar =
gd7805922eb7cb2008-07-01 11:22:16 -07001398 wc_polled_getchar;
lq150181fea9cb92006-01-12 18:17:46 -08001399 wscons.wc_polledio.
1400 cons_polledio_ischar =
gd7805922eb7cb2008-07-01 11:22:16 -07001401 wc_polled_ischar;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001402 break;
1403
1404 case CONSCLOSEPOLLEDIO:
1405 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -07001406 ("wclrput: "
1407 "ACK CONSCLOSEPOLLEDIO\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001408 wscons.wc_kb_polledio = NULL;
1409 wscons.wc_kbdqueue = NULL;
lq150181fea9cb92006-01-12 18:17:46 -08001410 wscons.wc_polledio.
1411 cons_polledio_getchar = NULL;
1412 wscons.wc_polledio.
1413 cons_polledio_ischar = NULL;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001414 break;
1415 default:
1416 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001417 ("wclrput: "
1418 "ACK UNKNOWN\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001419 }
1420
1421 break;
1422 case M_IOCNAK:
1423 /*
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.
1429 */
1430 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
gd7805922eb7cb2008-07-01 11:22:16 -07001431 ("wclrput: NAK\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001432
1433 switch (iocp->ioc_cmd) {
1434
1435 case CONSCLOSEPOLLEDIO:
1436 wscons.wc_kb_polledio = NULL;
1437 wscons.wc_kbdqueue = NULL;
lq150181fea9cb92006-01-12 18:17:46 -08001438 wscons.wc_polledio.
1439 cons_polledio_getchar = NULL;
1440 wscons.wc_polledio.
1441 cons_polledio_ischar = NULL;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001442 break;
1443 }
1444 break;
1445 }
1446
1447 /*
1448 * Discard the response, replace it with the
1449 * pending response to the I_PLINK, then let it
1450 * flow upward.
1451 */
1452 freemsg(mp);
1453 mp = wscons.wc_pending_link;
1454 wscons.wc_pending_link = NULL;
1455 wscons.wc_kb_getpolledio_id = 0;
1456 }
1457 /* FALLTHROUGH */
1458
1459 default: /* inc M_ERROR, M_HANGUP, M_IOCACK, M_IOCNAK, ... */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001460 if (wscons.wc_pending_wq != NULL) {
1461 qreply(wscons.wc_pending_wq, mp);
1462 wscons.wc_pending_wq = NULL;
1463 break;
1464 }
1465
1466 if ((upq = pvc->vc_ttycommon.t_readq) != NULL) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001467 putnext(upq, mp);
1468 } else {
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001469 DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1470 ("wclrput: Message DISCARDED\n"));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001471 freemsg(mp);
1472 }
1473 break;
1474 }
1475
1476 return (0);
1477}
1478
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001479#ifdef _HAVE_TEM_FIRMWARE
1480/*
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.
1488 */
1489/* ARGSUSED */
1490ssize_t
1491wc_cons_wrtvec(promif_redir_arg_t arg, uchar_t *s, size_t n)
1492{
1493 vc_state_t *pvc;
1494
1495 pvc = vt_minor2vc(VT_ACTIVE);
1496
1497 if (pvc->vc_tem == NULL)
1498 return (0);
1499
1500 ASSERT(consmode == CONS_KFB);
1501
1502 if (panicstr)
1503 polled_io_cons_write(s, n);
1504 else
1505 (void) tem_write(pvc->vc_tem, s, n, kcred);
1506
1507 return (n);
1508}
1509#endif /* _HAVE_TEM_FIRMWARE */
1510
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001511/*
lq150181fea9cb92006-01-12 18:17:46 -08001512 * These are for systems without OBP, and for devices that cannot be
1513 * shared between Solaris and the OBP.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001514 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001515static void
lt200341281f0742006-04-07 01:23:22 -07001516wc_polled_putchar(cons_polledio_arg_t arg, unsigned char c)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001517{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001518 vc_state_t *pvc;
1519
1520 pvc = vt_minor2vc(VT_ACTIVE);
1521
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001522 if (c == '\n')
lq150181fea9cb92006-01-12 18:17:46 -08001523 wc_polled_putchar(arg, '\r');
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001524
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001525 if (pvc->vc_tem == NULL) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001526 /*
1527 * We have no terminal emulator configured. We have no
1528 * recourse but to drop the output on the floor.
1529 */
1530 return;
1531 }
1532
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001533 tem_safe_polled_write(pvc->vc_tem, &c, 1);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001534}
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001535
1536/*
1537 * These are for systems without OBP, and for devices that cannot be
1538 * shared between Solaris and the OBP.
1539 */
1540static int
lt200341281f0742006-04-07 01:23:22 -07001541wc_polled_getchar(cons_polledio_arg_t arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001542{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001543 struct wscons_state *wscons = (struct wscons_state *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001544
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 */;
1550 }
1551
1552 return (wscons->wc_kb_polledio->cons_polledio_getchar(
lq150181fea9cb92006-01-12 18:17:46 -08001553 wscons->wc_kb_polledio->cons_polledio_argument));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001554}
1555
1556static boolean_t
lt200341281f0742006-04-07 01:23:22 -07001557wc_polled_ischar(cons_polledio_arg_t arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001558{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001559 struct wscons_state *wscons = (struct wscons_state *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001560
1561 if (wscons->wc_kb_polledio == NULL)
1562 return (B_FALSE);
1563
1564 return (wscons->wc_kb_polledio->cons_polledio_ischar(
lq150181fea9cb92006-01-12 18:17:46 -08001565 wscons->wc_kb_polledio->cons_polledio_argument));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001566}
1567
1568static void
lt200341281f0742006-04-07 01:23:22 -07001569wc_polled_enter(cons_polledio_arg_t arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001570{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001571 struct wscons_state *wscons = (struct wscons_state *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001572
1573 if (wscons->wc_kb_polledio == NULL)
1574 return;
1575
1576 if (wscons->wc_kb_polledio->cons_polledio_enter != NULL) {
1577 wscons->wc_kb_polledio->cons_polledio_enter(
lq150181fea9cb92006-01-12 18:17:46 -08001578 wscons->wc_kb_polledio->cons_polledio_argument);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001579 }
1580}
1581
1582static void
lt200341281f0742006-04-07 01:23:22 -07001583wc_polled_exit(cons_polledio_arg_t arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001584{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001585 struct wscons_state *wscons = (struct wscons_state *)arg;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001586
1587 if (wscons->wc_kb_polledio == NULL)
1588 return;
1589
1590 if (wscons->wc_kb_polledio->cons_polledio_exit != NULL) {
1591 wscons->wc_kb_polledio->cons_polledio_exit(
lq150181fea9cb92006-01-12 18:17:46 -08001592 wscons->wc_kb_polledio->cons_polledio_argument);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001593 }
1594}
1595
1596
1597#ifdef DEBUG
1598static void
1599wc_dprintf(const char *fmt, ...)
1600{
1601 char buf[256];
1602 va_list ap;
1603
1604 va_start(ap, fmt);
1605 (void) vsprintf(buf, fmt, ap);
1606 va_end(ap);
1607
lq150181fea9cb92006-01-12 18:17:46 -08001608 cmn_err(CE_WARN, "wc: %s", buf);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001609}
1610#endif
1611
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001612/*ARGSUSED*/
lq150181fea9cb92006-01-12 18:17:46 -08001613static void
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001614update_property(vc_state_t *pvc, char *name, ushort_t value)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001615{
lq150181fea9cb92006-01-12 18:17:46 -08001616 char data[8];
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001617
lq150181fea9cb92006-01-12 18:17:46 -08001618 (void) snprintf(data, sizeof (data), "%u", value);
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001619
1620 (void) ddi_prop_update_string(wscons.wc_dev, wc_dip, name, data);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001621}
1622
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001623/*
1624 * Gets the number of text rows and columns and the
1625 * width and height (in pixels) of the console.
1626 */
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001627void
1628wc_get_size(vc_state_t *pvc)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001629{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001630 struct winsize *t = &pvc->vc_ttycommon.t_size;
lq150181fea9cb92006-01-12 18:17:46 -08001631 ushort_t r = LOSCREENLINES, c = LOSCREENCOLS, x = 0, y = 0;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001632
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001633 if (pvc->vc_tem != NULL)
1634 tem_get_size(&r, &c, &x, &y);
lq150181fea9cb92006-01-12 18:17:46 -08001635#ifdef _HAVE_TEM_FIRMWARE
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001636 else
lq150181fea9cb92006-01-12 18:17:46 -08001637 console_get_size(&r, &c, &x, &y);
lq150181fea9cb92006-01-12 18:17:46 -08001638#endif /* _HAVE_TEM_FIRMWARE */
1639
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001640 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);
1646
1647 if (pvc->vc_minor != 0)
1648 return;
1649
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);
lq150181fea9cb92006-01-12 18:17:46 -08001655}
1656
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001657/*ARGSUSED*/
lq150181fea9cb92006-01-12 18:17:46 -08001658static void
1659wc_modechg_cb(tem_modechg_cb_arg_t arg)
1660{
rui zang - Sun Microsystems - Beijing Chinaaecfc012008-09-25 14:01:48 +08001661 minor_t index;
1662 vc_state_t *pvc;
1663
1664 mutex_enter(&vc_lock);
1665 for (index = 0; index < VC_INSTANCES_COUNT; index++) {
1666 pvc = vt_minor2vc(index);
1667
1668 mutex_enter(&pvc->vc_state_lock);
1669
1670 if ((pvc->vc_flags & WCS_ISOPEN) &&
1671 (pvc->vc_flags & WCS_INIT))
1672 wc_get_size(pvc);
1673
1674 mutex_exit(&pvc->vc_state_lock);
1675 }
1676 mutex_exit(&vc_lock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001677}