Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 2 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/ioport.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/sb.h> |
| 30 | #include <sound/initval.h> |
| 31 | |
| 32 | #include <asm/io.h> |
| 33 | #include <asm/dma.h> |
| 34 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 35 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards"); |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | |
| 39 | #define BUSY_LOOPS 100000 |
| 40 | |
| 41 | #undef IO_DEBUG |
| 42 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 43 | int snd_sbdsp_command(struct snd_sb *chip, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | int i; |
| 46 | #ifdef IO_DEBUG |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 47 | snd_printk(KERN_DEBUG "command 0x%x\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #endif |
| 49 | for (i = BUSY_LOOPS; i; i--) |
| 50 | if ((inb(SBP(chip, STATUS)) & 0x80) == 0) { |
| 51 | outb(val, SBP(chip, COMMAND)); |
| 52 | return 1; |
| 53 | } |
| 54 | snd_printd("%s [0x%lx]: timeout (0x%x)\n", __FUNCTION__, chip->port, val); |
| 55 | return 0; |
| 56 | } |
| 57 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 58 | int snd_sbdsp_get_byte(struct snd_sb *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | int val; |
| 61 | int i; |
| 62 | for (i = BUSY_LOOPS; i; i--) { |
| 63 | if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { |
| 64 | val = inb(SBP(chip, READ)); |
| 65 | #ifdef IO_DEBUG |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 66 | snd_printk(KERN_DEBUG "get_byte 0x%x\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #endif |
| 68 | return val; |
| 69 | } |
| 70 | } |
| 71 | snd_printd("%s [0x%lx]: timeout\n", __FUNCTION__, chip->port); |
| 72 | return -ENODEV; |
| 73 | } |
| 74 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 75 | int snd_sbdsp_reset(struct snd_sb *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | int i; |
| 78 | |
| 79 | outb(1, SBP(chip, RESET)); |
| 80 | udelay(10); |
| 81 | outb(0, SBP(chip, RESET)); |
| 82 | udelay(30); |
| 83 | for (i = BUSY_LOOPS; i; i--) |
| 84 | if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { |
| 85 | if (inb(SBP(chip, READ)) == 0xaa) |
| 86 | return 0; |
| 87 | else |
| 88 | break; |
| 89 | } |
| 90 | snd_printdd("%s [0x%lx] failed...\n", __FUNCTION__, chip->port); |
| 91 | return -ENODEV; |
| 92 | } |
| 93 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 94 | static int snd_sbdsp_version(struct snd_sb * chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | unsigned int result = -ENODEV; |
| 97 | |
| 98 | snd_sbdsp_command(chip, SB_DSP_GET_VERSION); |
| 99 | result = (short) snd_sbdsp_get_byte(chip) << 8; |
| 100 | result |= (short) snd_sbdsp_get_byte(chip); |
| 101 | return result; |
| 102 | } |
| 103 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 104 | static int snd_sbdsp_probe(struct snd_sb * chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | int version; |
| 107 | int major, minor; |
| 108 | char *str; |
| 109 | unsigned long flags; |
| 110 | |
| 111 | /* |
| 112 | * initialization sequence |
| 113 | */ |
| 114 | |
| 115 | spin_lock_irqsave(&chip->reg_lock, flags); |
| 116 | if (snd_sbdsp_reset(chip) < 0) { |
| 117 | spin_unlock_irqrestore(&chip->reg_lock, flags); |
| 118 | return -ENODEV; |
| 119 | } |
| 120 | version = snd_sbdsp_version(chip); |
| 121 | if (version < 0) { |
| 122 | spin_unlock_irqrestore(&chip->reg_lock, flags); |
| 123 | return -ENODEV; |
| 124 | } |
| 125 | spin_unlock_irqrestore(&chip->reg_lock, flags); |
| 126 | major = version >> 8; |
| 127 | minor = version & 0xff; |
| 128 | snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n", |
| 129 | chip->port, major, minor); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | switch (chip->hardware) { |
| 132 | case SB_HW_AUTO: |
| 133 | switch (major) { |
| 134 | case 1: |
| 135 | chip->hardware = SB_HW_10; |
| 136 | str = "1.0"; |
| 137 | break; |
| 138 | case 2: |
| 139 | if (minor) { |
| 140 | chip->hardware = SB_HW_201; |
| 141 | str = "2.01+"; |
| 142 | } else { |
| 143 | chip->hardware = SB_HW_20; |
| 144 | str = "2.0"; |
| 145 | } |
| 146 | break; |
| 147 | case 3: |
| 148 | chip->hardware = SB_HW_PRO; |
| 149 | str = "Pro"; |
| 150 | break; |
| 151 | case 4: |
| 152 | chip->hardware = SB_HW_16; |
| 153 | str = "16"; |
| 154 | break; |
| 155 | default: |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 156 | snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | chip->port, major, minor); |
| 158 | return -ENODEV; |
| 159 | } |
| 160 | break; |
| 161 | case SB_HW_ALS100: |
| 162 | str = "16 (ALS-100)"; |
| 163 | break; |
| 164 | case SB_HW_ALS4000: |
| 165 | str = "16 (ALS-4000)"; |
| 166 | break; |
| 167 | case SB_HW_DT019X: |
| 168 | str = "(DT019X/ALS007)"; |
| 169 | break; |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 170 | case SB_HW_CS5530: |
| 171 | str = "16 (CS5530)"; |
| 172 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | default: |
| 174 | return -ENODEV; |
| 175 | } |
| 176 | sprintf(chip->name, "Sound Blaster %s", str); |
| 177 | chip->version = (major << 8) | minor; |
| 178 | return 0; |
| 179 | } |
| 180 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 181 | static int snd_sbdsp_free(struct snd_sb *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Takashi Iwai | b1d5776 | 2005-10-10 11:56:31 +0200 | [diff] [blame] | 183 | if (chip->res_port) |
| 184 | release_and_free_resource(chip->res_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 201 | static int snd_sbdsp_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 203 | struct snd_sb *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return snd_sbdsp_free(chip); |
| 205 | } |
| 206 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 207 | int snd_sbdsp_create(struct snd_card *card, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | unsigned long port, |
| 209 | int irq, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 210 | irq_handler_t irq_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | int dma8, |
| 212 | int dma16, |
| 213 | unsigned short hardware, |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 214 | struct snd_sb **r_chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 216 | struct snd_sb *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int err; |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 218 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | .dev_free = snd_sbdsp_dev_free, |
| 220 | }; |
| 221 | |
| 222 | snd_assert(r_chip != NULL, return -EINVAL); |
| 223 | *r_chip = NULL; |
Takashi Iwai | 9e76a76 | 2005-09-09 14:21:17 +0200 | [diff] [blame] | 224 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 2d4a485 | 2007-08-29 12:54:25 +0200 | [diff] [blame] | 236 | if (request_irq(irq, irq_handler, |
| 237 | (hardware == SB_HW_ALS4000 || |
| 238 | hardware == SB_HW_CS5530) ? |
Takashi Iwai | 437a5a4 | 2006-11-21 12:14:23 +0100 | [diff] [blame] | 239 | IRQF_SHARED : IRQF_DISABLED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | "SoundBlaster", (void *) chip)) { |
| 241 | snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq); |
| 242 | snd_sbdsp_free(chip); |
| 243 | return -EBUSY; |
| 244 | } |
| 245 | chip->irq = irq; |
| 246 | |
| 247 | if (hardware == SB_HW_ALS4000) |
| 248 | goto __skip_allocation; |
| 249 | |
| 250 | if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) { |
| 251 | snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port); |
| 252 | snd_sbdsp_free(chip); |
| 253 | return -EBUSY; |
| 254 | } |
| 255 | |
| 256 | #ifdef CONFIG_ISA |
| 257 | if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) { |
| 258 | snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8); |
| 259 | snd_sbdsp_free(chip); |
| 260 | return -EBUSY; |
| 261 | } |
| 262 | chip->dma8 = dma8; |
| 263 | if (dma16 >= 0) { |
| 264 | if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) { |
| 265 | /* no duplex */ |
| 266 | dma16 = -1; |
| 267 | } else if (request_dma(dma16, "SoundBlaster - 16bit")) { |
| 268 | snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16); |
| 269 | snd_sbdsp_free(chip); |
| 270 | return -EBUSY; |
| 271 | } |
| 272 | } |
| 273 | chip->dma16 = dma16; |
| 274 | #endif |
| 275 | |
| 276 | __skip_allocation: |
| 277 | chip->card = card; |
| 278 | chip->hardware = hardware; |
| 279 | if ((err = snd_sbdsp_probe(chip)) < 0) { |
| 280 | snd_sbdsp_free(chip); |
| 281 | return err; |
| 282 | } |
| 283 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { |
| 284 | snd_sbdsp_free(chip); |
| 285 | return err; |
| 286 | } |
| 287 | *r_chip = chip; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | EXPORT_SYMBOL(snd_sbdsp_command); |
| 292 | EXPORT_SYMBOL(snd_sbdsp_get_byte); |
| 293 | EXPORT_SYMBOL(snd_sbdsp_reset); |
| 294 | EXPORT_SYMBOL(snd_sbdsp_create); |
| 295 | /* sb_mixer.c */ |
| 296 | EXPORT_SYMBOL(snd_sbmixer_write); |
| 297 | EXPORT_SYMBOL(snd_sbmixer_read); |
| 298 | EXPORT_SYMBOL(snd_sbmixer_new); |
| 299 | EXPORT_SYMBOL(snd_sbmixer_add_ctl); |
Takashi Iwai | 5bdb6a1 | 2005-11-17 16:16:10 +0100 | [diff] [blame] | 300 | #ifdef CONFIG_PM |
| 301 | EXPORT_SYMBOL(snd_sbmixer_suspend); |
| 302 | EXPORT_SYMBOL(snd_sbmixer_resume); |
| 303 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * INIT part |
| 307 | */ |
| 308 | |
| 309 | static int __init alsa_sb_common_init(void) |
| 310 | { |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static void __exit alsa_sb_common_exit(void) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | module_init(alsa_sb_common_init) |
| 319 | module_exit(alsa_sb_common_exit) |