1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 /* 4 card-als100.c - driver for Avance Logic ALS100 based soundcards. 5 Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it> 6 Copyright (C) 1999-2002 by Massimo Piccioni <dafastidio@libero.it> 7 8 Thanks to Pierfrancesco 'qM2' Passerini. 9 10 Generalised for soundcards based on DT-0196 and ALS-007 chips 11 by Jonathan Woithe <jwoithe@just42.net>: June 2002. 12 13 */ 14 15 #include <linux/init.h> 16 #include <linux/wait.h> 17 #include <linux/time.h> 18 #include <linux/pnp.h> 19 #include <linux/module.h> 20 #include <sound/core.h> 21 #include <sound/initval.h> 22 #include <sound/mpu401.h> 23 #include <sound/opl3.h> 24 #include <sound/sb.h> 25 26 #define PFX "als100: " 27 28 MODULE_DESCRIPTION("Avance Logic ALS007/ALS1X0"); 29 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>"); 30 MODULE_LICENSE("GPL"); 31 32 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 33 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 34 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ 35 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ 36 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ 37 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ 38 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* PnP setup */ 39 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* PnP setup */ 40 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ 41 static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ 42 43 module_param_array(index, int, NULL, 0444); 44 MODULE_PARM_DESC(index, "Index value for Avance Logic based soundcard."); 45 module_param_array(id, charp, NULL, 0444); 46 MODULE_PARM_DESC(id, "ID string for Avance Logic based soundcard."); 47 module_param_array(enable, bool, NULL, 0444); 48 MODULE_PARM_DESC(enable, "Enable Avance Logic based soundcard."); 49 50 MODULE_ALIAS("snd-dt019x"); 51 52 struct snd_card_als100 { 53 struct pnp_dev *dev; 54 struct pnp_dev *devmpu; 55 struct pnp_dev *devopl; 56 struct snd_sb *chip; 57 }; 58 59 static const struct pnp_card_device_id snd_als100_pnpids[] = { 60 /* DT197A30 */ 61 { .id = "RWB1688", 62 .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } }, 63 .driver_data = SB_HW_DT019X }, 64 /* DT0196 / ALS-007 */ 65 { .id = "ALS0007", 66 .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } }, 67 .driver_data = SB_HW_DT019X }, 68 /* ALS100 - PRO16PNP */ 69 { .id = "ALS0001", 70 .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } }, 71 .driver_data = SB_HW_ALS100 }, 72 /* ALS110 - MF1000 - Digimate 3D Sound */ 73 { .id = "ALS0110", 74 .devs = { { "@@@1001" }, { "@X@1001" }, { "@H@1001" } }, 75 .driver_data = SB_HW_ALS100 }, 76 /* ALS120 */ 77 { .id = "ALS0120", 78 .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } }, 79 .driver_data = SB_HW_ALS100 }, 80 /* ALS200 */ 81 { .id = "ALS0200", 82 .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0001" } }, 83 .driver_data = SB_HW_ALS100 }, 84 /* ALS200 OEM */ 85 { .id = "ALS0200", 86 .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0020" } }, 87 .driver_data = SB_HW_ALS100 }, 88 /* RTL3000 */ 89 { .id = "RTL3000", 90 .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } }, 91 .driver_data = SB_HW_ALS100 }, 92 { .id = "" } /* end */ 93 }; 94 95 MODULE_DEVICE_TABLE(pnp_card, snd_als100_pnpids); 96 97 static int snd_card_als100_pnp(int dev, struct snd_card_als100 *acard, 98 struct pnp_card_link *card, 99 const struct pnp_card_device_id *id) 100 { 101 struct pnp_dev *pdev; 102 int err; 103 104 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL); 105 if (acard->dev == NULL) 106 return -ENODEV; 107 108 acard->devmpu = pnp_request_card_device(card, id->devs[1].id, acard->dev); 109 acard->devopl = pnp_request_card_device(card, id->devs[2].id, acard->dev); 110 111 pdev = acard->dev; 112 113 err = pnp_activate_dev(pdev); 114 if (err < 0) { 115 snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n"); 116 return err; 117 } 118 port[dev] = pnp_port_start(pdev, 0); 119 if (id->driver_data == SB_HW_DT019X) 120 dma8[dev] = pnp_dma(pdev, 0); 121 else { 122 dma8[dev] = pnp_dma(pdev, 1); 123 dma16[dev] = pnp_dma(pdev, 0); 124 } 125 irq[dev] = pnp_irq(pdev, 0); 126 127 pdev = acard->devmpu; 128 if (pdev != NULL) { 129 err = pnp_activate_dev(pdev); 130 if (err < 0) 131 goto __mpu_error; 132 mpu_port[dev] = pnp_port_start(pdev, 0); 133 mpu_irq[dev] = pnp_irq(pdev, 0); 134 } else { 135 __mpu_error: 136 if (pdev) { 137 pnp_release_card_device(pdev); 138 snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n"); 139 } 140 acard->devmpu = NULL; 141 mpu_port[dev] = -1; 142 } 143 144 pdev = acard->devopl; 145 if (pdev != NULL) { 146 err = pnp_activate_dev(pdev); 147 if (err < 0) 148 goto __fm_error; 149 fm_port[dev] = pnp_port_start(pdev, 0); 150 } else { 151 __fm_error: 152 if (pdev) { 153 pnp_release_card_device(pdev); 154 snd_printk(KERN_ERR PFX "OPL3 pnp configure failure, skipping\n"); 155 } 156 acard->devopl = NULL; 157 fm_port[dev] = -1; 158 } 159 160 return 0; 161 } 162 163 static int snd_card_als100_probe(int dev, 164 struct pnp_card_link *pcard, 165 const struct pnp_card_device_id *pid) 166 { 167 int error; 168 struct snd_sb *chip; 169 struct snd_card *card; 170 struct snd_card_als100 *acard; 171 struct snd_opl3 *opl3; 172 173 error = snd_card_new(&pcard->card->dev, 174 index[dev], id[dev], THIS_MODULE, 175 sizeof(struct snd_card_als100), &card); 176 if (error < 0) 177 return error; 178 acard = card->private_data; 179 180 error = snd_card_als100_pnp(dev, acard, pcard, pid); 181 if (error) { 182 snd_card_free(card); 183 return error; 184 } 185 186 if (pid->driver_data == SB_HW_DT019X) 187 dma16[dev] = -1; 188 189 error = snd_sbdsp_create(card, port[dev], irq[dev], 190 snd_sb16dsp_interrupt, 191 dma8[dev], dma16[dev], 192 pid->driver_data, 193 &chip); 194 if (error < 0) { 195 snd_card_free(card); 196 return error; 197 } 198 acard->chip = chip; 199 200 if (pid->driver_data == SB_HW_DT019X) { 201 strcpy(card->driver, "DT-019X"); 202 strcpy(card->shortname, "Diamond Tech. DT-019X"); 203 snprintf(card->longname, sizeof(card->longname), 204 "Diamond Tech. DT-019X, %s at 0x%lx, irq %d, dma %d", 205 chip->name, chip->port, irq[dev], dma8[dev]); 206 } else { 207 strcpy(card->driver, "ALS100"); 208 strcpy(card->shortname, "Avance Logic ALS100"); 209 snprintf(card->longname, sizeof(card->longname), 210 "Avance Logic ALS100, %s at 0x%lx, irq %d, dma %d&%d", 211 chip->name, chip->port, irq[dev], dma8[dev], 212 dma16[dev]); 213 } 214 215 error = snd_sb16dsp_pcm(chip, 0); 216 if (error < 0) { 217 snd_card_free(card); 218 return error; 219 } 220 221 error = snd_sbmixer_new(chip); 222 if (error < 0) { 223 snd_card_free(card); 224 return error; 225 } 226 227 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) { 228 int mpu_type = MPU401_HW_ALS100; 229 230 if (mpu_irq[dev] == SNDRV_AUTO_IRQ) 231 mpu_irq[dev] = -1; 232 233 if (pid->driver_data == SB_HW_DT019X) 234 mpu_type = MPU401_HW_MPU401; 235 236 if (snd_mpu401_uart_new(card, 0, 237 mpu_type, 238 mpu_port[dev], 0, 239 mpu_irq[dev], 240 NULL) < 0) 241 snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]); 242 } 243 244 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) { 245 if (snd_opl3_create(card, 246 fm_port[dev], fm_port[dev] + 2, 247 OPL3_HW_AUTO, 0, &opl3) < 0) { 248 snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n", 249 fm_port[dev], fm_port[dev] + 2); 250 } else { 251 error = snd_opl3_timer_new(opl3, 0, 1); 252 if (error < 0) { 253 snd_card_free(card); 254 return error; 255 } 256 error = snd_opl3_hwdep_new(opl3, 0, 1, NULL); 257 if (error < 0) { 258 snd_card_free(card); 259 return error; 260 } 261 } 262 } 263 264 error = snd_card_register(card); 265 if (error < 0) { 266 snd_card_free(card); 267 return error; 268 } 269 pnp_set_card_drvdata(pcard, card); 270 return 0; 271 } 272 273 static unsigned int als100_devices; 274 275 static int snd_als100_pnp_detect(struct pnp_card_link *card, 276 const struct pnp_card_device_id *id) 277 { 278 static int dev; 279 int res; 280 281 for ( ; dev < SNDRV_CARDS; dev++) { 282 if (!enable[dev]) 283 continue; 284 res = snd_card_als100_probe(dev, card, id); 285 if (res < 0) 286 return res; 287 dev++; 288 als100_devices++; 289 return 0; 290 } 291 return -ENODEV; 292 } 293 294 static void snd_als100_pnp_remove(struct pnp_card_link *pcard) 295 { 296 snd_card_free(pnp_get_card_drvdata(pcard)); 297 pnp_set_card_drvdata(pcard, NULL); 298 } 299 300 #ifdef CONFIG_PM 301 static int snd_als100_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) 302 { 303 struct snd_card *card = pnp_get_card_drvdata(pcard); 304 struct snd_card_als100 *acard = card->private_data; 305 struct snd_sb *chip = acard->chip; 306 307 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 308 snd_sbmixer_suspend(chip); 309 return 0; 310 } 311 312 static int snd_als100_pnp_resume(struct pnp_card_link *pcard) 313 { 314 struct snd_card *card = pnp_get_card_drvdata(pcard); 315 struct snd_card_als100 *acard = card->private_data; 316 struct snd_sb *chip = acard->chip; 317 318 snd_sbdsp_reset(chip); 319 snd_sbmixer_resume(chip); 320 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 321 return 0; 322 } 323 #endif 324 325 static struct pnp_card_driver als100_pnpc_driver = { 326 .flags = PNP_DRIVER_RES_DISABLE, 327 .name = "als100", 328 .id_table = snd_als100_pnpids, 329 .probe = snd_als100_pnp_detect, 330 .remove = snd_als100_pnp_remove, 331 #ifdef CONFIG_PM 332 .suspend = snd_als100_pnp_suspend, 333 .resume = snd_als100_pnp_resume, 334 #endif 335 }; 336 337 static int __init alsa_card_als100_init(void) 338 { 339 int err; 340 341 err = pnp_register_card_driver(&als100_pnpc_driver); 342 if (err) 343 return err; 344 345 if (!als100_devices) { 346 pnp_unregister_card_driver(&als100_pnpc_driver); 347 #ifdef MODULE 348 snd_printk(KERN_ERR "no Avance Logic based soundcards found\n"); 349 #endif 350 return -ENODEV; 351 } 352 return 0; 353 } 354 355 static void __exit alsa_card_als100_exit(void) 356 { 357 pnp_unregister_card_driver(&als100_pnpc_driver); 358 } 359 360 module_init(alsa_card_als100_init) 361 module_exit(alsa_card_als100_exit) 362