11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 3c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 41da177e4SLinus Torvalds * Uros Bizjak <uros@kss-loka.si> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Lowlevel routines for control of Sound Blaster cards 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/delay.h> 101da177e4SLinus Torvalds #include <linux/init.h> 111da177e4SLinus Torvalds #include <linux/interrupt.h> 121da177e4SLinus Torvalds #include <linux/slab.h> 131da177e4SLinus Torvalds #include <linux/ioport.h> 14da155d5bSPaul Gortmaker #include <linux/module.h> 156cbbfe1cSTakashi Iwai #include <linux/io.h> 161da177e4SLinus Torvalds #include <sound/core.h> 171da177e4SLinus Torvalds #include <sound/sb.h> 181da177e4SLinus Torvalds #include <sound/initval.h> 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <asm/dma.h> 211da177e4SLinus Torvalds 22c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 231da177e4SLinus Torvalds MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards"); 241da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #define BUSY_LOOPS 100000 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #undef IO_DEBUG 291da177e4SLinus Torvalds 30029d64b0STakashi Iwai int snd_sbdsp_command(struct snd_sb *chip, unsigned char val) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds int i; 331da177e4SLinus Torvalds #ifdef IO_DEBUG 3499b359baSTakashi Iwai snd_printk(KERN_DEBUG "command 0x%x\n", val); 351da177e4SLinus Torvalds #endif 361da177e4SLinus Torvalds for (i = BUSY_LOOPS; i; i--) 371da177e4SLinus Torvalds if ((inb(SBP(chip, STATUS)) & 0x80) == 0) { 381da177e4SLinus Torvalds outb(val, SBP(chip, COMMAND)); 391da177e4SLinus Torvalds return 1; 401da177e4SLinus Torvalds } 419bf8e7ddSHarvey Harrison snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val); 421da177e4SLinus Torvalds return 0; 431da177e4SLinus Torvalds } 441da177e4SLinus Torvalds 45029d64b0STakashi Iwai int snd_sbdsp_get_byte(struct snd_sb *chip) 461da177e4SLinus Torvalds { 471da177e4SLinus Torvalds int val; 481da177e4SLinus Torvalds int i; 491da177e4SLinus Torvalds for (i = BUSY_LOOPS; i; i--) { 501da177e4SLinus Torvalds if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { 511da177e4SLinus Torvalds val = inb(SBP(chip, READ)); 521da177e4SLinus Torvalds #ifdef IO_DEBUG 5399b359baSTakashi Iwai snd_printk(KERN_DEBUG "get_byte 0x%x\n", val); 541da177e4SLinus Torvalds #endif 551da177e4SLinus Torvalds return val; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds } 589bf8e7ddSHarvey Harrison snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port); 591da177e4SLinus Torvalds return -ENODEV; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 62029d64b0STakashi Iwai int snd_sbdsp_reset(struct snd_sb *chip) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds int i; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds outb(1, SBP(chip, RESET)); 671da177e4SLinus Torvalds udelay(10); 681da177e4SLinus Torvalds outb(0, SBP(chip, RESET)); 691da177e4SLinus Torvalds udelay(30); 701da177e4SLinus Torvalds for (i = BUSY_LOOPS; i; i--) 711da177e4SLinus Torvalds if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { 721da177e4SLinus Torvalds if (inb(SBP(chip, READ)) == 0xaa) 731da177e4SLinus Torvalds return 0; 741da177e4SLinus Torvalds else 751da177e4SLinus Torvalds break; 761da177e4SLinus Torvalds } 779bf8e7ddSHarvey Harrison snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port); 781da177e4SLinus Torvalds return -ENODEV; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 81029d64b0STakashi Iwai static int snd_sbdsp_version(struct snd_sb * chip) 821da177e4SLinus Torvalds { 83d7da4293SColin Ian King unsigned int result; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds snd_sbdsp_command(chip, SB_DSP_GET_VERSION); 861da177e4SLinus Torvalds result = (short) snd_sbdsp_get_byte(chip) << 8; 871da177e4SLinus Torvalds result |= (short) snd_sbdsp_get_byte(chip); 881da177e4SLinus Torvalds return result; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 91029d64b0STakashi Iwai static int snd_sbdsp_probe(struct snd_sb * chip) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds int version; 941da177e4SLinus Torvalds int major, minor; 951da177e4SLinus Torvalds char *str; 961da177e4SLinus Torvalds unsigned long flags; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * initialization sequence 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds spin_lock_irqsave(&chip->reg_lock, flags); 1031da177e4SLinus Torvalds if (snd_sbdsp_reset(chip) < 0) { 1041da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 1051da177e4SLinus Torvalds return -ENODEV; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds version = snd_sbdsp_version(chip); 1081da177e4SLinus Torvalds if (version < 0) { 1091da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 1101da177e4SLinus Torvalds return -ENODEV; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->reg_lock, flags); 1131da177e4SLinus Torvalds major = version >> 8; 1141da177e4SLinus Torvalds minor = version & 0xff; 1151da177e4SLinus Torvalds snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n", 1161da177e4SLinus Torvalds chip->port, major, minor); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds switch (chip->hardware) { 1191da177e4SLinus Torvalds case SB_HW_AUTO: 1201da177e4SLinus Torvalds switch (major) { 1211da177e4SLinus Torvalds case 1: 1221da177e4SLinus Torvalds chip->hardware = SB_HW_10; 1231da177e4SLinus Torvalds str = "1.0"; 1241da177e4SLinus Torvalds break; 1251da177e4SLinus Torvalds case 2: 1261da177e4SLinus Torvalds if (minor) { 1271da177e4SLinus Torvalds chip->hardware = SB_HW_201; 1281da177e4SLinus Torvalds str = "2.01+"; 1291da177e4SLinus Torvalds } else { 1301da177e4SLinus Torvalds chip->hardware = SB_HW_20; 1311da177e4SLinus Torvalds str = "2.0"; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds break; 1341da177e4SLinus Torvalds case 3: 1351da177e4SLinus Torvalds chip->hardware = SB_HW_PRO; 1361da177e4SLinus Torvalds str = "Pro"; 1371da177e4SLinus Torvalds break; 1381da177e4SLinus Torvalds case 4: 1391da177e4SLinus Torvalds chip->hardware = SB_HW_16; 1401da177e4SLinus Torvalds str = "16"; 1411da177e4SLinus Torvalds break; 1421da177e4SLinus Torvalds default: 14399b359baSTakashi Iwai snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n", 1441da177e4SLinus Torvalds chip->port, major, minor); 1451da177e4SLinus Torvalds return -ENODEV; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds break; 1481da177e4SLinus Torvalds case SB_HW_ALS100: 1491da177e4SLinus Torvalds str = "16 (ALS-100)"; 1501da177e4SLinus Torvalds break; 1511da177e4SLinus Torvalds case SB_HW_ALS4000: 1521da177e4SLinus Torvalds str = "16 (ALS-4000)"; 1531da177e4SLinus Torvalds break; 1541da177e4SLinus Torvalds case SB_HW_DT019X: 1551da177e4SLinus Torvalds str = "(DT019X/ALS007)"; 156621887aeSTakashi Iwai break; 157621887aeSTakashi Iwai case SB_HW_CS5530: 158621887aeSTakashi Iwai str = "16 (CS5530)"; 1591da177e4SLinus Torvalds break; 160ad8decb7SKrzysztof Helt case SB_HW_JAZZ16: 161ad8decb7SKrzysztof Helt str = "Pro (Jazz16)"; 162ad8decb7SKrzysztof Helt break; 1631da177e4SLinus Torvalds default: 1641da177e4SLinus Torvalds return -ENODEV; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds sprintf(chip->name, "Sound Blaster %s", str); 1671da177e4SLinus Torvalds chip->version = (major << 8) | minor; 1681da177e4SLinus Torvalds return 0; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 171029d64b0STakashi Iwai static int snd_sbdsp_free(struct snd_sb *chip) 1721da177e4SLinus Torvalds { 173b1d5776dSTakashi Iwai release_and_free_resource(chip->res_port); 1741da177e4SLinus Torvalds if (chip->irq >= 0) 1751da177e4SLinus Torvalds free_irq(chip->irq, (void *) chip); 1761da177e4SLinus Torvalds #ifdef CONFIG_ISA 1771da177e4SLinus Torvalds if (chip->dma8 >= 0) { 1781da177e4SLinus Torvalds disable_dma(chip->dma8); 1791da177e4SLinus Torvalds free_dma(chip->dma8); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) { 1821da177e4SLinus Torvalds disable_dma(chip->dma16); 1831da177e4SLinus Torvalds free_dma(chip->dma16); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds #endif 1861da177e4SLinus Torvalds kfree(chip); 1871da177e4SLinus Torvalds return 0; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 190029d64b0STakashi Iwai static int snd_sbdsp_dev_free(struct snd_device *device) 1911da177e4SLinus Torvalds { 192029d64b0STakashi Iwai struct snd_sb *chip = device->device_data; 1931da177e4SLinus Torvalds return snd_sbdsp_free(chip); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 196029d64b0STakashi Iwai int snd_sbdsp_create(struct snd_card *card, 1971da177e4SLinus Torvalds unsigned long port, 1981da177e4SLinus Torvalds int irq, 1997d12e780SDavid Howells irq_handler_t irq_handler, 2001da177e4SLinus Torvalds int dma8, 2011da177e4SLinus Torvalds int dma16, 2021da177e4SLinus Torvalds unsigned short hardware, 203029d64b0STakashi Iwai struct snd_sb **r_chip) 2041da177e4SLinus Torvalds { 205029d64b0STakashi Iwai struct snd_sb *chip; 2061da177e4SLinus Torvalds int err; 207029d64b0STakashi Iwai static struct snd_device_ops ops = { 2081da177e4SLinus Torvalds .dev_free = snd_sbdsp_dev_free, 2091da177e4SLinus Torvalds }; 2101da177e4SLinus Torvalds 211622207dcSTakashi Iwai if (snd_BUG_ON(!r_chip)) 212622207dcSTakashi Iwai return -EINVAL; 2131da177e4SLinus Torvalds *r_chip = NULL; 2149e76a76eSTakashi Iwai chip = kzalloc(sizeof(*chip), GFP_KERNEL); 2151da177e4SLinus Torvalds if (chip == NULL) 2161da177e4SLinus Torvalds return -ENOMEM; 2171da177e4SLinus Torvalds spin_lock_init(&chip->reg_lock); 2181da177e4SLinus Torvalds spin_lock_init(&chip->open_lock); 2191da177e4SLinus Torvalds spin_lock_init(&chip->midi_input_lock); 2201da177e4SLinus Torvalds spin_lock_init(&chip->mixer_lock); 2211da177e4SLinus Torvalds chip->irq = -1; 2221da177e4SLinus Torvalds chip->dma8 = -1; 2231da177e4SLinus Torvalds chip->dma16 = -1; 2241da177e4SLinus Torvalds chip->port = port; 2251da177e4SLinus Torvalds 2262d4a485bSTakashi Iwai if (request_irq(irq, irq_handler, 2272d4a485bSTakashi Iwai (hardware == SB_HW_ALS4000 || 2282d4a485bSTakashi Iwai hardware == SB_HW_CS5530) ? 22988e24c3aSYong Zhang IRQF_SHARED : 0, 2301da177e4SLinus Torvalds "SoundBlaster", (void *) chip)) { 2311da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq); 2321da177e4SLinus Torvalds snd_sbdsp_free(chip); 2331da177e4SLinus Torvalds return -EBUSY; 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds chip->irq = irq; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (hardware == SB_HW_ALS4000) 2381da177e4SLinus Torvalds goto __skip_allocation; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) { 2411da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port); 2421da177e4SLinus Torvalds snd_sbdsp_free(chip); 2431da177e4SLinus Torvalds return -EBUSY; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds #ifdef CONFIG_ISA 2471da177e4SLinus Torvalds if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) { 2481da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8); 2491da177e4SLinus Torvalds snd_sbdsp_free(chip); 2501da177e4SLinus Torvalds return -EBUSY; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds chip->dma8 = dma8; 2531da177e4SLinus Torvalds if (dma16 >= 0) { 2541da177e4SLinus Torvalds if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) { 2551da177e4SLinus Torvalds /* no duplex */ 2561da177e4SLinus Torvalds dma16 = -1; 2571da177e4SLinus Torvalds } else if (request_dma(dma16, "SoundBlaster - 16bit")) { 2581da177e4SLinus Torvalds snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16); 2591da177e4SLinus Torvalds snd_sbdsp_free(chip); 2601da177e4SLinus Torvalds return -EBUSY; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds chip->dma16 = dma16; 2641da177e4SLinus Torvalds #endif 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds __skip_allocation: 2671da177e4SLinus Torvalds chip->card = card; 2681da177e4SLinus Torvalds chip->hardware = hardware; 2691da177e4SLinus Torvalds if ((err = snd_sbdsp_probe(chip)) < 0) { 2701da177e4SLinus Torvalds snd_sbdsp_free(chip); 2711da177e4SLinus Torvalds return err; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { 2741da177e4SLinus Torvalds snd_sbdsp_free(chip); 2751da177e4SLinus Torvalds return err; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds *r_chip = chip; 2781da177e4SLinus Torvalds return 0; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_command); 2821da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_get_byte); 2831da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_reset); 2841da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbdsp_create); 2851da177e4SLinus Torvalds /* sb_mixer.c */ 2861da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_write); 2871da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_read); 2881da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_new); 2891da177e4SLinus Torvalds EXPORT_SYMBOL(snd_sbmixer_add_ctl); 2905bdb6a16STakashi Iwai #ifdef CONFIG_PM 2915bdb6a16STakashi Iwai EXPORT_SYMBOL(snd_sbmixer_suspend); 2925bdb6a16STakashi Iwai EXPORT_SYMBOL(snd_sbmixer_resume); 2935bdb6a16STakashi Iwai #endif 294