pcspkr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. static struct platform_device *pcspkr_platform_device;
  24. static DEFINE_SPINLOCK(i8253_beep_lock);
  25. static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  26. {
  27. unsigned int count = 0;
  28. unsigned long flags;
  29. if (type != EV_SND)
  30. return -1;
  31. switch (code) {
  32. case SND_BELL: if (value) value = 1000;
  33. case SND_TONE: break;
  34. default: return -1;
  35. }
  36. if (value > 20 && value < 32767)
  37. count = PIT_TICK_RATE / value;
  38. spin_lock_irqsave(&i8253_beep_lock, flags);
  39. if (count) {
  40. /* enable counter 2 */
  41. outb_p(inb_p(0x61) | 3, 0x61);
  42. /* set command for counter 2, 2 byte write */
  43. outb_p(0xB6, 0x43);
  44. /* select desired HZ */
  45. outb_p(count & 0xff, 0x42);
  46. outb((count >> 8) & 0xff, 0x42);
  47. } else {
  48. /* disable counter 2 */
  49. outb(inb_p(0x61) & 0xFC, 0x61);
  50. }
  51. spin_unlock_irqrestore(&i8253_beep_lock, flags);
  52. return 0;
  53. }
  54. static int __devinit pcspkr_probe(struct platform_device *dev)
  55. {
  56. struct input_dev *pcspkr_dev;
  57. int err;
  58. pcspkr_dev = input_allocate_device();
  59. if (!pcspkr_dev)
  60. return -ENOMEM;
  61. pcspkr_dev->name = "PC Speaker";
  62. pcspkr_dev->phys = "isa0061/input0";
  63. pcspkr_dev->id.bustype = BUS_ISA;
  64. pcspkr_dev->id.vendor = 0x001f;
  65. pcspkr_dev->id.product = 0x0001;
  66. pcspkr_dev->id.version = 0x0100;
  67. pcspkr_dev->cdev.dev = &dev->dev;
  68. pcspkr_dev->evbit[0] = BIT(EV_SND);
  69. pcspkr_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  70. pcspkr_dev->event = pcspkr_event;
  71. err = input_register_device(pcspkr_dev);
  72. if (err) {
  73. input_free_device(pcspkr_dev);
  74. return err;
  75. }
  76. platform_set_drvdata(dev, pcspkr_dev);
  77. return 0;
  78. }
  79. static int __devexit pcspkr_remove(struct platform_device *dev)
  80. {
  81. struct input_dev *pcspkr_dev = platform_get_drvdata(dev);
  82. input_unregister_device(pcspkr_dev);
  83. platform_set_drvdata(dev, NULL);
  84. /* turn off the speaker */
  85. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  86. return 0;
  87. }
  88. static int pcspkr_suspend(struct platform_device *dev, pm_message_t state)
  89. {
  90. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  91. return 0;
  92. }
  93. static void pcspkr_shutdown(struct platform_device *dev)
  94. {
  95. /* turn off the speaker */
  96. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  97. }
  98. static struct platform_driver pcspkr_platform_driver = {
  99. .driver = {
  100. .name = "pcspkr",
  101. .owner = THIS_MODULE,
  102. },
  103. .probe = pcspkr_probe,
  104. .remove = __devexit_p(pcspkr_remove),
  105. .suspend = pcspkr_suspend,
  106. .shutdown = pcspkr_shutdown,
  107. };
  108. static int __init pcspkr_init(void)
  109. {
  110. int err;
  111. err = platform_driver_register(&pcspkr_platform_driver);
  112. if (err)
  113. return err;
  114. pcspkr_platform_device = platform_device_alloc("pcspkr", -1);
  115. if (!pcspkr_platform_device) {
  116. err = -ENOMEM;
  117. goto err_unregister_driver;
  118. }
  119. err = platform_device_add(pcspkr_platform_device);
  120. if (err)
  121. goto err_free_device;
  122. return 0;
  123. err_free_device:
  124. platform_device_put(pcspkr_platform_device);
  125. err_unregister_driver:
  126. platform_driver_unregister(&pcspkr_platform_driver);
  127. return err;
  128. }
  129. static void __exit pcspkr_exit(void)
  130. {
  131. platform_device_unregister(pcspkr_platform_device);
  132. platform_driver_unregister(&pcspkr_platform_driver);
  133. }
  134. module_init(pcspkr_init);
  135. module_exit(pcspkr_exit);