1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Uros Bizjak <uros@kss-loka.si>
5 *
6 * Lowlevel routines for control of Sound Blaster cards
7 */
8
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>
15 #include <linux/io.h>
16 #include <sound/core.h>
17 #include <sound/sb.h>
18 #include <sound/initval.h>
19
20 #include <asm/dma.h>
21
22 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
23 MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
24 MODULE_LICENSE("GPL");
25
26 #define BUSY_LOOPS 100000
27
28 #undef IO_DEBUG
29
snd_sbdsp_command(struct snd_sb * chip,unsigned char val)30 int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
31 {
32 int i;
33 #ifdef IO_DEBUG
34 dev_dbg(chip->card->dev, "command 0x%x\n", val);
35 #endif
36 for (i = BUSY_LOOPS; i; i--)
37 if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
38 outb(val, SBP(chip, COMMAND));
39 return 1;
40 }
41 dev_dbg(chip->card->dev, "%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val);
42 return 0;
43 }
44
snd_sbdsp_get_byte(struct snd_sb * chip)45 int snd_sbdsp_get_byte(struct snd_sb *chip)
46 {
47 int val;
48 int i;
49 for (i = BUSY_LOOPS; i; i--) {
50 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
51 val = inb(SBP(chip, READ));
52 #ifdef IO_DEBUG
53 dev_dbg(chip->card->dev, "get_byte 0x%x\n", val);
54 #endif
55 return val;
56 }
57 }
58 dev_dbg(chip->card->dev, "%s [0x%lx]: timeout\n", __func__, chip->port);
59 return -ENODEV;
60 }
61
snd_sbdsp_reset(struct snd_sb * chip)62 int snd_sbdsp_reset(struct snd_sb *chip)
63 {
64 int i;
65
66 outb(1, SBP(chip, RESET));
67 udelay(10);
68 outb(0, SBP(chip, RESET));
69 udelay(30);
70 for (i = BUSY_LOOPS; i; i--)
71 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
72 if (inb(SBP(chip, READ)) == 0xaa)
73 return 0;
74 else
75 break;
76 }
77 if (chip->card)
78 dev_dbg(chip->card->dev, "%s [0x%lx] failed...\n", __func__, chip->port);
79 return -ENODEV;
80 }
81
snd_sbdsp_version(struct snd_sb * chip)82 static int snd_sbdsp_version(struct snd_sb * chip)
83 {
84 unsigned int result;
85
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);
89 return result;
90 }
91
snd_sbdsp_probe(struct snd_sb * chip)92 static int snd_sbdsp_probe(struct snd_sb * chip)
93 {
94 int version;
95 int major, minor;
96 char *str;
97
98 /*
99 * initialization sequence
100 */
101
102 scoped_guard(spinlock_irqsave, &chip->reg_lock) {
103 if (snd_sbdsp_reset(chip) < 0)
104 return -ENODEV;
105 version = snd_sbdsp_version(chip);
106 if (version < 0)
107 return -ENODEV;
108 }
109 major = version >> 8;
110 minor = version & 0xff;
111 dev_dbg(chip->card->dev, "SB [0x%lx]: DSP chip found, version = %i.%i\n",
112 chip->port, major, minor);
113
114 switch (chip->hardware) {
115 case SB_HW_AUTO:
116 switch (major) {
117 case 1:
118 chip->hardware = SB_HW_10;
119 str = "1.0";
120 break;
121 case 2:
122 if (minor) {
123 chip->hardware = SB_HW_201;
124 str = "2.01+";
125 } else {
126 chip->hardware = SB_HW_20;
127 str = "2.0";
128 }
129 break;
130 case 3:
131 chip->hardware = SB_HW_PRO;
132 str = "Pro";
133 break;
134 case 4:
135 chip->hardware = SB_HW_16;
136 str = "16";
137 break;
138 default:
139 dev_info(chip->card->dev, "SB [0x%lx]: unknown DSP chip version %i.%i\n",
140 chip->port, major, minor);
141 return -ENODEV;
142 }
143 break;
144 case SB_HW_ALS100:
145 str = "16 (ALS-100)";
146 break;
147 case SB_HW_ALS4000:
148 str = "16 (ALS-4000)";
149 break;
150 case SB_HW_DT019X:
151 str = "(DT019X/ALS007)";
152 break;
153 case SB_HW_CS5530:
154 str = "16 (CS5530)";
155 break;
156 case SB_HW_JAZZ16:
157 str = "Pro (Jazz16)";
158 break;
159 default:
160 return -ENODEV;
161 }
162 sprintf(chip->name, "Sound Blaster %s", str);
163 chip->version = (major << 8) | minor;
164 return 0;
165 }
166
snd_sbdsp_create(struct snd_card * card,unsigned long port,int irq,irq_handler_t irq_handler,int dma8,int dma16,unsigned short hardware,struct snd_sb ** r_chip)167 int snd_sbdsp_create(struct snd_card *card,
168 unsigned long port,
169 int irq,
170 irq_handler_t irq_handler,
171 int dma8,
172 int dma16,
173 unsigned short hardware,
174 struct snd_sb **r_chip)
175 {
176 struct snd_sb *chip;
177 int err;
178
179 if (snd_BUG_ON(!r_chip))
180 return -EINVAL;
181 *r_chip = NULL;
182 chip = devm_kzalloc(card->dev, sizeof(*chip), GFP_KERNEL);
183 if (!chip)
184 return -ENOMEM;
185 spin_lock_init(&chip->reg_lock);
186 spin_lock_init(&chip->open_lock);
187 spin_lock_init(&chip->midi_input_lock);
188 spin_lock_init(&chip->mixer_lock);
189 chip->irq = -1;
190 chip->dma8 = -1;
191 chip->dma16 = -1;
192 chip->port = port;
193
194 if (devm_request_irq(card->dev, irq, irq_handler,
195 (hardware == SB_HW_ALS4000 ||
196 hardware == SB_HW_CS5530) ?
197 IRQF_SHARED : 0,
198 "SoundBlaster", (void *) chip)) {
199 dev_err(card->dev, "sb: can't grab irq %d\n", irq);
200 return -EBUSY;
201 }
202 chip->irq = irq;
203 card->sync_irq = chip->irq;
204
205 if (hardware == SB_HW_ALS4000)
206 goto __skip_allocation;
207
208 chip->res_port = devm_request_region(card->dev, port, 16,
209 "SoundBlaster");
210 if (!chip->res_port) {
211 dev_err(card->dev, "sb: can't grab port 0x%lx\n", port);
212 return -EBUSY;
213 }
214
215 #ifdef CONFIG_ISA
216 if (dma8 >= 0 && snd_devm_request_dma(card->dev, dma8,
217 "SoundBlaster - 8bit")) {
218 dev_err(card->dev, "sb: can't grab DMA8 %d\n", dma8);
219 return -EBUSY;
220 }
221 chip->dma8 = dma8;
222 if (dma16 >= 0) {
223 if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
224 /* no duplex */
225 dma16 = -1;
226 } else if (snd_devm_request_dma(card->dev, dma16,
227 "SoundBlaster - 16bit")) {
228 dev_err(card->dev, "sb: can't grab DMA16 %d\n", dma16);
229 return -EBUSY;
230 }
231 }
232 chip->dma16 = dma16;
233 #endif
234
235 __skip_allocation:
236 chip->card = card;
237 chip->hardware = hardware;
238 err = snd_sbdsp_probe(chip);
239 if (err < 0)
240 return err;
241 *r_chip = chip;
242 return 0;
243 }
244
245 EXPORT_SYMBOL(snd_sbdsp_command);
246 EXPORT_SYMBOL(snd_sbdsp_get_byte);
247 EXPORT_SYMBOL(snd_sbdsp_reset);
248 EXPORT_SYMBOL(snd_sbdsp_create);
249 /* sb_mixer.c */
250 EXPORT_SYMBOL(snd_sbmixer_write);
251 EXPORT_SYMBOL(snd_sbmixer_read);
252 EXPORT_SYMBOL(snd_sbmixer_new);
253 EXPORT_SYMBOL(snd_sbmixer_add_ctl);
254 #ifdef CONFIG_PM
255 EXPORT_SYMBOL(snd_sbmixer_suspend);
256 EXPORT_SYMBOL(snd_sbmixer_resume);
257 #endif
258