beep.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Beep using pcm
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/io.h>
  21. #include <asm/irq.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/input.h>
  25. #include <linux/pci.h>
  26. #include <linux/dma-mapping.h>
  27. #include <sound/core.h>
  28. #include <sound/control.h>
  29. #include "pmac.h"
  30. struct pmac_beep {
  31. int running; /* boolean */
  32. int volume; /* mixer volume: 0-100 */
  33. int volume_play; /* currently playing volume */
  34. int hz;
  35. int nsamples;
  36. short *buf; /* allocated wave buffer */
  37. dma_addr_t addr; /* physical address of buffer */
  38. struct input_dev *dev;
  39. };
  40. /*
  41. * stop beep if running
  42. */
  43. void snd_pmac_beep_stop(struct snd_pmac *chip)
  44. {
  45. struct pmac_beep *beep = chip->beep;
  46. if (beep && beep->running) {
  47. beep->running = 0;
  48. snd_pmac_beep_dma_stop(chip);
  49. }
  50. }
  51. /*
  52. * Stuff for outputting a beep. The values range from -327 to +327
  53. * so we can multiply by an amplitude in the range 0..100 to get a
  54. * signed short value to put in the output buffer.
  55. */
  56. static short beep_wform[256] = {
  57. 0, 40, 79, 117, 153, 187, 218, 245,
  58. 269, 288, 304, 316, 323, 327, 327, 324,
  59. 318, 310, 299, 288, 275, 262, 249, 236,
  60. 224, 213, 204, 196, 190, 186, 183, 182,
  61. 182, 183, 186, 189, 192, 196, 200, 203,
  62. 206, 208, 209, 209, 209, 207, 204, 201,
  63. 197, 193, 188, 183, 179, 174, 170, 166,
  64. 163, 161, 160, 159, 159, 160, 161, 162,
  65. 164, 166, 168, 169, 171, 171, 171, 170,
  66. 169, 167, 163, 159, 155, 150, 144, 139,
  67. 133, 128, 122, 117, 113, 110, 107, 105,
  68. 103, 103, 103, 103, 104, 104, 105, 105,
  69. 105, 103, 101, 97, 92, 86, 78, 68,
  70. 58, 45, 32, 18, 3, -11, -26, -41,
  71. -55, -68, -79, -88, -95, -100, -102, -102,
  72. -99, -93, -85, -75, -62, -48, -33, -16,
  73. 0, 16, 33, 48, 62, 75, 85, 93,
  74. 99, 102, 102, 100, 95, 88, 79, 68,
  75. 55, 41, 26, 11, -3, -18, -32, -45,
  76. -58, -68, -78, -86, -92, -97, -101, -103,
  77. -105, -105, -105, -104, -104, -103, -103, -103,
  78. -103, -105, -107, -110, -113, -117, -122, -128,
  79. -133, -139, -144, -150, -155, -159, -163, -167,
  80. -169, -170, -171, -171, -171, -169, -168, -166,
  81. -164, -162, -161, -160, -159, -159, -160, -161,
  82. -163, -166, -170, -174, -179, -183, -188, -193,
  83. -197, -201, -204, -207, -209, -209, -209, -208,
  84. -206, -203, -200, -196, -192, -189, -186, -183,
  85. -182, -182, -183, -186, -190, -196, -204, -213,
  86. -224, -236, -249, -262, -275, -288, -299, -310,
  87. -318, -324, -327, -327, -323, -316, -304, -288,
  88. -269, -245, -218, -187, -153, -117, -79, -40,
  89. };
  90. #define BEEP_SRATE 22050 /* 22050 Hz sample rate */
  91. #define BEEP_BUFLEN 512
  92. #define BEEP_VOLUME 15 /* 0 - 100 */
  93. static int snd_pmac_beep_event(struct input_dev *dev, unsigned int type,
  94. unsigned int code, int hz)
  95. {
  96. struct snd_pmac *chip;
  97. struct pmac_beep *beep;
  98. unsigned long flags;
  99. int beep_speed = 0;
  100. int srate;
  101. int period, ncycles, nsamples;
  102. int i, j, f;
  103. short *p;
  104. if (type != EV_SND)
  105. return -1;
  106. switch (code) {
  107. case SND_BELL: if (hz) hz = 1000;
  108. case SND_TONE: break;
  109. default: return -1;
  110. }
  111. chip = input_get_drvdata(dev);
  112. if (! chip || (beep = chip->beep) == NULL)
  113. return -1;
  114. if (! hz) {
  115. spin_lock_irqsave(&chip->reg_lock, flags);
  116. if (beep->running)
  117. snd_pmac_beep_stop(chip);
  118. spin_unlock_irqrestore(&chip->reg_lock, flags);
  119. return 0;
  120. }
  121. beep_speed = snd_pmac_rate_index(chip, &chip->playback, BEEP_SRATE);
  122. srate = chip->freq_table[beep_speed];
  123. if (hz <= srate / BEEP_BUFLEN || hz > srate / 2)
  124. hz = 1000;
  125. spin_lock_irqsave(&chip->reg_lock, flags);
  126. if (chip->playback.running || chip->capture.running || beep->running) {
  127. spin_unlock_irqrestore(&chip->reg_lock, flags);
  128. return 0;
  129. }
  130. beep->running = 1;
  131. spin_unlock_irqrestore(&chip->reg_lock, flags);
  132. if (hz == beep->hz && beep->volume == beep->volume_play) {
  133. nsamples = beep->nsamples;
  134. } else {
  135. period = srate * 256 / hz; /* fixed point */
  136. ncycles = BEEP_BUFLEN * 256 / period;
  137. nsamples = (period * ncycles) >> 8;
  138. f = ncycles * 65536 / nsamples;
  139. j = 0;
  140. p = beep->buf;
  141. for (i = 0; i < nsamples; ++i, p += 2) {
  142. p[0] = p[1] = beep_wform[j >> 8] * beep->volume;
  143. j = (j + f) & 0xffff;
  144. }
  145. beep->hz = hz;
  146. beep->volume_play = beep->volume;
  147. beep->nsamples = nsamples;
  148. }
  149. spin_lock_irqsave(&chip->reg_lock, flags);
  150. snd_pmac_beep_dma_start(chip, beep->nsamples * 4, beep->addr, beep_speed);
  151. spin_unlock_irqrestore(&chip->reg_lock, flags);
  152. return 0;
  153. }
  154. /*
  155. * beep volume mixer
  156. */
  157. static int snd_pmac_info_beep(struct snd_kcontrol *kcontrol,
  158. struct snd_ctl_elem_info *uinfo)
  159. {
  160. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  161. uinfo->count = 1;
  162. uinfo->value.integer.min = 0;
  163. uinfo->value.integer.max = 100;
  164. return 0;
  165. }
  166. static int snd_pmac_get_beep(struct snd_kcontrol *kcontrol,
  167. struct snd_ctl_elem_value *ucontrol)
  168. {
  169. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  170. if (snd_BUG_ON(!chip->beep))
  171. return -ENXIO;
  172. ucontrol->value.integer.value[0] = chip->beep->volume;
  173. return 0;
  174. }
  175. static int snd_pmac_put_beep(struct snd_kcontrol *kcontrol,
  176. struct snd_ctl_elem_value *ucontrol)
  177. {
  178. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  179. unsigned int oval, nval;
  180. if (snd_BUG_ON(!chip->beep))
  181. return -ENXIO;
  182. oval = chip->beep->volume;
  183. nval = ucontrol->value.integer.value[0];
  184. if (nval > 100)
  185. return -EINVAL;
  186. chip->beep->volume = nval;
  187. return oval != chip->beep->volume;
  188. }
  189. static struct snd_kcontrol_new snd_pmac_beep_mixer = {
  190. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  191. .name = "Beep Playback Volume",
  192. .info = snd_pmac_info_beep,
  193. .get = snd_pmac_get_beep,
  194. .put = snd_pmac_put_beep,
  195. };
  196. /* Initialize beep stuff */
  197. int snd_pmac_attach_beep(struct snd_pmac *chip)
  198. {
  199. struct pmac_beep *beep;
  200. struct input_dev *input_dev;
  201. struct snd_kcontrol *beep_ctl;
  202. void *dmabuf;
  203. int err = -ENOMEM;
  204. beep = kzalloc(sizeof(*beep), GFP_KERNEL);
  205. if (! beep)
  206. return -ENOMEM;
  207. dmabuf = dma_alloc_coherent(&chip->pdev->dev, BEEP_BUFLEN * 4,
  208. &beep->addr, GFP_KERNEL);
  209. input_dev = input_allocate_device();
  210. if (! dmabuf || ! input_dev)
  211. goto fail1;
  212. /* FIXME: set more better values */
  213. input_dev->name = "PowerMac Beep";
  214. input_dev->phys = "powermac/beep";
  215. input_dev->id.bustype = BUS_ADB;
  216. input_dev->id.vendor = 0x001f;
  217. input_dev->id.product = 0x0001;
  218. input_dev->id.version = 0x0100;
  219. input_dev->evbit[0] = BIT_MASK(EV_SND);
  220. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  221. input_dev->event = snd_pmac_beep_event;
  222. input_dev->dev.parent = &chip->pdev->dev;
  223. input_set_drvdata(input_dev, chip);
  224. beep->dev = input_dev;
  225. beep->buf = dmabuf;
  226. beep->volume = BEEP_VOLUME;
  227. beep->running = 0;
  228. beep_ctl = snd_ctl_new1(&snd_pmac_beep_mixer, chip);
  229. err = snd_ctl_add(chip->card, beep_ctl);
  230. if (err < 0)
  231. goto fail1;
  232. chip->beep = beep;
  233. err = input_register_device(beep->dev);
  234. if (err)
  235. goto fail2;
  236. return 0;
  237. fail2: snd_ctl_remove(chip->card, beep_ctl);
  238. fail1: input_free_device(input_dev);
  239. if (dmabuf)
  240. dma_free_coherent(&chip->pdev->dev, BEEP_BUFLEN * 4,
  241. dmabuf, beep->addr);
  242. kfree(beep);
  243. return err;
  244. }
  245. void snd_pmac_detach_beep(struct snd_pmac *chip)
  246. {
  247. if (chip->beep) {
  248. input_unregister_device(chip->beep->dev);
  249. dma_free_coherent(&chip->pdev->dev, BEEP_BUFLEN * 4,
  250. chip->beep->buf, chip->beep->addr);
  251. kfree(chip->beep);
  252. chip->beep = NULL;
  253. }
  254. }