Loading...
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
22MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
23MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
24MODULE_LICENSE("GPL");
25
26#define BUSY_LOOPS 100000
27
28#undef IO_DEBUG
29
30int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
31{
32 int i;
33#ifdef IO_DEBUG
34 snd_printk(KERN_DEBUG "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 snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val);
42 return 0;
43}
44
45int 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 snd_printk(KERN_DEBUG "get_byte 0x%x\n", val);
54#endif
55 return val;
56 }
57 }
58 snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port);
59 return -ENODEV;
60}
61
62int 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 snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port);
78 return -ENODEV;
79}
80
81static int snd_sbdsp_version(struct snd_sb * chip)
82{
83 unsigned int result;
84
85 snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
86 result = (short) snd_sbdsp_get_byte(chip) << 8;
87 result |= (short) snd_sbdsp_get_byte(chip);
88 return result;
89}
90
91static int snd_sbdsp_probe(struct snd_sb * chip)
92{
93 int version;
94 int major, minor;
95 char *str;
96 unsigned long flags;
97
98 /*
99 * initialization sequence
100 */
101
102 spin_lock_irqsave(&chip->reg_lock, flags);
103 if (snd_sbdsp_reset(chip) < 0) {
104 spin_unlock_irqrestore(&chip->reg_lock, flags);
105 return -ENODEV;
106 }
107 version = snd_sbdsp_version(chip);
108 if (version < 0) {
109 spin_unlock_irqrestore(&chip->reg_lock, flags);
110 return -ENODEV;
111 }
112 spin_unlock_irqrestore(&chip->reg_lock, flags);
113 major = version >> 8;
114 minor = version & 0xff;
115 snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
116 chip->port, major, minor);
117
118 switch (chip->hardware) {
119 case SB_HW_AUTO:
120 switch (major) {
121 case 1:
122 chip->hardware = SB_HW_10;
123 str = "1.0";
124 break;
125 case 2:
126 if (minor) {
127 chip->hardware = SB_HW_201;
128 str = "2.01+";
129 } else {
130 chip->hardware = SB_HW_20;
131 str = "2.0";
132 }
133 break;
134 case 3:
135 chip->hardware = SB_HW_PRO;
136 str = "Pro";
137 break;
138 case 4:
139 chip->hardware = SB_HW_16;
140 str = "16";
141 break;
142 default:
143 snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n",
144 chip->port, major, minor);
145 return -ENODEV;
146 }
147 break;
148 case SB_HW_ALS100:
149 str = "16 (ALS-100)";
150 break;
151 case SB_HW_ALS4000:
152 str = "16 (ALS-4000)";
153 break;
154 case SB_HW_DT019X:
155 str = "(DT019X/ALS007)";
156 break;
157 case SB_HW_CS5530:
158 str = "16 (CS5530)";
159 break;
160 case SB_HW_JAZZ16:
161 str = "Pro (Jazz16)";
162 break;
163 default:
164 return -ENODEV;
165 }
166 sprintf(chip->name, "Sound Blaster %s", str);
167 chip->version = (major << 8) | minor;
168 return 0;
169}
170
171static int snd_sbdsp_free(struct snd_sb *chip)
172{
173 release_and_free_resource(chip->res_port);
174 if (chip->irq >= 0)
175 free_irq(chip->irq, (void *) chip);
176#ifdef CONFIG_ISA
177 if (chip->dma8 >= 0) {
178 disable_dma(chip->dma8);
179 free_dma(chip->dma8);
180 }
181 if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
182 disable_dma(chip->dma16);
183 free_dma(chip->dma16);
184 }
185#endif
186 kfree(chip);
187 return 0;
188}
189
190static int snd_sbdsp_dev_free(struct snd_device *device)
191{
192 struct snd_sb *chip = device->device_data;
193 return snd_sbdsp_free(chip);
194}
195
196int snd_sbdsp_create(struct snd_card *card,
197 unsigned long port,
198 int irq,
199 irq_handler_t irq_handler,
200 int dma8,
201 int dma16,
202 unsigned short hardware,
203 struct snd_sb **r_chip)
204{
205 struct snd_sb *chip;
206 int err;
207 static const struct snd_device_ops ops = {
208 .dev_free = snd_sbdsp_dev_free,
209 };
210
211 if (snd_BUG_ON(!r_chip))
212 return -EINVAL;
213 *r_chip = NULL;
214 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
215 if (chip == NULL)
216 return -ENOMEM;
217 spin_lock_init(&chip->reg_lock);
218 spin_lock_init(&chip->open_lock);
219 spin_lock_init(&chip->midi_input_lock);
220 spin_lock_init(&chip->mixer_lock);
221 chip->irq = -1;
222 chip->dma8 = -1;
223 chip->dma16 = -1;
224 chip->port = port;
225
226 if (request_irq(irq, irq_handler,
227 (hardware == SB_HW_ALS4000 ||
228 hardware == SB_HW_CS5530) ?
229 IRQF_SHARED : 0,
230 "SoundBlaster", (void *) chip)) {
231 snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
232 snd_sbdsp_free(chip);
233 return -EBUSY;
234 }
235 chip->irq = irq;
236 card->sync_irq = chip->irq;
237
238 if (hardware == SB_HW_ALS4000)
239 goto __skip_allocation;
240
241 if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
242 snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
243 snd_sbdsp_free(chip);
244 return -EBUSY;
245 }
246
247#ifdef CONFIG_ISA
248 if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
249 snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
250 snd_sbdsp_free(chip);
251 return -EBUSY;
252 }
253 chip->dma8 = dma8;
254 if (dma16 >= 0) {
255 if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
256 /* no duplex */
257 dma16 = -1;
258 } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
259 snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
260 snd_sbdsp_free(chip);
261 return -EBUSY;
262 }
263 }
264 chip->dma16 = dma16;
265#endif
266
267 __skip_allocation:
268 chip->card = card;
269 chip->hardware = hardware;
270 if ((err = snd_sbdsp_probe(chip)) < 0) {
271 snd_sbdsp_free(chip);
272 return err;
273 }
274 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
275 snd_sbdsp_free(chip);
276 return err;
277 }
278 *r_chip = chip;
279 return 0;
280}
281
282EXPORT_SYMBOL(snd_sbdsp_command);
283EXPORT_SYMBOL(snd_sbdsp_get_byte);
284EXPORT_SYMBOL(snd_sbdsp_reset);
285EXPORT_SYMBOL(snd_sbdsp_create);
286/* sb_mixer.c */
287EXPORT_SYMBOL(snd_sbmixer_write);
288EXPORT_SYMBOL(snd_sbmixer_read);
289EXPORT_SYMBOL(snd_sbmixer_new);
290EXPORT_SYMBOL(snd_sbmixer_add_ctl);
291#ifdef CONFIG_PM
292EXPORT_SYMBOL(snd_sbmixer_suspend);
293EXPORT_SYMBOL(snd_sbmixer_resume);
294#endif
1/*
2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Uros Bizjak <uros@kss-loka.si>
4 *
5 * Lowlevel routines for control of Sound Blaster cards
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/slab.h>
27#include <linux/ioport.h>
28#include <sound/core.h>
29#include <sound/sb.h>
30#include <sound/initval.h>
31
32#include <asm/io.h>
33#include <asm/dma.h>
34
35MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
36MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
37MODULE_LICENSE("GPL");
38
39#define BUSY_LOOPS 100000
40
41#undef IO_DEBUG
42
43int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
44{
45 int i;
46#ifdef IO_DEBUG
47 snd_printk(KERN_DEBUG "command 0x%x\n", val);
48#endif
49 for (i = BUSY_LOOPS; i; i--)
50 if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
51 outb(val, SBP(chip, COMMAND));
52 return 1;
53 }
54 snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val);
55 return 0;
56}
57
58int snd_sbdsp_get_byte(struct snd_sb *chip)
59{
60 int val;
61 int i;
62 for (i = BUSY_LOOPS; i; i--) {
63 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
64 val = inb(SBP(chip, READ));
65#ifdef IO_DEBUG
66 snd_printk(KERN_DEBUG "get_byte 0x%x\n", val);
67#endif
68 return val;
69 }
70 }
71 snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port);
72 return -ENODEV;
73}
74
75int snd_sbdsp_reset(struct snd_sb *chip)
76{
77 int i;
78
79 outb(1, SBP(chip, RESET));
80 udelay(10);
81 outb(0, SBP(chip, RESET));
82 udelay(30);
83 for (i = BUSY_LOOPS; i; i--)
84 if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
85 if (inb(SBP(chip, READ)) == 0xaa)
86 return 0;
87 else
88 break;
89 }
90 snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port);
91 return -ENODEV;
92}
93
94static int snd_sbdsp_version(struct snd_sb * chip)
95{
96 unsigned int result = -ENODEV;
97
98 snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
99 result = (short) snd_sbdsp_get_byte(chip) << 8;
100 result |= (short) snd_sbdsp_get_byte(chip);
101 return result;
102}
103
104static int snd_sbdsp_probe(struct snd_sb * chip)
105{
106 int version;
107 int major, minor;
108 char *str;
109 unsigned long flags;
110
111 /*
112 * initialization sequence
113 */
114
115 spin_lock_irqsave(&chip->reg_lock, flags);
116 if (snd_sbdsp_reset(chip) < 0) {
117 spin_unlock_irqrestore(&chip->reg_lock, flags);
118 return -ENODEV;
119 }
120 version = snd_sbdsp_version(chip);
121 if (version < 0) {
122 spin_unlock_irqrestore(&chip->reg_lock, flags);
123 return -ENODEV;
124 }
125 spin_unlock_irqrestore(&chip->reg_lock, flags);
126 major = version >> 8;
127 minor = version & 0xff;
128 snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
129 chip->port, major, minor);
130
131 switch (chip->hardware) {
132 case SB_HW_AUTO:
133 switch (major) {
134 case 1:
135 chip->hardware = SB_HW_10;
136 str = "1.0";
137 break;
138 case 2:
139 if (minor) {
140 chip->hardware = SB_HW_201;
141 str = "2.01+";
142 } else {
143 chip->hardware = SB_HW_20;
144 str = "2.0";
145 }
146 break;
147 case 3:
148 chip->hardware = SB_HW_PRO;
149 str = "Pro";
150 break;
151 case 4:
152 chip->hardware = SB_HW_16;
153 str = "16";
154 break;
155 default:
156 snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n",
157 chip->port, major, minor);
158 return -ENODEV;
159 }
160 break;
161 case SB_HW_ALS100:
162 str = "16 (ALS-100)";
163 break;
164 case SB_HW_ALS4000:
165 str = "16 (ALS-4000)";
166 break;
167 case SB_HW_DT019X:
168 str = "(DT019X/ALS007)";
169 break;
170 case SB_HW_CS5530:
171 str = "16 (CS5530)";
172 break;
173 case SB_HW_JAZZ16:
174 str = "Pro (Jazz16)";
175 break;
176 default:
177 return -ENODEV;
178 }
179 sprintf(chip->name, "Sound Blaster %s", str);
180 chip->version = (major << 8) | minor;
181 return 0;
182}
183
184static int snd_sbdsp_free(struct snd_sb *chip)
185{
186 if (chip->res_port)
187 release_and_free_resource(chip->res_port);
188 if (chip->irq >= 0)
189 free_irq(chip->irq, (void *) chip);
190#ifdef CONFIG_ISA
191 if (chip->dma8 >= 0) {
192 disable_dma(chip->dma8);
193 free_dma(chip->dma8);
194 }
195 if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
196 disable_dma(chip->dma16);
197 free_dma(chip->dma16);
198 }
199#endif
200 kfree(chip);
201 return 0;
202}
203
204static int snd_sbdsp_dev_free(struct snd_device *device)
205{
206 struct snd_sb *chip = device->device_data;
207 return snd_sbdsp_free(chip);
208}
209
210int snd_sbdsp_create(struct snd_card *card,
211 unsigned long port,
212 int irq,
213 irq_handler_t irq_handler,
214 int dma8,
215 int dma16,
216 unsigned short hardware,
217 struct snd_sb **r_chip)
218{
219 struct snd_sb *chip;
220 int err;
221 static struct snd_device_ops ops = {
222 .dev_free = snd_sbdsp_dev_free,
223 };
224
225 if (snd_BUG_ON(!r_chip))
226 return -EINVAL;
227 *r_chip = NULL;
228 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
229 if (chip == NULL)
230 return -ENOMEM;
231 spin_lock_init(&chip->reg_lock);
232 spin_lock_init(&chip->open_lock);
233 spin_lock_init(&chip->midi_input_lock);
234 spin_lock_init(&chip->mixer_lock);
235 chip->irq = -1;
236 chip->dma8 = -1;
237 chip->dma16 = -1;
238 chip->port = port;
239
240 if (request_irq(irq, irq_handler,
241 (hardware == SB_HW_ALS4000 ||
242 hardware == SB_HW_CS5530) ?
243 IRQF_SHARED : IRQF_DISABLED,
244 "SoundBlaster", (void *) chip)) {
245 snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
246 snd_sbdsp_free(chip);
247 return -EBUSY;
248 }
249 chip->irq = irq;
250
251 if (hardware == SB_HW_ALS4000)
252 goto __skip_allocation;
253
254 if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
255 snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
256 snd_sbdsp_free(chip);
257 return -EBUSY;
258 }
259
260#ifdef CONFIG_ISA
261 if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
262 snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
263 snd_sbdsp_free(chip);
264 return -EBUSY;
265 }
266 chip->dma8 = dma8;
267 if (dma16 >= 0) {
268 if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
269 /* no duplex */
270 dma16 = -1;
271 } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
272 snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
273 snd_sbdsp_free(chip);
274 return -EBUSY;
275 }
276 }
277 chip->dma16 = dma16;
278#endif
279
280 __skip_allocation:
281 chip->card = card;
282 chip->hardware = hardware;
283 if ((err = snd_sbdsp_probe(chip)) < 0) {
284 snd_sbdsp_free(chip);
285 return err;
286 }
287 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
288 snd_sbdsp_free(chip);
289 return err;
290 }
291 *r_chip = chip;
292 return 0;
293}
294
295EXPORT_SYMBOL(snd_sbdsp_command);
296EXPORT_SYMBOL(snd_sbdsp_get_byte);
297EXPORT_SYMBOL(snd_sbdsp_reset);
298EXPORT_SYMBOL(snd_sbdsp_create);
299/* sb_mixer.c */
300EXPORT_SYMBOL(snd_sbmixer_write);
301EXPORT_SYMBOL(snd_sbmixer_read);
302EXPORT_SYMBOL(snd_sbmixer_new);
303EXPORT_SYMBOL(snd_sbmixer_add_ctl);
304#ifdef CONFIG_PM
305EXPORT_SYMBOL(snd_sbmixer_suspend);
306EXPORT_SYMBOL(snd_sbmixer_resume);
307#endif
308
309/*
310 * INIT part
311 */
312
313static int __init alsa_sb_common_init(void)
314{
315 return 0;
316}
317
318static void __exit alsa_sb_common_exit(void)
319{
320}
321
322module_init(alsa_sb_common_init)
323module_exit(alsa_sb_common_exit)