beep.c 7.1 KB

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