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