11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) by Jaroslav Kysela <perex@suse.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 361da177e4SLinus Torvalds MODULE_AUTHOR("Jaroslav Kysela <perex@suse.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)"; 1701da177e4SLinus Torvalds break; 1711da177e4SLinus Torvalds default: 1721da177e4SLinus Torvalds return -ENODEV; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds sprintf(chip->name, "Sound Blaster %s", str); 1751da177e4SLinus Torvalds chip->version = (major << 8) | minor; 1761da177e4SLinus Torvalds return 0; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179029d64b0STakashi Iwai static int snd_sbdsp_free(struct snd_sb *chip) 1801da177e4SLinus Torvalds { 181b1d5776dSTakashi Iwai if (chip->res_port) 182b1d5776dSTakashi Iwai release_and_free_resource(chip->res_port); 1831da177e4SLinus Torvalds if (chip->irq >= 0) 1841da177e4SLinus Torvalds free_irq(chip->irq, (void *) chip); 1851da177e4SLinus Torvalds #ifdef CONFIG_ISA 1861da177e4SLinus Torvalds if (chip->dma8 >= 0) { 1871da177e4SLinus Torvalds disable_dma(chip->dma8); 1881da177e4SLinus Torvalds free_dma(chip->dma8); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) { 1911da177e4SLinus Torvalds disable_dma(chip->dma16); 1921da177e4SLinus Torvalds free_dma(chip->dma16); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds #endif 1951da177e4SLinus Torvalds kfree(chip); 1961da177e4SLinus Torvalds return 0; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 199029d64b0STakashi Iwai static int snd_sbdsp_dev_free(struct snd_device *device) 2001da177e4SLinus Torvalds { 201029d64b0STakashi Iwai struct snd_sb *chip = device->device_data; 2021da177e4SLinus Torvalds return snd_sbdsp_free(chip); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205029d64b0STakashi Iwai int snd_sbdsp_create(struct snd_card *card, 2061da177e4SLinus Torvalds unsigned long port, 2071da177e4SLinus Torvalds int irq, 2087d12e780SDavid Howells irq_handler_t irq_handler, 2091da177e4SLinus Torvalds int dma8, 2101da177e4SLinus Torvalds int dma16, 2111da177e4SLinus Torvalds unsigned short hardware, 212029d64b0STakashi Iwai struct snd_sb **r_chip) 2131da177e4SLinus Torvalds { 214029d64b0STakashi Iwai struct snd_sb *chip; 2151da177e4SLinus Torvalds int err; 216029d64b0STakashi Iwai static struct snd_device_ops ops = { 2171da177e4SLinus Torvalds .dev_free = snd_sbdsp_dev_free, 2181da177e4SLinus Torvalds }; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds snd_assert(r_chip != NULL, return -EINVAL); 2211da177e4SLinus Torvalds *r_chip = NULL; 2229e76a76eSTakashi Iwai chip = kzalloc(sizeof(*chip), GFP_KERNEL); 2231da177e4SLinus Torvalds if (chip == NULL) 2241da177e4SLinus Torvalds return -ENOMEM; 2251da177e4SLinus Torvalds spin_lock_init(&chip->reg_lock); 2261da177e4SLinus Torvalds spin_lock_init(&chip->open_lock); 2271da177e4SLinus Torvalds spin_lock_init(&chip->midi_input_lock); 2281da177e4SLinus Torvalds spin_lock_init(&chip->mixer_lock); 2291da177e4SLinus Torvalds chip->irq = -1; 2301da177e4SLinus Torvalds chip->dma8 = -1; 2311da177e4SLinus Torvalds chip->dma16 = -1; 2321da177e4SLinus Torvalds chip->port = port; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds if (request_irq(irq, irq_handler, hardware == SB_HW_ALS4000 ? 23565ca68b3SThomas Gleixner IRQF_DISABLED | IRQF_SHARED : IRQF_DISABLED, 2361da177e4SLinus Torvalds "SoundBlaster", (void *) chip)) { 2371da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq); 2381da177e4SLinus Torvalds snd_sbdsp_free(chip); 2391da177e4SLinus Torvalds return -EBUSY; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds chip->irq = irq; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds if (hardware == SB_HW_ALS4000) 2441da177e4SLinus Torvalds goto __skip_allocation; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) { 2471da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port); 2481da177e4SLinus Torvalds snd_sbdsp_free(chip); 2491da177e4SLinus Torvalds return -EBUSY; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds #ifdef CONFIG_ISA 2531da177e4SLinus Torvalds if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) { 2541da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8); 2551da177e4SLinus Torvalds snd_sbdsp_free(chip); 2561da177e4SLinus Torvalds return -EBUSY; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds chip->dma8 = dma8; 2591da177e4SLinus Torvalds if (dma16 >= 0) { 2601da177e4SLinus Torvalds if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) { 2611da177e4SLinus Torvalds /* no duplex */ 2621da177e4SLinus Torvalds dma16 = -1; 2631da177e4SLinus Torvalds } else if (request_dma(dma16, "SoundBlaster - 16bit")) { 2641da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16); 2651da177e4SLinus Torvalds snd_sbdsp_free(chip); 2661da177e4SLinus Torvalds return -EBUSY; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds chip->dma16 = dma16; 2701da177e4SLinus Torvalds #endif 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds __skip_allocation: 2731da177e4SLinus Torvalds chip->card = card; 2741da177e4SLinus Torvalds chip->hardware = hardware; 2751da177e4SLinus Torvalds if ((err = snd_sbdsp_probe(chip)) < 0) { 2761da177e4SLinus Torvalds snd_sbdsp_free(chip); 2771da177e4SLinus Torvalds return err; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { 2801da177e4SLinus Torvalds snd_sbdsp_free(chip); 2811da177e4SLinus Torvalds return err; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds *r_chip = chip; 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_command); 2881da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_get_byte); 2891da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_reset); 2901da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_create); 2911da177e4SLinus Torvalds /* sb_mixer.c */ 2921da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_write); 2931da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_read); 2941da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_new); 2951da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_add_ctl); 2965bdb6a16STakashi Iwai #ifdef CONFIG_PM 2975bdb6a16STakashi Iwai EXPORT_SYMBOL(snd_sbmixer_suspend); 2985bdb6a16STakashi Iwai EXPORT_SYMBOL(snd_sbmixer_resume); 2995bdb6a16STakashi Iwai #endif 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* 3021da177e4SLinus Torvalds * INIT part 3031da177e4SLinus Torvalds */ 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds static int __init alsa_sb_common_init(void) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds return 0; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static void __exit alsa_sb_common_exit(void) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds module_init(alsa_sb_common_init) 3151da177e4SLinus Torvalds module_exit(alsa_sb_common_exit) 316