spkmodem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* console.c -- Open Firmware console for GRUB. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/term.h>
  20. #include <grub/types.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/terminfo.h>
  25. #include <grub/dl.h>
  26. #include <grub/speaker.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. extern struct grub_terminfo_output_state grub_spkmodem_terminfo_output;
  29. static void
  30. make_tone (grub_uint16_t freq_count, unsigned int duration)
  31. {
  32. /* Program timer 2. */
  33. grub_outb (GRUB_PIT_CTRL_SELECT_2
  34. | GRUB_PIT_CTRL_READLOAD_WORD
  35. | GRUB_PIT_CTRL_SQUAREWAVE_GEN
  36. | GRUB_PIT_CTRL_COUNT_BINARY, GRUB_PIT_CTRL);
  37. grub_outb (freq_count & 0xff, GRUB_PIT_COUNTER_2); /* LSB */
  38. grub_outb ((freq_count >> 8) & 0xff, GRUB_PIT_COUNTER_2); /* MSB */
  39. /* Start speaker. */
  40. grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
  41. | GRUB_PIT_SPK_TMR2 | GRUB_PIT_SPK_DATA,
  42. GRUB_PIT_SPEAKER_PORT);
  43. for (; duration; duration--)
  44. {
  45. unsigned short counter, previous_counter = 0xffff;
  46. while (1)
  47. {
  48. counter = grub_inb (GRUB_PIT_COUNTER_2);
  49. counter |= ((grub_uint16_t) grub_inb (GRUB_PIT_COUNTER_2)) << 8;
  50. if (counter > previous_counter)
  51. {
  52. previous_counter = counter;
  53. break;
  54. }
  55. previous_counter = counter;
  56. }
  57. }
  58. }
  59. static int inited;
  60. static void
  61. put (struct grub_term_output *term __attribute__ ((unused)), const int c)
  62. {
  63. int i;
  64. make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 200, 4);
  65. for (i = 7; i >= 0; i--)
  66. {
  67. if ((c >> i) & 1)
  68. make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 2000, 20);
  69. else
  70. make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 4000, 40);
  71. make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 1000, 10);
  72. }
  73. make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 200, 0);
  74. }
  75. static grub_err_t
  76. grub_spkmodem_init_output (struct grub_term_output *term)
  77. {
  78. /* Some models shutdown sound when not in use and it takes for it
  79. around 30 ms to come back on which loses 3 bits. So generate a base
  80. 200 Hz continously. */
  81. make_tone (GRUB_SPEAKER_PIT_FREQUENCY / 200, 0);
  82. grub_terminfo_output_init (term);
  83. return 0;
  84. }
  85. static grub_err_t
  86. grub_spkmodem_fini_output (struct grub_term_output *term __attribute__ ((unused)))
  87. {
  88. grub_speaker_beep_off ();
  89. inited = 0;
  90. return 0;
  91. }
  92. struct grub_terminfo_output_state grub_spkmodem_terminfo_output =
  93. {
  94. .put = put,
  95. .size = { 80, 24 }
  96. };
  97. static struct grub_term_output grub_spkmodem_term_output =
  98. {
  99. .name = "spkmodem",
  100. .init = grub_spkmodem_init_output,
  101. .fini = grub_spkmodem_fini_output,
  102. .putchar = grub_terminfo_putchar,
  103. .getxy = grub_terminfo_getxy,
  104. .getwh = grub_terminfo_getwh,
  105. .gotoxy = grub_terminfo_gotoxy,
  106. .cls = grub_terminfo_cls,
  107. .setcolorstate = grub_terminfo_setcolorstate,
  108. .setcursor = grub_terminfo_setcursor,
  109. .flags = GRUB_TERM_CODE_TYPE_ASCII,
  110. .data = &grub_spkmodem_terminfo_output,
  111. .progress_update_divisor = GRUB_PROGRESS_NO_UPDATE
  112. };
  113. GRUB_MOD_INIT (spkmodem)
  114. {
  115. grub_term_register_output ("spkmodem", &grub_spkmodem_term_output);
  116. }
  117. GRUB_MOD_FINI (spkmodem)
  118. {
  119. grub_term_unregister_output (&grub_spkmodem_term_output);
  120. }