ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / sb / sb_common.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *                   Uros Bizjak <uros@kss-loka.si>
4  *
5  *  Lowlevel routines for control of Sound Blaster cards
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
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>
30 #include <sound/sb.h>
31 #include <sound/initval.h>
32
33 #include <asm/io.h>
34 #include <asm/dma.h>
35
36 #define chip_t sb_t
37
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}");
42
43 #define BUSY_LOOPS 100000
44
45 #undef IO_DEBUG
46
47 int snd_sbdsp_command(sb_t *chip, unsigned char val)
48 {
49         int i;
50 #ifdef IO_DEBUG
51         snd_printk("command 0x%x\n", val);
52 #endif
53         for (i = BUSY_LOOPS; i; i--)
54                 if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
55                         outb(val, SBP(chip, COMMAND));
56                         return 1;
57                 }
58         snd_printd("%s [0x%lx]: timeout (0x%x)\n", __FUNCTION__, chip->port, val);
59         return 0;
60 }
61
62 int snd_sbdsp_get_byte(sb_t *chip)
63 {
64         int val;
65         int i;
66         for (i = BUSY_LOOPS; i; i--) {
67                 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
68                         val = inb(SBP(chip, READ));
69 #ifdef IO_DEBUG
70                         snd_printk("get_byte 0x%x\n", val);
71 #endif
72                         return val;
73                 }
74         }
75         snd_printd("%s [0x%lx]: timeout\n", __FUNCTION__, chip->port);
76         return -ENODEV;
77 }
78
79 int snd_sbdsp_reset(sb_t *chip)
80 {
81         int i;
82
83         outb(1, SBP(chip, RESET));
84         udelay(10);
85         outb(0, SBP(chip, RESET));
86         udelay(30);
87         for (i = BUSY_LOOPS; i; i--)
88                 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
89                         if (inb(SBP(chip, READ)) == 0xaa)
90                                 return 0;
91                         else
92                                 break;
93                 }
94         snd_printdd("%s [0x%lx] failed...\n", __FUNCTION__, chip->port);
95         return -ENODEV;
96 }
97
98 int snd_sbdsp_version(sb_t * chip)
99 {
100         unsigned int result = -ENODEV;
101
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);
105         return result;
106 }
107
108 static int snd_sbdsp_probe(sb_t * chip)
109 {
110         int version;
111         int major, minor;
112         char *str;
113         unsigned long flags;
114
115         /*
116          *  initialization sequence
117          */
118
119         spin_lock_irqsave(&chip->reg_lock, flags);
120         if (snd_sbdsp_reset(chip) < 0) {
121                 spin_unlock_irqrestore(&chip->reg_lock, flags);
122                 return -ENODEV;
123         }
124         version = snd_sbdsp_version(chip);
125         if (version < 0) {
126                 spin_unlock_irqrestore(&chip->reg_lock, flags);
127                 return -ENODEV;
128         }
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);
134         
135         switch (chip->hardware) {
136         case SB_HW_AUTO:
137                 switch (major) {
138                 case 1:
139                         chip->hardware = SB_HW_10;
140                         str = "1.0";
141                         break;
142                 case 2:
143                         if (minor) {
144                                 chip->hardware = SB_HW_201;
145                                 str = "2.01+";
146                         } else {
147                                 chip->hardware = SB_HW_20;
148                                 str = "2.0";
149                         }
150                         break;
151                 case 3:
152                         chip->hardware = SB_HW_PRO;
153                         str = "Pro";
154                         break;
155                 case 4:
156                         chip->hardware = SB_HW_16;
157                         str = "16";
158                         break;
159                 default:
160                         snd_printk("SB [0x%lx]: unknown DSP chip version %i.%i\n",
161                                    chip->port, major, minor);
162                         return -ENODEV;
163                 }
164                 break;
165         case SB_HW_ALS100:
166                 str = "16 (ALS-100)";
167                 break;
168         case SB_HW_ALS4000:
169                 str = "16 (ALS-4000)";
170                 break;
171         case SB_HW_DT019X:
172                 str = "(DT019X/ALS007)";
173                 break;
174         default:
175                 return -ENODEV;
176         }
177         sprintf(chip->name, "Sound Blaster %s", str);
178         chip->version = (major << 8) | minor;
179         return 0;
180 }
181
182 static int snd_sbdsp_free(sb_t *chip)
183 {
184         if (chip->res_port) {
185                 release_resource(chip->res_port);
186                 kfree_nocheck(chip->res_port);
187         }
188         if (chip->irq >= 0)
189                 free_irq(chip->irq, (void *) chip);
190 #ifdef CONFIG_ISA
191         if (chip->dma8 >= 0) {
192                 disable_dma(chip->dma8);
193                 free_dma(chip->dma8);
194         }
195         if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
196                 disable_dma(chip->dma16);
197                 free_dma(chip->dma16);
198         }
199 #endif
200         snd_magic_kfree(chip);
201         return 0;
202 }
203
204 static int snd_sbdsp_dev_free(snd_device_t *device)
205 {
206         sb_t *chip = snd_magic_cast(sb_t, device->device_data, return -ENXIO);
207         return snd_sbdsp_free(chip);
208 }
209
210 int snd_sbdsp_create(snd_card_t *card,
211                      unsigned long port,
212                      int irq,
213                      irqreturn_t (*irq_handler)(int, void *, struct pt_regs *),
214                      int dma8,
215                      int dma16,
216                      unsigned short hardware,
217                      sb_t **r_chip)
218 {
219         sb_t *chip;
220         int err;
221         static snd_device_ops_t ops = {
222                 .dev_free =     snd_sbdsp_dev_free,
223         };
224
225         snd_assert(r_chip != NULL, return -EINVAL);
226         *r_chip = NULL;
227         chip = snd_magic_kcalloc(sb_t, 0, GFP_KERNEL);
228         if (chip == NULL)
229                 return -ENOMEM;
230         spin_lock_init(&chip->reg_lock);
231         spin_lock_init(&chip->open_lock);
232         spin_lock_init(&chip->midi_input_lock);
233         spin_lock_init(&chip->mixer_lock);
234         chip->irq = -1;
235         chip->dma8 = -1;
236         chip->dma16 = -1;
237         chip->port = port;
238         
239         if (request_irq(irq, irq_handler, hardware == SB_HW_ALS4000 ?
240                         SA_INTERRUPT | SA_SHIRQ : SA_INTERRUPT,
241                         "SoundBlaster", (void *) chip)) {
242                 snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
243                 snd_sbdsp_free(chip);
244                 return -EBUSY;
245         }
246         chip->irq = irq;
247
248         if (hardware == SB_HW_ALS4000)
249                 goto __skip_allocation;
250         
251         if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
252                 snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
253                 snd_sbdsp_free(chip);
254                 return -EBUSY;
255         }
256
257 #ifdef CONFIG_ISA
258         if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
259                 snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
260                 snd_sbdsp_free(chip);
261                 return -EBUSY;
262         }
263         chip->dma8 = dma8;
264         if (dma16 >= 0) {
265                 if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
266                         /* no duplex */
267                         dma16 = -1;
268                 } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
269                         snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
270                         snd_sbdsp_free(chip);
271                         return -EBUSY;
272                 }
273         }
274         chip->dma16 = dma16;
275 #endif
276
277       __skip_allocation:
278         chip->card = card;
279         chip->hardware = hardware;
280         if ((err = snd_sbdsp_probe(chip)) < 0) {
281                 snd_sbdsp_free(chip);
282                 return err;
283         }
284         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
285                 snd_sbdsp_free(chip);
286                 return err;
287         }
288         *r_chip = chip;
289         return 0;
290 }
291
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);
296 /* sb_mixer.c */
297 EXPORT_SYMBOL(snd_sbmixer_write);
298 EXPORT_SYMBOL(snd_sbmixer_read);
299 EXPORT_SYMBOL(snd_sbmixer_new);
300 EXPORT_SYMBOL(snd_sbmixer_add_ctl);
301
302 /*
303  *  INIT part
304  */
305
306 static int __init alsa_sb_common_init(void)
307 {
308         return 0;
309 }
310
311 static void __exit alsa_sb_common_exit(void)
312 {
313 }
314
315 module_init(alsa_sb_common_init)
316 module_exit(alsa_sb_common_exit)