cons.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* $OpenBSD: cons.c,v 1.14 2010/05/09 15:30:28 jsg Exp $ */
  2. /*
  3. * Copyright (c) 1988 University of Utah.
  4. * Copyright (c) 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * the Systems Programming Group of the University of Utah Computer
  9. * Science Department.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * form: OpenBSD: cons.c,v 1.7 1996/04/21 22:19:48
  36. * from: OpenBSD: cninit.c,v 1.2 1996/03/30 02:03:45
  37. * from: Utah $Hdr: cons.c 1.7 92/01/21$
  38. *
  39. * @(#)cons.c 8.2 (Berkeley) 1/12/94
  40. */
  41. #include <sys/param.h>
  42. #include "stand.h"
  43. #include <dev/cons.h>
  44. extern struct consdev constab[];
  45. void
  46. cninit(void)
  47. {
  48. struct consdev *cp;
  49. /*
  50. * Collect information about all possible consoles
  51. * and find the one with highest priority
  52. */
  53. for (cp = constab; cp->cn_probe; cp++) {
  54. (*cp->cn_probe)(cp);
  55. if (cp->cn_pri != CN_DEAD &&
  56. (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
  57. cn_tab = cp;
  58. }
  59. /*
  60. * No console, we can handle it
  61. */
  62. if ((cp = cn_tab) == NULL)
  63. return;
  64. /*
  65. * Turn on console
  66. */
  67. (*cp->cn_init)(cp);
  68. }
  69. int
  70. cnset(dev_t dev)
  71. {
  72. struct consdev *cp;
  73. /*
  74. * Look for the specified console device and use it.
  75. */
  76. for (cp = constab; cp->cn_probe; cp++) {
  77. if (major(cp->cn_dev) == major(dev)) {
  78. /* short-circuit noop */
  79. if (cp == cn_tab && cp->cn_dev == dev)
  80. return (0);
  81. if (cp->cn_pri != CN_DEAD) {
  82. cn_tab = cp;
  83. cp->cn_dev = dev;
  84. /* Turn it on. */
  85. (*cp->cn_init)(cp);
  86. return (0);
  87. }
  88. break;
  89. }
  90. }
  91. return (1);
  92. }
  93. int
  94. cngetc(void)
  95. {
  96. if (cn_tab == NULL)
  97. return (0);
  98. return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
  99. }
  100. void
  101. cnputc(int c)
  102. {
  103. if (cn_tab != NULL && c) {
  104. (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
  105. if (c == '\n')
  106. (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
  107. }
  108. }
  109. int
  110. cnischar(void)
  111. {
  112. if (cn_tab != NULL)
  113. return ((*cn_tab->cn_getc)(cn_tab->cn_dev|0x80));
  114. return 0;
  115. }