xref: /openbmc/linux/sound/isa/sb/sb_common.c (revision 622207dc)
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 <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/interrupt.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/ioport.h>
281da177e4SLinus Torvalds #include <sound/core.h>
291da177e4SLinus Torvalds #include <sound/sb.h>
301da177e4SLinus Torvalds #include <sound/initval.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/io.h>
331da177e4SLinus Torvalds #include <asm/dma.h>
341da177e4SLinus Torvalds 
35c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
361da177e4SLinus Torvalds MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
371da177e4SLinus Torvalds MODULE_LICENSE("GPL");
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define BUSY_LOOPS 100000
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #undef IO_DEBUG
421da177e4SLinus Torvalds 
43029d64b0STakashi Iwai int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds 	int i;
461da177e4SLinus Torvalds #ifdef IO_DEBUG
4799b359baSTakashi Iwai 	snd_printk(KERN_DEBUG "command 0x%x\n", val);
481da177e4SLinus Torvalds #endif
491da177e4SLinus Torvalds 	for (i = BUSY_LOOPS; i; i--)
501da177e4SLinus Torvalds 		if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
511da177e4SLinus Torvalds 			outb(val, SBP(chip, COMMAND));
521da177e4SLinus Torvalds 			return 1;
531da177e4SLinus Torvalds 		}
549bf8e7ddSHarvey Harrison 	snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val);
551da177e4SLinus Torvalds 	return 0;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
58029d64b0STakashi Iwai int snd_sbdsp_get_byte(struct snd_sb *chip)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	int val;
611da177e4SLinus Torvalds 	int i;
621da177e4SLinus Torvalds 	for (i = BUSY_LOOPS; i; i--) {
631da177e4SLinus Torvalds 		if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
641da177e4SLinus Torvalds 			val = inb(SBP(chip, READ));
651da177e4SLinus Torvalds #ifdef IO_DEBUG
6699b359baSTakashi Iwai 			snd_printk(KERN_DEBUG "get_byte 0x%x\n", val);
671da177e4SLinus Torvalds #endif
681da177e4SLinus Torvalds 			return val;
691da177e4SLinus Torvalds 		}
701da177e4SLinus Torvalds 	}
719bf8e7ddSHarvey Harrison 	snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port);
721da177e4SLinus Torvalds 	return -ENODEV;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
75029d64b0STakashi Iwai int snd_sbdsp_reset(struct snd_sb *chip)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	int i;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	outb(1, SBP(chip, RESET));
801da177e4SLinus Torvalds 	udelay(10);
811da177e4SLinus Torvalds 	outb(0, SBP(chip, RESET));
821da177e4SLinus Torvalds 	udelay(30);
831da177e4SLinus Torvalds 	for (i = BUSY_LOOPS; i; i--)
841da177e4SLinus Torvalds 		if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
851da177e4SLinus Torvalds 			if (inb(SBP(chip, READ)) == 0xaa)
861da177e4SLinus Torvalds 				return 0;
871da177e4SLinus Torvalds 			else
881da177e4SLinus Torvalds 				break;
891da177e4SLinus Torvalds 		}
909bf8e7ddSHarvey Harrison 	snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port);
911da177e4SLinus Torvalds 	return -ENODEV;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94029d64b0STakashi Iwai static int snd_sbdsp_version(struct snd_sb * chip)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	unsigned int result = -ENODEV;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
991da177e4SLinus Torvalds 	result = (short) snd_sbdsp_get_byte(chip) << 8;
1001da177e4SLinus Torvalds 	result |= (short) snd_sbdsp_get_byte(chip);
1011da177e4SLinus Torvalds 	return result;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
104029d64b0STakashi Iwai static int snd_sbdsp_probe(struct snd_sb * chip)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	int version;
1071da177e4SLinus Torvalds 	int major, minor;
1081da177e4SLinus Torvalds 	char *str;
1091da177e4SLinus Torvalds 	unsigned long flags;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	/*
1121da177e4SLinus Torvalds 	 *  initialization sequence
1131da177e4SLinus Torvalds 	 */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1161da177e4SLinus Torvalds 	if (snd_sbdsp_reset(chip) < 0) {
1171da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->reg_lock, flags);
1181da177e4SLinus Torvalds 		return -ENODEV;
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 	version = snd_sbdsp_version(chip);
1211da177e4SLinus Torvalds 	if (version < 0) {
1221da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->reg_lock, flags);
1231da177e4SLinus Torvalds 		return -ENODEV;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1261da177e4SLinus Torvalds 	major = version >> 8;
1271da177e4SLinus Torvalds 	minor = version & 0xff;
1281da177e4SLinus Torvalds 	snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
1291da177e4SLinus Torvalds 		    chip->port, major, minor);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	switch (chip->hardware) {
1321da177e4SLinus Torvalds 	case SB_HW_AUTO:
1331da177e4SLinus Torvalds 		switch (major) {
1341da177e4SLinus Torvalds 		case 1:
1351da177e4SLinus Torvalds 			chip->hardware = SB_HW_10;
1361da177e4SLinus Torvalds 			str = "1.0";
1371da177e4SLinus Torvalds 			break;
1381da177e4SLinus Torvalds 		case 2:
1391da177e4SLinus Torvalds 			if (minor) {
1401da177e4SLinus Torvalds 				chip->hardware = SB_HW_201;
1411da177e4SLinus Torvalds 				str = "2.01+";
1421da177e4SLinus Torvalds 			} else {
1431da177e4SLinus Torvalds 				chip->hardware = SB_HW_20;
1441da177e4SLinus Torvalds 				str = "2.0";
1451da177e4SLinus Torvalds 			}
1461da177e4SLinus Torvalds 			break;
1471da177e4SLinus Torvalds 		case 3:
1481da177e4SLinus Torvalds 			chip->hardware = SB_HW_PRO;
1491da177e4SLinus Torvalds 			str = "Pro";
1501da177e4SLinus Torvalds 			break;
1511da177e4SLinus Torvalds 		case 4:
1521da177e4SLinus Torvalds 			chip->hardware = SB_HW_16;
1531da177e4SLinus Torvalds 			str = "16";
1541da177e4SLinus Torvalds 			break;
1551da177e4SLinus Torvalds 		default:
15699b359baSTakashi Iwai 			snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n",
1571da177e4SLinus Torvalds 				   chip->port, major, minor);
1581da177e4SLinus Torvalds 			return -ENODEV;
1591da177e4SLinus Torvalds 		}
1601da177e4SLinus Torvalds 		break;
1611da177e4SLinus Torvalds 	case SB_HW_ALS100:
1621da177e4SLinus Torvalds 		str = "16 (ALS-100)";
1631da177e4SLinus Torvalds 		break;
1641da177e4SLinus Torvalds 	case SB_HW_ALS4000:
1651da177e4SLinus Torvalds 		str = "16 (ALS-4000)";
1661da177e4SLinus Torvalds 		break;
1671da177e4SLinus Torvalds 	case SB_HW_DT019X:
1681da177e4SLinus Torvalds 		str = "(DT019X/ALS007)";
169621887aeSTakashi Iwai 		break;
170621887aeSTakashi Iwai 	case SB_HW_CS5530:
171621887aeSTakashi Iwai 		str = "16 (CS5530)";
1721da177e4SLinus Torvalds 		break;
1731da177e4SLinus Torvalds 	default:
1741da177e4SLinus Torvalds 		return -ENODEV;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 	sprintf(chip->name, "Sound Blaster %s", str);
1771da177e4SLinus Torvalds 	chip->version = (major << 8) | minor;
1781da177e4SLinus Torvalds 	return 0;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
181029d64b0STakashi Iwai static int snd_sbdsp_free(struct snd_sb *chip)
1821da177e4SLinus Torvalds {
183b1d5776dSTakashi Iwai 	if (chip->res_port)
184b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_port);
1851da177e4SLinus Torvalds 	if (chip->irq >= 0)
1861da177e4SLinus Torvalds 		free_irq(chip->irq, (void *) chip);
1871da177e4SLinus Torvalds #ifdef CONFIG_ISA
1881da177e4SLinus Torvalds 	if (chip->dma8 >= 0) {
1891da177e4SLinus Torvalds 		disable_dma(chip->dma8);
1901da177e4SLinus Torvalds 		free_dma(chip->dma8);
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 	if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
1931da177e4SLinus Torvalds 		disable_dma(chip->dma16);
1941da177e4SLinus Torvalds 		free_dma(chip->dma16);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds #endif
1971da177e4SLinus Torvalds 	kfree(chip);
1981da177e4SLinus Torvalds 	return 0;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
201029d64b0STakashi Iwai static int snd_sbdsp_dev_free(struct snd_device *device)
2021da177e4SLinus Torvalds {
203029d64b0STakashi Iwai 	struct snd_sb *chip = device->device_data;
2041da177e4SLinus Torvalds 	return snd_sbdsp_free(chip);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207029d64b0STakashi Iwai int snd_sbdsp_create(struct snd_card *card,
2081da177e4SLinus Torvalds 		     unsigned long port,
2091da177e4SLinus Torvalds 		     int irq,
2107d12e780SDavid Howells 		     irq_handler_t irq_handler,
2111da177e4SLinus Torvalds 		     int dma8,
2121da177e4SLinus Torvalds 		     int dma16,
2131da177e4SLinus Torvalds 		     unsigned short hardware,
214029d64b0STakashi Iwai 		     struct snd_sb **r_chip)
2151da177e4SLinus Torvalds {
216029d64b0STakashi Iwai 	struct snd_sb *chip;
2171da177e4SLinus Torvalds 	int err;
218029d64b0STakashi Iwai 	static struct snd_device_ops ops = {
2191da177e4SLinus Torvalds 		.dev_free =	snd_sbdsp_dev_free,
2201da177e4SLinus Torvalds 	};
2211da177e4SLinus Torvalds 
222622207dcSTakashi Iwai 	if (snd_BUG_ON(!r_chip))
223622207dcSTakashi Iwai 		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