1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Lowlevel routines for control of Sound Blaster cards
9 #include <linux/delay.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/slab.h>
13 #include <linux/ioport.h>
14 #include <linux/module.h>
16 #include <sound/core.h>
18 #include <sound/initval.h>
23 MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
24 MODULE_LICENSE("GPL");
26 #define BUSY_LOOPS 100000
30 int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
34 dev_dbg(chip->card->dev, "command 0x%x\n", val);
36 for (i = BUSY_LOOPS; i; i--)
37 if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
38 outb(val, SBP(chip, COMMAND));
41 dev_dbg(chip->card->dev, "%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val);
45 int snd_sbdsp_get_byte(struct snd_sb *chip)
49 for (i = BUSY_LOOPS; i; i--) {
50 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
51 val = inb(SBP(chip, READ));
53 dev_dbg(chip->card->dev, "get_byte 0x%x\n", val);
58 dev_dbg(chip->card->dev, "%s [0x%lx]: timeout\n", __func__, chip->port);
62 int snd_sbdsp_reset(struct snd_sb *chip)
66 outb(1, SBP(chip, RESET));
68 outb(0, SBP(chip, RESET));
70 for (i = BUSY_LOOPS; i; i--)
71 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
72 if (inb(SBP(chip, READ)) == 0xaa)
78 dev_dbg(chip->card->dev, "%s [0x%lx] failed...\n", __func__, chip->port);
82 static int snd_sbdsp_version(struct snd_sb * chip)
86 snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
87 result = (short) snd_sbdsp_get_byte(chip) << 8;
88 result |= (short) snd_sbdsp_get_byte(chip);
92 static int snd_sbdsp_probe(struct snd_sb * chip)
100 * initialization sequence
103 spin_lock_irqsave(&chip->reg_lock, flags);
104 if (snd_sbdsp_reset(chip) < 0) {
105 spin_unlock_irqrestore(&chip->reg_lock, flags);
108 version = snd_sbdsp_version(chip);
110 spin_unlock_irqrestore(&chip->reg_lock, flags);
113 spin_unlock_irqrestore(&chip->reg_lock, flags);
114 major = version >> 8;
115 minor = version & 0xff;
116 dev_dbg(chip->card->dev, "SB [0x%lx]: DSP chip found, version = %i.%i\n",
117 chip->port, major, minor);
119 switch (chip->hardware) {
123 chip->hardware = SB_HW_10;
128 chip->hardware = SB_HW_201;
131 chip->hardware = SB_HW_20;
136 chip->hardware = SB_HW_PRO;
140 chip->hardware = SB_HW_16;
144 dev_info(chip->card->dev, "SB [0x%lx]: unknown DSP chip version %i.%i\n",
145 chip->port, major, minor);
150 str = "16 (ALS-100)";
153 str = "16 (ALS-4000)";
156 str = "(DT019X/ALS007)";
162 str = "Pro (Jazz16)";
167 sprintf(chip->name, "Sound Blaster %s", str);
168 chip->version = (major << 8) | minor;
172 int snd_sbdsp_create(struct snd_card *card,
175 irq_handler_t irq_handler,
178 unsigned short hardware,
179 struct snd_sb **r_chip)
184 if (snd_BUG_ON(!r_chip))
187 chip = devm_kzalloc(card->dev, sizeof(*chip), GFP_KERNEL);
190 spin_lock_init(&chip->reg_lock);
191 spin_lock_init(&chip->open_lock);
192 spin_lock_init(&chip->midi_input_lock);
193 spin_lock_init(&chip->mixer_lock);
199 if (devm_request_irq(card->dev, irq, irq_handler,
200 (hardware == SB_HW_ALS4000 ||
201 hardware == SB_HW_CS5530) ?
203 "SoundBlaster", (void *) chip)) {
204 dev_err(card->dev, "sb: can't grab irq %d\n", irq);
208 card->sync_irq = chip->irq;
210 if (hardware == SB_HW_ALS4000)
211 goto __skip_allocation;
213 chip->res_port = devm_request_region(card->dev, port, 16,
215 if (!chip->res_port) {
216 dev_err(card->dev, "sb: can't grab port 0x%lx\n", port);
221 if (dma8 >= 0 && snd_devm_request_dma(card->dev, dma8,
222 "SoundBlaster - 8bit")) {
223 dev_err(card->dev, "sb: can't grab DMA8 %d\n", dma8);
228 if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
231 } else if (snd_devm_request_dma(card->dev, dma16,
232 "SoundBlaster - 16bit")) {
233 dev_err(card->dev, "sb: can't grab DMA16 %d\n", dma16);
242 chip->hardware = hardware;
243 err = snd_sbdsp_probe(chip);
250 EXPORT_SYMBOL(snd_sbdsp_command);
251 EXPORT_SYMBOL(snd_sbdsp_get_byte);
252 EXPORT_SYMBOL(snd_sbdsp_reset);
253 EXPORT_SYMBOL(snd_sbdsp_create);
255 EXPORT_SYMBOL(snd_sbmixer_write);
256 EXPORT_SYMBOL(snd_sbmixer_read);
257 EXPORT_SYMBOL(snd_sbmixer_new);
258 EXPORT_SYMBOL(snd_sbmixer_add_ctl);
260 EXPORT_SYMBOL(snd_sbmixer_suspend);
261 EXPORT_SYMBOL(snd_sbmixer_resume);