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> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 36 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 44 | int snd_sbdsp_command(struct snd_sb *chip, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | int i; |
| 47 | #ifdef IO_DEBUG |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 48 | snd_printk(KERN_DEBUG "command 0x%x\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Harvey Harrison | 9bf8e7d | 2008-03-03 15:32:18 -0800 | [diff] [blame] | 55 | snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 59 | int snd_sbdsp_get_byte(struct snd_sb *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 67 | snd_printk(KERN_DEBUG "get_byte 0x%x\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #endif |
| 69 | return val; |
| 70 | } |
| 71 | } |
Harvey Harrison | 9bf8e7d | 2008-03-03 15:32:18 -0800 | [diff] [blame] | 72 | snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return -ENODEV; |
| 74 | } |
| 75 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 76 | int snd_sbdsp_reset(struct snd_sb *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Harvey Harrison | 9bf8e7d | 2008-03-03 15:32:18 -0800 | [diff] [blame] | 91 | snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return -ENODEV; |
| 93 | } |
| 94 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 95 | static int snd_sbdsp_version(struct snd_sb * chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 105 | static int snd_sbdsp_probe(struct snd_sb * chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 157 | 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] | 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; |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 171 | case SB_HW_CS5530: |
| 172 | str = "16 (CS5530)"; |
| 173 | break; |
Krzysztof Helt | ad8decb | 2009-12-20 19:01:50 +0100 | [diff] [blame] | 174 | case SB_HW_JAZZ16: |
| 175 | str = "Pro (Jazz16)"; |
| 176 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | default: |
| 178 | return -ENODEV; |
| 179 | } |
| 180 | sprintf(chip->name, "Sound Blaster %s", str); |
| 181 | chip->version = (major << 8) | minor; |
| 182 | return 0; |
| 183 | } |
| 184 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 185 | static int snd_sbdsp_free(struct snd_sb *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Takashi Iwai | b1d5776 | 2005-10-10 11:56:31 +0200 | [diff] [blame] | 187 | if (chip->res_port) |
| 188 | release_and_free_resource(chip->res_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (chip->irq >= 0) |
| 190 | free_irq(chip->irq, (void *) chip); |
| 191 | #ifdef CONFIG_ISA |
| 192 | if (chip->dma8 >= 0) { |
| 193 | disable_dma(chip->dma8); |
| 194 | free_dma(chip->dma8); |
| 195 | } |
| 196 | if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) { |
| 197 | disable_dma(chip->dma16); |
| 198 | free_dma(chip->dma16); |
| 199 | } |
| 200 | #endif |
| 201 | kfree(chip); |
| 202 | return 0; |
| 203 | } |
| 204 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 205 | static int snd_sbdsp_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 207 | struct snd_sb *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return snd_sbdsp_free(chip); |
| 209 | } |
| 210 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 211 | int snd_sbdsp_create(struct snd_card *card, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | unsigned long port, |
| 213 | int irq, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 214 | irq_handler_t irq_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | int dma8, |
| 216 | int dma16, |
| 217 | unsigned short hardware, |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 218 | struct snd_sb **r_chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 220 | struct snd_sb *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | int err; |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 222 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | .dev_free = snd_sbdsp_dev_free, |
| 224 | }; |
| 225 | |
Takashi Iwai | 622207d | 2008-08-08 17:11:45 +0200 | [diff] [blame] | 226 | if (snd_BUG_ON(!r_chip)) |
| 227 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | *r_chip = NULL; |
Takashi Iwai | 9e76a76 | 2005-09-09 14:21:17 +0200 | [diff] [blame] | 229 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (chip == NULL) |
| 231 | return -ENOMEM; |
| 232 | spin_lock_init(&chip->reg_lock); |
| 233 | spin_lock_init(&chip->open_lock); |
| 234 | spin_lock_init(&chip->midi_input_lock); |
| 235 | spin_lock_init(&chip->mixer_lock); |
| 236 | chip->irq = -1; |
| 237 | chip->dma8 = -1; |
| 238 | chip->dma16 = -1; |
| 239 | chip->port = port; |
| 240 | |
Takashi Iwai | 2d4a485 | 2007-08-29 12:54:25 +0200 | [diff] [blame] | 241 | if (request_irq(irq, irq_handler, |
| 242 | (hardware == SB_HW_ALS4000 || |
| 243 | hardware == SB_HW_CS5530) ? |
Yong Zhang | 88e24c3 | 2011-09-22 16:59:20 +0800 | [diff] [blame] | 244 | IRQF_SHARED : 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | "SoundBlaster", (void *) chip)) { |
| 246 | snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq); |
| 247 | snd_sbdsp_free(chip); |
| 248 | return -EBUSY; |
| 249 | } |
| 250 | chip->irq = irq; |
| 251 | |
| 252 | if (hardware == SB_HW_ALS4000) |
| 253 | goto __skip_allocation; |
| 254 | |
| 255 | if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) { |
| 256 | snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port); |
| 257 | snd_sbdsp_free(chip); |
| 258 | return -EBUSY; |
| 259 | } |
| 260 | |
| 261 | #ifdef CONFIG_ISA |
| 262 | if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) { |
| 263 | snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8); |
| 264 | snd_sbdsp_free(chip); |
| 265 | return -EBUSY; |
| 266 | } |
| 267 | chip->dma8 = dma8; |
| 268 | if (dma16 >= 0) { |
| 269 | if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) { |
| 270 | /* no duplex */ |
| 271 | dma16 = -1; |
| 272 | } else if (request_dma(dma16, "SoundBlaster - 16bit")) { |
| 273 | snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16); |
| 274 | snd_sbdsp_free(chip); |
| 275 | return -EBUSY; |
| 276 | } |
| 277 | } |
| 278 | chip->dma16 = dma16; |
| 279 | #endif |
| 280 | |
| 281 | __skip_allocation: |
| 282 | chip->card = card; |
| 283 | chip->hardware = hardware; |
| 284 | if ((err = snd_sbdsp_probe(chip)) < 0) { |
| 285 | snd_sbdsp_free(chip); |
| 286 | return err; |
| 287 | } |
| 288 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { |
| 289 | snd_sbdsp_free(chip); |
| 290 | return err; |
| 291 | } |
| 292 | *r_chip = chip; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | EXPORT_SYMBOL(snd_sbdsp_command); |
| 297 | EXPORT_SYMBOL(snd_sbdsp_get_byte); |
| 298 | EXPORT_SYMBOL(snd_sbdsp_reset); |
| 299 | EXPORT_SYMBOL(snd_sbdsp_create); |
| 300 | /* sb_mixer.c */ |
| 301 | EXPORT_SYMBOL(snd_sbmixer_write); |
| 302 | EXPORT_SYMBOL(snd_sbmixer_read); |
| 303 | EXPORT_SYMBOL(snd_sbmixer_new); |
| 304 | EXPORT_SYMBOL(snd_sbmixer_add_ctl); |
Takashi Iwai | 5bdb6a1 | 2005-11-17 16:16:10 +0100 | [diff] [blame] | 305 | #ifdef CONFIG_PM |
| 306 | EXPORT_SYMBOL(snd_sbmixer_suspend); |
| 307 | EXPORT_SYMBOL(snd_sbmixer_resume); |
| 308 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * INIT part |
| 312 | */ |
| 313 | |
| 314 | static int __init alsa_sb_common_init(void) |
| 315 | { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static void __exit alsa_sb_common_exit(void) |
| 320 | { |
| 321 | } |
| 322 | |
| 323 | module_init(alsa_sb_common_init) |
| 324 | module_exit(alsa_sb_common_exit) |