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>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
39 MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
40 MODULE_LICENSE("GPL");
41 MODULE_CLASSES("{sound}");
43 #define BUSY_LOOPS 100000
47 int snd_sbdsp_command(sb_t
*chip
, unsigned char val
)
51 snd_printk("command 0x%x\n", val
);
53 for (i
= BUSY_LOOPS
; i
; i
--)
54 if ((inb(SBP(chip
, STATUS
)) & 0x80) == 0) {
55 outb(val
, SBP(chip
, COMMAND
));
58 snd_printd("%s [0x%lx]: timeout (0x%x)\n", __FUNCTION__
, chip
->port
, val
);
62 int snd_sbdsp_get_byte(sb_t
*chip
)
66 for (i
= BUSY_LOOPS
; i
; i
--) {
67 if (inb(SBP(chip
, DATA_AVAIL
)) & 0x80) {
68 val
= inb(SBP(chip
, READ
));
70 snd_printk("get_byte 0x%x\n", val
);
75 snd_printd("%s [0x%lx]: timeout\n", __FUNCTION__
, chip
->port
);
79 int snd_sbdsp_reset(sb_t
*chip
)
83 outb(1, SBP(chip
, RESET
));
85 outb(0, SBP(chip
, RESET
));
87 for (i
= BUSY_LOOPS
; i
; i
--)
88 if (inb(SBP(chip
, DATA_AVAIL
)) & 0x80) {
89 if (inb(SBP(chip
, READ
)) == 0xaa)
94 snd_printdd("%s [0x%lx] failed...\n", __FUNCTION__
, chip
->port
);
98 int snd_sbdsp_version(sb_t
* chip
)
100 unsigned int result
= -ENODEV
;
102 snd_sbdsp_command(chip
, SB_DSP_GET_VERSION
);
103 result
= (short) snd_sbdsp_get_byte(chip
) << 8;
104 result
|= (short) snd_sbdsp_get_byte(chip
);
108 static int snd_sbdsp_probe(sb_t
* chip
)
116 * initialization sequence
119 spin_lock_irqsave(&chip
->reg_lock
, flags
);
120 if (snd_sbdsp_reset(chip
) < 0) {
121 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
124 version
= snd_sbdsp_version(chip
);
126 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
129 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
130 major
= version
>> 8;
131 minor
= version
& 0xff;
132 snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
133 chip
->port
, major
, minor
);
135 switch (chip
->hardware
) {
139 chip
->hardware
= SB_HW_10
;
144 chip
->hardware
= SB_HW_201
;
147 chip
->hardware
= SB_HW_20
;
152 chip
->hardware
= SB_HW_PRO
;
156 chip
->hardware
= SB_HW_16
;
160 snd_printk("SB [0x%lx]: unknown DSP chip version %i.%i\n",
161 chip
->port
, major
, minor
);
166 str
= "16 (ALS-100)";
169 str
= "16 (ALS-4000)";
172 str
= "(DT019X/ALS007)";
177 sprintf(chip
->name
, "Sound Blaster %s", str
);
178 chip
->version
= (major
<< 8) | minor
;
182 static int snd_sbdsp_free(sb_t
*chip
)
184 if (chip
->res_port
) {
185 release_resource(chip
->res_port
);
186 kfree_nocheck(chip
->res_port
);
188 if (chip
->res_alt_port
) {
189 release_resource(chip
->res_alt_port
);
190 kfree_nocheck(chip
->res_alt_port
);
193 free_irq(chip
->irq
, (void *) chip
);
195 if (chip
->dma8
>= 0) {
196 disable_dma(chip
->dma8
);
197 free_dma(chip
->dma8
);
199 if (chip
->dma16
>= 0 && chip
->dma16
!= chip
->dma8
) {
200 disable_dma(chip
->dma16
);
201 free_dma(chip
->dma16
);
204 snd_magic_kfree(chip
);
208 static int snd_sbdsp_dev_free(snd_device_t
*device
)
210 sb_t
*chip
= snd_magic_cast(sb_t
, device
->device_data
, return -ENXIO
);
211 return snd_sbdsp_free(chip
);
214 int snd_sbdsp_create(snd_card_t
*card
,
217 void (*irq_handler
)(int, void *, struct pt_regs
*),
220 unsigned short hardware
,
225 static snd_device_ops_t ops
= {
226 .dev_free
= snd_sbdsp_dev_free
,
229 snd_assert(r_chip
!= NULL
, return -EINVAL
);
231 chip
= snd_magic_kcalloc(sb_t
, 0, GFP_KERNEL
);
234 spin_lock_init(&chip
->reg_lock
);
235 spin_lock_init(&chip
->open_lock
);
236 spin_lock_init(&chip
->midi_input_lock
);
237 spin_lock_init(&chip
->mixer_lock
);
243 if (request_irq(irq
, irq_handler
, hardware
== SB_HW_ALS4000
?
244 SA_INTERRUPT
| SA_SHIRQ
: SA_INTERRUPT
,
245 "SoundBlaster", (void *) chip
)) {
246 snd_sbdsp_free(chip
);
251 if (hardware
== SB_HW_ALS4000
)
252 goto __skip_allocation
;
254 if ((chip
->res_port
= request_region(port
, 16, "SoundBlaster")) == NULL
) {
255 snd_sbdsp_free(chip
);
260 if (dma8
>= 0 && request_dma(dma8
, "SoundBlaster - 8bit")) {
261 snd_sbdsp_free(chip
);
266 if (hardware
!= SB_HW_ALS100
&& (dma16
< 5 || dma16
> 7)) {
269 } else if (request_dma(dma16
, "SoundBlaster - 16bit")) {
270 snd_sbdsp_free(chip
);
279 chip
->hardware
= hardware
;
280 if ((err
= snd_sbdsp_probe(chip
)) < 0) {
281 snd_sbdsp_free(chip
);
284 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
)) < 0) {
285 snd_sbdsp_free(chip
);
292 EXPORT_SYMBOL(snd_sbdsp_command
);
293 EXPORT_SYMBOL(snd_sbdsp_get_byte
);
294 EXPORT_SYMBOL(snd_sbdsp_reset
);
295 EXPORT_SYMBOL(snd_sbdsp_create
);
297 EXPORT_SYMBOL(snd_sbmixer_write
);
298 EXPORT_SYMBOL(snd_sbmixer_read
);
299 EXPORT_SYMBOL(snd_sbmixer_new
);
305 static int __init
alsa_sb_common_init(void)
310 static void __exit
alsa_sb_common_exit(void)
314 module_init(alsa_sb_common_init
)
315 module_exit(alsa_sb_common_exit
)