speaker.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef GRUB_SPEAKER_HEADER
  2. #define GRUB_SPEAKER_HEADER 1
  3. #include <grub/cpu/io.h>
  4. #include <grub/i386/pit.h>
  5. /* The frequency of the PIT clock. */
  6. #define GRUB_SPEAKER_PIT_FREQUENCY 0x1234dd
  7. static inline void
  8. grub_speaker_beep_off (void)
  9. {
  10. unsigned char status;
  11. status = grub_inb (GRUB_PIT_SPEAKER_PORT);
  12. grub_outb (status & ~(GRUB_PIT_SPK_TMR2 | GRUB_PIT_SPK_DATA),
  13. GRUB_PIT_SPEAKER_PORT);
  14. }
  15. static inline void
  16. grub_speaker_beep_on (grub_uint16_t pitch)
  17. {
  18. unsigned char status;
  19. unsigned int counter;
  20. if (pitch < 20)
  21. pitch = 20;
  22. else if (pitch > 20000)
  23. pitch = 20000;
  24. counter = GRUB_SPEAKER_PIT_FREQUENCY / pitch;
  25. /* Program timer 2. */
  26. grub_outb (GRUB_PIT_CTRL_SELECT_2
  27. | GRUB_PIT_CTRL_READLOAD_WORD
  28. | GRUB_PIT_CTRL_SQUAREWAVE_GEN
  29. | GRUB_PIT_CTRL_COUNT_BINARY, GRUB_PIT_CTRL);
  30. grub_outb (counter & 0xff, GRUB_PIT_COUNTER_2); /* LSB */
  31. grub_outb ((counter >> 8) & 0xff, GRUB_PIT_COUNTER_2); /* MSB */
  32. /* Start speaker. */
  33. status = grub_inb (GRUB_PIT_SPEAKER_PORT);
  34. grub_outb (status | GRUB_PIT_SPK_TMR2 | GRUB_PIT_SPK_DATA,
  35. GRUB_PIT_SPEAKER_PORT);
  36. }
  37. #endif