syscons_isa.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer as
  12. * the first lines of this file unmodified.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. #include "opt_syscons.h"
  30. #include <sys/param.h>
  31. #include <sys/systm.h>
  32. #include <sys/kernel.h>
  33. #include <sys/module.h>
  34. #include <sys/bus.h>
  35. #include <sys/cons.h>
  36. #include <sys/kbio.h>
  37. #include <sys/consio.h>
  38. #include <sys/sysctl.h>
  39. #if defined(__i386__) || defined(__amd64__)
  40. #include <machine/clock.h>
  41. #include <machine/md_var.h>
  42. #include <machine/pc/bios.h>
  43. #include <vm/vm.h>
  44. #include <vm/pmap.h>
  45. #include <vm/vm_param.h>
  46. #define BIOS_CLKED (1 << 6)
  47. #define BIOS_NLKED (1 << 5)
  48. #define BIOS_SLKED (1 << 4)
  49. #define BIOS_ALKED 0
  50. #endif
  51. #include <dev/syscons/syscons.h>
  52. #include <isa/isavar.h>
  53. static sc_softc_t main_softc;
  54. static void
  55. scidentify(driver_t *driver, device_t parent)
  56. {
  57. BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
  58. }
  59. static int
  60. scprobe(device_t dev)
  61. {
  62. /* No pnp support */
  63. if (isa_get_vendorid(dev))
  64. return (ENXIO);
  65. device_set_desc(dev, "System console");
  66. return (sc_probe_unit(device_get_unit(dev), device_get_flags(dev)));
  67. }
  68. static int
  69. scattach(device_t dev)
  70. {
  71. return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) |
  72. SC_AUTODETECT_KBD));
  73. }
  74. int
  75. sc_max_unit(void)
  76. {
  77. return (devclass_get_maxunit(devclass_find("sc")));
  78. }
  79. sc_softc_t
  80. *sc_get_softc(int unit, int flags)
  81. {
  82. sc_softc_t *sc;
  83. if (unit < 0)
  84. return (NULL);
  85. if ((flags & SC_KERNEL_CONSOLE) != 0) {
  86. /* FIXME: clear if it is wired to another unit! */
  87. sc = &main_softc;
  88. } else {
  89. sc = device_get_softc(devclass_get_device(devclass_find("sc"),
  90. unit));
  91. if (sc == NULL)
  92. return (NULL);
  93. }
  94. sc->unit = unit;
  95. if ((sc->flags & SC_INIT_DONE) == 0) {
  96. sc->adapter = -1;
  97. sc->cursor_char = SC_CURSOR_CHAR;
  98. sc->mouse_char = SC_MOUSE_CHAR;
  99. }
  100. return (sc);
  101. }
  102. sc_softc_t
  103. *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
  104. {
  105. devclass_t dc;
  106. sc_softc_t *sc;
  107. int i;
  108. int units;
  109. sc = &main_softc;
  110. if ((adp == NULL || adp == sc->adp) &&
  111. (kbd == NULL || kbd == sc->kbd))
  112. return (sc);
  113. dc = devclass_find("sc");
  114. units = devclass_get_maxunit(dc);
  115. for (i = 0; i < units; ++i) {
  116. sc = device_get_softc(devclass_get_device(dc, i));
  117. if (sc == NULL)
  118. continue;
  119. if ((adp == NULL || adp == sc->adp) &&
  120. (kbd == NULL || kbd == sc->kbd))
  121. return (sc);
  122. }
  123. return (NULL);
  124. }
  125. int
  126. sc_get_cons_priority(int *unit, int *flags)
  127. {
  128. const char *at;
  129. int f, u;
  130. *unit = -1;
  131. for (u = 0; u < 16; u++) {
  132. if (resource_disabled(SC_DRIVER_NAME, u))
  133. continue;
  134. if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
  135. continue;
  136. if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
  137. f = 0;
  138. if (f & SC_KERNEL_CONSOLE) {
  139. /* the user designates this unit to be the console */
  140. *unit = u;
  141. *flags = f;
  142. break;
  143. }
  144. if (*unit < 0) {
  145. /* ...otherwise remember the first found unit */
  146. *unit = u;
  147. *flags = f;
  148. }
  149. }
  150. if (*unit < 0) {
  151. *unit = 0;
  152. *flags = 0;
  153. }
  154. #if 0
  155. return ((*flags & SC_KERNEL_CONSOLE) != 0 ? CN_INTERNAL : CN_NORMAL);
  156. #endif
  157. return (CN_INTERNAL);
  158. }
  159. void
  160. sc_get_bios_values(bios_values_t *values)
  161. {
  162. #if defined(__i386__) || defined(__amd64__)
  163. uint8_t shift;
  164. shift = *(uint8_t *)BIOS_PADDRTOVADDR(0x417);
  165. values->shift_state = ((shift & BIOS_CLKED) != 0 ? CLKED : 0) |
  166. ((shift & BIOS_NLKED) != 0 ? NLKED : 0) |
  167. ((shift & BIOS_SLKED) != 0 ? SLKED : 0) |
  168. ((shift & BIOS_ALKED) != 0 ? ALKED : 0);
  169. #endif
  170. values->bell_pitch = BELL_PITCH;
  171. }
  172. int
  173. sc_tone(int herz)
  174. {
  175. #if defined(HAS_TIMER_SPKR)
  176. if (herz) {
  177. if (timer_spkr_acquire())
  178. return (EBUSY);
  179. timer_spkr_setfreq(herz);
  180. } else
  181. timer_spkr_release();
  182. #endif
  183. return (0);
  184. }
  185. static device_method_t sc_methods[] = {
  186. DEVMETHOD(device_identify, scidentify),
  187. DEVMETHOD(device_probe, scprobe),
  188. DEVMETHOD(device_attach, scattach),
  189. { 0, 0 }
  190. };
  191. static driver_t sc_driver = {
  192. SC_DRIVER_NAME,
  193. sc_methods,
  194. sizeof(sc_softc_t),
  195. };
  196. DRIVER_MODULE(sc, isa, sc_driver, 0, 0);