pcspkr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <linux/platform_device.h>
  18. #include <asm/8253pit.h>
  19. #include <asm/io.h>
  20. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  21. MODULE_DESCRIPTION("PC Speaker beeper driver");
  22. MODULE_LICENSE("GPL");
  23. #ifdef CONFIG_X86
  24. /* Use the global PIT lock ! */
  25. #include <asm/i8253.h>
  26. #else
  27. static DEFINE_SPINLOCK(i8253_lock);
  28. #endif
  29. static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  30. {
  31. unsigned int count = 0;
  32. unsigned long flags;
  33. if (type != EV_SND)
  34. return -1;
  35. switch (code) {
  36. case SND_BELL: if (value) value = 1000;
  37. case SND_TONE: break;
  38. default: return -1;
  39. }
  40. if (value > 20 && value < 32767)
  41. count = PIT_TICK_RATE / value;
  42. spin_lock_irqsave(&i8253_lock, flags);
  43. if (count) {
  44. /* enable counter 2 */
  45. outb_p(inb_p(0x61) | 3, 0x61);
  46. /* set command for counter 2, 2 byte write */
  47. outb_p(0xB6, 0x43);
  48. /* select desired HZ */
  49. outb_p(count & 0xff, 0x42);
  50. outb((count >> 8) & 0xff, 0x42);
  51. } else {
  52. /* disable counter 2 */
  53. outb(inb_p(0x61) & 0xFC, 0x61);
  54. }
  55. spin_unlock_irqrestore(&i8253_lock, flags);
  56. return 0;
  57. }
  58. static int __devinit pcspkr_probe(struct platform_device *dev)
  59. {
  60. struct input_dev *pcspkr_dev;
  61. int err;
  62. pcspkr_dev = input_allocate_device();
  63. if (!pcspkr_dev)
  64. return -ENOMEM;
  65. pcspkr_dev->name = "PC Speaker";
  66. pcspkr_dev->phys = "isa0061/input0";
  67. pcspkr_dev->id.bustype = BUS_ISA;
  68. pcspkr_dev->id.vendor = 0x001f;
  69. pcspkr_dev->id.product = 0x0001;
  70. pcspkr_dev->id.version = 0x0100;
  71. pcspkr_dev->dev.parent = &dev->dev;
  72. pcspkr_dev->evbit[0] = BIT(EV_SND);
  73. pcspkr_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  74. pcspkr_dev->event = pcspkr_event;
  75. err = input_register_device(pcspkr_dev);
  76. if (err) {
  77. input_free_device(pcspkr_dev);
  78. return err;
  79. }
  80. platform_set_drvdata(dev, pcspkr_dev);
  81. return 0;
  82. }
  83. static int __devexit pcspkr_remove(struct platform_device *dev)
  84. {
  85. struct input_dev *pcspkr_dev = platform_get_drvdata(dev);
  86. input_unregister_device(pcspkr_dev);
  87. platform_set_drvdata(dev, NULL);
  88. /* turn off the speaker */
  89. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  90. return 0;
  91. }
  92. static int pcspkr_suspend(struct platform_device *dev, pm_message_t state)
  93. {
  94. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  95. return 0;
  96. }
  97. static void pcspkr_shutdown(struct platform_device *dev)
  98. {
  99. /* turn off the speaker */
  100. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  101. }
  102. static struct platform_driver pcspkr_platform_driver = {
  103. .driver = {
  104. .name = "pcspkr",
  105. .owner = THIS_MODULE,
  106. },
  107. .probe = pcspkr_probe,
  108. .remove = __devexit_p(pcspkr_remove),
  109. .suspend = pcspkr_suspend,
  110. .shutdown = pcspkr_shutdown,
  111. };
  112. static int __init pcspkr_init(void)
  113. {
  114. return platform_driver_register(&pcspkr_platform_driver);
  115. }
  116. static void __exit pcspkr_exit(void)
  117. {
  118. platform_driver_unregister(&pcspkr_platform_driver);
  119. }
  120. module_init(pcspkr_init);
  121. module_exit(pcspkr_exit);