pcspkr.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * PC Speaker beeper driver for Linux
  3. *
  4. * Copyright (c) 2002 Vojtech Pavlik
  5. * Copyright (c) 1992 Orest Zborowski
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/input.h>
  17. #include <asm/8253pit.h>
  18. #include <asm/io.h>
  19. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  20. MODULE_DESCRIPTION("PC Speaker beeper driver");
  21. MODULE_LICENSE("GPL");
  22. static char pcspkr_name[] = "PC Speaker";
  23. static char pcspkr_phys[] = "isa0061/input0";
  24. static struct input_dev pcspkr_dev;
  25. static DEFINE_SPINLOCK(i8253_beep_lock);
  26. static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  27. {
  28. unsigned int count = 0;
  29. unsigned long flags;
  30. if (type != EV_SND)
  31. return -1;
  32. switch (code) {
  33. case SND_BELL: if (value) value = 1000;
  34. case SND_TONE: break;
  35. default: return -1;
  36. }
  37. if (value > 20 && value < 32767)
  38. count = PIT_TICK_RATE / value;
  39. spin_lock_irqsave(&i8253_beep_lock, flags);
  40. if (count) {
  41. /* enable counter 2 */
  42. outb_p(inb_p(0x61) | 3, 0x61);
  43. /* set command for counter 2, 2 byte write */
  44. outb_p(0xB6, 0x43);
  45. /* select desired HZ */
  46. outb_p(count & 0xff, 0x42);
  47. outb((count >> 8) & 0xff, 0x42);
  48. } else {
  49. /* disable counter 2 */
  50. outb(inb_p(0x61) & 0xFC, 0x61);
  51. }
  52. spin_unlock_irqrestore(&i8253_beep_lock, flags);
  53. return 0;
  54. }
  55. static int __init pcspkr_init(void)
  56. {
  57. pcspkr_dev.evbit[0] = BIT(EV_SND);
  58. pcspkr_dev.sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  59. pcspkr_dev.event = pcspkr_event;
  60. pcspkr_dev.name = pcspkr_name;
  61. pcspkr_dev.phys = pcspkr_phys;
  62. pcspkr_dev.id.bustype = BUS_ISA;
  63. pcspkr_dev.id.vendor = 0x001f;
  64. pcspkr_dev.id.product = 0x0001;
  65. pcspkr_dev.id.version = 0x0100;
  66. input_register_device(&pcspkr_dev);
  67. printk(KERN_INFO "input: %s\n", pcspkr_name);
  68. return 0;
  69. }
  70. static void __exit pcspkr_exit(void)
  71. {
  72. input_unregister_device(&pcspkr_dev);
  73. /* turn off the speaker */
  74. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  75. }
  76. module_init(pcspkr_init);
  77. module_exit(pcspkr_exit);