2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Uros Bizjak <uros@kss-loka.si>
5 * Lowlevel routines for control of Sound Blaster cards
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/slab.h>
28 #include <linux/ioport.h>
29 #include <sound/core.h>
31 #include <sound/initval.h>
36 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
37 MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
38 MODULE_LICENSE("GPL");
40 #define BUSY_LOOPS 100000
44 int snd_sbdsp_command(sb_t *chip, unsigned char val)
48 snd_printk(KERN_DEBUG "command 0x%x\n", val);
50 for (i = BUSY_LOOPS; i; i--)
51 if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
52 outb(val, SBP(chip, COMMAND));
55 snd_printd("%s [0x%lx]: timeout (0x%x)\n", __FUNCTION__, chip->port, val);
59 int snd_sbdsp_get_byte(sb_t *chip)
63 for (i = BUSY_LOOPS; i; i--) {
64 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
65 val = inb(SBP(chip, READ));
67 snd_printk(KERN_DEBUG "get_byte 0x%x\n", val);
72 snd_printd("%s [0x%lx]: timeout\n", __FUNCTION__, chip->port);
76 int snd_sbdsp_reset(sb_t *chip)
80 outb(1, SBP(chip, RESET));
82 outb(0, SBP(chip, RESET));
84 for (i = BUSY_LOOPS; i; i--)
85 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
86 if (inb(SBP(chip, READ)) == 0xaa)
91 snd_printdd("%s [0x%lx] failed...\n", __FUNCTION__, chip->port);
95 static int snd_sbdsp_version(sb_t * chip)
97 unsigned int result = -ENODEV;
99 snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
100 result = (short) snd_sbdsp_get_byte(chip) << 8;
101 result |= (short) snd_sbdsp_get_byte(chip);
105 static int snd_sbdsp_probe(sb_t * chip)
113 * initialization sequence
116 spin_lock_irqsave(&chip->reg_lock, flags);
117 if (snd_sbdsp_reset(chip) < 0) {
118 spin_unlock_irqrestore(&chip->reg_lock, flags);
121 version = snd_sbdsp_version(chip);
123 spin_unlock_irqrestore(&chip->reg_lock, flags);
126 spin_unlock_irqrestore(&chip->reg_lock, flags);
127 major = version >> 8;
128 minor = version & 0xff;
129 snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
130 chip->port, major, minor);
132 switch (chip->hardware) {
136 chip->hardware = SB_HW_10;
141 chip->hardware = SB_HW_201;
144 chip->hardware = SB_HW_20;
149 chip->hardware = SB_HW_PRO;
153 chip->hardware = SB_HW_16;
157 snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n",
158 chip->port, major, minor);
163 str = "16 (ALS-100)";
166 str = "16 (ALS-4000)";
169 str = "(DT019X/ALS007)";
174 sprintf(chip->name, "Sound Blaster %s", str);
175 chip->version = (major << 8) | minor;
179 static int snd_sbdsp_free(sb_t *chip)
182 release_and_free_resource(chip->res_port);
184 free_irq(chip->irq, (void *) chip);
186 if (chip->dma8 >= 0) {
187 disable_dma(chip->dma8);
188 free_dma(chip->dma8);
190 if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
191 disable_dma(chip->dma16);
192 free_dma(chip->dma16);
199 static int snd_sbdsp_dev_free(snd_device_t *device)
201 sb_t *chip = device->device_data;
202 return snd_sbdsp_free(chip);
205 int snd_sbdsp_create(snd_card_t *card,
208 irqreturn_t (*irq_handler)(int, void *, struct pt_regs *),
211 unsigned short hardware,
216 static snd_device_ops_t ops = {
217 .dev_free = snd_sbdsp_dev_free,
220 snd_assert(r_chip != NULL, return -EINVAL);
222 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
225 spin_lock_init(&chip->reg_lock);
226 spin_lock_init(&chip->open_lock);
227 spin_lock_init(&chip->midi_input_lock);
228 spin_lock_init(&chip->mixer_lock);
234 if (request_irq(irq, irq_handler, hardware == SB_HW_ALS4000 ?
235 SA_INTERRUPT | SA_SHIRQ : SA_INTERRUPT,
236 "SoundBlaster", (void *) chip)) {
237 snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
238 snd_sbdsp_free(chip);
243 if (hardware == SB_HW_ALS4000)
244 goto __skip_allocation;
246 if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
247 snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
248 snd_sbdsp_free(chip);
253 if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
254 snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
255 snd_sbdsp_free(chip);
260 if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
263 } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
264 snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
265 snd_sbdsp_free(chip);
274 chip->hardware = hardware;
275 if ((err = snd_sbdsp_probe(chip)) < 0) {
276 snd_sbdsp_free(chip);
279 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
280 snd_sbdsp_free(chip);
287 EXPORT_SYMBOL(snd_sbdsp_command);
288 EXPORT_SYMBOL(snd_sbdsp_get_byte);
289 EXPORT_SYMBOL(snd_sbdsp_reset);
290 EXPORT_SYMBOL(snd_sbdsp_create);
292 EXPORT_SYMBOL(snd_sbmixer_write);
293 EXPORT_SYMBOL(snd_sbmixer_read);
294 EXPORT_SYMBOL(snd_sbmixer_new);
295 EXPORT_SYMBOL(snd_sbmixer_add_ctl);
301 static int __init alsa_sb_common_init(void)
306 static void __exit alsa_sb_common_exit(void)
310 module_init(alsa_sb_common_init)
311 module_exit(alsa_sb_common_exit)