11da177e4SLinus Torvalds /* 2c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 31da177e4SLinus Torvalds * Uros Bizjak <uros@kss-loka.si> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Lowlevel routines for control of Sound Blaster cards 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 91da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 101da177e4SLinus Torvalds * (at your option) any later version. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 131da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 141da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 151da177e4SLinus Torvalds * GNU General Public License for more details. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 181da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 191da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <sound/driver.h> 241da177e4SLinus Torvalds #include <linux/delay.h> 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/interrupt.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/ioport.h> 291da177e4SLinus Torvalds #include <sound/core.h> 301da177e4SLinus Torvalds #include <sound/sb.h> 311da177e4SLinus Torvalds #include <sound/initval.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <asm/io.h> 341da177e4SLinus Torvalds #include <asm/dma.h> 351da177e4SLinus Torvalds 36c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 371da177e4SLinus Torvalds MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards"); 381da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define BUSY_LOOPS 100000 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #undef IO_DEBUG 431da177e4SLinus Torvalds 44029d64b0STakashi Iwai int snd_sbdsp_command(struct snd_sb *chip, unsigned char val) 451da177e4SLinus Torvalds { 461da177e4SLinus Torvalds int i; 471da177e4SLinus Torvalds #ifdef IO_DEBUG 4899b359baSTakashi Iwai snd_printk(KERN_DEBUG "command 0x%x\n", val); 491da177e4SLinus Torvalds #endif 501da177e4SLinus Torvalds for (i = BUSY_LOOPS; i; i--) 511da177e4SLinus Torvalds if ((inb(SBP(chip, STATUS)) & 0x80) == 0) { 521da177e4SLinus Torvalds outb(val, SBP(chip, COMMAND)); 531da177e4SLinus Torvalds return 1; 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds snd_printd("%s [0x%lx]: timeout (0x%x)\n", __FUNCTION__, chip->port, val); 561da177e4SLinus Torvalds return 0; 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 59029d64b0STakashi Iwai int snd_sbdsp_get_byte(struct snd_sb *chip) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds int val; 621da177e4SLinus Torvalds int i; 631da177e4SLinus Torvalds for (i = BUSY_LOOPS; i; i--) { 641da177e4SLinus Torvalds if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { 651da177e4SLinus Torvalds val = inb(SBP(chip, READ)); 661da177e4SLinus Torvalds #ifdef IO_DEBUG 6799b359baSTakashi Iwai snd_printk(KERN_DEBUG "get_byte 0x%x\n", val); 681da177e4SLinus Torvalds #endif 691da177e4SLinus Torvalds return val; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds snd_printd("%s [0x%lx]: timeout\n", __FUNCTION__, chip->port); 731da177e4SLinus Torvalds return -ENODEV; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 76029d64b0STakashi Iwai int snd_sbdsp_reset(struct snd_sb *chip) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds int i; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds outb(1, SBP(chip, RESET)); 811da177e4SLinus Torvalds udelay(10); 821da177e4SLinus Torvalds outb(0, SBP(chip, RESET)); 831da177e4SLinus Torvalds udelay(30); 841da177e4SLinus Torvalds for (i = BUSY_LOOPS; i; i--) 851da177e4SLinus Torvalds if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { 861da177e4SLinus Torvalds if (inb(SBP(chip, READ)) == 0xaa) 871da177e4SLinus Torvalds return 0; 881da177e4SLinus Torvalds else 891da177e4SLinus Torvalds break; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds snd_printdd("%s [0x%lx] failed...\n", __FUNCTION__, chip->port); 921da177e4SLinus Torvalds return -ENODEV; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 95029d64b0STakashi Iwai static int snd_sbdsp_version(struct snd_sb * chip) 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds unsigned int result = -ENODEV; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds snd_sbdsp_command(chip, SB_DSP_GET_VERSION); 1001da177e4SLinus Torvalds result = (short) snd_sbdsp_get_byte(chip) << 8; 1011da177e4SLinus Torvalds result |= (short) snd_sbdsp_get_byte(chip); 1021da177e4SLinus Torvalds return result; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 105029d64b0STakashi Iwai static int snd_sbdsp_probe(struct snd_sb * chip) 1061da177e4SLinus Torvalds { 1071da177e4SLinus Torvalds int version; 1081da177e4SLinus Torvalds int major, minor; 1091da177e4SLinus Torvalds char *str; 1101da177e4SLinus Torvalds unsigned long flags; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* 1131da177e4SLinus Torvalds * initialization sequence 1141da177e4SLinus Torvalds */ 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 1171da177e4SLinus Torvalds if (snd_sbdsp_reset(chip) < 0) { 1181da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 1191da177e4SLinus Torvalds return -ENODEV; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds version = snd_sbdsp_version(chip); 1221da177e4SLinus Torvalds if (version < 0) { 1231da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 1241da177e4SLinus Torvalds return -ENODEV; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 1271da177e4SLinus Torvalds major = version >> 8; 1281da177e4SLinus Torvalds minor = version & 0xff; 1291da177e4SLinus Torvalds snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n", 1301da177e4SLinus Torvalds chip->port, major, minor); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds switch (chip->hardware) { 1331da177e4SLinus Torvalds case SB_HW_AUTO: 1341da177e4SLinus Torvalds switch (major) { 1351da177e4SLinus Torvalds case 1: 1361da177e4SLinus Torvalds chip->hardware = SB_HW_10; 1371da177e4SLinus Torvalds str = "1.0"; 1381da177e4SLinus Torvalds break; 1391da177e4SLinus Torvalds case 2: 1401da177e4SLinus Torvalds if (minor) { 1411da177e4SLinus Torvalds chip->hardware = SB_HW_201; 1421da177e4SLinus Torvalds str = "2.01+"; 1431da177e4SLinus Torvalds } else { 1441da177e4SLinus Torvalds chip->hardware = SB_HW_20; 1451da177e4SLinus Torvalds str = "2.0"; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds break; 1481da177e4SLinus Torvalds case 3: 1491da177e4SLinus Torvalds chip->hardware = SB_HW_PRO; 1501da177e4SLinus Torvalds str = "Pro"; 1511da177e4SLinus Torvalds break; 1521da177e4SLinus Torvalds case 4: 1531da177e4SLinus Torvalds chip->hardware = SB_HW_16; 1541da177e4SLinus Torvalds str = "16"; 1551da177e4SLinus Torvalds break; 1561da177e4SLinus Torvalds default: 15799b359baSTakashi Iwai snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n", 1581da177e4SLinus Torvalds chip->port, major, minor); 1591da177e4SLinus Torvalds return -ENODEV; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds break; 1621da177e4SLinus Torvalds case SB_HW_ALS100: 1631da177e4SLinus Torvalds str = "16 (ALS-100)"; 1641da177e4SLinus Torvalds break; 1651da177e4SLinus Torvalds case SB_HW_ALS4000: 1661da177e4SLinus Torvalds str = "16 (ALS-4000)"; 1671da177e4SLinus Torvalds break; 1681da177e4SLinus Torvalds case SB_HW_DT019X: 1691da177e4SLinus Torvalds str = "(DT019X/ALS007)"; 170621887aeSTakashi Iwai break; 171621887aeSTakashi Iwai case SB_HW_CS5530: 172621887aeSTakashi Iwai str = "16 (CS5530)"; 1731da177e4SLinus Torvalds break; 1741da177e4SLinus Torvalds default: 1751da177e4SLinus Torvalds return -ENODEV; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds sprintf(chip->name, "Sound Blaster %s", str); 1781da177e4SLinus Torvalds chip->version = (major << 8) | minor; 1791da177e4SLinus Torvalds return 0; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 182029d64b0STakashi Iwai static int snd_sbdsp_free(struct snd_sb *chip) 1831da177e4SLinus Torvalds { 184b1d5776dSTakashi Iwai if (chip->res_port) 185b1d5776dSTakashi Iwai release_and_free_resource(chip->res_port); 1861da177e4SLinus Torvalds if (chip->irq >= 0) 1871da177e4SLinus Torvalds free_irq(chip->irq, (void *) chip); 1881da177e4SLinus Torvalds #ifdef CONFIG_ISA 1891da177e4SLinus Torvalds if (chip->dma8 >= 0) { 1901da177e4SLinus Torvalds disable_dma(chip->dma8); 1911da177e4SLinus Torvalds free_dma(chip->dma8); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) { 1941da177e4SLinus Torvalds disable_dma(chip->dma16); 1951da177e4SLinus Torvalds free_dma(chip->dma16); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds #endif 1981da177e4SLinus Torvalds kfree(chip); 1991da177e4SLinus Torvalds return 0; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 202029d64b0STakashi Iwai static int snd_sbdsp_dev_free(struct snd_device *device) 2031da177e4SLinus Torvalds { 204029d64b0STakashi Iwai struct snd_sb *chip = device->device_data; 2051da177e4SLinus Torvalds return snd_sbdsp_free(chip); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 208029d64b0STakashi Iwai int snd_sbdsp_create(struct snd_card *card, 2091da177e4SLinus Torvalds unsigned long port, 2101da177e4SLinus Torvalds int irq, 2117d12e780SDavid Howells irq_handler_t irq_handler, 2121da177e4SLinus Torvalds int dma8, 2131da177e4SLinus Torvalds int dma16, 2141da177e4SLinus Torvalds unsigned short hardware, 215029d64b0STakashi Iwai struct snd_sb **r_chip) 2161da177e4SLinus Torvalds { 217029d64b0STakashi Iwai struct snd_sb *chip; 2181da177e4SLinus Torvalds int err; 219029d64b0STakashi Iwai static struct snd_device_ops ops = { 2201da177e4SLinus Torvalds .dev_free = snd_sbdsp_dev_free, 2211da177e4SLinus Torvalds }; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds snd_assert(r_chip != NULL, return -EINVAL); 2241da177e4SLinus Torvalds *r_chip = NULL; 2259e76a76eSTakashi Iwai chip = kzalloc(sizeof(*chip), GFP_KERNEL); 2261da177e4SLinus Torvalds if (chip == NULL) 2271da177e4SLinus Torvalds return -ENOMEM; 2281da177e4SLinus Torvalds spin_lock_init(&chip->reg_lock); 2291da177e4SLinus Torvalds spin_lock_init(&chip->open_lock); 2301da177e4SLinus Torvalds spin_lock_init(&chip->midi_input_lock); 2311da177e4SLinus Torvalds spin_lock_init(&chip->mixer_lock); 2321da177e4SLinus Torvalds chip->irq = -1; 2331da177e4SLinus Torvalds chip->dma8 = -1; 2341da177e4SLinus Torvalds chip->dma16 = -1; 2351da177e4SLinus Torvalds chip->port = port; 2361da177e4SLinus Torvalds 2372d4a485bSTakashi Iwai if (request_irq(irq, irq_handler, 2382d4a485bSTakashi Iwai (hardware == SB_HW_ALS4000 || 2392d4a485bSTakashi Iwai hardware == SB_HW_CS5530) ? 240437a5a46STakashi Iwai IRQF_SHARED : IRQF_DISABLED, 2411da177e4SLinus Torvalds "SoundBlaster", (void *) chip)) { 2421da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq); 2431da177e4SLinus Torvalds snd_sbdsp_free(chip); 2441da177e4SLinus Torvalds return -EBUSY; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds chip->irq = irq; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds if (hardware == SB_HW_ALS4000) 2491da177e4SLinus Torvalds goto __skip_allocation; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) { 2521da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port); 2531da177e4SLinus Torvalds snd_sbdsp_free(chip); 2541da177e4SLinus Torvalds return -EBUSY; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds #ifdef CONFIG_ISA 2581da177e4SLinus Torvalds if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) { 2591da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8); 2601da177e4SLinus Torvalds snd_sbdsp_free(chip); 2611da177e4SLinus Torvalds return -EBUSY; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds chip->dma8 = dma8; 2641da177e4SLinus Torvalds if (dma16 >= 0) { 2651da177e4SLinus Torvalds if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) { 2661da177e4SLinus Torvalds /* no duplex */ 2671da177e4SLinus Torvalds dma16 = -1; 2681da177e4SLinus Torvalds } else if (request_dma(dma16, "SoundBlaster - 16bit")) { 2691da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16); 2701da177e4SLinus Torvalds snd_sbdsp_free(chip); 2711da177e4SLinus Torvalds return -EBUSY; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds chip->dma16 = dma16; 2751da177e4SLinus Torvalds #endif 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds __skip_allocation: 2781da177e4SLinus Torvalds chip->card = card; 2791da177e4SLinus Torvalds chip->hardware = hardware; 2801da177e4SLinus Torvalds if ((err = snd_sbdsp_probe(chip)) < 0) { 2811da177e4SLinus Torvalds snd_sbdsp_free(chip); 2821da177e4SLinus Torvalds return err; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { 2851da177e4SLinus Torvalds snd_sbdsp_free(chip); 2861da177e4SLinus Torvalds return err; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds *r_chip = chip; 2891da177e4SLinus Torvalds return 0; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_command); 2931da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_get_byte); 2941da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_reset); 2951da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_create); 2961da177e4SLinus Torvalds /* sb_mixer.c */ 2971da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_write); 2981da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_read); 2991da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_new); 3001da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_add_ctl); 3015bdb6a16STakashi Iwai #ifdef CONFIG_PM 3025bdb6a16STakashi Iwai EXPORT_SYMBOL(snd_sbmixer_suspend); 3035bdb6a16STakashi Iwai EXPORT_SYMBOL(snd_sbmixer_resume); 3045bdb6a16STakashi Iwai #endif 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* 3071da177e4SLinus Torvalds * INIT part 3081da177e4SLinus Torvalds */ 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static int __init alsa_sb_common_init(void) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds return 0; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds static void __exit alsa_sb_common_exit(void) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds module_init(alsa_sb_common_init) 3201da177e4SLinus Torvalds module_exit(alsa_sb_common_exit) 321