pcsp.c 5.5 KB

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