pcsp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * PC-Speaker driver for Linux
  3. *
  4. * Copyright (C) 1997-2001 David Woodhouse
  5. * Copyright (C) 2001-2008 Stas Sergeev
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <sound/core.h>
  11. #include <sound/initval.h>
  12. #include <sound/pcm.h>
  13. #include <linux/input.h>
  14. #include <linux/delay.h>
  15. #include <linux/bitops.h>
  16. #include <linux/mm.h>
  17. #include "pcsp_input.h"
  18. #include "pcsp.h"
  19. MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
  20. MODULE_DESCRIPTION("PC-Speaker driver");
  21. MODULE_LICENSE("GPL");
  22. MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");
  23. MODULE_ALIAS("platform:pcspkr");
  24. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  25. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  26. static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
  27. static bool nopcm; /* Disable PCM capability of the driver */
  28. module_param(index, int, 0444);
  29. MODULE_PARM_DESC(index, "Index value for pcsp soundcard.");
  30. module_param(id, charp, 0444);
  31. MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
  32. module_param(enable, bool, 0444);
  33. MODULE_PARM_DESC(enable, "Enable PC-Speaker sound.");
  34. module_param(nopcm, bool, 0444);
  35. MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain.");
  36. struct snd_pcsp pcsp_chip;
  37. static int snd_pcsp_create(struct snd_card *card)
  38. {
  39. static struct snd_device_ops ops = { };
  40. unsigned int resolution = hrtimer_resolution;
  41. int err, div, min_div, order;
  42. if (!nopcm) {
  43. if (resolution > PCSP_MAX_PERIOD_NS) {
  44. printk(KERN_ERR "PCSP: Timer resolution is not sufficient "
  45. "(%unS)\n", resolution);
  46. printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI "
  47. "enabled.\n");
  48. printk(KERN_ERR "PCSP: Turned into nopcm mode.\n");
  49. nopcm = 1;
  50. }
  51. }
  52. if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS)
  53. min_div = MIN_DIV;
  54. else
  55. min_div = MAX_DIV;
  56. #if PCSP_DEBUG
  57. printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n",
  58. loops_per_jiffy, min_div, resolution);
  59. #endif
  60. div = MAX_DIV / min_div;
  61. order = fls(div) - 1;
  62. pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE);
  63. pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE);
  64. pcsp_chip.playback_ptr = 0;
  65. pcsp_chip.period_ptr = 0;
  66. atomic_set(&pcsp_chip.timer_active, 0);
  67. pcsp_chip.enable = 1;
  68. pcsp_chip.pcspkr = 1;
  69. spin_lock_init(&pcsp_chip.substream_lock);
  70. pcsp_chip.card = card;
  71. pcsp_chip.port = 0x61;
  72. pcsp_chip.irq = -1;
  73. pcsp_chip.dma = -1;
  74. /* Register device */
  75. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops);
  76. if (err < 0)
  77. return err;
  78. return 0;
  79. }
  80. static int snd_card_pcsp_probe(int devnum, struct device *dev)
  81. {
  82. struct snd_card *card;
  83. int err;
  84. if (devnum != 0)
  85. return -EINVAL;
  86. hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  87. pcsp_chip.timer.function = pcsp_do_timer;
  88. err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card);
  89. if (err < 0)
  90. return err;
  91. err = snd_pcsp_create(card);
  92. if (err < 0) {
  93. snd_card_free(card);
  94. return err;
  95. }
  96. if (!nopcm) {
  97. err = snd_pcsp_new_pcm(&pcsp_chip);
  98. if (err < 0) {
  99. snd_card_free(card);
  100. return err;
  101. }
  102. }
  103. err = snd_pcsp_new_mixer(&pcsp_chip, nopcm);
  104. if (err < 0) {
  105. snd_card_free(card);
  106. return err;
  107. }
  108. strcpy(card->driver, "PC-Speaker");
  109. strcpy(card->shortname, "pcsp");
  110. sprintf(card->longname, "Internal PC-Speaker at port 0x%x",
  111. pcsp_chip.port);
  112. err = snd_card_register(card);
  113. if (err < 0) {
  114. snd_card_free(card);
  115. return err;
  116. }
  117. return 0;
  118. }
  119. static int alsa_card_pcsp_init(struct device *dev)
  120. {
  121. int err;
  122. err = snd_card_pcsp_probe(0, dev);
  123. if (err) {
  124. printk(KERN_ERR "PC-Speaker initialization failed.\n");
  125. return err;
  126. }
  127. /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
  128. if (debug_pagealloc_enabled()) {
  129. printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, "
  130. "which may make the sound noisy.\n");
  131. }
  132. return 0;
  133. }
  134. static void alsa_card_pcsp_exit(struct snd_pcsp *chip)
  135. {
  136. snd_card_free(chip->card);
  137. }
  138. static int pcsp_probe(struct platform_device *dev)
  139. {
  140. int err;
  141. err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev);
  142. if (err < 0)
  143. return err;
  144. err = alsa_card_pcsp_init(&dev->dev);
  145. if (err < 0) {
  146. pcspkr_input_remove(pcsp_chip.input_dev);
  147. return err;
  148. }
  149. platform_set_drvdata(dev, &pcsp_chip);
  150. return 0;
  151. }
  152. static int pcsp_remove(struct platform_device *dev)
  153. {
  154. struct snd_pcsp *chip = platform_get_drvdata(dev);
  155. pcspkr_input_remove(chip->input_dev);
  156. alsa_card_pcsp_exit(chip);
  157. return 0;
  158. }
  159. static void pcsp_stop_beep(struct snd_pcsp *chip)
  160. {
  161. pcsp_sync_stop(chip);
  162. pcspkr_stop_sound();
  163. }
  164. #ifdef CONFIG_PM_SLEEP
  165. static int pcsp_suspend(struct device *dev)
  166. {
  167. struct snd_pcsp *chip = dev_get_drvdata(dev);
  168. pcsp_stop_beep(chip);
  169. snd_pcm_suspend_all(chip->pcm);
  170. return 0;
  171. }
  172. static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL);
  173. #define PCSP_PM_OPS &pcsp_pm
  174. #else
  175. #define PCSP_PM_OPS NULL
  176. #endif /* CONFIG_PM_SLEEP */
  177. static void pcsp_shutdown(struct platform_device *dev)
  178. {
  179. struct snd_pcsp *chip = platform_get_drvdata(dev);
  180. pcsp_stop_beep(chip);
  181. }
  182. static struct platform_driver pcsp_platform_driver = {
  183. .driver = {
  184. .name = "pcspkr",
  185. .pm = PCSP_PM_OPS,
  186. },
  187. .probe = pcsp_probe,
  188. .remove = pcsp_remove,
  189. .shutdown = pcsp_shutdown,
  190. };
  191. static int __init pcsp_init(void)
  192. {
  193. if (!enable)
  194. return -ENODEV;
  195. return platform_driver_register(&pcsp_platform_driver);
  196. }
  197. static void __exit pcsp_exit(void)
  198. {
  199. platform_driver_unregister(&pcsp_platform_driver);
  200. }
  201. module_init(pcsp_init);
  202. module_exit(pcsp_exit);