1 /*
   2  * CDDL HEADER START
   3  *
   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.
   7  *
   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  */
  21 /*
  22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 
  25 #include <sys/types.h>
  26 #include <sys/param.h>
  27 #include <sys/termios.h>
  28 #include <sys/stream.h>
  29 #include <sys/stropts.h>
  30 #include <sys/kmem.h>
  31 #include <sys/stat.h>
  32 #include <sys/sunddi.h>
  33 #include <sys/ddi.h>
  34 #include <sys/bitmap.h>
  35 #include <sys/sysmacros.h>
  36 #include <sys/ddi_impldefs.h>
  37 #include <sys/zone.h>
  38 #include <sys/thread.h>
  39 #ifdef DEBUG
  40 #include <sys/strlog.h>
  41 #endif
  42 
  43 #include <sys/consdev.h>
  44 #include <sys/console.h>
  45 #include <sys/wscons.h>
  46 #include <sys/vt_impl.h>
  47 #include <sys/note.h>
  48 #include <sys/avl.h>
  49 
  50 /* set if console driver is attached */
  51 dev_info_t *wc_dip = NULL;
  52 /* active virtual console minor number */
  53 minor_t vc_active_console = VT_MINOR_INVALID;
  54 /*
  55  * console_user symbol link minor number.
  56  * VT_MINOR_INVALID     :       /dev/console
  57  *      N               :       /dev/vt/N
  58  */
  59 minor_t vc_cons_user = VT_MINOR_INVALID;
  60 /* vc_state_t AVL tree */
  61 avl_tree_t vc_avl_root;
  62 /* virtual console global lock */
  63 kmutex_t vc_lock;
  64 
  65 _NOTE(MUTEX_PROTECTS_DATA(vc_lock, wc_dip vc_avl_root vc_active_console
  66 vc_cons_user))
  67 
  68 /*
  69  * Called from vt devname part. Checks if dip is attached. If it is,
  70  * return its major number.
  71  */
  72 major_t
  73 vt_wc_attached(void)
  74 {
  75         major_t maj = (major_t)-1;
  76 
  77         mutex_enter(&vc_lock);
  78 
  79         if (wc_dip)
  80                 maj = ddi_driver_major(wc_dip);
  81 
  82         mutex_exit(&vc_lock);
  83 
  84         return (maj);
  85 }
  86 
  87 void
  88 vt_getactive(char *buf, int buflen)
  89 {
  90         ASSERT(buf);
  91         ASSERT(buflen != 0);
  92 
  93         mutex_enter(&vc_lock);
  94 
  95         if (vc_active_console == 0 || vc_active_console == VT_MINOR_INVALID)
  96                 (void) snprintf(buf, buflen, "/dev/console");
  97         else
  98                 (void) snprintf(buf, buflen, "%u", vc_active_console);
  99 
 100         mutex_exit(&vc_lock);
 101 }
 102 
 103 void
 104 vt_getconsuser(char *buf, int buflen)
 105 {
 106         ASSERT(buf);
 107         ASSERT(buflen != 0);
 108 
 109         mutex_enter(&vc_lock);
 110 
 111         if (vc_cons_user == VT_MINOR_INVALID) {
 112                 (void) snprintf(buf, buflen, "/dev/console");
 113                 mutex_exit(&vc_lock);
 114                 return;
 115         }
 116 
 117         (void) snprintf(buf, buflen, "%u", vc_cons_user);
 118         mutex_exit(&vc_lock);
 119 }
 120 
 121 boolean_t
 122 vt_minor_valid(minor_t minor)
 123 {
 124         if (consmode == CONS_FW) {
 125                 if (minor == 0)
 126                         return (B_TRUE);
 127 
 128                 return (B_FALSE);
 129         }
 130 
 131         mutex_enter(&vc_lock);
 132         if (minor < VC_INSTANCES_COUNT) {
 133                 mutex_exit(&vc_lock);
 134                 return (B_TRUE);
 135         }
 136 
 137         mutex_exit(&vc_lock);
 138         return (B_FALSE);
 139 
 140 }