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