pcspkr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/i8253.h>
  16. #include <linux/input.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/timex.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. MODULE_ALIAS("platform:pcspkr");
  24. static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  25. {
  26. unsigned int count = 0;
  27. unsigned long flags;
  28. if (type != EV_SND)
  29. return -1;
  30. switch (code) {
  31. case SND_BELL: if (value) value = 1000;
  32. case SND_TONE: break;
  33. default: return -1;
  34. }
  35. if (value > 20 && value < 32767)
  36. count = PIT_TICK_RATE / value;
  37. raw_spin_lock_irqsave(&i8253_lock, flags);
  38. if (count) {
  39. /* set command for counter 2, 2 byte write */
  40. outb_p(0xB6, 0x43);
  41. /* select desired HZ */
  42. outb_p(count & 0xff, 0x42);
  43. outb((count >> 8) & 0xff, 0x42);
  44. /* enable counter 2 */
  45. outb_p(inb_p(0x61) | 3, 0x61);
  46. } else {
  47. /* disable counter 2 */
  48. outb(inb_p(0x61) & 0xFC, 0x61);
  49. }
  50. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  51. return 0;
  52. }
  53. static int pcspkr_probe(struct platform_device *dev)
  54. {
  55. struct input_dev *pcspkr_dev;
  56. int err;
  57. pcspkr_dev = input_allocate_device();
  58. if (!pcspkr_dev)
  59. return -ENOMEM;
  60. pcspkr_dev->name = "PC Speaker";
  61. pcspkr_dev->phys = "isa0061/input0";
  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. pcspkr_dev->dev.parent = &dev->dev;
  67. pcspkr_dev->evbit[0] = BIT_MASK(EV_SND);
  68. pcspkr_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  69. pcspkr_dev->event = pcspkr_event;
  70. err = input_register_device(pcspkr_dev);
  71. if (err) {
  72. input_free_device(pcspkr_dev);
  73. return err;
  74. }
  75. platform_set_drvdata(dev, pcspkr_dev);
  76. return 0;
  77. }
  78. static int pcspkr_remove(struct platform_device *dev)
  79. {
  80. struct input_dev *pcspkr_dev = platform_get_drvdata(dev);
  81. input_unregister_device(pcspkr_dev);
  82. /* turn off the speaker */
  83. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  84. return 0;
  85. }
  86. static int pcspkr_suspend(struct device *dev)
  87. {
  88. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  89. return 0;
  90. }
  91. static void pcspkr_shutdown(struct platform_device *dev)
  92. {
  93. /* turn off the speaker */
  94. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  95. }
  96. static const struct dev_pm_ops pcspkr_pm_ops = {
  97. .suspend = pcspkr_suspend,
  98. };
  99. static struct platform_driver pcspkr_platform_driver = {
  100. .driver = {
  101. .name = "pcspkr",
  102. .pm = &pcspkr_pm_ops,
  103. },
  104. .probe = pcspkr_probe,
  105. .remove = pcspkr_remove,
  106. .shutdown = pcspkr_shutdown,
  107. };
  108. module_platform_driver(pcspkr_platform_driver);