1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for SoundBlaster 1.0/2.0/Pro soundcards and compatible 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/err.h> 9 #include <linux/isa.h> 10 #include <linux/ioport.h> 11 #include <linux/module.h> 12 #include <sound/core.h> 13 #include <sound/sb.h> 14 #include <sound/opl3.h> 15 #include <sound/initval.h> 16 17 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 18 MODULE_DESCRIPTION("Sound Blaster 1.0/2.0/Pro"); 19 MODULE_LICENSE("GPL"); 20 21 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 22 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 23 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ 24 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */ 25 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ 26 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3 */ 27 28 module_param_array(index, int, NULL, 0444); 29 MODULE_PARM_DESC(index, "Index value for Sound Blaster soundcard."); 30 module_param_array(id, charp, NULL, 0444); 31 MODULE_PARM_DESC(id, "ID string for Sound Blaster soundcard."); 32 module_param_array(enable, bool, NULL, 0444); 33 MODULE_PARM_DESC(enable, "Enable Sound Blaster soundcard."); 34 module_param_hw_array(port, long, ioport, NULL, 0444); 35 MODULE_PARM_DESC(port, "Port # for SB8 driver."); 36 module_param_hw_array(irq, int, irq, NULL, 0444); 37 MODULE_PARM_DESC(irq, "IRQ # for SB8 driver."); 38 module_param_hw_array(dma8, int, dma, NULL, 0444); 39 MODULE_PARM_DESC(dma8, "8-bit DMA # for SB8 driver."); 40 41 struct snd_sb8 { 42 struct resource *fm_res; /* used to block FM i/o region for legacy cards */ 43 struct snd_sb *chip; 44 }; 45 46 static irqreturn_t snd_sb8_interrupt(int irq, void *dev_id) 47 { 48 struct snd_sb *chip = dev_id; 49 50 if (chip->open & SB_OPEN_PCM) { 51 return snd_sb8dsp_interrupt(chip); 52 } else { 53 return snd_sb8dsp_midi_interrupt(chip); 54 } 55 } 56 57 static void snd_sb8_free(struct snd_card *card) 58 { 59 struct snd_sb8 *acard = card->private_data; 60 61 if (acard == NULL) 62 return; 63 release_and_free_resource(acard->fm_res); 64 } 65 66 static int snd_sb8_match(struct device *pdev, unsigned int dev) 67 { 68 if (!enable[dev]) 69 return 0; 70 if (irq[dev] == SNDRV_AUTO_IRQ) { 71 dev_err(pdev, "please specify irq\n"); 72 return 0; 73 } 74 if (dma8[dev] == SNDRV_AUTO_DMA) { 75 dev_err(pdev, "please specify dma8\n"); 76 return 0; 77 } 78 return 1; 79 } 80 81 static int snd_sb8_probe(struct device *pdev, unsigned int dev) 82 { 83 struct snd_sb *chip; 84 struct snd_card *card; 85 struct snd_sb8 *acard; 86 struct snd_opl3 *opl3; 87 int err; 88 89 err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE, 90 sizeof(struct snd_sb8), &card); 91 if (err < 0) 92 return err; 93 acard = card->private_data; 94 card->private_free = snd_sb8_free; 95 96 /* 97 * Block the 0x388 port to avoid PnP conflicts. 98 * No need to check this value after request_region, 99 * as we never do anything with it. 100 */ 101 acard->fm_res = request_region(0x388, 4, "SoundBlaster FM"); 102 103 if (port[dev] != SNDRV_AUTO_PORT) { 104 err = snd_sbdsp_create(card, port[dev], irq[dev], 105 snd_sb8_interrupt, dma8[dev], 106 -1, SB_HW_AUTO, &chip); 107 if (err < 0) 108 goto _err; 109 } else { 110 /* auto-probe legacy ports */ 111 static const unsigned long possible_ports[] = { 112 0x220, 0x240, 0x260, 113 }; 114 int i; 115 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { 116 err = snd_sbdsp_create(card, possible_ports[i], 117 irq[dev], 118 snd_sb8_interrupt, 119 dma8[dev], 120 -1, 121 SB_HW_AUTO, 122 &chip); 123 if (err >= 0) { 124 port[dev] = possible_ports[i]; 125 break; 126 } 127 } 128 if (i >= ARRAY_SIZE(possible_ports)) { 129 err = -EINVAL; 130 goto _err; 131 } 132 } 133 acard->chip = chip; 134 135 if (chip->hardware >= SB_HW_16) { 136 if (chip->hardware == SB_HW_ALS100) 137 snd_printk(KERN_WARNING "ALS100 chip detected at 0x%lx, try snd-als100 module\n", 138 port[dev]); 139 else 140 snd_printk(KERN_WARNING "SB 16 chip detected at 0x%lx, try snd-sb16 module\n", 141 port[dev]); 142 err = -ENODEV; 143 goto _err; 144 } 145 146 err = snd_sb8dsp_pcm(chip, 0); 147 if (err < 0) 148 goto _err; 149 150 err = snd_sbmixer_new(chip); 151 if (err < 0) 152 goto _err; 153 154 if (chip->hardware == SB_HW_10 || chip->hardware == SB_HW_20) { 155 err = snd_opl3_create(card, chip->port + 8, 0, 156 OPL3_HW_AUTO, 1, &opl3); 157 if (err < 0) 158 snd_printk(KERN_WARNING "sb8: no OPL device at 0x%lx\n", chip->port + 8); 159 } else { 160 err = snd_opl3_create(card, chip->port, chip->port + 2, 161 OPL3_HW_AUTO, 1, &opl3); 162 if (err < 0) { 163 snd_printk(KERN_WARNING "sb8: no OPL device at 0x%lx-0x%lx\n", 164 chip->port, chip->port + 2); 165 } 166 } 167 if (err >= 0) { 168 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); 169 if (err < 0) 170 goto _err; 171 } 172 173 err = snd_sb8dsp_midi(chip, 0); 174 if (err < 0) 175 goto _err; 176 177 strcpy(card->driver, chip->hardware == SB_HW_PRO ? "SB Pro" : "SB8"); 178 strcpy(card->shortname, chip->name); 179 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d", 180 chip->name, 181 chip->port, 182 irq[dev], dma8[dev]); 183 184 err = snd_card_register(card); 185 if (err < 0) 186 goto _err; 187 188 dev_set_drvdata(pdev, card); 189 return 0; 190 191 _err: 192 snd_card_free(card); 193 return err; 194 } 195 196 static void snd_sb8_remove(struct device *pdev, unsigned int dev) 197 { 198 snd_card_free(dev_get_drvdata(pdev)); 199 } 200 201 #ifdef CONFIG_PM 202 static int snd_sb8_suspend(struct device *dev, unsigned int n, 203 pm_message_t state) 204 { 205 struct snd_card *card = dev_get_drvdata(dev); 206 struct snd_sb8 *acard = card->private_data; 207 struct snd_sb *chip = acard->chip; 208 209 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 210 snd_sbmixer_suspend(chip); 211 return 0; 212 } 213 214 static int snd_sb8_resume(struct device *dev, unsigned int n) 215 { 216 struct snd_card *card = dev_get_drvdata(dev); 217 struct snd_sb8 *acard = card->private_data; 218 struct snd_sb *chip = acard->chip; 219 220 snd_sbdsp_reset(chip); 221 snd_sbmixer_resume(chip); 222 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 223 return 0; 224 } 225 #endif 226 227 #define DEV_NAME "sb8" 228 229 static struct isa_driver snd_sb8_driver = { 230 .match = snd_sb8_match, 231 .probe = snd_sb8_probe, 232 .remove = snd_sb8_remove, 233 #ifdef CONFIG_PM 234 .suspend = snd_sb8_suspend, 235 .resume = snd_sb8_resume, 236 #endif 237 .driver = { 238 .name = DEV_NAME 239 }, 240 }; 241 242 module_isa_driver(snd_sb8_driver, SNDRV_CARDS); 243